blob: 204e46a08d8783f86a4fd3486897a573ae0a1c01 [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;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000073 Expr *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
Alexey Bataev758e55e2013-09-06 18:03:48 +000080private:
Samuel Antao5de996e2016-01-22 20:21:36 +000081 typedef SmallVector<Expr *, 4> MapInfo;
82
Alexey Bataev758e55e2013-09-06 18:03:48 +000083 struct DSAInfo {
84 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000085 Expr *RefExpr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000087 typedef llvm::SmallDenseMap<ValueDecl *, DSAInfo, 64> DeclSAMapTy;
88 typedef llvm::SmallDenseMap<ValueDecl *, Expr *, 64> AlignedMapTy;
89 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
90 typedef llvm::SmallDenseMap<ValueDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000091 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
92 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093
94 struct SharingMapTy {
95 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000096 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000097 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000098 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000099 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000100 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 OpenMPDirectiveKind Directive;
102 DeclarationNameInfo DirectiveName;
103 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000105 /// \brief first argument (Expr *) contains optional argument of the
106 /// 'ordered' clause, the second one is true if the regions has 'ordered'
107 /// clause, false otherwise.
108 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000109 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000110 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000111 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000112 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000113 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000114 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000115 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000116 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000117 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000118 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000121 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000122 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124 };
125
126 typedef SmallVector<SharingMapTy, 64> StackTy;
127
128 /// \brief Stack of used declaration and their data-sharing attributes.
129 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000130 /// \brief true, if check for DSA must be from parent directive, false, if
131 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000132 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000133 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000134 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000135 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
138
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000139 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000140
141 /// \brief Checks if the variable is a local for OpenMP region.
142 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000145 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000146 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
147 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000148
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
150 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000152 bool isForceVarCapturing() const { return ForceCapturing; }
153 void setForceVarCapturing(bool V) { ForceCapturing = V; }
154
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000156 Scope *CurScope, SourceLocation Loc) {
157 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
158 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 }
160
161 void pop() {
162 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
163 Stack.pop_back();
164 }
165
Alexey Bataev28c75412015-12-15 08:19:24 +0000166 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
167 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
168 }
169 const std::pair<OMPCriticalDirective *, llvm::APSInt>
170 getCriticalWithHint(const DeclarationNameInfo &Name) const {
171 auto I = Criticals.find(Name.getAsString());
172 if (I != Criticals.end())
173 return I->second;
174 return std::make_pair(nullptr, llvm::APSInt());
175 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000176 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000177 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000178 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000179 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180
Alexey Bataev9c821032015-04-30 04:23:23 +0000181 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000182 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000183 /// \brief Check if the specified variable is a loop control variable for
184 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000185 /// \return The index of the loop control variable in the list of associated
186 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000187 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000188 /// \brief Check if the specified variable is a loop control variable for
189 /// parent region.
190 /// \return The index of the loop control variable in the list of associated
191 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000192 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000193 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
194 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000195 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000196
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000198 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200 /// \brief Returns data sharing attributes from top of the stack for the
201 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000202 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000204 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000205 /// \brief Checks if the specified variables has data-sharing attributes which
206 /// match specified \a CPred predicate in any directive which matches \a DPred
207 /// predicate.
208 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000210 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000211 /// \brief Checks if the specified variables has data-sharing attributes which
212 /// match specified \a CPred predicate in any innermost directive which
213 /// matches \a DPred predicate.
214 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000215 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
216 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000217 /// \brief Checks if the specified variables has explicit data-sharing
218 /// attributes which match specified \a CPred predicate at the specified
219 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000220 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000221 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
222 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000223
224 /// \brief Returns true if the directive at level \Level matches in the
225 /// specified \a DPred predicate.
226 bool hasExplicitDirective(
227 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
228 unsigned Level);
229
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000230 /// \brief Finds a directive which matches specified \a DPred predicate.
231 template <class NamedDirectivesPredicate>
232 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000233
Alexey Bataev758e55e2013-09-06 18:03:48 +0000234 /// \brief Returns currently analyzed directive.
235 OpenMPDirectiveKind getCurrentDirective() const {
236 return Stack.back().Directive;
237 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000238 /// \brief Returns parent directive.
239 OpenMPDirectiveKind getParentDirective() const {
240 if (Stack.size() > 2)
241 return Stack[Stack.size() - 2].Directive;
242 return OMPD_unknown;
243 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000244 /// \brief Return the directive associated with the provided scope.
245 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000248 void setDefaultDSANone(SourceLocation Loc) {
249 Stack.back().DefaultAttr = DSA_none;
250 Stack.back().DefaultAttrLoc = Loc;
251 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 void setDefaultDSAShared(SourceLocation Loc) {
254 Stack.back().DefaultAttr = DSA_shared;
255 Stack.back().DefaultAttrLoc = Loc;
256 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000257
258 DefaultDataSharingAttributes getDefaultDSA() const {
259 return Stack.back().DefaultAttr;
260 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000261 SourceLocation getDefaultDSALocation() const {
262 return Stack.back().DefaultAttrLoc;
263 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264
Alexey Bataevf29276e2014-06-18 04:14:57 +0000265 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000266 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000267 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 }
270
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000271 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 void setOrderedRegion(bool IsOrdered, Expr *Param) {
273 Stack.back().OrderedRegion.setInt(IsOrdered);
274 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000275 }
276 /// \brief Returns true, if parent region is ordered (has associated
277 /// 'ordered' clause), false - otherwise.
278 bool isParentOrderedRegion() const {
279 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000280 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000281 return false;
282 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 /// \brief Returns optional parameter for the ordered region.
284 Expr *getParentOrderedRegionParam() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
287 return nullptr;
288 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000289 /// \brief Marks current region as nowait (it has a 'nowait' clause).
290 void setNowaitRegion(bool IsNowait = true) {
291 Stack.back().NowaitRegion = IsNowait;
292 }
293 /// \brief Returns true, if parent region is nowait (has associated
294 /// 'nowait' clause), false - otherwise.
295 bool isParentNowaitRegion() const {
296 if (Stack.size() > 2)
297 return Stack[Stack.size() - 2].NowaitRegion;
298 return false;
299 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000300 /// \brief Marks parent region as cancel region.
301 void setParentCancelRegion(bool Cancel = true) {
302 if (Stack.size() > 2)
303 Stack[Stack.size() - 2].CancelRegion =
304 Stack[Stack.size() - 2].CancelRegion || Cancel;
305 }
306 /// \brief Return true if current region has inner cancel construct.
307 bool isCancelRegion() const {
308 return Stack.back().CancelRegion;
309 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000310
Alexey Bataev9c821032015-04-30 04:23:23 +0000311 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000312 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000313 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000314 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000315
Alexey Bataev13314bf2014-10-09 04:18:56 +0000316 /// \brief Marks current target region as one with closely nested teams
317 /// region.
318 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
319 if (Stack.size() > 2)
320 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
321 }
322 /// \brief Returns true, if current region has closely nested teams region.
323 bool hasInnerTeamsRegion() const {
324 return getInnerTeamsRegionLoc().isValid();
325 }
326 /// \brief Returns location of the nested teams region (if any).
327 SourceLocation getInnerTeamsRegionLoc() const {
328 if (Stack.size() > 1)
329 return Stack.back().InnerTeamsRegionLoc;
330 return SourceLocation();
331 }
332
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000333 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000334 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000335 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000336
Samuel Antao5de996e2016-01-22 20:21:36 +0000337 // Do the check specified in MapInfoCheck and return true if any issue is
338 // found.
339 template <class MapInfoCheck>
340 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
341 MapInfoCheck Check) {
342 auto SI = Stack.rbegin();
343 auto SE = Stack.rend();
344
345 if (SI == SE)
346 return false;
347
348 if (CurrentRegionOnly) {
349 SE = std::next(SI);
350 } else {
351 ++SI;
352 }
353
354 for (; SI != SE; ++SI) {
355 auto MI = SI->MappedDecls.find(VD);
356 if (MI != SI->MappedDecls.end()) {
357 for (Expr *E : MI->second) {
358 if (Check(E))
359 return true;
360 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000361 }
362 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000363 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000364 }
365
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000368 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000369 }
370 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000371};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
373 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000374 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000375 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000376}
Alexey Bataeved09d242014-05-28 05:53:51 +0000377} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000378
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000379static ValueDecl *getCanonicalDecl(ValueDecl *D) {
380 auto *VD = dyn_cast<VarDecl>(D);
381 auto *FD = dyn_cast<FieldDecl>(D);
382 if (VD != nullptr) {
383 VD = VD->getCanonicalDecl();
384 D = VD;
385 } else {
386 assert(FD);
387 FD = FD->getCanonicalDecl();
388 D = FD;
389 }
390 return D;
391}
392
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000394 ValueDecl *D) {
395 D = getCanonicalDecl(D);
396 auto *VD = dyn_cast<VarDecl>(D);
397 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000399 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000400 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
401 // in a region but not in construct]
402 // File-scope or namespace-scope variables referenced in called routines
403 // in the region are shared unless they appear in a threadprivate
404 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000405 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000406 DVar.CKind = OMPC_shared;
407
408 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
409 // in a region but not in construct]
410 // Variables with static storage duration that are declared in called
411 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000412 if (VD && VD->hasGlobalStorage())
413 DVar.CKind = OMPC_shared;
414
415 // Non-static data members are shared by default.
416 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000417 DVar.CKind = OMPC_shared;
418
Alexey Bataev758e55e2013-09-06 18:03:48 +0000419 return DVar;
420 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000421
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000423 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
424 // in a Construct, C/C++, predetermined, p.1]
425 // Variables with automatic storage duration that are declared in a scope
426 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000427 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
428 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000429 DVar.CKind = OMPC_private;
430 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000431 }
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 // Explicitly specified attributes and local variables with predetermined
434 // attributes.
435 if (Iter->SharingMap.count(D)) {
436 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
437 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
441
442 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
443 // in a Construct, C/C++, implicitly determined, p.1]
444 // In a parallel or task construct, the data-sharing attributes of these
445 // variables are determined by the default clause, if present.
446 switch (Iter->DefaultAttr) {
447 case DSA_shared:
448 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000449 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450 return DVar;
451 case DSA_none:
452 return DVar;
453 case DSA_unspecified:
454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
455 // in a Construct, implicitly determined, p.2]
456 // In a parallel construct, if no default clause is present, these
457 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000458 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000459 if (isOpenMPParallelDirective(DVar.DKind) ||
460 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000461 DVar.CKind = OMPC_shared;
462 return DVar;
463 }
464
465 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
466 // in a Construct, implicitly determined, p.4]
467 // In a task construct, if no default clause is present, a variable that in
468 // the enclosing context is determined to be shared by all implicit tasks
469 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 if (DVar.DKind == OMPD_task) {
471 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000472 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000473 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000474 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
475 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 // in a Construct, implicitly determined, p.6]
477 // In a task construct, if no default clause is present, a variable
478 // whose data-sharing attribute is not determined by the rules above is
479 // firstprivate.
480 DVarTemp = getDSA(I, D);
481 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000482 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000483 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000484 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485 return DVar;
486 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000487 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000488 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 }
490 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 return DVar;
494 }
495 }
496 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
497 // in a Construct, implicitly determined, p.3]
498 // For constructs other than task, if no default clause is present, these
499 // variables inherit their data-sharing attributes from the enclosing
500 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000501 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502}
503
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000504Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000505 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000506 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000507 auto It = Stack.back().AlignedMap.find(D);
508 if (It == Stack.back().AlignedMap.end()) {
509 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
510 Stack.back().AlignedMap[D] = NewDE;
511 return nullptr;
512 } else {
513 assert(It->second && "Unexpected nullptr expr in the aligned map");
514 return It->second;
515 }
516 return nullptr;
517}
518
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000519void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000520 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000521 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000522 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000523}
524
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000526 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000527 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000528 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
529}
530
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000532 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000534 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
535 ? Stack[Stack.size() - 2].LCVMap[D]
536 : 0;
537}
538
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000539ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000540 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
541 if (Stack[Stack.size() - 2].LCVMap.size() < I)
542 return nullptr;
543 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
544 if (Pair.second == I)
545 return Pair.first;
546 }
547 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000548}
549
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000550void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A) {
551 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000552 if (A == OMPC_threadprivate) {
553 Stack[0].SharingMap[D].Attributes = A;
554 Stack[0].SharingMap[D].RefExpr = E;
555 } else {
556 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
557 Stack.back().SharingMap[D].Attributes = A;
558 Stack.back().SharingMap[D].RefExpr = E;
559 }
560}
561
Alexey Bataeved09d242014-05-28 05:53:51 +0000562bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000563 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000564 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000565 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000566 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000567 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000568 ++I;
569 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000570 if (I == E)
571 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000572 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 Scope *CurScope = getCurScope();
574 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000575 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000576 }
577 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000578 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000579 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000580}
581
Alexey Bataev39f915b82015-05-08 10:41:21 +0000582/// \brief Build a variable declaration for OpenMP loop iteration variable.
583static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000584 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000585 DeclContext *DC = SemaRef.CurContext;
586 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
587 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
588 VarDecl *Decl =
589 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000590 if (Attrs) {
591 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
592 I != E; ++I)
593 Decl->addAttr(*I);
594 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000595 Decl->setImplicit();
596 return Decl;
597}
598
599static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
600 SourceLocation Loc,
601 bool RefersToCapture = false) {
602 D->setReferenced();
603 D->markUsed(S.Context);
604 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
605 SourceLocation(), D, RefersToCapture, Loc, Ty,
606 VK_LValue);
607}
608
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000609DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
610 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000611 DSAVarData DVar;
612
613 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
614 // in a Construct, C/C++, predetermined, p.1]
615 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000616 auto *VD = dyn_cast<VarDecl>(D);
617 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
618 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000619 SemaRef.getLangOpts().OpenMPUseTLS &&
620 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000621 (VD && VD->getStorageClass() == SC_Register &&
622 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
623 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000624 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000625 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000626 }
627 if (Stack[0].SharingMap.count(D)) {
628 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
629 DVar.CKind = OMPC_threadprivate;
630 return DVar;
631 }
632
633 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000634 // in a Construct, C/C++, predetermined, p.4]
635 // Static data members are shared.
636 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
637 // in a Construct, C/C++, predetermined, p.7]
638 // Variables with static storage duration that are declared in a scope
639 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000640 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000641 DSAVarData DVarTemp =
642 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
643 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000644 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000645
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000646 DVar.CKind = OMPC_shared;
647 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000648 }
649
650 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000651 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
652 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
654 // in a Construct, C/C++, predetermined, p.6]
655 // Variables with const qualified type having no mutable member are
656 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000657 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000658 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000659 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
660 if (auto *CTD = CTSD->getSpecializedTemplate())
661 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000663 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 // Variables with const-qualified type having no mutable member may be
665 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000666 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
667 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000668 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
669 return DVar;
670
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 DVar.CKind = OMPC_shared;
672 return DVar;
673 }
674
Alexey Bataev758e55e2013-09-06 18:03:48 +0000675 // Explicitly specified attributes and local variables with predetermined
676 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000677 auto StartI = std::next(Stack.rbegin());
678 auto EndI = std::prev(Stack.rend());
679 if (FromParent && StartI != EndI) {
680 StartI = std::next(StartI);
681 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000682 auto I = std::prev(StartI);
683 if (I->SharingMap.count(D)) {
684 DVar.RefExpr = I->SharingMap[D].RefExpr;
685 DVar.CKind = I->SharingMap[D].Attributes;
686 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000687 }
688
689 return DVar;
690}
691
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000692DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
693 bool FromParent) {
694 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000695 auto StartI = Stack.rbegin();
696 auto EndI = std::prev(Stack.rend());
697 if (FromParent && StartI != EndI) {
698 StartI = std::next(StartI);
699 }
700 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701}
702
Alexey Bataevf29276e2014-06-18 04:14:57 +0000703template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000704DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000705 DirectivesPredicate DPred,
706 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000707 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000708 auto StartI = std::next(Stack.rbegin());
709 auto EndI = std::prev(Stack.rend());
710 if (FromParent && StartI != EndI) {
711 StartI = std::next(StartI);
712 }
713 for (auto I = StartI, EE = EndI; I != EE; ++I) {
714 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000715 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000716 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000717 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000718 return DVar;
719 }
720 return DSAVarData();
721}
722
Alexey Bataevf29276e2014-06-18 04:14:57 +0000723template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000725DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000726 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000727 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 auto StartI = std::next(Stack.rbegin());
729 auto EndI = std::prev(Stack.rend());
730 if (FromParent && StartI != EndI) {
731 StartI = std::next(StartI);
732 }
733 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000736 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000737 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000738 return DVar;
739 return DSAVarData();
740 }
741 return DSAVarData();
742}
743
Alexey Bataevaac108a2015-06-23 04:51:00 +0000744bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000745 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000746 unsigned Level) {
747 if (CPred(ClauseKindMode))
748 return true;
749 if (isClauseParsingMode())
750 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000752 auto StartI = Stack.rbegin();
753 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000754 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000755 return false;
756 std::advance(StartI, Level);
757 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
758 CPred(StartI->SharingMap[D].Attributes);
759}
760
Samuel Antao4be30e92015-10-02 17:14:03 +0000761bool DSAStackTy::hasExplicitDirective(
762 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
763 unsigned Level) {
764 if (isClauseParsingMode())
765 ++Level;
766 auto StartI = Stack.rbegin();
767 auto EndI = std::prev(Stack.rend());
768 if (std::distance(StartI, EndI) <= (int)Level)
769 return false;
770 std::advance(StartI, Level);
771 return DPred(StartI->Directive);
772}
773
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000774template <class NamedDirectivesPredicate>
775bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
776 auto StartI = std::next(Stack.rbegin());
777 auto EndI = std::prev(Stack.rend());
778 if (FromParent && StartI != EndI) {
779 StartI = std::next(StartI);
780 }
781 for (auto I = StartI, EE = EndI; I != EE; ++I) {
782 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
783 return true;
784 }
785 return false;
786}
787
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000788OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
789 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
790 if (I->CurScope == S)
791 return I->Directive;
792 return OMPD_unknown;
793}
794
Alexey Bataev758e55e2013-09-06 18:03:48 +0000795void Sema::InitDataSharingAttributesStack() {
796 VarDataSharingAttributesStack = new DSAStackTy(*this);
797}
798
799#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
800
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000801bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000802 const CapturedRegionScopeInfo *RSI) {
803 assert(LangOpts.OpenMP && "OpenMP is not allowed");
804
805 auto &Ctx = getASTContext();
806 bool IsByRef = true;
807
808 // Find the directive that is associated with the provided scope.
809 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000810 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000811
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +0000812 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000813 // This table summarizes how a given variable should be passed to the device
814 // given its type and the clauses where it appears. This table is based on
815 // the description in OpenMP 4.5 [2.10.4, target Construct] and
816 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
817 //
818 // =========================================================================
819 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
820 // | |(tofrom:scalar)| | pvt | | | |
821 // =========================================================================
822 // | scl | | | | - | | bycopy|
823 // | scl | | - | x | - | - | bycopy|
824 // | scl | | x | - | - | - | null |
825 // | scl | x | | | - | | byref |
826 // | scl | x | - | x | - | - | bycopy|
827 // | scl | x | x | - | - | - | null |
828 // | scl | | - | - | - | x | byref |
829 // | scl | x | - | - | - | x | byref |
830 //
831 // | agg | n.a. | | | - | | byref |
832 // | agg | n.a. | - | x | - | - | byref |
833 // | agg | n.a. | x | - | - | - | null |
834 // | agg | n.a. | - | - | - | x | byref |
835 // | agg | n.a. | - | - | - | x[] | byref |
836 //
837 // | ptr | n.a. | | | - | | bycopy|
838 // | ptr | n.a. | - | x | - | - | bycopy|
839 // | ptr | n.a. | x | - | - | - | null |
840 // | ptr | n.a. | - | - | - | x | byref |
841 // | ptr | n.a. | - | - | - | x[] | bycopy|
842 // | ptr | n.a. | - | - | x | | bycopy|
843 // | ptr | n.a. | - | - | x | x | bycopy|
844 // | ptr | n.a. | - | - | x | x[] | bycopy|
845 // =========================================================================
846 // Legend:
847 // scl - scalar
848 // ptr - pointer
849 // agg - aggregate
850 // x - applies
851 // - - invalid in this combination
852 // [] - mapped with an array section
853 // byref - should be mapped by reference
854 // byval - should be mapped by value
855 // null - initialize a local variable to null on the device
856 //
857 // Observations:
858 // - All scalar declarations that show up in a map clause have to be passed
859 // by reference, because they may have been mapped in the enclosing data
860 // environment.
861 // - If the scalar value does not fit the size of uintptr, it has to be
862 // passed by reference, regardless the result in the table above.
863 // - For pointers mapped by value that have either an implicit map or an
864 // array section, the runtime library may pass the NULL value to the
865 // device instead of the value passed to it by the compiler.
866
867 // FIXME: Right now, only implicit maps are implemented. Properly mapping
868 // values requires having the map, private, and firstprivate clauses SEMA
869 // and parsing in place, which we don't yet.
870
871 if (Ty->isReferenceType())
872 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
873 IsByRef = !Ty->isScalarType();
874 }
875
876 // When passing data by value, we need to make sure it fits the uintptr size
877 // and alignment, because the runtime library only deals with uintptr types.
878 // If it does not fit the uintptr size, we need to pass the data by reference
879 // instead.
880 if (!IsByRef &&
881 (Ctx.getTypeSizeInChars(Ty) >
882 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000883 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000884 IsByRef = true;
885
886 return IsByRef;
887}
888
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000889bool Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000890 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000891 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000892
893 // If we are attempting to capture a global variable in a directive with
894 // 'target' we return true so that this global is also mapped to the device.
895 //
896 // FIXME: If the declaration is enclosed in a 'declare target' directive,
897 // then it should not be captured. Therefore, an extra check has to be
898 // inserted here once support for 'declare target' is added.
899 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000900 auto *VD = dyn_cast<VarDecl>(D);
901 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000902 if (DSAStack->getCurrentDirective() == OMPD_target &&
903 !DSAStack->isClauseParsingMode()) {
904 return true;
905 }
906 if (DSAStack->getCurScope() &&
907 DSAStack->hasDirective(
908 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
909 SourceLocation Loc) -> bool {
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +0000910 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000911 },
912 false)) {
913 return true;
914 }
915 }
916
Alexey Bataev48977c32015-08-04 08:10:48 +0000917 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
918 (!DSAStack->isClauseParsingMode() ||
919 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000920 if (DSAStack->isLoopControlVariable(D) ||
921 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000922 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000923 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev9c821032015-04-30 04:23:23 +0000924 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000925 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000926 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
927 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000928 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000929 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000930 return DVarPrivate.CKind != OMPC_unknown;
931 }
932 return false;
933}
934
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000935bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000936 assert(LangOpts.OpenMP && "OpenMP is not allowed");
937 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000938 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000939}
940
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000941bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000942 assert(LangOpts.OpenMP && "OpenMP is not allowed");
943 // Return true if the current level is no longer enclosed in a target region.
944
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945 auto *VD = dyn_cast<VarDecl>(D);
946 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +0000947 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
948 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000949}
950
Alexey Bataeved09d242014-05-28 05:53:51 +0000951void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000952
953void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
954 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000955 Scope *CurScope, SourceLocation Loc) {
956 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 PushExpressionEvaluationContext(PotentiallyEvaluated);
958}
959
Alexey Bataevaac108a2015-06-23 04:51:00 +0000960void Sema::StartOpenMPClause(OpenMPClauseKind K) {
961 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000962}
963
Alexey Bataevaac108a2015-06-23 04:51:00 +0000964void Sema::EndOpenMPClause() {
965 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000966}
967
Alexey Bataev758e55e2013-09-06 18:03:48 +0000968void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000969 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
970 // A variable of class type (or array thereof) that appears in a lastprivate
971 // clause requires an accessible, unambiguous default constructor for the
972 // class type, unless the list item is also specified in a firstprivate
973 // clause.
974 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000975 for (auto *C : D->clauses()) {
976 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
977 SmallVector<Expr *, 8> PrivateCopies;
978 for (auto *DE : Clause->varlists()) {
979 if (DE->isValueDependent() || DE->isTypeDependent()) {
980 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000981 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000982 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000983 DE = DE->IgnoreParens();
984 VarDecl *VD = nullptr;
985 FieldDecl *FD = nullptr;
986 ValueDecl *D;
987 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
988 VD = cast<VarDecl>(DRE->getDecl());
989 D = VD;
990 } else {
991 assert(isa<MemberExpr>(DE));
992 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
993 D = FD;
994 }
995 QualType Type = D->getType().getNonReferenceType();
996 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000998 // Generate helper private variable and initialize it with the
999 // default value. The address of the original variable is replaced
1000 // by the address of the new private variable in CodeGen. This new
1001 // variable is not added to IdResolver, so the code in the OpenMP
1002 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001003 auto *VDPrivate = buildVarDecl(
1004 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001005 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001006 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1007 if (VDPrivate->isInvalidDecl())
1008 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001009 PrivateCopies.push_back(buildDeclRefExpr(
1010 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001011 } else {
1012 // The variable is also a firstprivate, so initialization sequence
1013 // for private copy is generated already.
1014 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001015 }
1016 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001018 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001019 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001020 }
1021 }
1022 }
1023
Alexey Bataev758e55e2013-09-06 18:03:48 +00001024 DSAStack->pop();
1025 DiscardCleanupsInEvaluationContext();
1026 PopExpressionEvaluationContext();
1027}
1028
Alexander Musman3276a272015-03-21 10:12:56 +00001029static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1030 Expr *NumIterations, Sema &SemaRef,
1031 Scope *S);
1032
Alexey Bataeva769e072013-03-22 06:34:35 +00001033namespace {
1034
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035class VarDeclFilterCCC : public CorrectionCandidateCallback {
1036private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001037 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001038
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001040 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001041 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042 NamedDecl *ND = Candidate.getCorrectionDecl();
1043 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1044 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001045 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1046 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001047 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050};
Alexey Bataeved09d242014-05-28 05:53:51 +00001051} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052
1053ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1054 CXXScopeSpec &ScopeSpec,
1055 const DeclarationNameInfo &Id) {
1056 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1057 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1058
1059 if (Lookup.isAmbiguous())
1060 return ExprError();
1061
1062 VarDecl *VD;
1063 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001064 if (TypoCorrection Corrected = CorrectTypo(
1065 Id, LookupOrdinaryName, CurScope, nullptr,
1066 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001067 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001068 PDiag(Lookup.empty()
1069 ? diag::err_undeclared_var_use_suggest
1070 : diag::err_omp_expected_var_arg_suggest)
1071 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001072 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001073 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001074 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1075 : diag::err_omp_expected_var_arg)
1076 << Id.getName();
1077 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001079 } else {
1080 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001081 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001082 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1083 return ExprError();
1084 }
1085 }
1086 Lookup.suppressDiagnostics();
1087
1088 // OpenMP [2.9.2, Syntax, C/C++]
1089 // Variables must be file-scope, namespace-scope, or static block-scope.
1090 if (!VD->hasGlobalStorage()) {
1091 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1093 bool IsDecl =
1094 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001095 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001096 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1097 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001098 return ExprError();
1099 }
1100
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001101 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1102 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001103 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1104 // A threadprivate directive for file-scope variables must appear outside
1105 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001106 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1107 !getCurLexicalContext()->isTranslationUnit()) {
1108 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001109 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1110 bool IsDecl =
1111 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1112 Diag(VD->getLocation(),
1113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1114 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001115 return ExprError();
1116 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001117 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1118 // A threadprivate directive for static class member variables must appear
1119 // in the class definition, in the same scope in which the member
1120 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001121 if (CanonicalVD->isStaticDataMember() &&
1122 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1123 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001124 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1125 bool IsDecl =
1126 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1127 Diag(VD->getLocation(),
1128 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1129 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001130 return ExprError();
1131 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001132 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1133 // A threadprivate directive for namespace-scope variables must appear
1134 // outside any definition or declaration other than the namespace
1135 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001136 if (CanonicalVD->getDeclContext()->isNamespace() &&
1137 (!getCurLexicalContext()->isFileContext() ||
1138 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1139 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001140 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1141 bool IsDecl =
1142 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1143 Diag(VD->getLocation(),
1144 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1145 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001146 return ExprError();
1147 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1149 // A threadprivate directive for static block-scope variables must appear
1150 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001151 if (CanonicalVD->isStaticLocal() && CurScope &&
1152 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001153 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001154 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1155 bool IsDecl =
1156 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1157 Diag(VD->getLocation(),
1158 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1159 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 return ExprError();
1161 }
1162
1163 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1164 // A threadprivate directive must lexically precede all references to any
1165 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001166 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001168 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 return ExprError();
1170 }
1171
1172 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001173 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 return DE;
1175}
1176
Alexey Bataeved09d242014-05-28 05:53:51 +00001177Sema::DeclGroupPtrTy
1178Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1179 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001181 CurContext->addDecl(D);
1182 return DeclGroupPtrTy::make(DeclGroupRef(D));
1183 }
David Blaikie0403cb12016-01-15 23:43:25 +00001184 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001185}
1186
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001187namespace {
1188class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1189 Sema &SemaRef;
1190
1191public:
1192 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1193 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1194 if (VD->hasLocalStorage()) {
1195 SemaRef.Diag(E->getLocStart(),
1196 diag::err_omp_local_var_in_threadprivate_init)
1197 << E->getSourceRange();
1198 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1199 << VD << VD->getSourceRange();
1200 return true;
1201 }
1202 }
1203 return false;
1204 }
1205 bool VisitStmt(const Stmt *S) {
1206 for (auto Child : S->children()) {
1207 if (Child && Visit(Child))
1208 return true;
1209 }
1210 return false;
1211 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001212 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001213};
1214} // namespace
1215
Alexey Bataeved09d242014-05-28 05:53:51 +00001216OMPThreadPrivateDecl *
1217Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001218 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001219 for (auto &RefExpr : VarList) {
1220 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001221 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1222 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001223
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001224 QualType QType = VD->getType();
1225 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1226 // It will be analyzed later.
1227 Vars.push_back(DE);
1228 continue;
1229 }
1230
Alexey Bataeva769e072013-03-22 06:34:35 +00001231 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1232 // A threadprivate variable must not have an incomplete type.
1233 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001234 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001235 continue;
1236 }
1237
1238 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1239 // A threadprivate variable must not have a reference type.
1240 if (VD->getType()->isReferenceType()) {
1241 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001242 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1243 bool IsDecl =
1244 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1245 Diag(VD->getLocation(),
1246 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1247 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 continue;
1249 }
1250
Samuel Antaof8b50122015-07-13 22:54:53 +00001251 // Check if this is a TLS variable. If TLS is not being supported, produce
1252 // the corresponding diagnostic.
1253 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1254 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1255 getLangOpts().OpenMPUseTLS &&
1256 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001257 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1258 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001259 Diag(ILoc, diag::err_omp_var_thread_local)
1260 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001261 bool IsDecl =
1262 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1263 Diag(VD->getLocation(),
1264 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1265 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001266 continue;
1267 }
1268
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001269 // Check if initial value of threadprivate variable reference variable with
1270 // local storage (it is not supported by runtime).
1271 if (auto Init = VD->getAnyInitializer()) {
1272 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001273 if (Checker.Visit(Init))
1274 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001275 }
1276
Alexey Bataeved09d242014-05-28 05:53:51 +00001277 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001278 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001279 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1280 Context, SourceRange(Loc, Loc)));
1281 if (auto *ML = Context.getASTMutationListener())
1282 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001283 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001284 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001285 if (!Vars.empty()) {
1286 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1287 Vars);
1288 D->setAccess(AS_public);
1289 }
1290 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001291}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001292
Alexey Bataev7ff55242014-06-19 09:13:45 +00001293static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001294 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001295 bool IsLoopIterVar = false) {
1296 if (DVar.RefExpr) {
1297 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1298 << getOpenMPClauseName(DVar.CKind);
1299 return;
1300 }
1301 enum {
1302 PDSA_StaticMemberShared,
1303 PDSA_StaticLocalVarShared,
1304 PDSA_LoopIterVarPrivate,
1305 PDSA_LoopIterVarLinear,
1306 PDSA_LoopIterVarLastprivate,
1307 PDSA_ConstVarShared,
1308 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001309 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001310 PDSA_LocalVarPrivate,
1311 PDSA_Implicit
1312 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001313 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001314 auto ReportLoc = D->getLocation();
1315 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001316 if (IsLoopIterVar) {
1317 if (DVar.CKind == OMPC_private)
1318 Reason = PDSA_LoopIterVarPrivate;
1319 else if (DVar.CKind == OMPC_lastprivate)
1320 Reason = PDSA_LoopIterVarLastprivate;
1321 else
1322 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001323 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1324 Reason = PDSA_TaskVarFirstprivate;
1325 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001326 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001327 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001328 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001329 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001330 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001331 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001332 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001333 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001334 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001335 ReportHint = true;
1336 Reason = PDSA_LocalVarPrivate;
1337 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001338 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001339 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001340 << Reason << ReportHint
1341 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1342 } else if (DVar.ImplicitDSALoc.isValid()) {
1343 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1344 << getOpenMPClauseName(DVar.CKind);
1345 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001346}
1347
Alexey Bataev758e55e2013-09-06 18:03:48 +00001348namespace {
1349class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1350 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001351 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001352 bool ErrorFound;
1353 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001354 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001355 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001356
Alexey Bataev758e55e2013-09-06 18:03:48 +00001357public:
1358 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001359 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001361 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1362 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001364 auto DVar = Stack->getTopDSA(VD, false);
1365 // Check if the variable has explicit DSA set and stop analysis if it so.
1366 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001367
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001368 auto ELoc = E->getExprLoc();
1369 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001370 // The default(none) clause requires that each variable that is referenced
1371 // in the construct, and does not have a predetermined data-sharing
1372 // attribute, must have its data-sharing attribute explicitly determined
1373 // by being listed in a data-sharing attribute clause.
1374 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001375 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001376 VarsWithInheritedDSA.count(VD) == 0) {
1377 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001378 return;
1379 }
1380
1381 // OpenMP [2.9.3.6, Restrictions, p.2]
1382 // A list item that appears in a reduction clause of the innermost
1383 // enclosing worksharing or parallel construct may not be accessed in an
1384 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001385 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001386 [](OpenMPDirectiveKind K) -> bool {
1387 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001388 isOpenMPWorksharingDirective(K) ||
1389 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001390 },
1391 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001392 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1393 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001394 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1395 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001396 return;
1397 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001398
1399 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001400 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001401 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001402 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001403 }
1404 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001405 void VisitMemberExpr(MemberExpr *E) {
1406 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1407 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1408 auto DVar = Stack->getTopDSA(FD, false);
1409 // Check if the variable has explicit DSA set and stop analysis if it
1410 // so.
1411 if (DVar.RefExpr)
1412 return;
1413
1414 auto ELoc = E->getExprLoc();
1415 auto DKind = Stack->getCurrentDirective();
1416 // OpenMP [2.9.3.6, Restrictions, p.2]
1417 // A list item that appears in a reduction clause of the innermost
1418 // enclosing worksharing or parallel construct may not be accessed in
1419 // an
1420 // explicit task.
1421 DVar =
1422 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1423 [](OpenMPDirectiveKind K) -> bool {
1424 return isOpenMPParallelDirective(K) ||
1425 isOpenMPWorksharingDirective(K) ||
1426 isOpenMPTeamsDirective(K);
1427 },
1428 false);
1429 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1430 ErrorFound = true;
1431 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1432 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1433 return;
1434 }
1435
1436 // Define implicit data-sharing attributes for task.
1437 DVar = Stack->getImplicitDSA(FD, false);
1438 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1439 ImplicitFirstprivate.push_back(E);
1440 }
1441 }
1442 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001443 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 for (auto *C : S->clauses()) {
1445 // Skip analysis of arguments of implicitly defined firstprivate clause
1446 // for task directives.
1447 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1448 for (auto *CC : C->children()) {
1449 if (CC)
1450 Visit(CC);
1451 }
1452 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453 }
1454 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001455 for (auto *C : S->children()) {
1456 if (C && !isa<OMPExecutableDirective>(C))
1457 Visit(C);
1458 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001459 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001460
1461 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001462 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001463 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001464 return VarsWithInheritedDSA;
1465 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001466
Alexey Bataev7ff55242014-06-19 09:13:45 +00001467 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1468 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469};
Alexey Bataeved09d242014-05-28 05:53:51 +00001470} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataevbae9a792014-06-27 10:37:06 +00001472void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001473 switch (DKind) {
1474 case OMPD_parallel: {
1475 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001476 QualType KmpInt32PtrTy =
1477 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001478 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001479 std::make_pair(".global_tid.", KmpInt32PtrTy),
1480 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1481 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001482 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001483 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1484 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001485 break;
1486 }
1487 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001488 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001489 std::make_pair(StringRef(), QualType()) // __context with shared vars
1490 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001491 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1492 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001493 break;
1494 }
1495 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001496 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001497 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1500 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 break;
1502 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001503 case OMPD_for_simd: {
1504 Sema::CapturedParamNameType Params[] = {
1505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
1507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
1509 break;
1510 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001511 case OMPD_sections: {
1512 Sema::CapturedParamNameType Params[] = {
1513 std::make_pair(StringRef(), QualType()) // __context with shared vars
1514 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1516 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001517 break;
1518 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001519 case OMPD_section: {
1520 Sema::CapturedParamNameType Params[] = {
1521 std::make_pair(StringRef(), QualType()) // __context with shared vars
1522 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001523 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1524 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001525 break;
1526 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001527 case OMPD_single: {
1528 Sema::CapturedParamNameType Params[] = {
1529 std::make_pair(StringRef(), QualType()) // __context with shared vars
1530 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001531 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1532 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001533 break;
1534 }
Alexander Musman80c22892014-07-17 08:54:58 +00001535 case OMPD_master: {
1536 Sema::CapturedParamNameType Params[] = {
1537 std::make_pair(StringRef(), QualType()) // __context with shared vars
1538 };
1539 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1540 Params);
1541 break;
1542 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001543 case OMPD_critical: {
1544 Sema::CapturedParamNameType Params[] = {
1545 std::make_pair(StringRef(), QualType()) // __context with shared vars
1546 };
1547 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1548 Params);
1549 break;
1550 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001551 case OMPD_parallel_for: {
1552 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001553 QualType KmpInt32PtrTy =
1554 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001555 Sema::CapturedParamNameType Params[] = {
1556 std::make_pair(".global_tid.", KmpInt32PtrTy),
1557 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1558 std::make_pair(StringRef(), QualType()) // __context with shared vars
1559 };
1560 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1561 Params);
1562 break;
1563 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001564 case OMPD_parallel_for_simd: {
1565 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001566 QualType KmpInt32PtrTy =
1567 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001568 Sema::CapturedParamNameType Params[] = {
1569 std::make_pair(".global_tid.", KmpInt32PtrTy),
1570 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1571 std::make_pair(StringRef(), QualType()) // __context with shared vars
1572 };
1573 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1574 Params);
1575 break;
1576 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001577 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001578 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001579 QualType KmpInt32PtrTy =
1580 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001581 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001582 std::make_pair(".global_tid.", KmpInt32PtrTy),
1583 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001584 std::make_pair(StringRef(), QualType()) // __context with shared vars
1585 };
1586 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1587 Params);
1588 break;
1589 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001590 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001591 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001592 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1593 FunctionProtoType::ExtProtoInfo EPI;
1594 EPI.Variadic = true;
1595 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001597 std::make_pair(".global_tid.", KmpInt32Ty),
1598 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001599 std::make_pair(".privates.",
1600 Context.VoidPtrTy.withConst().withRestrict()),
1601 std::make_pair(
1602 ".copy_fn.",
1603 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001604 std::make_pair(StringRef(), QualType()) // __context with shared vars
1605 };
1606 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1607 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001608 // Mark this captured region as inlined, because we don't use outlined
1609 // function directly.
1610 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1611 AlwaysInlineAttr::CreateImplicit(
1612 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001613 break;
1614 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001615 case OMPD_ordered: {
1616 Sema::CapturedParamNameType Params[] = {
1617 std::make_pair(StringRef(), QualType()) // __context with shared vars
1618 };
1619 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1620 Params);
1621 break;
1622 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001623 case OMPD_atomic: {
1624 Sema::CapturedParamNameType Params[] = {
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
1627 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628 Params);
1629 break;
1630 }
Michael Wong65f367f2015-07-21 13:44:28 +00001631 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001632 case OMPD_target:
1633 case OMPD_target_parallel: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001634 Sema::CapturedParamNameType Params[] = {
1635 std::make_pair(StringRef(), QualType()) // __context with shared vars
1636 };
1637 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1638 Params);
1639 break;
1640 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001641 case OMPD_teams: {
1642 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001643 QualType KmpInt32PtrTy =
1644 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001645 Sema::CapturedParamNameType Params[] = {
1646 std::make_pair(".global_tid.", KmpInt32PtrTy),
1647 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1648 std::make_pair(StringRef(), QualType()) // __context with shared vars
1649 };
1650 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1651 Params);
1652 break;
1653 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001654 case OMPD_taskgroup: {
1655 Sema::CapturedParamNameType Params[] = {
1656 std::make_pair(StringRef(), QualType()) // __context with shared vars
1657 };
1658 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1659 Params);
1660 break;
1661 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001662 case OMPD_taskloop: {
1663 Sema::CapturedParamNameType Params[] = {
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001670 case OMPD_taskloop_simd: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001678 case OMPD_distribute: {
1679 Sema::CapturedParamNameType Params[] = {
1680 std::make_pair(StringRef(), QualType()) // __context with shared vars
1681 };
1682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1683 Params);
1684 break;
1685 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001686 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001687 case OMPD_taskyield:
1688 case OMPD_barrier:
1689 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001690 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001691 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001692 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001693 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001694 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001695 llvm_unreachable("OpenMP Directive is not allowed");
1696 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001697 llvm_unreachable("Unknown OpenMP directive");
1698 }
1699}
1700
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001701StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1702 ArrayRef<OMPClause *> Clauses) {
1703 if (!S.isUsable()) {
1704 ActOnCapturedRegionError();
1705 return StmtError();
1706 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001707
1708 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001709 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001710 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001711 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001712 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001713 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001714 Clause->getClauseKind() == OMPC_copyprivate ||
1715 (getLangOpts().OpenMPUseTLS &&
1716 getASTContext().getTargetInfo().isTLSSupported() &&
1717 Clause->getClauseKind() == OMPC_copyin)) {
1718 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001719 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001720 for (auto *VarRef : Clause->children()) {
1721 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001722 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001723 }
1724 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001725 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001726 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1727 Clause->getClauseKind() == OMPC_schedule) {
1728 // Mark all variables in private list clauses as used in inner region.
1729 // Required for proper codegen of combined directives.
1730 // TODO: add processing for other clauses.
1731 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001732 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1733 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001734 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001735 if (Clause->getClauseKind() == OMPC_schedule)
1736 SC = cast<OMPScheduleClause>(Clause);
1737 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001738 OC = cast<OMPOrderedClause>(Clause);
1739 else if (Clause->getClauseKind() == OMPC_linear)
1740 LCs.push_back(cast<OMPLinearClause>(Clause));
1741 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001742 bool ErrorFound = false;
1743 // OpenMP, 2.7.1 Loop Construct, Restrictions
1744 // The nonmonotonic modifier cannot be specified if an ordered clause is
1745 // specified.
1746 if (SC &&
1747 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1748 SC->getSecondScheduleModifier() ==
1749 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1750 OC) {
1751 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1752 ? SC->getFirstScheduleModifierLoc()
1753 : SC->getSecondScheduleModifierLoc(),
1754 diag::err_omp_schedule_nonmonotonic_ordered)
1755 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1756 ErrorFound = true;
1757 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001758 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1759 for (auto *C : LCs) {
1760 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1761 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1762 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001763 ErrorFound = true;
1764 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001765 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1766 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1767 OC->getNumForLoops()) {
1768 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1769 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1770 ErrorFound = true;
1771 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001772 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001773 ActOnCapturedRegionError();
1774 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001775 }
1776 return ActOnCapturedRegionEnd(S.get());
1777}
1778
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001779static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1780 OpenMPDirectiveKind CurrentRegion,
1781 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001782 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001783 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001784 // Allowed nesting of constructs
1785 // +------------------+-----------------+------------------------------------+
1786 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1787 // +------------------+-----------------+------------------------------------+
1788 // | parallel | parallel | * |
1789 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001790 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001791 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001792 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001793 // | parallel | simd | * |
1794 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001795 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001796 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001797 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001798 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001799 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001800 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001801 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001802 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001803 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001804 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001805 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001806 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001807 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001808 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001809 // | parallel | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001810 // | parallel | target enter | * |
1811 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001812 // | parallel | target exit | * |
1813 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001814 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001815 // | parallel | cancellation | |
1816 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001817 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001818 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001819 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001820 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001821 // +------------------+-----------------+------------------------------------+
1822 // | for | parallel | * |
1823 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001824 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001825 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001826 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001827 // | for | simd | * |
1828 // | for | sections | + |
1829 // | for | section | + |
1830 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001831 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001832 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001833 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001834 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001835 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001836 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001837 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001838 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001839 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001840 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001841 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001842 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001843 // | for | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001844 // | for | target enter | * |
1845 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001846 // | for | target exit | * |
1847 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001848 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001849 // | for | cancellation | |
1850 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001851 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001852 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001853 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001854 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001855 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001856 // | master | parallel | * |
1857 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001858 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001859 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001860 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001861 // | master | simd | * |
1862 // | master | sections | + |
1863 // | master | section | + |
1864 // | master | single | + |
1865 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001866 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001867 // | master |parallel sections| * |
1868 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001869 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001870 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001871 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001872 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001873 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001874 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001875 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001876 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001877 // | master | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001878 // | master | target enter | * |
1879 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001880 // | master | target exit | * |
1881 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001882 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001883 // | master | cancellation | |
1884 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001885 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001886 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001887 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001888 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001889 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001890 // | critical | parallel | * |
1891 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001892 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001893 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001894 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001895 // | critical | simd | * |
1896 // | critical | sections | + |
1897 // | critical | section | + |
1898 // | critical | single | + |
1899 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001900 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001901 // | critical |parallel sections| * |
1902 // | critical | task | * |
1903 // | critical | taskyield | * |
1904 // | critical | barrier | + |
1905 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001906 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001907 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001908 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001909 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001910 // | critical | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001911 // | critical | target enter | * |
1912 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001913 // | critical | target exit | * |
1914 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001915 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001916 // | critical | cancellation | |
1917 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001918 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001919 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001920 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001921 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001922 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001923 // | simd | parallel | |
1924 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001925 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001926 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001927 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001928 // | simd | simd | |
1929 // | simd | sections | |
1930 // | simd | section | |
1931 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001932 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001933 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001935 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001936 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001937 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001938 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001939 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001940 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001941 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001942 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001943 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001944 // | simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001945 // | simd | target enter | |
1946 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001947 // | simd | target exit | |
1948 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001949 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001950 // | simd | cancellation | |
1951 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001952 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001953 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001954 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001955 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001956 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001957 // | for simd | parallel | |
1958 // | for simd | for | |
1959 // | for simd | for simd | |
1960 // | for simd | master | |
1961 // | for simd | critical | |
1962 // | for simd | simd | |
1963 // | for simd | sections | |
1964 // | for simd | section | |
1965 // | for simd | single | |
1966 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001967 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001968 // | for simd |parallel sections| |
1969 // | for simd | task | |
1970 // | for simd | taskyield | |
1971 // | for simd | barrier | |
1972 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001973 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001974 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001975 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001976 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001977 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001978 // | for simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001979 // | for simd | target enter | |
1980 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001981 // | for simd | target exit | |
1982 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001983 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001984 // | for simd | cancellation | |
1985 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001986 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001987 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001988 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001989 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001990 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001991 // | parallel for simd| parallel | |
1992 // | parallel for simd| for | |
1993 // | parallel for simd| for simd | |
1994 // | parallel for simd| master | |
1995 // | parallel for simd| critical | |
1996 // | parallel for simd| simd | |
1997 // | parallel for simd| sections | |
1998 // | parallel for simd| section | |
1999 // | parallel for simd| single | |
2000 // | parallel for simd| parallel for | |
2001 // | parallel for simd|parallel for simd| |
2002 // | parallel for simd|parallel sections| |
2003 // | parallel for simd| task | |
2004 // | parallel for simd| taskyield | |
2005 // | parallel for simd| barrier | |
2006 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002007 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002008 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002009 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002010 // | parallel for simd| atomic | |
2011 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002012 // | parallel for simd| target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002013 // | parallel for simd| target enter | |
2014 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002015 // | parallel for simd| target exit | |
2016 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002017 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002018 // | parallel for simd| cancellation | |
2019 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002020 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002021 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002022 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002023 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002024 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002025 // | sections | parallel | * |
2026 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002027 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002028 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002029 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002030 // | sections | simd | * |
2031 // | sections | sections | + |
2032 // | sections | section | * |
2033 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002034 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002035 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002036 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002037 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002038 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002039 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002040 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002041 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002042 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002043 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002044 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002045 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002046 // | sections | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002047 // | sections | target enter | * |
2048 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002049 // | sections | target exit | * |
2050 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002051 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002052 // | sections | cancellation | |
2053 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002054 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002055 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002056 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002057 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002058 // +------------------+-----------------+------------------------------------+
2059 // | section | parallel | * |
2060 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002061 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002062 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002063 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002064 // | section | simd | * |
2065 // | section | sections | + |
2066 // | section | section | + |
2067 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002068 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002069 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002070 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002071 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002072 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002073 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002074 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002075 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002076 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002077 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002078 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002079 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002080 // | section | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002081 // | section | target enter | * |
2082 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002083 // | section | target exit | * |
2084 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002085 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002086 // | section | cancellation | |
2087 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002088 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002089 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002090 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002091 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002092 // +------------------+-----------------+------------------------------------+
2093 // | single | parallel | * |
2094 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002095 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002096 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002097 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002098 // | single | simd | * |
2099 // | single | sections | + |
2100 // | single | section | + |
2101 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002102 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002103 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002104 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002105 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002106 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002107 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002108 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002109 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002110 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002111 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002112 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002113 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002114 // | single | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002115 // | single | target enter | * |
2116 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002117 // | single | target exit | * |
2118 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002119 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002120 // | single | cancellation | |
2121 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002122 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002123 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002124 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002125 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002126 // +------------------+-----------------+------------------------------------+
2127 // | parallel for | parallel | * |
2128 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002129 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002130 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002131 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002132 // | parallel for | simd | * |
2133 // | parallel for | sections | + |
2134 // | parallel for | section | + |
2135 // | parallel for | single | + |
2136 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002137 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002138 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002139 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002140 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002141 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002142 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002143 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002144 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002145 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002146 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002147 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002148 // | parallel for | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002149 // | parallel for | target enter | * |
2150 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002151 // | parallel for | target exit | * |
2152 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002153 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002154 // | parallel for | cancellation | |
2155 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002156 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002157 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002158 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002159 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002160 // +------------------+-----------------+------------------------------------+
2161 // | parallel sections| parallel | * |
2162 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002163 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002164 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002165 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002166 // | parallel sections| simd | * |
2167 // | parallel sections| sections | + |
2168 // | parallel sections| section | * |
2169 // | parallel sections| single | + |
2170 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002171 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002172 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002173 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002174 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002175 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002176 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002177 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002178 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002179 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002180 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002181 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002182 // | parallel sections| target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002183 // | parallel sections| target enter | * |
2184 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002185 // | parallel sections| target exit | * |
2186 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002187 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002188 // | parallel sections| cancellation | |
2189 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002190 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002191 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002192 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002193 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002194 // +------------------+-----------------+------------------------------------+
2195 // | task | parallel | * |
2196 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002197 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002198 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002199 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002200 // | task | simd | * |
2201 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002202 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002203 // | task | single | + |
2204 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002205 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002206 // | task |parallel sections| * |
2207 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002208 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002209 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002210 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002211 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002212 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002213 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002214 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002215 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002216 // | task | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002217 // | task | target enter | * |
2218 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002219 // | task | target exit | * |
2220 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002221 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002222 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002223 // | | point | ! |
2224 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002225 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002226 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002227 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002228 // +------------------+-----------------+------------------------------------+
2229 // | ordered | parallel | * |
2230 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002231 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002232 // | ordered | master | * |
2233 // | ordered | critical | * |
2234 // | ordered | simd | * |
2235 // | ordered | sections | + |
2236 // | ordered | section | + |
2237 // | ordered | single | + |
2238 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002239 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002240 // | ordered |parallel sections| * |
2241 // | ordered | task | * |
2242 // | ordered | taskyield | * |
2243 // | ordered | barrier | + |
2244 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002245 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002246 // | ordered | flush | * |
2247 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002248 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002249 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002250 // | ordered | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002251 // | ordered | target enter | * |
2252 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002253 // | ordered | target exit | * |
2254 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002255 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002256 // | ordered | cancellation | |
2257 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002258 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002259 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002260 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002261 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002262 // +------------------+-----------------+------------------------------------+
2263 // | atomic | parallel | |
2264 // | atomic | for | |
2265 // | atomic | for simd | |
2266 // | atomic | master | |
2267 // | atomic | critical | |
2268 // | atomic | simd | |
2269 // | atomic | sections | |
2270 // | atomic | section | |
2271 // | atomic | single | |
2272 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002273 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002274 // | atomic |parallel sections| |
2275 // | atomic | task | |
2276 // | atomic | taskyield | |
2277 // | atomic | barrier | |
2278 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002279 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002280 // | atomic | flush | |
2281 // | atomic | ordered | |
2282 // | atomic | atomic | |
2283 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002284 // | atomic | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002285 // | atomic | target enter | |
2286 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002287 // | atomic | target exit | |
2288 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002289 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002290 // | atomic | cancellation | |
2291 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002292 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002293 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002294 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002295 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002296 // +------------------+-----------------+------------------------------------+
2297 // | target | parallel | * |
2298 // | target | for | * |
2299 // | target | for simd | * |
2300 // | target | master | * |
2301 // | target | critical | * |
2302 // | target | simd | * |
2303 // | target | sections | * |
2304 // | target | section | * |
2305 // | target | single | * |
2306 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002307 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002308 // | target |parallel sections| * |
2309 // | target | task | * |
2310 // | target | taskyield | * |
2311 // | target | barrier | * |
2312 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002313 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002314 // | target | flush | * |
2315 // | target | ordered | * |
2316 // | target | atomic | * |
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002317 // | target | target | |
2318 // | target | target parallel | |
2319 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002320 // | | data | |
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002321 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002322 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002323 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002324 // | target | cancellation | |
2325 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002326 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002327 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002328 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002329 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002330 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002331 // | target parallel | parallel | * |
2332 // | target parallel | for | * |
2333 // | target parallel | for simd | * |
2334 // | target parallel | master | * |
2335 // | target parallel | critical | * |
2336 // | target parallel | simd | * |
2337 // | target parallel | sections | * |
2338 // | target parallel | section | * |
2339 // | target parallel | single | * |
2340 // | target parallel | parallel for | * |
2341 // | target parallel |parallel for simd| * |
2342 // | target parallel |parallel sections| * |
2343 // | target parallel | task | * |
2344 // | target parallel | taskyield | * |
2345 // | target parallel | barrier | * |
2346 // | target parallel | taskwait | * |
2347 // | target parallel | taskgroup | * |
2348 // | target parallel | flush | * |
2349 // | target parallel | ordered | * |
2350 // | target parallel | atomic | * |
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002351 // | target parallel | target | |
2352 // | target parallel | target parallel | |
2353 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002354 // | | data | |
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002355 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002356 // | | data | |
2357 // | target parallel | teams | |
2358 // | target parallel | cancellation | |
2359 // | | point | ! |
2360 // | target parallel | cancel | ! |
2361 // | target parallel | taskloop | * |
2362 // | target parallel | taskloop simd | * |
2363 // | target parallel | distribute | |
2364 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002365 // | teams | parallel | * |
2366 // | teams | for | + |
2367 // | teams | for simd | + |
2368 // | teams | master | + |
2369 // | teams | critical | + |
2370 // | teams | simd | + |
2371 // | teams | sections | + |
2372 // | teams | section | + |
2373 // | teams | single | + |
2374 // | teams | parallel for | * |
2375 // | teams |parallel for simd| * |
2376 // | teams |parallel sections| * |
2377 // | teams | task | + |
2378 // | teams | taskyield | + |
2379 // | teams | barrier | + |
2380 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002381 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002382 // | teams | flush | + |
2383 // | teams | ordered | + |
2384 // | teams | atomic | + |
2385 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002386 // | teams | target parallel | + |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002387 // | teams | target enter | + |
2388 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002389 // | teams | target exit | + |
2390 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002391 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002392 // | teams | cancellation | |
2393 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002394 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002395 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002396 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002397 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002398 // +------------------+-----------------+------------------------------------+
2399 // | taskloop | parallel | * |
2400 // | taskloop | for | + |
2401 // | taskloop | for simd | + |
2402 // | taskloop | master | + |
2403 // | taskloop | critical | * |
2404 // | taskloop | simd | * |
2405 // | taskloop | sections | + |
2406 // | taskloop | section | + |
2407 // | taskloop | single | + |
2408 // | taskloop | parallel for | * |
2409 // | taskloop |parallel for simd| * |
2410 // | taskloop |parallel sections| * |
2411 // | taskloop | task | * |
2412 // | taskloop | taskyield | * |
2413 // | taskloop | barrier | + |
2414 // | taskloop | taskwait | * |
2415 // | taskloop | taskgroup | * |
2416 // | taskloop | flush | * |
2417 // | taskloop | ordered | + |
2418 // | taskloop | atomic | * |
2419 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002420 // | taskloop | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002421 // | taskloop | target enter | * |
2422 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002423 // | taskloop | target exit | * |
2424 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002425 // | taskloop | teams | + |
2426 // | taskloop | cancellation | |
2427 // | | point | |
2428 // | taskloop | cancel | |
2429 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002430 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002431 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002432 // | taskloop simd | parallel | |
2433 // | taskloop simd | for | |
2434 // | taskloop simd | for simd | |
2435 // | taskloop simd | master | |
2436 // | taskloop simd | critical | |
2437 // | taskloop simd | simd | |
2438 // | taskloop simd | sections | |
2439 // | taskloop simd | section | |
2440 // | taskloop simd | single | |
2441 // | taskloop simd | parallel for | |
2442 // | taskloop simd |parallel for simd| |
2443 // | taskloop simd |parallel sections| |
2444 // | taskloop simd | task | |
2445 // | taskloop simd | taskyield | |
2446 // | taskloop simd | barrier | |
2447 // | taskloop simd | taskwait | |
2448 // | taskloop simd | taskgroup | |
2449 // | taskloop simd | flush | |
2450 // | taskloop simd | ordered | + (with simd clause) |
2451 // | taskloop simd | atomic | |
2452 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002453 // | taskloop simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002454 // | taskloop simd | target enter | |
2455 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002456 // | taskloop simd | target exit | |
2457 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002458 // | taskloop simd | teams | |
2459 // | taskloop simd | cancellation | |
2460 // | | point | |
2461 // | taskloop simd | cancel | |
2462 // | taskloop simd | taskloop | |
2463 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002464 // | taskloop simd | distribute | |
2465 // +------------------+-----------------+------------------------------------+
2466 // | distribute | parallel | * |
2467 // | distribute | for | * |
2468 // | distribute | for simd | * |
2469 // | distribute | master | * |
2470 // | distribute | critical | * |
2471 // | distribute | simd | * |
2472 // | distribute | sections | * |
2473 // | distribute | section | * |
2474 // | distribute | single | * |
2475 // | distribute | parallel for | * |
2476 // | distribute |parallel for simd| * |
2477 // | distribute |parallel sections| * |
2478 // | distribute | task | * |
2479 // | distribute | taskyield | * |
2480 // | distribute | barrier | * |
2481 // | distribute | taskwait | * |
2482 // | distribute | taskgroup | * |
2483 // | distribute | flush | * |
2484 // | distribute | ordered | + |
2485 // | distribute | atomic | * |
2486 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002487 // | distribute | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002488 // | distribute | target enter | |
2489 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002490 // | distribute | target exit | |
2491 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002492 // | distribute | teams | |
2493 // | distribute | cancellation | + |
2494 // | | point | |
2495 // | distribute | cancel | + |
2496 // | distribute | taskloop | * |
2497 // | distribute | taskloop simd | * |
2498 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002499 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002500 if (Stack->getCurScope()) {
2501 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002502 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002503 bool NestingProhibited = false;
2504 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002505 enum {
2506 NoRecommend,
2507 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002508 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002509 ShouldBeInTargetRegion,
2510 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002511 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002512 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002513 // OpenMP [2.16, Nesting of Regions]
2514 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002515 // OpenMP [2.8.1,simd Construct, Restrictions]
2516 // An ordered construct with the simd clause is the only OpenMP construct
2517 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002518 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2519 return true;
2520 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002521 if (ParentRegion == OMPD_atomic) {
2522 // OpenMP [2.16, Nesting of Regions]
2523 // OpenMP constructs may not be nested inside an atomic region.
2524 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2525 return true;
2526 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002527 if (CurrentRegion == OMPD_section) {
2528 // OpenMP [2.7.2, sections Construct, Restrictions]
2529 // Orphaned section directives are prohibited. That is, the section
2530 // directives must appear within the sections construct and must not be
2531 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002532 if (ParentRegion != OMPD_sections &&
2533 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002534 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2535 << (ParentRegion != OMPD_unknown)
2536 << getOpenMPDirectiveName(ParentRegion);
2537 return true;
2538 }
2539 return false;
2540 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002541 // Allow some constructs to be orphaned (they could be used in functions,
2542 // called from OpenMP regions with the required preconditions).
2543 if (ParentRegion == OMPD_unknown)
2544 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002545 if (CurrentRegion == OMPD_cancellation_point ||
2546 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002547 // OpenMP [2.16, Nesting of Regions]
2548 // A cancellation point construct for which construct-type-clause is
2549 // taskgroup must be nested inside a task construct. A cancellation
2550 // point construct for which construct-type-clause is not taskgroup must
2551 // be closely nested inside an OpenMP construct that matches the type
2552 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002553 // A cancel construct for which construct-type-clause is taskgroup must be
2554 // nested inside a task construct. A cancel construct for which
2555 // construct-type-clause is not taskgroup must be closely nested inside an
2556 // OpenMP construct that matches the type specified in
2557 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002558 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002559 !((CancelRegion == OMPD_parallel &&
2560 (ParentRegion == OMPD_parallel ||
2561 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002562 (CancelRegion == OMPD_for &&
2563 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002564 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2565 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002566 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2567 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002568 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002569 // OpenMP [2.16, Nesting of Regions]
2570 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002571 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002572 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002573 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002574 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002575 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2576 // OpenMP [2.16, Nesting of Regions]
2577 // A critical region may not be nested (closely or otherwise) inside a
2578 // critical region with the same name. Note that this restriction is not
2579 // sufficient to prevent deadlock.
2580 SourceLocation PreviousCriticalLoc;
2581 bool DeadLock =
2582 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2583 OpenMPDirectiveKind K,
2584 const DeclarationNameInfo &DNI,
2585 SourceLocation Loc)
2586 ->bool {
2587 if (K == OMPD_critical &&
2588 DNI.getName() == CurrentName.getName()) {
2589 PreviousCriticalLoc = Loc;
2590 return true;
2591 } else
2592 return false;
2593 },
2594 false /* skip top directive */);
2595 if (DeadLock) {
2596 SemaRef.Diag(StartLoc,
2597 diag::err_omp_prohibited_region_critical_same_name)
2598 << CurrentName.getName();
2599 if (PreviousCriticalLoc.isValid())
2600 SemaRef.Diag(PreviousCriticalLoc,
2601 diag::note_omp_previous_critical_region);
2602 return true;
2603 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002604 } else if (CurrentRegion == OMPD_barrier) {
2605 // OpenMP [2.16, Nesting of Regions]
2606 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002607 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002608 NestingProhibited =
2609 isOpenMPWorksharingDirective(ParentRegion) ||
2610 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002611 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002612 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002613 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002614 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002615 // OpenMP [2.16, Nesting of Regions]
2616 // A worksharing region may not be closely nested inside a worksharing,
2617 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002618 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002619 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002620 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002621 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002622 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002623 Recommend = ShouldBeInParallelRegion;
2624 } else if (CurrentRegion == OMPD_ordered) {
2625 // OpenMP [2.16, Nesting of Regions]
2626 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002627 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002628 // An ordered region must be closely nested inside a loop region (or
2629 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002630 // OpenMP [2.8.1,simd Construct, Restrictions]
2631 // An ordered construct with the simd clause is the only OpenMP construct
2632 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002633 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002634 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002635 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002636 !(isOpenMPSimdDirective(ParentRegion) ||
2637 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002638 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002639 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2640 // OpenMP [2.16, Nesting of Regions]
2641 // If specified, a teams construct must be contained within a target
2642 // construct.
2643 NestingProhibited = ParentRegion != OMPD_target;
2644 Recommend = ShouldBeInTargetRegion;
2645 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2646 }
2647 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2648 // OpenMP [2.16, Nesting of Regions]
2649 // distribute, parallel, parallel sections, parallel workshare, and the
2650 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2651 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002652 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2653 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002654 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002655 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002656 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2657 // OpenMP 4.5 [2.17 Nesting of Regions]
2658 // The region associated with the distribute construct must be strictly
2659 // nested inside a teams region
2660 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2661 Recommend = ShouldBeInTeamsRegion;
2662 }
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002663 if (!NestingProhibited &&
2664 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2665 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2666 // OpenMP 4.5 [2.17 Nesting of Regions]
2667 // If a target, target update, target data, target enter data, or
2668 // target exit data construct is encountered during execution of a
2669 // target region, the behavior is unspecified.
2670 OpenMPDirectiveKind PreviousTargetExecutionDirective;
2671 NestingProhibited = Stack->hasDirective(
2672 [&PreviousTargetExecutionDirective](OpenMPDirectiveKind K,
2673 const DeclarationNameInfo &DNI,
2674 SourceLocation Loc) -> bool {
2675 if (isOpenMPTargetExecutionDirective(K)) {
2676 PreviousTargetExecutionDirective = K;
2677 return true;
2678 } else
2679 return false;
2680 },
2681 false /* don't skip top directive */);
2682 CloseNesting = false;
2683 OffendingRegion = PreviousTargetExecutionDirective;
2684 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002685 if (NestingProhibited) {
2686 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacobf1958622016-02-01 16:32:47 +00002687 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2688 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002689 return true;
2690 }
2691 }
2692 return false;
2693}
2694
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002695static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2696 ArrayRef<OMPClause *> Clauses,
2697 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2698 bool ErrorFound = false;
2699 unsigned NamedModifiersNumber = 0;
2700 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2701 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002702 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002703 for (const auto *C : Clauses) {
2704 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2705 // At most one if clause without a directive-name-modifier can appear on
2706 // the directive.
2707 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2708 if (FoundNameModifiers[CurNM]) {
2709 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2710 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2711 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2712 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002713 } else if (CurNM != OMPD_unknown) {
2714 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002715 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002716 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002717 FoundNameModifiers[CurNM] = IC;
2718 if (CurNM == OMPD_unknown)
2719 continue;
2720 // Check if the specified name modifier is allowed for the current
2721 // directive.
2722 // At most one if clause with the particular directive-name-modifier can
2723 // appear on the directive.
2724 bool MatchFound = false;
2725 for (auto NM : AllowedNameModifiers) {
2726 if (CurNM == NM) {
2727 MatchFound = true;
2728 break;
2729 }
2730 }
2731 if (!MatchFound) {
2732 S.Diag(IC->getNameModifierLoc(),
2733 diag::err_omp_wrong_if_directive_name_modifier)
2734 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2735 ErrorFound = true;
2736 }
2737 }
2738 }
2739 // If any if clause on the directive includes a directive-name-modifier then
2740 // all if clauses on the directive must include a directive-name-modifier.
2741 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2742 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2743 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2744 diag::err_omp_no_more_if_clause);
2745 } else {
2746 std::string Values;
2747 std::string Sep(", ");
2748 unsigned AllowedCnt = 0;
2749 unsigned TotalAllowedNum =
2750 AllowedNameModifiers.size() - NamedModifiersNumber;
2751 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2752 ++Cnt) {
2753 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2754 if (!FoundNameModifiers[NM]) {
2755 Values += "'";
2756 Values += getOpenMPDirectiveName(NM);
2757 Values += "'";
2758 if (AllowedCnt + 2 == TotalAllowedNum)
2759 Values += " or ";
2760 else if (AllowedCnt + 1 != TotalAllowedNum)
2761 Values += Sep;
2762 ++AllowedCnt;
2763 }
2764 }
2765 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2766 diag::err_omp_unnamed_if_clause)
2767 << (TotalAllowedNum > 1) << Values;
2768 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002769 for (auto Loc : NameModifierLoc) {
2770 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2771 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002772 ErrorFound = true;
2773 }
2774 return ErrorFound;
2775}
2776
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002777StmtResult Sema::ActOnOpenMPExecutableDirective(
2778 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2779 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2780 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002781 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002782 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2783 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002784 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002785
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002786 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002787 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002788 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002789 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002790 if (AStmt) {
2791 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2792
2793 // Check default data sharing attributes for referenced variables.
2794 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2795 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2796 if (DSAChecker.isErrorFound())
2797 return StmtError();
2798 // Generate list of implicitly defined firstprivate variables.
2799 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002800
2801 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2802 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2803 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2804 SourceLocation(), SourceLocation())) {
2805 ClausesWithImplicit.push_back(Implicit);
2806 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2807 DSAChecker.getImplicitFirstprivate().size();
2808 } else
2809 ErrorFound = true;
2810 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002811 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002812
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002813 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002814 switch (Kind) {
2815 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002816 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2817 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002818 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002819 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002820 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002821 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2822 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002823 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002824 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002825 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2826 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002827 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002828 case OMPD_for_simd:
2829 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2830 EndLoc, VarsWithInheritedDSA);
2831 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002832 case OMPD_sections:
2833 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2834 EndLoc);
2835 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002836 case OMPD_section:
2837 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002838 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002839 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2840 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002841 case OMPD_single:
2842 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2843 EndLoc);
2844 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002845 case OMPD_master:
2846 assert(ClausesWithImplicit.empty() &&
2847 "No clauses are allowed for 'omp master' directive");
2848 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2849 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002850 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002851 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2852 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002853 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002854 case OMPD_parallel_for:
2855 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2856 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002857 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002858 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002859 case OMPD_parallel_for_simd:
2860 Res = ActOnOpenMPParallelForSimdDirective(
2861 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002862 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002863 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002864 case OMPD_parallel_sections:
2865 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2866 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002867 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002868 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002869 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002870 Res =
2871 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002872 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002873 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002874 case OMPD_taskyield:
2875 assert(ClausesWithImplicit.empty() &&
2876 "No clauses are allowed for 'omp taskyield' directive");
2877 assert(AStmt == nullptr &&
2878 "No associated statement allowed for 'omp taskyield' directive");
2879 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2880 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002881 case OMPD_barrier:
2882 assert(ClausesWithImplicit.empty() &&
2883 "No clauses are allowed for 'omp barrier' directive");
2884 assert(AStmt == nullptr &&
2885 "No associated statement allowed for 'omp barrier' directive");
2886 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2887 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002888 case OMPD_taskwait:
2889 assert(ClausesWithImplicit.empty() &&
2890 "No clauses are allowed for 'omp taskwait' directive");
2891 assert(AStmt == nullptr &&
2892 "No associated statement allowed for 'omp taskwait' directive");
2893 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2894 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002895 case OMPD_taskgroup:
2896 assert(ClausesWithImplicit.empty() &&
2897 "No clauses are allowed for 'omp taskgroup' directive");
2898 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2899 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002900 case OMPD_flush:
2901 assert(AStmt == nullptr &&
2902 "No associated statement allowed for 'omp flush' directive");
2903 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2904 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002905 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002906 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2907 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002908 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002909 case OMPD_atomic:
2910 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2911 EndLoc);
2912 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002913 case OMPD_teams:
2914 Res =
2915 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2916 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002917 case OMPD_target:
2918 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2919 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002920 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002921 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002922 case OMPD_target_parallel:
2923 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2924 StartLoc, EndLoc);
2925 AllowedNameModifiers.push_back(OMPD_target);
2926 AllowedNameModifiers.push_back(OMPD_parallel);
2927 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002928 case OMPD_cancellation_point:
2929 assert(ClausesWithImplicit.empty() &&
2930 "No clauses are allowed for 'omp cancellation point' directive");
2931 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2932 "cancellation point' directive");
2933 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2934 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002935 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002936 assert(AStmt == nullptr &&
2937 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002938 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2939 CancelRegion);
2940 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002941 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002942 case OMPD_target_data:
2943 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2944 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002945 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002946 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002947 case OMPD_target_enter_data:
2948 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2949 EndLoc);
2950 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2951 break;
Samuel Antao72590762016-01-19 20:04:50 +00002952 case OMPD_target_exit_data:
2953 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2954 EndLoc);
2955 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2956 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002957 case OMPD_taskloop:
2958 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2959 EndLoc, VarsWithInheritedDSA);
2960 AllowedNameModifiers.push_back(OMPD_taskloop);
2961 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002962 case OMPD_taskloop_simd:
2963 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2964 EndLoc, VarsWithInheritedDSA);
2965 AllowedNameModifiers.push_back(OMPD_taskloop);
2966 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002967 case OMPD_distribute:
2968 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2969 EndLoc, VarsWithInheritedDSA);
2970 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002971 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002972 llvm_unreachable("OpenMP Directive is not allowed");
2973 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002974 llvm_unreachable("Unknown OpenMP directive");
2975 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002976
Alexey Bataev4acb8592014-07-07 13:01:15 +00002977 for (auto P : VarsWithInheritedDSA) {
2978 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2979 << P.first << P.second->getSourceRange();
2980 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002981 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2982
2983 if (!AllowedNameModifiers.empty())
2984 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2985 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002986
Alexey Bataeved09d242014-05-28 05:53:51 +00002987 if (ErrorFound)
2988 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002989 return Res;
2990}
2991
2992StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2993 Stmt *AStmt,
2994 SourceLocation StartLoc,
2995 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002996 if (!AStmt)
2997 return StmtError();
2998
Alexey Bataev9959db52014-05-06 10:08:46 +00002999 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3000 // 1.2.2 OpenMP Language Terminology
3001 // Structured block - An executable statement with a single entry at the
3002 // top and a single exit at the bottom.
3003 // The point of exit cannot be a branch out of the structured block.
3004 // longjmp() and throw() must not violate the entry/exit criteria.
3005 CS->getCapturedDecl()->setNothrow();
3006
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003007 getCurFunction()->setHasBranchProtectedScope();
3008
Alexey Bataev25e5b442015-09-15 12:52:43 +00003009 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3010 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003011}
3012
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003013namespace {
3014/// \brief Helper class for checking canonical form of the OpenMP loops and
3015/// extracting iteration space of each loop in the loop nest, that will be used
3016/// for IR generation.
3017class OpenMPIterationSpaceChecker {
3018 /// \brief Reference to Sema.
3019 Sema &SemaRef;
3020 /// \brief A location for diagnostics (when there is no some better location).
3021 SourceLocation DefaultLoc;
3022 /// \brief A location for diagnostics (when increment is not compatible).
3023 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003024 /// \brief A source location for referring to loop init later.
3025 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003026 /// \brief A source location for referring to condition later.
3027 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003028 /// \brief A source location for referring to increment later.
3029 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003030 /// \brief Loop variable.
3031 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003032 /// \brief Reference to loop variable.
3033 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003034 /// \brief Lower bound (initializer for the var).
3035 Expr *LB;
3036 /// \brief Upper bound.
3037 Expr *UB;
3038 /// \brief Loop step (increment).
3039 Expr *Step;
3040 /// \brief This flag is true when condition is one of:
3041 /// Var < UB
3042 /// Var <= UB
3043 /// UB > Var
3044 /// UB >= Var
3045 bool TestIsLessOp;
3046 /// \brief This flag is true when condition is strict ( < or > ).
3047 bool TestIsStrictOp;
3048 /// \brief This flag is true when step is subtracted on each iteration.
3049 bool SubtractStep;
3050
3051public:
3052 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3053 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003054 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3055 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003056 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3057 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003058 /// \brief Check init-expr for canonical loop form and save loop counter
3059 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003060 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003061 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3062 /// for less/greater and for strict/non-strict comparison.
3063 bool CheckCond(Expr *S);
3064 /// \brief Check incr-expr for canonical loop form and return true if it
3065 /// does not conform, otherwise save loop step (#Step).
3066 bool CheckInc(Expr *S);
3067 /// \brief Return the loop counter variable.
3068 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003069 /// \brief Return the reference expression to loop counter variable.
3070 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003071 /// \brief Source range of the loop init.
3072 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3073 /// \brief Source range of the loop condition.
3074 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3075 /// \brief Source range of the loop increment.
3076 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3077 /// \brief True if the step should be subtracted.
3078 bool ShouldSubtractStep() const { return SubtractStep; }
3079 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003080 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003081 /// \brief Build the precondition expression for the loops.
3082 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003083 /// \brief Build reference expression to the counter be used for codegen.
3084 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003085 /// \brief Build reference expression to the private counter be used for
3086 /// codegen.
3087 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003088 /// \brief Build initization of the counter be used for codegen.
3089 Expr *BuildCounterInit() const;
3090 /// \brief Build step of the counter be used for codegen.
3091 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003092 /// \brief Return true if any expression is dependent.
3093 bool Dependent() const;
3094
3095private:
3096 /// \brief Check the right-hand side of an assignment in the increment
3097 /// expression.
3098 bool CheckIncRHS(Expr *RHS);
3099 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003100 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003101 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003102 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003103 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003104 /// \brief Helper to set loop increment.
3105 bool SetStep(Expr *NewStep, bool Subtract);
3106};
3107
3108bool OpenMPIterationSpaceChecker::Dependent() const {
3109 if (!Var) {
3110 assert(!LB && !UB && !Step);
3111 return false;
3112 }
3113 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3114 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3115}
3116
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003117template <typename T>
3118static T *getExprAsWritten(T *E) {
3119 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3120 E = ExprTemp->getSubExpr();
3121
3122 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3123 E = MTE->GetTemporaryExpr();
3124
3125 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3126 E = Binder->getSubExpr();
3127
3128 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3129 E = ICE->getSubExprAsWritten();
3130 return E->IgnoreParens();
3131}
3132
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003133bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3134 DeclRefExpr *NewVarRefExpr,
3135 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003136 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003137 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3138 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003139 if (!NewVar || !NewLB)
3140 return true;
3141 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003142 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003143 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3144 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003145 if ((Ctor->isCopyOrMoveConstructor() ||
3146 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3147 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003148 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003149 LB = NewLB;
3150 return false;
3151}
3152
3153bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003154 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003155 // State consistency checking to ensure correct usage.
3156 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3157 !TestIsLessOp && !TestIsStrictOp);
3158 if (!NewUB)
3159 return true;
3160 UB = NewUB;
3161 TestIsLessOp = LessOp;
3162 TestIsStrictOp = StrictOp;
3163 ConditionSrcRange = SR;
3164 ConditionLoc = SL;
3165 return false;
3166}
3167
3168bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3169 // State consistency checking to ensure correct usage.
3170 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3171 if (!NewStep)
3172 return true;
3173 if (!NewStep->isValueDependent()) {
3174 // Check that the step is integer expression.
3175 SourceLocation StepLoc = NewStep->getLocStart();
3176 ExprResult Val =
3177 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3178 if (Val.isInvalid())
3179 return true;
3180 NewStep = Val.get();
3181
3182 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3183 // If test-expr is of form var relational-op b and relational-op is < or
3184 // <= then incr-expr must cause var to increase on each iteration of the
3185 // loop. If test-expr is of form var relational-op b and relational-op is
3186 // > or >= then incr-expr must cause var to decrease on each iteration of
3187 // the loop.
3188 // If test-expr is of form b relational-op var and relational-op is < or
3189 // <= then incr-expr must cause var to decrease on each iteration of the
3190 // loop. If test-expr is of form b relational-op var and relational-op is
3191 // > or >= then incr-expr must cause var to increase on each iteration of
3192 // the loop.
3193 llvm::APSInt Result;
3194 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3195 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3196 bool IsConstNeg =
3197 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003198 bool IsConstPos =
3199 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003200 bool IsConstZero = IsConstant && !Result.getBoolValue();
3201 if (UB && (IsConstZero ||
3202 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003203 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003204 SemaRef.Diag(NewStep->getExprLoc(),
3205 diag::err_omp_loop_incr_not_compatible)
3206 << Var << TestIsLessOp << NewStep->getSourceRange();
3207 SemaRef.Diag(ConditionLoc,
3208 diag::note_omp_loop_cond_requres_compatible_incr)
3209 << TestIsLessOp << ConditionSrcRange;
3210 return true;
3211 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003212 if (TestIsLessOp == Subtract) {
3213 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3214 NewStep).get();
3215 Subtract = !Subtract;
3216 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003217 }
3218
3219 Step = NewStep;
3220 SubtractStep = Subtract;
3221 return false;
3222}
3223
Alexey Bataev9c821032015-04-30 04:23:23 +00003224bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003225 // Check init-expr for canonical loop form and save loop counter
3226 // variable - #Var and its initialization value - #LB.
3227 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3228 // var = lb
3229 // integer-type var = lb
3230 // random-access-iterator-type var = lb
3231 // pointer-type var = lb
3232 //
3233 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003234 if (EmitDiags) {
3235 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3236 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003237 return true;
3238 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003239 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003240 if (Expr *E = dyn_cast<Expr>(S))
3241 S = E->IgnoreParens();
3242 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3243 if (BO->getOpcode() == BO_Assign)
3244 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003245 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003246 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003247 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3248 if (DS->isSingleDecl()) {
3249 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003250 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003251 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003252 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003253 SemaRef.Diag(S->getLocStart(),
3254 diag::ext_omp_loop_not_canonical_init)
3255 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003256 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003257 }
3258 }
3259 }
3260 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3261 if (CE->getOperator() == OO_Equal)
3262 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003263 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3264 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003265
Alexey Bataev9c821032015-04-30 04:23:23 +00003266 if (EmitDiags) {
3267 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3268 << S->getSourceRange();
3269 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003270 return true;
3271}
3272
Alexey Bataev23b69422014-06-18 07:08:49 +00003273/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003274/// variable (which may be the loop variable) if possible.
3275static const VarDecl *GetInitVarDecl(const Expr *E) {
3276 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003277 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003278 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003279 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3280 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003281 if ((Ctor->isCopyOrMoveConstructor() ||
3282 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3283 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003284 E = CE->getArg(0)->IgnoreParenImpCasts();
3285 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3286 if (!DRE)
3287 return nullptr;
3288 return dyn_cast<VarDecl>(DRE->getDecl());
3289}
3290
3291bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3292 // Check test-expr for canonical form, save upper-bound UB, flags for
3293 // less/greater and for strict/non-strict comparison.
3294 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3295 // var relational-op b
3296 // b relational-op var
3297 //
3298 if (!S) {
3299 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3300 return true;
3301 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003302 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003303 SourceLocation CondLoc = S->getLocStart();
3304 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3305 if (BO->isRelationalOp()) {
3306 if (GetInitVarDecl(BO->getLHS()) == Var)
3307 return SetUB(BO->getRHS(),
3308 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3309 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3310 BO->getSourceRange(), BO->getOperatorLoc());
3311 if (GetInitVarDecl(BO->getRHS()) == Var)
3312 return SetUB(BO->getLHS(),
3313 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3314 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3315 BO->getSourceRange(), BO->getOperatorLoc());
3316 }
3317 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3318 if (CE->getNumArgs() == 2) {
3319 auto Op = CE->getOperator();
3320 switch (Op) {
3321 case OO_Greater:
3322 case OO_GreaterEqual:
3323 case OO_Less:
3324 case OO_LessEqual:
3325 if (GetInitVarDecl(CE->getArg(0)) == Var)
3326 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3327 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3328 CE->getOperatorLoc());
3329 if (GetInitVarDecl(CE->getArg(1)) == Var)
3330 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3331 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3332 CE->getOperatorLoc());
3333 break;
3334 default:
3335 break;
3336 }
3337 }
3338 }
3339 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3340 << S->getSourceRange() << Var;
3341 return true;
3342}
3343
3344bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3345 // RHS of canonical loop form increment can be:
3346 // var + incr
3347 // incr + var
3348 // var - incr
3349 //
3350 RHS = RHS->IgnoreParenImpCasts();
3351 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3352 if (BO->isAdditiveOp()) {
3353 bool IsAdd = BO->getOpcode() == BO_Add;
3354 if (GetInitVarDecl(BO->getLHS()) == Var)
3355 return SetStep(BO->getRHS(), !IsAdd);
3356 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3357 return SetStep(BO->getLHS(), false);
3358 }
3359 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3360 bool IsAdd = CE->getOperator() == OO_Plus;
3361 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3362 if (GetInitVarDecl(CE->getArg(0)) == Var)
3363 return SetStep(CE->getArg(1), !IsAdd);
3364 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3365 return SetStep(CE->getArg(0), false);
3366 }
3367 }
3368 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3369 << RHS->getSourceRange() << Var;
3370 return true;
3371}
3372
3373bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3374 // Check incr-expr for canonical loop form and return true if it
3375 // does not conform.
3376 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3377 // ++var
3378 // var++
3379 // --var
3380 // var--
3381 // var += incr
3382 // var -= incr
3383 // var = var + incr
3384 // var = incr + var
3385 // var = var - incr
3386 //
3387 if (!S) {
3388 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3389 return true;
3390 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003391 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003392 S = S->IgnoreParens();
3393 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3394 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3395 return SetStep(
3396 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3397 (UO->isDecrementOp() ? -1 : 1)).get(),
3398 false);
3399 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3400 switch (BO->getOpcode()) {
3401 case BO_AddAssign:
3402 case BO_SubAssign:
3403 if (GetInitVarDecl(BO->getLHS()) == Var)
3404 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3405 break;
3406 case BO_Assign:
3407 if (GetInitVarDecl(BO->getLHS()) == Var)
3408 return CheckIncRHS(BO->getRHS());
3409 break;
3410 default:
3411 break;
3412 }
3413 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3414 switch (CE->getOperator()) {
3415 case OO_PlusPlus:
3416 case OO_MinusMinus:
3417 if (GetInitVarDecl(CE->getArg(0)) == Var)
3418 return SetStep(
3419 SemaRef.ActOnIntegerConstant(
3420 CE->getLocStart(),
3421 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3422 false);
3423 break;
3424 case OO_PlusEqual:
3425 case OO_MinusEqual:
3426 if (GetInitVarDecl(CE->getArg(0)) == Var)
3427 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3428 break;
3429 case OO_Equal:
3430 if (GetInitVarDecl(CE->getArg(0)) == Var)
3431 return CheckIncRHS(CE->getArg(1));
3432 break;
3433 default:
3434 break;
3435 }
3436 }
3437 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3438 << S->getSourceRange() << Var;
3439 return true;
3440}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003441
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003442namespace {
3443// Transform variables declared in GNU statement expressions to new ones to
3444// avoid crash on codegen.
3445class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3446 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3447
3448public:
3449 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3450
3451 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3452 if (auto *VD = cast<VarDecl>(D))
3453 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3454 !isa<ImplicitParamDecl>(D)) {
3455 auto *NewVD = VarDecl::Create(
3456 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3457 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3458 VD->getTypeSourceInfo(), VD->getStorageClass());
3459 NewVD->setTSCSpec(VD->getTSCSpec());
3460 NewVD->setInit(VD->getInit());
3461 NewVD->setInitStyle(VD->getInitStyle());
3462 NewVD->setExceptionVariable(VD->isExceptionVariable());
3463 NewVD->setNRVOVariable(VD->isNRVOVariable());
3464 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3465 NewVD->setConstexpr(VD->isConstexpr());
3466 NewVD->setInitCapture(VD->isInitCapture());
3467 NewVD->setPreviousDeclInSameBlockScope(
3468 VD->isPreviousDeclInSameBlockScope());
3469 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003470 if (VD->hasAttrs())
3471 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003472 transformedLocalDecl(VD, NewVD);
3473 return NewVD;
3474 }
3475 return BaseTransform::TransformDefinition(Loc, D);
3476 }
3477
3478 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3479 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3480 if (E->getDecl() != NewD) {
3481 NewD->setReferenced();
3482 NewD->markUsed(SemaRef.Context);
3483 return DeclRefExpr::Create(
3484 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3485 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3486 E->getNameInfo(), E->getType(), E->getValueKind());
3487 }
3488 return BaseTransform::TransformDeclRefExpr(E);
3489 }
3490};
3491}
3492
Alexander Musmana5f070a2014-10-01 06:03:56 +00003493/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003494Expr *
3495OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3496 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003497 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003498 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003499 auto VarType = Var->getType().getNonReferenceType();
3500 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003501 SemaRef.getLangOpts().CPlusPlus) {
3502 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003503 auto *UBExpr = TestIsLessOp ? UB : LB;
3504 auto *LBExpr = TestIsLessOp ? LB : UB;
3505 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3506 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3507 if (!Upper || !Lower)
3508 return nullptr;
3509 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3510 Sema::AA_Converting,
3511 /*AllowExplicit=*/true)
3512 .get();
3513 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3514 Sema::AA_Converting,
3515 /*AllowExplicit=*/true)
3516 .get();
3517 if (!Upper || !Lower)
3518 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003519
3520 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3521
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003522 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003523 // BuildBinOp already emitted error, this one is to point user to upper
3524 // and lower bound, and to tell what is passed to 'operator-'.
3525 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3526 << Upper->getSourceRange() << Lower->getSourceRange();
3527 return nullptr;
3528 }
3529 }
3530
3531 if (!Diff.isUsable())
3532 return nullptr;
3533
3534 // Upper - Lower [- 1]
3535 if (TestIsStrictOp)
3536 Diff = SemaRef.BuildBinOp(
3537 S, DefaultLoc, BO_Sub, Diff.get(),
3538 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3539 if (!Diff.isUsable())
3540 return nullptr;
3541
3542 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003543 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3544 if (NewStep.isInvalid())
3545 return nullptr;
3546 NewStep = SemaRef.PerformImplicitConversion(
3547 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3548 /*AllowExplicit=*/true);
3549 if (NewStep.isInvalid())
3550 return nullptr;
3551 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003552 if (!Diff.isUsable())
3553 return nullptr;
3554
3555 // Parentheses (for dumping/debugging purposes only).
3556 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3557 if (!Diff.isUsable())
3558 return nullptr;
3559
3560 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003561 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3562 if (NewStep.isInvalid())
3563 return nullptr;
3564 NewStep = SemaRef.PerformImplicitConversion(
3565 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3566 /*AllowExplicit=*/true);
3567 if (NewStep.isInvalid())
3568 return nullptr;
3569 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003570 if (!Diff.isUsable())
3571 return nullptr;
3572
Alexander Musman174b3ca2014-10-06 11:16:29 +00003573 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003574 QualType Type = Diff.get()->getType();
3575 auto &C = SemaRef.Context;
3576 bool UseVarType = VarType->hasIntegerRepresentation() &&
3577 C.getTypeSize(Type) > C.getTypeSize(VarType);
3578 if (!Type->isIntegerType() || UseVarType) {
3579 unsigned NewSize =
3580 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3581 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3582 : Type->hasSignedIntegerRepresentation();
3583 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3584 Diff = SemaRef.PerformImplicitConversion(
3585 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3586 if (!Diff.isUsable())
3587 return nullptr;
3588 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003589 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003590 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3591 if (NewSize != C.getTypeSize(Type)) {
3592 if (NewSize < C.getTypeSize(Type)) {
3593 assert(NewSize == 64 && "incorrect loop var size");
3594 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3595 << InitSrcRange << ConditionSrcRange;
3596 }
3597 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003598 NewSize, Type->hasSignedIntegerRepresentation() ||
3599 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003600 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3601 Sema::AA_Converting, true);
3602 if (!Diff.isUsable())
3603 return nullptr;
3604 }
3605 }
3606
Alexander Musmana5f070a2014-10-01 06:03:56 +00003607 return Diff.get();
3608}
3609
Alexey Bataev62dbb972015-04-22 11:59:37 +00003610Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3611 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3612 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3613 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003614 TransformToNewDefs Transform(SemaRef);
3615
3616 auto NewLB = Transform.TransformExpr(LB);
3617 auto NewUB = Transform.TransformExpr(UB);
3618 if (NewLB.isInvalid() || NewUB.isInvalid())
3619 return Cond;
3620 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3621 Sema::AA_Converting,
3622 /*AllowExplicit=*/true);
3623 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3624 Sema::AA_Converting,
3625 /*AllowExplicit=*/true);
3626 if (NewLB.isInvalid() || NewUB.isInvalid())
3627 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003628 auto CondExpr = SemaRef.BuildBinOp(
3629 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3630 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003631 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003632 if (CondExpr.isUsable()) {
3633 CondExpr = SemaRef.PerformImplicitConversion(
3634 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3635 /*AllowExplicit=*/true);
3636 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003637 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3638 // Otherwise use original loop conditon and evaluate it in runtime.
3639 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3640}
3641
Alexander Musmana5f070a2014-10-01 06:03:56 +00003642/// \brief Build reference expression to the counter be used for codegen.
3643Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003644 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3645 DefaultLoc);
3646}
3647
3648Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3649 if (Var && !Var->isInvalidDecl()) {
3650 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003651 auto *PrivateVar =
3652 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3653 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003654 if (PrivateVar->isInvalidDecl())
3655 return nullptr;
3656 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3657 }
3658 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003659}
3660
3661/// \brief Build initization of the counter be used for codegen.
3662Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3663
3664/// \brief Build step of the counter be used for codegen.
3665Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3666
3667/// \brief Iteration space of a single for loop.
3668struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003669 /// \brief Condition of the loop.
3670 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003671 /// \brief This expression calculates the number of iterations in the loop.
3672 /// It is always possible to calculate it before starting the loop.
3673 Expr *NumIterations;
3674 /// \brief The loop counter variable.
3675 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003676 /// \brief Private loop counter variable.
3677 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003678 /// \brief This is initializer for the initial value of #CounterVar.
3679 Expr *CounterInit;
3680 /// \brief This is step for the #CounterVar used to generate its update:
3681 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3682 Expr *CounterStep;
3683 /// \brief Should step be subtracted?
3684 bool Subtract;
3685 /// \brief Source range of the loop init.
3686 SourceRange InitSrcRange;
3687 /// \brief Source range of the loop condition.
3688 SourceRange CondSrcRange;
3689 /// \brief Source range of the loop increment.
3690 SourceRange IncSrcRange;
3691};
3692
Alexey Bataev23b69422014-06-18 07:08:49 +00003693} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003694
Alexey Bataev9c821032015-04-30 04:23:23 +00003695void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3696 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3697 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003698 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3699 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003700 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3701 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003702 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003703 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003704 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003705 }
3706}
3707
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003708/// \brief Called on a for stmt to check and extract its iteration space
3709/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003710static bool CheckOpenMPIterationSpace(
3711 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3712 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003713 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003714 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003715 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003716 // OpenMP [2.6, Canonical Loop Form]
3717 // for (init-expr; test-expr; incr-expr) structured-block
3718 auto For = dyn_cast_or_null<ForStmt>(S);
3719 if (!For) {
3720 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003721 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3722 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3723 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3724 if (NestedLoopCount > 1) {
3725 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3726 SemaRef.Diag(DSA.getConstructLoc(),
3727 diag::note_omp_collapse_ordered_expr)
3728 << 2 << CollapseLoopCountExpr->getSourceRange()
3729 << OrderedLoopCountExpr->getSourceRange();
3730 else if (CollapseLoopCountExpr)
3731 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3732 diag::note_omp_collapse_ordered_expr)
3733 << 0 << CollapseLoopCountExpr->getSourceRange();
3734 else
3735 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3736 diag::note_omp_collapse_ordered_expr)
3737 << 1 << OrderedLoopCountExpr->getSourceRange();
3738 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003739 return true;
3740 }
3741 assert(For->getBody());
3742
3743 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3744
3745 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003746 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003747 if (ISC.CheckInit(Init)) {
3748 return true;
3749 }
3750
3751 bool HasErrors = false;
3752
3753 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003754 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003755
3756 // OpenMP [2.6, Canonical Loop Form]
3757 // Var is one of the following:
3758 // A variable of signed or unsigned integer type.
3759 // For C++, a variable of a random access iterator type.
3760 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003761 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003762 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3763 !VarType->isPointerType() &&
3764 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3765 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3766 << SemaRef.getLangOpts().CPlusPlus;
3767 HasErrors = true;
3768 }
3769
Alexey Bataev4acb8592014-07-07 13:01:15 +00003770 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3771 // Construct
3772 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3773 // parallel for construct is (are) private.
3774 // The loop iteration variable in the associated for-loop of a simd construct
3775 // with just one associated for-loop is linear with a constant-linear-step
3776 // that is the increment of the associated for-loop.
3777 // Exclude loop var from the list of variables with implicitly defined data
3778 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003779 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003780
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3782 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003783 // The loop iteration variable in the associated for-loop of a simd construct
3784 // with just one associated for-loop may be listed in a linear clause with a
3785 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003786 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3787 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003788 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003789 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3790 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3791 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003792 auto PredeterminedCKind =
3793 isOpenMPSimdDirective(DKind)
3794 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3795 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003796 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003797 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003798 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003799 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003800 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003801 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3802 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003803 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003804 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3805 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003806 if (DVar.RefExpr == nullptr)
3807 DVar.CKind = PredeterminedCKind;
3808 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003809 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003810 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003811 // Make the loop iteration variable private (for worksharing constructs),
3812 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003813 // lastprivate (for simd directives with several collapsed or ordered
3814 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003815 if (DVar.CKind == OMPC_unknown)
3816 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3817 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003818 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003819 }
3820
Alexey Bataev7ff55242014-06-19 09:13:45 +00003821 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003822
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003823 // Check test-expr.
3824 HasErrors |= ISC.CheckCond(For->getCond());
3825
3826 // Check incr-expr.
3827 HasErrors |= ISC.CheckInc(For->getInc());
3828
Alexander Musmana5f070a2014-10-01 06:03:56 +00003829 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003830 return HasErrors;
3831
Alexander Musmana5f070a2014-10-01 06:03:56 +00003832 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003833 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003834 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003835 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003836 isOpenMPTaskLoopDirective(DKind) ||
3837 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003838 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003839 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003840 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3841 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3842 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3843 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3844 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3845 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3846
Alexey Bataev62dbb972015-04-22 11:59:37 +00003847 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3848 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003849 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003850 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003851 ResultIterSpace.CounterInit == nullptr ||
3852 ResultIterSpace.CounterStep == nullptr);
3853
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003854 return HasErrors;
3855}
3856
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003857/// \brief Build 'VarRef = Start.
3858static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3859 ExprResult VarRef, ExprResult Start) {
3860 TransformToNewDefs Transform(SemaRef);
3861 // Build 'VarRef = Start.
3862 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3863 if (NewStart.isInvalid())
3864 return ExprError();
3865 NewStart = SemaRef.PerformImplicitConversion(
3866 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3867 Sema::AA_Converting,
3868 /*AllowExplicit=*/true);
3869 if (NewStart.isInvalid())
3870 return ExprError();
3871 NewStart = SemaRef.PerformImplicitConversion(
3872 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3873 /*AllowExplicit=*/true);
3874 if (!NewStart.isUsable())
3875 return ExprError();
3876
3877 auto Init =
3878 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3879 return Init;
3880}
3881
Alexander Musmana5f070a2014-10-01 06:03:56 +00003882/// \brief Build 'VarRef = Start + Iter * Step'.
3883static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3884 SourceLocation Loc, ExprResult VarRef,
3885 ExprResult Start, ExprResult Iter,
3886 ExprResult Step, bool Subtract) {
3887 // Add parentheses (for debugging purposes only).
3888 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3889 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3890 !Step.isUsable())
3891 return ExprError();
3892
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003893 TransformToNewDefs Transform(SemaRef);
3894 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3895 if (NewStep.isInvalid())
3896 return ExprError();
3897 NewStep = SemaRef.PerformImplicitConversion(
3898 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3899 Sema::AA_Converting,
3900 /*AllowExplicit=*/true);
3901 if (NewStep.isInvalid())
3902 return ExprError();
3903 ExprResult Update =
3904 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003905 if (!Update.isUsable())
3906 return ExprError();
3907
3908 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003909 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3910 if (NewStart.isInvalid())
3911 return ExprError();
3912 NewStart = SemaRef.PerformImplicitConversion(
3913 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3914 Sema::AA_Converting,
3915 /*AllowExplicit=*/true);
3916 if (NewStart.isInvalid())
3917 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003918 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003919 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003920 if (!Update.isUsable())
3921 return ExprError();
3922
3923 Update = SemaRef.PerformImplicitConversion(
3924 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3925 if (!Update.isUsable())
3926 return ExprError();
3927
3928 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3929 return Update;
3930}
3931
3932/// \brief Convert integer expression \a E to make it have at least \a Bits
3933/// bits.
3934static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3935 Sema &SemaRef) {
3936 if (E == nullptr)
3937 return ExprError();
3938 auto &C = SemaRef.Context;
3939 QualType OldType = E->getType();
3940 unsigned HasBits = C.getTypeSize(OldType);
3941 if (HasBits >= Bits)
3942 return ExprResult(E);
3943 // OK to convert to signed, because new type has more bits than old.
3944 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3945 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3946 true);
3947}
3948
3949/// \brief Check if the given expression \a E is a constant integer that fits
3950/// into \a Bits bits.
3951static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3952 if (E == nullptr)
3953 return false;
3954 llvm::APSInt Result;
3955 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3956 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3957 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003958}
3959
3960/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003961/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3962/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003963static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003964CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3965 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3966 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003967 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003968 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003969 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003970 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003971 // Found 'collapse' clause - calculate collapse number.
3972 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003973 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003974 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003975 }
3976 if (OrderedLoopCountExpr) {
3977 // Found 'ordered' clause - calculate collapse number.
3978 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003979 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3980 if (Result.getLimitedValue() < NestedLoopCount) {
3981 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3982 diag::err_omp_wrong_ordered_loop_count)
3983 << OrderedLoopCountExpr->getSourceRange();
3984 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3985 diag::note_collapse_loop_count)
3986 << CollapseLoopCountExpr->getSourceRange();
3987 }
3988 NestedLoopCount = Result.getLimitedValue();
3989 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003990 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003991 // This is helper routine for loop directives (e.g., 'for', 'simd',
3992 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003993 SmallVector<LoopIterationSpace, 4> IterSpaces;
3994 IterSpaces.resize(NestedLoopCount);
3995 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003997 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003998 NestedLoopCount, CollapseLoopCountExpr,
3999 OrderedLoopCountExpr, VarsWithImplicitDSA,
4000 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004001 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004003 // OpenMP [2.8.1, simd construct, Restrictions]
4004 // All loops associated with the construct must be perfectly nested; that
4005 // is, there must be no intervening code nor any OpenMP directive between
4006 // any two loops.
4007 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008 }
4009
Alexander Musmana5f070a2014-10-01 06:03:56 +00004010 Built.clear(/* size */ NestedLoopCount);
4011
4012 if (SemaRef.CurContext->isDependentContext())
4013 return NestedLoopCount;
4014
4015 // An example of what is generated for the following code:
4016 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004017 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004018 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004019 // for (k = 0; k < NK; ++k)
4020 // for (j = J0; j < NJ; j+=2) {
4021 // <loop body>
4022 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004023 //
4024 // We generate the code below.
4025 // Note: the loop body may be outlined in CodeGen.
4026 // Note: some counters may be C++ classes, operator- is used to find number of
4027 // iterations and operator+= to calculate counter value.
4028 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4029 // or i64 is currently supported).
4030 //
4031 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4032 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4033 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4034 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4035 // // similar updates for vars in clauses (e.g. 'linear')
4036 // <loop body (using local i and j)>
4037 // }
4038 // i = NI; // assign final values of counters
4039 // j = NJ;
4040 //
4041
4042 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4043 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004044 // Precondition tests if there is at least one iteration (all conditions are
4045 // true).
4046 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004047 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004048 ExprResult LastIteration32 = WidenIterationCount(
4049 32 /* Bits */, SemaRef.PerformImplicitConversion(
4050 N0->IgnoreImpCasts(), N0->getType(),
4051 Sema::AA_Converting, /*AllowExplicit=*/true)
4052 .get(),
4053 SemaRef);
4054 ExprResult LastIteration64 = WidenIterationCount(
4055 64 /* Bits */, SemaRef.PerformImplicitConversion(
4056 N0->IgnoreImpCasts(), N0->getType(),
4057 Sema::AA_Converting, /*AllowExplicit=*/true)
4058 .get(),
4059 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004060
4061 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4062 return NestedLoopCount;
4063
4064 auto &C = SemaRef.Context;
4065 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4066
4067 Scope *CurScope = DSA.getCurScope();
4068 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004069 if (PreCond.isUsable()) {
4070 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4071 PreCond.get(), IterSpaces[Cnt].PreCond);
4072 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004073 auto N = IterSpaces[Cnt].NumIterations;
4074 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4075 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004076 LastIteration32 = SemaRef.BuildBinOp(
4077 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4078 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4079 Sema::AA_Converting,
4080 /*AllowExplicit=*/true)
4081 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004083 LastIteration64 = SemaRef.BuildBinOp(
4084 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4085 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4086 Sema::AA_Converting,
4087 /*AllowExplicit=*/true)
4088 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004089 }
4090
4091 // Choose either the 32-bit or 64-bit version.
4092 ExprResult LastIteration = LastIteration64;
4093 if (LastIteration32.isUsable() &&
4094 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4095 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4096 FitsInto(
4097 32 /* Bits */,
4098 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4099 LastIteration64.get(), SemaRef)))
4100 LastIteration = LastIteration32;
4101
4102 if (!LastIteration.isUsable())
4103 return 0;
4104
4105 // Save the number of iterations.
4106 ExprResult NumIterations = LastIteration;
4107 {
4108 LastIteration = SemaRef.BuildBinOp(
4109 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4110 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4111 if (!LastIteration.isUsable())
4112 return 0;
4113 }
4114
4115 // Calculate the last iteration number beforehand instead of doing this on
4116 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4117 llvm::APSInt Result;
4118 bool IsConstant =
4119 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4120 ExprResult CalcLastIteration;
4121 if (!IsConstant) {
4122 SourceLocation SaveLoc;
4123 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004124 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004125 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004126 ExprResult SaveRef = buildDeclRefExpr(
4127 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004128 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4129 SaveRef.get(), LastIteration.get());
4130 LastIteration = SaveRef;
4131
4132 // Prepare SaveRef + 1.
4133 NumIterations = SemaRef.BuildBinOp(
4134 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4135 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4136 if (!NumIterations.isUsable())
4137 return 0;
4138 }
4139
4140 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4141
Alexander Musmanc6388682014-12-15 07:07:06 +00004142 QualType VType = LastIteration.get()->getType();
4143 // Build variables passed into runtime, nesessary for worksharing directives.
4144 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004145 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4146 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004147 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004148 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4149 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004150 SemaRef.AddInitializerToDecl(
4151 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4152 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4153
4154 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004155 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4156 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004157 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4158 /*DirectInit*/ false,
4159 /*TypeMayContainAuto*/ false);
4160
4161 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4162 // This will be used to implement clause 'lastprivate'.
4163 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004164 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4165 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004166 SemaRef.AddInitializerToDecl(
4167 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4168 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4169
4170 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004171 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4172 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004173 SemaRef.AddInitializerToDecl(
4174 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4175 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4176
4177 // Build expression: UB = min(UB, LastIteration)
4178 // It is nesessary for CodeGen of directives with static scheduling.
4179 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4180 UB.get(), LastIteration.get());
4181 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4182 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4183 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4184 CondOp.get());
4185 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4186 }
4187
4188 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004189 ExprResult IV;
4190 ExprResult Init;
4191 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004192 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4193 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004194 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004195 isOpenMPTaskLoopDirective(DKind) ||
4196 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004197 ? LB.get()
4198 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4199 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4200 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004201 }
4202
Alexander Musmanc6388682014-12-15 07:07:06 +00004203 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004204 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004205 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004206 (isOpenMPWorksharingDirective(DKind) ||
4207 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004208 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4209 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4210 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004211
4212 // Loop increment (IV = IV + 1)
4213 SourceLocation IncLoc;
4214 ExprResult Inc =
4215 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4216 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4217 if (!Inc.isUsable())
4218 return 0;
4219 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004220 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4221 if (!Inc.isUsable())
4222 return 0;
4223
4224 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4225 // Used for directives with static scheduling.
4226 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004227 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4228 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004229 // LB + ST
4230 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4231 if (!NextLB.isUsable())
4232 return 0;
4233 // LB = LB + ST
4234 NextLB =
4235 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4236 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4237 if (!NextLB.isUsable())
4238 return 0;
4239 // UB + ST
4240 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4241 if (!NextUB.isUsable())
4242 return 0;
4243 // UB = UB + ST
4244 NextUB =
4245 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4246 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4247 if (!NextUB.isUsable())
4248 return 0;
4249 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004250
4251 // Build updates and final values of the loop counters.
4252 bool HasErrors = false;
4253 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004254 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004255 Built.Updates.resize(NestedLoopCount);
4256 Built.Finals.resize(NestedLoopCount);
4257 {
4258 ExprResult Div;
4259 // Go from inner nested loop to outer.
4260 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4261 LoopIterationSpace &IS = IterSpaces[Cnt];
4262 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4263 // Build: Iter = (IV / Div) % IS.NumIters
4264 // where Div is product of previous iterations' IS.NumIters.
4265 ExprResult Iter;
4266 if (Div.isUsable()) {
4267 Iter =
4268 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4269 } else {
4270 Iter = IV;
4271 assert((Cnt == (int)NestedLoopCount - 1) &&
4272 "unusable div expected on first iteration only");
4273 }
4274
4275 if (Cnt != 0 && Iter.isUsable())
4276 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4277 IS.NumIterations);
4278 if (!Iter.isUsable()) {
4279 HasErrors = true;
4280 break;
4281 }
4282
Alexey Bataev39f915b82015-05-08 10:41:21 +00004283 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4284 auto *CounterVar = buildDeclRefExpr(
4285 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4286 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4287 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004288 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4289 IS.CounterInit);
4290 if (!Init.isUsable()) {
4291 HasErrors = true;
4292 break;
4293 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004294 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004295 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004296 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4297 if (!Update.isUsable()) {
4298 HasErrors = true;
4299 break;
4300 }
4301
4302 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4303 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004304 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004305 IS.NumIterations, IS.CounterStep, IS.Subtract);
4306 if (!Final.isUsable()) {
4307 HasErrors = true;
4308 break;
4309 }
4310
4311 // Build Div for the next iteration: Div <- Div * IS.NumIters
4312 if (Cnt != 0) {
4313 if (Div.isUnset())
4314 Div = IS.NumIterations;
4315 else
4316 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4317 IS.NumIterations);
4318
4319 // Add parentheses (for debugging purposes only).
4320 if (Div.isUsable())
4321 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4322 if (!Div.isUsable()) {
4323 HasErrors = true;
4324 break;
4325 }
4326 }
4327 if (!Update.isUsable() || !Final.isUsable()) {
4328 HasErrors = true;
4329 break;
4330 }
4331 // Save results
4332 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004333 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004334 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004335 Built.Updates[Cnt] = Update.get();
4336 Built.Finals[Cnt] = Final.get();
4337 }
4338 }
4339
4340 if (HasErrors)
4341 return 0;
4342
4343 // Save results
4344 Built.IterationVarRef = IV.get();
4345 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004346 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004347 Built.CalcLastIteration =
4348 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004349 Built.PreCond = PreCond.get();
4350 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004351 Built.Init = Init.get();
4352 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004353 Built.LB = LB.get();
4354 Built.UB = UB.get();
4355 Built.IL = IL.get();
4356 Built.ST = ST.get();
4357 Built.EUB = EUB.get();
4358 Built.NLB = NextLB.get();
4359 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360
Alexey Bataevabfc0692014-06-25 06:52:00 +00004361 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004362}
4363
Alexey Bataev10e775f2015-07-30 11:36:16 +00004364static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004365 auto CollapseClauses =
4366 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4367 if (CollapseClauses.begin() != CollapseClauses.end())
4368 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004369 return nullptr;
4370}
4371
Alexey Bataev10e775f2015-07-30 11:36:16 +00004372static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004373 auto OrderedClauses =
4374 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4375 if (OrderedClauses.begin() != OrderedClauses.end())
4376 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004377 return nullptr;
4378}
4379
Alexey Bataev66b15b52015-08-21 11:14:16 +00004380static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4381 const Expr *Safelen) {
4382 llvm::APSInt SimdlenRes, SafelenRes;
4383 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4384 Simdlen->isInstantiationDependent() ||
4385 Simdlen->containsUnexpandedParameterPack())
4386 return false;
4387 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4388 Safelen->isInstantiationDependent() ||
4389 Safelen->containsUnexpandedParameterPack())
4390 return false;
4391 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4392 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4393 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4394 // If both simdlen and safelen clauses are specified, the value of the simdlen
4395 // parameter must be less than or equal to the value of the safelen parameter.
4396 if (SimdlenRes > SafelenRes) {
4397 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4398 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4399 return true;
4400 }
4401 return false;
4402}
4403
Alexey Bataev4acb8592014-07-07 13:01:15 +00004404StmtResult Sema::ActOnOpenMPSimdDirective(
4405 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4406 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004407 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004408 if (!AStmt)
4409 return StmtError();
4410
4411 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004412 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004413 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4414 // define the nested loops number.
4415 unsigned NestedLoopCount = CheckOpenMPLoop(
4416 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4417 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004418 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004419 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004420
Alexander Musmana5f070a2014-10-01 06:03:56 +00004421 assert((CurContext->isDependentContext() || B.builtAll()) &&
4422 "omp simd loop exprs were not built");
4423
Alexander Musman3276a272015-03-21 10:12:56 +00004424 if (!CurContext->isDependentContext()) {
4425 // Finalize the clauses that need pre-built expressions for CodeGen.
4426 for (auto C : Clauses) {
4427 if (auto LC = dyn_cast<OMPLinearClause>(C))
4428 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4429 B.NumIterations, *this, CurScope))
4430 return StmtError();
4431 }
4432 }
4433
Alexey Bataev66b15b52015-08-21 11:14:16 +00004434 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4435 // If both simdlen and safelen clauses are specified, the value of the simdlen
4436 // parameter must be less than or equal to the value of the safelen parameter.
4437 OMPSafelenClause *Safelen = nullptr;
4438 OMPSimdlenClause *Simdlen = nullptr;
4439 for (auto *Clause : Clauses) {
4440 if (Clause->getClauseKind() == OMPC_safelen)
4441 Safelen = cast<OMPSafelenClause>(Clause);
4442 else if (Clause->getClauseKind() == OMPC_simdlen)
4443 Simdlen = cast<OMPSimdlenClause>(Clause);
4444 if (Safelen && Simdlen)
4445 break;
4446 }
4447 if (Simdlen && Safelen &&
4448 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4449 Safelen->getSafelen()))
4450 return StmtError();
4451
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004452 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004453 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4454 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004455}
4456
Alexey Bataev4acb8592014-07-07 13:01:15 +00004457StmtResult Sema::ActOnOpenMPForDirective(
4458 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4459 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004460 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004461 if (!AStmt)
4462 return StmtError();
4463
4464 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004465 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004466 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4467 // define the nested loops number.
4468 unsigned NestedLoopCount = CheckOpenMPLoop(
4469 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4470 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004471 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004472 return StmtError();
4473
Alexander Musmana5f070a2014-10-01 06:03:56 +00004474 assert((CurContext->isDependentContext() || B.builtAll()) &&
4475 "omp for loop exprs were not built");
4476
Alexey Bataev54acd402015-08-04 11:18:19 +00004477 if (!CurContext->isDependentContext()) {
4478 // Finalize the clauses that need pre-built expressions for CodeGen.
4479 for (auto C : Clauses) {
4480 if (auto LC = dyn_cast<OMPLinearClause>(C))
4481 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4482 B.NumIterations, *this, CurScope))
4483 return StmtError();
4484 }
4485 }
4486
Alexey Bataevf29276e2014-06-18 04:14:57 +00004487 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004488 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004489 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004490}
4491
Alexander Musmanf82886e2014-09-18 05:12:34 +00004492StmtResult Sema::ActOnOpenMPForSimdDirective(
4493 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4494 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004495 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004496 if (!AStmt)
4497 return StmtError();
4498
4499 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004500 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004501 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4502 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004503 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004504 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4505 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4506 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004507 if (NestedLoopCount == 0)
4508 return StmtError();
4509
Alexander Musmanc6388682014-12-15 07:07:06 +00004510 assert((CurContext->isDependentContext() || B.builtAll()) &&
4511 "omp for simd loop exprs were not built");
4512
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004513 if (!CurContext->isDependentContext()) {
4514 // Finalize the clauses that need pre-built expressions for CodeGen.
4515 for (auto C : Clauses) {
4516 if (auto LC = dyn_cast<OMPLinearClause>(C))
4517 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4518 B.NumIterations, *this, CurScope))
4519 return StmtError();
4520 }
4521 }
4522
Alexey Bataev66b15b52015-08-21 11:14:16 +00004523 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4524 // If both simdlen and safelen clauses are specified, the value of the simdlen
4525 // parameter must be less than or equal to the value of the safelen parameter.
4526 OMPSafelenClause *Safelen = nullptr;
4527 OMPSimdlenClause *Simdlen = nullptr;
4528 for (auto *Clause : Clauses) {
4529 if (Clause->getClauseKind() == OMPC_safelen)
4530 Safelen = cast<OMPSafelenClause>(Clause);
4531 else if (Clause->getClauseKind() == OMPC_simdlen)
4532 Simdlen = cast<OMPSimdlenClause>(Clause);
4533 if (Safelen && Simdlen)
4534 break;
4535 }
4536 if (Simdlen && Safelen &&
4537 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4538 Safelen->getSafelen()))
4539 return StmtError();
4540
Alexander Musmanf82886e2014-09-18 05:12:34 +00004541 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004542 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4543 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004544}
4545
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004546StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4547 Stmt *AStmt,
4548 SourceLocation StartLoc,
4549 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004550 if (!AStmt)
4551 return StmtError();
4552
4553 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004554 auto BaseStmt = AStmt;
4555 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4556 BaseStmt = CS->getCapturedStmt();
4557 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4558 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004559 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004560 return StmtError();
4561 // All associated statements must be '#pragma omp section' except for
4562 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004563 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004564 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4565 if (SectionStmt)
4566 Diag(SectionStmt->getLocStart(),
4567 diag::err_omp_sections_substmt_not_section);
4568 return StmtError();
4569 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004570 cast<OMPSectionDirective>(SectionStmt)
4571 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004572 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004573 } else {
4574 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4575 return StmtError();
4576 }
4577
4578 getCurFunction()->setHasBranchProtectedScope();
4579
Alexey Bataev25e5b442015-09-15 12:52:43 +00004580 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4581 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004582}
4583
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004584StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4585 SourceLocation StartLoc,
4586 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004587 if (!AStmt)
4588 return StmtError();
4589
4590 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004591
4592 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004593 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004594
Alexey Bataev25e5b442015-09-15 12:52:43 +00004595 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4596 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004597}
4598
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004599StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4600 Stmt *AStmt,
4601 SourceLocation StartLoc,
4602 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004603 if (!AStmt)
4604 return StmtError();
4605
4606 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004607
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004608 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004609
Alexey Bataev3255bf32015-01-19 05:20:46 +00004610 // OpenMP [2.7.3, single Construct, Restrictions]
4611 // The copyprivate clause must not be used with the nowait clause.
4612 OMPClause *Nowait = nullptr;
4613 OMPClause *Copyprivate = nullptr;
4614 for (auto *Clause : Clauses) {
4615 if (Clause->getClauseKind() == OMPC_nowait)
4616 Nowait = Clause;
4617 else if (Clause->getClauseKind() == OMPC_copyprivate)
4618 Copyprivate = Clause;
4619 if (Copyprivate && Nowait) {
4620 Diag(Copyprivate->getLocStart(),
4621 diag::err_omp_single_copyprivate_with_nowait);
4622 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4623 return StmtError();
4624 }
4625 }
4626
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004627 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4628}
4629
Alexander Musman80c22892014-07-17 08:54:58 +00004630StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4631 SourceLocation StartLoc,
4632 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004633 if (!AStmt)
4634 return StmtError();
4635
4636 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004637
4638 getCurFunction()->setHasBranchProtectedScope();
4639
4640 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4641}
4642
Alexey Bataev28c75412015-12-15 08:19:24 +00004643StmtResult Sema::ActOnOpenMPCriticalDirective(
4644 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4645 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004646 if (!AStmt)
4647 return StmtError();
4648
4649 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004650
Alexey Bataev28c75412015-12-15 08:19:24 +00004651 bool ErrorFound = false;
4652 llvm::APSInt Hint;
4653 SourceLocation HintLoc;
4654 bool DependentHint = false;
4655 for (auto *C : Clauses) {
4656 if (C->getClauseKind() == OMPC_hint) {
4657 if (!DirName.getName()) {
4658 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4659 ErrorFound = true;
4660 }
4661 Expr *E = cast<OMPHintClause>(C)->getHint();
4662 if (E->isTypeDependent() || E->isValueDependent() ||
4663 E->isInstantiationDependent())
4664 DependentHint = true;
4665 else {
4666 Hint = E->EvaluateKnownConstInt(Context);
4667 HintLoc = C->getLocStart();
4668 }
4669 }
4670 }
4671 if (ErrorFound)
4672 return StmtError();
4673 auto Pair = DSAStack->getCriticalWithHint(DirName);
4674 if (Pair.first && DirName.getName() && !DependentHint) {
4675 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4676 Diag(StartLoc, diag::err_omp_critical_with_hint);
4677 if (HintLoc.isValid()) {
4678 Diag(HintLoc, diag::note_omp_critical_hint_here)
4679 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4680 } else
4681 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4682 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4683 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4684 << 1
4685 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4686 /*Radix=*/10, /*Signed=*/false);
4687 } else
4688 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4689 }
4690 }
4691
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004692 getCurFunction()->setHasBranchProtectedScope();
4693
Alexey Bataev28c75412015-12-15 08:19:24 +00004694 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4695 Clauses, AStmt);
4696 if (!Pair.first && DirName.getName() && !DependentHint)
4697 DSAStack->addCriticalWithHint(Dir, Hint);
4698 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004699}
4700
Alexey Bataev4acb8592014-07-07 13:01:15 +00004701StmtResult Sema::ActOnOpenMPParallelForDirective(
4702 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4703 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004704 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004705 if (!AStmt)
4706 return StmtError();
4707
Alexey Bataev4acb8592014-07-07 13:01:15 +00004708 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4709 // 1.2.2 OpenMP Language Terminology
4710 // Structured block - An executable statement with a single entry at the
4711 // top and a single exit at the bottom.
4712 // The point of exit cannot be a branch out of the structured block.
4713 // longjmp() and throw() must not violate the entry/exit criteria.
4714 CS->getCapturedDecl()->setNothrow();
4715
Alexander Musmanc6388682014-12-15 07:07:06 +00004716 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004717 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4718 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004719 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004720 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4721 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4722 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004723 if (NestedLoopCount == 0)
4724 return StmtError();
4725
Alexander Musmana5f070a2014-10-01 06:03:56 +00004726 assert((CurContext->isDependentContext() || B.builtAll()) &&
4727 "omp parallel for loop exprs were not built");
4728
Alexey Bataev54acd402015-08-04 11:18:19 +00004729 if (!CurContext->isDependentContext()) {
4730 // Finalize the clauses that need pre-built expressions for CodeGen.
4731 for (auto C : Clauses) {
4732 if (auto LC = dyn_cast<OMPLinearClause>(C))
4733 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4734 B.NumIterations, *this, CurScope))
4735 return StmtError();
4736 }
4737 }
4738
Alexey Bataev4acb8592014-07-07 13:01:15 +00004739 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004740 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004741 NestedLoopCount, Clauses, AStmt, B,
4742 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004743}
4744
Alexander Musmane4e893b2014-09-23 09:33:00 +00004745StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4746 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4747 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004748 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004749 if (!AStmt)
4750 return StmtError();
4751
Alexander Musmane4e893b2014-09-23 09:33:00 +00004752 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4753 // 1.2.2 OpenMP Language Terminology
4754 // Structured block - An executable statement with a single entry at the
4755 // top and a single exit at the bottom.
4756 // The point of exit cannot be a branch out of the structured block.
4757 // longjmp() and throw() must not violate the entry/exit criteria.
4758 CS->getCapturedDecl()->setNothrow();
4759
Alexander Musmanc6388682014-12-15 07:07:06 +00004760 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004761 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4762 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004763 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004764 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4765 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4766 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004767 if (NestedLoopCount == 0)
4768 return StmtError();
4769
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004770 if (!CurContext->isDependentContext()) {
4771 // Finalize the clauses that need pre-built expressions for CodeGen.
4772 for (auto C : Clauses) {
4773 if (auto LC = dyn_cast<OMPLinearClause>(C))
4774 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4775 B.NumIterations, *this, CurScope))
4776 return StmtError();
4777 }
4778 }
4779
Alexey Bataev66b15b52015-08-21 11:14:16 +00004780 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4781 // If both simdlen and safelen clauses are specified, the value of the simdlen
4782 // parameter must be less than or equal to the value of the safelen parameter.
4783 OMPSafelenClause *Safelen = nullptr;
4784 OMPSimdlenClause *Simdlen = nullptr;
4785 for (auto *Clause : Clauses) {
4786 if (Clause->getClauseKind() == OMPC_safelen)
4787 Safelen = cast<OMPSafelenClause>(Clause);
4788 else if (Clause->getClauseKind() == OMPC_simdlen)
4789 Simdlen = cast<OMPSimdlenClause>(Clause);
4790 if (Safelen && Simdlen)
4791 break;
4792 }
4793 if (Simdlen && Safelen &&
4794 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4795 Safelen->getSafelen()))
4796 return StmtError();
4797
Alexander Musmane4e893b2014-09-23 09:33:00 +00004798 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004799 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004800 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004801}
4802
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004803StmtResult
4804Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4805 Stmt *AStmt, SourceLocation StartLoc,
4806 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004807 if (!AStmt)
4808 return StmtError();
4809
4810 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004811 auto BaseStmt = AStmt;
4812 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4813 BaseStmt = CS->getCapturedStmt();
4814 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4815 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004816 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004817 return StmtError();
4818 // All associated statements must be '#pragma omp section' except for
4819 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004820 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004821 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4822 if (SectionStmt)
4823 Diag(SectionStmt->getLocStart(),
4824 diag::err_omp_parallel_sections_substmt_not_section);
4825 return StmtError();
4826 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004827 cast<OMPSectionDirective>(SectionStmt)
4828 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004829 }
4830 } else {
4831 Diag(AStmt->getLocStart(),
4832 diag::err_omp_parallel_sections_not_compound_stmt);
4833 return StmtError();
4834 }
4835
4836 getCurFunction()->setHasBranchProtectedScope();
4837
Alexey Bataev25e5b442015-09-15 12:52:43 +00004838 return OMPParallelSectionsDirective::Create(
4839 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004840}
4841
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004842StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4843 Stmt *AStmt, SourceLocation StartLoc,
4844 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004845 if (!AStmt)
4846 return StmtError();
4847
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004848 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4849 // 1.2.2 OpenMP Language Terminology
4850 // Structured block - An executable statement with a single entry at the
4851 // top and a single exit at the bottom.
4852 // The point of exit cannot be a branch out of the structured block.
4853 // longjmp() and throw() must not violate the entry/exit criteria.
4854 CS->getCapturedDecl()->setNothrow();
4855
4856 getCurFunction()->setHasBranchProtectedScope();
4857
Alexey Bataev25e5b442015-09-15 12:52:43 +00004858 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4859 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004860}
4861
Alexey Bataev68446b72014-07-18 07:47:19 +00004862StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4863 SourceLocation EndLoc) {
4864 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4865}
4866
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004867StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4868 SourceLocation EndLoc) {
4869 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4870}
4871
Alexey Bataev2df347a2014-07-18 10:17:07 +00004872StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4873 SourceLocation EndLoc) {
4874 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4875}
4876
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004877StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4878 SourceLocation StartLoc,
4879 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004880 if (!AStmt)
4881 return StmtError();
4882
4883 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004884
4885 getCurFunction()->setHasBranchProtectedScope();
4886
4887 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4888}
4889
Alexey Bataev6125da92014-07-21 11:26:11 +00004890StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4891 SourceLocation StartLoc,
4892 SourceLocation EndLoc) {
4893 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4894 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4895}
4896
Alexey Bataev346265e2015-09-25 10:37:12 +00004897StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4898 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004899 SourceLocation StartLoc,
4900 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004901 OMPClause *DependFound = nullptr;
4902 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004903 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004904 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004905 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004906 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004907 for (auto *C : Clauses) {
4908 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4909 DependFound = C;
4910 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4911 if (DependSourceClause) {
4912 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4913 << getOpenMPDirectiveName(OMPD_ordered)
4914 << getOpenMPClauseName(OMPC_depend) << 2;
4915 ErrorFound = true;
4916 } else
4917 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004918 if (DependSinkClause) {
4919 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4920 << 0;
4921 ErrorFound = true;
4922 }
4923 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4924 if (DependSourceClause) {
4925 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4926 << 1;
4927 ErrorFound = true;
4928 }
4929 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004930 }
4931 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004932 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004933 else if (C->getClauseKind() == OMPC_simd)
4934 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004935 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004936 if (!ErrorFound && !SC &&
4937 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004938 // OpenMP [2.8.1,simd Construct, Restrictions]
4939 // An ordered construct with the simd clause is the only OpenMP construct
4940 // that can appear in the simd region.
4941 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004942 ErrorFound = true;
4943 } else if (DependFound && (TC || SC)) {
4944 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4945 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4946 ErrorFound = true;
4947 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4948 Diag(DependFound->getLocStart(),
4949 diag::err_omp_ordered_directive_without_param);
4950 ErrorFound = true;
4951 } else if (TC || Clauses.empty()) {
4952 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4953 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4954 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4955 << (TC != nullptr);
4956 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4957 ErrorFound = true;
4958 }
4959 }
4960 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004961 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004962
4963 if (AStmt) {
4964 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4965
4966 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004967 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004968
4969 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004970}
4971
Alexey Bataev1d160b12015-03-13 12:27:31 +00004972namespace {
4973/// \brief Helper class for checking expression in 'omp atomic [update]'
4974/// construct.
4975class OpenMPAtomicUpdateChecker {
4976 /// \brief Error results for atomic update expressions.
4977 enum ExprAnalysisErrorCode {
4978 /// \brief A statement is not an expression statement.
4979 NotAnExpression,
4980 /// \brief Expression is not builtin binary or unary operation.
4981 NotABinaryOrUnaryExpression,
4982 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4983 NotAnUnaryIncDecExpression,
4984 /// \brief An expression is not of scalar type.
4985 NotAScalarType,
4986 /// \brief A binary operation is not an assignment operation.
4987 NotAnAssignmentOp,
4988 /// \brief RHS part of the binary operation is not a binary expression.
4989 NotABinaryExpression,
4990 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4991 /// expression.
4992 NotABinaryOperator,
4993 /// \brief RHS binary operation does not have reference to the updated LHS
4994 /// part.
4995 NotAnUpdateExpression,
4996 /// \brief No errors is found.
4997 NoError
4998 };
4999 /// \brief Reference to Sema.
5000 Sema &SemaRef;
5001 /// \brief A location for note diagnostics (when error is found).
5002 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005003 /// \brief 'x' lvalue part of the source atomic expression.
5004 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005005 /// \brief 'expr' rvalue part of the source atomic expression.
5006 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005007 /// \brief Helper expression of the form
5008 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5009 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5010 Expr *UpdateExpr;
5011 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5012 /// important for non-associative operations.
5013 bool IsXLHSInRHSPart;
5014 BinaryOperatorKind Op;
5015 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005016 /// \brief true if the source expression is a postfix unary operation, false
5017 /// if it is a prefix unary operation.
5018 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005019
5020public:
5021 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005022 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005023 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005024 /// \brief Check specified statement that it is suitable for 'atomic update'
5025 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005026 /// expression. If DiagId and NoteId == 0, then only check is performed
5027 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005028 /// \param DiagId Diagnostic which should be emitted if error is found.
5029 /// \param NoteId Diagnostic note for the main error message.
5030 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005031 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005032 /// \brief Return the 'x' lvalue part of the source atomic expression.
5033 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005034 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5035 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005036 /// \brief Return the update expression used in calculation of the updated
5037 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5038 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5039 Expr *getUpdateExpr() const { return UpdateExpr; }
5040 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5041 /// false otherwise.
5042 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5043
Alexey Bataevb78ca832015-04-01 03:33:17 +00005044 /// \brief true if the source expression is a postfix unary operation, false
5045 /// if it is a prefix unary operation.
5046 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5047
Alexey Bataev1d160b12015-03-13 12:27:31 +00005048private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005049 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5050 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005051};
5052} // namespace
5053
5054bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5055 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5056 ExprAnalysisErrorCode ErrorFound = NoError;
5057 SourceLocation ErrorLoc, NoteLoc;
5058 SourceRange ErrorRange, NoteRange;
5059 // Allowed constructs are:
5060 // x = x binop expr;
5061 // x = expr binop x;
5062 if (AtomicBinOp->getOpcode() == BO_Assign) {
5063 X = AtomicBinOp->getLHS();
5064 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5065 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5066 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5067 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5068 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005069 Op = AtomicInnerBinOp->getOpcode();
5070 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005071 auto *LHS = AtomicInnerBinOp->getLHS();
5072 auto *RHS = AtomicInnerBinOp->getRHS();
5073 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5074 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5075 /*Canonical=*/true);
5076 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5077 /*Canonical=*/true);
5078 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5079 /*Canonical=*/true);
5080 if (XId == LHSId) {
5081 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005082 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005083 } else if (XId == RHSId) {
5084 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005085 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005086 } else {
5087 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5088 ErrorRange = AtomicInnerBinOp->getSourceRange();
5089 NoteLoc = X->getExprLoc();
5090 NoteRange = X->getSourceRange();
5091 ErrorFound = NotAnUpdateExpression;
5092 }
5093 } else {
5094 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5095 ErrorRange = AtomicInnerBinOp->getSourceRange();
5096 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5097 NoteRange = SourceRange(NoteLoc, NoteLoc);
5098 ErrorFound = NotABinaryOperator;
5099 }
5100 } else {
5101 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5102 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5103 ErrorFound = NotABinaryExpression;
5104 }
5105 } else {
5106 ErrorLoc = AtomicBinOp->getExprLoc();
5107 ErrorRange = AtomicBinOp->getSourceRange();
5108 NoteLoc = AtomicBinOp->getOperatorLoc();
5109 NoteRange = SourceRange(NoteLoc, NoteLoc);
5110 ErrorFound = NotAnAssignmentOp;
5111 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005112 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005113 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5114 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5115 return true;
5116 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005117 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005118 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005119}
5120
5121bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5122 unsigned NoteId) {
5123 ExprAnalysisErrorCode ErrorFound = NoError;
5124 SourceLocation ErrorLoc, NoteLoc;
5125 SourceRange ErrorRange, NoteRange;
5126 // Allowed constructs are:
5127 // x++;
5128 // x--;
5129 // ++x;
5130 // --x;
5131 // x binop= expr;
5132 // x = x binop expr;
5133 // x = expr binop x;
5134 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5135 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5136 if (AtomicBody->getType()->isScalarType() ||
5137 AtomicBody->isInstantiationDependent()) {
5138 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5139 AtomicBody->IgnoreParenImpCasts())) {
5140 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005141 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005142 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005143 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005144 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005145 X = AtomicCompAssignOp->getLHS();
5146 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005147 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5148 AtomicBody->IgnoreParenImpCasts())) {
5149 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005150 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5151 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005152 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005153 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5154 // Check for Unary Operation
5155 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005156 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005157 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5158 OpLoc = AtomicUnaryOp->getOperatorLoc();
5159 X = AtomicUnaryOp->getSubExpr();
5160 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5161 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005162 } else {
5163 ErrorFound = NotAnUnaryIncDecExpression;
5164 ErrorLoc = AtomicUnaryOp->getExprLoc();
5165 ErrorRange = AtomicUnaryOp->getSourceRange();
5166 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5167 NoteRange = SourceRange(NoteLoc, NoteLoc);
5168 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005169 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005170 ErrorFound = NotABinaryOrUnaryExpression;
5171 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5172 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5173 }
5174 } else {
5175 ErrorFound = NotAScalarType;
5176 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5177 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5178 }
5179 } else {
5180 ErrorFound = NotAnExpression;
5181 NoteLoc = ErrorLoc = S->getLocStart();
5182 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5183 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005184 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005185 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5186 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5187 return true;
5188 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005189 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005190 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005191 // Build an update expression of form 'OpaqueValueExpr(x) binop
5192 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5193 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5194 auto *OVEX = new (SemaRef.getASTContext())
5195 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5196 auto *OVEExpr = new (SemaRef.getASTContext())
5197 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5198 auto Update =
5199 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5200 IsXLHSInRHSPart ? OVEExpr : OVEX);
5201 if (Update.isInvalid())
5202 return true;
5203 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5204 Sema::AA_Casting);
5205 if (Update.isInvalid())
5206 return true;
5207 UpdateExpr = Update.get();
5208 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005209 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005210}
5211
Alexey Bataev0162e452014-07-22 10:10:35 +00005212StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5213 Stmt *AStmt,
5214 SourceLocation StartLoc,
5215 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005216 if (!AStmt)
5217 return StmtError();
5218
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005219 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005220 // 1.2.2 OpenMP Language Terminology
5221 // Structured block - An executable statement with a single entry at the
5222 // top and a single exit at the bottom.
5223 // The point of exit cannot be a branch out of the structured block.
5224 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005225 OpenMPClauseKind AtomicKind = OMPC_unknown;
5226 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005227 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005228 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005229 C->getClauseKind() == OMPC_update ||
5230 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005231 if (AtomicKind != OMPC_unknown) {
5232 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5233 << SourceRange(C->getLocStart(), C->getLocEnd());
5234 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5235 << getOpenMPClauseName(AtomicKind);
5236 } else {
5237 AtomicKind = C->getClauseKind();
5238 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005239 }
5240 }
5241 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005242
Alexey Bataev459dec02014-07-24 06:46:57 +00005243 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005244 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5245 Body = EWC->getSubExpr();
5246
Alexey Bataev62cec442014-11-18 10:14:22 +00005247 Expr *X = nullptr;
5248 Expr *V = nullptr;
5249 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005250 Expr *UE = nullptr;
5251 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005252 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005253 // OpenMP [2.12.6, atomic Construct]
5254 // In the next expressions:
5255 // * x and v (as applicable) are both l-value expressions with scalar type.
5256 // * During the execution of an atomic region, multiple syntactic
5257 // occurrences of x must designate the same storage location.
5258 // * Neither of v and expr (as applicable) may access the storage location
5259 // designated by x.
5260 // * Neither of x and expr (as applicable) may access the storage location
5261 // designated by v.
5262 // * expr is an expression with scalar type.
5263 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5264 // * binop, binop=, ++, and -- are not overloaded operators.
5265 // * The expression x binop expr must be numerically equivalent to x binop
5266 // (expr). This requirement is satisfied if the operators in expr have
5267 // precedence greater than binop, or by using parentheses around expr or
5268 // subexpressions of expr.
5269 // * The expression expr binop x must be numerically equivalent to (expr)
5270 // binop x. This requirement is satisfied if the operators in expr have
5271 // precedence equal to or greater than binop, or by using parentheses around
5272 // expr or subexpressions of expr.
5273 // * For forms that allow multiple occurrences of x, the number of times
5274 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005275 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005276 enum {
5277 NotAnExpression,
5278 NotAnAssignmentOp,
5279 NotAScalarType,
5280 NotAnLValue,
5281 NoError
5282 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005283 SourceLocation ErrorLoc, NoteLoc;
5284 SourceRange ErrorRange, NoteRange;
5285 // If clause is read:
5286 // v = x;
5287 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5288 auto AtomicBinOp =
5289 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5290 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5291 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5292 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5293 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5294 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5295 if (!X->isLValue() || !V->isLValue()) {
5296 auto NotLValueExpr = X->isLValue() ? V : X;
5297 ErrorFound = NotAnLValue;
5298 ErrorLoc = AtomicBinOp->getExprLoc();
5299 ErrorRange = AtomicBinOp->getSourceRange();
5300 NoteLoc = NotLValueExpr->getExprLoc();
5301 NoteRange = NotLValueExpr->getSourceRange();
5302 }
5303 } else if (!X->isInstantiationDependent() ||
5304 !V->isInstantiationDependent()) {
5305 auto NotScalarExpr =
5306 (X->isInstantiationDependent() || X->getType()->isScalarType())
5307 ? V
5308 : X;
5309 ErrorFound = NotAScalarType;
5310 ErrorLoc = AtomicBinOp->getExprLoc();
5311 ErrorRange = AtomicBinOp->getSourceRange();
5312 NoteLoc = NotScalarExpr->getExprLoc();
5313 NoteRange = NotScalarExpr->getSourceRange();
5314 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005315 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005316 ErrorFound = NotAnAssignmentOp;
5317 ErrorLoc = AtomicBody->getExprLoc();
5318 ErrorRange = AtomicBody->getSourceRange();
5319 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5320 : AtomicBody->getExprLoc();
5321 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5322 : AtomicBody->getSourceRange();
5323 }
5324 } else {
5325 ErrorFound = NotAnExpression;
5326 NoteLoc = ErrorLoc = Body->getLocStart();
5327 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005328 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005329 if (ErrorFound != NoError) {
5330 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5331 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005332 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5333 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005334 return StmtError();
5335 } else if (CurContext->isDependentContext())
5336 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005337 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005338 enum {
5339 NotAnExpression,
5340 NotAnAssignmentOp,
5341 NotAScalarType,
5342 NotAnLValue,
5343 NoError
5344 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005345 SourceLocation ErrorLoc, NoteLoc;
5346 SourceRange ErrorRange, NoteRange;
5347 // If clause is write:
5348 // x = expr;
5349 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5350 auto AtomicBinOp =
5351 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5352 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005353 X = AtomicBinOp->getLHS();
5354 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005355 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5356 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5357 if (!X->isLValue()) {
5358 ErrorFound = NotAnLValue;
5359 ErrorLoc = AtomicBinOp->getExprLoc();
5360 ErrorRange = AtomicBinOp->getSourceRange();
5361 NoteLoc = X->getExprLoc();
5362 NoteRange = X->getSourceRange();
5363 }
5364 } else if (!X->isInstantiationDependent() ||
5365 !E->isInstantiationDependent()) {
5366 auto NotScalarExpr =
5367 (X->isInstantiationDependent() || X->getType()->isScalarType())
5368 ? E
5369 : X;
5370 ErrorFound = NotAScalarType;
5371 ErrorLoc = AtomicBinOp->getExprLoc();
5372 ErrorRange = AtomicBinOp->getSourceRange();
5373 NoteLoc = NotScalarExpr->getExprLoc();
5374 NoteRange = NotScalarExpr->getSourceRange();
5375 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005376 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005377 ErrorFound = NotAnAssignmentOp;
5378 ErrorLoc = AtomicBody->getExprLoc();
5379 ErrorRange = AtomicBody->getSourceRange();
5380 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5381 : AtomicBody->getExprLoc();
5382 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5383 : AtomicBody->getSourceRange();
5384 }
5385 } else {
5386 ErrorFound = NotAnExpression;
5387 NoteLoc = ErrorLoc = Body->getLocStart();
5388 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005389 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005390 if (ErrorFound != NoError) {
5391 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5392 << ErrorRange;
5393 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5394 << NoteRange;
5395 return StmtError();
5396 } else if (CurContext->isDependentContext())
5397 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005398 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005399 // If clause is update:
5400 // x++;
5401 // x--;
5402 // ++x;
5403 // --x;
5404 // x binop= expr;
5405 // x = x binop expr;
5406 // x = expr binop x;
5407 OpenMPAtomicUpdateChecker Checker(*this);
5408 if (Checker.checkStatement(
5409 Body, (AtomicKind == OMPC_update)
5410 ? diag::err_omp_atomic_update_not_expression_statement
5411 : diag::err_omp_atomic_not_expression_statement,
5412 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005413 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005414 if (!CurContext->isDependentContext()) {
5415 E = Checker.getExpr();
5416 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005417 UE = Checker.getUpdateExpr();
5418 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005419 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005420 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005421 enum {
5422 NotAnAssignmentOp,
5423 NotACompoundStatement,
5424 NotTwoSubstatements,
5425 NotASpecificExpression,
5426 NoError
5427 } ErrorFound = NoError;
5428 SourceLocation ErrorLoc, NoteLoc;
5429 SourceRange ErrorRange, NoteRange;
5430 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5431 // If clause is a capture:
5432 // v = x++;
5433 // v = x--;
5434 // v = ++x;
5435 // v = --x;
5436 // v = x binop= expr;
5437 // v = x = x binop expr;
5438 // v = x = expr binop x;
5439 auto *AtomicBinOp =
5440 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5441 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5442 V = AtomicBinOp->getLHS();
5443 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5444 OpenMPAtomicUpdateChecker Checker(*this);
5445 if (Checker.checkStatement(
5446 Body, diag::err_omp_atomic_capture_not_expression_statement,
5447 diag::note_omp_atomic_update))
5448 return StmtError();
5449 E = Checker.getExpr();
5450 X = Checker.getX();
5451 UE = Checker.getUpdateExpr();
5452 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5453 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005454 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005455 ErrorLoc = AtomicBody->getExprLoc();
5456 ErrorRange = AtomicBody->getSourceRange();
5457 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5458 : AtomicBody->getExprLoc();
5459 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5460 : AtomicBody->getSourceRange();
5461 ErrorFound = NotAnAssignmentOp;
5462 }
5463 if (ErrorFound != NoError) {
5464 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5465 << ErrorRange;
5466 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5467 return StmtError();
5468 } else if (CurContext->isDependentContext()) {
5469 UE = V = E = X = nullptr;
5470 }
5471 } else {
5472 // If clause is a capture:
5473 // { v = x; x = expr; }
5474 // { v = x; x++; }
5475 // { v = x; x--; }
5476 // { v = x; ++x; }
5477 // { v = x; --x; }
5478 // { v = x; x binop= expr; }
5479 // { v = x; x = x binop expr; }
5480 // { v = x; x = expr binop x; }
5481 // { x++; v = x; }
5482 // { x--; v = x; }
5483 // { ++x; v = x; }
5484 // { --x; v = x; }
5485 // { x binop= expr; v = x; }
5486 // { x = x binop expr; v = x; }
5487 // { x = expr binop x; v = x; }
5488 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5489 // Check that this is { expr1; expr2; }
5490 if (CS->size() == 2) {
5491 auto *First = CS->body_front();
5492 auto *Second = CS->body_back();
5493 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5494 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5495 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5496 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5497 // Need to find what subexpression is 'v' and what is 'x'.
5498 OpenMPAtomicUpdateChecker Checker(*this);
5499 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5500 BinaryOperator *BinOp = nullptr;
5501 if (IsUpdateExprFound) {
5502 BinOp = dyn_cast<BinaryOperator>(First);
5503 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5504 }
5505 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5506 // { v = x; x++; }
5507 // { v = x; x--; }
5508 // { v = x; ++x; }
5509 // { v = x; --x; }
5510 // { v = x; x binop= expr; }
5511 // { v = x; x = x binop expr; }
5512 // { v = x; x = expr binop x; }
5513 // Check that the first expression has form v = x.
5514 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5515 llvm::FoldingSetNodeID XId, PossibleXId;
5516 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5517 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5518 IsUpdateExprFound = XId == PossibleXId;
5519 if (IsUpdateExprFound) {
5520 V = BinOp->getLHS();
5521 X = Checker.getX();
5522 E = Checker.getExpr();
5523 UE = Checker.getUpdateExpr();
5524 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005525 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005526 }
5527 }
5528 if (!IsUpdateExprFound) {
5529 IsUpdateExprFound = !Checker.checkStatement(First);
5530 BinOp = nullptr;
5531 if (IsUpdateExprFound) {
5532 BinOp = dyn_cast<BinaryOperator>(Second);
5533 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5534 }
5535 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5536 // { x++; v = x; }
5537 // { x--; v = x; }
5538 // { ++x; v = x; }
5539 // { --x; v = x; }
5540 // { x binop= expr; v = x; }
5541 // { x = x binop expr; v = x; }
5542 // { x = expr binop x; v = x; }
5543 // Check that the second expression has form v = x.
5544 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5545 llvm::FoldingSetNodeID XId, PossibleXId;
5546 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5547 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5548 IsUpdateExprFound = XId == PossibleXId;
5549 if (IsUpdateExprFound) {
5550 V = BinOp->getLHS();
5551 X = Checker.getX();
5552 E = Checker.getExpr();
5553 UE = Checker.getUpdateExpr();
5554 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005555 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005556 }
5557 }
5558 }
5559 if (!IsUpdateExprFound) {
5560 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005561 auto *FirstExpr = dyn_cast<Expr>(First);
5562 auto *SecondExpr = dyn_cast<Expr>(Second);
5563 if (!FirstExpr || !SecondExpr ||
5564 !(FirstExpr->isInstantiationDependent() ||
5565 SecondExpr->isInstantiationDependent())) {
5566 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5567 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005568 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005569 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5570 : First->getLocStart();
5571 NoteRange = ErrorRange = FirstBinOp
5572 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005573 : SourceRange(ErrorLoc, ErrorLoc);
5574 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005575 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5576 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5577 ErrorFound = NotAnAssignmentOp;
5578 NoteLoc = ErrorLoc = SecondBinOp
5579 ? SecondBinOp->getOperatorLoc()
5580 : Second->getLocStart();
5581 NoteRange = ErrorRange =
5582 SecondBinOp ? SecondBinOp->getSourceRange()
5583 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005584 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005585 auto *PossibleXRHSInFirst =
5586 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5587 auto *PossibleXLHSInSecond =
5588 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5589 llvm::FoldingSetNodeID X1Id, X2Id;
5590 PossibleXRHSInFirst->Profile(X1Id, Context,
5591 /*Canonical=*/true);
5592 PossibleXLHSInSecond->Profile(X2Id, Context,
5593 /*Canonical=*/true);
5594 IsUpdateExprFound = X1Id == X2Id;
5595 if (IsUpdateExprFound) {
5596 V = FirstBinOp->getLHS();
5597 X = SecondBinOp->getLHS();
5598 E = SecondBinOp->getRHS();
5599 UE = nullptr;
5600 IsXLHSInRHSPart = false;
5601 IsPostfixUpdate = true;
5602 } else {
5603 ErrorFound = NotASpecificExpression;
5604 ErrorLoc = FirstBinOp->getExprLoc();
5605 ErrorRange = FirstBinOp->getSourceRange();
5606 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5607 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5608 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005609 }
5610 }
5611 }
5612 }
5613 } else {
5614 NoteLoc = ErrorLoc = Body->getLocStart();
5615 NoteRange = ErrorRange =
5616 SourceRange(Body->getLocStart(), Body->getLocStart());
5617 ErrorFound = NotTwoSubstatements;
5618 }
5619 } else {
5620 NoteLoc = ErrorLoc = Body->getLocStart();
5621 NoteRange = ErrorRange =
5622 SourceRange(Body->getLocStart(), Body->getLocStart());
5623 ErrorFound = NotACompoundStatement;
5624 }
5625 if (ErrorFound != NoError) {
5626 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5627 << ErrorRange;
5628 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5629 return StmtError();
5630 } else if (CurContext->isDependentContext()) {
5631 UE = V = E = X = nullptr;
5632 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005633 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005634 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005635
5636 getCurFunction()->setHasBranchProtectedScope();
5637
Alexey Bataev62cec442014-11-18 10:14:22 +00005638 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005639 X, V, E, UE, IsXLHSInRHSPart,
5640 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005641}
5642
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005643StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5644 Stmt *AStmt,
5645 SourceLocation StartLoc,
5646 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005647 if (!AStmt)
5648 return StmtError();
5649
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005650 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5651 // 1.2.2 OpenMP Language Terminology
5652 // Structured block - An executable statement with a single entry at the
5653 // top and a single exit at the bottom.
5654 // The point of exit cannot be a branch out of the structured block.
5655 // longjmp() and throw() must not violate the entry/exit criteria.
5656 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005657
Alexey Bataev13314bf2014-10-09 04:18:56 +00005658 // OpenMP [2.16, Nesting of Regions]
5659 // If specified, a teams construct must be contained within a target
5660 // construct. That target construct must contain no statements or directives
5661 // outside of the teams construct.
5662 if (DSAStack->hasInnerTeamsRegion()) {
5663 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5664 bool OMPTeamsFound = true;
5665 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5666 auto I = CS->body_begin();
5667 while (I != CS->body_end()) {
5668 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5669 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5670 OMPTeamsFound = false;
5671 break;
5672 }
5673 ++I;
5674 }
5675 assert(I != CS->body_end() && "Not found statement");
5676 S = *I;
5677 }
5678 if (!OMPTeamsFound) {
5679 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5680 Diag(DSAStack->getInnerTeamsRegionLoc(),
5681 diag::note_omp_nested_teams_construct_here);
5682 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5683 << isa<OMPExecutableDirective>(S);
5684 return StmtError();
5685 }
5686 }
5687
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005688 getCurFunction()->setHasBranchProtectedScope();
5689
5690 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5691}
5692
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005693StmtResult
5694Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5695 Stmt *AStmt, SourceLocation StartLoc,
5696 SourceLocation EndLoc) {
5697 if (!AStmt)
5698 return StmtError();
5699
5700 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5701 // 1.2.2 OpenMP Language Terminology
5702 // Structured block - An executable statement with a single entry at the
5703 // top and a single exit at the bottom.
5704 // The point of exit cannot be a branch out of the structured block.
5705 // longjmp() and throw() must not violate the entry/exit criteria.
5706 CS->getCapturedDecl()->setNothrow();
5707
5708 getCurFunction()->setHasBranchProtectedScope();
5709
5710 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5711 AStmt);
5712}
5713
Samuel Antaodf67fc42016-01-19 19:15:56 +00005714/// \brief Check for existence of a map clause in the list of clauses.
5715static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5716 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5717 I != E; ++I) {
5718 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5719 return true;
5720 }
5721 }
5722
5723 return false;
5724}
5725
Michael Wong65f367f2015-07-21 13:44:28 +00005726StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5727 Stmt *AStmt,
5728 SourceLocation StartLoc,
5729 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005730 if (!AStmt)
5731 return StmtError();
5732
5733 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5734
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005735 // OpenMP [2.10.1, Restrictions, p. 97]
5736 // At least one map clause must appear on the directive.
5737 if (!HasMapClause(Clauses)) {
5738 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5739 getOpenMPDirectiveName(OMPD_target_data);
5740 return StmtError();
5741 }
5742
Michael Wong65f367f2015-07-21 13:44:28 +00005743 getCurFunction()->setHasBranchProtectedScope();
5744
5745 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5746 AStmt);
5747}
5748
Samuel Antaodf67fc42016-01-19 19:15:56 +00005749StmtResult
5750Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5751 SourceLocation StartLoc,
5752 SourceLocation EndLoc) {
5753 // OpenMP [2.10.2, Restrictions, p. 99]
5754 // At least one map clause must appear on the directive.
5755 if (!HasMapClause(Clauses)) {
5756 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5757 << getOpenMPDirectiveName(OMPD_target_enter_data);
5758 return StmtError();
5759 }
5760
5761 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5762 Clauses);
5763}
5764
Samuel Antao72590762016-01-19 20:04:50 +00005765StmtResult
5766Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5767 SourceLocation StartLoc,
5768 SourceLocation EndLoc) {
5769 // OpenMP [2.10.3, Restrictions, p. 102]
5770 // At least one map clause must appear on the directive.
5771 if (!HasMapClause(Clauses)) {
5772 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5773 << getOpenMPDirectiveName(OMPD_target_exit_data);
5774 return StmtError();
5775 }
5776
5777 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5778}
5779
Alexey Bataev13314bf2014-10-09 04:18:56 +00005780StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5781 Stmt *AStmt, SourceLocation StartLoc,
5782 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005783 if (!AStmt)
5784 return StmtError();
5785
Alexey Bataev13314bf2014-10-09 04:18:56 +00005786 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5787 // 1.2.2 OpenMP Language Terminology
5788 // Structured block - An executable statement with a single entry at the
5789 // top and a single exit at the bottom.
5790 // The point of exit cannot be a branch out of the structured block.
5791 // longjmp() and throw() must not violate the entry/exit criteria.
5792 CS->getCapturedDecl()->setNothrow();
5793
5794 getCurFunction()->setHasBranchProtectedScope();
5795
5796 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5797}
5798
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005799StmtResult
5800Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5801 SourceLocation EndLoc,
5802 OpenMPDirectiveKind CancelRegion) {
5803 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5804 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5805 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5806 << getOpenMPDirectiveName(CancelRegion);
5807 return StmtError();
5808 }
5809 if (DSAStack->isParentNowaitRegion()) {
5810 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5811 return StmtError();
5812 }
5813 if (DSAStack->isParentOrderedRegion()) {
5814 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5815 return StmtError();
5816 }
5817 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5818 CancelRegion);
5819}
5820
Alexey Bataev87933c72015-09-18 08:07:34 +00005821StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5822 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005823 SourceLocation EndLoc,
5824 OpenMPDirectiveKind CancelRegion) {
5825 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5826 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5827 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5828 << getOpenMPDirectiveName(CancelRegion);
5829 return StmtError();
5830 }
5831 if (DSAStack->isParentNowaitRegion()) {
5832 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5833 return StmtError();
5834 }
5835 if (DSAStack->isParentOrderedRegion()) {
5836 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5837 return StmtError();
5838 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005839 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005840 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5841 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005842}
5843
Alexey Bataev382967a2015-12-08 12:06:20 +00005844static bool checkGrainsizeNumTasksClauses(Sema &S,
5845 ArrayRef<OMPClause *> Clauses) {
5846 OMPClause *PrevClause = nullptr;
5847 bool ErrorFound = false;
5848 for (auto *C : Clauses) {
5849 if (C->getClauseKind() == OMPC_grainsize ||
5850 C->getClauseKind() == OMPC_num_tasks) {
5851 if (!PrevClause)
5852 PrevClause = C;
5853 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5854 S.Diag(C->getLocStart(),
5855 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5856 << getOpenMPClauseName(C->getClauseKind())
5857 << getOpenMPClauseName(PrevClause->getClauseKind());
5858 S.Diag(PrevClause->getLocStart(),
5859 diag::note_omp_previous_grainsize_num_tasks)
5860 << getOpenMPClauseName(PrevClause->getClauseKind());
5861 ErrorFound = true;
5862 }
5863 }
5864 }
5865 return ErrorFound;
5866}
5867
Alexey Bataev49f6e782015-12-01 04:18:41 +00005868StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5869 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5870 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005871 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005872 if (!AStmt)
5873 return StmtError();
5874
5875 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5876 OMPLoopDirective::HelperExprs B;
5877 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5878 // define the nested loops number.
5879 unsigned NestedLoopCount =
5880 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005881 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005882 VarsWithImplicitDSA, B);
5883 if (NestedLoopCount == 0)
5884 return StmtError();
5885
5886 assert((CurContext->isDependentContext() || B.builtAll()) &&
5887 "omp for loop exprs were not built");
5888
Alexey Bataev382967a2015-12-08 12:06:20 +00005889 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5890 // The grainsize clause and num_tasks clause are mutually exclusive and may
5891 // not appear on the same taskloop directive.
5892 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5893 return StmtError();
5894
Alexey Bataev49f6e782015-12-01 04:18:41 +00005895 getCurFunction()->setHasBranchProtectedScope();
5896 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5897 NestedLoopCount, Clauses, AStmt, B);
5898}
5899
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005900StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5901 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5902 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005903 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005904 if (!AStmt)
5905 return StmtError();
5906
5907 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5908 OMPLoopDirective::HelperExprs B;
5909 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5910 // define the nested loops number.
5911 unsigned NestedLoopCount =
5912 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5913 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5914 VarsWithImplicitDSA, B);
5915 if (NestedLoopCount == 0)
5916 return StmtError();
5917
5918 assert((CurContext->isDependentContext() || B.builtAll()) &&
5919 "omp for loop exprs were not built");
5920
Alexey Bataev382967a2015-12-08 12:06:20 +00005921 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5922 // The grainsize clause and num_tasks clause are mutually exclusive and may
5923 // not appear on the same taskloop directive.
5924 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5925 return StmtError();
5926
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005927 getCurFunction()->setHasBranchProtectedScope();
5928 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5929 NestedLoopCount, Clauses, AStmt, B);
5930}
5931
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005932StmtResult Sema::ActOnOpenMPDistributeDirective(
5933 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5934 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005935 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005936 if (!AStmt)
5937 return StmtError();
5938
5939 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5940 OMPLoopDirective::HelperExprs B;
5941 // In presence of clause 'collapse' with number of loops, it will
5942 // define the nested loops number.
5943 unsigned NestedLoopCount =
5944 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5945 nullptr /*ordered not a clause on distribute*/, AStmt,
5946 *this, *DSAStack, VarsWithImplicitDSA, B);
5947 if (NestedLoopCount == 0)
5948 return StmtError();
5949
5950 assert((CurContext->isDependentContext() || B.builtAll()) &&
5951 "omp for loop exprs were not built");
5952
5953 getCurFunction()->setHasBranchProtectedScope();
5954 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5955 NestedLoopCount, Clauses, AStmt, B);
5956}
5957
Alexey Bataeved09d242014-05-28 05:53:51 +00005958OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005959 SourceLocation StartLoc,
5960 SourceLocation LParenLoc,
5961 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005962 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005963 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005964 case OMPC_final:
5965 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5966 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005967 case OMPC_num_threads:
5968 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5969 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005970 case OMPC_safelen:
5971 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5972 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005973 case OMPC_simdlen:
5974 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5975 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005976 case OMPC_collapse:
5977 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5978 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005979 case OMPC_ordered:
5980 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5981 break;
Michael Wonge710d542015-08-07 16:16:36 +00005982 case OMPC_device:
5983 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5984 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005985 case OMPC_num_teams:
5986 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5987 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005988 case OMPC_thread_limit:
5989 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5990 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005991 case OMPC_priority:
5992 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5993 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005994 case OMPC_grainsize:
5995 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5996 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005997 case OMPC_num_tasks:
5998 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5999 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006000 case OMPC_hint:
6001 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6002 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006003 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006004 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006005 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006006 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006007 case OMPC_private:
6008 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006009 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006010 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006011 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006012 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006013 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006014 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006015 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006016 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006017 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006018 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006019 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006020 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006021 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006022 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006023 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006024 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006025 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006026 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006027 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006028 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006029 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006030 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006031 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006032 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006033 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006034 llvm_unreachable("Clause is not allowed.");
6035 }
6036 return Res;
6037}
6038
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006039OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6040 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006041 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006042 SourceLocation NameModifierLoc,
6043 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006044 SourceLocation EndLoc) {
6045 Expr *ValExpr = Condition;
6046 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6047 !Condition->isInstantiationDependent() &&
6048 !Condition->containsUnexpandedParameterPack()) {
6049 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006050 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006051 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006052 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006053
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006054 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006055 }
6056
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006057 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6058 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006059}
6060
Alexey Bataev3778b602014-07-17 07:32:53 +00006061OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6062 SourceLocation StartLoc,
6063 SourceLocation LParenLoc,
6064 SourceLocation EndLoc) {
6065 Expr *ValExpr = Condition;
6066 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6067 !Condition->isInstantiationDependent() &&
6068 !Condition->containsUnexpandedParameterPack()) {
6069 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6070 Condition->getExprLoc(), Condition);
6071 if (Val.isInvalid())
6072 return nullptr;
6073
6074 ValExpr = Val.get();
6075 }
6076
6077 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6078}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006079ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6080 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006081 if (!Op)
6082 return ExprError();
6083
6084 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6085 public:
6086 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006087 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006088 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6089 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006090 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6091 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006092 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6093 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006094 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6095 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006096 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6097 QualType T,
6098 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006099 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6100 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006101 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6102 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006103 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006104 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006105 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006106 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6107 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006108 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6109 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006110 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6111 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006112 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006113 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006114 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006115 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6116 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006117 llvm_unreachable("conversion functions are permitted");
6118 }
6119 } ConvertDiagnoser;
6120 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6121}
6122
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006123static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006124 OpenMPClauseKind CKind,
6125 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006126 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6127 !ValExpr->isInstantiationDependent()) {
6128 SourceLocation Loc = ValExpr->getExprLoc();
6129 ExprResult Value =
6130 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6131 if (Value.isInvalid())
6132 return false;
6133
6134 ValExpr = Value.get();
6135 // The expression must evaluate to a non-negative integer value.
6136 llvm::APSInt Result;
6137 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006138 Result.isSigned() &&
6139 !((!StrictlyPositive && Result.isNonNegative()) ||
6140 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006141 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006142 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6143 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006144 return false;
6145 }
6146 }
6147 return true;
6148}
6149
Alexey Bataev568a8332014-03-06 06:15:19 +00006150OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6151 SourceLocation StartLoc,
6152 SourceLocation LParenLoc,
6153 SourceLocation EndLoc) {
6154 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006155
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006156 // OpenMP [2.5, Restrictions]
6157 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006158 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6159 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006160 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006161
Alexey Bataeved09d242014-05-28 05:53:51 +00006162 return new (Context)
6163 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006164}
6165
Alexey Bataev62c87d22014-03-21 04:51:18 +00006166ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006167 OpenMPClauseKind CKind,
6168 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006169 if (!E)
6170 return ExprError();
6171 if (E->isValueDependent() || E->isTypeDependent() ||
6172 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006173 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006174 llvm::APSInt Result;
6175 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6176 if (ICE.isInvalid())
6177 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006178 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6179 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006180 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006181 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6182 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006183 return ExprError();
6184 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006185 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6186 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6187 << E->getSourceRange();
6188 return ExprError();
6189 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006190 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6191 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006192 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006193 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006194 return ICE;
6195}
6196
6197OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6198 SourceLocation LParenLoc,
6199 SourceLocation EndLoc) {
6200 // OpenMP [2.8.1, simd construct, Description]
6201 // The parameter of the safelen clause must be a constant
6202 // positive integer expression.
6203 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6204 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006205 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006206 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006207 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006208}
6209
Alexey Bataev66b15b52015-08-21 11:14:16 +00006210OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6211 SourceLocation LParenLoc,
6212 SourceLocation EndLoc) {
6213 // OpenMP [2.8.1, simd construct, Description]
6214 // The parameter of the simdlen clause must be a constant
6215 // positive integer expression.
6216 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6217 if (Simdlen.isInvalid())
6218 return nullptr;
6219 return new (Context)
6220 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6221}
6222
Alexander Musman64d33f12014-06-04 07:53:32 +00006223OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6224 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006225 SourceLocation LParenLoc,
6226 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006227 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006228 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006229 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006230 // The parameter of the collapse clause must be a constant
6231 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006232 ExprResult NumForLoopsResult =
6233 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6234 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006235 return nullptr;
6236 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006237 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006238}
6239
Alexey Bataev10e775f2015-07-30 11:36:16 +00006240OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6241 SourceLocation EndLoc,
6242 SourceLocation LParenLoc,
6243 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006244 // OpenMP [2.7.1, loop construct, Description]
6245 // OpenMP [2.8.1, simd construct, Description]
6246 // OpenMP [2.9.6, distribute construct, Description]
6247 // The parameter of the ordered clause must be a constant
6248 // positive integer expression if any.
6249 if (NumForLoops && LParenLoc.isValid()) {
6250 ExprResult NumForLoopsResult =
6251 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6252 if (NumForLoopsResult.isInvalid())
6253 return nullptr;
6254 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006255 } else
6256 NumForLoops = nullptr;
6257 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006258 return new (Context)
6259 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6260}
6261
Alexey Bataeved09d242014-05-28 05:53:51 +00006262OMPClause *Sema::ActOnOpenMPSimpleClause(
6263 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6264 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006265 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006266 switch (Kind) {
6267 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006268 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006269 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6270 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006271 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006272 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006273 Res = ActOnOpenMPProcBindClause(
6274 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6275 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006276 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006277 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006278 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006279 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006280 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006281 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006282 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006283 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006284 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006285 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006286 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006287 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006288 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006289 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006290 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006291 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006292 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006293 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006294 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006295 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006296 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006297 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006298 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006299 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006300 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006301 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006302 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006303 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006304 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006305 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006306 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006307 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006308 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006309 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006310 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006311 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006312 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006313 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006314 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006315 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006316 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006317 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006318 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006319 llvm_unreachable("Clause is not allowed.");
6320 }
6321 return Res;
6322}
6323
Alexey Bataev6402bca2015-12-28 07:25:51 +00006324static std::string
6325getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6326 ArrayRef<unsigned> Exclude = llvm::None) {
6327 std::string Values;
6328 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6329 unsigned Skipped = Exclude.size();
6330 auto S = Exclude.begin(), E = Exclude.end();
6331 for (unsigned i = First; i < Last; ++i) {
6332 if (std::find(S, E, i) != E) {
6333 --Skipped;
6334 continue;
6335 }
6336 Values += "'";
6337 Values += getOpenMPSimpleClauseTypeName(K, i);
6338 Values += "'";
6339 if (i == Bound - Skipped)
6340 Values += " or ";
6341 else if (i != Bound + 1 - Skipped)
6342 Values += ", ";
6343 }
6344 return Values;
6345}
6346
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006347OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6348 SourceLocation KindKwLoc,
6349 SourceLocation StartLoc,
6350 SourceLocation LParenLoc,
6351 SourceLocation EndLoc) {
6352 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006353 static_assert(OMPC_DEFAULT_unknown > 0,
6354 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006355 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006356 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6357 /*Last=*/OMPC_DEFAULT_unknown)
6358 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006359 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006360 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006361 switch (Kind) {
6362 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006363 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006364 break;
6365 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006366 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006367 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006368 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006369 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006370 break;
6371 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006372 return new (Context)
6373 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006374}
6375
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006376OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6377 SourceLocation KindKwLoc,
6378 SourceLocation StartLoc,
6379 SourceLocation LParenLoc,
6380 SourceLocation EndLoc) {
6381 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006382 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006383 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6384 /*Last=*/OMPC_PROC_BIND_unknown)
6385 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006386 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006387 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006388 return new (Context)
6389 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006390}
6391
Alexey Bataev56dafe82014-06-20 07:16:17 +00006392OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006393 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006394 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006395 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006396 SourceLocation EndLoc) {
6397 OMPClause *Res = nullptr;
6398 switch (Kind) {
6399 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006400 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6401 assert(Argument.size() == NumberOfElements &&
6402 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006403 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006404 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6405 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6406 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6407 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6408 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006409 break;
6410 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006411 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6412 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6413 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6414 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006415 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006416 case OMPC_dist_schedule:
6417 Res = ActOnOpenMPDistScheduleClause(
6418 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6419 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6420 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006421 case OMPC_defaultmap:
6422 enum { Modifier, DefaultmapKind };
6423 Res = ActOnOpenMPDefaultmapClause(
6424 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6425 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6426 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6427 ArgumentLoc[DefaultmapKind], EndLoc);
6428 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006429 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006430 case OMPC_num_threads:
6431 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006432 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006433 case OMPC_collapse:
6434 case OMPC_default:
6435 case OMPC_proc_bind:
6436 case OMPC_private:
6437 case OMPC_firstprivate:
6438 case OMPC_lastprivate:
6439 case OMPC_shared:
6440 case OMPC_reduction:
6441 case OMPC_linear:
6442 case OMPC_aligned:
6443 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006444 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006445 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006446 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006447 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006448 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006449 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006450 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006451 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006452 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006453 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006454 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006455 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006456 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006457 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006458 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006459 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006460 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006461 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006462 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006463 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006464 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006465 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006466 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006467 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006468 case OMPC_unknown:
6469 llvm_unreachable("Clause is not allowed.");
6470 }
6471 return Res;
6472}
6473
Alexey Bataev6402bca2015-12-28 07:25:51 +00006474static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6475 OpenMPScheduleClauseModifier M2,
6476 SourceLocation M1Loc, SourceLocation M2Loc) {
6477 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6478 SmallVector<unsigned, 2> Excluded;
6479 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6480 Excluded.push_back(M2);
6481 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6482 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6483 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6484 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6485 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6486 << getListOfPossibleValues(OMPC_schedule,
6487 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6488 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6489 Excluded)
6490 << getOpenMPClauseName(OMPC_schedule);
6491 return true;
6492 }
6493 return false;
6494}
6495
Alexey Bataev56dafe82014-06-20 07:16:17 +00006496OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006497 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006498 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006499 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6500 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6501 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6502 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6503 return nullptr;
6504 // OpenMP, 2.7.1, Loop Construct, Restrictions
6505 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6506 // but not both.
6507 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6508 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6509 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6510 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6511 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6512 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6513 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6514 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6515 return nullptr;
6516 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006517 if (Kind == OMPC_SCHEDULE_unknown) {
6518 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006519 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6520 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6521 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6522 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6523 Exclude);
6524 } else {
6525 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6526 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006527 }
6528 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6529 << Values << getOpenMPClauseName(OMPC_schedule);
6530 return nullptr;
6531 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006532 // OpenMP, 2.7.1, Loop Construct, Restrictions
6533 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6534 // schedule(guided).
6535 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6536 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6537 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6538 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6539 diag::err_omp_schedule_nonmonotonic_static);
6540 return nullptr;
6541 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006542 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006543 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006544 if (ChunkSize) {
6545 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6546 !ChunkSize->isInstantiationDependent() &&
6547 !ChunkSize->containsUnexpandedParameterPack()) {
6548 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6549 ExprResult Val =
6550 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6551 if (Val.isInvalid())
6552 return nullptr;
6553
6554 ValExpr = Val.get();
6555
6556 // OpenMP [2.7.1, Restrictions]
6557 // chunk_size must be a loop invariant integer expression with a positive
6558 // value.
6559 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006560 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6561 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6562 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006563 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006564 return nullptr;
6565 }
6566 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6567 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6568 ChunkSize->getType(), ".chunk.");
6569 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6570 ChunkSize->getExprLoc(),
6571 /*RefersToCapture=*/true);
6572 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006573 }
6574 }
6575 }
6576
Alexey Bataev6402bca2015-12-28 07:25:51 +00006577 return new (Context)
6578 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6579 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006580}
6581
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006582OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6583 SourceLocation StartLoc,
6584 SourceLocation EndLoc) {
6585 OMPClause *Res = nullptr;
6586 switch (Kind) {
6587 case OMPC_ordered:
6588 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6589 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006590 case OMPC_nowait:
6591 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6592 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006593 case OMPC_untied:
6594 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6595 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006596 case OMPC_mergeable:
6597 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6598 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006599 case OMPC_read:
6600 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6601 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006602 case OMPC_write:
6603 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6604 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006605 case OMPC_update:
6606 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6607 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006608 case OMPC_capture:
6609 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6610 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006611 case OMPC_seq_cst:
6612 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6613 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006614 case OMPC_threads:
6615 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6616 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006617 case OMPC_simd:
6618 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6619 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006620 case OMPC_nogroup:
6621 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6622 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006623 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006624 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006625 case OMPC_num_threads:
6626 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006627 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006628 case OMPC_collapse:
6629 case OMPC_schedule:
6630 case OMPC_private:
6631 case OMPC_firstprivate:
6632 case OMPC_lastprivate:
6633 case OMPC_shared:
6634 case OMPC_reduction:
6635 case OMPC_linear:
6636 case OMPC_aligned:
6637 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006638 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006639 case OMPC_default:
6640 case OMPC_proc_bind:
6641 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006642 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006643 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006644 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006645 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006646 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006647 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006648 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006649 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006650 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006651 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006652 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006653 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006654 case OMPC_unknown:
6655 llvm_unreachable("Clause is not allowed.");
6656 }
6657 return Res;
6658}
6659
Alexey Bataev236070f2014-06-20 11:19:47 +00006660OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6661 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006662 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006663 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6664}
6665
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006666OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6667 SourceLocation EndLoc) {
6668 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6669}
6670
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006671OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6672 SourceLocation EndLoc) {
6673 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6674}
6675
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006676OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6677 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006678 return new (Context) OMPReadClause(StartLoc, EndLoc);
6679}
6680
Alexey Bataevdea47612014-07-23 07:46:59 +00006681OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6682 SourceLocation EndLoc) {
6683 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6684}
6685
Alexey Bataev67a4f222014-07-23 10:25:33 +00006686OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6687 SourceLocation EndLoc) {
6688 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6689}
6690
Alexey Bataev459dec02014-07-24 06:46:57 +00006691OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6692 SourceLocation EndLoc) {
6693 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6694}
6695
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006696OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6697 SourceLocation EndLoc) {
6698 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6699}
6700
Alexey Bataev346265e2015-09-25 10:37:12 +00006701OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6702 SourceLocation EndLoc) {
6703 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6704}
6705
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006706OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6707 SourceLocation EndLoc) {
6708 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6709}
6710
Alexey Bataevb825de12015-12-07 10:51:44 +00006711OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6712 SourceLocation EndLoc) {
6713 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6714}
6715
Alexey Bataevc5e02582014-06-16 07:08:35 +00006716OMPClause *Sema::ActOnOpenMPVarListClause(
6717 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6718 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6719 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006720 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006721 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6722 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6723 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006724 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006725 switch (Kind) {
6726 case OMPC_private:
6727 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6728 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006729 case OMPC_firstprivate:
6730 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6731 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006732 case OMPC_lastprivate:
6733 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6734 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006735 case OMPC_shared:
6736 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6737 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006738 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006739 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6740 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006741 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006742 case OMPC_linear:
6743 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006744 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006745 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006746 case OMPC_aligned:
6747 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6748 ColonLoc, EndLoc);
6749 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006750 case OMPC_copyin:
6751 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6752 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006753 case OMPC_copyprivate:
6754 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6755 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006756 case OMPC_flush:
6757 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6758 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006759 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006760 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6761 StartLoc, LParenLoc, EndLoc);
6762 break;
6763 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006764 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6765 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6766 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006767 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006768 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006769 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006770 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006771 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006772 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006773 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006774 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006775 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006776 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006777 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006778 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006779 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006780 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006781 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006782 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006783 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006784 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006785 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006786 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006787 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006788 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006789 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006790 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006791 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006792 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006793 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006794 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006795 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006796 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006797 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006798 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006799 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006800 llvm_unreachable("Clause is not allowed.");
6801 }
6802 return Res;
6803}
6804
6805OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6806 SourceLocation StartLoc,
6807 SourceLocation LParenLoc,
6808 SourceLocation EndLoc) {
6809 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006810 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006811 for (auto &RefExpr : VarList) {
6812 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006813 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6814 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006815 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006816 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006817 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006818 continue;
6819 }
6820
Alexey Bataeved09d242014-05-28 05:53:51 +00006821 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006822 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006823 // A list item is a variable name.
6824 // OpenMP [2.9.3.3, Restrictions, p.1]
6825 // A variable that is part of another variable (as an array or
6826 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006827 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6828 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6829 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6830 (getCurrentThisType().isNull() || !ME ||
6831 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6832 !isa<FieldDecl>(ME->getMemberDecl()))) {
6833 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6834 << (getCurrentThisType().isNull() ? 0 : 1)
6835 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006836 continue;
6837 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006838 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6839 QualType Type = D->getType();
6840 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006841
6842 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6843 // A variable that appears in a private clause must not have an incomplete
6844 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006845 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006846 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006847 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006848
Alexey Bataev758e55e2013-09-06 18:03:48 +00006849 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6850 // in a Construct]
6851 // Variables with the predetermined data-sharing attributes may not be
6852 // listed in data-sharing attributes clauses, except for the cases
6853 // listed below. For these exceptions only, listing a predetermined
6854 // variable in a data-sharing attribute clause is allowed and overrides
6855 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006856 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006857 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006858 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6859 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006860 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006861 continue;
6862 }
6863
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006864 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006865 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006866 DSAStack->getCurrentDirective() == OMPD_task) {
6867 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6868 << getOpenMPClauseName(OMPC_private) << Type
6869 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6870 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006871 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006872 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006873 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006874 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006875 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006876 continue;
6877 }
6878
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006879 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6880 // A variable of class type (or array thereof) that appears in a private
6881 // clause requires an accessible, unambiguous default constructor for the
6882 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006883 // Generate helper private variable and initialize it with the default
6884 // value. The address of the original variable is replaced by the address of
6885 // the new private variable in CodeGen. This new variable is not added to
6886 // IdResolver, so the code in the OpenMP region uses original variable for
6887 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006888 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006889 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
6890 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006891 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006892 if (VDPrivate->isInvalidDecl())
6893 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006894 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006895 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006896
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006897 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
6898 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006899 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006900 }
6901
Alexey Bataeved09d242014-05-28 05:53:51 +00006902 if (Vars.empty())
6903 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006904
Alexey Bataev03b340a2014-10-21 03:16:40 +00006905 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6906 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006907}
6908
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006909namespace {
6910class DiagsUninitializedSeveretyRAII {
6911private:
6912 DiagnosticsEngine &Diags;
6913 SourceLocation SavedLoc;
6914 bool IsIgnored;
6915
6916public:
6917 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6918 bool IsIgnored)
6919 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6920 if (!IsIgnored) {
6921 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6922 /*Map*/ diag::Severity::Ignored, Loc);
6923 }
6924 }
6925 ~DiagsUninitializedSeveretyRAII() {
6926 if (!IsIgnored)
6927 Diags.popMappings(SavedLoc);
6928 }
6929};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006930}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006931
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006932OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6933 SourceLocation StartLoc,
6934 SourceLocation LParenLoc,
6935 SourceLocation EndLoc) {
6936 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006937 SmallVector<Expr *, 8> PrivateCopies;
6938 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006939 bool IsImplicitClause =
6940 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6941 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6942
Alexey Bataeved09d242014-05-28 05:53:51 +00006943 for (auto &RefExpr : VarList) {
6944 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6945 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006946 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006947 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006948 PrivateCopies.push_back(nullptr);
6949 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006950 continue;
6951 }
6952
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006953 SourceLocation ELoc =
6954 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006955 // OpenMP [2.1, C/C++]
6956 // A list item is a variable name.
6957 // OpenMP [2.9.3.3, Restrictions, p.1]
6958 // A variable that is part of another variable (as an array or
6959 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006960 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006961 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006962 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6963 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006964 continue;
6965 }
6966 Decl *D = DE->getDecl();
6967 VarDecl *VD = cast<VarDecl>(D);
6968
6969 QualType Type = VD->getType();
6970 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6971 // It will be analyzed later.
6972 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006973 PrivateCopies.push_back(nullptr);
6974 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006975 continue;
6976 }
6977
6978 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6979 // A variable that appears in a private clause must not have an incomplete
6980 // type or a reference type.
6981 if (RequireCompleteType(ELoc, Type,
6982 diag::err_omp_firstprivate_incomplete_type)) {
6983 continue;
6984 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006985 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006986
6987 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6988 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006989 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006990 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006991 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006992
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006993 // If an implicit firstprivate variable found it was checked already.
6994 if (!IsImplicitClause) {
6995 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006996 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006997 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6998 // A list item that specifies a given variable may not appear in more
6999 // than one clause on the same directive, except that a variable may be
7000 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007001 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007002 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007003 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007004 << getOpenMPClauseName(DVar.CKind)
7005 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007006 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007007 continue;
7008 }
7009
7010 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7011 // in a Construct]
7012 // Variables with the predetermined data-sharing attributes may not be
7013 // listed in data-sharing attributes clauses, except for the cases
7014 // listed below. For these exceptions only, listing a predetermined
7015 // variable in a data-sharing attribute clause is allowed and overrides
7016 // the variable's predetermined data-sharing attributes.
7017 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7018 // in a Construct, C/C++, p.2]
7019 // Variables with const-qualified type having no mutable member may be
7020 // listed in a firstprivate clause, even if they are static data members.
7021 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7022 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7023 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007024 << getOpenMPClauseName(DVar.CKind)
7025 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007026 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007027 continue;
7028 }
7029
Alexey Bataevf29276e2014-06-18 04:14:57 +00007030 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007031 // OpenMP [2.9.3.4, Restrictions, p.2]
7032 // A list item that is private within a parallel region must not appear
7033 // in a firstprivate clause on a worksharing construct if any of the
7034 // worksharing regions arising from the worksharing construct ever bind
7035 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007036 if (isOpenMPWorksharingDirective(CurrDir) &&
7037 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007038 DVar = DSAStack->getImplicitDSA(VD, true);
7039 if (DVar.CKind != OMPC_shared &&
7040 (isOpenMPParallelDirective(DVar.DKind) ||
7041 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007042 Diag(ELoc, diag::err_omp_required_access)
7043 << getOpenMPClauseName(OMPC_firstprivate)
7044 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007045 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007046 continue;
7047 }
7048 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007049 // OpenMP [2.9.3.4, Restrictions, p.3]
7050 // A list item that appears in a reduction clause of a parallel construct
7051 // must not appear in a firstprivate clause on a worksharing or task
7052 // construct if any of the worksharing or task regions arising from the
7053 // worksharing or task construct ever bind to any of the parallel regions
7054 // arising from the parallel construct.
7055 // OpenMP [2.9.3.4, Restrictions, p.4]
7056 // A list item that appears in a reduction clause in worksharing
7057 // construct must not appear in a firstprivate clause in a task construct
7058 // encountered during execution of any of the worksharing regions arising
7059 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007060 if (CurrDir == OMPD_task) {
7061 DVar =
7062 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7063 [](OpenMPDirectiveKind K) -> bool {
7064 return isOpenMPParallelDirective(K) ||
7065 isOpenMPWorksharingDirective(K);
7066 },
7067 false);
7068 if (DVar.CKind == OMPC_reduction &&
7069 (isOpenMPParallelDirective(DVar.DKind) ||
7070 isOpenMPWorksharingDirective(DVar.DKind))) {
7071 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7072 << getOpenMPDirectiveName(DVar.DKind);
7073 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7074 continue;
7075 }
7076 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007077
7078 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7079 // A list item that is private within a teams region must not appear in a
7080 // firstprivate clause on a distribute construct if any of the distribute
7081 // regions arising from the distribute construct ever bind to any of the
7082 // teams regions arising from the teams construct.
7083 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7084 // A list item that appears in a reduction clause of a teams construct
7085 // must not appear in a firstprivate clause on a distribute construct if
7086 // any of the distribute regions arising from the distribute construct
7087 // ever bind to any of the teams regions arising from the teams construct.
7088 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7089 // A list item may appear in a firstprivate or lastprivate clause but not
7090 // both.
7091 if (CurrDir == OMPD_distribute) {
7092 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7093 [](OpenMPDirectiveKind K) -> bool {
7094 return isOpenMPTeamsDirective(K);
7095 },
7096 false);
7097 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7098 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7099 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7100 continue;
7101 }
7102 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7103 [](OpenMPDirectiveKind K) -> bool {
7104 return isOpenMPTeamsDirective(K);
7105 },
7106 false);
7107 if (DVar.CKind == OMPC_reduction &&
7108 isOpenMPTeamsDirective(DVar.DKind)) {
7109 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7110 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7111 continue;
7112 }
7113 DVar = DSAStack->getTopDSA(VD, false);
7114 if (DVar.CKind == OMPC_lastprivate) {
7115 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7116 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7117 continue;
7118 }
7119 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007120 }
7121
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007122 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007123 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007124 DSAStack->getCurrentDirective() == OMPD_task) {
7125 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7126 << getOpenMPClauseName(OMPC_firstprivate) << Type
7127 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7128 bool IsDecl =
7129 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7130 Diag(VD->getLocation(),
7131 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7132 << VD;
7133 continue;
7134 }
7135
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007136 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007137 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7138 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007139 // Generate helper private variable and initialize it with the value of the
7140 // original variable. The address of the original variable is replaced by
7141 // the address of the new private variable in the CodeGen. This new variable
7142 // is not added to IdResolver, so the code in the OpenMP region uses
7143 // original variable for proper diagnostics and variable capturing.
7144 Expr *VDInitRefExpr = nullptr;
7145 // For arrays generate initializer for single element and replace it by the
7146 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007147 if (Type->isArrayType()) {
7148 auto VDInit =
7149 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7150 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007151 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007152 ElemType = ElemType.getUnqualifiedType();
7153 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7154 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007155 InitializedEntity Entity =
7156 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007157 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7158
7159 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7160 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7161 if (Result.isInvalid())
7162 VDPrivate->setInvalidDecl();
7163 else
7164 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007165 // Remove temp variable declaration.
7166 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007167 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007168 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007169 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007170 VDInitRefExpr =
7171 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007172 AddInitializerToDecl(VDPrivate,
7173 DefaultLvalueConversion(VDInitRefExpr).get(),
7174 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007175 }
7176 if (VDPrivate->isInvalidDecl()) {
7177 if (IsImplicitClause) {
7178 Diag(DE->getExprLoc(),
7179 diag::note_omp_task_predetermined_firstprivate_here);
7180 }
7181 continue;
7182 }
7183 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007184 auto VDPrivateRefExpr = buildDeclRefExpr(
7185 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007186 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7187 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007188 PrivateCopies.push_back(VDPrivateRefExpr);
7189 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007190 }
7191
Alexey Bataeved09d242014-05-28 05:53:51 +00007192 if (Vars.empty())
7193 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007194
7195 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007196 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007197}
7198
Alexander Musman1bb328c2014-06-04 13:06:39 +00007199OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7200 SourceLocation StartLoc,
7201 SourceLocation LParenLoc,
7202 SourceLocation EndLoc) {
7203 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007204 SmallVector<Expr *, 8> SrcExprs;
7205 SmallVector<Expr *, 8> DstExprs;
7206 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007207 for (auto &RefExpr : VarList) {
7208 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7209 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7210 // It will be analyzed later.
7211 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007212 SrcExprs.push_back(nullptr);
7213 DstExprs.push_back(nullptr);
7214 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007215 continue;
7216 }
7217
7218 SourceLocation ELoc = RefExpr->getExprLoc();
7219 // OpenMP [2.1, C/C++]
7220 // A list item is a variable name.
7221 // OpenMP [2.14.3.5, Restrictions, p.1]
7222 // A variable that is part of another variable (as an array or structure
7223 // element) cannot appear in a lastprivate clause.
7224 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7225 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007226 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7227 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007228 continue;
7229 }
7230 Decl *D = DE->getDecl();
7231 VarDecl *VD = cast<VarDecl>(D);
7232
7233 QualType Type = VD->getType();
7234 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7235 // It will be analyzed later.
7236 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007237 SrcExprs.push_back(nullptr);
7238 DstExprs.push_back(nullptr);
7239 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007240 continue;
7241 }
7242
7243 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7244 // A variable that appears in a lastprivate clause must not have an
7245 // incomplete type or a reference type.
7246 if (RequireCompleteType(ELoc, Type,
7247 diag::err_omp_lastprivate_incomplete_type)) {
7248 continue;
7249 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007250 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007251
7252 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7253 // in a Construct]
7254 // Variables with the predetermined data-sharing attributes may not be
7255 // listed in data-sharing attributes clauses, except for the cases
7256 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007257 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007258 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7259 DVar.CKind != OMPC_firstprivate &&
7260 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7261 Diag(ELoc, diag::err_omp_wrong_dsa)
7262 << getOpenMPClauseName(DVar.CKind)
7263 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007264 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007265 continue;
7266 }
7267
Alexey Bataevf29276e2014-06-18 04:14:57 +00007268 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7269 // OpenMP [2.14.3.5, Restrictions, p.2]
7270 // A list item that is private within a parallel region, or that appears in
7271 // the reduction clause of a parallel construct, must not appear in a
7272 // lastprivate clause on a worksharing construct if any of the corresponding
7273 // worksharing regions ever binds to any of the corresponding parallel
7274 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007275 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007276 if (isOpenMPWorksharingDirective(CurrDir) &&
7277 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007278 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007279 if (DVar.CKind != OMPC_shared) {
7280 Diag(ELoc, diag::err_omp_required_access)
7281 << getOpenMPClauseName(OMPC_lastprivate)
7282 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007283 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007284 continue;
7285 }
7286 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007287 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007288 // A variable of class type (or array thereof) that appears in a
7289 // lastprivate clause requires an accessible, unambiguous default
7290 // constructor for the class type, unless the list item is also specified
7291 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007292 // A variable of class type (or array thereof) that appears in a
7293 // lastprivate clause requires an accessible, unambiguous copy assignment
7294 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007295 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007296 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007297 Type.getUnqualifiedType(), ".lastprivate.src",
7298 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007299 auto *PseudoSrcExpr = buildDeclRefExpr(
7300 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007301 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007302 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7303 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007304 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007305 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007306 // For arrays generate assignment operation for single element and replace
7307 // it by the original array element in CodeGen.
7308 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7309 PseudoDstExpr, PseudoSrcExpr);
7310 if (AssignmentOp.isInvalid())
7311 continue;
7312 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7313 /*DiscardedValue=*/true);
7314 if (AssignmentOp.isInvalid())
7315 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007316
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007317 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7318 // A list item may appear in a firstprivate or lastprivate clause but not
7319 // both.
7320 if (CurrDir == OMPD_distribute) {
7321 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7322 if (DVar.CKind == OMPC_firstprivate) {
7323 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7324 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7325 continue;
7326 }
7327 }
7328
Alexey Bataev39f915b82015-05-08 10:41:21 +00007329 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007330 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007331 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007332 SrcExprs.push_back(PseudoSrcExpr);
7333 DstExprs.push_back(PseudoDstExpr);
7334 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007335 }
7336
7337 if (Vars.empty())
7338 return nullptr;
7339
7340 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007341 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007342}
7343
Alexey Bataev758e55e2013-09-06 18:03:48 +00007344OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7345 SourceLocation StartLoc,
7346 SourceLocation LParenLoc,
7347 SourceLocation EndLoc) {
7348 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007349 for (auto &RefExpr : VarList) {
7350 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7351 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007352 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007353 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007354 continue;
7355 }
7356
Alexey Bataeved09d242014-05-28 05:53:51 +00007357 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007358 // OpenMP [2.1, C/C++]
7359 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007360 // OpenMP [2.14.3.2, Restrictions, p.1]
7361 // A variable that is part of another variable (as an array or structure
7362 // element) cannot appear in a shared unless it is a static data member
7363 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007364 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007365 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007366 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7367 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007368 continue;
7369 }
7370 Decl *D = DE->getDecl();
7371 VarDecl *VD = cast<VarDecl>(D);
7372
7373 QualType Type = VD->getType();
7374 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7375 // It will be analyzed later.
7376 Vars.push_back(DE);
7377 continue;
7378 }
7379
7380 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7381 // in a Construct]
7382 // Variables with the predetermined data-sharing attributes may not be
7383 // listed in data-sharing attributes clauses, except for the cases
7384 // listed below. For these exceptions only, listing a predetermined
7385 // variable in a data-sharing attribute clause is allowed and overrides
7386 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007387 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007388 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7389 DVar.RefExpr) {
7390 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7391 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007392 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007393 continue;
7394 }
7395
7396 DSAStack->addDSA(VD, DE, OMPC_shared);
7397 Vars.push_back(DE);
7398 }
7399
Alexey Bataeved09d242014-05-28 05:53:51 +00007400 if (Vars.empty())
7401 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007402
7403 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7404}
7405
Alexey Bataevc5e02582014-06-16 07:08:35 +00007406namespace {
7407class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7408 DSAStackTy *Stack;
7409
7410public:
7411 bool VisitDeclRefExpr(DeclRefExpr *E) {
7412 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007413 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007414 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7415 return false;
7416 if (DVar.CKind != OMPC_unknown)
7417 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007418 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007419 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007420 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007421 return true;
7422 return false;
7423 }
7424 return false;
7425 }
7426 bool VisitStmt(Stmt *S) {
7427 for (auto Child : S->children()) {
7428 if (Child && Visit(Child))
7429 return true;
7430 }
7431 return false;
7432 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007433 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007434};
Alexey Bataev23b69422014-06-18 07:08:49 +00007435} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007436
7437OMPClause *Sema::ActOnOpenMPReductionClause(
7438 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7439 SourceLocation ColonLoc, SourceLocation EndLoc,
7440 CXXScopeSpec &ReductionIdScopeSpec,
7441 const DeclarationNameInfo &ReductionId) {
7442 // TODO: Allow scope specification search when 'declare reduction' is
7443 // supported.
7444 assert(ReductionIdScopeSpec.isEmpty() &&
7445 "No support for scoped reduction identifiers yet.");
7446
7447 auto DN = ReductionId.getName();
7448 auto OOK = DN.getCXXOverloadedOperator();
7449 BinaryOperatorKind BOK = BO_Comma;
7450
7451 // OpenMP [2.14.3.6, reduction clause]
7452 // C
7453 // reduction-identifier is either an identifier or one of the following
7454 // operators: +, -, *, &, |, ^, && and ||
7455 // C++
7456 // reduction-identifier is either an id-expression or one of the following
7457 // operators: +, -, *, &, |, ^, && and ||
7458 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7459 switch (OOK) {
7460 case OO_Plus:
7461 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007462 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007463 break;
7464 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007465 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007466 break;
7467 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007468 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007469 break;
7470 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007471 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007472 break;
7473 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007474 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007475 break;
7476 case OO_AmpAmp:
7477 BOK = BO_LAnd;
7478 break;
7479 case OO_PipePipe:
7480 BOK = BO_LOr;
7481 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007482 case OO_New:
7483 case OO_Delete:
7484 case OO_Array_New:
7485 case OO_Array_Delete:
7486 case OO_Slash:
7487 case OO_Percent:
7488 case OO_Tilde:
7489 case OO_Exclaim:
7490 case OO_Equal:
7491 case OO_Less:
7492 case OO_Greater:
7493 case OO_LessEqual:
7494 case OO_GreaterEqual:
7495 case OO_PlusEqual:
7496 case OO_MinusEqual:
7497 case OO_StarEqual:
7498 case OO_SlashEqual:
7499 case OO_PercentEqual:
7500 case OO_CaretEqual:
7501 case OO_AmpEqual:
7502 case OO_PipeEqual:
7503 case OO_LessLess:
7504 case OO_GreaterGreater:
7505 case OO_LessLessEqual:
7506 case OO_GreaterGreaterEqual:
7507 case OO_EqualEqual:
7508 case OO_ExclaimEqual:
7509 case OO_PlusPlus:
7510 case OO_MinusMinus:
7511 case OO_Comma:
7512 case OO_ArrowStar:
7513 case OO_Arrow:
7514 case OO_Call:
7515 case OO_Subscript:
7516 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007517 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007518 case NUM_OVERLOADED_OPERATORS:
7519 llvm_unreachable("Unexpected reduction identifier");
7520 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007521 if (auto II = DN.getAsIdentifierInfo()) {
7522 if (II->isStr("max"))
7523 BOK = BO_GT;
7524 else if (II->isStr("min"))
7525 BOK = BO_LT;
7526 }
7527 break;
7528 }
7529 SourceRange ReductionIdRange;
7530 if (ReductionIdScopeSpec.isValid()) {
7531 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7532 }
7533 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7534 if (BOK == BO_Comma) {
7535 // Not allowed reduction identifier is found.
7536 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7537 << ReductionIdRange;
7538 return nullptr;
7539 }
7540
7541 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007542 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007543 SmallVector<Expr *, 8> LHSs;
7544 SmallVector<Expr *, 8> RHSs;
7545 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007546 for (auto RefExpr : VarList) {
7547 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7548 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7549 // It will be analyzed later.
7550 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007551 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007552 LHSs.push_back(nullptr);
7553 RHSs.push_back(nullptr);
7554 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007555 continue;
7556 }
7557
7558 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7559 RefExpr->isInstantiationDependent() ||
7560 RefExpr->containsUnexpandedParameterPack()) {
7561 // It will be analyzed later.
7562 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007563 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007564 LHSs.push_back(nullptr);
7565 RHSs.push_back(nullptr);
7566 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007567 continue;
7568 }
7569
7570 auto ELoc = RefExpr->getExprLoc();
7571 auto ERange = RefExpr->getSourceRange();
7572 // OpenMP [2.1, C/C++]
7573 // A list item is a variable or array section, subject to the restrictions
7574 // specified in Section 2.4 on page 42 and in each of the sections
7575 // describing clauses and directives for which a list appears.
7576 // OpenMP [2.14.3.3, Restrictions, p.1]
7577 // A variable that is part of another variable (as an array or
7578 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007579 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7580 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7581 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7582 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007583 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7584 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007585 continue;
7586 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007587 QualType Type;
7588 VarDecl *VD = nullptr;
7589 if (DE) {
7590 auto D = DE->getDecl();
7591 VD = cast<VarDecl>(D);
Alexey Bataev1189bd02016-01-26 12:20:39 +00007592 Type = Context.getBaseElementType(VD->getType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007593 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007594 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007595 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7596 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7597 Base = TempASE->getBase()->IgnoreParenImpCasts();
7598 DE = dyn_cast<DeclRefExpr>(Base);
7599 if (DE)
7600 VD = dyn_cast<VarDecl>(DE->getDecl());
7601 if (!VD) {
7602 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7603 << 0 << Base->getSourceRange();
7604 continue;
7605 }
7606 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007607 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7608 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7609 Type = ATy->getElementType();
7610 else
7611 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007612 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7613 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7614 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7615 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7616 Base = TempASE->getBase()->IgnoreParenImpCasts();
7617 DE = dyn_cast<DeclRefExpr>(Base);
7618 if (DE)
7619 VD = dyn_cast<VarDecl>(DE->getDecl());
7620 if (!VD) {
7621 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7622 << 1 << Base->getSourceRange();
7623 continue;
7624 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007625 }
7626
Alexey Bataevc5e02582014-06-16 07:08:35 +00007627 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7628 // A variable that appears in a private clause must not have an incomplete
7629 // type or a reference type.
7630 if (RequireCompleteType(ELoc, Type,
7631 diag::err_omp_reduction_incomplete_type))
7632 continue;
7633 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007634 // A list item that appears in a reduction clause must not be
7635 // const-qualified.
7636 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007637 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007638 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007639 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007640 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7641 VarDecl::DeclarationOnly;
7642 Diag(VD->getLocation(),
7643 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7644 << VD;
7645 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007646 continue;
7647 }
7648 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7649 // If a list-item is a reference type then it must bind to the same object
7650 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007651 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007652 VarDecl *VDDef = VD->getDefinition();
7653 if (Type->isReferenceType() && VDDef) {
7654 DSARefChecker Check(DSAStack);
7655 if (Check.Visit(VDDef->getInit())) {
7656 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7657 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7658 continue;
7659 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007660 }
7661 }
7662 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7663 // The type of a list item that appears in a reduction clause must be valid
7664 // for the reduction-identifier. For a max or min reduction in C, the type
7665 // of the list item must be an allowed arithmetic data type: char, int,
7666 // float, double, or _Bool, possibly modified with long, short, signed, or
7667 // unsigned. For a max or min reduction in C++, the type of the list item
7668 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7669 // double, or bool, possibly modified with long, short, signed, or unsigned.
7670 if ((BOK == BO_GT || BOK == BO_LT) &&
7671 !(Type->isScalarType() ||
7672 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7673 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7674 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007675 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007676 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7677 VarDecl::DeclarationOnly;
7678 Diag(VD->getLocation(),
7679 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7680 << VD;
7681 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007682 continue;
7683 }
7684 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7685 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7686 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007687 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007688 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7689 VarDecl::DeclarationOnly;
7690 Diag(VD->getLocation(),
7691 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7692 << VD;
7693 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007694 continue;
7695 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007696 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7697 // in a Construct]
7698 // Variables with the predetermined data-sharing attributes may not be
7699 // listed in data-sharing attributes clauses, except for the cases
7700 // listed below. For these exceptions only, listing a predetermined
7701 // variable in a data-sharing attribute clause is allowed and overrides
7702 // the variable's predetermined data-sharing attributes.
7703 // OpenMP [2.14.3.6, Restrictions, p.3]
7704 // Any number of reduction clauses can be specified on the directive,
7705 // but a list item can appear only once in the reduction clauses for that
7706 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007707 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007708 DVar = DSAStack->getTopDSA(VD, false);
7709 if (DVar.CKind == OMPC_reduction) {
7710 Diag(ELoc, diag::err_omp_once_referenced)
7711 << getOpenMPClauseName(OMPC_reduction);
7712 if (DVar.RefExpr) {
7713 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007714 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007715 } else if (DVar.CKind != OMPC_unknown) {
7716 Diag(ELoc, diag::err_omp_wrong_dsa)
7717 << getOpenMPClauseName(DVar.CKind)
7718 << getOpenMPClauseName(OMPC_reduction);
7719 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7720 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007721 }
7722
7723 // OpenMP [2.14.3.6, Restrictions, p.1]
7724 // A list item that appears in a reduction clause of a worksharing
7725 // construct must be shared in the parallel regions to which any of the
7726 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007727 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7728 if (isOpenMPWorksharingDirective(CurrDir) &&
7729 !isOpenMPParallelDirective(CurrDir)) {
7730 DVar = DSAStack->getImplicitDSA(VD, true);
7731 if (DVar.CKind != OMPC_shared) {
7732 Diag(ELoc, diag::err_omp_required_access)
7733 << getOpenMPClauseName(OMPC_reduction)
7734 << getOpenMPClauseName(OMPC_shared);
7735 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7736 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007737 }
7738 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007739
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007740 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007741 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7742 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7743 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7744 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7745 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007746 if (OASE ||
7747 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7748 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007749 // Create pseudo array type for private copy. The size for this array will
7750 // be generated during codegen.
7751 // For array subscripts or single variables Private Ty is the same as Type
7752 // (type of the variable or single array element).
7753 PrivateTy = Context.getVariableArrayType(
7754 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7755 Context.getSizeType(), VK_RValue),
7756 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007757 } else if (DE &&
7758 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7759 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007760 // Private copy.
7761 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7762 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007763 // Add initializer for private variable.
7764 Expr *Init = nullptr;
7765 switch (BOK) {
7766 case BO_Add:
7767 case BO_Xor:
7768 case BO_Or:
7769 case BO_LOr:
7770 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7771 if (Type->isScalarType() || Type->isAnyComplexType()) {
7772 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007773 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007774 break;
7775 case BO_Mul:
7776 case BO_LAnd:
7777 if (Type->isScalarType() || Type->isAnyComplexType()) {
7778 // '*' and '&&' reduction ops - initializer is '1'.
7779 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7780 }
7781 break;
7782 case BO_And: {
7783 // '&' reduction op - initializer is '~0'.
7784 QualType OrigType = Type;
7785 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7786 Type = ComplexTy->getElementType();
7787 }
7788 if (Type->isRealFloatingType()) {
7789 llvm::APFloat InitValue =
7790 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7791 /*isIEEE=*/true);
7792 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7793 Type, ELoc);
7794 } else if (Type->isScalarType()) {
7795 auto Size = Context.getTypeSize(Type);
7796 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7797 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7798 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7799 }
7800 if (Init && OrigType->isAnyComplexType()) {
7801 // Init = 0xFFFF + 0xFFFFi;
7802 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7803 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7804 }
7805 Type = OrigType;
7806 break;
7807 }
7808 case BO_LT:
7809 case BO_GT: {
7810 // 'min' reduction op - initializer is 'Largest representable number in
7811 // the reduction list item type'.
7812 // 'max' reduction op - initializer is 'Least representable number in
7813 // the reduction list item type'.
7814 if (Type->isIntegerType() || Type->isPointerType()) {
7815 bool IsSigned = Type->hasSignedIntegerRepresentation();
7816 auto Size = Context.getTypeSize(Type);
7817 QualType IntTy =
7818 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7819 llvm::APInt InitValue =
7820 (BOK != BO_LT)
7821 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7822 : llvm::APInt::getMinValue(Size)
7823 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7824 : llvm::APInt::getMaxValue(Size);
7825 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7826 if (Type->isPointerType()) {
7827 // Cast to pointer type.
7828 auto CastExpr = BuildCStyleCastExpr(
7829 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7830 SourceLocation(), Init);
7831 if (CastExpr.isInvalid())
7832 continue;
7833 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007834 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007835 } else if (Type->isRealFloatingType()) {
7836 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7837 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7838 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7839 Type, ELoc);
7840 }
7841 break;
7842 }
7843 case BO_PtrMemD:
7844 case BO_PtrMemI:
7845 case BO_MulAssign:
7846 case BO_Div:
7847 case BO_Rem:
7848 case BO_Sub:
7849 case BO_Shl:
7850 case BO_Shr:
7851 case BO_LE:
7852 case BO_GE:
7853 case BO_EQ:
7854 case BO_NE:
7855 case BO_AndAssign:
7856 case BO_XorAssign:
7857 case BO_OrAssign:
7858 case BO_Assign:
7859 case BO_AddAssign:
7860 case BO_SubAssign:
7861 case BO_DivAssign:
7862 case BO_RemAssign:
7863 case BO_ShlAssign:
7864 case BO_ShrAssign:
7865 case BO_Comma:
7866 llvm_unreachable("Unexpected reduction operation");
7867 }
7868 if (Init) {
7869 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7870 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007871 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007872 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007873 if (!RHSVD->hasInit()) {
7874 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7875 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007876 if (VD) {
7877 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7878 VarDecl::DeclarationOnly;
7879 Diag(VD->getLocation(),
7880 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7881 << VD;
7882 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007883 continue;
7884 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007885 // Store initializer for single element in private copy. Will be used during
7886 // codegen.
7887 PrivateVD->setInit(RHSVD->getInit());
7888 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007889 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7890 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007891 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007892 ExprResult ReductionOp =
7893 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7894 LHSDRE, RHSDRE);
7895 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007896 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007897 ReductionOp =
7898 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7899 BO_Assign, LHSDRE, ReductionOp.get());
7900 } else {
7901 auto *ConditionalOp = new (Context) ConditionalOperator(
7902 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7903 RHSDRE, Type, VK_LValue, OK_Ordinary);
7904 ReductionOp =
7905 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7906 BO_Assign, LHSDRE, ConditionalOp);
7907 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007908 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007909 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007910 if (ReductionOp.isInvalid())
7911 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007912
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007913 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007914 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007915 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007916 LHSs.push_back(LHSDRE);
7917 RHSs.push_back(RHSDRE);
7918 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007919 }
7920
7921 if (Vars.empty())
7922 return nullptr;
7923
7924 return OMPReductionClause::Create(
7925 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007926 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7927 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007928}
7929
Alexey Bataev182227b2015-08-20 10:54:39 +00007930OMPClause *Sema::ActOnOpenMPLinearClause(
7931 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7932 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7933 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007934 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007935 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007936 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007937 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7938 LinKind == OMPC_LINEAR_unknown) {
7939 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7940 LinKind = OMPC_LINEAR_val;
7941 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007942 for (auto &RefExpr : VarList) {
7943 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7944 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007945 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007946 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007947 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007948 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007949 continue;
7950 }
7951
7952 // OpenMP [2.14.3.7, linear clause]
7953 // A list item that appears in a linear clause is subject to the private
7954 // clause semantics described in Section 2.14.3.3 on page 159 except as
7955 // noted. In addition, the value of the new list item on each iteration
7956 // of the associated loop(s) corresponds to the value of the original
7957 // list item before entering the construct plus the logical number of
7958 // the iteration times linear-step.
7959
Alexey Bataeved09d242014-05-28 05:53:51 +00007960 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007961 // OpenMP [2.1, C/C++]
7962 // A list item is a variable name.
7963 // OpenMP [2.14.3.3, Restrictions, p.1]
7964 // A variable that is part of another variable (as an array or
7965 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007966 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007967 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007968 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7969 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007970 continue;
7971 }
7972
7973 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7974
7975 // OpenMP [2.14.3.7, linear clause]
7976 // A list-item cannot appear in more than one linear clause.
7977 // A list-item that appears in a linear clause cannot appear in any
7978 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007979 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007980 if (DVar.RefExpr) {
7981 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7982 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007983 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007984 continue;
7985 }
7986
7987 QualType QType = VD->getType();
7988 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7989 // It will be analyzed later.
7990 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007991 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007992 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007993 continue;
7994 }
7995
7996 // A variable must not have an incomplete type or a reference type.
7997 if (RequireCompleteType(ELoc, QType,
7998 diag::err_omp_linear_incomplete_type)) {
7999 continue;
8000 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008001 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8002 !QType->isReferenceType()) {
8003 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8004 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8005 continue;
8006 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008007 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008008
8009 // A list item must not be const-qualified.
8010 if (QType.isConstant(Context)) {
8011 Diag(ELoc, diag::err_omp_const_variable)
8012 << getOpenMPClauseName(OMPC_linear);
8013 bool IsDecl =
8014 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8015 Diag(VD->getLocation(),
8016 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8017 << VD;
8018 continue;
8019 }
8020
8021 // A list item must be of integral or pointer type.
8022 QType = QType.getUnqualifiedType().getCanonicalType();
8023 const Type *Ty = QType.getTypePtrOrNull();
8024 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8025 !Ty->isPointerType())) {
8026 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8027 bool IsDecl =
8028 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8029 Diag(VD->getLocation(),
8030 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8031 << VD;
8032 continue;
8033 }
8034
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008035 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008036 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8037 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008038 auto *PrivateRef = buildDeclRefExpr(
8039 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008040 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008041 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008042 Expr *InitExpr;
8043 if (LinKind == OMPC_LINEAR_uval)
8044 InitExpr = VD->getInit();
8045 else
8046 InitExpr = DE;
8047 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008048 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008049 auto InitRef = buildDeclRefExpr(
8050 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008051 DSAStack->addDSA(VD, DE, OMPC_linear);
8052 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008053 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008054 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008055 }
8056
8057 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008058 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008059
8060 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008061 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008062 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8063 !Step->isInstantiationDependent() &&
8064 !Step->containsUnexpandedParameterPack()) {
8065 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008066 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008067 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008068 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008069 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008070
Alexander Musman3276a272015-03-21 10:12:56 +00008071 // Build var to save the step value.
8072 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008073 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008074 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008075 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008076 ExprResult CalcStep =
8077 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008078 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008079
Alexander Musman8dba6642014-04-22 13:09:42 +00008080 // Warn about zero linear step (it would be probably better specified as
8081 // making corresponding variables 'const').
8082 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008083 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8084 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008085 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8086 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008087 if (!IsConstant && CalcStep.isUsable()) {
8088 // Calculate the step beforehand instead of doing this on each iteration.
8089 // (This is not used if the number of iterations may be kfold-ed).
8090 CalcStepExpr = CalcStep.get();
8091 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008092 }
8093
Alexey Bataev182227b2015-08-20 10:54:39 +00008094 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8095 ColonLoc, EndLoc, Vars, Privates, Inits,
8096 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008097}
8098
8099static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8100 Expr *NumIterations, Sema &SemaRef,
8101 Scope *S) {
8102 // Walk the vars and build update/final expressions for the CodeGen.
8103 SmallVector<Expr *, 8> Updates;
8104 SmallVector<Expr *, 8> Finals;
8105 Expr *Step = Clause.getStep();
8106 Expr *CalcStep = Clause.getCalcStep();
8107 // OpenMP [2.14.3.7, linear clause]
8108 // If linear-step is not specified it is assumed to be 1.
8109 if (Step == nullptr)
8110 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8111 else if (CalcStep)
8112 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8113 bool HasErrors = false;
8114 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008115 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008116 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008117 for (auto &RefExpr : Clause.varlists()) {
8118 Expr *InitExpr = *CurInit;
8119
8120 // Build privatized reference to the current linear var.
8121 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008122 Expr *CapturedRef;
8123 if (LinKind == OMPC_LINEAR_uval)
8124 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8125 else
8126 CapturedRef =
8127 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8128 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8129 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008130
8131 // Build update: Var = InitExpr + IV * Step
8132 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008133 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008134 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008135 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8136 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008137
8138 // Build final: Var = InitExpr + NumIterations * Step
8139 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008140 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008141 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008142 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8143 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008144 if (!Update.isUsable() || !Final.isUsable()) {
8145 Updates.push_back(nullptr);
8146 Finals.push_back(nullptr);
8147 HasErrors = true;
8148 } else {
8149 Updates.push_back(Update.get());
8150 Finals.push_back(Final.get());
8151 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008152 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008153 }
8154 Clause.setUpdates(Updates);
8155 Clause.setFinals(Finals);
8156 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008157}
8158
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008159OMPClause *Sema::ActOnOpenMPAlignedClause(
8160 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8161 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8162
8163 SmallVector<Expr *, 8> Vars;
8164 for (auto &RefExpr : VarList) {
8165 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8166 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8167 // It will be analyzed later.
8168 Vars.push_back(RefExpr);
8169 continue;
8170 }
8171
8172 SourceLocation ELoc = RefExpr->getExprLoc();
8173 // OpenMP [2.1, C/C++]
8174 // A list item is a variable name.
8175 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8176 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008177 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8178 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008179 continue;
8180 }
8181
8182 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8183
8184 // OpenMP [2.8.1, simd construct, Restrictions]
8185 // The type of list items appearing in the aligned clause must be
8186 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008187 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008188 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008189 const Type *Ty = QType.getTypePtrOrNull();
8190 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8191 !Ty->isPointerType())) {
8192 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8193 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8194 bool IsDecl =
8195 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8196 Diag(VD->getLocation(),
8197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8198 << VD;
8199 continue;
8200 }
8201
8202 // OpenMP [2.8.1, simd construct, Restrictions]
8203 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008204 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008205 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8206 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8207 << getOpenMPClauseName(OMPC_aligned);
8208 continue;
8209 }
8210
8211 Vars.push_back(DE);
8212 }
8213
8214 // OpenMP [2.8.1, simd construct, Description]
8215 // The parameter of the aligned clause, alignment, must be a constant
8216 // positive integer expression.
8217 // If no optional parameter is specified, implementation-defined default
8218 // alignments for SIMD instructions on the target platforms are assumed.
8219 if (Alignment != nullptr) {
8220 ExprResult AlignResult =
8221 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8222 if (AlignResult.isInvalid())
8223 return nullptr;
8224 Alignment = AlignResult.get();
8225 }
8226 if (Vars.empty())
8227 return nullptr;
8228
8229 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8230 EndLoc, Vars, Alignment);
8231}
8232
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008233OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8234 SourceLocation StartLoc,
8235 SourceLocation LParenLoc,
8236 SourceLocation EndLoc) {
8237 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008238 SmallVector<Expr *, 8> SrcExprs;
8239 SmallVector<Expr *, 8> DstExprs;
8240 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008241 for (auto &RefExpr : VarList) {
8242 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8243 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008244 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008245 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008246 SrcExprs.push_back(nullptr);
8247 DstExprs.push_back(nullptr);
8248 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008249 continue;
8250 }
8251
Alexey Bataeved09d242014-05-28 05:53:51 +00008252 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008253 // OpenMP [2.1, C/C++]
8254 // A list item is a variable name.
8255 // OpenMP [2.14.4.1, Restrictions, p.1]
8256 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008257 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008258 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008259 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8260 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008261 continue;
8262 }
8263
8264 Decl *D = DE->getDecl();
8265 VarDecl *VD = cast<VarDecl>(D);
8266
8267 QualType Type = VD->getType();
8268 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8269 // It will be analyzed later.
8270 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008271 SrcExprs.push_back(nullptr);
8272 DstExprs.push_back(nullptr);
8273 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008274 continue;
8275 }
8276
8277 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8278 // A list item that appears in a copyin clause must be threadprivate.
8279 if (!DSAStack->isThreadPrivate(VD)) {
8280 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008281 << getOpenMPClauseName(OMPC_copyin)
8282 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008283 continue;
8284 }
8285
8286 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8287 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008288 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008289 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008290 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008291 auto *SrcVD =
8292 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8293 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008294 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008295 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8296 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008297 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8298 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008299 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008300 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008301 // For arrays generate assignment operation for single element and replace
8302 // it by the original array element in CodeGen.
8303 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8304 PseudoDstExpr, PseudoSrcExpr);
8305 if (AssignmentOp.isInvalid())
8306 continue;
8307 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8308 /*DiscardedValue=*/true);
8309 if (AssignmentOp.isInvalid())
8310 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008311
8312 DSAStack->addDSA(VD, DE, OMPC_copyin);
8313 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008314 SrcExprs.push_back(PseudoSrcExpr);
8315 DstExprs.push_back(PseudoDstExpr);
8316 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008317 }
8318
Alexey Bataeved09d242014-05-28 05:53:51 +00008319 if (Vars.empty())
8320 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008321
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008322 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8323 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008324}
8325
Alexey Bataevbae9a792014-06-27 10:37:06 +00008326OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8327 SourceLocation StartLoc,
8328 SourceLocation LParenLoc,
8329 SourceLocation EndLoc) {
8330 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008331 SmallVector<Expr *, 8> SrcExprs;
8332 SmallVector<Expr *, 8> DstExprs;
8333 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008334 for (auto &RefExpr : VarList) {
8335 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8336 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8337 // It will be analyzed later.
8338 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008339 SrcExprs.push_back(nullptr);
8340 DstExprs.push_back(nullptr);
8341 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008342 continue;
8343 }
8344
8345 SourceLocation ELoc = RefExpr->getExprLoc();
8346 // OpenMP [2.1, C/C++]
8347 // A list item is a variable name.
8348 // OpenMP [2.14.4.1, Restrictions, p.1]
8349 // A list item that appears in a copyin clause must be threadprivate.
8350 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8351 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008352 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8353 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008354 continue;
8355 }
8356
8357 Decl *D = DE->getDecl();
8358 VarDecl *VD = cast<VarDecl>(D);
8359
8360 QualType Type = VD->getType();
8361 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8362 // It will be analyzed later.
8363 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008364 SrcExprs.push_back(nullptr);
8365 DstExprs.push_back(nullptr);
8366 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008367 continue;
8368 }
8369
8370 // OpenMP [2.14.4.2, Restrictions, p.2]
8371 // A list item that appears in a copyprivate clause may not appear in a
8372 // private or firstprivate clause on the single construct.
8373 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008374 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008375 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8376 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008377 Diag(ELoc, diag::err_omp_wrong_dsa)
8378 << getOpenMPClauseName(DVar.CKind)
8379 << getOpenMPClauseName(OMPC_copyprivate);
8380 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8381 continue;
8382 }
8383
8384 // OpenMP [2.11.4.2, Restrictions, p.1]
8385 // All list items that appear in a copyprivate clause must be either
8386 // threadprivate or private in the enclosing context.
8387 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008388 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008389 if (DVar.CKind == OMPC_shared) {
8390 Diag(ELoc, diag::err_omp_required_access)
8391 << getOpenMPClauseName(OMPC_copyprivate)
8392 << "threadprivate or private in the enclosing context";
8393 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8394 continue;
8395 }
8396 }
8397 }
8398
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008399 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008400 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008401 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008402 << getOpenMPClauseName(OMPC_copyprivate) << Type
8403 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008404 bool IsDecl =
8405 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8406 Diag(VD->getLocation(),
8407 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8408 << VD;
8409 continue;
8410 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008411
Alexey Bataevbae9a792014-06-27 10:37:06 +00008412 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8413 // A variable of class type (or array thereof) that appears in a
8414 // copyin clause requires an accessible, unambiguous copy assignment
8415 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008416 Type = Context.getBaseElementType(Type.getNonReferenceType())
8417 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008418 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008419 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8420 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008421 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008422 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008423 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008424 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8425 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008426 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008427 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008428 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8429 PseudoDstExpr, PseudoSrcExpr);
8430 if (AssignmentOp.isInvalid())
8431 continue;
8432 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8433 /*DiscardedValue=*/true);
8434 if (AssignmentOp.isInvalid())
8435 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008436
8437 // No need to mark vars as copyprivate, they are already threadprivate or
8438 // implicitly private.
8439 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008440 SrcExprs.push_back(PseudoSrcExpr);
8441 DstExprs.push_back(PseudoDstExpr);
8442 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008443 }
8444
8445 if (Vars.empty())
8446 return nullptr;
8447
Alexey Bataeva63048e2015-03-23 06:18:07 +00008448 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8449 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008450}
8451
Alexey Bataev6125da92014-07-21 11:26:11 +00008452OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8453 SourceLocation StartLoc,
8454 SourceLocation LParenLoc,
8455 SourceLocation EndLoc) {
8456 if (VarList.empty())
8457 return nullptr;
8458
8459 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8460}
Alexey Bataevdea47612014-07-23 07:46:59 +00008461
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008462OMPClause *
8463Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8464 SourceLocation DepLoc, SourceLocation ColonLoc,
8465 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8466 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008467 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008468 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008469 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008470 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008471 return nullptr;
8472 }
8473 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008474 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8475 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008476 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008477 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008478 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8479 /*Last=*/OMPC_DEPEND_unknown, Except)
8480 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008481 return nullptr;
8482 }
8483 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008484 llvm::APSInt DepCounter(/*BitWidth=*/32);
8485 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8486 if (DepKind == OMPC_DEPEND_sink) {
8487 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8488 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8489 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008490 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008491 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008492 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8493 DSAStack->getParentOrderedRegionParam()) {
8494 for (auto &RefExpr : VarList) {
8495 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8496 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8497 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8498 // It will be analyzed later.
8499 Vars.push_back(RefExpr);
8500 continue;
8501 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008502
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008503 SourceLocation ELoc = RefExpr->getExprLoc();
8504 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8505 if (DepKind == OMPC_DEPEND_sink) {
8506 if (DepCounter >= TotalDepCount) {
8507 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8508 continue;
8509 }
8510 ++DepCounter;
8511 // OpenMP [2.13.9, Summary]
8512 // depend(dependence-type : vec), where dependence-type is:
8513 // 'sink' and where vec is the iteration vector, which has the form:
8514 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8515 // where n is the value specified by the ordered clause in the loop
8516 // directive, xi denotes the loop iteration variable of the i-th nested
8517 // loop associated with the loop directive, and di is a constant
8518 // non-negative integer.
8519 SimpleExpr = SimpleExpr->IgnoreImplicit();
8520 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8521 if (!DE) {
8522 OverloadedOperatorKind OOK = OO_None;
8523 SourceLocation OOLoc;
8524 Expr *LHS, *RHS;
8525 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8526 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8527 OOLoc = BO->getOperatorLoc();
8528 LHS = BO->getLHS()->IgnoreParenImpCasts();
8529 RHS = BO->getRHS()->IgnoreParenImpCasts();
8530 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8531 OOK = OCE->getOperator();
8532 OOLoc = OCE->getOperatorLoc();
8533 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8534 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8535 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8536 OOK = MCE->getMethodDecl()
8537 ->getNameInfo()
8538 .getName()
8539 .getCXXOverloadedOperator();
8540 OOLoc = MCE->getCallee()->getExprLoc();
8541 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8542 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8543 } else {
8544 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8545 continue;
8546 }
8547 DE = dyn_cast<DeclRefExpr>(LHS);
8548 if (!DE) {
8549 Diag(LHS->getExprLoc(),
8550 diag::err_omp_depend_sink_expected_loop_iteration)
8551 << DSAStack->getParentLoopControlVariable(
8552 DepCounter.getZExtValue());
8553 continue;
8554 }
8555 if (OOK != OO_Plus && OOK != OO_Minus) {
8556 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8557 continue;
8558 }
8559 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8560 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8561 if (Res.isInvalid())
8562 continue;
8563 }
8564 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8565 if (!CurContext->isDependentContext() &&
8566 DSAStack->getParentOrderedRegionParam() &&
8567 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8568 Diag(DE->getExprLoc(),
8569 diag::err_omp_depend_sink_expected_loop_iteration)
8570 << DSAStack->getParentLoopControlVariable(
8571 DepCounter.getZExtValue());
8572 continue;
8573 }
8574 } else {
8575 // OpenMP [2.11.1.1, Restrictions, p.3]
8576 // A variable that is part of another variable (such as a field of a
8577 // structure) but is not an array element or an array section cannot
8578 // appear in a depend clause.
8579 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8580 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8581 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8582 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8583 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8584 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8585 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008586 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8587 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008588 continue;
8589 }
8590 }
8591
8592 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8593 }
8594
8595 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8596 TotalDepCount > VarList.size() &&
8597 DSAStack->getParentOrderedRegionParam()) {
8598 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8599 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8600 }
8601 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8602 Vars.empty())
8603 return nullptr;
8604 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008605
8606 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8607 DepLoc, ColonLoc, Vars);
8608}
Michael Wonge710d542015-08-07 16:16:36 +00008609
8610OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8611 SourceLocation LParenLoc,
8612 SourceLocation EndLoc) {
8613 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008614
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008615 // OpenMP [2.9.1, Restrictions]
8616 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008617 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8618 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008619 return nullptr;
8620
Michael Wonge710d542015-08-07 16:16:36 +00008621 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8622}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008623
8624static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8625 DSAStackTy *Stack, CXXRecordDecl *RD) {
8626 if (!RD || RD->isInvalidDecl())
8627 return true;
8628
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008629 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8630 if (auto *CTD = CTSD->getSpecializedTemplate())
8631 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008632 auto QTy = SemaRef.Context.getRecordType(RD);
8633 if (RD->isDynamicClass()) {
8634 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8635 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8636 return false;
8637 }
8638 auto *DC = RD;
8639 bool IsCorrect = true;
8640 for (auto *I : DC->decls()) {
8641 if (I) {
8642 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8643 if (MD->isStatic()) {
8644 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8645 SemaRef.Diag(MD->getLocation(),
8646 diag::note_omp_static_member_in_target);
8647 IsCorrect = false;
8648 }
8649 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8650 if (VD->isStaticDataMember()) {
8651 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8652 SemaRef.Diag(VD->getLocation(),
8653 diag::note_omp_static_member_in_target);
8654 IsCorrect = false;
8655 }
8656 }
8657 }
8658 }
8659
8660 for (auto &I : RD->bases()) {
8661 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8662 I.getType()->getAsCXXRecordDecl()))
8663 IsCorrect = false;
8664 }
8665 return IsCorrect;
8666}
8667
8668static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8669 DSAStackTy *Stack, QualType QTy) {
8670 NamedDecl *ND;
8671 if (QTy->isIncompleteType(&ND)) {
8672 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8673 return false;
8674 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8675 if (!RD->isInvalidDecl() &&
8676 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8677 return false;
8678 }
8679 return true;
8680}
8681
Samuel Antao5de996e2016-01-22 20:21:36 +00008682// Return the expression of the base of the map clause or null if it cannot
8683// be determined and do all the necessary checks to see if the expression is
8684// valid as a standalone map clause expression.
8685static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8686 SourceLocation ELoc = E->getExprLoc();
8687 SourceRange ERange = E->getSourceRange();
8688
8689 // The base of elements of list in a map clause have to be either:
8690 // - a reference to variable or field.
8691 // - a member expression.
8692 // - an array expression.
8693 //
8694 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8695 // reference to 'r'.
8696 //
8697 // If we have:
8698 //
8699 // struct SS {
8700 // Bla S;
8701 // foo() {
8702 // #pragma omp target map (S.Arr[:12]);
8703 // }
8704 // }
8705 //
8706 // We want to retrieve the member expression 'this->S';
8707
8708 Expr *RelevantExpr = nullptr;
8709
8710 // Flags to help capture some memory
8711
8712 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8713 // If a list item is an array section, it must specify contiguous storage.
8714 //
8715 // For this restriction it is sufficient that we make sure only references
8716 // to variables or fields and array expressions, and that no array sections
8717 // exist except in the rightmost expression. E.g. these would be invalid:
8718 //
8719 // r.ArrS[3:5].Arr[6:7]
8720 //
8721 // r.ArrS[3:5].x
8722 //
8723 // but these would be valid:
8724 // r.ArrS[3].Arr[6:7]
8725 //
8726 // r.ArrS[3].x
8727
8728 bool IsRightMostExpression = true;
8729
8730 while (!RelevantExpr) {
8731 auto AllowArraySection = IsRightMostExpression;
8732 IsRightMostExpression = false;
8733
8734 E = E->IgnoreParenImpCasts();
8735
8736 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8737 if (!isa<VarDecl>(CurE->getDecl()))
8738 break;
8739
8740 RelevantExpr = CurE;
8741 continue;
8742 }
8743
8744 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8745 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8746
8747 if (isa<CXXThisExpr>(BaseE))
8748 // We found a base expression: this->Val.
8749 RelevantExpr = CurE;
8750 else
8751 E = BaseE;
8752
8753 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8754 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8755 << CurE->getSourceRange();
8756 break;
8757 }
8758
8759 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8760
8761 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8762 // A bit-field cannot appear in a map clause.
8763 //
8764 if (FD->isBitField()) {
8765 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8766 << CurE->getSourceRange();
8767 break;
8768 }
8769
8770 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8771 // If the type of a list item is a reference to a type T then the type
8772 // will be considered to be T for all purposes of this clause.
8773 QualType CurType = BaseE->getType();
8774 if (CurType->isReferenceType())
8775 CurType = CurType->getPointeeType();
8776
8777 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8778 // A list item cannot be a variable that is a member of a structure with
8779 // a union type.
8780 //
8781 if (auto *RT = CurType->getAs<RecordType>())
8782 if (RT->isUnionType()) {
8783 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
8784 << CurE->getSourceRange();
8785 break;
8786 }
8787
8788 continue;
8789 }
8790
8791 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
8792 E = CurE->getBase()->IgnoreParenImpCasts();
8793
8794 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
8795 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8796 << 0 << CurE->getSourceRange();
8797 break;
8798 }
8799 continue;
8800 }
8801
8802 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
8803 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
8804 // If a list item is an element of a structure, only the rightmost symbol
8805 // of the variable reference can be an array section.
8806 //
8807 if (!AllowArraySection) {
8808 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
8809 << CurE->getSourceRange();
8810 break;
8811 }
8812
8813 E = CurE->getBase()->IgnoreParenImpCasts();
8814
8815 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8816 // If the type of a list item is a reference to a type T then the type
8817 // will be considered to be T for all purposes of this clause.
8818 QualType CurType = E->getType();
8819 if (CurType->isReferenceType())
8820 CurType = CurType->getPointeeType();
8821
8822 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
8823 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8824 << 0 << CurE->getSourceRange();
8825 break;
8826 }
8827
8828 continue;
8829 }
8830
8831 // If nothing else worked, this is not a valid map clause expression.
8832 SemaRef.Diag(ELoc,
8833 diag::err_omp_expected_named_var_member_or_array_expression)
8834 << ERange;
8835 break;
8836 }
8837
8838 return RelevantExpr;
8839}
8840
8841// Return true if expression E associated with value VD has conflicts with other
8842// map information.
8843static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
8844 Expr *E, bool CurrentRegionOnly) {
8845 assert(VD && E);
8846
8847 // Types used to organize the components of a valid map clause.
8848 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
8849 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
8850
8851 // Helper to extract the components in the map clause expression E and store
8852 // them into MEC. This assumes that E is a valid map clause expression, i.e.
8853 // it has already passed the single clause checks.
8854 auto ExtractMapExpressionComponents = [](Expr *TE,
8855 MapExpressionComponents &MEC) {
8856 while (true) {
8857 TE = TE->IgnoreParenImpCasts();
8858
8859 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
8860 MEC.push_back(
8861 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
8862 break;
8863 }
8864
8865 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
8866 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8867
8868 MEC.push_back(MapExpressionComponent(
8869 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
8870 if (isa<CXXThisExpr>(BaseE))
8871 break;
8872
8873 TE = BaseE;
8874 continue;
8875 }
8876
8877 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
8878 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8879 TE = CurE->getBase()->IgnoreParenImpCasts();
8880 continue;
8881 }
8882
8883 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
8884 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8885 TE = CurE->getBase()->IgnoreParenImpCasts();
8886 continue;
8887 }
8888
8889 llvm_unreachable(
8890 "Expecting only valid map clause expressions at this point!");
8891 }
8892 };
8893
8894 SourceLocation ELoc = E->getExprLoc();
8895 SourceRange ERange = E->getSourceRange();
8896
8897 // In order to easily check the conflicts we need to match each component of
8898 // the expression under test with the components of the expressions that are
8899 // already in the stack.
8900
8901 MapExpressionComponents CurComponents;
8902 ExtractMapExpressionComponents(E, CurComponents);
8903
8904 assert(!CurComponents.empty() && "Map clause expression with no components!");
8905 assert(CurComponents.back().second == VD &&
8906 "Map clause expression with unexpected base!");
8907
8908 // Variables to help detecting enclosing problems in data environment nests.
8909 bool IsEnclosedByDataEnvironmentExpr = false;
8910 Expr *EnclosingExpr = nullptr;
8911
8912 bool FoundError =
8913 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
8914 MapExpressionComponents StackComponents;
8915 ExtractMapExpressionComponents(RE, StackComponents);
8916 assert(!StackComponents.empty() &&
8917 "Map clause expression with no components!");
8918 assert(StackComponents.back().second == VD &&
8919 "Map clause expression with unexpected base!");
8920
8921 // Expressions must start from the same base. Here we detect at which
8922 // point both expressions diverge from each other and see if we can
8923 // detect if the memory referred to both expressions is contiguous and
8924 // do not overlap.
8925 auto CI = CurComponents.rbegin();
8926 auto CE = CurComponents.rend();
8927 auto SI = StackComponents.rbegin();
8928 auto SE = StackComponents.rend();
8929 for (; CI != CE && SI != SE; ++CI, ++SI) {
8930
8931 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
8932 // At most one list item can be an array item derived from a given
8933 // variable in map clauses of the same construct.
8934 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
8935 isa<OMPArraySectionExpr>(CI->first)) &&
8936 (isa<ArraySubscriptExpr>(SI->first) ||
8937 isa<OMPArraySectionExpr>(SI->first))) {
8938 SemaRef.Diag(CI->first->getExprLoc(),
8939 diag::err_omp_multiple_array_items_in_map_clause)
8940 << CI->first->getSourceRange();
8941 ;
8942 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
8943 << SI->first->getSourceRange();
8944 return true;
8945 }
8946
8947 // Do both expressions have the same kind?
8948 if (CI->first->getStmtClass() != SI->first->getStmtClass())
8949 break;
8950
8951 // Are we dealing with different variables/fields?
8952 if (CI->second != SI->second)
8953 break;
8954 }
8955
8956 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8957 // List items of map clauses in the same construct must not share
8958 // original storage.
8959 //
8960 // If the expressions are exactly the same or one is a subset of the
8961 // other, it means they are sharing storage.
8962 if (CI == CE && SI == SE) {
8963 if (CurrentRegionOnly) {
8964 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8965 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8966 << RE->getSourceRange();
8967 return true;
8968 } else {
8969 // If we find the same expression in the enclosing data environment,
8970 // that is legal.
8971 IsEnclosedByDataEnvironmentExpr = true;
8972 return false;
8973 }
8974 }
8975
8976 QualType DerivedType = std::prev(CI)->first->getType();
8977 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
8978
8979 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8980 // If the type of a list item is a reference to a type T then the type
8981 // will be considered to be T for all purposes of this clause.
8982 if (DerivedType->isReferenceType())
8983 DerivedType = DerivedType->getPointeeType();
8984
8985 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
8986 // A variable for which the type is pointer and an array section
8987 // derived from that variable must not appear as list items of map
8988 // clauses of the same construct.
8989 //
8990 // Also, cover one of the cases in:
8991 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
8992 // If any part of the original storage of a list item has corresponding
8993 // storage in the device data environment, all of the original storage
8994 // must have corresponding storage in the device data environment.
8995 //
8996 if (DerivedType->isAnyPointerType()) {
8997 if (CI == CE || SI == SE) {
8998 SemaRef.Diag(
8999 DerivedLoc,
9000 diag::err_omp_pointer_mapped_along_with_derived_section)
9001 << DerivedLoc;
9002 } else {
9003 assert(CI != CE && SI != SE);
9004 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9005 << DerivedLoc;
9006 }
9007 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9008 << RE->getSourceRange();
9009 return true;
9010 }
9011
9012 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9013 // List items of map clauses in the same construct must not share
9014 // original storage.
9015 //
9016 // An expression is a subset of the other.
9017 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9018 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9019 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9020 << RE->getSourceRange();
9021 return true;
9022 }
9023
9024 // The current expression uses the same base as other expression in the
9025 // data environment but does not contain it completelly.
9026 if (!CurrentRegionOnly && SI != SE)
9027 EnclosingExpr = RE;
9028
9029 // The current expression is a subset of the expression in the data
9030 // environment.
9031 IsEnclosedByDataEnvironmentExpr |=
9032 (!CurrentRegionOnly && CI != CE && SI == SE);
9033
9034 return false;
9035 });
9036
9037 if (CurrentRegionOnly)
9038 return FoundError;
9039
9040 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9041 // If any part of the original storage of a list item has corresponding
9042 // storage in the device data environment, all of the original storage must
9043 // have corresponding storage in the device data environment.
9044 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9045 // If a list item is an element of a structure, and a different element of
9046 // the structure has a corresponding list item in the device data environment
9047 // prior to a task encountering the construct associated with the map clause,
9048 // then the list item must also have a correspnding list item in the device
9049 // data environment prior to the task encountering the construct.
9050 //
9051 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9052 SemaRef.Diag(ELoc,
9053 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9054 << ERange;
9055 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9056 << EnclosingExpr->getSourceRange();
9057 return true;
9058 }
9059
9060 return FoundError;
9061}
9062
Samuel Antao23abd722016-01-19 20:40:49 +00009063OMPClause *
9064Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9065 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9066 SourceLocation MapLoc, SourceLocation ColonLoc,
9067 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9068 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009069 SmallVector<Expr *, 4> Vars;
9070
9071 for (auto &RE : VarList) {
9072 assert(RE && "Null expr in omp map");
9073 if (isa<DependentScopeDeclRefExpr>(RE)) {
9074 // It will be analyzed later.
9075 Vars.push_back(RE);
9076 continue;
9077 }
9078 SourceLocation ELoc = RE->getExprLoc();
9079
Kelvin Li0bff7af2015-11-23 05:32:03 +00009080 auto *VE = RE->IgnoreParenLValueCasts();
9081
9082 if (VE->isValueDependent() || VE->isTypeDependent() ||
9083 VE->isInstantiationDependent() ||
9084 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009085 // We can only analyze this information once the missing information is
9086 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009087 Vars.push_back(RE);
9088 continue;
9089 }
9090
9091 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009092
Samuel Antao5de996e2016-01-22 20:21:36 +00009093 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9094 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9095 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009096 continue;
9097 }
9098
Samuel Antao5de996e2016-01-22 20:21:36 +00009099 // Obtain the array or member expression bases if required.
9100 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9101 if (!BE)
9102 continue;
9103
9104 // If the base is a reference to a variable, we rely on that variable for
9105 // the following checks. If it is a 'this' expression we rely on the field.
9106 ValueDecl *D = nullptr;
9107 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9108 D = DRE->getDecl();
9109 } else {
9110 auto *ME = cast<MemberExpr>(BE);
9111 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9112 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009113 }
9114 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009115
Samuel Antao5de996e2016-01-22 20:21:36 +00009116 auto *VD = dyn_cast<VarDecl>(D);
9117 auto *FD = dyn_cast<FieldDecl>(D);
9118
9119 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009120 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009121
9122 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9123 // threadprivate variables cannot appear in a map clause.
9124 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009125 auto DVar = DSAStack->getTopDSA(VD, false);
9126 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9127 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9128 continue;
9129 }
9130
Samuel Antao5de996e2016-01-22 20:21:36 +00009131 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9132 // A list item cannot appear in both a map clause and a data-sharing
9133 // attribute clause on the same construct.
9134 //
9135 // TODO: Implement this check - it cannot currently be tested because of
9136 // missing implementation of the other data sharing clauses in target
9137 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009138
Samuel Antao5de996e2016-01-22 20:21:36 +00009139 // Check conflicts with other map clause expressions. We check the conflicts
9140 // with the current construct separately from the enclosing data
9141 // environment, because the restrictions are different.
9142 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9143 /*CurrentRegionOnly=*/true))
9144 break;
9145 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9146 /*CurrentRegionOnly=*/false))
9147 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009148
Samuel Antao5de996e2016-01-22 20:21:36 +00009149 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9150 // If the type of a list item is a reference to a type T then the type will
9151 // be considered to be T for all purposes of this clause.
9152 QualType Type = D->getType();
9153 if (Type->isReferenceType())
9154 Type = Type->getPointeeType();
9155
9156 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009157 // A list item must have a mappable type.
9158 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9159 DSAStack, Type))
9160 continue;
9161
Samuel Antaodf67fc42016-01-19 19:15:56 +00009162 // target enter data
9163 // OpenMP [2.10.2, Restrictions, p. 99]
9164 // A map-type must be specified in all map clauses and must be either
9165 // to or alloc.
9166 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9167 if (DKind == OMPD_target_enter_data &&
9168 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9169 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009170 << (IsMapTypeImplicit ? 1 : 0)
9171 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009172 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009173 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009174 }
9175
Samuel Antao72590762016-01-19 20:04:50 +00009176 // target exit_data
9177 // OpenMP [2.10.3, Restrictions, p. 102]
9178 // A map-type must be specified in all map clauses and must be either
9179 // from, release, or delete.
9180 DKind = DSAStack->getCurrentDirective();
9181 if (DKind == OMPD_target_exit_data &&
9182 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9183 MapType == OMPC_MAP_delete)) {
9184 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009185 << (IsMapTypeImplicit ? 1 : 0)
9186 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009187 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009188 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009189 }
9190
Kelvin Li0bff7af2015-11-23 05:32:03 +00009191 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009192 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009193 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009194
Samuel Antao5de996e2016-01-22 20:21:36 +00009195 // We need to produce a map clause even if we don't have variables so that
9196 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009197 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009198 MapTypeModifier, MapType, IsMapTypeImplicit,
9199 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009200}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009201
9202OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9203 SourceLocation StartLoc,
9204 SourceLocation LParenLoc,
9205 SourceLocation EndLoc) {
9206 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009207
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009208 // OpenMP [teams Constrcut, Restrictions]
9209 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009210 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9211 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009212 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009213
9214 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9215}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009216
9217OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9218 SourceLocation StartLoc,
9219 SourceLocation LParenLoc,
9220 SourceLocation EndLoc) {
9221 Expr *ValExpr = ThreadLimit;
9222
9223 // OpenMP [teams Constrcut, Restrictions]
9224 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009225 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9226 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009227 return nullptr;
9228
9229 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9230 EndLoc);
9231}
Alexey Bataeva0569352015-12-01 10:17:31 +00009232
9233OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9234 SourceLocation StartLoc,
9235 SourceLocation LParenLoc,
9236 SourceLocation EndLoc) {
9237 Expr *ValExpr = Priority;
9238
9239 // OpenMP [2.9.1, task Constrcut]
9240 // The priority-value is a non-negative numerical scalar expression.
9241 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9242 /*StrictlyPositive=*/false))
9243 return nullptr;
9244
9245 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9246}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009247
9248OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9249 SourceLocation StartLoc,
9250 SourceLocation LParenLoc,
9251 SourceLocation EndLoc) {
9252 Expr *ValExpr = Grainsize;
9253
9254 // OpenMP [2.9.2, taskloop Constrcut]
9255 // The parameter of the grainsize clause must be a positive integer
9256 // expression.
9257 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9258 /*StrictlyPositive=*/true))
9259 return nullptr;
9260
9261 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9262}
Alexey Bataev382967a2015-12-08 12:06:20 +00009263
9264OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9265 SourceLocation StartLoc,
9266 SourceLocation LParenLoc,
9267 SourceLocation EndLoc) {
9268 Expr *ValExpr = NumTasks;
9269
9270 // OpenMP [2.9.2, taskloop Constrcut]
9271 // The parameter of the num_tasks clause must be a positive integer
9272 // expression.
9273 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9274 /*StrictlyPositive=*/true))
9275 return nullptr;
9276
9277 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9278}
9279
Alexey Bataev28c75412015-12-15 08:19:24 +00009280OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9281 SourceLocation LParenLoc,
9282 SourceLocation EndLoc) {
9283 // OpenMP [2.13.2, critical construct, Description]
9284 // ... where hint-expression is an integer constant expression that evaluates
9285 // to a valid lock hint.
9286 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9287 if (HintExpr.isInvalid())
9288 return nullptr;
9289 return new (Context)
9290 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9291}
9292
Carlo Bertollib4adf552016-01-15 18:50:31 +00009293OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9294 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9295 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9296 SourceLocation EndLoc) {
9297 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9298 std::string Values;
9299 Values += "'";
9300 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9301 Values += "'";
9302 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9303 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9304 return nullptr;
9305 }
9306 Expr *ValExpr = ChunkSize;
9307 Expr *HelperValExpr = nullptr;
9308 if (ChunkSize) {
9309 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9310 !ChunkSize->isInstantiationDependent() &&
9311 !ChunkSize->containsUnexpandedParameterPack()) {
9312 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9313 ExprResult Val =
9314 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9315 if (Val.isInvalid())
9316 return nullptr;
9317
9318 ValExpr = Val.get();
9319
9320 // OpenMP [2.7.1, Restrictions]
9321 // chunk_size must be a loop invariant integer expression with a positive
9322 // value.
9323 llvm::APSInt Result;
9324 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9325 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9326 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9327 << "dist_schedule" << ChunkSize->getSourceRange();
9328 return nullptr;
9329 }
9330 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9331 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9332 ChunkSize->getType(), ".chunk.");
9333 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9334 ChunkSize->getExprLoc(),
9335 /*RefersToCapture=*/true);
9336 HelperValExpr = ImpVarRef;
9337 }
9338 }
9339 }
9340
9341 return new (Context)
9342 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9343 Kind, ValExpr, HelperValExpr);
9344}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009345
9346OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9347 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9348 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9349 SourceLocation KindLoc, SourceLocation EndLoc) {
9350 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9351 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9352 Kind != OMPC_DEFAULTMAP_scalar) {
9353 std::string Value;
9354 SourceLocation Loc;
9355 Value += "'";
9356 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9357 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9358 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9359 Loc = MLoc;
9360 } else {
9361 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9362 OMPC_DEFAULTMAP_scalar);
9363 Loc = KindLoc;
9364 }
9365 Value += "'";
9366 Diag(Loc, diag::err_omp_unexpected_clause_value)
9367 << Value << getOpenMPClauseName(OMPC_defaultmap);
9368 return nullptr;
9369 }
9370
9371 return new (Context)
9372 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9373}