blob: f61d959e89bda3b1c0d8b108c9e44b37f81e11f2 [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 Jacob3d58f262016-02-02 04:00: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 Jacob3d58f262016-02-02 04:00: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 Jacob3d58f262016-02-02 04:00: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 Bataev1f092212016-02-02 04:59:52 +00001928 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001929 // | 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 | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001962 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001963 // | 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 | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001996 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001997 // | 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 Jacob3d58f262016-02-02 04:00:47 +00002317 // | target | target | |
2318 // | target | target parallel | |
2319 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002320 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00: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 Jacob3d58f262016-02-02 04:00: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 Jacob3d58f262016-02-02 04:00: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 | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002437 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002438 // | 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 Jacob3d58f262016-02-02 04:00: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 Bataev1f092212016-02-02 04:59:52 +00002512 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2513 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002514 // OpenMP [2.16, Nesting of Regions]
2515 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002516 // OpenMP [2.8.1,simd Construct, Restrictions]
2517 // An ordered construct with the simd clause is the only OpenMP construct
2518 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002519 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2520 return true;
2521 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002522 if (ParentRegion == OMPD_atomic) {
2523 // OpenMP [2.16, Nesting of Regions]
2524 // OpenMP constructs may not be nested inside an atomic region.
2525 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2526 return true;
2527 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002528 if (CurrentRegion == OMPD_section) {
2529 // OpenMP [2.7.2, sections Construct, Restrictions]
2530 // Orphaned section directives are prohibited. That is, the section
2531 // directives must appear within the sections construct and must not be
2532 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002533 if (ParentRegion != OMPD_sections &&
2534 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002535 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2536 << (ParentRegion != OMPD_unknown)
2537 << getOpenMPDirectiveName(ParentRegion);
2538 return true;
2539 }
2540 return false;
2541 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002542 // Allow some constructs to be orphaned (they could be used in functions,
2543 // called from OpenMP regions with the required preconditions).
2544 if (ParentRegion == OMPD_unknown)
2545 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002546 if (CurrentRegion == OMPD_cancellation_point ||
2547 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002548 // OpenMP [2.16, Nesting of Regions]
2549 // A cancellation point construct for which construct-type-clause is
2550 // taskgroup must be nested inside a task construct. A cancellation
2551 // point construct for which construct-type-clause is not taskgroup must
2552 // be closely nested inside an OpenMP construct that matches the type
2553 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002554 // A cancel construct for which construct-type-clause is taskgroup must be
2555 // nested inside a task construct. A cancel construct for which
2556 // construct-type-clause is not taskgroup must be closely nested inside an
2557 // OpenMP construct that matches the type specified in
2558 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002559 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002560 !((CancelRegion == OMPD_parallel &&
2561 (ParentRegion == OMPD_parallel ||
2562 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002563 (CancelRegion == OMPD_for &&
2564 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002565 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2566 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002567 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2568 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002569 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002570 // OpenMP [2.16, Nesting of Regions]
2571 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002572 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002573 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002574 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002575 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002576 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2577 // OpenMP [2.16, Nesting of Regions]
2578 // A critical region may not be nested (closely or otherwise) inside a
2579 // critical region with the same name. Note that this restriction is not
2580 // sufficient to prevent deadlock.
2581 SourceLocation PreviousCriticalLoc;
2582 bool DeadLock =
2583 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2584 OpenMPDirectiveKind K,
2585 const DeclarationNameInfo &DNI,
2586 SourceLocation Loc)
2587 ->bool {
2588 if (K == OMPD_critical &&
2589 DNI.getName() == CurrentName.getName()) {
2590 PreviousCriticalLoc = Loc;
2591 return true;
2592 } else
2593 return false;
2594 },
2595 false /* skip top directive */);
2596 if (DeadLock) {
2597 SemaRef.Diag(StartLoc,
2598 diag::err_omp_prohibited_region_critical_same_name)
2599 << CurrentName.getName();
2600 if (PreviousCriticalLoc.isValid())
2601 SemaRef.Diag(PreviousCriticalLoc,
2602 diag::note_omp_previous_critical_region);
2603 return true;
2604 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002605 } else if (CurrentRegion == OMPD_barrier) {
2606 // OpenMP [2.16, Nesting of Regions]
2607 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002608 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002609 NestingProhibited =
2610 isOpenMPWorksharingDirective(ParentRegion) ||
2611 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002612 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002613 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002614 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002615 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002616 // OpenMP [2.16, Nesting of Regions]
2617 // A worksharing region may not be closely nested inside a worksharing,
2618 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002619 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002620 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002621 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002622 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002623 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002624 Recommend = ShouldBeInParallelRegion;
2625 } else if (CurrentRegion == OMPD_ordered) {
2626 // OpenMP [2.16, Nesting of Regions]
2627 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002628 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002629 // An ordered region must be closely nested inside a loop region (or
2630 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002631 // OpenMP [2.8.1,simd Construct, Restrictions]
2632 // An ordered construct with the simd clause is the only OpenMP construct
2633 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002634 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002635 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002636 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002637 !(isOpenMPSimdDirective(ParentRegion) ||
2638 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002639 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002640 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2641 // OpenMP [2.16, Nesting of Regions]
2642 // If specified, a teams construct must be contained within a target
2643 // construct.
2644 NestingProhibited = ParentRegion != OMPD_target;
2645 Recommend = ShouldBeInTargetRegion;
2646 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2647 }
2648 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2649 // OpenMP [2.16, Nesting of Regions]
2650 // distribute, parallel, parallel sections, parallel workshare, and the
2651 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2652 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002653 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2654 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002655 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002656 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002657 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2658 // OpenMP 4.5 [2.17 Nesting of Regions]
2659 // The region associated with the distribute construct must be strictly
2660 // nested inside a teams region
2661 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2662 Recommend = ShouldBeInTeamsRegion;
2663 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002664 if (!NestingProhibited &&
2665 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2666 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2667 // OpenMP 4.5 [2.17 Nesting of Regions]
2668 // If a target, target update, target data, target enter data, or
2669 // target exit data construct is encountered during execution of a
2670 // target region, the behavior is unspecified.
2671 NestingProhibited = Stack->hasDirective(
2672 [&OffendingRegion](OpenMPDirectiveKind K,
2673 const DeclarationNameInfo &DNI,
2674 SourceLocation Loc) -> bool {
2675 if (isOpenMPTargetExecutionDirective(K)) {
2676 OffendingRegion = K;
2677 return true;
2678 } else
2679 return false;
2680 },
2681 false /* don't skip top directive */);
2682 CloseNesting = false;
2683 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002684 if (NestingProhibited) {
2685 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002686 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2687 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002688 return true;
2689 }
2690 }
2691 return false;
2692}
2693
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002694static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2695 ArrayRef<OMPClause *> Clauses,
2696 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2697 bool ErrorFound = false;
2698 unsigned NamedModifiersNumber = 0;
2699 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2700 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002701 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002702 for (const auto *C : Clauses) {
2703 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2704 // At most one if clause without a directive-name-modifier can appear on
2705 // the directive.
2706 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2707 if (FoundNameModifiers[CurNM]) {
2708 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2709 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2710 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2711 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002712 } else if (CurNM != OMPD_unknown) {
2713 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002714 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002715 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002716 FoundNameModifiers[CurNM] = IC;
2717 if (CurNM == OMPD_unknown)
2718 continue;
2719 // Check if the specified name modifier is allowed for the current
2720 // directive.
2721 // At most one if clause with the particular directive-name-modifier can
2722 // appear on the directive.
2723 bool MatchFound = false;
2724 for (auto NM : AllowedNameModifiers) {
2725 if (CurNM == NM) {
2726 MatchFound = true;
2727 break;
2728 }
2729 }
2730 if (!MatchFound) {
2731 S.Diag(IC->getNameModifierLoc(),
2732 diag::err_omp_wrong_if_directive_name_modifier)
2733 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2734 ErrorFound = true;
2735 }
2736 }
2737 }
2738 // If any if clause on the directive includes a directive-name-modifier then
2739 // all if clauses on the directive must include a directive-name-modifier.
2740 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2741 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2742 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2743 diag::err_omp_no_more_if_clause);
2744 } else {
2745 std::string Values;
2746 std::string Sep(", ");
2747 unsigned AllowedCnt = 0;
2748 unsigned TotalAllowedNum =
2749 AllowedNameModifiers.size() - NamedModifiersNumber;
2750 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2751 ++Cnt) {
2752 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2753 if (!FoundNameModifiers[NM]) {
2754 Values += "'";
2755 Values += getOpenMPDirectiveName(NM);
2756 Values += "'";
2757 if (AllowedCnt + 2 == TotalAllowedNum)
2758 Values += " or ";
2759 else if (AllowedCnt + 1 != TotalAllowedNum)
2760 Values += Sep;
2761 ++AllowedCnt;
2762 }
2763 }
2764 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2765 diag::err_omp_unnamed_if_clause)
2766 << (TotalAllowedNum > 1) << Values;
2767 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002768 for (auto Loc : NameModifierLoc) {
2769 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2770 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002771 ErrorFound = true;
2772 }
2773 return ErrorFound;
2774}
2775
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002776StmtResult Sema::ActOnOpenMPExecutableDirective(
2777 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2778 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2779 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002780 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002781 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2782 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002783 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002784
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002785 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002786 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002787 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002788 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002789 if (AStmt) {
2790 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2791
2792 // Check default data sharing attributes for referenced variables.
2793 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2794 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2795 if (DSAChecker.isErrorFound())
2796 return StmtError();
2797 // Generate list of implicitly defined firstprivate variables.
2798 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002799
2800 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2801 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2802 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2803 SourceLocation(), SourceLocation())) {
2804 ClausesWithImplicit.push_back(Implicit);
2805 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2806 DSAChecker.getImplicitFirstprivate().size();
2807 } else
2808 ErrorFound = true;
2809 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002810 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002811
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002812 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002813 switch (Kind) {
2814 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002815 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2816 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002817 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002818 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002819 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002820 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2821 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002822 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002823 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002824 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2825 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002826 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002827 case OMPD_for_simd:
2828 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2829 EndLoc, VarsWithInheritedDSA);
2830 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002831 case OMPD_sections:
2832 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2833 EndLoc);
2834 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002835 case OMPD_section:
2836 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002837 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002838 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2839 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002840 case OMPD_single:
2841 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2842 EndLoc);
2843 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002844 case OMPD_master:
2845 assert(ClausesWithImplicit.empty() &&
2846 "No clauses are allowed for 'omp master' directive");
2847 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2848 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002849 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002850 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2851 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002852 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002853 case OMPD_parallel_for:
2854 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2855 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002856 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002857 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002858 case OMPD_parallel_for_simd:
2859 Res = ActOnOpenMPParallelForSimdDirective(
2860 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002861 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002862 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002863 case OMPD_parallel_sections:
2864 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2865 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002866 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002867 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002868 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002869 Res =
2870 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002871 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002872 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002873 case OMPD_taskyield:
2874 assert(ClausesWithImplicit.empty() &&
2875 "No clauses are allowed for 'omp taskyield' directive");
2876 assert(AStmt == nullptr &&
2877 "No associated statement allowed for 'omp taskyield' directive");
2878 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2879 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002880 case OMPD_barrier:
2881 assert(ClausesWithImplicit.empty() &&
2882 "No clauses are allowed for 'omp barrier' directive");
2883 assert(AStmt == nullptr &&
2884 "No associated statement allowed for 'omp barrier' directive");
2885 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2886 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002887 case OMPD_taskwait:
2888 assert(ClausesWithImplicit.empty() &&
2889 "No clauses are allowed for 'omp taskwait' directive");
2890 assert(AStmt == nullptr &&
2891 "No associated statement allowed for 'omp taskwait' directive");
2892 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2893 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002894 case OMPD_taskgroup:
2895 assert(ClausesWithImplicit.empty() &&
2896 "No clauses are allowed for 'omp taskgroup' directive");
2897 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2898 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002899 case OMPD_flush:
2900 assert(AStmt == nullptr &&
2901 "No associated statement allowed for 'omp flush' directive");
2902 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2903 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002904 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002905 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2906 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002907 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002908 case OMPD_atomic:
2909 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2910 EndLoc);
2911 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002912 case OMPD_teams:
2913 Res =
2914 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2915 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002916 case OMPD_target:
2917 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2918 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002919 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002920 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002921 case OMPD_target_parallel:
2922 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2923 StartLoc, EndLoc);
2924 AllowedNameModifiers.push_back(OMPD_target);
2925 AllowedNameModifiers.push_back(OMPD_parallel);
2926 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002927 case OMPD_cancellation_point:
2928 assert(ClausesWithImplicit.empty() &&
2929 "No clauses are allowed for 'omp cancellation point' directive");
2930 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2931 "cancellation point' directive");
2932 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2933 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002934 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002935 assert(AStmt == nullptr &&
2936 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002937 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2938 CancelRegion);
2939 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002940 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002941 case OMPD_target_data:
2942 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2943 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002944 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002945 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002946 case OMPD_target_enter_data:
2947 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2948 EndLoc);
2949 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2950 break;
Samuel Antao72590762016-01-19 20:04:50 +00002951 case OMPD_target_exit_data:
2952 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2953 EndLoc);
2954 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2955 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002956 case OMPD_taskloop:
2957 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2958 EndLoc, VarsWithInheritedDSA);
2959 AllowedNameModifiers.push_back(OMPD_taskloop);
2960 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002961 case OMPD_taskloop_simd:
2962 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2963 EndLoc, VarsWithInheritedDSA);
2964 AllowedNameModifiers.push_back(OMPD_taskloop);
2965 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002966 case OMPD_distribute:
2967 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2968 EndLoc, VarsWithInheritedDSA);
2969 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002970 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002971 llvm_unreachable("OpenMP Directive is not allowed");
2972 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002973 llvm_unreachable("Unknown OpenMP directive");
2974 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002975
Alexey Bataev4acb8592014-07-07 13:01:15 +00002976 for (auto P : VarsWithInheritedDSA) {
2977 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2978 << P.first << P.second->getSourceRange();
2979 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002980 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2981
2982 if (!AllowedNameModifiers.empty())
2983 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2984 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002985
Alexey Bataeved09d242014-05-28 05:53:51 +00002986 if (ErrorFound)
2987 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002988 return Res;
2989}
2990
2991StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2992 Stmt *AStmt,
2993 SourceLocation StartLoc,
2994 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002995 if (!AStmt)
2996 return StmtError();
2997
Alexey Bataev9959db52014-05-06 10:08:46 +00002998 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2999 // 1.2.2 OpenMP Language Terminology
3000 // Structured block - An executable statement with a single entry at the
3001 // top and a single exit at the bottom.
3002 // The point of exit cannot be a branch out of the structured block.
3003 // longjmp() and throw() must not violate the entry/exit criteria.
3004 CS->getCapturedDecl()->setNothrow();
3005
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003006 getCurFunction()->setHasBranchProtectedScope();
3007
Alexey Bataev25e5b442015-09-15 12:52:43 +00003008 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3009 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003010}
3011
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003012namespace {
3013/// \brief Helper class for checking canonical form of the OpenMP loops and
3014/// extracting iteration space of each loop in the loop nest, that will be used
3015/// for IR generation.
3016class OpenMPIterationSpaceChecker {
3017 /// \brief Reference to Sema.
3018 Sema &SemaRef;
3019 /// \brief A location for diagnostics (when there is no some better location).
3020 SourceLocation DefaultLoc;
3021 /// \brief A location for diagnostics (when increment is not compatible).
3022 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003023 /// \brief A source location for referring to loop init later.
3024 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003025 /// \brief A source location for referring to condition later.
3026 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003027 /// \brief A source location for referring to increment later.
3028 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003029 /// \brief Loop variable.
3030 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003031 /// \brief Reference to loop variable.
3032 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003033 /// \brief Lower bound (initializer for the var).
3034 Expr *LB;
3035 /// \brief Upper bound.
3036 Expr *UB;
3037 /// \brief Loop step (increment).
3038 Expr *Step;
3039 /// \brief This flag is true when condition is one of:
3040 /// Var < UB
3041 /// Var <= UB
3042 /// UB > Var
3043 /// UB >= Var
3044 bool TestIsLessOp;
3045 /// \brief This flag is true when condition is strict ( < or > ).
3046 bool TestIsStrictOp;
3047 /// \brief This flag is true when step is subtracted on each iteration.
3048 bool SubtractStep;
3049
3050public:
3051 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3052 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003053 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3054 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003055 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3056 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003057 /// \brief Check init-expr for canonical loop form and save loop counter
3058 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003059 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003060 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3061 /// for less/greater and for strict/non-strict comparison.
3062 bool CheckCond(Expr *S);
3063 /// \brief Check incr-expr for canonical loop form and return true if it
3064 /// does not conform, otherwise save loop step (#Step).
3065 bool CheckInc(Expr *S);
3066 /// \brief Return the loop counter variable.
3067 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003068 /// \brief Return the reference expression to loop counter variable.
3069 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003070 /// \brief Source range of the loop init.
3071 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3072 /// \brief Source range of the loop condition.
3073 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3074 /// \brief Source range of the loop increment.
3075 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3076 /// \brief True if the step should be subtracted.
3077 bool ShouldSubtractStep() const { return SubtractStep; }
3078 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003079 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003080 /// \brief Build the precondition expression for the loops.
3081 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003082 /// \brief Build reference expression to the counter be used for codegen.
3083 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003084 /// \brief Build reference expression to the private counter be used for
3085 /// codegen.
3086 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003087 /// \brief Build initization of the counter be used for codegen.
3088 Expr *BuildCounterInit() const;
3089 /// \brief Build step of the counter be used for codegen.
3090 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003091 /// \brief Return true if any expression is dependent.
3092 bool Dependent() const;
3093
3094private:
3095 /// \brief Check the right-hand side of an assignment in the increment
3096 /// expression.
3097 bool CheckIncRHS(Expr *RHS);
3098 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003099 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003100 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003101 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003102 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003103 /// \brief Helper to set loop increment.
3104 bool SetStep(Expr *NewStep, bool Subtract);
3105};
3106
3107bool OpenMPIterationSpaceChecker::Dependent() const {
3108 if (!Var) {
3109 assert(!LB && !UB && !Step);
3110 return false;
3111 }
3112 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3113 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3114}
3115
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003116template <typename T>
3117static T *getExprAsWritten(T *E) {
3118 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3119 E = ExprTemp->getSubExpr();
3120
3121 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3122 E = MTE->GetTemporaryExpr();
3123
3124 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3125 E = Binder->getSubExpr();
3126
3127 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3128 E = ICE->getSubExprAsWritten();
3129 return E->IgnoreParens();
3130}
3131
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003132bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3133 DeclRefExpr *NewVarRefExpr,
3134 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003135 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003136 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3137 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003138 if (!NewVar || !NewLB)
3139 return true;
3140 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003141 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003142 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3143 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003144 if ((Ctor->isCopyOrMoveConstructor() ||
3145 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3146 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003147 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003148 LB = NewLB;
3149 return false;
3150}
3151
3152bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003153 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003154 // State consistency checking to ensure correct usage.
3155 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3156 !TestIsLessOp && !TestIsStrictOp);
3157 if (!NewUB)
3158 return true;
3159 UB = NewUB;
3160 TestIsLessOp = LessOp;
3161 TestIsStrictOp = StrictOp;
3162 ConditionSrcRange = SR;
3163 ConditionLoc = SL;
3164 return false;
3165}
3166
3167bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3168 // State consistency checking to ensure correct usage.
3169 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3170 if (!NewStep)
3171 return true;
3172 if (!NewStep->isValueDependent()) {
3173 // Check that the step is integer expression.
3174 SourceLocation StepLoc = NewStep->getLocStart();
3175 ExprResult Val =
3176 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3177 if (Val.isInvalid())
3178 return true;
3179 NewStep = Val.get();
3180
3181 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3182 // If test-expr is of form var relational-op b and relational-op is < or
3183 // <= then incr-expr must cause var to increase on each iteration of the
3184 // loop. If test-expr is of form var relational-op b and relational-op is
3185 // > or >= then incr-expr must cause var to decrease on each iteration of
3186 // the loop.
3187 // If test-expr is of form b relational-op var and relational-op is < or
3188 // <= then incr-expr must cause var to decrease on each iteration of the
3189 // loop. If test-expr is of form b relational-op var and relational-op is
3190 // > or >= then incr-expr must cause var to increase on each iteration of
3191 // the loop.
3192 llvm::APSInt Result;
3193 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3194 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3195 bool IsConstNeg =
3196 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003197 bool IsConstPos =
3198 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003199 bool IsConstZero = IsConstant && !Result.getBoolValue();
3200 if (UB && (IsConstZero ||
3201 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003202 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003203 SemaRef.Diag(NewStep->getExprLoc(),
3204 diag::err_omp_loop_incr_not_compatible)
3205 << Var << TestIsLessOp << NewStep->getSourceRange();
3206 SemaRef.Diag(ConditionLoc,
3207 diag::note_omp_loop_cond_requres_compatible_incr)
3208 << TestIsLessOp << ConditionSrcRange;
3209 return true;
3210 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003211 if (TestIsLessOp == Subtract) {
3212 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3213 NewStep).get();
3214 Subtract = !Subtract;
3215 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003216 }
3217
3218 Step = NewStep;
3219 SubtractStep = Subtract;
3220 return false;
3221}
3222
Alexey Bataev9c821032015-04-30 04:23:23 +00003223bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003224 // Check init-expr for canonical loop form and save loop counter
3225 // variable - #Var and its initialization value - #LB.
3226 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3227 // var = lb
3228 // integer-type var = lb
3229 // random-access-iterator-type var = lb
3230 // pointer-type var = lb
3231 //
3232 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003233 if (EmitDiags) {
3234 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3235 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003236 return true;
3237 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003238 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003239 if (Expr *E = dyn_cast<Expr>(S))
3240 S = E->IgnoreParens();
3241 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3242 if (BO->getOpcode() == BO_Assign)
3243 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003244 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003245 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003246 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3247 if (DS->isSingleDecl()) {
3248 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003249 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003250 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003251 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003252 SemaRef.Diag(S->getLocStart(),
3253 diag::ext_omp_loop_not_canonical_init)
3254 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003255 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003256 }
3257 }
3258 }
3259 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3260 if (CE->getOperator() == OO_Equal)
3261 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003262 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3263 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003264
Alexey Bataev9c821032015-04-30 04:23:23 +00003265 if (EmitDiags) {
3266 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3267 << S->getSourceRange();
3268 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003269 return true;
3270}
3271
Alexey Bataev23b69422014-06-18 07:08:49 +00003272/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003273/// variable (which may be the loop variable) if possible.
3274static const VarDecl *GetInitVarDecl(const Expr *E) {
3275 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003276 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003277 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003278 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3279 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003280 if ((Ctor->isCopyOrMoveConstructor() ||
3281 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3282 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003283 E = CE->getArg(0)->IgnoreParenImpCasts();
3284 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3285 if (!DRE)
3286 return nullptr;
3287 return dyn_cast<VarDecl>(DRE->getDecl());
3288}
3289
3290bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3291 // Check test-expr for canonical form, save upper-bound UB, flags for
3292 // less/greater and for strict/non-strict comparison.
3293 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3294 // var relational-op b
3295 // b relational-op var
3296 //
3297 if (!S) {
3298 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3299 return true;
3300 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003301 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003302 SourceLocation CondLoc = S->getLocStart();
3303 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3304 if (BO->isRelationalOp()) {
3305 if (GetInitVarDecl(BO->getLHS()) == Var)
3306 return SetUB(BO->getRHS(),
3307 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3308 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3309 BO->getSourceRange(), BO->getOperatorLoc());
3310 if (GetInitVarDecl(BO->getRHS()) == Var)
3311 return SetUB(BO->getLHS(),
3312 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3313 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3314 BO->getSourceRange(), BO->getOperatorLoc());
3315 }
3316 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3317 if (CE->getNumArgs() == 2) {
3318 auto Op = CE->getOperator();
3319 switch (Op) {
3320 case OO_Greater:
3321 case OO_GreaterEqual:
3322 case OO_Less:
3323 case OO_LessEqual:
3324 if (GetInitVarDecl(CE->getArg(0)) == Var)
3325 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3326 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3327 CE->getOperatorLoc());
3328 if (GetInitVarDecl(CE->getArg(1)) == Var)
3329 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3330 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3331 CE->getOperatorLoc());
3332 break;
3333 default:
3334 break;
3335 }
3336 }
3337 }
3338 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3339 << S->getSourceRange() << Var;
3340 return true;
3341}
3342
3343bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3344 // RHS of canonical loop form increment can be:
3345 // var + incr
3346 // incr + var
3347 // var - incr
3348 //
3349 RHS = RHS->IgnoreParenImpCasts();
3350 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3351 if (BO->isAdditiveOp()) {
3352 bool IsAdd = BO->getOpcode() == BO_Add;
3353 if (GetInitVarDecl(BO->getLHS()) == Var)
3354 return SetStep(BO->getRHS(), !IsAdd);
3355 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3356 return SetStep(BO->getLHS(), false);
3357 }
3358 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3359 bool IsAdd = CE->getOperator() == OO_Plus;
3360 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3361 if (GetInitVarDecl(CE->getArg(0)) == Var)
3362 return SetStep(CE->getArg(1), !IsAdd);
3363 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3364 return SetStep(CE->getArg(0), false);
3365 }
3366 }
3367 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3368 << RHS->getSourceRange() << Var;
3369 return true;
3370}
3371
3372bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3373 // Check incr-expr for canonical loop form and return true if it
3374 // does not conform.
3375 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3376 // ++var
3377 // var++
3378 // --var
3379 // var--
3380 // var += incr
3381 // var -= incr
3382 // var = var + incr
3383 // var = incr + var
3384 // var = var - incr
3385 //
3386 if (!S) {
3387 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3388 return true;
3389 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003390 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003391 S = S->IgnoreParens();
3392 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3393 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3394 return SetStep(
3395 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3396 (UO->isDecrementOp() ? -1 : 1)).get(),
3397 false);
3398 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3399 switch (BO->getOpcode()) {
3400 case BO_AddAssign:
3401 case BO_SubAssign:
3402 if (GetInitVarDecl(BO->getLHS()) == Var)
3403 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3404 break;
3405 case BO_Assign:
3406 if (GetInitVarDecl(BO->getLHS()) == Var)
3407 return CheckIncRHS(BO->getRHS());
3408 break;
3409 default:
3410 break;
3411 }
3412 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3413 switch (CE->getOperator()) {
3414 case OO_PlusPlus:
3415 case OO_MinusMinus:
3416 if (GetInitVarDecl(CE->getArg(0)) == Var)
3417 return SetStep(
3418 SemaRef.ActOnIntegerConstant(
3419 CE->getLocStart(),
3420 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3421 false);
3422 break;
3423 case OO_PlusEqual:
3424 case OO_MinusEqual:
3425 if (GetInitVarDecl(CE->getArg(0)) == Var)
3426 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3427 break;
3428 case OO_Equal:
3429 if (GetInitVarDecl(CE->getArg(0)) == Var)
3430 return CheckIncRHS(CE->getArg(1));
3431 break;
3432 default:
3433 break;
3434 }
3435 }
3436 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3437 << S->getSourceRange() << Var;
3438 return true;
3439}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003440
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003441namespace {
3442// Transform variables declared in GNU statement expressions to new ones to
3443// avoid crash on codegen.
3444class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3445 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3446
3447public:
3448 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3449
3450 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3451 if (auto *VD = cast<VarDecl>(D))
3452 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3453 !isa<ImplicitParamDecl>(D)) {
3454 auto *NewVD = VarDecl::Create(
3455 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3456 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3457 VD->getTypeSourceInfo(), VD->getStorageClass());
3458 NewVD->setTSCSpec(VD->getTSCSpec());
3459 NewVD->setInit(VD->getInit());
3460 NewVD->setInitStyle(VD->getInitStyle());
3461 NewVD->setExceptionVariable(VD->isExceptionVariable());
3462 NewVD->setNRVOVariable(VD->isNRVOVariable());
3463 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3464 NewVD->setConstexpr(VD->isConstexpr());
3465 NewVD->setInitCapture(VD->isInitCapture());
3466 NewVD->setPreviousDeclInSameBlockScope(
3467 VD->isPreviousDeclInSameBlockScope());
3468 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003469 if (VD->hasAttrs())
3470 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003471 transformedLocalDecl(VD, NewVD);
3472 return NewVD;
3473 }
3474 return BaseTransform::TransformDefinition(Loc, D);
3475 }
3476
3477 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3478 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3479 if (E->getDecl() != NewD) {
3480 NewD->setReferenced();
3481 NewD->markUsed(SemaRef.Context);
3482 return DeclRefExpr::Create(
3483 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3484 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3485 E->getNameInfo(), E->getType(), E->getValueKind());
3486 }
3487 return BaseTransform::TransformDeclRefExpr(E);
3488 }
3489};
3490}
3491
Alexander Musmana5f070a2014-10-01 06:03:56 +00003492/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003493Expr *
3494OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3495 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003496 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003497 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003498 auto VarType = Var->getType().getNonReferenceType();
3499 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003500 SemaRef.getLangOpts().CPlusPlus) {
3501 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003502 auto *UBExpr = TestIsLessOp ? UB : LB;
3503 auto *LBExpr = TestIsLessOp ? LB : UB;
3504 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3505 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3506 if (!Upper || !Lower)
3507 return nullptr;
3508 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3509 Sema::AA_Converting,
3510 /*AllowExplicit=*/true)
3511 .get();
3512 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3513 Sema::AA_Converting,
3514 /*AllowExplicit=*/true)
3515 .get();
3516 if (!Upper || !Lower)
3517 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003518
3519 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3520
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003521 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003522 // BuildBinOp already emitted error, this one is to point user to upper
3523 // and lower bound, and to tell what is passed to 'operator-'.
3524 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3525 << Upper->getSourceRange() << Lower->getSourceRange();
3526 return nullptr;
3527 }
3528 }
3529
3530 if (!Diff.isUsable())
3531 return nullptr;
3532
3533 // Upper - Lower [- 1]
3534 if (TestIsStrictOp)
3535 Diff = SemaRef.BuildBinOp(
3536 S, DefaultLoc, BO_Sub, Diff.get(),
3537 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3538 if (!Diff.isUsable())
3539 return nullptr;
3540
3541 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003542 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3543 if (NewStep.isInvalid())
3544 return nullptr;
3545 NewStep = SemaRef.PerformImplicitConversion(
3546 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3547 /*AllowExplicit=*/true);
3548 if (NewStep.isInvalid())
3549 return nullptr;
3550 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003551 if (!Diff.isUsable())
3552 return nullptr;
3553
3554 // Parentheses (for dumping/debugging purposes only).
3555 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3556 if (!Diff.isUsable())
3557 return nullptr;
3558
3559 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003560 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3561 if (NewStep.isInvalid())
3562 return nullptr;
3563 NewStep = SemaRef.PerformImplicitConversion(
3564 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3565 /*AllowExplicit=*/true);
3566 if (NewStep.isInvalid())
3567 return nullptr;
3568 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003569 if (!Diff.isUsable())
3570 return nullptr;
3571
Alexander Musman174b3ca2014-10-06 11:16:29 +00003572 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003573 QualType Type = Diff.get()->getType();
3574 auto &C = SemaRef.Context;
3575 bool UseVarType = VarType->hasIntegerRepresentation() &&
3576 C.getTypeSize(Type) > C.getTypeSize(VarType);
3577 if (!Type->isIntegerType() || UseVarType) {
3578 unsigned NewSize =
3579 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3580 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3581 : Type->hasSignedIntegerRepresentation();
3582 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3583 Diff = SemaRef.PerformImplicitConversion(
3584 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3585 if (!Diff.isUsable())
3586 return nullptr;
3587 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003588 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003589 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3590 if (NewSize != C.getTypeSize(Type)) {
3591 if (NewSize < C.getTypeSize(Type)) {
3592 assert(NewSize == 64 && "incorrect loop var size");
3593 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3594 << InitSrcRange << ConditionSrcRange;
3595 }
3596 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003597 NewSize, Type->hasSignedIntegerRepresentation() ||
3598 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003599 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3600 Sema::AA_Converting, true);
3601 if (!Diff.isUsable())
3602 return nullptr;
3603 }
3604 }
3605
Alexander Musmana5f070a2014-10-01 06:03:56 +00003606 return Diff.get();
3607}
3608
Alexey Bataev62dbb972015-04-22 11:59:37 +00003609Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3610 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3611 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3612 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003613 TransformToNewDefs Transform(SemaRef);
3614
3615 auto NewLB = Transform.TransformExpr(LB);
3616 auto NewUB = Transform.TransformExpr(UB);
3617 if (NewLB.isInvalid() || NewUB.isInvalid())
3618 return Cond;
3619 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3620 Sema::AA_Converting,
3621 /*AllowExplicit=*/true);
3622 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3623 Sema::AA_Converting,
3624 /*AllowExplicit=*/true);
3625 if (NewLB.isInvalid() || NewUB.isInvalid())
3626 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003627 auto CondExpr = SemaRef.BuildBinOp(
3628 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3629 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003630 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003631 if (CondExpr.isUsable()) {
3632 CondExpr = SemaRef.PerformImplicitConversion(
3633 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3634 /*AllowExplicit=*/true);
3635 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003636 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3637 // Otherwise use original loop conditon and evaluate it in runtime.
3638 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3639}
3640
Alexander Musmana5f070a2014-10-01 06:03:56 +00003641/// \brief Build reference expression to the counter be used for codegen.
3642Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003643 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3644 DefaultLoc);
3645}
3646
3647Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3648 if (Var && !Var->isInvalidDecl()) {
3649 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003650 auto *PrivateVar =
3651 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3652 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003653 if (PrivateVar->isInvalidDecl())
3654 return nullptr;
3655 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3656 }
3657 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003658}
3659
3660/// \brief Build initization of the counter be used for codegen.
3661Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3662
3663/// \brief Build step of the counter be used for codegen.
3664Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3665
3666/// \brief Iteration space of a single for loop.
3667struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003668 /// \brief Condition of the loop.
3669 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003670 /// \brief This expression calculates the number of iterations in the loop.
3671 /// It is always possible to calculate it before starting the loop.
3672 Expr *NumIterations;
3673 /// \brief The loop counter variable.
3674 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003675 /// \brief Private loop counter variable.
3676 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003677 /// \brief This is initializer for the initial value of #CounterVar.
3678 Expr *CounterInit;
3679 /// \brief This is step for the #CounterVar used to generate its update:
3680 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3681 Expr *CounterStep;
3682 /// \brief Should step be subtracted?
3683 bool Subtract;
3684 /// \brief Source range of the loop init.
3685 SourceRange InitSrcRange;
3686 /// \brief Source range of the loop condition.
3687 SourceRange CondSrcRange;
3688 /// \brief Source range of the loop increment.
3689 SourceRange IncSrcRange;
3690};
3691
Alexey Bataev23b69422014-06-18 07:08:49 +00003692} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003693
Alexey Bataev9c821032015-04-30 04:23:23 +00003694void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3695 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3696 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003697 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3698 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003699 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3700 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003701 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003702 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003703 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003704 }
3705}
3706
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003707/// \brief Called on a for stmt to check and extract its iteration space
3708/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003709static bool CheckOpenMPIterationSpace(
3710 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3711 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003712 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003713 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003714 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003715 // OpenMP [2.6, Canonical Loop Form]
3716 // for (init-expr; test-expr; incr-expr) structured-block
3717 auto For = dyn_cast_or_null<ForStmt>(S);
3718 if (!For) {
3719 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003720 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3721 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3722 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3723 if (NestedLoopCount > 1) {
3724 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3725 SemaRef.Diag(DSA.getConstructLoc(),
3726 diag::note_omp_collapse_ordered_expr)
3727 << 2 << CollapseLoopCountExpr->getSourceRange()
3728 << OrderedLoopCountExpr->getSourceRange();
3729 else if (CollapseLoopCountExpr)
3730 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3731 diag::note_omp_collapse_ordered_expr)
3732 << 0 << CollapseLoopCountExpr->getSourceRange();
3733 else
3734 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3735 diag::note_omp_collapse_ordered_expr)
3736 << 1 << OrderedLoopCountExpr->getSourceRange();
3737 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003738 return true;
3739 }
3740 assert(For->getBody());
3741
3742 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3743
3744 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003745 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003746 if (ISC.CheckInit(Init)) {
3747 return true;
3748 }
3749
3750 bool HasErrors = false;
3751
3752 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003753 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003754
3755 // OpenMP [2.6, Canonical Loop Form]
3756 // Var is one of the following:
3757 // A variable of signed or unsigned integer type.
3758 // For C++, a variable of a random access iterator type.
3759 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003760 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003761 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3762 !VarType->isPointerType() &&
3763 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3764 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3765 << SemaRef.getLangOpts().CPlusPlus;
3766 HasErrors = true;
3767 }
3768
Alexey Bataev4acb8592014-07-07 13:01:15 +00003769 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3770 // Construct
3771 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3772 // parallel for construct is (are) private.
3773 // The loop iteration variable in the associated for-loop of a simd construct
3774 // with just one associated for-loop is linear with a constant-linear-step
3775 // that is the increment of the associated for-loop.
3776 // Exclude loop var from the list of variables with implicitly defined data
3777 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003778 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003779
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003780 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3781 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003782 // The loop iteration variable in the associated for-loop of a simd construct
3783 // with just one associated for-loop may be listed in a linear clause with a
3784 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003785 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3786 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003787 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003788 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3789 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3790 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003791 auto PredeterminedCKind =
3792 isOpenMPSimdDirective(DKind)
3793 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3794 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003795 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003796 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003797 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003798 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003799 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003800 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3801 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003802 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003803 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3804 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003805 if (DVar.RefExpr == nullptr)
3806 DVar.CKind = PredeterminedCKind;
3807 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003808 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003809 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003810 // Make the loop iteration variable private (for worksharing constructs),
3811 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003812 // lastprivate (for simd directives with several collapsed or ordered
3813 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003814 if (DVar.CKind == OMPC_unknown)
3815 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3816 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003817 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003818 }
3819
Alexey Bataev7ff55242014-06-19 09:13:45 +00003820 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003821
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003822 // Check test-expr.
3823 HasErrors |= ISC.CheckCond(For->getCond());
3824
3825 // Check incr-expr.
3826 HasErrors |= ISC.CheckInc(For->getInc());
3827
Alexander Musmana5f070a2014-10-01 06:03:56 +00003828 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003829 return HasErrors;
3830
Alexander Musmana5f070a2014-10-01 06:03:56 +00003831 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003832 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003833 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003834 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003835 isOpenMPTaskLoopDirective(DKind) ||
3836 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003837 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003838 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003839 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3840 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3841 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3842 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3843 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3844 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3845
Alexey Bataev62dbb972015-04-22 11:59:37 +00003846 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3847 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003848 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003849 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003850 ResultIterSpace.CounterInit == nullptr ||
3851 ResultIterSpace.CounterStep == nullptr);
3852
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003853 return HasErrors;
3854}
3855
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003856/// \brief Build 'VarRef = Start.
3857static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3858 ExprResult VarRef, ExprResult Start) {
3859 TransformToNewDefs Transform(SemaRef);
3860 // Build 'VarRef = Start.
3861 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3862 if (NewStart.isInvalid())
3863 return ExprError();
3864 NewStart = SemaRef.PerformImplicitConversion(
3865 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3866 Sema::AA_Converting,
3867 /*AllowExplicit=*/true);
3868 if (NewStart.isInvalid())
3869 return ExprError();
3870 NewStart = SemaRef.PerformImplicitConversion(
3871 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3872 /*AllowExplicit=*/true);
3873 if (!NewStart.isUsable())
3874 return ExprError();
3875
3876 auto Init =
3877 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3878 return Init;
3879}
3880
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881/// \brief Build 'VarRef = Start + Iter * Step'.
3882static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3883 SourceLocation Loc, ExprResult VarRef,
3884 ExprResult Start, ExprResult Iter,
3885 ExprResult Step, bool Subtract) {
3886 // Add parentheses (for debugging purposes only).
3887 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3888 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3889 !Step.isUsable())
3890 return ExprError();
3891
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003892 TransformToNewDefs Transform(SemaRef);
3893 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3894 if (NewStep.isInvalid())
3895 return ExprError();
3896 NewStep = SemaRef.PerformImplicitConversion(
3897 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3898 Sema::AA_Converting,
3899 /*AllowExplicit=*/true);
3900 if (NewStep.isInvalid())
3901 return ExprError();
3902 ExprResult Update =
3903 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003904 if (!Update.isUsable())
3905 return ExprError();
3906
3907 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003908 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3909 if (NewStart.isInvalid())
3910 return ExprError();
3911 NewStart = SemaRef.PerformImplicitConversion(
3912 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3913 Sema::AA_Converting,
3914 /*AllowExplicit=*/true);
3915 if (NewStart.isInvalid())
3916 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003917 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003918 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003919 if (!Update.isUsable())
3920 return ExprError();
3921
3922 Update = SemaRef.PerformImplicitConversion(
3923 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3924 if (!Update.isUsable())
3925 return ExprError();
3926
3927 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3928 return Update;
3929}
3930
3931/// \brief Convert integer expression \a E to make it have at least \a Bits
3932/// bits.
3933static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3934 Sema &SemaRef) {
3935 if (E == nullptr)
3936 return ExprError();
3937 auto &C = SemaRef.Context;
3938 QualType OldType = E->getType();
3939 unsigned HasBits = C.getTypeSize(OldType);
3940 if (HasBits >= Bits)
3941 return ExprResult(E);
3942 // OK to convert to signed, because new type has more bits than old.
3943 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3944 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3945 true);
3946}
3947
3948/// \brief Check if the given expression \a E is a constant integer that fits
3949/// into \a Bits bits.
3950static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3951 if (E == nullptr)
3952 return false;
3953 llvm::APSInt Result;
3954 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3955 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3956 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003957}
3958
3959/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003960/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3961/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003962static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003963CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3964 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3965 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003966 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003967 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003968 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003969 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003970 // Found 'collapse' clause - calculate collapse number.
3971 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003972 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003973 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003974 }
3975 if (OrderedLoopCountExpr) {
3976 // Found 'ordered' clause - calculate collapse number.
3977 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003978 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3979 if (Result.getLimitedValue() < NestedLoopCount) {
3980 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3981 diag::err_omp_wrong_ordered_loop_count)
3982 << OrderedLoopCountExpr->getSourceRange();
3983 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3984 diag::note_collapse_loop_count)
3985 << CollapseLoopCountExpr->getSourceRange();
3986 }
3987 NestedLoopCount = Result.getLimitedValue();
3988 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003989 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003990 // This is helper routine for loop directives (e.g., 'for', 'simd',
3991 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003992 SmallVector<LoopIterationSpace, 4> IterSpaces;
3993 IterSpaces.resize(NestedLoopCount);
3994 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003995 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003996 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003997 NestedLoopCount, CollapseLoopCountExpr,
3998 OrderedLoopCountExpr, VarsWithImplicitDSA,
3999 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004000 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004001 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004002 // OpenMP [2.8.1, simd construct, Restrictions]
4003 // All loops associated with the construct must be perfectly nested; that
4004 // is, there must be no intervening code nor any OpenMP directive between
4005 // any two loops.
4006 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004007 }
4008
Alexander Musmana5f070a2014-10-01 06:03:56 +00004009 Built.clear(/* size */ NestedLoopCount);
4010
4011 if (SemaRef.CurContext->isDependentContext())
4012 return NestedLoopCount;
4013
4014 // An example of what is generated for the following code:
4015 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004016 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004017 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004018 // for (k = 0; k < NK; ++k)
4019 // for (j = J0; j < NJ; j+=2) {
4020 // <loop body>
4021 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004022 //
4023 // We generate the code below.
4024 // Note: the loop body may be outlined in CodeGen.
4025 // Note: some counters may be C++ classes, operator- is used to find number of
4026 // iterations and operator+= to calculate counter value.
4027 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4028 // or i64 is currently supported).
4029 //
4030 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4031 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4032 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4033 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4034 // // similar updates for vars in clauses (e.g. 'linear')
4035 // <loop body (using local i and j)>
4036 // }
4037 // i = NI; // assign final values of counters
4038 // j = NJ;
4039 //
4040
4041 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4042 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004043 // Precondition tests if there is at least one iteration (all conditions are
4044 // true).
4045 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004046 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004047 ExprResult LastIteration32 = WidenIterationCount(
4048 32 /* Bits */, SemaRef.PerformImplicitConversion(
4049 N0->IgnoreImpCasts(), N0->getType(),
4050 Sema::AA_Converting, /*AllowExplicit=*/true)
4051 .get(),
4052 SemaRef);
4053 ExprResult LastIteration64 = WidenIterationCount(
4054 64 /* Bits */, SemaRef.PerformImplicitConversion(
4055 N0->IgnoreImpCasts(), N0->getType(),
4056 Sema::AA_Converting, /*AllowExplicit=*/true)
4057 .get(),
4058 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004059
4060 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4061 return NestedLoopCount;
4062
4063 auto &C = SemaRef.Context;
4064 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4065
4066 Scope *CurScope = DSA.getCurScope();
4067 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004068 if (PreCond.isUsable()) {
4069 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4070 PreCond.get(), IterSpaces[Cnt].PreCond);
4071 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004072 auto N = IterSpaces[Cnt].NumIterations;
4073 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4074 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004075 LastIteration32 = SemaRef.BuildBinOp(
4076 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4077 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4078 Sema::AA_Converting,
4079 /*AllowExplicit=*/true)
4080 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004081 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004082 LastIteration64 = SemaRef.BuildBinOp(
4083 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4084 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4085 Sema::AA_Converting,
4086 /*AllowExplicit=*/true)
4087 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004088 }
4089
4090 // Choose either the 32-bit or 64-bit version.
4091 ExprResult LastIteration = LastIteration64;
4092 if (LastIteration32.isUsable() &&
4093 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4094 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4095 FitsInto(
4096 32 /* Bits */,
4097 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4098 LastIteration64.get(), SemaRef)))
4099 LastIteration = LastIteration32;
4100
4101 if (!LastIteration.isUsable())
4102 return 0;
4103
4104 // Save the number of iterations.
4105 ExprResult NumIterations = LastIteration;
4106 {
4107 LastIteration = SemaRef.BuildBinOp(
4108 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4109 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4110 if (!LastIteration.isUsable())
4111 return 0;
4112 }
4113
4114 // Calculate the last iteration number beforehand instead of doing this on
4115 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4116 llvm::APSInt Result;
4117 bool IsConstant =
4118 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4119 ExprResult CalcLastIteration;
4120 if (!IsConstant) {
4121 SourceLocation SaveLoc;
4122 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004123 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004124 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004125 ExprResult SaveRef = buildDeclRefExpr(
4126 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004127 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4128 SaveRef.get(), LastIteration.get());
4129 LastIteration = SaveRef;
4130
4131 // Prepare SaveRef + 1.
4132 NumIterations = SemaRef.BuildBinOp(
4133 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4134 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4135 if (!NumIterations.isUsable())
4136 return 0;
4137 }
4138
4139 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4140
Alexander Musmanc6388682014-12-15 07:07:06 +00004141 QualType VType = LastIteration.get()->getType();
4142 // Build variables passed into runtime, nesessary for worksharing directives.
4143 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004144 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4145 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004146 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004147 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4148 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004149 SemaRef.AddInitializerToDecl(
4150 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4151 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4152
4153 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004154 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4155 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004156 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4157 /*DirectInit*/ false,
4158 /*TypeMayContainAuto*/ false);
4159
4160 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4161 // This will be used to implement clause 'lastprivate'.
4162 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004163 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4164 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004165 SemaRef.AddInitializerToDecl(
4166 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4167 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4168
4169 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004170 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4171 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004172 SemaRef.AddInitializerToDecl(
4173 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4174 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4175
4176 // Build expression: UB = min(UB, LastIteration)
4177 // It is nesessary for CodeGen of directives with static scheduling.
4178 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4179 UB.get(), LastIteration.get());
4180 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4181 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4182 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4183 CondOp.get());
4184 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4185 }
4186
4187 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004188 ExprResult IV;
4189 ExprResult Init;
4190 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004191 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4192 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004193 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004194 isOpenMPTaskLoopDirective(DKind) ||
4195 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004196 ? LB.get()
4197 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4198 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4199 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004200 }
4201
Alexander Musmanc6388682014-12-15 07:07:06 +00004202 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004203 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004204 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004205 (isOpenMPWorksharingDirective(DKind) ||
4206 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004207 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4208 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4209 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004210
4211 // Loop increment (IV = IV + 1)
4212 SourceLocation IncLoc;
4213 ExprResult Inc =
4214 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4215 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4216 if (!Inc.isUsable())
4217 return 0;
4218 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004219 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4220 if (!Inc.isUsable())
4221 return 0;
4222
4223 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4224 // Used for directives with static scheduling.
4225 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004226 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4227 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004228 // LB + ST
4229 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4230 if (!NextLB.isUsable())
4231 return 0;
4232 // LB = LB + ST
4233 NextLB =
4234 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4235 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4236 if (!NextLB.isUsable())
4237 return 0;
4238 // UB + ST
4239 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4240 if (!NextUB.isUsable())
4241 return 0;
4242 // UB = UB + ST
4243 NextUB =
4244 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4245 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4246 if (!NextUB.isUsable())
4247 return 0;
4248 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004249
4250 // Build updates and final values of the loop counters.
4251 bool HasErrors = false;
4252 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004253 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004254 Built.Updates.resize(NestedLoopCount);
4255 Built.Finals.resize(NestedLoopCount);
4256 {
4257 ExprResult Div;
4258 // Go from inner nested loop to outer.
4259 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4260 LoopIterationSpace &IS = IterSpaces[Cnt];
4261 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4262 // Build: Iter = (IV / Div) % IS.NumIters
4263 // where Div is product of previous iterations' IS.NumIters.
4264 ExprResult Iter;
4265 if (Div.isUsable()) {
4266 Iter =
4267 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4268 } else {
4269 Iter = IV;
4270 assert((Cnt == (int)NestedLoopCount - 1) &&
4271 "unusable div expected on first iteration only");
4272 }
4273
4274 if (Cnt != 0 && Iter.isUsable())
4275 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4276 IS.NumIterations);
4277 if (!Iter.isUsable()) {
4278 HasErrors = true;
4279 break;
4280 }
4281
Alexey Bataev39f915b82015-05-08 10:41:21 +00004282 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4283 auto *CounterVar = buildDeclRefExpr(
4284 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4285 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4286 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004287 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4288 IS.CounterInit);
4289 if (!Init.isUsable()) {
4290 HasErrors = true;
4291 break;
4292 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004293 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004294 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004295 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4296 if (!Update.isUsable()) {
4297 HasErrors = true;
4298 break;
4299 }
4300
4301 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4302 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004303 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004304 IS.NumIterations, IS.CounterStep, IS.Subtract);
4305 if (!Final.isUsable()) {
4306 HasErrors = true;
4307 break;
4308 }
4309
4310 // Build Div for the next iteration: Div <- Div * IS.NumIters
4311 if (Cnt != 0) {
4312 if (Div.isUnset())
4313 Div = IS.NumIterations;
4314 else
4315 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4316 IS.NumIterations);
4317
4318 // Add parentheses (for debugging purposes only).
4319 if (Div.isUsable())
4320 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4321 if (!Div.isUsable()) {
4322 HasErrors = true;
4323 break;
4324 }
4325 }
4326 if (!Update.isUsable() || !Final.isUsable()) {
4327 HasErrors = true;
4328 break;
4329 }
4330 // Save results
4331 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004332 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004333 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004334 Built.Updates[Cnt] = Update.get();
4335 Built.Finals[Cnt] = Final.get();
4336 }
4337 }
4338
4339 if (HasErrors)
4340 return 0;
4341
4342 // Save results
4343 Built.IterationVarRef = IV.get();
4344 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004345 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004346 Built.CalcLastIteration =
4347 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004348 Built.PreCond = PreCond.get();
4349 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004350 Built.Init = Init.get();
4351 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004352 Built.LB = LB.get();
4353 Built.UB = UB.get();
4354 Built.IL = IL.get();
4355 Built.ST = ST.get();
4356 Built.EUB = EUB.get();
4357 Built.NLB = NextLB.get();
4358 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004359
Alexey Bataevabfc0692014-06-25 06:52:00 +00004360 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004361}
4362
Alexey Bataev10e775f2015-07-30 11:36:16 +00004363static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004364 auto CollapseClauses =
4365 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4366 if (CollapseClauses.begin() != CollapseClauses.end())
4367 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004368 return nullptr;
4369}
4370
Alexey Bataev10e775f2015-07-30 11:36:16 +00004371static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004372 auto OrderedClauses =
4373 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4374 if (OrderedClauses.begin() != OrderedClauses.end())
4375 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004376 return nullptr;
4377}
4378
Alexey Bataev66b15b52015-08-21 11:14:16 +00004379static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4380 const Expr *Safelen) {
4381 llvm::APSInt SimdlenRes, SafelenRes;
4382 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4383 Simdlen->isInstantiationDependent() ||
4384 Simdlen->containsUnexpandedParameterPack())
4385 return false;
4386 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4387 Safelen->isInstantiationDependent() ||
4388 Safelen->containsUnexpandedParameterPack())
4389 return false;
4390 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4391 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4392 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4393 // If both simdlen and safelen clauses are specified, the value of the simdlen
4394 // parameter must be less than or equal to the value of the safelen parameter.
4395 if (SimdlenRes > SafelenRes) {
4396 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4397 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4398 return true;
4399 }
4400 return false;
4401}
4402
Alexey Bataev4acb8592014-07-07 13:01:15 +00004403StmtResult Sema::ActOnOpenMPSimdDirective(
4404 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4405 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004406 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004407 if (!AStmt)
4408 return StmtError();
4409
4410 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004411 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004412 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4413 // define the nested loops number.
4414 unsigned NestedLoopCount = CheckOpenMPLoop(
4415 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4416 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004417 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004418 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004419
Alexander Musmana5f070a2014-10-01 06:03:56 +00004420 assert((CurContext->isDependentContext() || B.builtAll()) &&
4421 "omp simd loop exprs were not built");
4422
Alexander Musman3276a272015-03-21 10:12:56 +00004423 if (!CurContext->isDependentContext()) {
4424 // Finalize the clauses that need pre-built expressions for CodeGen.
4425 for (auto C : Clauses) {
4426 if (auto LC = dyn_cast<OMPLinearClause>(C))
4427 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4428 B.NumIterations, *this, CurScope))
4429 return StmtError();
4430 }
4431 }
4432
Alexey Bataev66b15b52015-08-21 11:14:16 +00004433 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4434 // If both simdlen and safelen clauses are specified, the value of the simdlen
4435 // parameter must be less than or equal to the value of the safelen parameter.
4436 OMPSafelenClause *Safelen = nullptr;
4437 OMPSimdlenClause *Simdlen = nullptr;
4438 for (auto *Clause : Clauses) {
4439 if (Clause->getClauseKind() == OMPC_safelen)
4440 Safelen = cast<OMPSafelenClause>(Clause);
4441 else if (Clause->getClauseKind() == OMPC_simdlen)
4442 Simdlen = cast<OMPSimdlenClause>(Clause);
4443 if (Safelen && Simdlen)
4444 break;
4445 }
4446 if (Simdlen && Safelen &&
4447 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4448 Safelen->getSafelen()))
4449 return StmtError();
4450
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004451 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004452 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4453 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004454}
4455
Alexey Bataev4acb8592014-07-07 13:01:15 +00004456StmtResult Sema::ActOnOpenMPForDirective(
4457 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4458 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004459 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004460 if (!AStmt)
4461 return StmtError();
4462
4463 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004464 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004465 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4466 // define the nested loops number.
4467 unsigned NestedLoopCount = CheckOpenMPLoop(
4468 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4469 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004470 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004471 return StmtError();
4472
Alexander Musmana5f070a2014-10-01 06:03:56 +00004473 assert((CurContext->isDependentContext() || B.builtAll()) &&
4474 "omp for loop exprs were not built");
4475
Alexey Bataev54acd402015-08-04 11:18:19 +00004476 if (!CurContext->isDependentContext()) {
4477 // Finalize the clauses that need pre-built expressions for CodeGen.
4478 for (auto C : Clauses) {
4479 if (auto LC = dyn_cast<OMPLinearClause>(C))
4480 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4481 B.NumIterations, *this, CurScope))
4482 return StmtError();
4483 }
4484 }
4485
Alexey Bataevf29276e2014-06-18 04:14:57 +00004486 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004487 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004488 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004489}
4490
Alexander Musmanf82886e2014-09-18 05:12:34 +00004491StmtResult Sema::ActOnOpenMPForSimdDirective(
4492 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4493 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004494 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004495 if (!AStmt)
4496 return StmtError();
4497
4498 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004499 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004500 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4501 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004502 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004503 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4504 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4505 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004506 if (NestedLoopCount == 0)
4507 return StmtError();
4508
Alexander Musmanc6388682014-12-15 07:07:06 +00004509 assert((CurContext->isDependentContext() || B.builtAll()) &&
4510 "omp for simd loop exprs were not built");
4511
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004512 if (!CurContext->isDependentContext()) {
4513 // Finalize the clauses that need pre-built expressions for CodeGen.
4514 for (auto C : Clauses) {
4515 if (auto LC = dyn_cast<OMPLinearClause>(C))
4516 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4517 B.NumIterations, *this, CurScope))
4518 return StmtError();
4519 }
4520 }
4521
Alexey Bataev66b15b52015-08-21 11:14:16 +00004522 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4523 // If both simdlen and safelen clauses are specified, the value of the simdlen
4524 // parameter must be less than or equal to the value of the safelen parameter.
4525 OMPSafelenClause *Safelen = nullptr;
4526 OMPSimdlenClause *Simdlen = nullptr;
4527 for (auto *Clause : Clauses) {
4528 if (Clause->getClauseKind() == OMPC_safelen)
4529 Safelen = cast<OMPSafelenClause>(Clause);
4530 else if (Clause->getClauseKind() == OMPC_simdlen)
4531 Simdlen = cast<OMPSimdlenClause>(Clause);
4532 if (Safelen && Simdlen)
4533 break;
4534 }
4535 if (Simdlen && Safelen &&
4536 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4537 Safelen->getSafelen()))
4538 return StmtError();
4539
Alexander Musmanf82886e2014-09-18 05:12:34 +00004540 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004541 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4542 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004543}
4544
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004545StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4546 Stmt *AStmt,
4547 SourceLocation StartLoc,
4548 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004549 if (!AStmt)
4550 return StmtError();
4551
4552 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004553 auto BaseStmt = AStmt;
4554 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4555 BaseStmt = CS->getCapturedStmt();
4556 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4557 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004558 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004559 return StmtError();
4560 // All associated statements must be '#pragma omp section' except for
4561 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004562 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004563 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4564 if (SectionStmt)
4565 Diag(SectionStmt->getLocStart(),
4566 diag::err_omp_sections_substmt_not_section);
4567 return StmtError();
4568 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004569 cast<OMPSectionDirective>(SectionStmt)
4570 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004571 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004572 } else {
4573 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4574 return StmtError();
4575 }
4576
4577 getCurFunction()->setHasBranchProtectedScope();
4578
Alexey Bataev25e5b442015-09-15 12:52:43 +00004579 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4580 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004581}
4582
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004583StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4584 SourceLocation StartLoc,
4585 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004586 if (!AStmt)
4587 return StmtError();
4588
4589 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004590
4591 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004592 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004593
Alexey Bataev25e5b442015-09-15 12:52:43 +00004594 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4595 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004596}
4597
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004598StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4599 Stmt *AStmt,
4600 SourceLocation StartLoc,
4601 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004602 if (!AStmt)
4603 return StmtError();
4604
4605 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004606
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004607 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004608
Alexey Bataev3255bf32015-01-19 05:20:46 +00004609 // OpenMP [2.7.3, single Construct, Restrictions]
4610 // The copyprivate clause must not be used with the nowait clause.
4611 OMPClause *Nowait = nullptr;
4612 OMPClause *Copyprivate = nullptr;
4613 for (auto *Clause : Clauses) {
4614 if (Clause->getClauseKind() == OMPC_nowait)
4615 Nowait = Clause;
4616 else if (Clause->getClauseKind() == OMPC_copyprivate)
4617 Copyprivate = Clause;
4618 if (Copyprivate && Nowait) {
4619 Diag(Copyprivate->getLocStart(),
4620 diag::err_omp_single_copyprivate_with_nowait);
4621 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4622 return StmtError();
4623 }
4624 }
4625
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004626 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4627}
4628
Alexander Musman80c22892014-07-17 08:54:58 +00004629StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4630 SourceLocation StartLoc,
4631 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004632 if (!AStmt)
4633 return StmtError();
4634
4635 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004636
4637 getCurFunction()->setHasBranchProtectedScope();
4638
4639 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4640}
4641
Alexey Bataev28c75412015-12-15 08:19:24 +00004642StmtResult Sema::ActOnOpenMPCriticalDirective(
4643 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4644 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004645 if (!AStmt)
4646 return StmtError();
4647
4648 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004649
Alexey Bataev28c75412015-12-15 08:19:24 +00004650 bool ErrorFound = false;
4651 llvm::APSInt Hint;
4652 SourceLocation HintLoc;
4653 bool DependentHint = false;
4654 for (auto *C : Clauses) {
4655 if (C->getClauseKind() == OMPC_hint) {
4656 if (!DirName.getName()) {
4657 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4658 ErrorFound = true;
4659 }
4660 Expr *E = cast<OMPHintClause>(C)->getHint();
4661 if (E->isTypeDependent() || E->isValueDependent() ||
4662 E->isInstantiationDependent())
4663 DependentHint = true;
4664 else {
4665 Hint = E->EvaluateKnownConstInt(Context);
4666 HintLoc = C->getLocStart();
4667 }
4668 }
4669 }
4670 if (ErrorFound)
4671 return StmtError();
4672 auto Pair = DSAStack->getCriticalWithHint(DirName);
4673 if (Pair.first && DirName.getName() && !DependentHint) {
4674 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4675 Diag(StartLoc, diag::err_omp_critical_with_hint);
4676 if (HintLoc.isValid()) {
4677 Diag(HintLoc, diag::note_omp_critical_hint_here)
4678 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4679 } else
4680 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4681 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4682 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4683 << 1
4684 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4685 /*Radix=*/10, /*Signed=*/false);
4686 } else
4687 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4688 }
4689 }
4690
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004691 getCurFunction()->setHasBranchProtectedScope();
4692
Alexey Bataev28c75412015-12-15 08:19:24 +00004693 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4694 Clauses, AStmt);
4695 if (!Pair.first && DirName.getName() && !DependentHint)
4696 DSAStack->addCriticalWithHint(Dir, Hint);
4697 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004698}
4699
Alexey Bataev4acb8592014-07-07 13:01:15 +00004700StmtResult Sema::ActOnOpenMPParallelForDirective(
4701 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4702 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004703 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004704 if (!AStmt)
4705 return StmtError();
4706
Alexey Bataev4acb8592014-07-07 13:01:15 +00004707 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4708 // 1.2.2 OpenMP Language Terminology
4709 // Structured block - An executable statement with a single entry at the
4710 // top and a single exit at the bottom.
4711 // The point of exit cannot be a branch out of the structured block.
4712 // longjmp() and throw() must not violate the entry/exit criteria.
4713 CS->getCapturedDecl()->setNothrow();
4714
Alexander Musmanc6388682014-12-15 07:07:06 +00004715 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004716 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4717 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004718 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004719 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4720 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4721 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004722 if (NestedLoopCount == 0)
4723 return StmtError();
4724
Alexander Musmana5f070a2014-10-01 06:03:56 +00004725 assert((CurContext->isDependentContext() || B.builtAll()) &&
4726 "omp parallel for loop exprs were not built");
4727
Alexey Bataev54acd402015-08-04 11:18:19 +00004728 if (!CurContext->isDependentContext()) {
4729 // Finalize the clauses that need pre-built expressions for CodeGen.
4730 for (auto C : Clauses) {
4731 if (auto LC = dyn_cast<OMPLinearClause>(C))
4732 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4733 B.NumIterations, *this, CurScope))
4734 return StmtError();
4735 }
4736 }
4737
Alexey Bataev4acb8592014-07-07 13:01:15 +00004738 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004739 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004740 NestedLoopCount, Clauses, AStmt, B,
4741 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004742}
4743
Alexander Musmane4e893b2014-09-23 09:33:00 +00004744StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4745 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4746 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004747 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004748 if (!AStmt)
4749 return StmtError();
4750
Alexander Musmane4e893b2014-09-23 09:33:00 +00004751 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4752 // 1.2.2 OpenMP Language Terminology
4753 // Structured block - An executable statement with a single entry at the
4754 // top and a single exit at the bottom.
4755 // The point of exit cannot be a branch out of the structured block.
4756 // longjmp() and throw() must not violate the entry/exit criteria.
4757 CS->getCapturedDecl()->setNothrow();
4758
Alexander Musmanc6388682014-12-15 07:07:06 +00004759 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004760 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4761 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004762 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004763 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4764 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4765 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004766 if (NestedLoopCount == 0)
4767 return StmtError();
4768
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004769 if (!CurContext->isDependentContext()) {
4770 // Finalize the clauses that need pre-built expressions for CodeGen.
4771 for (auto C : Clauses) {
4772 if (auto LC = dyn_cast<OMPLinearClause>(C))
4773 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4774 B.NumIterations, *this, CurScope))
4775 return StmtError();
4776 }
4777 }
4778
Alexey Bataev66b15b52015-08-21 11:14:16 +00004779 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4780 // If both simdlen and safelen clauses are specified, the value of the simdlen
4781 // parameter must be less than or equal to the value of the safelen parameter.
4782 OMPSafelenClause *Safelen = nullptr;
4783 OMPSimdlenClause *Simdlen = nullptr;
4784 for (auto *Clause : Clauses) {
4785 if (Clause->getClauseKind() == OMPC_safelen)
4786 Safelen = cast<OMPSafelenClause>(Clause);
4787 else if (Clause->getClauseKind() == OMPC_simdlen)
4788 Simdlen = cast<OMPSimdlenClause>(Clause);
4789 if (Safelen && Simdlen)
4790 break;
4791 }
4792 if (Simdlen && Safelen &&
4793 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4794 Safelen->getSafelen()))
4795 return StmtError();
4796
Alexander Musmane4e893b2014-09-23 09:33:00 +00004797 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004798 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004799 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004800}
4801
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004802StmtResult
4803Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4804 Stmt *AStmt, SourceLocation StartLoc,
4805 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004806 if (!AStmt)
4807 return StmtError();
4808
4809 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004810 auto BaseStmt = AStmt;
4811 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4812 BaseStmt = CS->getCapturedStmt();
4813 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4814 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004815 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004816 return StmtError();
4817 // All associated statements must be '#pragma omp section' except for
4818 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004819 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004820 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4821 if (SectionStmt)
4822 Diag(SectionStmt->getLocStart(),
4823 diag::err_omp_parallel_sections_substmt_not_section);
4824 return StmtError();
4825 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004826 cast<OMPSectionDirective>(SectionStmt)
4827 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004828 }
4829 } else {
4830 Diag(AStmt->getLocStart(),
4831 diag::err_omp_parallel_sections_not_compound_stmt);
4832 return StmtError();
4833 }
4834
4835 getCurFunction()->setHasBranchProtectedScope();
4836
Alexey Bataev25e5b442015-09-15 12:52:43 +00004837 return OMPParallelSectionsDirective::Create(
4838 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004839}
4840
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004841StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4842 Stmt *AStmt, SourceLocation StartLoc,
4843 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004844 if (!AStmt)
4845 return StmtError();
4846
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004847 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4848 // 1.2.2 OpenMP Language Terminology
4849 // Structured block - An executable statement with a single entry at the
4850 // top and a single exit at the bottom.
4851 // The point of exit cannot be a branch out of the structured block.
4852 // longjmp() and throw() must not violate the entry/exit criteria.
4853 CS->getCapturedDecl()->setNothrow();
4854
4855 getCurFunction()->setHasBranchProtectedScope();
4856
Alexey Bataev25e5b442015-09-15 12:52:43 +00004857 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4858 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004859}
4860
Alexey Bataev68446b72014-07-18 07:47:19 +00004861StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4862 SourceLocation EndLoc) {
4863 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4864}
4865
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004866StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4867 SourceLocation EndLoc) {
4868 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4869}
4870
Alexey Bataev2df347a2014-07-18 10:17:07 +00004871StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4872 SourceLocation EndLoc) {
4873 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4874}
4875
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004876StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4877 SourceLocation StartLoc,
4878 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004879 if (!AStmt)
4880 return StmtError();
4881
4882 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004883
4884 getCurFunction()->setHasBranchProtectedScope();
4885
4886 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4887}
4888
Alexey Bataev6125da92014-07-21 11:26:11 +00004889StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4890 SourceLocation StartLoc,
4891 SourceLocation EndLoc) {
4892 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4893 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4894}
4895
Alexey Bataev346265e2015-09-25 10:37:12 +00004896StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4897 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004898 SourceLocation StartLoc,
4899 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004900 OMPClause *DependFound = nullptr;
4901 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004902 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004903 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004904 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004905 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004906 for (auto *C : Clauses) {
4907 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4908 DependFound = C;
4909 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4910 if (DependSourceClause) {
4911 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4912 << getOpenMPDirectiveName(OMPD_ordered)
4913 << getOpenMPClauseName(OMPC_depend) << 2;
4914 ErrorFound = true;
4915 } else
4916 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004917 if (DependSinkClause) {
4918 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4919 << 0;
4920 ErrorFound = true;
4921 }
4922 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4923 if (DependSourceClause) {
4924 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4925 << 1;
4926 ErrorFound = true;
4927 }
4928 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004929 }
4930 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004931 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004932 else if (C->getClauseKind() == OMPC_simd)
4933 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004934 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004935 if (!ErrorFound && !SC &&
4936 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004937 // OpenMP [2.8.1,simd Construct, Restrictions]
4938 // An ordered construct with the simd clause is the only OpenMP construct
4939 // that can appear in the simd region.
4940 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004941 ErrorFound = true;
4942 } else if (DependFound && (TC || SC)) {
4943 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4944 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4945 ErrorFound = true;
4946 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4947 Diag(DependFound->getLocStart(),
4948 diag::err_omp_ordered_directive_without_param);
4949 ErrorFound = true;
4950 } else if (TC || Clauses.empty()) {
4951 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4952 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4953 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4954 << (TC != nullptr);
4955 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4956 ErrorFound = true;
4957 }
4958 }
4959 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004960 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004961
4962 if (AStmt) {
4963 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4964
4965 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004966 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004967
4968 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004969}
4970
Alexey Bataev1d160b12015-03-13 12:27:31 +00004971namespace {
4972/// \brief Helper class for checking expression in 'omp atomic [update]'
4973/// construct.
4974class OpenMPAtomicUpdateChecker {
4975 /// \brief Error results for atomic update expressions.
4976 enum ExprAnalysisErrorCode {
4977 /// \brief A statement is not an expression statement.
4978 NotAnExpression,
4979 /// \brief Expression is not builtin binary or unary operation.
4980 NotABinaryOrUnaryExpression,
4981 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4982 NotAnUnaryIncDecExpression,
4983 /// \brief An expression is not of scalar type.
4984 NotAScalarType,
4985 /// \brief A binary operation is not an assignment operation.
4986 NotAnAssignmentOp,
4987 /// \brief RHS part of the binary operation is not a binary expression.
4988 NotABinaryExpression,
4989 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4990 /// expression.
4991 NotABinaryOperator,
4992 /// \brief RHS binary operation does not have reference to the updated LHS
4993 /// part.
4994 NotAnUpdateExpression,
4995 /// \brief No errors is found.
4996 NoError
4997 };
4998 /// \brief Reference to Sema.
4999 Sema &SemaRef;
5000 /// \brief A location for note diagnostics (when error is found).
5001 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005002 /// \brief 'x' lvalue part of the source atomic expression.
5003 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005004 /// \brief 'expr' rvalue part of the source atomic expression.
5005 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005006 /// \brief Helper expression of the form
5007 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5008 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5009 Expr *UpdateExpr;
5010 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5011 /// important for non-associative operations.
5012 bool IsXLHSInRHSPart;
5013 BinaryOperatorKind Op;
5014 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005015 /// \brief true if the source expression is a postfix unary operation, false
5016 /// if it is a prefix unary operation.
5017 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005018
5019public:
5020 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005021 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005022 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005023 /// \brief Check specified statement that it is suitable for 'atomic update'
5024 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005025 /// expression. If DiagId and NoteId == 0, then only check is performed
5026 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005027 /// \param DiagId Diagnostic which should be emitted if error is found.
5028 /// \param NoteId Diagnostic note for the main error message.
5029 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005030 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005031 /// \brief Return the 'x' lvalue part of the source atomic expression.
5032 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005033 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5034 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005035 /// \brief Return the update expression used in calculation of the updated
5036 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5037 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5038 Expr *getUpdateExpr() const { return UpdateExpr; }
5039 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5040 /// false otherwise.
5041 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5042
Alexey Bataevb78ca832015-04-01 03:33:17 +00005043 /// \brief true if the source expression is a postfix unary operation, false
5044 /// if it is a prefix unary operation.
5045 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5046
Alexey Bataev1d160b12015-03-13 12:27:31 +00005047private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005048 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5049 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005050};
5051} // namespace
5052
5053bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5054 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5055 ExprAnalysisErrorCode ErrorFound = NoError;
5056 SourceLocation ErrorLoc, NoteLoc;
5057 SourceRange ErrorRange, NoteRange;
5058 // Allowed constructs are:
5059 // x = x binop expr;
5060 // x = expr binop x;
5061 if (AtomicBinOp->getOpcode() == BO_Assign) {
5062 X = AtomicBinOp->getLHS();
5063 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5064 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5065 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5066 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5067 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005068 Op = AtomicInnerBinOp->getOpcode();
5069 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005070 auto *LHS = AtomicInnerBinOp->getLHS();
5071 auto *RHS = AtomicInnerBinOp->getRHS();
5072 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5073 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5074 /*Canonical=*/true);
5075 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5076 /*Canonical=*/true);
5077 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5078 /*Canonical=*/true);
5079 if (XId == LHSId) {
5080 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005081 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005082 } else if (XId == RHSId) {
5083 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005084 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005085 } else {
5086 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5087 ErrorRange = AtomicInnerBinOp->getSourceRange();
5088 NoteLoc = X->getExprLoc();
5089 NoteRange = X->getSourceRange();
5090 ErrorFound = NotAnUpdateExpression;
5091 }
5092 } else {
5093 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5094 ErrorRange = AtomicInnerBinOp->getSourceRange();
5095 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5096 NoteRange = SourceRange(NoteLoc, NoteLoc);
5097 ErrorFound = NotABinaryOperator;
5098 }
5099 } else {
5100 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5101 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5102 ErrorFound = NotABinaryExpression;
5103 }
5104 } else {
5105 ErrorLoc = AtomicBinOp->getExprLoc();
5106 ErrorRange = AtomicBinOp->getSourceRange();
5107 NoteLoc = AtomicBinOp->getOperatorLoc();
5108 NoteRange = SourceRange(NoteLoc, NoteLoc);
5109 ErrorFound = NotAnAssignmentOp;
5110 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005111 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005112 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5113 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5114 return true;
5115 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005116 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005117 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005118}
5119
5120bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5121 unsigned NoteId) {
5122 ExprAnalysisErrorCode ErrorFound = NoError;
5123 SourceLocation ErrorLoc, NoteLoc;
5124 SourceRange ErrorRange, NoteRange;
5125 // Allowed constructs are:
5126 // x++;
5127 // x--;
5128 // ++x;
5129 // --x;
5130 // x binop= expr;
5131 // x = x binop expr;
5132 // x = expr binop x;
5133 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5134 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5135 if (AtomicBody->getType()->isScalarType() ||
5136 AtomicBody->isInstantiationDependent()) {
5137 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5138 AtomicBody->IgnoreParenImpCasts())) {
5139 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005140 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005141 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005142 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005143 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005144 X = AtomicCompAssignOp->getLHS();
5145 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005146 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5147 AtomicBody->IgnoreParenImpCasts())) {
5148 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005149 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5150 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005151 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005152 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5153 // Check for Unary Operation
5154 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005155 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005156 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5157 OpLoc = AtomicUnaryOp->getOperatorLoc();
5158 X = AtomicUnaryOp->getSubExpr();
5159 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5160 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005161 } else {
5162 ErrorFound = NotAnUnaryIncDecExpression;
5163 ErrorLoc = AtomicUnaryOp->getExprLoc();
5164 ErrorRange = AtomicUnaryOp->getSourceRange();
5165 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5166 NoteRange = SourceRange(NoteLoc, NoteLoc);
5167 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005168 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005169 ErrorFound = NotABinaryOrUnaryExpression;
5170 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5171 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5172 }
5173 } else {
5174 ErrorFound = NotAScalarType;
5175 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5176 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5177 }
5178 } else {
5179 ErrorFound = NotAnExpression;
5180 NoteLoc = ErrorLoc = S->getLocStart();
5181 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5182 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005183 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005184 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5185 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5186 return true;
5187 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005188 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005189 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005190 // Build an update expression of form 'OpaqueValueExpr(x) binop
5191 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5192 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5193 auto *OVEX = new (SemaRef.getASTContext())
5194 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5195 auto *OVEExpr = new (SemaRef.getASTContext())
5196 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5197 auto Update =
5198 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5199 IsXLHSInRHSPart ? OVEExpr : OVEX);
5200 if (Update.isInvalid())
5201 return true;
5202 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5203 Sema::AA_Casting);
5204 if (Update.isInvalid())
5205 return true;
5206 UpdateExpr = Update.get();
5207 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005208 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005209}
5210
Alexey Bataev0162e452014-07-22 10:10:35 +00005211StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5212 Stmt *AStmt,
5213 SourceLocation StartLoc,
5214 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005215 if (!AStmt)
5216 return StmtError();
5217
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005218 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005219 // 1.2.2 OpenMP Language Terminology
5220 // Structured block - An executable statement with a single entry at the
5221 // top and a single exit at the bottom.
5222 // The point of exit cannot be a branch out of the structured block.
5223 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005224 OpenMPClauseKind AtomicKind = OMPC_unknown;
5225 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005226 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005227 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005228 C->getClauseKind() == OMPC_update ||
5229 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005230 if (AtomicKind != OMPC_unknown) {
5231 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5232 << SourceRange(C->getLocStart(), C->getLocEnd());
5233 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5234 << getOpenMPClauseName(AtomicKind);
5235 } else {
5236 AtomicKind = C->getClauseKind();
5237 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005238 }
5239 }
5240 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005241
Alexey Bataev459dec02014-07-24 06:46:57 +00005242 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005243 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5244 Body = EWC->getSubExpr();
5245
Alexey Bataev62cec442014-11-18 10:14:22 +00005246 Expr *X = nullptr;
5247 Expr *V = nullptr;
5248 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005249 Expr *UE = nullptr;
5250 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005251 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005252 // OpenMP [2.12.6, atomic Construct]
5253 // In the next expressions:
5254 // * x and v (as applicable) are both l-value expressions with scalar type.
5255 // * During the execution of an atomic region, multiple syntactic
5256 // occurrences of x must designate the same storage location.
5257 // * Neither of v and expr (as applicable) may access the storage location
5258 // designated by x.
5259 // * Neither of x and expr (as applicable) may access the storage location
5260 // designated by v.
5261 // * expr is an expression with scalar type.
5262 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5263 // * binop, binop=, ++, and -- are not overloaded operators.
5264 // * The expression x binop expr must be numerically equivalent to x binop
5265 // (expr). This requirement is satisfied if the operators in expr have
5266 // precedence greater than binop, or by using parentheses around expr or
5267 // subexpressions of expr.
5268 // * The expression expr binop x must be numerically equivalent to (expr)
5269 // binop x. This requirement is satisfied if the operators in expr have
5270 // precedence equal to or greater than binop, or by using parentheses around
5271 // expr or subexpressions of expr.
5272 // * For forms that allow multiple occurrences of x, the number of times
5273 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005274 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005275 enum {
5276 NotAnExpression,
5277 NotAnAssignmentOp,
5278 NotAScalarType,
5279 NotAnLValue,
5280 NoError
5281 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005282 SourceLocation ErrorLoc, NoteLoc;
5283 SourceRange ErrorRange, NoteRange;
5284 // If clause is read:
5285 // v = x;
5286 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5287 auto AtomicBinOp =
5288 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5289 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5290 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5291 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5292 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5293 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5294 if (!X->isLValue() || !V->isLValue()) {
5295 auto NotLValueExpr = X->isLValue() ? V : X;
5296 ErrorFound = NotAnLValue;
5297 ErrorLoc = AtomicBinOp->getExprLoc();
5298 ErrorRange = AtomicBinOp->getSourceRange();
5299 NoteLoc = NotLValueExpr->getExprLoc();
5300 NoteRange = NotLValueExpr->getSourceRange();
5301 }
5302 } else if (!X->isInstantiationDependent() ||
5303 !V->isInstantiationDependent()) {
5304 auto NotScalarExpr =
5305 (X->isInstantiationDependent() || X->getType()->isScalarType())
5306 ? V
5307 : X;
5308 ErrorFound = NotAScalarType;
5309 ErrorLoc = AtomicBinOp->getExprLoc();
5310 ErrorRange = AtomicBinOp->getSourceRange();
5311 NoteLoc = NotScalarExpr->getExprLoc();
5312 NoteRange = NotScalarExpr->getSourceRange();
5313 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005314 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005315 ErrorFound = NotAnAssignmentOp;
5316 ErrorLoc = AtomicBody->getExprLoc();
5317 ErrorRange = AtomicBody->getSourceRange();
5318 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5319 : AtomicBody->getExprLoc();
5320 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5321 : AtomicBody->getSourceRange();
5322 }
5323 } else {
5324 ErrorFound = NotAnExpression;
5325 NoteLoc = ErrorLoc = Body->getLocStart();
5326 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005327 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005328 if (ErrorFound != NoError) {
5329 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5330 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005331 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5332 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005333 return StmtError();
5334 } else if (CurContext->isDependentContext())
5335 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005336 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005337 enum {
5338 NotAnExpression,
5339 NotAnAssignmentOp,
5340 NotAScalarType,
5341 NotAnLValue,
5342 NoError
5343 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005344 SourceLocation ErrorLoc, NoteLoc;
5345 SourceRange ErrorRange, NoteRange;
5346 // If clause is write:
5347 // x = expr;
5348 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5349 auto AtomicBinOp =
5350 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5351 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005352 X = AtomicBinOp->getLHS();
5353 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005354 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5355 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5356 if (!X->isLValue()) {
5357 ErrorFound = NotAnLValue;
5358 ErrorLoc = AtomicBinOp->getExprLoc();
5359 ErrorRange = AtomicBinOp->getSourceRange();
5360 NoteLoc = X->getExprLoc();
5361 NoteRange = X->getSourceRange();
5362 }
5363 } else if (!X->isInstantiationDependent() ||
5364 !E->isInstantiationDependent()) {
5365 auto NotScalarExpr =
5366 (X->isInstantiationDependent() || X->getType()->isScalarType())
5367 ? E
5368 : X;
5369 ErrorFound = NotAScalarType;
5370 ErrorLoc = AtomicBinOp->getExprLoc();
5371 ErrorRange = AtomicBinOp->getSourceRange();
5372 NoteLoc = NotScalarExpr->getExprLoc();
5373 NoteRange = NotScalarExpr->getSourceRange();
5374 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005375 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005376 ErrorFound = NotAnAssignmentOp;
5377 ErrorLoc = AtomicBody->getExprLoc();
5378 ErrorRange = AtomicBody->getSourceRange();
5379 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5380 : AtomicBody->getExprLoc();
5381 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5382 : AtomicBody->getSourceRange();
5383 }
5384 } else {
5385 ErrorFound = NotAnExpression;
5386 NoteLoc = ErrorLoc = Body->getLocStart();
5387 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005388 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005389 if (ErrorFound != NoError) {
5390 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5391 << ErrorRange;
5392 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5393 << NoteRange;
5394 return StmtError();
5395 } else if (CurContext->isDependentContext())
5396 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005397 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005398 // If clause is update:
5399 // x++;
5400 // x--;
5401 // ++x;
5402 // --x;
5403 // x binop= expr;
5404 // x = x binop expr;
5405 // x = expr binop x;
5406 OpenMPAtomicUpdateChecker Checker(*this);
5407 if (Checker.checkStatement(
5408 Body, (AtomicKind == OMPC_update)
5409 ? diag::err_omp_atomic_update_not_expression_statement
5410 : diag::err_omp_atomic_not_expression_statement,
5411 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005412 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005413 if (!CurContext->isDependentContext()) {
5414 E = Checker.getExpr();
5415 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005416 UE = Checker.getUpdateExpr();
5417 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005418 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005419 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005420 enum {
5421 NotAnAssignmentOp,
5422 NotACompoundStatement,
5423 NotTwoSubstatements,
5424 NotASpecificExpression,
5425 NoError
5426 } ErrorFound = NoError;
5427 SourceLocation ErrorLoc, NoteLoc;
5428 SourceRange ErrorRange, NoteRange;
5429 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5430 // If clause is a capture:
5431 // v = x++;
5432 // v = x--;
5433 // v = ++x;
5434 // v = --x;
5435 // v = x binop= expr;
5436 // v = x = x binop expr;
5437 // v = x = expr binop x;
5438 auto *AtomicBinOp =
5439 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5440 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5441 V = AtomicBinOp->getLHS();
5442 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5443 OpenMPAtomicUpdateChecker Checker(*this);
5444 if (Checker.checkStatement(
5445 Body, diag::err_omp_atomic_capture_not_expression_statement,
5446 diag::note_omp_atomic_update))
5447 return StmtError();
5448 E = Checker.getExpr();
5449 X = Checker.getX();
5450 UE = Checker.getUpdateExpr();
5451 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5452 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005453 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005454 ErrorLoc = AtomicBody->getExprLoc();
5455 ErrorRange = AtomicBody->getSourceRange();
5456 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5457 : AtomicBody->getExprLoc();
5458 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5459 : AtomicBody->getSourceRange();
5460 ErrorFound = NotAnAssignmentOp;
5461 }
5462 if (ErrorFound != NoError) {
5463 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5464 << ErrorRange;
5465 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5466 return StmtError();
5467 } else if (CurContext->isDependentContext()) {
5468 UE = V = E = X = nullptr;
5469 }
5470 } else {
5471 // If clause is a capture:
5472 // { v = x; x = expr; }
5473 // { v = x; x++; }
5474 // { v = x; x--; }
5475 // { v = x; ++x; }
5476 // { v = x; --x; }
5477 // { v = x; x binop= expr; }
5478 // { v = x; x = x binop expr; }
5479 // { v = x; x = expr binop x; }
5480 // { x++; v = x; }
5481 // { x--; v = x; }
5482 // { ++x; v = x; }
5483 // { --x; v = x; }
5484 // { x binop= expr; v = x; }
5485 // { x = x binop expr; v = x; }
5486 // { x = expr binop x; v = x; }
5487 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5488 // Check that this is { expr1; expr2; }
5489 if (CS->size() == 2) {
5490 auto *First = CS->body_front();
5491 auto *Second = CS->body_back();
5492 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5493 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5494 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5495 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5496 // Need to find what subexpression is 'v' and what is 'x'.
5497 OpenMPAtomicUpdateChecker Checker(*this);
5498 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5499 BinaryOperator *BinOp = nullptr;
5500 if (IsUpdateExprFound) {
5501 BinOp = dyn_cast<BinaryOperator>(First);
5502 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5503 }
5504 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5505 // { v = x; x++; }
5506 // { v = x; x--; }
5507 // { v = x; ++x; }
5508 // { v = x; --x; }
5509 // { v = x; x binop= expr; }
5510 // { v = x; x = x binop expr; }
5511 // { v = x; x = expr binop x; }
5512 // Check that the first expression has form v = x.
5513 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5514 llvm::FoldingSetNodeID XId, PossibleXId;
5515 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5516 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5517 IsUpdateExprFound = XId == PossibleXId;
5518 if (IsUpdateExprFound) {
5519 V = BinOp->getLHS();
5520 X = Checker.getX();
5521 E = Checker.getExpr();
5522 UE = Checker.getUpdateExpr();
5523 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005524 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005525 }
5526 }
5527 if (!IsUpdateExprFound) {
5528 IsUpdateExprFound = !Checker.checkStatement(First);
5529 BinOp = nullptr;
5530 if (IsUpdateExprFound) {
5531 BinOp = dyn_cast<BinaryOperator>(Second);
5532 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5533 }
5534 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5535 // { x++; v = x; }
5536 // { x--; v = x; }
5537 // { ++x; v = x; }
5538 // { --x; v = x; }
5539 // { x binop= expr; v = x; }
5540 // { x = x binop expr; v = x; }
5541 // { x = expr binop x; v = x; }
5542 // Check that the second expression has form v = x.
5543 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5544 llvm::FoldingSetNodeID XId, PossibleXId;
5545 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5546 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5547 IsUpdateExprFound = XId == PossibleXId;
5548 if (IsUpdateExprFound) {
5549 V = BinOp->getLHS();
5550 X = Checker.getX();
5551 E = Checker.getExpr();
5552 UE = Checker.getUpdateExpr();
5553 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005554 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005555 }
5556 }
5557 }
5558 if (!IsUpdateExprFound) {
5559 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005560 auto *FirstExpr = dyn_cast<Expr>(First);
5561 auto *SecondExpr = dyn_cast<Expr>(Second);
5562 if (!FirstExpr || !SecondExpr ||
5563 !(FirstExpr->isInstantiationDependent() ||
5564 SecondExpr->isInstantiationDependent())) {
5565 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5566 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005567 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005568 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5569 : First->getLocStart();
5570 NoteRange = ErrorRange = FirstBinOp
5571 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005572 : SourceRange(ErrorLoc, ErrorLoc);
5573 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005574 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5575 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5576 ErrorFound = NotAnAssignmentOp;
5577 NoteLoc = ErrorLoc = SecondBinOp
5578 ? SecondBinOp->getOperatorLoc()
5579 : Second->getLocStart();
5580 NoteRange = ErrorRange =
5581 SecondBinOp ? SecondBinOp->getSourceRange()
5582 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005583 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005584 auto *PossibleXRHSInFirst =
5585 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5586 auto *PossibleXLHSInSecond =
5587 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5588 llvm::FoldingSetNodeID X1Id, X2Id;
5589 PossibleXRHSInFirst->Profile(X1Id, Context,
5590 /*Canonical=*/true);
5591 PossibleXLHSInSecond->Profile(X2Id, Context,
5592 /*Canonical=*/true);
5593 IsUpdateExprFound = X1Id == X2Id;
5594 if (IsUpdateExprFound) {
5595 V = FirstBinOp->getLHS();
5596 X = SecondBinOp->getLHS();
5597 E = SecondBinOp->getRHS();
5598 UE = nullptr;
5599 IsXLHSInRHSPart = false;
5600 IsPostfixUpdate = true;
5601 } else {
5602 ErrorFound = NotASpecificExpression;
5603 ErrorLoc = FirstBinOp->getExprLoc();
5604 ErrorRange = FirstBinOp->getSourceRange();
5605 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5606 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5607 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005608 }
5609 }
5610 }
5611 }
5612 } else {
5613 NoteLoc = ErrorLoc = Body->getLocStart();
5614 NoteRange = ErrorRange =
5615 SourceRange(Body->getLocStart(), Body->getLocStart());
5616 ErrorFound = NotTwoSubstatements;
5617 }
5618 } else {
5619 NoteLoc = ErrorLoc = Body->getLocStart();
5620 NoteRange = ErrorRange =
5621 SourceRange(Body->getLocStart(), Body->getLocStart());
5622 ErrorFound = NotACompoundStatement;
5623 }
5624 if (ErrorFound != NoError) {
5625 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5626 << ErrorRange;
5627 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5628 return StmtError();
5629 } else if (CurContext->isDependentContext()) {
5630 UE = V = E = X = nullptr;
5631 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005632 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005633 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005634
5635 getCurFunction()->setHasBranchProtectedScope();
5636
Alexey Bataev62cec442014-11-18 10:14:22 +00005637 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005638 X, V, E, UE, IsXLHSInRHSPart,
5639 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005640}
5641
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005642StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5643 Stmt *AStmt,
5644 SourceLocation StartLoc,
5645 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005646 if (!AStmt)
5647 return StmtError();
5648
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005649 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5650 // 1.2.2 OpenMP Language Terminology
5651 // Structured block - An executable statement with a single entry at the
5652 // top and a single exit at the bottom.
5653 // The point of exit cannot be a branch out of the structured block.
5654 // longjmp() and throw() must not violate the entry/exit criteria.
5655 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005656
Alexey Bataev13314bf2014-10-09 04:18:56 +00005657 // OpenMP [2.16, Nesting of Regions]
5658 // If specified, a teams construct must be contained within a target
5659 // construct. That target construct must contain no statements or directives
5660 // outside of the teams construct.
5661 if (DSAStack->hasInnerTeamsRegion()) {
5662 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5663 bool OMPTeamsFound = true;
5664 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5665 auto I = CS->body_begin();
5666 while (I != CS->body_end()) {
5667 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5668 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5669 OMPTeamsFound = false;
5670 break;
5671 }
5672 ++I;
5673 }
5674 assert(I != CS->body_end() && "Not found statement");
5675 S = *I;
5676 }
5677 if (!OMPTeamsFound) {
5678 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5679 Diag(DSAStack->getInnerTeamsRegionLoc(),
5680 diag::note_omp_nested_teams_construct_here);
5681 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5682 << isa<OMPExecutableDirective>(S);
5683 return StmtError();
5684 }
5685 }
5686
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005687 getCurFunction()->setHasBranchProtectedScope();
5688
5689 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5690}
5691
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005692StmtResult
5693Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5694 Stmt *AStmt, SourceLocation StartLoc,
5695 SourceLocation EndLoc) {
5696 if (!AStmt)
5697 return StmtError();
5698
5699 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5700 // 1.2.2 OpenMP Language Terminology
5701 // Structured block - An executable statement with a single entry at the
5702 // top and a single exit at the bottom.
5703 // The point of exit cannot be a branch out of the structured block.
5704 // longjmp() and throw() must not violate the entry/exit criteria.
5705 CS->getCapturedDecl()->setNothrow();
5706
5707 getCurFunction()->setHasBranchProtectedScope();
5708
5709 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5710 AStmt);
5711}
5712
Samuel Antaodf67fc42016-01-19 19:15:56 +00005713/// \brief Check for existence of a map clause in the list of clauses.
5714static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5715 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5716 I != E; ++I) {
5717 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5718 return true;
5719 }
5720 }
5721
5722 return false;
5723}
5724
Michael Wong65f367f2015-07-21 13:44:28 +00005725StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5726 Stmt *AStmt,
5727 SourceLocation StartLoc,
5728 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005729 if (!AStmt)
5730 return StmtError();
5731
5732 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5733
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005734 // OpenMP [2.10.1, Restrictions, p. 97]
5735 // At least one map clause must appear on the directive.
5736 if (!HasMapClause(Clauses)) {
5737 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5738 getOpenMPDirectiveName(OMPD_target_data);
5739 return StmtError();
5740 }
5741
Michael Wong65f367f2015-07-21 13:44:28 +00005742 getCurFunction()->setHasBranchProtectedScope();
5743
5744 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5745 AStmt);
5746}
5747
Samuel Antaodf67fc42016-01-19 19:15:56 +00005748StmtResult
5749Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5750 SourceLocation StartLoc,
5751 SourceLocation EndLoc) {
5752 // OpenMP [2.10.2, Restrictions, p. 99]
5753 // At least one map clause must appear on the directive.
5754 if (!HasMapClause(Clauses)) {
5755 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5756 << getOpenMPDirectiveName(OMPD_target_enter_data);
5757 return StmtError();
5758 }
5759
5760 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5761 Clauses);
5762}
5763
Samuel Antao72590762016-01-19 20:04:50 +00005764StmtResult
5765Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5766 SourceLocation StartLoc,
5767 SourceLocation EndLoc) {
5768 // OpenMP [2.10.3, Restrictions, p. 102]
5769 // At least one map clause must appear on the directive.
5770 if (!HasMapClause(Clauses)) {
5771 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5772 << getOpenMPDirectiveName(OMPD_target_exit_data);
5773 return StmtError();
5774 }
5775
5776 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5777}
5778
Alexey Bataev13314bf2014-10-09 04:18:56 +00005779StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5780 Stmt *AStmt, SourceLocation StartLoc,
5781 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005782 if (!AStmt)
5783 return StmtError();
5784
Alexey Bataev13314bf2014-10-09 04:18:56 +00005785 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5786 // 1.2.2 OpenMP Language Terminology
5787 // Structured block - An executable statement with a single entry at the
5788 // top and a single exit at the bottom.
5789 // The point of exit cannot be a branch out of the structured block.
5790 // longjmp() and throw() must not violate the entry/exit criteria.
5791 CS->getCapturedDecl()->setNothrow();
5792
5793 getCurFunction()->setHasBranchProtectedScope();
5794
5795 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5796}
5797
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005798StmtResult
5799Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5800 SourceLocation EndLoc,
5801 OpenMPDirectiveKind CancelRegion) {
5802 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5803 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5804 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5805 << getOpenMPDirectiveName(CancelRegion);
5806 return StmtError();
5807 }
5808 if (DSAStack->isParentNowaitRegion()) {
5809 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5810 return StmtError();
5811 }
5812 if (DSAStack->isParentOrderedRegion()) {
5813 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5814 return StmtError();
5815 }
5816 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5817 CancelRegion);
5818}
5819
Alexey Bataev87933c72015-09-18 08:07:34 +00005820StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5821 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005822 SourceLocation EndLoc,
5823 OpenMPDirectiveKind CancelRegion) {
5824 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5825 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5826 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5827 << getOpenMPDirectiveName(CancelRegion);
5828 return StmtError();
5829 }
5830 if (DSAStack->isParentNowaitRegion()) {
5831 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5832 return StmtError();
5833 }
5834 if (DSAStack->isParentOrderedRegion()) {
5835 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5836 return StmtError();
5837 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005838 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005839 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5840 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005841}
5842
Alexey Bataev382967a2015-12-08 12:06:20 +00005843static bool checkGrainsizeNumTasksClauses(Sema &S,
5844 ArrayRef<OMPClause *> Clauses) {
5845 OMPClause *PrevClause = nullptr;
5846 bool ErrorFound = false;
5847 for (auto *C : Clauses) {
5848 if (C->getClauseKind() == OMPC_grainsize ||
5849 C->getClauseKind() == OMPC_num_tasks) {
5850 if (!PrevClause)
5851 PrevClause = C;
5852 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5853 S.Diag(C->getLocStart(),
5854 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5855 << getOpenMPClauseName(C->getClauseKind())
5856 << getOpenMPClauseName(PrevClause->getClauseKind());
5857 S.Diag(PrevClause->getLocStart(),
5858 diag::note_omp_previous_grainsize_num_tasks)
5859 << getOpenMPClauseName(PrevClause->getClauseKind());
5860 ErrorFound = true;
5861 }
5862 }
5863 }
5864 return ErrorFound;
5865}
5866
Alexey Bataev49f6e782015-12-01 04:18:41 +00005867StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5868 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5869 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005870 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005871 if (!AStmt)
5872 return StmtError();
5873
5874 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5875 OMPLoopDirective::HelperExprs B;
5876 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5877 // define the nested loops number.
5878 unsigned NestedLoopCount =
5879 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005880 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005881 VarsWithImplicitDSA, B);
5882 if (NestedLoopCount == 0)
5883 return StmtError();
5884
5885 assert((CurContext->isDependentContext() || B.builtAll()) &&
5886 "omp for loop exprs were not built");
5887
Alexey Bataev382967a2015-12-08 12:06:20 +00005888 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5889 // The grainsize clause and num_tasks clause are mutually exclusive and may
5890 // not appear on the same taskloop directive.
5891 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5892 return StmtError();
5893
Alexey Bataev49f6e782015-12-01 04:18:41 +00005894 getCurFunction()->setHasBranchProtectedScope();
5895 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5896 NestedLoopCount, Clauses, AStmt, B);
5897}
5898
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005899StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5900 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5901 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005902 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005903 if (!AStmt)
5904 return StmtError();
5905
5906 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5907 OMPLoopDirective::HelperExprs B;
5908 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5909 // define the nested loops number.
5910 unsigned NestedLoopCount =
5911 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5912 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5913 VarsWithImplicitDSA, B);
5914 if (NestedLoopCount == 0)
5915 return StmtError();
5916
5917 assert((CurContext->isDependentContext() || B.builtAll()) &&
5918 "omp for loop exprs were not built");
5919
Alexey Bataev382967a2015-12-08 12:06:20 +00005920 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5921 // The grainsize clause and num_tasks clause are mutually exclusive and may
5922 // not appear on the same taskloop directive.
5923 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5924 return StmtError();
5925
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005926 getCurFunction()->setHasBranchProtectedScope();
5927 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5928 NestedLoopCount, Clauses, AStmt, B);
5929}
5930
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005931StmtResult Sema::ActOnOpenMPDistributeDirective(
5932 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5933 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005934 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005935 if (!AStmt)
5936 return StmtError();
5937
5938 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5939 OMPLoopDirective::HelperExprs B;
5940 // In presence of clause 'collapse' with number of loops, it will
5941 // define the nested loops number.
5942 unsigned NestedLoopCount =
5943 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5944 nullptr /*ordered not a clause on distribute*/, AStmt,
5945 *this, *DSAStack, VarsWithImplicitDSA, B);
5946 if (NestedLoopCount == 0)
5947 return StmtError();
5948
5949 assert((CurContext->isDependentContext() || B.builtAll()) &&
5950 "omp for loop exprs were not built");
5951
5952 getCurFunction()->setHasBranchProtectedScope();
5953 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5954 NestedLoopCount, Clauses, AStmt, B);
5955}
5956
Alexey Bataeved09d242014-05-28 05:53:51 +00005957OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005958 SourceLocation StartLoc,
5959 SourceLocation LParenLoc,
5960 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005961 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005962 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005963 case OMPC_final:
5964 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5965 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005966 case OMPC_num_threads:
5967 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5968 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005969 case OMPC_safelen:
5970 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5971 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005972 case OMPC_simdlen:
5973 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5974 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005975 case OMPC_collapse:
5976 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5977 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005978 case OMPC_ordered:
5979 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5980 break;
Michael Wonge710d542015-08-07 16:16:36 +00005981 case OMPC_device:
5982 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5983 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005984 case OMPC_num_teams:
5985 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5986 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005987 case OMPC_thread_limit:
5988 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5989 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005990 case OMPC_priority:
5991 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5992 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005993 case OMPC_grainsize:
5994 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5995 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005996 case OMPC_num_tasks:
5997 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5998 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005999 case OMPC_hint:
6000 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6001 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006002 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006003 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006004 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006005 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006006 case OMPC_private:
6007 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006008 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006009 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006010 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006011 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006012 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006013 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006014 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006015 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006016 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006017 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006018 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006019 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006020 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006021 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006022 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006023 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006024 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006025 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006026 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006027 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006028 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006029 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006030 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006031 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006032 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006033 llvm_unreachable("Clause is not allowed.");
6034 }
6035 return Res;
6036}
6037
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006038OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6039 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006040 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006041 SourceLocation NameModifierLoc,
6042 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006043 SourceLocation EndLoc) {
6044 Expr *ValExpr = Condition;
6045 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6046 !Condition->isInstantiationDependent() &&
6047 !Condition->containsUnexpandedParameterPack()) {
6048 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006049 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006050 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006051 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006052
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006053 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006054 }
6055
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006056 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6057 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006058}
6059
Alexey Bataev3778b602014-07-17 07:32:53 +00006060OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6061 SourceLocation StartLoc,
6062 SourceLocation LParenLoc,
6063 SourceLocation EndLoc) {
6064 Expr *ValExpr = Condition;
6065 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6066 !Condition->isInstantiationDependent() &&
6067 !Condition->containsUnexpandedParameterPack()) {
6068 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6069 Condition->getExprLoc(), Condition);
6070 if (Val.isInvalid())
6071 return nullptr;
6072
6073 ValExpr = Val.get();
6074 }
6075
6076 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6077}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006078ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6079 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006080 if (!Op)
6081 return ExprError();
6082
6083 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6084 public:
6085 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006086 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006087 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6088 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006089 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6090 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006091 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6092 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006093 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6094 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006095 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6096 QualType T,
6097 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006098 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6099 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006100 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6101 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006102 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006103 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006104 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006105 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6106 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006107 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6108 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006109 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6110 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006111 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006112 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006113 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006114 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6115 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006116 llvm_unreachable("conversion functions are permitted");
6117 }
6118 } ConvertDiagnoser;
6119 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6120}
6121
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006122static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006123 OpenMPClauseKind CKind,
6124 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006125 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6126 !ValExpr->isInstantiationDependent()) {
6127 SourceLocation Loc = ValExpr->getExprLoc();
6128 ExprResult Value =
6129 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6130 if (Value.isInvalid())
6131 return false;
6132
6133 ValExpr = Value.get();
6134 // The expression must evaluate to a non-negative integer value.
6135 llvm::APSInt Result;
6136 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006137 Result.isSigned() &&
6138 !((!StrictlyPositive && Result.isNonNegative()) ||
6139 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006140 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006141 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6142 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006143 return false;
6144 }
6145 }
6146 return true;
6147}
6148
Alexey Bataev568a8332014-03-06 06:15:19 +00006149OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6150 SourceLocation StartLoc,
6151 SourceLocation LParenLoc,
6152 SourceLocation EndLoc) {
6153 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006154
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006155 // OpenMP [2.5, Restrictions]
6156 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006157 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6158 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006159 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006160
Alexey Bataeved09d242014-05-28 05:53:51 +00006161 return new (Context)
6162 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006163}
6164
Alexey Bataev62c87d22014-03-21 04:51:18 +00006165ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006166 OpenMPClauseKind CKind,
6167 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006168 if (!E)
6169 return ExprError();
6170 if (E->isValueDependent() || E->isTypeDependent() ||
6171 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006172 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006173 llvm::APSInt Result;
6174 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6175 if (ICE.isInvalid())
6176 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006177 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6178 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006179 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006180 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6181 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006182 return ExprError();
6183 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006184 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6185 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6186 << E->getSourceRange();
6187 return ExprError();
6188 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006189 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6190 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006191 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006192 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006193 return ICE;
6194}
6195
6196OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6197 SourceLocation LParenLoc,
6198 SourceLocation EndLoc) {
6199 // OpenMP [2.8.1, simd construct, Description]
6200 // The parameter of the safelen clause must be a constant
6201 // positive integer expression.
6202 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6203 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006204 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006205 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006206 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006207}
6208
Alexey Bataev66b15b52015-08-21 11:14:16 +00006209OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6210 SourceLocation LParenLoc,
6211 SourceLocation EndLoc) {
6212 // OpenMP [2.8.1, simd construct, Description]
6213 // The parameter of the simdlen clause must be a constant
6214 // positive integer expression.
6215 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6216 if (Simdlen.isInvalid())
6217 return nullptr;
6218 return new (Context)
6219 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6220}
6221
Alexander Musman64d33f12014-06-04 07:53:32 +00006222OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6223 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006224 SourceLocation LParenLoc,
6225 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006226 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006227 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006228 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006229 // The parameter of the collapse clause must be a constant
6230 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006231 ExprResult NumForLoopsResult =
6232 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6233 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006234 return nullptr;
6235 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006236 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006237}
6238
Alexey Bataev10e775f2015-07-30 11:36:16 +00006239OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6240 SourceLocation EndLoc,
6241 SourceLocation LParenLoc,
6242 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006243 // OpenMP [2.7.1, loop construct, Description]
6244 // OpenMP [2.8.1, simd construct, Description]
6245 // OpenMP [2.9.6, distribute construct, Description]
6246 // The parameter of the ordered clause must be a constant
6247 // positive integer expression if any.
6248 if (NumForLoops && LParenLoc.isValid()) {
6249 ExprResult NumForLoopsResult =
6250 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6251 if (NumForLoopsResult.isInvalid())
6252 return nullptr;
6253 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006254 } else
6255 NumForLoops = nullptr;
6256 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006257 return new (Context)
6258 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6259}
6260
Alexey Bataeved09d242014-05-28 05:53:51 +00006261OMPClause *Sema::ActOnOpenMPSimpleClause(
6262 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6263 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006264 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006265 switch (Kind) {
6266 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006267 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006268 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6269 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006270 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006271 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006272 Res = ActOnOpenMPProcBindClause(
6273 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6274 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006275 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006276 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006277 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006278 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006279 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006280 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006281 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006282 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006283 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006284 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006285 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006286 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006287 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006288 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006289 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006290 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006291 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006292 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006293 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006294 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006295 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006296 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006297 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006298 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006299 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006300 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006301 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006302 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006303 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006304 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006305 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006306 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006307 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006308 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006309 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006310 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006311 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006312 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006313 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006314 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006315 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006316 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006317 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006318 llvm_unreachable("Clause is not allowed.");
6319 }
6320 return Res;
6321}
6322
Alexey Bataev6402bca2015-12-28 07:25:51 +00006323static std::string
6324getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6325 ArrayRef<unsigned> Exclude = llvm::None) {
6326 std::string Values;
6327 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6328 unsigned Skipped = Exclude.size();
6329 auto S = Exclude.begin(), E = Exclude.end();
6330 for (unsigned i = First; i < Last; ++i) {
6331 if (std::find(S, E, i) != E) {
6332 --Skipped;
6333 continue;
6334 }
6335 Values += "'";
6336 Values += getOpenMPSimpleClauseTypeName(K, i);
6337 Values += "'";
6338 if (i == Bound - Skipped)
6339 Values += " or ";
6340 else if (i != Bound + 1 - Skipped)
6341 Values += ", ";
6342 }
6343 return Values;
6344}
6345
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006346OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6347 SourceLocation KindKwLoc,
6348 SourceLocation StartLoc,
6349 SourceLocation LParenLoc,
6350 SourceLocation EndLoc) {
6351 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006352 static_assert(OMPC_DEFAULT_unknown > 0,
6353 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006354 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006355 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6356 /*Last=*/OMPC_DEFAULT_unknown)
6357 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006358 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006360 switch (Kind) {
6361 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006362 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006363 break;
6364 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006365 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006366 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006367 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006368 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006369 break;
6370 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006371 return new (Context)
6372 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006373}
6374
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006375OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6376 SourceLocation KindKwLoc,
6377 SourceLocation StartLoc,
6378 SourceLocation LParenLoc,
6379 SourceLocation EndLoc) {
6380 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006381 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006382 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6383 /*Last=*/OMPC_PROC_BIND_unknown)
6384 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006385 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006386 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006387 return new (Context)
6388 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006389}
6390
Alexey Bataev56dafe82014-06-20 07:16:17 +00006391OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006392 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006393 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006394 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006395 SourceLocation EndLoc) {
6396 OMPClause *Res = nullptr;
6397 switch (Kind) {
6398 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006399 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6400 assert(Argument.size() == NumberOfElements &&
6401 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006402 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006403 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6404 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6405 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6406 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6407 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006408 break;
6409 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006410 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6411 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6412 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6413 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006414 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006415 case OMPC_dist_schedule:
6416 Res = ActOnOpenMPDistScheduleClause(
6417 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6418 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6419 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006420 case OMPC_defaultmap:
6421 enum { Modifier, DefaultmapKind };
6422 Res = ActOnOpenMPDefaultmapClause(
6423 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6424 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6425 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6426 ArgumentLoc[DefaultmapKind], EndLoc);
6427 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006428 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006429 case OMPC_num_threads:
6430 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006431 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006432 case OMPC_collapse:
6433 case OMPC_default:
6434 case OMPC_proc_bind:
6435 case OMPC_private:
6436 case OMPC_firstprivate:
6437 case OMPC_lastprivate:
6438 case OMPC_shared:
6439 case OMPC_reduction:
6440 case OMPC_linear:
6441 case OMPC_aligned:
6442 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006443 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006444 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006445 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006446 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006447 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006448 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006449 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006450 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006451 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006452 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006453 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006454 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006455 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006456 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006457 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006458 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006459 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006460 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006461 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006462 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006463 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006464 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006465 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006466 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006467 case OMPC_unknown:
6468 llvm_unreachable("Clause is not allowed.");
6469 }
6470 return Res;
6471}
6472
Alexey Bataev6402bca2015-12-28 07:25:51 +00006473static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6474 OpenMPScheduleClauseModifier M2,
6475 SourceLocation M1Loc, SourceLocation M2Loc) {
6476 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6477 SmallVector<unsigned, 2> Excluded;
6478 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6479 Excluded.push_back(M2);
6480 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6481 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6482 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6483 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6484 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6485 << getListOfPossibleValues(OMPC_schedule,
6486 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6487 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6488 Excluded)
6489 << getOpenMPClauseName(OMPC_schedule);
6490 return true;
6491 }
6492 return false;
6493}
6494
Alexey Bataev56dafe82014-06-20 07:16:17 +00006495OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006496 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006497 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006498 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6499 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6500 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6501 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6502 return nullptr;
6503 // OpenMP, 2.7.1, Loop Construct, Restrictions
6504 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6505 // but not both.
6506 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6507 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6508 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6509 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6510 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6511 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6512 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6513 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6514 return nullptr;
6515 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006516 if (Kind == OMPC_SCHEDULE_unknown) {
6517 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006518 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6519 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6520 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6521 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6522 Exclude);
6523 } else {
6524 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6525 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006526 }
6527 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6528 << Values << getOpenMPClauseName(OMPC_schedule);
6529 return nullptr;
6530 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006531 // OpenMP, 2.7.1, Loop Construct, Restrictions
6532 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6533 // schedule(guided).
6534 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6535 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6536 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6537 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6538 diag::err_omp_schedule_nonmonotonic_static);
6539 return nullptr;
6540 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006541 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006542 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006543 if (ChunkSize) {
6544 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6545 !ChunkSize->isInstantiationDependent() &&
6546 !ChunkSize->containsUnexpandedParameterPack()) {
6547 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6548 ExprResult Val =
6549 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6550 if (Val.isInvalid())
6551 return nullptr;
6552
6553 ValExpr = Val.get();
6554
6555 // OpenMP [2.7.1, Restrictions]
6556 // chunk_size must be a loop invariant integer expression with a positive
6557 // value.
6558 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006559 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6560 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6561 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006562 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006563 return nullptr;
6564 }
6565 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6566 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6567 ChunkSize->getType(), ".chunk.");
6568 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6569 ChunkSize->getExprLoc(),
6570 /*RefersToCapture=*/true);
6571 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006572 }
6573 }
6574 }
6575
Alexey Bataev6402bca2015-12-28 07:25:51 +00006576 return new (Context)
6577 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6578 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006579}
6580
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006581OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6582 SourceLocation StartLoc,
6583 SourceLocation EndLoc) {
6584 OMPClause *Res = nullptr;
6585 switch (Kind) {
6586 case OMPC_ordered:
6587 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6588 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006589 case OMPC_nowait:
6590 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6591 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006592 case OMPC_untied:
6593 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6594 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006595 case OMPC_mergeable:
6596 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6597 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006598 case OMPC_read:
6599 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6600 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006601 case OMPC_write:
6602 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6603 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006604 case OMPC_update:
6605 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6606 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006607 case OMPC_capture:
6608 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6609 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006610 case OMPC_seq_cst:
6611 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6612 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006613 case OMPC_threads:
6614 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6615 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006616 case OMPC_simd:
6617 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6618 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006619 case OMPC_nogroup:
6620 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6621 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006622 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006623 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006624 case OMPC_num_threads:
6625 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006626 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006627 case OMPC_collapse:
6628 case OMPC_schedule:
6629 case OMPC_private:
6630 case OMPC_firstprivate:
6631 case OMPC_lastprivate:
6632 case OMPC_shared:
6633 case OMPC_reduction:
6634 case OMPC_linear:
6635 case OMPC_aligned:
6636 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006637 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006638 case OMPC_default:
6639 case OMPC_proc_bind:
6640 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006641 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006642 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006643 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006644 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006645 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006646 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006647 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006648 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006649 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006650 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006651 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006652 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006653 case OMPC_unknown:
6654 llvm_unreachable("Clause is not allowed.");
6655 }
6656 return Res;
6657}
6658
Alexey Bataev236070f2014-06-20 11:19:47 +00006659OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6660 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006661 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006662 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6663}
6664
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006665OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6666 SourceLocation EndLoc) {
6667 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6668}
6669
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006670OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6671 SourceLocation EndLoc) {
6672 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6673}
6674
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006675OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6676 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006677 return new (Context) OMPReadClause(StartLoc, EndLoc);
6678}
6679
Alexey Bataevdea47612014-07-23 07:46:59 +00006680OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6681 SourceLocation EndLoc) {
6682 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6683}
6684
Alexey Bataev67a4f222014-07-23 10:25:33 +00006685OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6686 SourceLocation EndLoc) {
6687 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6688}
6689
Alexey Bataev459dec02014-07-24 06:46:57 +00006690OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6691 SourceLocation EndLoc) {
6692 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6693}
6694
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006695OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6696 SourceLocation EndLoc) {
6697 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6698}
6699
Alexey Bataev346265e2015-09-25 10:37:12 +00006700OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6701 SourceLocation EndLoc) {
6702 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6703}
6704
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006705OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6706 SourceLocation EndLoc) {
6707 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6708}
6709
Alexey Bataevb825de12015-12-07 10:51:44 +00006710OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6711 SourceLocation EndLoc) {
6712 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6713}
6714
Alexey Bataevc5e02582014-06-16 07:08:35 +00006715OMPClause *Sema::ActOnOpenMPVarListClause(
6716 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6717 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6718 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006719 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006720 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6721 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6722 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006723 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006724 switch (Kind) {
6725 case OMPC_private:
6726 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6727 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006728 case OMPC_firstprivate:
6729 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6730 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006731 case OMPC_lastprivate:
6732 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6733 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006734 case OMPC_shared:
6735 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6736 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006737 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006738 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6739 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006740 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006741 case OMPC_linear:
6742 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006743 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006744 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006745 case OMPC_aligned:
6746 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6747 ColonLoc, EndLoc);
6748 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006749 case OMPC_copyin:
6750 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6751 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006752 case OMPC_copyprivate:
6753 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6754 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006755 case OMPC_flush:
6756 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6757 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006758 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006759 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6760 StartLoc, LParenLoc, EndLoc);
6761 break;
6762 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006763 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6764 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6765 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006766 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006767 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006768 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006769 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006770 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006771 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006772 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006773 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006774 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006775 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006776 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006777 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006778 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006779 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006780 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006781 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006782 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006783 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006784 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006785 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006786 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006787 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006788 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006789 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006790 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006791 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006792 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006793 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006794 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006795 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006796 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006797 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006798 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006799 llvm_unreachable("Clause is not allowed.");
6800 }
6801 return Res;
6802}
6803
6804OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6805 SourceLocation StartLoc,
6806 SourceLocation LParenLoc,
6807 SourceLocation EndLoc) {
6808 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006809 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006810 for (auto &RefExpr : VarList) {
6811 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006812 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6813 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006814 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006815 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006816 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006817 continue;
6818 }
6819
Alexey Bataeved09d242014-05-28 05:53:51 +00006820 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006821 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006822 // A list item is a variable name.
6823 // OpenMP [2.9.3.3, Restrictions, p.1]
6824 // A variable that is part of another variable (as an array or
6825 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006826 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6827 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6828 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6829 (getCurrentThisType().isNull() || !ME ||
6830 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6831 !isa<FieldDecl>(ME->getMemberDecl()))) {
6832 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6833 << (getCurrentThisType().isNull() ? 0 : 1)
6834 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006835 continue;
6836 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006837 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6838 QualType Type = D->getType();
6839 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006840
6841 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6842 // A variable that appears in a private clause must not have an incomplete
6843 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006844 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006845 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006846 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006847
Alexey Bataev758e55e2013-09-06 18:03:48 +00006848 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6849 // in a Construct]
6850 // Variables with the predetermined data-sharing attributes may not be
6851 // listed in data-sharing attributes clauses, except for the cases
6852 // listed below. For these exceptions only, listing a predetermined
6853 // variable in a data-sharing attribute clause is allowed and overrides
6854 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006855 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006856 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006857 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6858 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006859 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006860 continue;
6861 }
6862
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006863 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006864 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006865 DSAStack->getCurrentDirective() == OMPD_task) {
6866 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6867 << getOpenMPClauseName(OMPC_private) << Type
6868 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6869 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006870 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006872 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006874 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006875 continue;
6876 }
6877
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006878 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6879 // A variable of class type (or array thereof) that appears in a private
6880 // clause requires an accessible, unambiguous default constructor for the
6881 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006882 // Generate helper private variable and initialize it with the default
6883 // value. The address of the original variable is replaced by the address of
6884 // the new private variable in CodeGen. This new variable is not added to
6885 // IdResolver, so the code in the OpenMP region uses original variable for
6886 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006887 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006888 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
6889 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006890 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006891 if (VDPrivate->isInvalidDecl())
6892 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006893 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006894 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006895
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006896 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
6897 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006898 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006899 }
6900
Alexey Bataeved09d242014-05-28 05:53:51 +00006901 if (Vars.empty())
6902 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006903
Alexey Bataev03b340a2014-10-21 03:16:40 +00006904 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6905 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006906}
6907
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006908namespace {
6909class DiagsUninitializedSeveretyRAII {
6910private:
6911 DiagnosticsEngine &Diags;
6912 SourceLocation SavedLoc;
6913 bool IsIgnored;
6914
6915public:
6916 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6917 bool IsIgnored)
6918 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6919 if (!IsIgnored) {
6920 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6921 /*Map*/ diag::Severity::Ignored, Loc);
6922 }
6923 }
6924 ~DiagsUninitializedSeveretyRAII() {
6925 if (!IsIgnored)
6926 Diags.popMappings(SavedLoc);
6927 }
6928};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006929}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006930
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006931OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6932 SourceLocation StartLoc,
6933 SourceLocation LParenLoc,
6934 SourceLocation EndLoc) {
6935 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006936 SmallVector<Expr *, 8> PrivateCopies;
6937 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006938 bool IsImplicitClause =
6939 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6940 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6941
Alexey Bataeved09d242014-05-28 05:53:51 +00006942 for (auto &RefExpr : VarList) {
6943 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6944 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006945 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006946 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006947 PrivateCopies.push_back(nullptr);
6948 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006949 continue;
6950 }
6951
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006952 SourceLocation ELoc =
6953 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006954 // OpenMP [2.1, C/C++]
6955 // A list item is a variable name.
6956 // OpenMP [2.9.3.3, Restrictions, p.1]
6957 // A variable that is part of another variable (as an array or
6958 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006959 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006960 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006961 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6962 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006963 continue;
6964 }
6965 Decl *D = DE->getDecl();
6966 VarDecl *VD = cast<VarDecl>(D);
6967
6968 QualType Type = VD->getType();
6969 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6970 // It will be analyzed later.
6971 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006972 PrivateCopies.push_back(nullptr);
6973 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006974 continue;
6975 }
6976
6977 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6978 // A variable that appears in a private clause must not have an incomplete
6979 // type or a reference type.
6980 if (RequireCompleteType(ELoc, Type,
6981 diag::err_omp_firstprivate_incomplete_type)) {
6982 continue;
6983 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006984 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006985
6986 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6987 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006988 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006989 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006990 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006991
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006992 // If an implicit firstprivate variable found it was checked already.
6993 if (!IsImplicitClause) {
6994 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006995 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006996 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6997 // A list item that specifies a given variable may not appear in more
6998 // than one clause on the same directive, except that a variable may be
6999 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007000 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007001 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007002 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007003 << getOpenMPClauseName(DVar.CKind)
7004 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007005 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007006 continue;
7007 }
7008
7009 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7010 // in a Construct]
7011 // Variables with the predetermined data-sharing attributes may not be
7012 // listed in data-sharing attributes clauses, except for the cases
7013 // listed below. For these exceptions only, listing a predetermined
7014 // variable in a data-sharing attribute clause is allowed and overrides
7015 // the variable's predetermined data-sharing attributes.
7016 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7017 // in a Construct, C/C++, p.2]
7018 // Variables with const-qualified type having no mutable member may be
7019 // listed in a firstprivate clause, even if they are static data members.
7020 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7021 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7022 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007023 << getOpenMPClauseName(DVar.CKind)
7024 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007025 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007026 continue;
7027 }
7028
Alexey Bataevf29276e2014-06-18 04:14:57 +00007029 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007030 // OpenMP [2.9.3.4, Restrictions, p.2]
7031 // A list item that is private within a parallel region must not appear
7032 // in a firstprivate clause on a worksharing construct if any of the
7033 // worksharing regions arising from the worksharing construct ever bind
7034 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007035 if (isOpenMPWorksharingDirective(CurrDir) &&
7036 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007037 DVar = DSAStack->getImplicitDSA(VD, true);
7038 if (DVar.CKind != OMPC_shared &&
7039 (isOpenMPParallelDirective(DVar.DKind) ||
7040 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007041 Diag(ELoc, diag::err_omp_required_access)
7042 << getOpenMPClauseName(OMPC_firstprivate)
7043 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007044 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007045 continue;
7046 }
7047 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007048 // OpenMP [2.9.3.4, Restrictions, p.3]
7049 // A list item that appears in a reduction clause of a parallel construct
7050 // must not appear in a firstprivate clause on a worksharing or task
7051 // construct if any of the worksharing or task regions arising from the
7052 // worksharing or task construct ever bind to any of the parallel regions
7053 // arising from the parallel construct.
7054 // OpenMP [2.9.3.4, Restrictions, p.4]
7055 // A list item that appears in a reduction clause in worksharing
7056 // construct must not appear in a firstprivate clause in a task construct
7057 // encountered during execution of any of the worksharing regions arising
7058 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007059 if (CurrDir == OMPD_task) {
7060 DVar =
7061 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7062 [](OpenMPDirectiveKind K) -> bool {
7063 return isOpenMPParallelDirective(K) ||
7064 isOpenMPWorksharingDirective(K);
7065 },
7066 false);
7067 if (DVar.CKind == OMPC_reduction &&
7068 (isOpenMPParallelDirective(DVar.DKind) ||
7069 isOpenMPWorksharingDirective(DVar.DKind))) {
7070 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7071 << getOpenMPDirectiveName(DVar.DKind);
7072 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7073 continue;
7074 }
7075 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007076
7077 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7078 // A list item that is private within a teams region must not appear in a
7079 // firstprivate clause on a distribute construct if any of the distribute
7080 // regions arising from the distribute construct ever bind to any of the
7081 // teams regions arising from the teams construct.
7082 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7083 // A list item that appears in a reduction clause of a teams construct
7084 // must not appear in a firstprivate clause on a distribute construct if
7085 // any of the distribute regions arising from the distribute construct
7086 // ever bind to any of the teams regions arising from the teams construct.
7087 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7088 // A list item may appear in a firstprivate or lastprivate clause but not
7089 // both.
7090 if (CurrDir == OMPD_distribute) {
7091 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7092 [](OpenMPDirectiveKind K) -> bool {
7093 return isOpenMPTeamsDirective(K);
7094 },
7095 false);
7096 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7097 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7098 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7099 continue;
7100 }
7101 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7102 [](OpenMPDirectiveKind K) -> bool {
7103 return isOpenMPTeamsDirective(K);
7104 },
7105 false);
7106 if (DVar.CKind == OMPC_reduction &&
7107 isOpenMPTeamsDirective(DVar.DKind)) {
7108 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7109 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7110 continue;
7111 }
7112 DVar = DSAStack->getTopDSA(VD, false);
7113 if (DVar.CKind == OMPC_lastprivate) {
7114 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7115 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7116 continue;
7117 }
7118 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007119 }
7120
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007121 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007122 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007123 DSAStack->getCurrentDirective() == OMPD_task) {
7124 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7125 << getOpenMPClauseName(OMPC_firstprivate) << Type
7126 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7127 bool IsDecl =
7128 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7129 Diag(VD->getLocation(),
7130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7131 << VD;
7132 continue;
7133 }
7134
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007135 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007136 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7137 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007138 // Generate helper private variable and initialize it with the value of the
7139 // original variable. The address of the original variable is replaced by
7140 // the address of the new private variable in the CodeGen. This new variable
7141 // is not added to IdResolver, so the code in the OpenMP region uses
7142 // original variable for proper diagnostics and variable capturing.
7143 Expr *VDInitRefExpr = nullptr;
7144 // For arrays generate initializer for single element and replace it by the
7145 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007146 if (Type->isArrayType()) {
7147 auto VDInit =
7148 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7149 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007150 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007151 ElemType = ElemType.getUnqualifiedType();
7152 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7153 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007154 InitializedEntity Entity =
7155 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007156 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7157
7158 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7159 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7160 if (Result.isInvalid())
7161 VDPrivate->setInvalidDecl();
7162 else
7163 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007164 // Remove temp variable declaration.
7165 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007166 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007167 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007168 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007169 VDInitRefExpr =
7170 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007171 AddInitializerToDecl(VDPrivate,
7172 DefaultLvalueConversion(VDInitRefExpr).get(),
7173 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007174 }
7175 if (VDPrivate->isInvalidDecl()) {
7176 if (IsImplicitClause) {
7177 Diag(DE->getExprLoc(),
7178 diag::note_omp_task_predetermined_firstprivate_here);
7179 }
7180 continue;
7181 }
7182 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007183 auto VDPrivateRefExpr = buildDeclRefExpr(
7184 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007185 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7186 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007187 PrivateCopies.push_back(VDPrivateRefExpr);
7188 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007189 }
7190
Alexey Bataeved09d242014-05-28 05:53:51 +00007191 if (Vars.empty())
7192 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007193
7194 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007195 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007196}
7197
Alexander Musman1bb328c2014-06-04 13:06:39 +00007198OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7199 SourceLocation StartLoc,
7200 SourceLocation LParenLoc,
7201 SourceLocation EndLoc) {
7202 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007203 SmallVector<Expr *, 8> SrcExprs;
7204 SmallVector<Expr *, 8> DstExprs;
7205 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007206 for (auto &RefExpr : VarList) {
7207 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7208 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7209 // It will be analyzed later.
7210 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007211 SrcExprs.push_back(nullptr);
7212 DstExprs.push_back(nullptr);
7213 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007214 continue;
7215 }
7216
7217 SourceLocation ELoc = RefExpr->getExprLoc();
7218 // OpenMP [2.1, C/C++]
7219 // A list item is a variable name.
7220 // OpenMP [2.14.3.5, Restrictions, p.1]
7221 // A variable that is part of another variable (as an array or structure
7222 // element) cannot appear in a lastprivate clause.
7223 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7224 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007225 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7226 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007227 continue;
7228 }
7229 Decl *D = DE->getDecl();
7230 VarDecl *VD = cast<VarDecl>(D);
7231
7232 QualType Type = VD->getType();
7233 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7234 // It will be analyzed later.
7235 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007236 SrcExprs.push_back(nullptr);
7237 DstExprs.push_back(nullptr);
7238 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007239 continue;
7240 }
7241
7242 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7243 // A variable that appears in a lastprivate clause must not have an
7244 // incomplete type or a reference type.
7245 if (RequireCompleteType(ELoc, Type,
7246 diag::err_omp_lastprivate_incomplete_type)) {
7247 continue;
7248 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007249 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007250
7251 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7252 // in a Construct]
7253 // Variables with the predetermined data-sharing attributes may not be
7254 // listed in data-sharing attributes clauses, except for the cases
7255 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007256 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007257 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7258 DVar.CKind != OMPC_firstprivate &&
7259 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7260 Diag(ELoc, diag::err_omp_wrong_dsa)
7261 << getOpenMPClauseName(DVar.CKind)
7262 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007263 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007264 continue;
7265 }
7266
Alexey Bataevf29276e2014-06-18 04:14:57 +00007267 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7268 // OpenMP [2.14.3.5, Restrictions, p.2]
7269 // A list item that is private within a parallel region, or that appears in
7270 // the reduction clause of a parallel construct, must not appear in a
7271 // lastprivate clause on a worksharing construct if any of the corresponding
7272 // worksharing regions ever binds to any of the corresponding parallel
7273 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007274 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007275 if (isOpenMPWorksharingDirective(CurrDir) &&
7276 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007277 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007278 if (DVar.CKind != OMPC_shared) {
7279 Diag(ELoc, diag::err_omp_required_access)
7280 << getOpenMPClauseName(OMPC_lastprivate)
7281 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007282 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007283 continue;
7284 }
7285 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007286 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007287 // A variable of class type (or array thereof) that appears in a
7288 // lastprivate clause requires an accessible, unambiguous default
7289 // constructor for the class type, unless the list item is also specified
7290 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007291 // A variable of class type (or array thereof) that appears in a
7292 // lastprivate clause requires an accessible, unambiguous copy assignment
7293 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007294 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007295 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007296 Type.getUnqualifiedType(), ".lastprivate.src",
7297 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007298 auto *PseudoSrcExpr = buildDeclRefExpr(
7299 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007300 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007301 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7302 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007303 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007304 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007305 // For arrays generate assignment operation for single element and replace
7306 // it by the original array element in CodeGen.
7307 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7308 PseudoDstExpr, PseudoSrcExpr);
7309 if (AssignmentOp.isInvalid())
7310 continue;
7311 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7312 /*DiscardedValue=*/true);
7313 if (AssignmentOp.isInvalid())
7314 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007315
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007316 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7317 // A list item may appear in a firstprivate or lastprivate clause but not
7318 // both.
7319 if (CurrDir == OMPD_distribute) {
7320 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7321 if (DVar.CKind == OMPC_firstprivate) {
7322 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7323 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7324 continue;
7325 }
7326 }
7327
Alexey Bataev39f915b82015-05-08 10:41:21 +00007328 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007329 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007330 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007331 SrcExprs.push_back(PseudoSrcExpr);
7332 DstExprs.push_back(PseudoDstExpr);
7333 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007334 }
7335
7336 if (Vars.empty())
7337 return nullptr;
7338
7339 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007340 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007341}
7342
Alexey Bataev758e55e2013-09-06 18:03:48 +00007343OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7344 SourceLocation StartLoc,
7345 SourceLocation LParenLoc,
7346 SourceLocation EndLoc) {
7347 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007348 for (auto &RefExpr : VarList) {
7349 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7350 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007351 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007352 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007353 continue;
7354 }
7355
Alexey Bataeved09d242014-05-28 05:53:51 +00007356 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007357 // OpenMP [2.1, C/C++]
7358 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007359 // OpenMP [2.14.3.2, Restrictions, p.1]
7360 // A variable that is part of another variable (as an array or structure
7361 // element) cannot appear in a shared unless it is a static data member
7362 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007363 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007364 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007365 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7366 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007367 continue;
7368 }
7369 Decl *D = DE->getDecl();
7370 VarDecl *VD = cast<VarDecl>(D);
7371
7372 QualType Type = VD->getType();
7373 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7374 // It will be analyzed later.
7375 Vars.push_back(DE);
7376 continue;
7377 }
7378
7379 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7380 // in a Construct]
7381 // Variables with the predetermined data-sharing attributes may not be
7382 // listed in data-sharing attributes clauses, except for the cases
7383 // listed below. For these exceptions only, listing a predetermined
7384 // variable in a data-sharing attribute clause is allowed and overrides
7385 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007386 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007387 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7388 DVar.RefExpr) {
7389 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7390 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007391 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007392 continue;
7393 }
7394
7395 DSAStack->addDSA(VD, DE, OMPC_shared);
7396 Vars.push_back(DE);
7397 }
7398
Alexey Bataeved09d242014-05-28 05:53:51 +00007399 if (Vars.empty())
7400 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007401
7402 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7403}
7404
Alexey Bataevc5e02582014-06-16 07:08:35 +00007405namespace {
7406class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7407 DSAStackTy *Stack;
7408
7409public:
7410 bool VisitDeclRefExpr(DeclRefExpr *E) {
7411 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007412 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007413 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7414 return false;
7415 if (DVar.CKind != OMPC_unknown)
7416 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007417 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007418 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007419 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007420 return true;
7421 return false;
7422 }
7423 return false;
7424 }
7425 bool VisitStmt(Stmt *S) {
7426 for (auto Child : S->children()) {
7427 if (Child && Visit(Child))
7428 return true;
7429 }
7430 return false;
7431 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007432 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007433};
Alexey Bataev23b69422014-06-18 07:08:49 +00007434} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007435
7436OMPClause *Sema::ActOnOpenMPReductionClause(
7437 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7438 SourceLocation ColonLoc, SourceLocation EndLoc,
7439 CXXScopeSpec &ReductionIdScopeSpec,
7440 const DeclarationNameInfo &ReductionId) {
7441 // TODO: Allow scope specification search when 'declare reduction' is
7442 // supported.
7443 assert(ReductionIdScopeSpec.isEmpty() &&
7444 "No support for scoped reduction identifiers yet.");
7445
7446 auto DN = ReductionId.getName();
7447 auto OOK = DN.getCXXOverloadedOperator();
7448 BinaryOperatorKind BOK = BO_Comma;
7449
7450 // OpenMP [2.14.3.6, reduction clause]
7451 // C
7452 // reduction-identifier is either an identifier or one of the following
7453 // operators: +, -, *, &, |, ^, && and ||
7454 // C++
7455 // reduction-identifier is either an id-expression or one of the following
7456 // operators: +, -, *, &, |, ^, && and ||
7457 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7458 switch (OOK) {
7459 case OO_Plus:
7460 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007461 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007462 break;
7463 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007464 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007465 break;
7466 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007467 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007468 break;
7469 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007470 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007471 break;
7472 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007473 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007474 break;
7475 case OO_AmpAmp:
7476 BOK = BO_LAnd;
7477 break;
7478 case OO_PipePipe:
7479 BOK = BO_LOr;
7480 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007481 case OO_New:
7482 case OO_Delete:
7483 case OO_Array_New:
7484 case OO_Array_Delete:
7485 case OO_Slash:
7486 case OO_Percent:
7487 case OO_Tilde:
7488 case OO_Exclaim:
7489 case OO_Equal:
7490 case OO_Less:
7491 case OO_Greater:
7492 case OO_LessEqual:
7493 case OO_GreaterEqual:
7494 case OO_PlusEqual:
7495 case OO_MinusEqual:
7496 case OO_StarEqual:
7497 case OO_SlashEqual:
7498 case OO_PercentEqual:
7499 case OO_CaretEqual:
7500 case OO_AmpEqual:
7501 case OO_PipeEqual:
7502 case OO_LessLess:
7503 case OO_GreaterGreater:
7504 case OO_LessLessEqual:
7505 case OO_GreaterGreaterEqual:
7506 case OO_EqualEqual:
7507 case OO_ExclaimEqual:
7508 case OO_PlusPlus:
7509 case OO_MinusMinus:
7510 case OO_Comma:
7511 case OO_ArrowStar:
7512 case OO_Arrow:
7513 case OO_Call:
7514 case OO_Subscript:
7515 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007516 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007517 case NUM_OVERLOADED_OPERATORS:
7518 llvm_unreachable("Unexpected reduction identifier");
7519 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007520 if (auto II = DN.getAsIdentifierInfo()) {
7521 if (II->isStr("max"))
7522 BOK = BO_GT;
7523 else if (II->isStr("min"))
7524 BOK = BO_LT;
7525 }
7526 break;
7527 }
7528 SourceRange ReductionIdRange;
7529 if (ReductionIdScopeSpec.isValid()) {
7530 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7531 }
7532 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7533 if (BOK == BO_Comma) {
7534 // Not allowed reduction identifier is found.
7535 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7536 << ReductionIdRange;
7537 return nullptr;
7538 }
7539
7540 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007541 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007542 SmallVector<Expr *, 8> LHSs;
7543 SmallVector<Expr *, 8> RHSs;
7544 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007545 for (auto RefExpr : VarList) {
7546 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7547 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7548 // It will be analyzed later.
7549 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007550 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007551 LHSs.push_back(nullptr);
7552 RHSs.push_back(nullptr);
7553 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007554 continue;
7555 }
7556
7557 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7558 RefExpr->isInstantiationDependent() ||
7559 RefExpr->containsUnexpandedParameterPack()) {
7560 // It will be analyzed later.
7561 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007562 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007563 LHSs.push_back(nullptr);
7564 RHSs.push_back(nullptr);
7565 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007566 continue;
7567 }
7568
7569 auto ELoc = RefExpr->getExprLoc();
7570 auto ERange = RefExpr->getSourceRange();
7571 // OpenMP [2.1, C/C++]
7572 // A list item is a variable or array section, subject to the restrictions
7573 // specified in Section 2.4 on page 42 and in each of the sections
7574 // describing clauses and directives for which a list appears.
7575 // OpenMP [2.14.3.3, Restrictions, p.1]
7576 // A variable that is part of another variable (as an array or
7577 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007578 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7579 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7580 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7581 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007582 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7583 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007584 continue;
7585 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007586 QualType Type;
7587 VarDecl *VD = nullptr;
7588 if (DE) {
7589 auto D = DE->getDecl();
7590 VD = cast<VarDecl>(D);
Alexey Bataev1189bd02016-01-26 12:20:39 +00007591 Type = Context.getBaseElementType(VD->getType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007592 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007593 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007594 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7595 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7596 Base = TempASE->getBase()->IgnoreParenImpCasts();
7597 DE = dyn_cast<DeclRefExpr>(Base);
7598 if (DE)
7599 VD = dyn_cast<VarDecl>(DE->getDecl());
7600 if (!VD) {
7601 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7602 << 0 << Base->getSourceRange();
7603 continue;
7604 }
7605 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007606 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7607 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7608 Type = ATy->getElementType();
7609 else
7610 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007611 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7612 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7613 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7614 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7615 Base = TempASE->getBase()->IgnoreParenImpCasts();
7616 DE = dyn_cast<DeclRefExpr>(Base);
7617 if (DE)
7618 VD = dyn_cast<VarDecl>(DE->getDecl());
7619 if (!VD) {
7620 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7621 << 1 << Base->getSourceRange();
7622 continue;
7623 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007624 }
7625
Alexey Bataevc5e02582014-06-16 07:08:35 +00007626 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7627 // A variable that appears in a private clause must not have an incomplete
7628 // type or a reference type.
7629 if (RequireCompleteType(ELoc, Type,
7630 diag::err_omp_reduction_incomplete_type))
7631 continue;
7632 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007633 // A list item that appears in a reduction clause must not be
7634 // const-qualified.
7635 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007636 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007637 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007638 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007639 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7640 VarDecl::DeclarationOnly;
7641 Diag(VD->getLocation(),
7642 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7643 << VD;
7644 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007645 continue;
7646 }
7647 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7648 // If a list-item is a reference type then it must bind to the same object
7649 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007650 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007651 VarDecl *VDDef = VD->getDefinition();
7652 if (Type->isReferenceType() && VDDef) {
7653 DSARefChecker Check(DSAStack);
7654 if (Check.Visit(VDDef->getInit())) {
7655 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7656 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7657 continue;
7658 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007659 }
7660 }
7661 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7662 // The type of a list item that appears in a reduction clause must be valid
7663 // for the reduction-identifier. For a max or min reduction in C, the type
7664 // of the list item must be an allowed arithmetic data type: char, int,
7665 // float, double, or _Bool, possibly modified with long, short, signed, or
7666 // unsigned. For a max or min reduction in C++, the type of the list item
7667 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7668 // double, or bool, possibly modified with long, short, signed, or unsigned.
7669 if ((BOK == BO_GT || BOK == BO_LT) &&
7670 !(Type->isScalarType() ||
7671 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7672 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7673 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007674 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007675 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7676 VarDecl::DeclarationOnly;
7677 Diag(VD->getLocation(),
7678 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7679 << VD;
7680 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007681 continue;
7682 }
7683 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7684 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7685 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007686 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007687 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7688 VarDecl::DeclarationOnly;
7689 Diag(VD->getLocation(),
7690 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7691 << VD;
7692 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007693 continue;
7694 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007695 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7696 // in a Construct]
7697 // Variables with the predetermined data-sharing attributes may not be
7698 // listed in data-sharing attributes clauses, except for the cases
7699 // listed below. For these exceptions only, listing a predetermined
7700 // variable in a data-sharing attribute clause is allowed and overrides
7701 // the variable's predetermined data-sharing attributes.
7702 // OpenMP [2.14.3.6, Restrictions, p.3]
7703 // Any number of reduction clauses can be specified on the directive,
7704 // but a list item can appear only once in the reduction clauses for that
7705 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007706 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007707 DVar = DSAStack->getTopDSA(VD, false);
7708 if (DVar.CKind == OMPC_reduction) {
7709 Diag(ELoc, diag::err_omp_once_referenced)
7710 << getOpenMPClauseName(OMPC_reduction);
7711 if (DVar.RefExpr) {
7712 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007713 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007714 } else if (DVar.CKind != OMPC_unknown) {
7715 Diag(ELoc, diag::err_omp_wrong_dsa)
7716 << getOpenMPClauseName(DVar.CKind)
7717 << getOpenMPClauseName(OMPC_reduction);
7718 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7719 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007720 }
7721
7722 // OpenMP [2.14.3.6, Restrictions, p.1]
7723 // A list item that appears in a reduction clause of a worksharing
7724 // construct must be shared in the parallel regions to which any of the
7725 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007726 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7727 if (isOpenMPWorksharingDirective(CurrDir) &&
7728 !isOpenMPParallelDirective(CurrDir)) {
7729 DVar = DSAStack->getImplicitDSA(VD, true);
7730 if (DVar.CKind != OMPC_shared) {
7731 Diag(ELoc, diag::err_omp_required_access)
7732 << getOpenMPClauseName(OMPC_reduction)
7733 << getOpenMPClauseName(OMPC_shared);
7734 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7735 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007736 }
7737 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007738
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007739 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007740 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7741 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7742 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7743 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7744 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007745 if (OASE ||
7746 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7747 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007748 // Create pseudo array type for private copy. The size for this array will
7749 // be generated during codegen.
7750 // For array subscripts or single variables Private Ty is the same as Type
7751 // (type of the variable or single array element).
7752 PrivateTy = Context.getVariableArrayType(
7753 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7754 Context.getSizeType(), VK_RValue),
7755 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007756 } else if (DE &&
7757 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7758 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007759 // Private copy.
7760 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7761 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007762 // Add initializer for private variable.
7763 Expr *Init = nullptr;
7764 switch (BOK) {
7765 case BO_Add:
7766 case BO_Xor:
7767 case BO_Or:
7768 case BO_LOr:
7769 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7770 if (Type->isScalarType() || Type->isAnyComplexType()) {
7771 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007772 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007773 break;
7774 case BO_Mul:
7775 case BO_LAnd:
7776 if (Type->isScalarType() || Type->isAnyComplexType()) {
7777 // '*' and '&&' reduction ops - initializer is '1'.
7778 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7779 }
7780 break;
7781 case BO_And: {
7782 // '&' reduction op - initializer is '~0'.
7783 QualType OrigType = Type;
7784 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7785 Type = ComplexTy->getElementType();
7786 }
7787 if (Type->isRealFloatingType()) {
7788 llvm::APFloat InitValue =
7789 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7790 /*isIEEE=*/true);
7791 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7792 Type, ELoc);
7793 } else if (Type->isScalarType()) {
7794 auto Size = Context.getTypeSize(Type);
7795 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7796 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7797 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7798 }
7799 if (Init && OrigType->isAnyComplexType()) {
7800 // Init = 0xFFFF + 0xFFFFi;
7801 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7802 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7803 }
7804 Type = OrigType;
7805 break;
7806 }
7807 case BO_LT:
7808 case BO_GT: {
7809 // 'min' reduction op - initializer is 'Largest representable number in
7810 // the reduction list item type'.
7811 // 'max' reduction op - initializer is 'Least representable number in
7812 // the reduction list item type'.
7813 if (Type->isIntegerType() || Type->isPointerType()) {
7814 bool IsSigned = Type->hasSignedIntegerRepresentation();
7815 auto Size = Context.getTypeSize(Type);
7816 QualType IntTy =
7817 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7818 llvm::APInt InitValue =
7819 (BOK != BO_LT)
7820 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7821 : llvm::APInt::getMinValue(Size)
7822 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7823 : llvm::APInt::getMaxValue(Size);
7824 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7825 if (Type->isPointerType()) {
7826 // Cast to pointer type.
7827 auto CastExpr = BuildCStyleCastExpr(
7828 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7829 SourceLocation(), Init);
7830 if (CastExpr.isInvalid())
7831 continue;
7832 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007833 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007834 } else if (Type->isRealFloatingType()) {
7835 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7836 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7837 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7838 Type, ELoc);
7839 }
7840 break;
7841 }
7842 case BO_PtrMemD:
7843 case BO_PtrMemI:
7844 case BO_MulAssign:
7845 case BO_Div:
7846 case BO_Rem:
7847 case BO_Sub:
7848 case BO_Shl:
7849 case BO_Shr:
7850 case BO_LE:
7851 case BO_GE:
7852 case BO_EQ:
7853 case BO_NE:
7854 case BO_AndAssign:
7855 case BO_XorAssign:
7856 case BO_OrAssign:
7857 case BO_Assign:
7858 case BO_AddAssign:
7859 case BO_SubAssign:
7860 case BO_DivAssign:
7861 case BO_RemAssign:
7862 case BO_ShlAssign:
7863 case BO_ShrAssign:
7864 case BO_Comma:
7865 llvm_unreachable("Unexpected reduction operation");
7866 }
7867 if (Init) {
7868 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7869 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007870 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007871 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007872 if (!RHSVD->hasInit()) {
7873 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7874 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007875 if (VD) {
7876 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7877 VarDecl::DeclarationOnly;
7878 Diag(VD->getLocation(),
7879 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7880 << VD;
7881 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007882 continue;
7883 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007884 // Store initializer for single element in private copy. Will be used during
7885 // codegen.
7886 PrivateVD->setInit(RHSVD->getInit());
7887 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007888 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7889 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007890 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007891 ExprResult ReductionOp =
7892 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7893 LHSDRE, RHSDRE);
7894 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007895 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007896 ReductionOp =
7897 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7898 BO_Assign, LHSDRE, ReductionOp.get());
7899 } else {
7900 auto *ConditionalOp = new (Context) ConditionalOperator(
7901 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7902 RHSDRE, Type, VK_LValue, OK_Ordinary);
7903 ReductionOp =
7904 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7905 BO_Assign, LHSDRE, ConditionalOp);
7906 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007907 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007908 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007909 if (ReductionOp.isInvalid())
7910 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007911
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007912 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007913 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007914 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007915 LHSs.push_back(LHSDRE);
7916 RHSs.push_back(RHSDRE);
7917 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007918 }
7919
7920 if (Vars.empty())
7921 return nullptr;
7922
7923 return OMPReductionClause::Create(
7924 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007925 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7926 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007927}
7928
Alexey Bataev182227b2015-08-20 10:54:39 +00007929OMPClause *Sema::ActOnOpenMPLinearClause(
7930 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7931 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7932 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007933 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007934 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007935 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007936 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7937 LinKind == OMPC_LINEAR_unknown) {
7938 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7939 LinKind = OMPC_LINEAR_val;
7940 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007941 for (auto &RefExpr : VarList) {
7942 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7943 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007944 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007945 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007946 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007947 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007948 continue;
7949 }
7950
7951 // OpenMP [2.14.3.7, linear clause]
7952 // A list item that appears in a linear clause is subject to the private
7953 // clause semantics described in Section 2.14.3.3 on page 159 except as
7954 // noted. In addition, the value of the new list item on each iteration
7955 // of the associated loop(s) corresponds to the value of the original
7956 // list item before entering the construct plus the logical number of
7957 // the iteration times linear-step.
7958
Alexey Bataeved09d242014-05-28 05:53:51 +00007959 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007960 // OpenMP [2.1, C/C++]
7961 // A list item is a variable name.
7962 // OpenMP [2.14.3.3, Restrictions, p.1]
7963 // A variable that is part of another variable (as an array or
7964 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007965 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007966 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007967 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7968 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007969 continue;
7970 }
7971
7972 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7973
7974 // OpenMP [2.14.3.7, linear clause]
7975 // A list-item cannot appear in more than one linear clause.
7976 // A list-item that appears in a linear clause cannot appear in any
7977 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007978 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007979 if (DVar.RefExpr) {
7980 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7981 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007982 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007983 continue;
7984 }
7985
7986 QualType QType = VD->getType();
7987 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7988 // It will be analyzed later.
7989 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007990 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007991 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007992 continue;
7993 }
7994
7995 // A variable must not have an incomplete type or a reference type.
7996 if (RequireCompleteType(ELoc, QType,
7997 diag::err_omp_linear_incomplete_type)) {
7998 continue;
7999 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008000 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8001 !QType->isReferenceType()) {
8002 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8003 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8004 continue;
8005 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008006 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008007
8008 // A list item must not be const-qualified.
8009 if (QType.isConstant(Context)) {
8010 Diag(ELoc, diag::err_omp_const_variable)
8011 << getOpenMPClauseName(OMPC_linear);
8012 bool IsDecl =
8013 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8014 Diag(VD->getLocation(),
8015 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8016 << VD;
8017 continue;
8018 }
8019
8020 // A list item must be of integral or pointer type.
8021 QType = QType.getUnqualifiedType().getCanonicalType();
8022 const Type *Ty = QType.getTypePtrOrNull();
8023 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8024 !Ty->isPointerType())) {
8025 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8026 bool IsDecl =
8027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8028 Diag(VD->getLocation(),
8029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8030 << VD;
8031 continue;
8032 }
8033
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008034 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008035 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8036 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008037 auto *PrivateRef = buildDeclRefExpr(
8038 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008039 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008040 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008041 Expr *InitExpr;
8042 if (LinKind == OMPC_LINEAR_uval)
8043 InitExpr = VD->getInit();
8044 else
8045 InitExpr = DE;
8046 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008047 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008048 auto InitRef = buildDeclRefExpr(
8049 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008050 DSAStack->addDSA(VD, DE, OMPC_linear);
8051 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008052 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008053 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008054 }
8055
8056 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008057 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008058
8059 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008060 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008061 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8062 !Step->isInstantiationDependent() &&
8063 !Step->containsUnexpandedParameterPack()) {
8064 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008065 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008066 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008067 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008068 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008069
Alexander Musman3276a272015-03-21 10:12:56 +00008070 // Build var to save the step value.
8071 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008072 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008073 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008074 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008075 ExprResult CalcStep =
8076 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008077 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008078
Alexander Musman8dba6642014-04-22 13:09:42 +00008079 // Warn about zero linear step (it would be probably better specified as
8080 // making corresponding variables 'const').
8081 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008082 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8083 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008084 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8085 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008086 if (!IsConstant && CalcStep.isUsable()) {
8087 // Calculate the step beforehand instead of doing this on each iteration.
8088 // (This is not used if the number of iterations may be kfold-ed).
8089 CalcStepExpr = CalcStep.get();
8090 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008091 }
8092
Alexey Bataev182227b2015-08-20 10:54:39 +00008093 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8094 ColonLoc, EndLoc, Vars, Privates, Inits,
8095 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008096}
8097
8098static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8099 Expr *NumIterations, Sema &SemaRef,
8100 Scope *S) {
8101 // Walk the vars and build update/final expressions for the CodeGen.
8102 SmallVector<Expr *, 8> Updates;
8103 SmallVector<Expr *, 8> Finals;
8104 Expr *Step = Clause.getStep();
8105 Expr *CalcStep = Clause.getCalcStep();
8106 // OpenMP [2.14.3.7, linear clause]
8107 // If linear-step is not specified it is assumed to be 1.
8108 if (Step == nullptr)
8109 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8110 else if (CalcStep)
8111 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8112 bool HasErrors = false;
8113 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008114 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008115 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008116 for (auto &RefExpr : Clause.varlists()) {
8117 Expr *InitExpr = *CurInit;
8118
8119 // Build privatized reference to the current linear var.
8120 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008121 Expr *CapturedRef;
8122 if (LinKind == OMPC_LINEAR_uval)
8123 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8124 else
8125 CapturedRef =
8126 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8127 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8128 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008129
8130 // Build update: Var = InitExpr + IV * Step
8131 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008132 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008133 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008134 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8135 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008136
8137 // Build final: Var = InitExpr + NumIterations * Step
8138 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008139 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008140 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008141 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8142 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008143 if (!Update.isUsable() || !Final.isUsable()) {
8144 Updates.push_back(nullptr);
8145 Finals.push_back(nullptr);
8146 HasErrors = true;
8147 } else {
8148 Updates.push_back(Update.get());
8149 Finals.push_back(Final.get());
8150 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008151 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008152 }
8153 Clause.setUpdates(Updates);
8154 Clause.setFinals(Finals);
8155 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008156}
8157
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008158OMPClause *Sema::ActOnOpenMPAlignedClause(
8159 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8160 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8161
8162 SmallVector<Expr *, 8> Vars;
8163 for (auto &RefExpr : VarList) {
8164 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8165 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8166 // It will be analyzed later.
8167 Vars.push_back(RefExpr);
8168 continue;
8169 }
8170
8171 SourceLocation ELoc = RefExpr->getExprLoc();
8172 // OpenMP [2.1, C/C++]
8173 // A list item is a variable name.
8174 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8175 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008176 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8177 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008178 continue;
8179 }
8180
8181 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8182
8183 // OpenMP [2.8.1, simd construct, Restrictions]
8184 // The type of list items appearing in the aligned clause must be
8185 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008186 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008187 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008188 const Type *Ty = QType.getTypePtrOrNull();
8189 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8190 !Ty->isPointerType())) {
8191 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8192 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8193 bool IsDecl =
8194 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8195 Diag(VD->getLocation(),
8196 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8197 << VD;
8198 continue;
8199 }
8200
8201 // OpenMP [2.8.1, simd construct, Restrictions]
8202 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008203 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008204 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8205 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8206 << getOpenMPClauseName(OMPC_aligned);
8207 continue;
8208 }
8209
8210 Vars.push_back(DE);
8211 }
8212
8213 // OpenMP [2.8.1, simd construct, Description]
8214 // The parameter of the aligned clause, alignment, must be a constant
8215 // positive integer expression.
8216 // If no optional parameter is specified, implementation-defined default
8217 // alignments for SIMD instructions on the target platforms are assumed.
8218 if (Alignment != nullptr) {
8219 ExprResult AlignResult =
8220 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8221 if (AlignResult.isInvalid())
8222 return nullptr;
8223 Alignment = AlignResult.get();
8224 }
8225 if (Vars.empty())
8226 return nullptr;
8227
8228 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8229 EndLoc, Vars, Alignment);
8230}
8231
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008232OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8233 SourceLocation StartLoc,
8234 SourceLocation LParenLoc,
8235 SourceLocation EndLoc) {
8236 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008237 SmallVector<Expr *, 8> SrcExprs;
8238 SmallVector<Expr *, 8> DstExprs;
8239 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008240 for (auto &RefExpr : VarList) {
8241 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8242 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008243 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008244 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008245 SrcExprs.push_back(nullptr);
8246 DstExprs.push_back(nullptr);
8247 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008248 continue;
8249 }
8250
Alexey Bataeved09d242014-05-28 05:53:51 +00008251 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008252 // OpenMP [2.1, C/C++]
8253 // A list item is a variable name.
8254 // OpenMP [2.14.4.1, Restrictions, p.1]
8255 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008256 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008257 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008258 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8259 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008260 continue;
8261 }
8262
8263 Decl *D = DE->getDecl();
8264 VarDecl *VD = cast<VarDecl>(D);
8265
8266 QualType Type = VD->getType();
8267 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8268 // It will be analyzed later.
8269 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008270 SrcExprs.push_back(nullptr);
8271 DstExprs.push_back(nullptr);
8272 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008273 continue;
8274 }
8275
8276 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8277 // A list item that appears in a copyin clause must be threadprivate.
8278 if (!DSAStack->isThreadPrivate(VD)) {
8279 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008280 << getOpenMPClauseName(OMPC_copyin)
8281 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008282 continue;
8283 }
8284
8285 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8286 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008287 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008288 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008289 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008290 auto *SrcVD =
8291 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8292 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008293 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008294 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8295 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008296 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8297 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008298 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008299 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008300 // For arrays generate assignment operation for single element and replace
8301 // it by the original array element in CodeGen.
8302 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8303 PseudoDstExpr, PseudoSrcExpr);
8304 if (AssignmentOp.isInvalid())
8305 continue;
8306 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8307 /*DiscardedValue=*/true);
8308 if (AssignmentOp.isInvalid())
8309 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008310
8311 DSAStack->addDSA(VD, DE, OMPC_copyin);
8312 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008313 SrcExprs.push_back(PseudoSrcExpr);
8314 DstExprs.push_back(PseudoDstExpr);
8315 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008316 }
8317
Alexey Bataeved09d242014-05-28 05:53:51 +00008318 if (Vars.empty())
8319 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008320
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008321 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8322 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008323}
8324
Alexey Bataevbae9a792014-06-27 10:37:06 +00008325OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8326 SourceLocation StartLoc,
8327 SourceLocation LParenLoc,
8328 SourceLocation EndLoc) {
8329 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008330 SmallVector<Expr *, 8> SrcExprs;
8331 SmallVector<Expr *, 8> DstExprs;
8332 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008333 for (auto &RefExpr : VarList) {
8334 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8335 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8336 // It will be analyzed later.
8337 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008338 SrcExprs.push_back(nullptr);
8339 DstExprs.push_back(nullptr);
8340 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008341 continue;
8342 }
8343
8344 SourceLocation ELoc = RefExpr->getExprLoc();
8345 // OpenMP [2.1, C/C++]
8346 // A list item is a variable name.
8347 // OpenMP [2.14.4.1, Restrictions, p.1]
8348 // A list item that appears in a copyin clause must be threadprivate.
8349 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8350 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008351 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8352 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008353 continue;
8354 }
8355
8356 Decl *D = DE->getDecl();
8357 VarDecl *VD = cast<VarDecl>(D);
8358
8359 QualType Type = VD->getType();
8360 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8361 // It will be analyzed later.
8362 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008363 SrcExprs.push_back(nullptr);
8364 DstExprs.push_back(nullptr);
8365 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008366 continue;
8367 }
8368
8369 // OpenMP [2.14.4.2, Restrictions, p.2]
8370 // A list item that appears in a copyprivate clause may not appear in a
8371 // private or firstprivate clause on the single construct.
8372 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008373 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008374 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8375 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008376 Diag(ELoc, diag::err_omp_wrong_dsa)
8377 << getOpenMPClauseName(DVar.CKind)
8378 << getOpenMPClauseName(OMPC_copyprivate);
8379 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8380 continue;
8381 }
8382
8383 // OpenMP [2.11.4.2, Restrictions, p.1]
8384 // All list items that appear in a copyprivate clause must be either
8385 // threadprivate or private in the enclosing context.
8386 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008387 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008388 if (DVar.CKind == OMPC_shared) {
8389 Diag(ELoc, diag::err_omp_required_access)
8390 << getOpenMPClauseName(OMPC_copyprivate)
8391 << "threadprivate or private in the enclosing context";
8392 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8393 continue;
8394 }
8395 }
8396 }
8397
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008398 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008399 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008400 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008401 << getOpenMPClauseName(OMPC_copyprivate) << Type
8402 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008403 bool IsDecl =
8404 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8405 Diag(VD->getLocation(),
8406 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8407 << VD;
8408 continue;
8409 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008410
Alexey Bataevbae9a792014-06-27 10:37:06 +00008411 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8412 // A variable of class type (or array thereof) that appears in a
8413 // copyin clause requires an accessible, unambiguous copy assignment
8414 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008415 Type = Context.getBaseElementType(Type.getNonReferenceType())
8416 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008417 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008418 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8419 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008420 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008421 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008422 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008423 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8424 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008425 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008426 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008427 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8428 PseudoDstExpr, PseudoSrcExpr);
8429 if (AssignmentOp.isInvalid())
8430 continue;
8431 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8432 /*DiscardedValue=*/true);
8433 if (AssignmentOp.isInvalid())
8434 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008435
8436 // No need to mark vars as copyprivate, they are already threadprivate or
8437 // implicitly private.
8438 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008439 SrcExprs.push_back(PseudoSrcExpr);
8440 DstExprs.push_back(PseudoDstExpr);
8441 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008442 }
8443
8444 if (Vars.empty())
8445 return nullptr;
8446
Alexey Bataeva63048e2015-03-23 06:18:07 +00008447 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8448 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008449}
8450
Alexey Bataev6125da92014-07-21 11:26:11 +00008451OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8452 SourceLocation StartLoc,
8453 SourceLocation LParenLoc,
8454 SourceLocation EndLoc) {
8455 if (VarList.empty())
8456 return nullptr;
8457
8458 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8459}
Alexey Bataevdea47612014-07-23 07:46:59 +00008460
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008461OMPClause *
8462Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8463 SourceLocation DepLoc, SourceLocation ColonLoc,
8464 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8465 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008466 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008467 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008468 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008469 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008470 return nullptr;
8471 }
8472 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008473 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8474 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008475 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008476 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008477 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8478 /*Last=*/OMPC_DEPEND_unknown, Except)
8479 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008480 return nullptr;
8481 }
8482 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008483 llvm::APSInt DepCounter(/*BitWidth=*/32);
8484 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8485 if (DepKind == OMPC_DEPEND_sink) {
8486 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8487 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8488 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008489 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008490 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008491 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8492 DSAStack->getParentOrderedRegionParam()) {
8493 for (auto &RefExpr : VarList) {
8494 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8495 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8496 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8497 // It will be analyzed later.
8498 Vars.push_back(RefExpr);
8499 continue;
8500 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008501
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008502 SourceLocation ELoc = RefExpr->getExprLoc();
8503 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8504 if (DepKind == OMPC_DEPEND_sink) {
8505 if (DepCounter >= TotalDepCount) {
8506 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8507 continue;
8508 }
8509 ++DepCounter;
8510 // OpenMP [2.13.9, Summary]
8511 // depend(dependence-type : vec), where dependence-type is:
8512 // 'sink' and where vec is the iteration vector, which has the form:
8513 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8514 // where n is the value specified by the ordered clause in the loop
8515 // directive, xi denotes the loop iteration variable of the i-th nested
8516 // loop associated with the loop directive, and di is a constant
8517 // non-negative integer.
8518 SimpleExpr = SimpleExpr->IgnoreImplicit();
8519 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8520 if (!DE) {
8521 OverloadedOperatorKind OOK = OO_None;
8522 SourceLocation OOLoc;
8523 Expr *LHS, *RHS;
8524 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8525 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8526 OOLoc = BO->getOperatorLoc();
8527 LHS = BO->getLHS()->IgnoreParenImpCasts();
8528 RHS = BO->getRHS()->IgnoreParenImpCasts();
8529 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8530 OOK = OCE->getOperator();
8531 OOLoc = OCE->getOperatorLoc();
8532 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8533 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8534 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8535 OOK = MCE->getMethodDecl()
8536 ->getNameInfo()
8537 .getName()
8538 .getCXXOverloadedOperator();
8539 OOLoc = MCE->getCallee()->getExprLoc();
8540 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8541 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8542 } else {
8543 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8544 continue;
8545 }
8546 DE = dyn_cast<DeclRefExpr>(LHS);
8547 if (!DE) {
8548 Diag(LHS->getExprLoc(),
8549 diag::err_omp_depend_sink_expected_loop_iteration)
8550 << DSAStack->getParentLoopControlVariable(
8551 DepCounter.getZExtValue());
8552 continue;
8553 }
8554 if (OOK != OO_Plus && OOK != OO_Minus) {
8555 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8556 continue;
8557 }
8558 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8559 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8560 if (Res.isInvalid())
8561 continue;
8562 }
8563 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8564 if (!CurContext->isDependentContext() &&
8565 DSAStack->getParentOrderedRegionParam() &&
8566 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8567 Diag(DE->getExprLoc(),
8568 diag::err_omp_depend_sink_expected_loop_iteration)
8569 << DSAStack->getParentLoopControlVariable(
8570 DepCounter.getZExtValue());
8571 continue;
8572 }
8573 } else {
8574 // OpenMP [2.11.1.1, Restrictions, p.3]
8575 // A variable that is part of another variable (such as a field of a
8576 // structure) but is not an array element or an array section cannot
8577 // appear in a depend clause.
8578 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8579 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8580 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8581 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8582 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8583 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8584 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008585 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8586 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008587 continue;
8588 }
8589 }
8590
8591 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8592 }
8593
8594 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8595 TotalDepCount > VarList.size() &&
8596 DSAStack->getParentOrderedRegionParam()) {
8597 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8598 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8599 }
8600 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8601 Vars.empty())
8602 return nullptr;
8603 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008604
8605 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8606 DepLoc, ColonLoc, Vars);
8607}
Michael Wonge710d542015-08-07 16:16:36 +00008608
8609OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8610 SourceLocation LParenLoc,
8611 SourceLocation EndLoc) {
8612 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008613
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008614 // OpenMP [2.9.1, Restrictions]
8615 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008616 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8617 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008618 return nullptr;
8619
Michael Wonge710d542015-08-07 16:16:36 +00008620 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8621}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008622
8623static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8624 DSAStackTy *Stack, CXXRecordDecl *RD) {
8625 if (!RD || RD->isInvalidDecl())
8626 return true;
8627
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008628 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8629 if (auto *CTD = CTSD->getSpecializedTemplate())
8630 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008631 auto QTy = SemaRef.Context.getRecordType(RD);
8632 if (RD->isDynamicClass()) {
8633 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8634 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8635 return false;
8636 }
8637 auto *DC = RD;
8638 bool IsCorrect = true;
8639 for (auto *I : DC->decls()) {
8640 if (I) {
8641 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8642 if (MD->isStatic()) {
8643 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8644 SemaRef.Diag(MD->getLocation(),
8645 diag::note_omp_static_member_in_target);
8646 IsCorrect = false;
8647 }
8648 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8649 if (VD->isStaticDataMember()) {
8650 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8651 SemaRef.Diag(VD->getLocation(),
8652 diag::note_omp_static_member_in_target);
8653 IsCorrect = false;
8654 }
8655 }
8656 }
8657 }
8658
8659 for (auto &I : RD->bases()) {
8660 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8661 I.getType()->getAsCXXRecordDecl()))
8662 IsCorrect = false;
8663 }
8664 return IsCorrect;
8665}
8666
8667static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8668 DSAStackTy *Stack, QualType QTy) {
8669 NamedDecl *ND;
8670 if (QTy->isIncompleteType(&ND)) {
8671 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8672 return false;
8673 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8674 if (!RD->isInvalidDecl() &&
8675 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8676 return false;
8677 }
8678 return true;
8679}
8680
Samuel Antao5de996e2016-01-22 20:21:36 +00008681// Return the expression of the base of the map clause or null if it cannot
8682// be determined and do all the necessary checks to see if the expression is
8683// valid as a standalone map clause expression.
8684static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8685 SourceLocation ELoc = E->getExprLoc();
8686 SourceRange ERange = E->getSourceRange();
8687
8688 // The base of elements of list in a map clause have to be either:
8689 // - a reference to variable or field.
8690 // - a member expression.
8691 // - an array expression.
8692 //
8693 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8694 // reference to 'r'.
8695 //
8696 // If we have:
8697 //
8698 // struct SS {
8699 // Bla S;
8700 // foo() {
8701 // #pragma omp target map (S.Arr[:12]);
8702 // }
8703 // }
8704 //
8705 // We want to retrieve the member expression 'this->S';
8706
8707 Expr *RelevantExpr = nullptr;
8708
8709 // Flags to help capture some memory
8710
8711 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8712 // If a list item is an array section, it must specify contiguous storage.
8713 //
8714 // For this restriction it is sufficient that we make sure only references
8715 // to variables or fields and array expressions, and that no array sections
8716 // exist except in the rightmost expression. E.g. these would be invalid:
8717 //
8718 // r.ArrS[3:5].Arr[6:7]
8719 //
8720 // r.ArrS[3:5].x
8721 //
8722 // but these would be valid:
8723 // r.ArrS[3].Arr[6:7]
8724 //
8725 // r.ArrS[3].x
8726
8727 bool IsRightMostExpression = true;
8728
8729 while (!RelevantExpr) {
8730 auto AllowArraySection = IsRightMostExpression;
8731 IsRightMostExpression = false;
8732
8733 E = E->IgnoreParenImpCasts();
8734
8735 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8736 if (!isa<VarDecl>(CurE->getDecl()))
8737 break;
8738
8739 RelevantExpr = CurE;
8740 continue;
8741 }
8742
8743 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8744 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8745
8746 if (isa<CXXThisExpr>(BaseE))
8747 // We found a base expression: this->Val.
8748 RelevantExpr = CurE;
8749 else
8750 E = BaseE;
8751
8752 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8753 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8754 << CurE->getSourceRange();
8755 break;
8756 }
8757
8758 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8759
8760 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8761 // A bit-field cannot appear in a map clause.
8762 //
8763 if (FD->isBitField()) {
8764 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8765 << CurE->getSourceRange();
8766 break;
8767 }
8768
8769 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8770 // If the type of a list item is a reference to a type T then the type
8771 // will be considered to be T for all purposes of this clause.
8772 QualType CurType = BaseE->getType();
8773 if (CurType->isReferenceType())
8774 CurType = CurType->getPointeeType();
8775
8776 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8777 // A list item cannot be a variable that is a member of a structure with
8778 // a union type.
8779 //
8780 if (auto *RT = CurType->getAs<RecordType>())
8781 if (RT->isUnionType()) {
8782 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
8783 << CurE->getSourceRange();
8784 break;
8785 }
8786
8787 continue;
8788 }
8789
8790 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
8791 E = CurE->getBase()->IgnoreParenImpCasts();
8792
8793 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
8794 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8795 << 0 << CurE->getSourceRange();
8796 break;
8797 }
8798 continue;
8799 }
8800
8801 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
8802 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
8803 // If a list item is an element of a structure, only the rightmost symbol
8804 // of the variable reference can be an array section.
8805 //
8806 if (!AllowArraySection) {
8807 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
8808 << CurE->getSourceRange();
8809 break;
8810 }
8811
8812 E = CurE->getBase()->IgnoreParenImpCasts();
8813
8814 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8815 // If the type of a list item is a reference to a type T then the type
8816 // will be considered to be T for all purposes of this clause.
8817 QualType CurType = E->getType();
8818 if (CurType->isReferenceType())
8819 CurType = CurType->getPointeeType();
8820
8821 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
8822 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8823 << 0 << CurE->getSourceRange();
8824 break;
8825 }
8826
8827 continue;
8828 }
8829
8830 // If nothing else worked, this is not a valid map clause expression.
8831 SemaRef.Diag(ELoc,
8832 diag::err_omp_expected_named_var_member_or_array_expression)
8833 << ERange;
8834 break;
8835 }
8836
8837 return RelevantExpr;
8838}
8839
8840// Return true if expression E associated with value VD has conflicts with other
8841// map information.
8842static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
8843 Expr *E, bool CurrentRegionOnly) {
8844 assert(VD && E);
8845
8846 // Types used to organize the components of a valid map clause.
8847 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
8848 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
8849
8850 // Helper to extract the components in the map clause expression E and store
8851 // them into MEC. This assumes that E is a valid map clause expression, i.e.
8852 // it has already passed the single clause checks.
8853 auto ExtractMapExpressionComponents = [](Expr *TE,
8854 MapExpressionComponents &MEC) {
8855 while (true) {
8856 TE = TE->IgnoreParenImpCasts();
8857
8858 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
8859 MEC.push_back(
8860 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
8861 break;
8862 }
8863
8864 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
8865 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8866
8867 MEC.push_back(MapExpressionComponent(
8868 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
8869 if (isa<CXXThisExpr>(BaseE))
8870 break;
8871
8872 TE = BaseE;
8873 continue;
8874 }
8875
8876 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
8877 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8878 TE = CurE->getBase()->IgnoreParenImpCasts();
8879 continue;
8880 }
8881
8882 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
8883 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8884 TE = CurE->getBase()->IgnoreParenImpCasts();
8885 continue;
8886 }
8887
8888 llvm_unreachable(
8889 "Expecting only valid map clause expressions at this point!");
8890 }
8891 };
8892
8893 SourceLocation ELoc = E->getExprLoc();
8894 SourceRange ERange = E->getSourceRange();
8895
8896 // In order to easily check the conflicts we need to match each component of
8897 // the expression under test with the components of the expressions that are
8898 // already in the stack.
8899
8900 MapExpressionComponents CurComponents;
8901 ExtractMapExpressionComponents(E, CurComponents);
8902
8903 assert(!CurComponents.empty() && "Map clause expression with no components!");
8904 assert(CurComponents.back().second == VD &&
8905 "Map clause expression with unexpected base!");
8906
8907 // Variables to help detecting enclosing problems in data environment nests.
8908 bool IsEnclosedByDataEnvironmentExpr = false;
8909 Expr *EnclosingExpr = nullptr;
8910
8911 bool FoundError =
8912 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
8913 MapExpressionComponents StackComponents;
8914 ExtractMapExpressionComponents(RE, StackComponents);
8915 assert(!StackComponents.empty() &&
8916 "Map clause expression with no components!");
8917 assert(StackComponents.back().second == VD &&
8918 "Map clause expression with unexpected base!");
8919
8920 // Expressions must start from the same base. Here we detect at which
8921 // point both expressions diverge from each other and see if we can
8922 // detect if the memory referred to both expressions is contiguous and
8923 // do not overlap.
8924 auto CI = CurComponents.rbegin();
8925 auto CE = CurComponents.rend();
8926 auto SI = StackComponents.rbegin();
8927 auto SE = StackComponents.rend();
8928 for (; CI != CE && SI != SE; ++CI, ++SI) {
8929
8930 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
8931 // At most one list item can be an array item derived from a given
8932 // variable in map clauses of the same construct.
8933 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
8934 isa<OMPArraySectionExpr>(CI->first)) &&
8935 (isa<ArraySubscriptExpr>(SI->first) ||
8936 isa<OMPArraySectionExpr>(SI->first))) {
8937 SemaRef.Diag(CI->first->getExprLoc(),
8938 diag::err_omp_multiple_array_items_in_map_clause)
8939 << CI->first->getSourceRange();
8940 ;
8941 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
8942 << SI->first->getSourceRange();
8943 return true;
8944 }
8945
8946 // Do both expressions have the same kind?
8947 if (CI->first->getStmtClass() != SI->first->getStmtClass())
8948 break;
8949
8950 // Are we dealing with different variables/fields?
8951 if (CI->second != SI->second)
8952 break;
8953 }
8954
8955 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8956 // List items of map clauses in the same construct must not share
8957 // original storage.
8958 //
8959 // If the expressions are exactly the same or one is a subset of the
8960 // other, it means they are sharing storage.
8961 if (CI == CE && SI == SE) {
8962 if (CurrentRegionOnly) {
8963 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8964 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8965 << RE->getSourceRange();
8966 return true;
8967 } else {
8968 // If we find the same expression in the enclosing data environment,
8969 // that is legal.
8970 IsEnclosedByDataEnvironmentExpr = true;
8971 return false;
8972 }
8973 }
8974
8975 QualType DerivedType = std::prev(CI)->first->getType();
8976 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
8977
8978 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8979 // If the type of a list item is a reference to a type T then the type
8980 // will be considered to be T for all purposes of this clause.
8981 if (DerivedType->isReferenceType())
8982 DerivedType = DerivedType->getPointeeType();
8983
8984 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
8985 // A variable for which the type is pointer and an array section
8986 // derived from that variable must not appear as list items of map
8987 // clauses of the same construct.
8988 //
8989 // Also, cover one of the cases in:
8990 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
8991 // If any part of the original storage of a list item has corresponding
8992 // storage in the device data environment, all of the original storage
8993 // must have corresponding storage in the device data environment.
8994 //
8995 if (DerivedType->isAnyPointerType()) {
8996 if (CI == CE || SI == SE) {
8997 SemaRef.Diag(
8998 DerivedLoc,
8999 diag::err_omp_pointer_mapped_along_with_derived_section)
9000 << DerivedLoc;
9001 } else {
9002 assert(CI != CE && SI != SE);
9003 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9004 << DerivedLoc;
9005 }
9006 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9007 << RE->getSourceRange();
9008 return true;
9009 }
9010
9011 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9012 // List items of map clauses in the same construct must not share
9013 // original storage.
9014 //
9015 // An expression is a subset of the other.
9016 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9017 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9018 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9019 << RE->getSourceRange();
9020 return true;
9021 }
9022
9023 // The current expression uses the same base as other expression in the
9024 // data environment but does not contain it completelly.
9025 if (!CurrentRegionOnly && SI != SE)
9026 EnclosingExpr = RE;
9027
9028 // The current expression is a subset of the expression in the data
9029 // environment.
9030 IsEnclosedByDataEnvironmentExpr |=
9031 (!CurrentRegionOnly && CI != CE && SI == SE);
9032
9033 return false;
9034 });
9035
9036 if (CurrentRegionOnly)
9037 return FoundError;
9038
9039 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9040 // If any part of the original storage of a list item has corresponding
9041 // storage in the device data environment, all of the original storage must
9042 // have corresponding storage in the device data environment.
9043 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9044 // If a list item is an element of a structure, and a different element of
9045 // the structure has a corresponding list item in the device data environment
9046 // prior to a task encountering the construct associated with the map clause,
9047 // then the list item must also have a correspnding list item in the device
9048 // data environment prior to the task encountering the construct.
9049 //
9050 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9051 SemaRef.Diag(ELoc,
9052 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9053 << ERange;
9054 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9055 << EnclosingExpr->getSourceRange();
9056 return true;
9057 }
9058
9059 return FoundError;
9060}
9061
Samuel Antao23abd722016-01-19 20:40:49 +00009062OMPClause *
9063Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9064 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9065 SourceLocation MapLoc, SourceLocation ColonLoc,
9066 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9067 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009068 SmallVector<Expr *, 4> Vars;
9069
9070 for (auto &RE : VarList) {
9071 assert(RE && "Null expr in omp map");
9072 if (isa<DependentScopeDeclRefExpr>(RE)) {
9073 // It will be analyzed later.
9074 Vars.push_back(RE);
9075 continue;
9076 }
9077 SourceLocation ELoc = RE->getExprLoc();
9078
Kelvin Li0bff7af2015-11-23 05:32:03 +00009079 auto *VE = RE->IgnoreParenLValueCasts();
9080
9081 if (VE->isValueDependent() || VE->isTypeDependent() ||
9082 VE->isInstantiationDependent() ||
9083 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009084 // We can only analyze this information once the missing information is
9085 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009086 Vars.push_back(RE);
9087 continue;
9088 }
9089
9090 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009091
Samuel Antao5de996e2016-01-22 20:21:36 +00009092 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9093 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9094 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009095 continue;
9096 }
9097
Samuel Antao5de996e2016-01-22 20:21:36 +00009098 // Obtain the array or member expression bases if required.
9099 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9100 if (!BE)
9101 continue;
9102
9103 // If the base is a reference to a variable, we rely on that variable for
9104 // the following checks. If it is a 'this' expression we rely on the field.
9105 ValueDecl *D = nullptr;
9106 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9107 D = DRE->getDecl();
9108 } else {
9109 auto *ME = cast<MemberExpr>(BE);
9110 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9111 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009112 }
9113 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009114
Samuel Antao5de996e2016-01-22 20:21:36 +00009115 auto *VD = dyn_cast<VarDecl>(D);
9116 auto *FD = dyn_cast<FieldDecl>(D);
9117
9118 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009119 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009120
9121 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9122 // threadprivate variables cannot appear in a map clause.
9123 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009124 auto DVar = DSAStack->getTopDSA(VD, false);
9125 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9126 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9127 continue;
9128 }
9129
Samuel Antao5de996e2016-01-22 20:21:36 +00009130 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9131 // A list item cannot appear in both a map clause and a data-sharing
9132 // attribute clause on the same construct.
9133 //
9134 // TODO: Implement this check - it cannot currently be tested because of
9135 // missing implementation of the other data sharing clauses in target
9136 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009137
Samuel Antao5de996e2016-01-22 20:21:36 +00009138 // Check conflicts with other map clause expressions. We check the conflicts
9139 // with the current construct separately from the enclosing data
9140 // environment, because the restrictions are different.
9141 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9142 /*CurrentRegionOnly=*/true))
9143 break;
9144 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9145 /*CurrentRegionOnly=*/false))
9146 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009147
Samuel Antao5de996e2016-01-22 20:21:36 +00009148 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9149 // If the type of a list item is a reference to a type T then the type will
9150 // be considered to be T for all purposes of this clause.
9151 QualType Type = D->getType();
9152 if (Type->isReferenceType())
9153 Type = Type->getPointeeType();
9154
9155 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009156 // A list item must have a mappable type.
9157 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9158 DSAStack, Type))
9159 continue;
9160
Samuel Antaodf67fc42016-01-19 19:15:56 +00009161 // target enter data
9162 // OpenMP [2.10.2, Restrictions, p. 99]
9163 // A map-type must be specified in all map clauses and must be either
9164 // to or alloc.
9165 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9166 if (DKind == OMPD_target_enter_data &&
9167 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9168 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009169 << (IsMapTypeImplicit ? 1 : 0)
9170 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009171 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009172 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009173 }
9174
Samuel Antao72590762016-01-19 20:04:50 +00009175 // target exit_data
9176 // OpenMP [2.10.3, Restrictions, p. 102]
9177 // A map-type must be specified in all map clauses and must be either
9178 // from, release, or delete.
9179 DKind = DSAStack->getCurrentDirective();
9180 if (DKind == OMPD_target_exit_data &&
9181 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9182 MapType == OMPC_MAP_delete)) {
9183 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009184 << (IsMapTypeImplicit ? 1 : 0)
9185 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009186 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009187 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009188 }
9189
Kelvin Li0bff7af2015-11-23 05:32:03 +00009190 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009191 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009192 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009193
Samuel Antao5de996e2016-01-22 20:21:36 +00009194 // We need to produce a map clause even if we don't have variables so that
9195 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009196 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009197 MapTypeModifier, MapType, IsMapTypeImplicit,
9198 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009199}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009200
9201OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9202 SourceLocation StartLoc,
9203 SourceLocation LParenLoc,
9204 SourceLocation EndLoc) {
9205 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009206
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009207 // OpenMP [teams Constrcut, Restrictions]
9208 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009209 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9210 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009211 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009212
9213 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9214}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009215
9216OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9217 SourceLocation StartLoc,
9218 SourceLocation LParenLoc,
9219 SourceLocation EndLoc) {
9220 Expr *ValExpr = ThreadLimit;
9221
9222 // OpenMP [teams Constrcut, Restrictions]
9223 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009224 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9225 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009226 return nullptr;
9227
9228 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9229 EndLoc);
9230}
Alexey Bataeva0569352015-12-01 10:17:31 +00009231
9232OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9233 SourceLocation StartLoc,
9234 SourceLocation LParenLoc,
9235 SourceLocation EndLoc) {
9236 Expr *ValExpr = Priority;
9237
9238 // OpenMP [2.9.1, task Constrcut]
9239 // The priority-value is a non-negative numerical scalar expression.
9240 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9241 /*StrictlyPositive=*/false))
9242 return nullptr;
9243
9244 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9245}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009246
9247OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9248 SourceLocation StartLoc,
9249 SourceLocation LParenLoc,
9250 SourceLocation EndLoc) {
9251 Expr *ValExpr = Grainsize;
9252
9253 // OpenMP [2.9.2, taskloop Constrcut]
9254 // The parameter of the grainsize clause must be a positive integer
9255 // expression.
9256 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9257 /*StrictlyPositive=*/true))
9258 return nullptr;
9259
9260 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9261}
Alexey Bataev382967a2015-12-08 12:06:20 +00009262
9263OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9264 SourceLocation StartLoc,
9265 SourceLocation LParenLoc,
9266 SourceLocation EndLoc) {
9267 Expr *ValExpr = NumTasks;
9268
9269 // OpenMP [2.9.2, taskloop Constrcut]
9270 // The parameter of the num_tasks clause must be a positive integer
9271 // expression.
9272 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9273 /*StrictlyPositive=*/true))
9274 return nullptr;
9275
9276 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9277}
9278
Alexey Bataev28c75412015-12-15 08:19:24 +00009279OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9280 SourceLocation LParenLoc,
9281 SourceLocation EndLoc) {
9282 // OpenMP [2.13.2, critical construct, Description]
9283 // ... where hint-expression is an integer constant expression that evaluates
9284 // to a valid lock hint.
9285 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9286 if (HintExpr.isInvalid())
9287 return nullptr;
9288 return new (Context)
9289 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9290}
9291
Carlo Bertollib4adf552016-01-15 18:50:31 +00009292OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9293 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9294 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9295 SourceLocation EndLoc) {
9296 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9297 std::string Values;
9298 Values += "'";
9299 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9300 Values += "'";
9301 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9302 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9303 return nullptr;
9304 }
9305 Expr *ValExpr = ChunkSize;
9306 Expr *HelperValExpr = nullptr;
9307 if (ChunkSize) {
9308 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9309 !ChunkSize->isInstantiationDependent() &&
9310 !ChunkSize->containsUnexpandedParameterPack()) {
9311 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9312 ExprResult Val =
9313 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9314 if (Val.isInvalid())
9315 return nullptr;
9316
9317 ValExpr = Val.get();
9318
9319 // OpenMP [2.7.1, Restrictions]
9320 // chunk_size must be a loop invariant integer expression with a positive
9321 // value.
9322 llvm::APSInt Result;
9323 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9324 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9325 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9326 << "dist_schedule" << ChunkSize->getSourceRange();
9327 return nullptr;
9328 }
9329 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9330 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9331 ChunkSize->getType(), ".chunk.");
9332 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9333 ChunkSize->getExprLoc(),
9334 /*RefersToCapture=*/true);
9335 HelperValExpr = ImpVarRef;
9336 }
9337 }
9338 }
9339
9340 return new (Context)
9341 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9342 Kind, ValExpr, HelperValExpr);
9343}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009344
9345OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9346 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9347 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9348 SourceLocation KindLoc, SourceLocation EndLoc) {
9349 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9350 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9351 Kind != OMPC_DEFAULTMAP_scalar) {
9352 std::string Value;
9353 SourceLocation Loc;
9354 Value += "'";
9355 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9356 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9357 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9358 Loc = MLoc;
9359 } else {
9360 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9361 OMPC_DEFAULTMAP_scalar);
9362 Loc = KindLoc;
9363 }
9364 Value += "'";
9365 Diag(Loc, diag::err_omp_unexpected_clause_value)
9366 << Value << getOpenMPClauseName(OMPC_defaultmap);
9367 return nullptr;
9368 }
9369
9370 return new (Context)
9371 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9372}