blob: 1300de10b295c3b44194cf401ce40b1fed883816 [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 Bataev18eb25e2014-06-30 10:22:46 +00001928 // | simd | simd | |
1929 // | simd | sections | |
1930 // | simd | section | |
1931 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001932 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001933 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001935 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001936 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001937 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001938 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001939 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001940 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001941 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001942 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001943 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001944 // | simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001945 // | simd | target enter | |
1946 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001947 // | simd | target exit | |
1948 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001949 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001950 // | simd | cancellation | |
1951 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001952 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001953 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001954 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001955 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001956 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001957 // | for simd | parallel | |
1958 // | for simd | for | |
1959 // | for simd | for simd | |
1960 // | for simd | master | |
1961 // | for simd | critical | |
1962 // | for simd | simd | |
1963 // | for simd | sections | |
1964 // | for simd | section | |
1965 // | for simd | single | |
1966 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001967 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001968 // | for simd |parallel sections| |
1969 // | for simd | task | |
1970 // | for simd | taskyield | |
1971 // | for simd | barrier | |
1972 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001973 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001974 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001975 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001976 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001977 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001978 // | for simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001979 // | for simd | target enter | |
1980 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001981 // | for simd | target exit | |
1982 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001983 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001984 // | for simd | cancellation | |
1985 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001986 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001987 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001988 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001989 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001990 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001991 // | parallel for simd| parallel | |
1992 // | parallel for simd| for | |
1993 // | parallel for simd| for simd | |
1994 // | parallel for simd| master | |
1995 // | parallel for simd| critical | |
1996 // | parallel for simd| simd | |
1997 // | parallel for simd| sections | |
1998 // | parallel for simd| section | |
1999 // | parallel for simd| single | |
2000 // | parallel for simd| parallel for | |
2001 // | parallel for simd|parallel for simd| |
2002 // | parallel for simd|parallel sections| |
2003 // | parallel for simd| task | |
2004 // | parallel for simd| taskyield | |
2005 // | parallel for simd| barrier | |
2006 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002007 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002008 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002009 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002010 // | parallel for simd| atomic | |
2011 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002012 // | parallel for simd| target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002013 // | parallel for simd| target enter | |
2014 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002015 // | parallel for simd| target exit | |
2016 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002017 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002018 // | parallel for simd| cancellation | |
2019 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002020 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002021 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002022 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002023 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002024 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002025 // | sections | parallel | * |
2026 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002027 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002028 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002029 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002030 // | sections | simd | * |
2031 // | sections | sections | + |
2032 // | sections | section | * |
2033 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002034 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002035 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002036 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002037 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002038 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002039 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002040 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002041 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002042 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002043 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002044 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002045 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002046 // | sections | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002047 // | sections | target enter | * |
2048 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002049 // | sections | target exit | * |
2050 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002051 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002052 // | sections | cancellation | |
2053 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002054 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002055 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002056 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002057 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002058 // +------------------+-----------------+------------------------------------+
2059 // | section | parallel | * |
2060 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002061 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002062 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002063 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002064 // | section | simd | * |
2065 // | section | sections | + |
2066 // | section | section | + |
2067 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002068 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002069 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002070 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002071 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002072 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002073 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002074 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002075 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002076 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002077 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002078 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002079 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002080 // | section | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002081 // | section | target enter | * |
2082 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002083 // | section | target exit | * |
2084 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002085 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002086 // | section | cancellation | |
2087 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002088 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002089 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002090 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002091 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002092 // +------------------+-----------------+------------------------------------+
2093 // | single | parallel | * |
2094 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002095 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002096 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002097 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002098 // | single | simd | * |
2099 // | single | sections | + |
2100 // | single | section | + |
2101 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002102 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002103 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002104 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002105 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002106 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002107 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002108 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002109 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002110 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002111 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002112 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002113 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002114 // | single | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002115 // | single | target enter | * |
2116 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002117 // | single | target exit | * |
2118 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002119 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002120 // | single | cancellation | |
2121 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002122 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002123 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002124 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002125 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002126 // +------------------+-----------------+------------------------------------+
2127 // | parallel for | parallel | * |
2128 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002129 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002130 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002131 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002132 // | parallel for | simd | * |
2133 // | parallel for | sections | + |
2134 // | parallel for | section | + |
2135 // | parallel for | single | + |
2136 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002137 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002138 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002139 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002140 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002141 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002142 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002143 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002144 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002145 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002146 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002147 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002148 // | parallel for | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002149 // | parallel for | target enter | * |
2150 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002151 // | parallel for | target exit | * |
2152 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002153 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002154 // | parallel for | cancellation | |
2155 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002156 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002157 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002158 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002159 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002160 // +------------------+-----------------+------------------------------------+
2161 // | parallel sections| parallel | * |
2162 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002163 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002164 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002165 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002166 // | parallel sections| simd | * |
2167 // | parallel sections| sections | + |
2168 // | parallel sections| section | * |
2169 // | parallel sections| single | + |
2170 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002171 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002172 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002173 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002174 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002175 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002176 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002177 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002178 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002179 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002180 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002181 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002182 // | parallel sections| target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002183 // | parallel sections| target enter | * |
2184 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002185 // | parallel sections| target exit | * |
2186 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002187 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002188 // | parallel sections| cancellation | |
2189 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002190 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002191 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002192 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002193 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002194 // +------------------+-----------------+------------------------------------+
2195 // | task | parallel | * |
2196 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002197 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002198 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002199 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002200 // | task | simd | * |
2201 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002202 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002203 // | task | single | + |
2204 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002205 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002206 // | task |parallel sections| * |
2207 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002208 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002209 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002210 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002211 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002212 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002213 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002214 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002215 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002216 // | task | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002217 // | task | target enter | * |
2218 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002219 // | task | target exit | * |
2220 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002221 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002222 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002223 // | | point | ! |
2224 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002225 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002226 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002227 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002228 // +------------------+-----------------+------------------------------------+
2229 // | ordered | parallel | * |
2230 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002231 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002232 // | ordered | master | * |
2233 // | ordered | critical | * |
2234 // | ordered | simd | * |
2235 // | ordered | sections | + |
2236 // | ordered | section | + |
2237 // | ordered | single | + |
2238 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002239 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002240 // | ordered |parallel sections| * |
2241 // | ordered | task | * |
2242 // | ordered | taskyield | * |
2243 // | ordered | barrier | + |
2244 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002245 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002246 // | ordered | flush | * |
2247 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002248 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002249 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002250 // | ordered | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002251 // | ordered | target enter | * |
2252 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002253 // | ordered | target exit | * |
2254 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002255 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002256 // | ordered | cancellation | |
2257 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002258 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002259 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002260 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002261 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002262 // +------------------+-----------------+------------------------------------+
2263 // | atomic | parallel | |
2264 // | atomic | for | |
2265 // | atomic | for simd | |
2266 // | atomic | master | |
2267 // | atomic | critical | |
2268 // | atomic | simd | |
2269 // | atomic | sections | |
2270 // | atomic | section | |
2271 // | atomic | single | |
2272 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002273 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002274 // | atomic |parallel sections| |
2275 // | atomic | task | |
2276 // | atomic | taskyield | |
2277 // | atomic | barrier | |
2278 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002279 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002280 // | atomic | flush | |
2281 // | atomic | ordered | |
2282 // | atomic | atomic | |
2283 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002284 // | atomic | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002285 // | atomic | target enter | |
2286 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002287 // | atomic | target exit | |
2288 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002289 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002290 // | atomic | cancellation | |
2291 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002292 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002293 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002294 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002295 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002296 // +------------------+-----------------+------------------------------------+
2297 // | target | parallel | * |
2298 // | target | for | * |
2299 // | target | for simd | * |
2300 // | target | master | * |
2301 // | target | critical | * |
2302 // | target | simd | * |
2303 // | target | sections | * |
2304 // | target | section | * |
2305 // | target | single | * |
2306 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002307 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002308 // | target |parallel sections| * |
2309 // | target | task | * |
2310 // | target | taskyield | * |
2311 // | target | barrier | * |
2312 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002313 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002314 // | target | flush | * |
2315 // | target | ordered | * |
2316 // | target | atomic | * |
Arpith Chacko 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 | |
2437 // | taskloop simd | simd | |
2438 // | taskloop simd | sections | |
2439 // | taskloop simd | section | |
2440 // | taskloop simd | single | |
2441 // | taskloop simd | parallel for | |
2442 // | taskloop simd |parallel for simd| |
2443 // | taskloop simd |parallel sections| |
2444 // | taskloop simd | task | |
2445 // | taskloop simd | taskyield | |
2446 // | taskloop simd | barrier | |
2447 // | taskloop simd | taskwait | |
2448 // | taskloop simd | taskgroup | |
2449 // | taskloop simd | flush | |
2450 // | taskloop simd | ordered | + (with simd clause) |
2451 // | taskloop simd | atomic | |
2452 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002453 // | taskloop simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002454 // | taskloop simd | target enter | |
2455 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002456 // | taskloop simd | target exit | |
2457 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002458 // | taskloop simd | teams | |
2459 // | taskloop simd | cancellation | |
2460 // | | point | |
2461 // | taskloop simd | cancel | |
2462 // | taskloop simd | taskloop | |
2463 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002464 // | taskloop simd | distribute | |
2465 // +------------------+-----------------+------------------------------------+
2466 // | distribute | parallel | * |
2467 // | distribute | for | * |
2468 // | distribute | for simd | * |
2469 // | distribute | master | * |
2470 // | distribute | critical | * |
2471 // | distribute | simd | * |
2472 // | distribute | sections | * |
2473 // | distribute | section | * |
2474 // | distribute | single | * |
2475 // | distribute | parallel for | * |
2476 // | distribute |parallel for simd| * |
2477 // | distribute |parallel sections| * |
2478 // | distribute | task | * |
2479 // | distribute | taskyield | * |
2480 // | distribute | barrier | * |
2481 // | distribute | taskwait | * |
2482 // | distribute | taskgroup | * |
2483 // | distribute | flush | * |
2484 // | distribute | ordered | + |
2485 // | distribute | atomic | * |
2486 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002487 // | distribute | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002488 // | distribute | target enter | |
2489 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002490 // | distribute | target exit | |
2491 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002492 // | distribute | teams | |
2493 // | distribute | cancellation | + |
2494 // | | point | |
2495 // | distribute | cancel | + |
2496 // | distribute | taskloop | * |
2497 // | distribute | taskloop simd | * |
2498 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002499 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002500 if (Stack->getCurScope()) {
2501 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko 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 Bataevd14d1e62015-09-28 06:39:35 +00002512 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002513 // OpenMP [2.16, Nesting of Regions]
2514 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002515 // OpenMP [2.8.1,simd Construct, Restrictions]
2516 // An ordered construct with the simd clause is the only OpenMP construct
2517 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002518 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2519 return true;
2520 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002521 if (ParentRegion == OMPD_atomic) {
2522 // OpenMP [2.16, Nesting of Regions]
2523 // OpenMP constructs may not be nested inside an atomic region.
2524 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2525 return true;
2526 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002527 if (CurrentRegion == OMPD_section) {
2528 // OpenMP [2.7.2, sections Construct, Restrictions]
2529 // Orphaned section directives are prohibited. That is, the section
2530 // directives must appear within the sections construct and must not be
2531 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002532 if (ParentRegion != OMPD_sections &&
2533 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002534 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2535 << (ParentRegion != OMPD_unknown)
2536 << getOpenMPDirectiveName(ParentRegion);
2537 return true;
2538 }
2539 return false;
2540 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002541 // Allow some constructs to be orphaned (they could be used in functions,
2542 // called from OpenMP regions with the required preconditions).
2543 if (ParentRegion == OMPD_unknown)
2544 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002545 if (CurrentRegion == OMPD_cancellation_point ||
2546 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002547 // OpenMP [2.16, Nesting of Regions]
2548 // A cancellation point construct for which construct-type-clause is
2549 // taskgroup must be nested inside a task construct. A cancellation
2550 // point construct for which construct-type-clause is not taskgroup must
2551 // be closely nested inside an OpenMP construct that matches the type
2552 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002553 // A cancel construct for which construct-type-clause is taskgroup must be
2554 // nested inside a task construct. A cancel construct for which
2555 // construct-type-clause is not taskgroup must be closely nested inside an
2556 // OpenMP construct that matches the type specified in
2557 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002558 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002559 !((CancelRegion == OMPD_parallel &&
2560 (ParentRegion == OMPD_parallel ||
2561 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002562 (CancelRegion == OMPD_for &&
2563 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002564 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2565 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002566 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2567 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002568 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002569 // OpenMP [2.16, Nesting of Regions]
2570 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002571 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002572 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002573 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002574 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002575 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2576 // OpenMP [2.16, Nesting of Regions]
2577 // A critical region may not be nested (closely or otherwise) inside a
2578 // critical region with the same name. Note that this restriction is not
2579 // sufficient to prevent deadlock.
2580 SourceLocation PreviousCriticalLoc;
2581 bool DeadLock =
2582 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2583 OpenMPDirectiveKind K,
2584 const DeclarationNameInfo &DNI,
2585 SourceLocation Loc)
2586 ->bool {
2587 if (K == OMPD_critical &&
2588 DNI.getName() == CurrentName.getName()) {
2589 PreviousCriticalLoc = Loc;
2590 return true;
2591 } else
2592 return false;
2593 },
2594 false /* skip top directive */);
2595 if (DeadLock) {
2596 SemaRef.Diag(StartLoc,
2597 diag::err_omp_prohibited_region_critical_same_name)
2598 << CurrentName.getName();
2599 if (PreviousCriticalLoc.isValid())
2600 SemaRef.Diag(PreviousCriticalLoc,
2601 diag::note_omp_previous_critical_region);
2602 return true;
2603 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002604 } else if (CurrentRegion == OMPD_barrier) {
2605 // OpenMP [2.16, Nesting of Regions]
2606 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002607 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002608 NestingProhibited =
2609 isOpenMPWorksharingDirective(ParentRegion) ||
2610 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002611 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002612 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002613 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002614 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002615 // OpenMP [2.16, Nesting of Regions]
2616 // A worksharing region may not be closely nested inside a worksharing,
2617 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002618 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002619 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002620 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002621 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002622 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002623 Recommend = ShouldBeInParallelRegion;
2624 } else if (CurrentRegion == OMPD_ordered) {
2625 // OpenMP [2.16, Nesting of Regions]
2626 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002627 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002628 // An ordered region must be closely nested inside a loop region (or
2629 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002630 // OpenMP [2.8.1,simd Construct, Restrictions]
2631 // An ordered construct with the simd clause is the only OpenMP construct
2632 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002633 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002634 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002635 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002636 !(isOpenMPSimdDirective(ParentRegion) ||
2637 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002638 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002639 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2640 // OpenMP [2.16, Nesting of Regions]
2641 // If specified, a teams construct must be contained within a target
2642 // construct.
2643 NestingProhibited = ParentRegion != OMPD_target;
2644 Recommend = ShouldBeInTargetRegion;
2645 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2646 }
2647 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2648 // OpenMP [2.16, Nesting of Regions]
2649 // distribute, parallel, parallel sections, parallel workshare, and the
2650 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2651 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002652 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2653 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002654 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002655 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002656 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2657 // OpenMP 4.5 [2.17 Nesting of Regions]
2658 // The region associated with the distribute construct must be strictly
2659 // nested inside a teams region
2660 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2661 Recommend = ShouldBeInTeamsRegion;
2662 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002663 if (!NestingProhibited &&
2664 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2665 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2666 // OpenMP 4.5 [2.17 Nesting of Regions]
2667 // If a target, target update, target data, target enter data, or
2668 // target exit data construct is encountered during execution of a
2669 // target region, the behavior is unspecified.
2670 NestingProhibited = Stack->hasDirective(
2671 [&OffendingRegion](OpenMPDirectiveKind K,
2672 const DeclarationNameInfo &DNI,
2673 SourceLocation Loc) -> bool {
2674 if (isOpenMPTargetExecutionDirective(K)) {
2675 OffendingRegion = K;
2676 return true;
2677 } else
2678 return false;
2679 },
2680 false /* don't skip top directive */);
2681 CloseNesting = false;
2682 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002683 if (NestingProhibited) {
2684 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002685 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2686 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002687 return true;
2688 }
2689 }
2690 return false;
2691}
2692
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002693static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2694 ArrayRef<OMPClause *> Clauses,
2695 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2696 bool ErrorFound = false;
2697 unsigned NamedModifiersNumber = 0;
2698 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2699 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002700 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002701 for (const auto *C : Clauses) {
2702 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2703 // At most one if clause without a directive-name-modifier can appear on
2704 // the directive.
2705 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2706 if (FoundNameModifiers[CurNM]) {
2707 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2708 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2709 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2710 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002711 } else if (CurNM != OMPD_unknown) {
2712 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002713 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002714 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002715 FoundNameModifiers[CurNM] = IC;
2716 if (CurNM == OMPD_unknown)
2717 continue;
2718 // Check if the specified name modifier is allowed for the current
2719 // directive.
2720 // At most one if clause with the particular directive-name-modifier can
2721 // appear on the directive.
2722 bool MatchFound = false;
2723 for (auto NM : AllowedNameModifiers) {
2724 if (CurNM == NM) {
2725 MatchFound = true;
2726 break;
2727 }
2728 }
2729 if (!MatchFound) {
2730 S.Diag(IC->getNameModifierLoc(),
2731 diag::err_omp_wrong_if_directive_name_modifier)
2732 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2733 ErrorFound = true;
2734 }
2735 }
2736 }
2737 // If any if clause on the directive includes a directive-name-modifier then
2738 // all if clauses on the directive must include a directive-name-modifier.
2739 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2740 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2741 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2742 diag::err_omp_no_more_if_clause);
2743 } else {
2744 std::string Values;
2745 std::string Sep(", ");
2746 unsigned AllowedCnt = 0;
2747 unsigned TotalAllowedNum =
2748 AllowedNameModifiers.size() - NamedModifiersNumber;
2749 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2750 ++Cnt) {
2751 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2752 if (!FoundNameModifiers[NM]) {
2753 Values += "'";
2754 Values += getOpenMPDirectiveName(NM);
2755 Values += "'";
2756 if (AllowedCnt + 2 == TotalAllowedNum)
2757 Values += " or ";
2758 else if (AllowedCnt + 1 != TotalAllowedNum)
2759 Values += Sep;
2760 ++AllowedCnt;
2761 }
2762 }
2763 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2764 diag::err_omp_unnamed_if_clause)
2765 << (TotalAllowedNum > 1) << Values;
2766 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002767 for (auto Loc : NameModifierLoc) {
2768 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2769 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002770 ErrorFound = true;
2771 }
2772 return ErrorFound;
2773}
2774
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002775StmtResult Sema::ActOnOpenMPExecutableDirective(
2776 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2777 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2778 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002779 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002780 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2781 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002782 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002783
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002784 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002785 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002786 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002787 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002788 if (AStmt) {
2789 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2790
2791 // Check default data sharing attributes for referenced variables.
2792 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2793 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2794 if (DSAChecker.isErrorFound())
2795 return StmtError();
2796 // Generate list of implicitly defined firstprivate variables.
2797 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002798
2799 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2800 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2801 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2802 SourceLocation(), SourceLocation())) {
2803 ClausesWithImplicit.push_back(Implicit);
2804 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2805 DSAChecker.getImplicitFirstprivate().size();
2806 } else
2807 ErrorFound = true;
2808 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002809 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002810
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002811 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002812 switch (Kind) {
2813 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002814 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2815 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002816 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002817 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002818 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002819 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2820 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002821 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002822 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002823 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2824 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002825 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002826 case OMPD_for_simd:
2827 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2828 EndLoc, VarsWithInheritedDSA);
2829 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002830 case OMPD_sections:
2831 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2832 EndLoc);
2833 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002834 case OMPD_section:
2835 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002836 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002837 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2838 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002839 case OMPD_single:
2840 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2841 EndLoc);
2842 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002843 case OMPD_master:
2844 assert(ClausesWithImplicit.empty() &&
2845 "No clauses are allowed for 'omp master' directive");
2846 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2847 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002848 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002849 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2850 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002851 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002852 case OMPD_parallel_for:
2853 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2854 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002855 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002856 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002857 case OMPD_parallel_for_simd:
2858 Res = ActOnOpenMPParallelForSimdDirective(
2859 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002860 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002861 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002862 case OMPD_parallel_sections:
2863 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2864 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002865 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002866 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002867 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002868 Res =
2869 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002870 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002871 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002872 case OMPD_taskyield:
2873 assert(ClausesWithImplicit.empty() &&
2874 "No clauses are allowed for 'omp taskyield' directive");
2875 assert(AStmt == nullptr &&
2876 "No associated statement allowed for 'omp taskyield' directive");
2877 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2878 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002879 case OMPD_barrier:
2880 assert(ClausesWithImplicit.empty() &&
2881 "No clauses are allowed for 'omp barrier' directive");
2882 assert(AStmt == nullptr &&
2883 "No associated statement allowed for 'omp barrier' directive");
2884 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2885 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002886 case OMPD_taskwait:
2887 assert(ClausesWithImplicit.empty() &&
2888 "No clauses are allowed for 'omp taskwait' directive");
2889 assert(AStmt == nullptr &&
2890 "No associated statement allowed for 'omp taskwait' directive");
2891 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2892 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002893 case OMPD_taskgroup:
2894 assert(ClausesWithImplicit.empty() &&
2895 "No clauses are allowed for 'omp taskgroup' directive");
2896 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2897 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002898 case OMPD_flush:
2899 assert(AStmt == nullptr &&
2900 "No associated statement allowed for 'omp flush' directive");
2901 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2902 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002903 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002904 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2905 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002906 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002907 case OMPD_atomic:
2908 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2909 EndLoc);
2910 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002911 case OMPD_teams:
2912 Res =
2913 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2914 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002915 case OMPD_target:
2916 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2917 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002918 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002919 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002920 case OMPD_target_parallel:
2921 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2922 StartLoc, EndLoc);
2923 AllowedNameModifiers.push_back(OMPD_target);
2924 AllowedNameModifiers.push_back(OMPD_parallel);
2925 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002926 case OMPD_cancellation_point:
2927 assert(ClausesWithImplicit.empty() &&
2928 "No clauses are allowed for 'omp cancellation point' directive");
2929 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2930 "cancellation point' directive");
2931 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2932 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002933 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002934 assert(AStmt == nullptr &&
2935 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002936 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2937 CancelRegion);
2938 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002939 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002940 case OMPD_target_data:
2941 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2942 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002943 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002944 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002945 case OMPD_target_enter_data:
2946 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2947 EndLoc);
2948 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2949 break;
Samuel Antao72590762016-01-19 20:04:50 +00002950 case OMPD_target_exit_data:
2951 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2952 EndLoc);
2953 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2954 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002955 case OMPD_taskloop:
2956 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2957 EndLoc, VarsWithInheritedDSA);
2958 AllowedNameModifiers.push_back(OMPD_taskloop);
2959 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002960 case OMPD_taskloop_simd:
2961 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2962 EndLoc, VarsWithInheritedDSA);
2963 AllowedNameModifiers.push_back(OMPD_taskloop);
2964 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002965 case OMPD_distribute:
2966 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2967 EndLoc, VarsWithInheritedDSA);
2968 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002969 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002970 llvm_unreachable("OpenMP Directive is not allowed");
2971 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002972 llvm_unreachable("Unknown OpenMP directive");
2973 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002974
Alexey Bataev4acb8592014-07-07 13:01:15 +00002975 for (auto P : VarsWithInheritedDSA) {
2976 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2977 << P.first << P.second->getSourceRange();
2978 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002979 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2980
2981 if (!AllowedNameModifiers.empty())
2982 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2983 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002984
Alexey Bataeved09d242014-05-28 05:53:51 +00002985 if (ErrorFound)
2986 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002987 return Res;
2988}
2989
2990StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2991 Stmt *AStmt,
2992 SourceLocation StartLoc,
2993 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002994 if (!AStmt)
2995 return StmtError();
2996
Alexey Bataev9959db52014-05-06 10:08:46 +00002997 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2998 // 1.2.2 OpenMP Language Terminology
2999 // Structured block - An executable statement with a single entry at the
3000 // top and a single exit at the bottom.
3001 // The point of exit cannot be a branch out of the structured block.
3002 // longjmp() and throw() must not violate the entry/exit criteria.
3003 CS->getCapturedDecl()->setNothrow();
3004
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003005 getCurFunction()->setHasBranchProtectedScope();
3006
Alexey Bataev25e5b442015-09-15 12:52:43 +00003007 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3008 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003009}
3010
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003011namespace {
3012/// \brief Helper class for checking canonical form of the OpenMP loops and
3013/// extracting iteration space of each loop in the loop nest, that will be used
3014/// for IR generation.
3015class OpenMPIterationSpaceChecker {
3016 /// \brief Reference to Sema.
3017 Sema &SemaRef;
3018 /// \brief A location for diagnostics (when there is no some better location).
3019 SourceLocation DefaultLoc;
3020 /// \brief A location for diagnostics (when increment is not compatible).
3021 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003022 /// \brief A source location for referring to loop init later.
3023 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003024 /// \brief A source location for referring to condition later.
3025 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003026 /// \brief A source location for referring to increment later.
3027 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003028 /// \brief Loop variable.
3029 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003030 /// \brief Reference to loop variable.
3031 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003032 /// \brief Lower bound (initializer for the var).
3033 Expr *LB;
3034 /// \brief Upper bound.
3035 Expr *UB;
3036 /// \brief Loop step (increment).
3037 Expr *Step;
3038 /// \brief This flag is true when condition is one of:
3039 /// Var < UB
3040 /// Var <= UB
3041 /// UB > Var
3042 /// UB >= Var
3043 bool TestIsLessOp;
3044 /// \brief This flag is true when condition is strict ( < or > ).
3045 bool TestIsStrictOp;
3046 /// \brief This flag is true when step is subtracted on each iteration.
3047 bool SubtractStep;
3048
3049public:
3050 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3051 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003052 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3053 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003054 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3055 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003056 /// \brief Check init-expr for canonical loop form and save loop counter
3057 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003058 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003059 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3060 /// for less/greater and for strict/non-strict comparison.
3061 bool CheckCond(Expr *S);
3062 /// \brief Check incr-expr for canonical loop form and return true if it
3063 /// does not conform, otherwise save loop step (#Step).
3064 bool CheckInc(Expr *S);
3065 /// \brief Return the loop counter variable.
3066 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003067 /// \brief Return the reference expression to loop counter variable.
3068 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003069 /// \brief Source range of the loop init.
3070 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3071 /// \brief Source range of the loop condition.
3072 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3073 /// \brief Source range of the loop increment.
3074 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3075 /// \brief True if the step should be subtracted.
3076 bool ShouldSubtractStep() const { return SubtractStep; }
3077 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003078 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003079 /// \brief Build the precondition expression for the loops.
3080 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003081 /// \brief Build reference expression to the counter be used for codegen.
3082 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003083 /// \brief Build reference expression to the private counter be used for
3084 /// codegen.
3085 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003086 /// \brief Build initization of the counter be used for codegen.
3087 Expr *BuildCounterInit() const;
3088 /// \brief Build step of the counter be used for codegen.
3089 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003090 /// \brief Return true if any expression is dependent.
3091 bool Dependent() const;
3092
3093private:
3094 /// \brief Check the right-hand side of an assignment in the increment
3095 /// expression.
3096 bool CheckIncRHS(Expr *RHS);
3097 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003098 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003099 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003100 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003101 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003102 /// \brief Helper to set loop increment.
3103 bool SetStep(Expr *NewStep, bool Subtract);
3104};
3105
3106bool OpenMPIterationSpaceChecker::Dependent() const {
3107 if (!Var) {
3108 assert(!LB && !UB && !Step);
3109 return false;
3110 }
3111 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3112 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3113}
3114
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003115template <typename T>
3116static T *getExprAsWritten(T *E) {
3117 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3118 E = ExprTemp->getSubExpr();
3119
3120 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3121 E = MTE->GetTemporaryExpr();
3122
3123 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3124 E = Binder->getSubExpr();
3125
3126 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3127 E = ICE->getSubExprAsWritten();
3128 return E->IgnoreParens();
3129}
3130
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003131bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3132 DeclRefExpr *NewVarRefExpr,
3133 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003134 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003135 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3136 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003137 if (!NewVar || !NewLB)
3138 return true;
3139 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003140 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003141 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3142 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003143 if ((Ctor->isCopyOrMoveConstructor() ||
3144 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3145 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003146 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003147 LB = NewLB;
3148 return false;
3149}
3150
3151bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003152 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003153 // State consistency checking to ensure correct usage.
3154 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3155 !TestIsLessOp && !TestIsStrictOp);
3156 if (!NewUB)
3157 return true;
3158 UB = NewUB;
3159 TestIsLessOp = LessOp;
3160 TestIsStrictOp = StrictOp;
3161 ConditionSrcRange = SR;
3162 ConditionLoc = SL;
3163 return false;
3164}
3165
3166bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3167 // State consistency checking to ensure correct usage.
3168 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3169 if (!NewStep)
3170 return true;
3171 if (!NewStep->isValueDependent()) {
3172 // Check that the step is integer expression.
3173 SourceLocation StepLoc = NewStep->getLocStart();
3174 ExprResult Val =
3175 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3176 if (Val.isInvalid())
3177 return true;
3178 NewStep = Val.get();
3179
3180 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3181 // If test-expr is of form var relational-op b and relational-op is < or
3182 // <= then incr-expr must cause var to increase on each iteration of the
3183 // loop. If test-expr is of form var relational-op b and relational-op is
3184 // > or >= then incr-expr must cause var to decrease on each iteration of
3185 // the loop.
3186 // If test-expr is of form b relational-op var and relational-op is < or
3187 // <= then incr-expr must cause var to decrease on each iteration of the
3188 // loop. If test-expr is of form b relational-op var and relational-op is
3189 // > or >= then incr-expr must cause var to increase on each iteration of
3190 // the loop.
3191 llvm::APSInt Result;
3192 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3193 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3194 bool IsConstNeg =
3195 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003196 bool IsConstPos =
3197 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003198 bool IsConstZero = IsConstant && !Result.getBoolValue();
3199 if (UB && (IsConstZero ||
3200 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003201 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003202 SemaRef.Diag(NewStep->getExprLoc(),
3203 diag::err_omp_loop_incr_not_compatible)
3204 << Var << TestIsLessOp << NewStep->getSourceRange();
3205 SemaRef.Diag(ConditionLoc,
3206 diag::note_omp_loop_cond_requres_compatible_incr)
3207 << TestIsLessOp << ConditionSrcRange;
3208 return true;
3209 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003210 if (TestIsLessOp == Subtract) {
3211 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3212 NewStep).get();
3213 Subtract = !Subtract;
3214 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003215 }
3216
3217 Step = NewStep;
3218 SubtractStep = Subtract;
3219 return false;
3220}
3221
Alexey Bataev9c821032015-04-30 04:23:23 +00003222bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003223 // Check init-expr for canonical loop form and save loop counter
3224 // variable - #Var and its initialization value - #LB.
3225 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3226 // var = lb
3227 // integer-type var = lb
3228 // random-access-iterator-type var = lb
3229 // pointer-type var = lb
3230 //
3231 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003232 if (EmitDiags) {
3233 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3234 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003235 return true;
3236 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003237 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003238 if (Expr *E = dyn_cast<Expr>(S))
3239 S = E->IgnoreParens();
3240 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3241 if (BO->getOpcode() == BO_Assign)
3242 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003243 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003244 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003245 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3246 if (DS->isSingleDecl()) {
3247 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003248 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003249 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003250 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003251 SemaRef.Diag(S->getLocStart(),
3252 diag::ext_omp_loop_not_canonical_init)
3253 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003254 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003255 }
3256 }
3257 }
3258 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3259 if (CE->getOperator() == OO_Equal)
3260 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003261 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3262 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003263
Alexey Bataev9c821032015-04-30 04:23:23 +00003264 if (EmitDiags) {
3265 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3266 << S->getSourceRange();
3267 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003268 return true;
3269}
3270
Alexey Bataev23b69422014-06-18 07:08:49 +00003271/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003272/// variable (which may be the loop variable) if possible.
3273static const VarDecl *GetInitVarDecl(const Expr *E) {
3274 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003275 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003276 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003277 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3278 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003279 if ((Ctor->isCopyOrMoveConstructor() ||
3280 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3281 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003282 E = CE->getArg(0)->IgnoreParenImpCasts();
3283 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3284 if (!DRE)
3285 return nullptr;
3286 return dyn_cast<VarDecl>(DRE->getDecl());
3287}
3288
3289bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3290 // Check test-expr for canonical form, save upper-bound UB, flags for
3291 // less/greater and for strict/non-strict comparison.
3292 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3293 // var relational-op b
3294 // b relational-op var
3295 //
3296 if (!S) {
3297 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3298 return true;
3299 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003300 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003301 SourceLocation CondLoc = S->getLocStart();
3302 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3303 if (BO->isRelationalOp()) {
3304 if (GetInitVarDecl(BO->getLHS()) == Var)
3305 return SetUB(BO->getRHS(),
3306 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3307 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3308 BO->getSourceRange(), BO->getOperatorLoc());
3309 if (GetInitVarDecl(BO->getRHS()) == Var)
3310 return SetUB(BO->getLHS(),
3311 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3312 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3313 BO->getSourceRange(), BO->getOperatorLoc());
3314 }
3315 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3316 if (CE->getNumArgs() == 2) {
3317 auto Op = CE->getOperator();
3318 switch (Op) {
3319 case OO_Greater:
3320 case OO_GreaterEqual:
3321 case OO_Less:
3322 case OO_LessEqual:
3323 if (GetInitVarDecl(CE->getArg(0)) == Var)
3324 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3325 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3326 CE->getOperatorLoc());
3327 if (GetInitVarDecl(CE->getArg(1)) == Var)
3328 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3329 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3330 CE->getOperatorLoc());
3331 break;
3332 default:
3333 break;
3334 }
3335 }
3336 }
3337 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3338 << S->getSourceRange() << Var;
3339 return true;
3340}
3341
3342bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3343 // RHS of canonical loop form increment can be:
3344 // var + incr
3345 // incr + var
3346 // var - incr
3347 //
3348 RHS = RHS->IgnoreParenImpCasts();
3349 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3350 if (BO->isAdditiveOp()) {
3351 bool IsAdd = BO->getOpcode() == BO_Add;
3352 if (GetInitVarDecl(BO->getLHS()) == Var)
3353 return SetStep(BO->getRHS(), !IsAdd);
3354 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3355 return SetStep(BO->getLHS(), false);
3356 }
3357 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3358 bool IsAdd = CE->getOperator() == OO_Plus;
3359 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3360 if (GetInitVarDecl(CE->getArg(0)) == Var)
3361 return SetStep(CE->getArg(1), !IsAdd);
3362 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3363 return SetStep(CE->getArg(0), false);
3364 }
3365 }
3366 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3367 << RHS->getSourceRange() << Var;
3368 return true;
3369}
3370
3371bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3372 // Check incr-expr for canonical loop form and return true if it
3373 // does not conform.
3374 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3375 // ++var
3376 // var++
3377 // --var
3378 // var--
3379 // var += incr
3380 // var -= incr
3381 // var = var + incr
3382 // var = incr + var
3383 // var = var - incr
3384 //
3385 if (!S) {
3386 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3387 return true;
3388 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003389 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003390 S = S->IgnoreParens();
3391 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3392 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3393 return SetStep(
3394 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3395 (UO->isDecrementOp() ? -1 : 1)).get(),
3396 false);
3397 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3398 switch (BO->getOpcode()) {
3399 case BO_AddAssign:
3400 case BO_SubAssign:
3401 if (GetInitVarDecl(BO->getLHS()) == Var)
3402 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3403 break;
3404 case BO_Assign:
3405 if (GetInitVarDecl(BO->getLHS()) == Var)
3406 return CheckIncRHS(BO->getRHS());
3407 break;
3408 default:
3409 break;
3410 }
3411 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3412 switch (CE->getOperator()) {
3413 case OO_PlusPlus:
3414 case OO_MinusMinus:
3415 if (GetInitVarDecl(CE->getArg(0)) == Var)
3416 return SetStep(
3417 SemaRef.ActOnIntegerConstant(
3418 CE->getLocStart(),
3419 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3420 false);
3421 break;
3422 case OO_PlusEqual:
3423 case OO_MinusEqual:
3424 if (GetInitVarDecl(CE->getArg(0)) == Var)
3425 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3426 break;
3427 case OO_Equal:
3428 if (GetInitVarDecl(CE->getArg(0)) == Var)
3429 return CheckIncRHS(CE->getArg(1));
3430 break;
3431 default:
3432 break;
3433 }
3434 }
3435 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3436 << S->getSourceRange() << Var;
3437 return true;
3438}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003439
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003440namespace {
3441// Transform variables declared in GNU statement expressions to new ones to
3442// avoid crash on codegen.
3443class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3444 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3445
3446public:
3447 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3448
3449 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3450 if (auto *VD = cast<VarDecl>(D))
3451 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3452 !isa<ImplicitParamDecl>(D)) {
3453 auto *NewVD = VarDecl::Create(
3454 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3455 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3456 VD->getTypeSourceInfo(), VD->getStorageClass());
3457 NewVD->setTSCSpec(VD->getTSCSpec());
3458 NewVD->setInit(VD->getInit());
3459 NewVD->setInitStyle(VD->getInitStyle());
3460 NewVD->setExceptionVariable(VD->isExceptionVariable());
3461 NewVD->setNRVOVariable(VD->isNRVOVariable());
3462 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3463 NewVD->setConstexpr(VD->isConstexpr());
3464 NewVD->setInitCapture(VD->isInitCapture());
3465 NewVD->setPreviousDeclInSameBlockScope(
3466 VD->isPreviousDeclInSameBlockScope());
3467 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003468 if (VD->hasAttrs())
3469 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003470 transformedLocalDecl(VD, NewVD);
3471 return NewVD;
3472 }
3473 return BaseTransform::TransformDefinition(Loc, D);
3474 }
3475
3476 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3477 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3478 if (E->getDecl() != NewD) {
3479 NewD->setReferenced();
3480 NewD->markUsed(SemaRef.Context);
3481 return DeclRefExpr::Create(
3482 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3483 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3484 E->getNameInfo(), E->getType(), E->getValueKind());
3485 }
3486 return BaseTransform::TransformDeclRefExpr(E);
3487 }
3488};
3489}
3490
Alexander Musmana5f070a2014-10-01 06:03:56 +00003491/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003492Expr *
3493OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3494 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003495 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003496 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003497 auto VarType = Var->getType().getNonReferenceType();
3498 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003499 SemaRef.getLangOpts().CPlusPlus) {
3500 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003501 auto *UBExpr = TestIsLessOp ? UB : LB;
3502 auto *LBExpr = TestIsLessOp ? LB : UB;
3503 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3504 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3505 if (!Upper || !Lower)
3506 return nullptr;
3507 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3508 Sema::AA_Converting,
3509 /*AllowExplicit=*/true)
3510 .get();
3511 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3512 Sema::AA_Converting,
3513 /*AllowExplicit=*/true)
3514 .get();
3515 if (!Upper || !Lower)
3516 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003517
3518 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3519
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003520 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003521 // BuildBinOp already emitted error, this one is to point user to upper
3522 // and lower bound, and to tell what is passed to 'operator-'.
3523 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3524 << Upper->getSourceRange() << Lower->getSourceRange();
3525 return nullptr;
3526 }
3527 }
3528
3529 if (!Diff.isUsable())
3530 return nullptr;
3531
3532 // Upper - Lower [- 1]
3533 if (TestIsStrictOp)
3534 Diff = SemaRef.BuildBinOp(
3535 S, DefaultLoc, BO_Sub, Diff.get(),
3536 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3537 if (!Diff.isUsable())
3538 return nullptr;
3539
3540 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003541 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3542 if (NewStep.isInvalid())
3543 return nullptr;
3544 NewStep = SemaRef.PerformImplicitConversion(
3545 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3546 /*AllowExplicit=*/true);
3547 if (NewStep.isInvalid())
3548 return nullptr;
3549 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003550 if (!Diff.isUsable())
3551 return nullptr;
3552
3553 // Parentheses (for dumping/debugging purposes only).
3554 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3555 if (!Diff.isUsable())
3556 return nullptr;
3557
3558 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003559 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3560 if (NewStep.isInvalid())
3561 return nullptr;
3562 NewStep = SemaRef.PerformImplicitConversion(
3563 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3564 /*AllowExplicit=*/true);
3565 if (NewStep.isInvalid())
3566 return nullptr;
3567 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003568 if (!Diff.isUsable())
3569 return nullptr;
3570
Alexander Musman174b3ca2014-10-06 11:16:29 +00003571 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003572 QualType Type = Diff.get()->getType();
3573 auto &C = SemaRef.Context;
3574 bool UseVarType = VarType->hasIntegerRepresentation() &&
3575 C.getTypeSize(Type) > C.getTypeSize(VarType);
3576 if (!Type->isIntegerType() || UseVarType) {
3577 unsigned NewSize =
3578 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3579 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3580 : Type->hasSignedIntegerRepresentation();
3581 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3582 Diff = SemaRef.PerformImplicitConversion(
3583 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3584 if (!Diff.isUsable())
3585 return nullptr;
3586 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003587 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003588 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3589 if (NewSize != C.getTypeSize(Type)) {
3590 if (NewSize < C.getTypeSize(Type)) {
3591 assert(NewSize == 64 && "incorrect loop var size");
3592 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3593 << InitSrcRange << ConditionSrcRange;
3594 }
3595 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003596 NewSize, Type->hasSignedIntegerRepresentation() ||
3597 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003598 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3599 Sema::AA_Converting, true);
3600 if (!Diff.isUsable())
3601 return nullptr;
3602 }
3603 }
3604
Alexander Musmana5f070a2014-10-01 06:03:56 +00003605 return Diff.get();
3606}
3607
Alexey Bataev62dbb972015-04-22 11:59:37 +00003608Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3609 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3610 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3611 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003612 TransformToNewDefs Transform(SemaRef);
3613
3614 auto NewLB = Transform.TransformExpr(LB);
3615 auto NewUB = Transform.TransformExpr(UB);
3616 if (NewLB.isInvalid() || NewUB.isInvalid())
3617 return Cond;
3618 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3619 Sema::AA_Converting,
3620 /*AllowExplicit=*/true);
3621 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3622 Sema::AA_Converting,
3623 /*AllowExplicit=*/true);
3624 if (NewLB.isInvalid() || NewUB.isInvalid())
3625 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003626 auto CondExpr = SemaRef.BuildBinOp(
3627 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3628 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003629 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003630 if (CondExpr.isUsable()) {
3631 CondExpr = SemaRef.PerformImplicitConversion(
3632 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3633 /*AllowExplicit=*/true);
3634 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003635 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3636 // Otherwise use original loop conditon and evaluate it in runtime.
3637 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3638}
3639
Alexander Musmana5f070a2014-10-01 06:03:56 +00003640/// \brief Build reference expression to the counter be used for codegen.
3641Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003642 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3643 DefaultLoc);
3644}
3645
3646Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3647 if (Var && !Var->isInvalidDecl()) {
3648 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003649 auto *PrivateVar =
3650 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3651 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003652 if (PrivateVar->isInvalidDecl())
3653 return nullptr;
3654 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3655 }
3656 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003657}
3658
3659/// \brief Build initization of the counter be used for codegen.
3660Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3661
3662/// \brief Build step of the counter be used for codegen.
3663Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3664
3665/// \brief Iteration space of a single for loop.
3666struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003667 /// \brief Condition of the loop.
3668 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003669 /// \brief This expression calculates the number of iterations in the loop.
3670 /// It is always possible to calculate it before starting the loop.
3671 Expr *NumIterations;
3672 /// \brief The loop counter variable.
3673 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003674 /// \brief Private loop counter variable.
3675 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003676 /// \brief This is initializer for the initial value of #CounterVar.
3677 Expr *CounterInit;
3678 /// \brief This is step for the #CounterVar used to generate its update:
3679 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3680 Expr *CounterStep;
3681 /// \brief Should step be subtracted?
3682 bool Subtract;
3683 /// \brief Source range of the loop init.
3684 SourceRange InitSrcRange;
3685 /// \brief Source range of the loop condition.
3686 SourceRange CondSrcRange;
3687 /// \brief Source range of the loop increment.
3688 SourceRange IncSrcRange;
3689};
3690
Alexey Bataev23b69422014-06-18 07:08:49 +00003691} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003692
Alexey Bataev9c821032015-04-30 04:23:23 +00003693void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3694 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3695 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003696 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3697 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003698 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3699 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003700 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003701 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003702 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003703 }
3704}
3705
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003706/// \brief Called on a for stmt to check and extract its iteration space
3707/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003708static bool CheckOpenMPIterationSpace(
3709 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3710 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003711 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003712 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003713 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003714 // OpenMP [2.6, Canonical Loop Form]
3715 // for (init-expr; test-expr; incr-expr) structured-block
3716 auto For = dyn_cast_or_null<ForStmt>(S);
3717 if (!For) {
3718 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003719 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3720 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3721 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3722 if (NestedLoopCount > 1) {
3723 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3724 SemaRef.Diag(DSA.getConstructLoc(),
3725 diag::note_omp_collapse_ordered_expr)
3726 << 2 << CollapseLoopCountExpr->getSourceRange()
3727 << OrderedLoopCountExpr->getSourceRange();
3728 else if (CollapseLoopCountExpr)
3729 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3730 diag::note_omp_collapse_ordered_expr)
3731 << 0 << CollapseLoopCountExpr->getSourceRange();
3732 else
3733 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3734 diag::note_omp_collapse_ordered_expr)
3735 << 1 << OrderedLoopCountExpr->getSourceRange();
3736 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003737 return true;
3738 }
3739 assert(For->getBody());
3740
3741 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3742
3743 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003744 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003745 if (ISC.CheckInit(Init)) {
3746 return true;
3747 }
3748
3749 bool HasErrors = false;
3750
3751 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003752 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003753
3754 // OpenMP [2.6, Canonical Loop Form]
3755 // Var is one of the following:
3756 // A variable of signed or unsigned integer type.
3757 // For C++, a variable of a random access iterator type.
3758 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003759 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003760 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3761 !VarType->isPointerType() &&
3762 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3763 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3764 << SemaRef.getLangOpts().CPlusPlus;
3765 HasErrors = true;
3766 }
3767
Alexey Bataev4acb8592014-07-07 13:01:15 +00003768 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3769 // Construct
3770 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3771 // parallel for construct is (are) private.
3772 // The loop iteration variable in the associated for-loop of a simd construct
3773 // with just one associated for-loop is linear with a constant-linear-step
3774 // that is the increment of the associated for-loop.
3775 // Exclude loop var from the list of variables with implicitly defined data
3776 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003777 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003778
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003779 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3780 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003781 // The loop iteration variable in the associated for-loop of a simd construct
3782 // with just one associated for-loop may be listed in a linear clause with a
3783 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003784 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3785 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003786 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003787 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3788 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3789 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003790 auto PredeterminedCKind =
3791 isOpenMPSimdDirective(DKind)
3792 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3793 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003794 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003795 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003796 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003797 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003798 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003799 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3800 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003801 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003802 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3803 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003804 if (DVar.RefExpr == nullptr)
3805 DVar.CKind = PredeterminedCKind;
3806 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003807 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003808 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003809 // Make the loop iteration variable private (for worksharing constructs),
3810 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003811 // lastprivate (for simd directives with several collapsed or ordered
3812 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003813 if (DVar.CKind == OMPC_unknown)
3814 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3815 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003816 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 }
3818
Alexey Bataev7ff55242014-06-19 09:13:45 +00003819 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003820
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003821 // Check test-expr.
3822 HasErrors |= ISC.CheckCond(For->getCond());
3823
3824 // Check incr-expr.
3825 HasErrors |= ISC.CheckInc(For->getInc());
3826
Alexander Musmana5f070a2014-10-01 06:03:56 +00003827 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003828 return HasErrors;
3829
Alexander Musmana5f070a2014-10-01 06:03:56 +00003830 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003831 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003832 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003833 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003834 isOpenMPTaskLoopDirective(DKind) ||
3835 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003836 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003837 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003838 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3839 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3840 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3841 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3842 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3843 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3844
Alexey Bataev62dbb972015-04-22 11:59:37 +00003845 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3846 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003847 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003848 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003849 ResultIterSpace.CounterInit == nullptr ||
3850 ResultIterSpace.CounterStep == nullptr);
3851
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003852 return HasErrors;
3853}
3854
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003855/// \brief Build 'VarRef = Start.
3856static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3857 ExprResult VarRef, ExprResult Start) {
3858 TransformToNewDefs Transform(SemaRef);
3859 // Build 'VarRef = Start.
3860 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3861 if (NewStart.isInvalid())
3862 return ExprError();
3863 NewStart = SemaRef.PerformImplicitConversion(
3864 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3865 Sema::AA_Converting,
3866 /*AllowExplicit=*/true);
3867 if (NewStart.isInvalid())
3868 return ExprError();
3869 NewStart = SemaRef.PerformImplicitConversion(
3870 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3871 /*AllowExplicit=*/true);
3872 if (!NewStart.isUsable())
3873 return ExprError();
3874
3875 auto Init =
3876 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3877 return Init;
3878}
3879
Alexander Musmana5f070a2014-10-01 06:03:56 +00003880/// \brief Build 'VarRef = Start + Iter * Step'.
3881static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3882 SourceLocation Loc, ExprResult VarRef,
3883 ExprResult Start, ExprResult Iter,
3884 ExprResult Step, bool Subtract) {
3885 // Add parentheses (for debugging purposes only).
3886 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3887 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3888 !Step.isUsable())
3889 return ExprError();
3890
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003891 TransformToNewDefs Transform(SemaRef);
3892 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3893 if (NewStep.isInvalid())
3894 return ExprError();
3895 NewStep = SemaRef.PerformImplicitConversion(
3896 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3897 Sema::AA_Converting,
3898 /*AllowExplicit=*/true);
3899 if (NewStep.isInvalid())
3900 return ExprError();
3901 ExprResult Update =
3902 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003903 if (!Update.isUsable())
3904 return ExprError();
3905
3906 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003907 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3908 if (NewStart.isInvalid())
3909 return ExprError();
3910 NewStart = SemaRef.PerformImplicitConversion(
3911 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3912 Sema::AA_Converting,
3913 /*AllowExplicit=*/true);
3914 if (NewStart.isInvalid())
3915 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003916 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003917 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003918 if (!Update.isUsable())
3919 return ExprError();
3920
3921 Update = SemaRef.PerformImplicitConversion(
3922 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3923 if (!Update.isUsable())
3924 return ExprError();
3925
3926 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3927 return Update;
3928}
3929
3930/// \brief Convert integer expression \a E to make it have at least \a Bits
3931/// bits.
3932static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3933 Sema &SemaRef) {
3934 if (E == nullptr)
3935 return ExprError();
3936 auto &C = SemaRef.Context;
3937 QualType OldType = E->getType();
3938 unsigned HasBits = C.getTypeSize(OldType);
3939 if (HasBits >= Bits)
3940 return ExprResult(E);
3941 // OK to convert to signed, because new type has more bits than old.
3942 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3943 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3944 true);
3945}
3946
3947/// \brief Check if the given expression \a E is a constant integer that fits
3948/// into \a Bits bits.
3949static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3950 if (E == nullptr)
3951 return false;
3952 llvm::APSInt Result;
3953 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3954 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3955 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003956}
3957
3958/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003959/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3960/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003961static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003962CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3963 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3964 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003965 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003966 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003967 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003968 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003969 // Found 'collapse' clause - calculate collapse number.
3970 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003971 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003972 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003973 }
3974 if (OrderedLoopCountExpr) {
3975 // Found 'ordered' clause - calculate collapse number.
3976 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003977 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3978 if (Result.getLimitedValue() < NestedLoopCount) {
3979 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3980 diag::err_omp_wrong_ordered_loop_count)
3981 << OrderedLoopCountExpr->getSourceRange();
3982 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3983 diag::note_collapse_loop_count)
3984 << CollapseLoopCountExpr->getSourceRange();
3985 }
3986 NestedLoopCount = Result.getLimitedValue();
3987 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003988 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003989 // This is helper routine for loop directives (e.g., 'for', 'simd',
3990 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003991 SmallVector<LoopIterationSpace, 4> IterSpaces;
3992 IterSpaces.resize(NestedLoopCount);
3993 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003995 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003996 NestedLoopCount, CollapseLoopCountExpr,
3997 OrderedLoopCountExpr, VarsWithImplicitDSA,
3998 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003999 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004001 // OpenMP [2.8.1, simd construct, Restrictions]
4002 // All loops associated with the construct must be perfectly nested; that
4003 // is, there must be no intervening code nor any OpenMP directive between
4004 // any two loops.
4005 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004006 }
4007
Alexander Musmana5f070a2014-10-01 06:03:56 +00004008 Built.clear(/* size */ NestedLoopCount);
4009
4010 if (SemaRef.CurContext->isDependentContext())
4011 return NestedLoopCount;
4012
4013 // An example of what is generated for the following code:
4014 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004015 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004016 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004017 // for (k = 0; k < NK; ++k)
4018 // for (j = J0; j < NJ; j+=2) {
4019 // <loop body>
4020 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004021 //
4022 // We generate the code below.
4023 // Note: the loop body may be outlined in CodeGen.
4024 // Note: some counters may be C++ classes, operator- is used to find number of
4025 // iterations and operator+= to calculate counter value.
4026 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4027 // or i64 is currently supported).
4028 //
4029 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4030 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4031 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4032 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4033 // // similar updates for vars in clauses (e.g. 'linear')
4034 // <loop body (using local i and j)>
4035 // }
4036 // i = NI; // assign final values of counters
4037 // j = NJ;
4038 //
4039
4040 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4041 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004042 // Precondition tests if there is at least one iteration (all conditions are
4043 // true).
4044 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004045 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004046 ExprResult LastIteration32 = WidenIterationCount(
4047 32 /* Bits */, SemaRef.PerformImplicitConversion(
4048 N0->IgnoreImpCasts(), N0->getType(),
4049 Sema::AA_Converting, /*AllowExplicit=*/true)
4050 .get(),
4051 SemaRef);
4052 ExprResult LastIteration64 = WidenIterationCount(
4053 64 /* Bits */, SemaRef.PerformImplicitConversion(
4054 N0->IgnoreImpCasts(), N0->getType(),
4055 Sema::AA_Converting, /*AllowExplicit=*/true)
4056 .get(),
4057 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004058
4059 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4060 return NestedLoopCount;
4061
4062 auto &C = SemaRef.Context;
4063 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4064
4065 Scope *CurScope = DSA.getCurScope();
4066 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004067 if (PreCond.isUsable()) {
4068 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4069 PreCond.get(), IterSpaces[Cnt].PreCond);
4070 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004071 auto N = IterSpaces[Cnt].NumIterations;
4072 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4073 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004074 LastIteration32 = SemaRef.BuildBinOp(
4075 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4076 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4077 Sema::AA_Converting,
4078 /*AllowExplicit=*/true)
4079 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004080 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004081 LastIteration64 = SemaRef.BuildBinOp(
4082 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4083 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4084 Sema::AA_Converting,
4085 /*AllowExplicit=*/true)
4086 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004087 }
4088
4089 // Choose either the 32-bit or 64-bit version.
4090 ExprResult LastIteration = LastIteration64;
4091 if (LastIteration32.isUsable() &&
4092 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4093 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4094 FitsInto(
4095 32 /* Bits */,
4096 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4097 LastIteration64.get(), SemaRef)))
4098 LastIteration = LastIteration32;
4099
4100 if (!LastIteration.isUsable())
4101 return 0;
4102
4103 // Save the number of iterations.
4104 ExprResult NumIterations = LastIteration;
4105 {
4106 LastIteration = SemaRef.BuildBinOp(
4107 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4108 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4109 if (!LastIteration.isUsable())
4110 return 0;
4111 }
4112
4113 // Calculate the last iteration number beforehand instead of doing this on
4114 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4115 llvm::APSInt Result;
4116 bool IsConstant =
4117 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4118 ExprResult CalcLastIteration;
4119 if (!IsConstant) {
4120 SourceLocation SaveLoc;
4121 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004122 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004123 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004124 ExprResult SaveRef = buildDeclRefExpr(
4125 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004126 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4127 SaveRef.get(), LastIteration.get());
4128 LastIteration = SaveRef;
4129
4130 // Prepare SaveRef + 1.
4131 NumIterations = SemaRef.BuildBinOp(
4132 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4133 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4134 if (!NumIterations.isUsable())
4135 return 0;
4136 }
4137
4138 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4139
Alexander Musmanc6388682014-12-15 07:07:06 +00004140 QualType VType = LastIteration.get()->getType();
4141 // Build variables passed into runtime, nesessary for worksharing directives.
4142 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004143 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4144 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004145 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004146 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4147 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004148 SemaRef.AddInitializerToDecl(
4149 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4150 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4151
4152 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004153 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4154 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004155 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4156 /*DirectInit*/ false,
4157 /*TypeMayContainAuto*/ false);
4158
4159 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4160 // This will be used to implement clause 'lastprivate'.
4161 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004162 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4163 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004164 SemaRef.AddInitializerToDecl(
4165 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4166 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4167
4168 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004169 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4170 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004171 SemaRef.AddInitializerToDecl(
4172 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4173 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4174
4175 // Build expression: UB = min(UB, LastIteration)
4176 // It is nesessary for CodeGen of directives with static scheduling.
4177 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4178 UB.get(), LastIteration.get());
4179 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4180 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4181 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4182 CondOp.get());
4183 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4184 }
4185
4186 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004187 ExprResult IV;
4188 ExprResult Init;
4189 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004190 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4191 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004192 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004193 isOpenMPTaskLoopDirective(DKind) ||
4194 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004195 ? LB.get()
4196 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4197 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4198 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004199 }
4200
Alexander Musmanc6388682014-12-15 07:07:06 +00004201 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004202 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004203 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004204 (isOpenMPWorksharingDirective(DKind) ||
4205 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004206 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4207 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4208 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004209
4210 // Loop increment (IV = IV + 1)
4211 SourceLocation IncLoc;
4212 ExprResult Inc =
4213 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4214 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4215 if (!Inc.isUsable())
4216 return 0;
4217 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004218 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4219 if (!Inc.isUsable())
4220 return 0;
4221
4222 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4223 // Used for directives with static scheduling.
4224 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004225 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4226 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004227 // LB + ST
4228 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4229 if (!NextLB.isUsable())
4230 return 0;
4231 // LB = LB + ST
4232 NextLB =
4233 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4234 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4235 if (!NextLB.isUsable())
4236 return 0;
4237 // UB + ST
4238 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4239 if (!NextUB.isUsable())
4240 return 0;
4241 // UB = UB + ST
4242 NextUB =
4243 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4244 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4245 if (!NextUB.isUsable())
4246 return 0;
4247 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004248
4249 // Build updates and final values of the loop counters.
4250 bool HasErrors = false;
4251 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004252 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004253 Built.Updates.resize(NestedLoopCount);
4254 Built.Finals.resize(NestedLoopCount);
4255 {
4256 ExprResult Div;
4257 // Go from inner nested loop to outer.
4258 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4259 LoopIterationSpace &IS = IterSpaces[Cnt];
4260 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4261 // Build: Iter = (IV / Div) % IS.NumIters
4262 // where Div is product of previous iterations' IS.NumIters.
4263 ExprResult Iter;
4264 if (Div.isUsable()) {
4265 Iter =
4266 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4267 } else {
4268 Iter = IV;
4269 assert((Cnt == (int)NestedLoopCount - 1) &&
4270 "unusable div expected on first iteration only");
4271 }
4272
4273 if (Cnt != 0 && Iter.isUsable())
4274 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4275 IS.NumIterations);
4276 if (!Iter.isUsable()) {
4277 HasErrors = true;
4278 break;
4279 }
4280
Alexey Bataev39f915b82015-05-08 10:41:21 +00004281 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4282 auto *CounterVar = buildDeclRefExpr(
4283 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4284 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4285 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004286 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4287 IS.CounterInit);
4288 if (!Init.isUsable()) {
4289 HasErrors = true;
4290 break;
4291 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004292 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004293 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004294 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4295 if (!Update.isUsable()) {
4296 HasErrors = true;
4297 break;
4298 }
4299
4300 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4301 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004302 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004303 IS.NumIterations, IS.CounterStep, IS.Subtract);
4304 if (!Final.isUsable()) {
4305 HasErrors = true;
4306 break;
4307 }
4308
4309 // Build Div for the next iteration: Div <- Div * IS.NumIters
4310 if (Cnt != 0) {
4311 if (Div.isUnset())
4312 Div = IS.NumIterations;
4313 else
4314 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4315 IS.NumIterations);
4316
4317 // Add parentheses (for debugging purposes only).
4318 if (Div.isUsable())
4319 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4320 if (!Div.isUsable()) {
4321 HasErrors = true;
4322 break;
4323 }
4324 }
4325 if (!Update.isUsable() || !Final.isUsable()) {
4326 HasErrors = true;
4327 break;
4328 }
4329 // Save results
4330 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004331 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004332 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004333 Built.Updates[Cnt] = Update.get();
4334 Built.Finals[Cnt] = Final.get();
4335 }
4336 }
4337
4338 if (HasErrors)
4339 return 0;
4340
4341 // Save results
4342 Built.IterationVarRef = IV.get();
4343 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004344 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004345 Built.CalcLastIteration =
4346 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004347 Built.PreCond = PreCond.get();
4348 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004349 Built.Init = Init.get();
4350 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004351 Built.LB = LB.get();
4352 Built.UB = UB.get();
4353 Built.IL = IL.get();
4354 Built.ST = ST.get();
4355 Built.EUB = EUB.get();
4356 Built.NLB = NextLB.get();
4357 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004358
Alexey Bataevabfc0692014-06-25 06:52:00 +00004359 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004360}
4361
Alexey Bataev10e775f2015-07-30 11:36:16 +00004362static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004363 auto CollapseClauses =
4364 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4365 if (CollapseClauses.begin() != CollapseClauses.end())
4366 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004367 return nullptr;
4368}
4369
Alexey Bataev10e775f2015-07-30 11:36:16 +00004370static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004371 auto OrderedClauses =
4372 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4373 if (OrderedClauses.begin() != OrderedClauses.end())
4374 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004375 return nullptr;
4376}
4377
Alexey Bataev66b15b52015-08-21 11:14:16 +00004378static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4379 const Expr *Safelen) {
4380 llvm::APSInt SimdlenRes, SafelenRes;
4381 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4382 Simdlen->isInstantiationDependent() ||
4383 Simdlen->containsUnexpandedParameterPack())
4384 return false;
4385 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4386 Safelen->isInstantiationDependent() ||
4387 Safelen->containsUnexpandedParameterPack())
4388 return false;
4389 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4390 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4391 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4392 // If both simdlen and safelen clauses are specified, the value of the simdlen
4393 // parameter must be less than or equal to the value of the safelen parameter.
4394 if (SimdlenRes > SafelenRes) {
4395 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4396 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4397 return true;
4398 }
4399 return false;
4400}
4401
Alexey Bataev4acb8592014-07-07 13:01:15 +00004402StmtResult Sema::ActOnOpenMPSimdDirective(
4403 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4404 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004405 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004406 if (!AStmt)
4407 return StmtError();
4408
4409 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004410 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004411 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4412 // define the nested loops number.
4413 unsigned NestedLoopCount = CheckOpenMPLoop(
4414 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4415 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004416 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004417 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004418
Alexander Musmana5f070a2014-10-01 06:03:56 +00004419 assert((CurContext->isDependentContext() || B.builtAll()) &&
4420 "omp simd loop exprs were not built");
4421
Alexander Musman3276a272015-03-21 10:12:56 +00004422 if (!CurContext->isDependentContext()) {
4423 // Finalize the clauses that need pre-built expressions for CodeGen.
4424 for (auto C : Clauses) {
4425 if (auto LC = dyn_cast<OMPLinearClause>(C))
4426 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4427 B.NumIterations, *this, CurScope))
4428 return StmtError();
4429 }
4430 }
4431
Alexey Bataev66b15b52015-08-21 11:14:16 +00004432 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4433 // If both simdlen and safelen clauses are specified, the value of the simdlen
4434 // parameter must be less than or equal to the value of the safelen parameter.
4435 OMPSafelenClause *Safelen = nullptr;
4436 OMPSimdlenClause *Simdlen = nullptr;
4437 for (auto *Clause : Clauses) {
4438 if (Clause->getClauseKind() == OMPC_safelen)
4439 Safelen = cast<OMPSafelenClause>(Clause);
4440 else if (Clause->getClauseKind() == OMPC_simdlen)
4441 Simdlen = cast<OMPSimdlenClause>(Clause);
4442 if (Safelen && Simdlen)
4443 break;
4444 }
4445 if (Simdlen && Safelen &&
4446 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4447 Safelen->getSafelen()))
4448 return StmtError();
4449
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004450 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004451 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4452 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004453}
4454
Alexey Bataev4acb8592014-07-07 13:01:15 +00004455StmtResult Sema::ActOnOpenMPForDirective(
4456 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4457 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004458 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004459 if (!AStmt)
4460 return StmtError();
4461
4462 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004463 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004464 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4465 // define the nested loops number.
4466 unsigned NestedLoopCount = CheckOpenMPLoop(
4467 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4468 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004469 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004470 return StmtError();
4471
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472 assert((CurContext->isDependentContext() || B.builtAll()) &&
4473 "omp for loop exprs were not built");
4474
Alexey Bataev54acd402015-08-04 11:18:19 +00004475 if (!CurContext->isDependentContext()) {
4476 // Finalize the clauses that need pre-built expressions for CodeGen.
4477 for (auto C : Clauses) {
4478 if (auto LC = dyn_cast<OMPLinearClause>(C))
4479 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4480 B.NumIterations, *this, CurScope))
4481 return StmtError();
4482 }
4483 }
4484
Alexey Bataevf29276e2014-06-18 04:14:57 +00004485 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004486 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004487 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004488}
4489
Alexander Musmanf82886e2014-09-18 05:12:34 +00004490StmtResult Sema::ActOnOpenMPForSimdDirective(
4491 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4492 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004493 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004494 if (!AStmt)
4495 return StmtError();
4496
4497 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004498 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004499 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4500 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004501 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004502 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4503 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4504 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004505 if (NestedLoopCount == 0)
4506 return StmtError();
4507
Alexander Musmanc6388682014-12-15 07:07:06 +00004508 assert((CurContext->isDependentContext() || B.builtAll()) &&
4509 "omp for simd loop exprs were not built");
4510
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004511 if (!CurContext->isDependentContext()) {
4512 // Finalize the clauses that need pre-built expressions for CodeGen.
4513 for (auto C : Clauses) {
4514 if (auto LC = dyn_cast<OMPLinearClause>(C))
4515 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4516 B.NumIterations, *this, CurScope))
4517 return StmtError();
4518 }
4519 }
4520
Alexey Bataev66b15b52015-08-21 11:14:16 +00004521 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4522 // If both simdlen and safelen clauses are specified, the value of the simdlen
4523 // parameter must be less than or equal to the value of the safelen parameter.
4524 OMPSafelenClause *Safelen = nullptr;
4525 OMPSimdlenClause *Simdlen = nullptr;
4526 for (auto *Clause : Clauses) {
4527 if (Clause->getClauseKind() == OMPC_safelen)
4528 Safelen = cast<OMPSafelenClause>(Clause);
4529 else if (Clause->getClauseKind() == OMPC_simdlen)
4530 Simdlen = cast<OMPSimdlenClause>(Clause);
4531 if (Safelen && Simdlen)
4532 break;
4533 }
4534 if (Simdlen && Safelen &&
4535 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4536 Safelen->getSafelen()))
4537 return StmtError();
4538
Alexander Musmanf82886e2014-09-18 05:12:34 +00004539 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004540 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4541 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004542}
4543
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004544StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4545 Stmt *AStmt,
4546 SourceLocation StartLoc,
4547 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004548 if (!AStmt)
4549 return StmtError();
4550
4551 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004552 auto BaseStmt = AStmt;
4553 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4554 BaseStmt = CS->getCapturedStmt();
4555 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4556 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004557 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004558 return StmtError();
4559 // All associated statements must be '#pragma omp section' except for
4560 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004561 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004562 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4563 if (SectionStmt)
4564 Diag(SectionStmt->getLocStart(),
4565 diag::err_omp_sections_substmt_not_section);
4566 return StmtError();
4567 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004568 cast<OMPSectionDirective>(SectionStmt)
4569 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004570 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004571 } else {
4572 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4573 return StmtError();
4574 }
4575
4576 getCurFunction()->setHasBranchProtectedScope();
4577
Alexey Bataev25e5b442015-09-15 12:52:43 +00004578 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4579 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004580}
4581
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004582StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4583 SourceLocation StartLoc,
4584 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004585 if (!AStmt)
4586 return StmtError();
4587
4588 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004589
4590 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004591 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004592
Alexey Bataev25e5b442015-09-15 12:52:43 +00004593 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4594 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004595}
4596
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004597StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4598 Stmt *AStmt,
4599 SourceLocation StartLoc,
4600 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004601 if (!AStmt)
4602 return StmtError();
4603
4604 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004605
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004606 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004607
Alexey Bataev3255bf32015-01-19 05:20:46 +00004608 // OpenMP [2.7.3, single Construct, Restrictions]
4609 // The copyprivate clause must not be used with the nowait clause.
4610 OMPClause *Nowait = nullptr;
4611 OMPClause *Copyprivate = nullptr;
4612 for (auto *Clause : Clauses) {
4613 if (Clause->getClauseKind() == OMPC_nowait)
4614 Nowait = Clause;
4615 else if (Clause->getClauseKind() == OMPC_copyprivate)
4616 Copyprivate = Clause;
4617 if (Copyprivate && Nowait) {
4618 Diag(Copyprivate->getLocStart(),
4619 diag::err_omp_single_copyprivate_with_nowait);
4620 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4621 return StmtError();
4622 }
4623 }
4624
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004625 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4626}
4627
Alexander Musman80c22892014-07-17 08:54:58 +00004628StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4629 SourceLocation StartLoc,
4630 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004631 if (!AStmt)
4632 return StmtError();
4633
4634 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004635
4636 getCurFunction()->setHasBranchProtectedScope();
4637
4638 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4639}
4640
Alexey Bataev28c75412015-12-15 08:19:24 +00004641StmtResult Sema::ActOnOpenMPCriticalDirective(
4642 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4643 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004644 if (!AStmt)
4645 return StmtError();
4646
4647 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004648
Alexey Bataev28c75412015-12-15 08:19:24 +00004649 bool ErrorFound = false;
4650 llvm::APSInt Hint;
4651 SourceLocation HintLoc;
4652 bool DependentHint = false;
4653 for (auto *C : Clauses) {
4654 if (C->getClauseKind() == OMPC_hint) {
4655 if (!DirName.getName()) {
4656 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4657 ErrorFound = true;
4658 }
4659 Expr *E = cast<OMPHintClause>(C)->getHint();
4660 if (E->isTypeDependent() || E->isValueDependent() ||
4661 E->isInstantiationDependent())
4662 DependentHint = true;
4663 else {
4664 Hint = E->EvaluateKnownConstInt(Context);
4665 HintLoc = C->getLocStart();
4666 }
4667 }
4668 }
4669 if (ErrorFound)
4670 return StmtError();
4671 auto Pair = DSAStack->getCriticalWithHint(DirName);
4672 if (Pair.first && DirName.getName() && !DependentHint) {
4673 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4674 Diag(StartLoc, diag::err_omp_critical_with_hint);
4675 if (HintLoc.isValid()) {
4676 Diag(HintLoc, diag::note_omp_critical_hint_here)
4677 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4678 } else
4679 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4680 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4681 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4682 << 1
4683 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4684 /*Radix=*/10, /*Signed=*/false);
4685 } else
4686 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4687 }
4688 }
4689
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004690 getCurFunction()->setHasBranchProtectedScope();
4691
Alexey Bataev28c75412015-12-15 08:19:24 +00004692 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4693 Clauses, AStmt);
4694 if (!Pair.first && DirName.getName() && !DependentHint)
4695 DSAStack->addCriticalWithHint(Dir, Hint);
4696 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004697}
4698
Alexey Bataev4acb8592014-07-07 13:01:15 +00004699StmtResult Sema::ActOnOpenMPParallelForDirective(
4700 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4701 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004702 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004703 if (!AStmt)
4704 return StmtError();
4705
Alexey Bataev4acb8592014-07-07 13:01:15 +00004706 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4707 // 1.2.2 OpenMP Language Terminology
4708 // Structured block - An executable statement with a single entry at the
4709 // top and a single exit at the bottom.
4710 // The point of exit cannot be a branch out of the structured block.
4711 // longjmp() and throw() must not violate the entry/exit criteria.
4712 CS->getCapturedDecl()->setNothrow();
4713
Alexander Musmanc6388682014-12-15 07:07:06 +00004714 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004715 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4716 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004717 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004718 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4719 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4720 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004721 if (NestedLoopCount == 0)
4722 return StmtError();
4723
Alexander Musmana5f070a2014-10-01 06:03:56 +00004724 assert((CurContext->isDependentContext() || B.builtAll()) &&
4725 "omp parallel for loop exprs were not built");
4726
Alexey Bataev54acd402015-08-04 11:18:19 +00004727 if (!CurContext->isDependentContext()) {
4728 // Finalize the clauses that need pre-built expressions for CodeGen.
4729 for (auto C : Clauses) {
4730 if (auto LC = dyn_cast<OMPLinearClause>(C))
4731 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4732 B.NumIterations, *this, CurScope))
4733 return StmtError();
4734 }
4735 }
4736
Alexey Bataev4acb8592014-07-07 13:01:15 +00004737 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004738 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004739 NestedLoopCount, Clauses, AStmt, B,
4740 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004741}
4742
Alexander Musmane4e893b2014-09-23 09:33:00 +00004743StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4744 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4745 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004746 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004747 if (!AStmt)
4748 return StmtError();
4749
Alexander Musmane4e893b2014-09-23 09:33:00 +00004750 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4751 // 1.2.2 OpenMP Language Terminology
4752 // Structured block - An executable statement with a single entry at the
4753 // top and a single exit at the bottom.
4754 // The point of exit cannot be a branch out of the structured block.
4755 // longjmp() and throw() must not violate the entry/exit criteria.
4756 CS->getCapturedDecl()->setNothrow();
4757
Alexander Musmanc6388682014-12-15 07:07:06 +00004758 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004759 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4760 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004761 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004762 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4763 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4764 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004765 if (NestedLoopCount == 0)
4766 return StmtError();
4767
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004768 if (!CurContext->isDependentContext()) {
4769 // Finalize the clauses that need pre-built expressions for CodeGen.
4770 for (auto C : Clauses) {
4771 if (auto LC = dyn_cast<OMPLinearClause>(C))
4772 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4773 B.NumIterations, *this, CurScope))
4774 return StmtError();
4775 }
4776 }
4777
Alexey Bataev66b15b52015-08-21 11:14:16 +00004778 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4779 // If both simdlen and safelen clauses are specified, the value of the simdlen
4780 // parameter must be less than or equal to the value of the safelen parameter.
4781 OMPSafelenClause *Safelen = nullptr;
4782 OMPSimdlenClause *Simdlen = nullptr;
4783 for (auto *Clause : Clauses) {
4784 if (Clause->getClauseKind() == OMPC_safelen)
4785 Safelen = cast<OMPSafelenClause>(Clause);
4786 else if (Clause->getClauseKind() == OMPC_simdlen)
4787 Simdlen = cast<OMPSimdlenClause>(Clause);
4788 if (Safelen && Simdlen)
4789 break;
4790 }
4791 if (Simdlen && Safelen &&
4792 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4793 Safelen->getSafelen()))
4794 return StmtError();
4795
Alexander Musmane4e893b2014-09-23 09:33:00 +00004796 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004797 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004798 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004799}
4800
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004801StmtResult
4802Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4803 Stmt *AStmt, SourceLocation StartLoc,
4804 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004805 if (!AStmt)
4806 return StmtError();
4807
4808 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004809 auto BaseStmt = AStmt;
4810 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4811 BaseStmt = CS->getCapturedStmt();
4812 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4813 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004814 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004815 return StmtError();
4816 // All associated statements must be '#pragma omp section' except for
4817 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004818 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004819 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4820 if (SectionStmt)
4821 Diag(SectionStmt->getLocStart(),
4822 diag::err_omp_parallel_sections_substmt_not_section);
4823 return StmtError();
4824 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004825 cast<OMPSectionDirective>(SectionStmt)
4826 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004827 }
4828 } else {
4829 Diag(AStmt->getLocStart(),
4830 diag::err_omp_parallel_sections_not_compound_stmt);
4831 return StmtError();
4832 }
4833
4834 getCurFunction()->setHasBranchProtectedScope();
4835
Alexey Bataev25e5b442015-09-15 12:52:43 +00004836 return OMPParallelSectionsDirective::Create(
4837 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004838}
4839
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004840StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4841 Stmt *AStmt, SourceLocation StartLoc,
4842 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004843 if (!AStmt)
4844 return StmtError();
4845
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004846 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4847 // 1.2.2 OpenMP Language Terminology
4848 // Structured block - An executable statement with a single entry at the
4849 // top and a single exit at the bottom.
4850 // The point of exit cannot be a branch out of the structured block.
4851 // longjmp() and throw() must not violate the entry/exit criteria.
4852 CS->getCapturedDecl()->setNothrow();
4853
4854 getCurFunction()->setHasBranchProtectedScope();
4855
Alexey Bataev25e5b442015-09-15 12:52:43 +00004856 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4857 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004858}
4859
Alexey Bataev68446b72014-07-18 07:47:19 +00004860StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4861 SourceLocation EndLoc) {
4862 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4863}
4864
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004865StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4866 SourceLocation EndLoc) {
4867 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4868}
4869
Alexey Bataev2df347a2014-07-18 10:17:07 +00004870StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4871 SourceLocation EndLoc) {
4872 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4873}
4874
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004875StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4876 SourceLocation StartLoc,
4877 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004878 if (!AStmt)
4879 return StmtError();
4880
4881 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004882
4883 getCurFunction()->setHasBranchProtectedScope();
4884
4885 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4886}
4887
Alexey Bataev6125da92014-07-21 11:26:11 +00004888StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4889 SourceLocation StartLoc,
4890 SourceLocation EndLoc) {
4891 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4892 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4893}
4894
Alexey Bataev346265e2015-09-25 10:37:12 +00004895StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4896 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004897 SourceLocation StartLoc,
4898 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004899 OMPClause *DependFound = nullptr;
4900 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004901 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004902 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004903 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004904 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004905 for (auto *C : Clauses) {
4906 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4907 DependFound = C;
4908 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4909 if (DependSourceClause) {
4910 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4911 << getOpenMPDirectiveName(OMPD_ordered)
4912 << getOpenMPClauseName(OMPC_depend) << 2;
4913 ErrorFound = true;
4914 } else
4915 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004916 if (DependSinkClause) {
4917 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4918 << 0;
4919 ErrorFound = true;
4920 }
4921 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4922 if (DependSourceClause) {
4923 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4924 << 1;
4925 ErrorFound = true;
4926 }
4927 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004928 }
4929 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004930 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004931 else if (C->getClauseKind() == OMPC_simd)
4932 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004933 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004934 if (!ErrorFound && !SC &&
4935 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004936 // OpenMP [2.8.1,simd Construct, Restrictions]
4937 // An ordered construct with the simd clause is the only OpenMP construct
4938 // that can appear in the simd region.
4939 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004940 ErrorFound = true;
4941 } else if (DependFound && (TC || SC)) {
4942 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4943 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4944 ErrorFound = true;
4945 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4946 Diag(DependFound->getLocStart(),
4947 diag::err_omp_ordered_directive_without_param);
4948 ErrorFound = true;
4949 } else if (TC || Clauses.empty()) {
4950 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4951 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4952 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4953 << (TC != nullptr);
4954 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4955 ErrorFound = true;
4956 }
4957 }
4958 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004959 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004960
4961 if (AStmt) {
4962 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4963
4964 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004965 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004966
4967 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004968}
4969
Alexey Bataev1d160b12015-03-13 12:27:31 +00004970namespace {
4971/// \brief Helper class for checking expression in 'omp atomic [update]'
4972/// construct.
4973class OpenMPAtomicUpdateChecker {
4974 /// \brief Error results for atomic update expressions.
4975 enum ExprAnalysisErrorCode {
4976 /// \brief A statement is not an expression statement.
4977 NotAnExpression,
4978 /// \brief Expression is not builtin binary or unary operation.
4979 NotABinaryOrUnaryExpression,
4980 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4981 NotAnUnaryIncDecExpression,
4982 /// \brief An expression is not of scalar type.
4983 NotAScalarType,
4984 /// \brief A binary operation is not an assignment operation.
4985 NotAnAssignmentOp,
4986 /// \brief RHS part of the binary operation is not a binary expression.
4987 NotABinaryExpression,
4988 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4989 /// expression.
4990 NotABinaryOperator,
4991 /// \brief RHS binary operation does not have reference to the updated LHS
4992 /// part.
4993 NotAnUpdateExpression,
4994 /// \brief No errors is found.
4995 NoError
4996 };
4997 /// \brief Reference to Sema.
4998 Sema &SemaRef;
4999 /// \brief A location for note diagnostics (when error is found).
5000 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005001 /// \brief 'x' lvalue part of the source atomic expression.
5002 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005003 /// \brief 'expr' rvalue part of the source atomic expression.
5004 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005005 /// \brief Helper expression of the form
5006 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5007 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5008 Expr *UpdateExpr;
5009 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5010 /// important for non-associative operations.
5011 bool IsXLHSInRHSPart;
5012 BinaryOperatorKind Op;
5013 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005014 /// \brief true if the source expression is a postfix unary operation, false
5015 /// if it is a prefix unary operation.
5016 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005017
5018public:
5019 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005020 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005021 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005022 /// \brief Check specified statement that it is suitable for 'atomic update'
5023 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005024 /// expression. If DiagId and NoteId == 0, then only check is performed
5025 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005026 /// \param DiagId Diagnostic which should be emitted if error is found.
5027 /// \param NoteId Diagnostic note for the main error message.
5028 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005029 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005030 /// \brief Return the 'x' lvalue part of the source atomic expression.
5031 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005032 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5033 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005034 /// \brief Return the update expression used in calculation of the updated
5035 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5036 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5037 Expr *getUpdateExpr() const { return UpdateExpr; }
5038 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5039 /// false otherwise.
5040 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5041
Alexey Bataevb78ca832015-04-01 03:33:17 +00005042 /// \brief true if the source expression is a postfix unary operation, false
5043 /// if it is a prefix unary operation.
5044 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5045
Alexey Bataev1d160b12015-03-13 12:27:31 +00005046private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005047 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5048 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005049};
5050} // namespace
5051
5052bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5053 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5054 ExprAnalysisErrorCode ErrorFound = NoError;
5055 SourceLocation ErrorLoc, NoteLoc;
5056 SourceRange ErrorRange, NoteRange;
5057 // Allowed constructs are:
5058 // x = x binop expr;
5059 // x = expr binop x;
5060 if (AtomicBinOp->getOpcode() == BO_Assign) {
5061 X = AtomicBinOp->getLHS();
5062 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5063 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5064 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5065 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5066 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005067 Op = AtomicInnerBinOp->getOpcode();
5068 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005069 auto *LHS = AtomicInnerBinOp->getLHS();
5070 auto *RHS = AtomicInnerBinOp->getRHS();
5071 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5072 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5073 /*Canonical=*/true);
5074 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5075 /*Canonical=*/true);
5076 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5077 /*Canonical=*/true);
5078 if (XId == LHSId) {
5079 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005080 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005081 } else if (XId == RHSId) {
5082 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005083 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005084 } else {
5085 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5086 ErrorRange = AtomicInnerBinOp->getSourceRange();
5087 NoteLoc = X->getExprLoc();
5088 NoteRange = X->getSourceRange();
5089 ErrorFound = NotAnUpdateExpression;
5090 }
5091 } else {
5092 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5093 ErrorRange = AtomicInnerBinOp->getSourceRange();
5094 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5095 NoteRange = SourceRange(NoteLoc, NoteLoc);
5096 ErrorFound = NotABinaryOperator;
5097 }
5098 } else {
5099 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5100 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5101 ErrorFound = NotABinaryExpression;
5102 }
5103 } else {
5104 ErrorLoc = AtomicBinOp->getExprLoc();
5105 ErrorRange = AtomicBinOp->getSourceRange();
5106 NoteLoc = AtomicBinOp->getOperatorLoc();
5107 NoteRange = SourceRange(NoteLoc, NoteLoc);
5108 ErrorFound = NotAnAssignmentOp;
5109 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005110 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005111 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5112 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5113 return true;
5114 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005115 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005116 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005117}
5118
5119bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5120 unsigned NoteId) {
5121 ExprAnalysisErrorCode ErrorFound = NoError;
5122 SourceLocation ErrorLoc, NoteLoc;
5123 SourceRange ErrorRange, NoteRange;
5124 // Allowed constructs are:
5125 // x++;
5126 // x--;
5127 // ++x;
5128 // --x;
5129 // x binop= expr;
5130 // x = x binop expr;
5131 // x = expr binop x;
5132 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5133 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5134 if (AtomicBody->getType()->isScalarType() ||
5135 AtomicBody->isInstantiationDependent()) {
5136 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5137 AtomicBody->IgnoreParenImpCasts())) {
5138 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005139 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005140 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005141 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005142 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005143 X = AtomicCompAssignOp->getLHS();
5144 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005145 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5146 AtomicBody->IgnoreParenImpCasts())) {
5147 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005148 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5149 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005150 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005151 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5152 // Check for Unary Operation
5153 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005154 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005155 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5156 OpLoc = AtomicUnaryOp->getOperatorLoc();
5157 X = AtomicUnaryOp->getSubExpr();
5158 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5159 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005160 } else {
5161 ErrorFound = NotAnUnaryIncDecExpression;
5162 ErrorLoc = AtomicUnaryOp->getExprLoc();
5163 ErrorRange = AtomicUnaryOp->getSourceRange();
5164 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5165 NoteRange = SourceRange(NoteLoc, NoteLoc);
5166 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005167 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005168 ErrorFound = NotABinaryOrUnaryExpression;
5169 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5170 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5171 }
5172 } else {
5173 ErrorFound = NotAScalarType;
5174 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5175 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5176 }
5177 } else {
5178 ErrorFound = NotAnExpression;
5179 NoteLoc = ErrorLoc = S->getLocStart();
5180 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5181 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005182 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005183 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5184 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5185 return true;
5186 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005187 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005188 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005189 // Build an update expression of form 'OpaqueValueExpr(x) binop
5190 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5191 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5192 auto *OVEX = new (SemaRef.getASTContext())
5193 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5194 auto *OVEExpr = new (SemaRef.getASTContext())
5195 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5196 auto Update =
5197 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5198 IsXLHSInRHSPart ? OVEExpr : OVEX);
5199 if (Update.isInvalid())
5200 return true;
5201 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5202 Sema::AA_Casting);
5203 if (Update.isInvalid())
5204 return true;
5205 UpdateExpr = Update.get();
5206 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005207 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005208}
5209
Alexey Bataev0162e452014-07-22 10:10:35 +00005210StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5211 Stmt *AStmt,
5212 SourceLocation StartLoc,
5213 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005214 if (!AStmt)
5215 return StmtError();
5216
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005217 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005218 // 1.2.2 OpenMP Language Terminology
5219 // Structured block - An executable statement with a single entry at the
5220 // top and a single exit at the bottom.
5221 // The point of exit cannot be a branch out of the structured block.
5222 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005223 OpenMPClauseKind AtomicKind = OMPC_unknown;
5224 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005225 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005226 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005227 C->getClauseKind() == OMPC_update ||
5228 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005229 if (AtomicKind != OMPC_unknown) {
5230 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5231 << SourceRange(C->getLocStart(), C->getLocEnd());
5232 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5233 << getOpenMPClauseName(AtomicKind);
5234 } else {
5235 AtomicKind = C->getClauseKind();
5236 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005237 }
5238 }
5239 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005240
Alexey Bataev459dec02014-07-24 06:46:57 +00005241 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005242 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5243 Body = EWC->getSubExpr();
5244
Alexey Bataev62cec442014-11-18 10:14:22 +00005245 Expr *X = nullptr;
5246 Expr *V = nullptr;
5247 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005248 Expr *UE = nullptr;
5249 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005250 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005251 // OpenMP [2.12.6, atomic Construct]
5252 // In the next expressions:
5253 // * x and v (as applicable) are both l-value expressions with scalar type.
5254 // * During the execution of an atomic region, multiple syntactic
5255 // occurrences of x must designate the same storage location.
5256 // * Neither of v and expr (as applicable) may access the storage location
5257 // designated by x.
5258 // * Neither of x and expr (as applicable) may access the storage location
5259 // designated by v.
5260 // * expr is an expression with scalar type.
5261 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5262 // * binop, binop=, ++, and -- are not overloaded operators.
5263 // * The expression x binop expr must be numerically equivalent to x binop
5264 // (expr). This requirement is satisfied if the operators in expr have
5265 // precedence greater than binop, or by using parentheses around expr or
5266 // subexpressions of expr.
5267 // * The expression expr binop x must be numerically equivalent to (expr)
5268 // binop x. This requirement is satisfied if the operators in expr have
5269 // precedence equal to or greater than binop, or by using parentheses around
5270 // expr or subexpressions of expr.
5271 // * For forms that allow multiple occurrences of x, the number of times
5272 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005273 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005274 enum {
5275 NotAnExpression,
5276 NotAnAssignmentOp,
5277 NotAScalarType,
5278 NotAnLValue,
5279 NoError
5280 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005281 SourceLocation ErrorLoc, NoteLoc;
5282 SourceRange ErrorRange, NoteRange;
5283 // If clause is read:
5284 // v = x;
5285 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5286 auto AtomicBinOp =
5287 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5288 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5289 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5290 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5291 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5292 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5293 if (!X->isLValue() || !V->isLValue()) {
5294 auto NotLValueExpr = X->isLValue() ? V : X;
5295 ErrorFound = NotAnLValue;
5296 ErrorLoc = AtomicBinOp->getExprLoc();
5297 ErrorRange = AtomicBinOp->getSourceRange();
5298 NoteLoc = NotLValueExpr->getExprLoc();
5299 NoteRange = NotLValueExpr->getSourceRange();
5300 }
5301 } else if (!X->isInstantiationDependent() ||
5302 !V->isInstantiationDependent()) {
5303 auto NotScalarExpr =
5304 (X->isInstantiationDependent() || X->getType()->isScalarType())
5305 ? V
5306 : X;
5307 ErrorFound = NotAScalarType;
5308 ErrorLoc = AtomicBinOp->getExprLoc();
5309 ErrorRange = AtomicBinOp->getSourceRange();
5310 NoteLoc = NotScalarExpr->getExprLoc();
5311 NoteRange = NotScalarExpr->getSourceRange();
5312 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005313 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005314 ErrorFound = NotAnAssignmentOp;
5315 ErrorLoc = AtomicBody->getExprLoc();
5316 ErrorRange = AtomicBody->getSourceRange();
5317 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5318 : AtomicBody->getExprLoc();
5319 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5320 : AtomicBody->getSourceRange();
5321 }
5322 } else {
5323 ErrorFound = NotAnExpression;
5324 NoteLoc = ErrorLoc = Body->getLocStart();
5325 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005326 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005327 if (ErrorFound != NoError) {
5328 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5329 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005330 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5331 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005332 return StmtError();
5333 } else if (CurContext->isDependentContext())
5334 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005335 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005336 enum {
5337 NotAnExpression,
5338 NotAnAssignmentOp,
5339 NotAScalarType,
5340 NotAnLValue,
5341 NoError
5342 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005343 SourceLocation ErrorLoc, NoteLoc;
5344 SourceRange ErrorRange, NoteRange;
5345 // If clause is write:
5346 // x = expr;
5347 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5348 auto AtomicBinOp =
5349 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5350 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005351 X = AtomicBinOp->getLHS();
5352 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005353 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5354 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5355 if (!X->isLValue()) {
5356 ErrorFound = NotAnLValue;
5357 ErrorLoc = AtomicBinOp->getExprLoc();
5358 ErrorRange = AtomicBinOp->getSourceRange();
5359 NoteLoc = X->getExprLoc();
5360 NoteRange = X->getSourceRange();
5361 }
5362 } else if (!X->isInstantiationDependent() ||
5363 !E->isInstantiationDependent()) {
5364 auto NotScalarExpr =
5365 (X->isInstantiationDependent() || X->getType()->isScalarType())
5366 ? E
5367 : X;
5368 ErrorFound = NotAScalarType;
5369 ErrorLoc = AtomicBinOp->getExprLoc();
5370 ErrorRange = AtomicBinOp->getSourceRange();
5371 NoteLoc = NotScalarExpr->getExprLoc();
5372 NoteRange = NotScalarExpr->getSourceRange();
5373 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005374 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005375 ErrorFound = NotAnAssignmentOp;
5376 ErrorLoc = AtomicBody->getExprLoc();
5377 ErrorRange = AtomicBody->getSourceRange();
5378 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5379 : AtomicBody->getExprLoc();
5380 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5381 : AtomicBody->getSourceRange();
5382 }
5383 } else {
5384 ErrorFound = NotAnExpression;
5385 NoteLoc = ErrorLoc = Body->getLocStart();
5386 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005387 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005388 if (ErrorFound != NoError) {
5389 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5390 << ErrorRange;
5391 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5392 << NoteRange;
5393 return StmtError();
5394 } else if (CurContext->isDependentContext())
5395 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005396 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005397 // If clause is update:
5398 // x++;
5399 // x--;
5400 // ++x;
5401 // --x;
5402 // x binop= expr;
5403 // x = x binop expr;
5404 // x = expr binop x;
5405 OpenMPAtomicUpdateChecker Checker(*this);
5406 if (Checker.checkStatement(
5407 Body, (AtomicKind == OMPC_update)
5408 ? diag::err_omp_atomic_update_not_expression_statement
5409 : diag::err_omp_atomic_not_expression_statement,
5410 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005411 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005412 if (!CurContext->isDependentContext()) {
5413 E = Checker.getExpr();
5414 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005415 UE = Checker.getUpdateExpr();
5416 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005417 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005418 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005419 enum {
5420 NotAnAssignmentOp,
5421 NotACompoundStatement,
5422 NotTwoSubstatements,
5423 NotASpecificExpression,
5424 NoError
5425 } ErrorFound = NoError;
5426 SourceLocation ErrorLoc, NoteLoc;
5427 SourceRange ErrorRange, NoteRange;
5428 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5429 // If clause is a capture:
5430 // v = x++;
5431 // v = x--;
5432 // v = ++x;
5433 // v = --x;
5434 // v = x binop= expr;
5435 // v = x = x binop expr;
5436 // v = x = expr binop x;
5437 auto *AtomicBinOp =
5438 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5439 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5440 V = AtomicBinOp->getLHS();
5441 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5442 OpenMPAtomicUpdateChecker Checker(*this);
5443 if (Checker.checkStatement(
5444 Body, diag::err_omp_atomic_capture_not_expression_statement,
5445 diag::note_omp_atomic_update))
5446 return StmtError();
5447 E = Checker.getExpr();
5448 X = Checker.getX();
5449 UE = Checker.getUpdateExpr();
5450 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5451 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005452 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005453 ErrorLoc = AtomicBody->getExprLoc();
5454 ErrorRange = AtomicBody->getSourceRange();
5455 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5456 : AtomicBody->getExprLoc();
5457 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5458 : AtomicBody->getSourceRange();
5459 ErrorFound = NotAnAssignmentOp;
5460 }
5461 if (ErrorFound != NoError) {
5462 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5463 << ErrorRange;
5464 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5465 return StmtError();
5466 } else if (CurContext->isDependentContext()) {
5467 UE = V = E = X = nullptr;
5468 }
5469 } else {
5470 // If clause is a capture:
5471 // { v = x; x = expr; }
5472 // { v = x; x++; }
5473 // { v = x; x--; }
5474 // { v = x; ++x; }
5475 // { v = x; --x; }
5476 // { v = x; x binop= expr; }
5477 // { v = x; x = x binop expr; }
5478 // { v = x; x = expr binop x; }
5479 // { x++; v = x; }
5480 // { x--; v = x; }
5481 // { ++x; v = x; }
5482 // { --x; v = x; }
5483 // { x binop= expr; v = x; }
5484 // { x = x binop expr; v = x; }
5485 // { x = expr binop x; v = x; }
5486 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5487 // Check that this is { expr1; expr2; }
5488 if (CS->size() == 2) {
5489 auto *First = CS->body_front();
5490 auto *Second = CS->body_back();
5491 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5492 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5493 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5494 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5495 // Need to find what subexpression is 'v' and what is 'x'.
5496 OpenMPAtomicUpdateChecker Checker(*this);
5497 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5498 BinaryOperator *BinOp = nullptr;
5499 if (IsUpdateExprFound) {
5500 BinOp = dyn_cast<BinaryOperator>(First);
5501 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5502 }
5503 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5504 // { v = x; x++; }
5505 // { v = x; x--; }
5506 // { v = x; ++x; }
5507 // { v = x; --x; }
5508 // { v = x; x binop= expr; }
5509 // { v = x; x = x binop expr; }
5510 // { v = x; x = expr binop x; }
5511 // Check that the first expression has form v = x.
5512 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5513 llvm::FoldingSetNodeID XId, PossibleXId;
5514 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5515 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5516 IsUpdateExprFound = XId == PossibleXId;
5517 if (IsUpdateExprFound) {
5518 V = BinOp->getLHS();
5519 X = Checker.getX();
5520 E = Checker.getExpr();
5521 UE = Checker.getUpdateExpr();
5522 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005523 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005524 }
5525 }
5526 if (!IsUpdateExprFound) {
5527 IsUpdateExprFound = !Checker.checkStatement(First);
5528 BinOp = nullptr;
5529 if (IsUpdateExprFound) {
5530 BinOp = dyn_cast<BinaryOperator>(Second);
5531 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5532 }
5533 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5534 // { x++; v = x; }
5535 // { x--; v = x; }
5536 // { ++x; v = x; }
5537 // { --x; v = x; }
5538 // { x binop= expr; v = x; }
5539 // { x = x binop expr; v = x; }
5540 // { x = expr binop x; v = x; }
5541 // Check that the second expression has form v = x.
5542 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5543 llvm::FoldingSetNodeID XId, PossibleXId;
5544 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5545 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5546 IsUpdateExprFound = XId == PossibleXId;
5547 if (IsUpdateExprFound) {
5548 V = BinOp->getLHS();
5549 X = Checker.getX();
5550 E = Checker.getExpr();
5551 UE = Checker.getUpdateExpr();
5552 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005553 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005554 }
5555 }
5556 }
5557 if (!IsUpdateExprFound) {
5558 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005559 auto *FirstExpr = dyn_cast<Expr>(First);
5560 auto *SecondExpr = dyn_cast<Expr>(Second);
5561 if (!FirstExpr || !SecondExpr ||
5562 !(FirstExpr->isInstantiationDependent() ||
5563 SecondExpr->isInstantiationDependent())) {
5564 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5565 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005566 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005567 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5568 : First->getLocStart();
5569 NoteRange = ErrorRange = FirstBinOp
5570 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005571 : SourceRange(ErrorLoc, ErrorLoc);
5572 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005573 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5574 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5575 ErrorFound = NotAnAssignmentOp;
5576 NoteLoc = ErrorLoc = SecondBinOp
5577 ? SecondBinOp->getOperatorLoc()
5578 : Second->getLocStart();
5579 NoteRange = ErrorRange =
5580 SecondBinOp ? SecondBinOp->getSourceRange()
5581 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005582 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005583 auto *PossibleXRHSInFirst =
5584 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5585 auto *PossibleXLHSInSecond =
5586 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5587 llvm::FoldingSetNodeID X1Id, X2Id;
5588 PossibleXRHSInFirst->Profile(X1Id, Context,
5589 /*Canonical=*/true);
5590 PossibleXLHSInSecond->Profile(X2Id, Context,
5591 /*Canonical=*/true);
5592 IsUpdateExprFound = X1Id == X2Id;
5593 if (IsUpdateExprFound) {
5594 V = FirstBinOp->getLHS();
5595 X = SecondBinOp->getLHS();
5596 E = SecondBinOp->getRHS();
5597 UE = nullptr;
5598 IsXLHSInRHSPart = false;
5599 IsPostfixUpdate = true;
5600 } else {
5601 ErrorFound = NotASpecificExpression;
5602 ErrorLoc = FirstBinOp->getExprLoc();
5603 ErrorRange = FirstBinOp->getSourceRange();
5604 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5605 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5606 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005607 }
5608 }
5609 }
5610 }
5611 } else {
5612 NoteLoc = ErrorLoc = Body->getLocStart();
5613 NoteRange = ErrorRange =
5614 SourceRange(Body->getLocStart(), Body->getLocStart());
5615 ErrorFound = NotTwoSubstatements;
5616 }
5617 } else {
5618 NoteLoc = ErrorLoc = Body->getLocStart();
5619 NoteRange = ErrorRange =
5620 SourceRange(Body->getLocStart(), Body->getLocStart());
5621 ErrorFound = NotACompoundStatement;
5622 }
5623 if (ErrorFound != NoError) {
5624 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5625 << ErrorRange;
5626 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5627 return StmtError();
5628 } else if (CurContext->isDependentContext()) {
5629 UE = V = E = X = nullptr;
5630 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005631 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005632 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005633
5634 getCurFunction()->setHasBranchProtectedScope();
5635
Alexey Bataev62cec442014-11-18 10:14:22 +00005636 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005637 X, V, E, UE, IsXLHSInRHSPart,
5638 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005639}
5640
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005641StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5642 Stmt *AStmt,
5643 SourceLocation StartLoc,
5644 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005645 if (!AStmt)
5646 return StmtError();
5647
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005648 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5649 // 1.2.2 OpenMP Language Terminology
5650 // Structured block - An executable statement with a single entry at the
5651 // top and a single exit at the bottom.
5652 // The point of exit cannot be a branch out of the structured block.
5653 // longjmp() and throw() must not violate the entry/exit criteria.
5654 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005655
Alexey Bataev13314bf2014-10-09 04:18:56 +00005656 // OpenMP [2.16, Nesting of Regions]
5657 // If specified, a teams construct must be contained within a target
5658 // construct. That target construct must contain no statements or directives
5659 // outside of the teams construct.
5660 if (DSAStack->hasInnerTeamsRegion()) {
5661 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5662 bool OMPTeamsFound = true;
5663 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5664 auto I = CS->body_begin();
5665 while (I != CS->body_end()) {
5666 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5667 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5668 OMPTeamsFound = false;
5669 break;
5670 }
5671 ++I;
5672 }
5673 assert(I != CS->body_end() && "Not found statement");
5674 S = *I;
5675 }
5676 if (!OMPTeamsFound) {
5677 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5678 Diag(DSAStack->getInnerTeamsRegionLoc(),
5679 diag::note_omp_nested_teams_construct_here);
5680 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5681 << isa<OMPExecutableDirective>(S);
5682 return StmtError();
5683 }
5684 }
5685
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005686 getCurFunction()->setHasBranchProtectedScope();
5687
5688 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5689}
5690
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005691StmtResult
5692Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5693 Stmt *AStmt, SourceLocation StartLoc,
5694 SourceLocation EndLoc) {
5695 if (!AStmt)
5696 return StmtError();
5697
5698 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5699 // 1.2.2 OpenMP Language Terminology
5700 // Structured block - An executable statement with a single entry at the
5701 // top and a single exit at the bottom.
5702 // The point of exit cannot be a branch out of the structured block.
5703 // longjmp() and throw() must not violate the entry/exit criteria.
5704 CS->getCapturedDecl()->setNothrow();
5705
5706 getCurFunction()->setHasBranchProtectedScope();
5707
5708 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5709 AStmt);
5710}
5711
Samuel Antaodf67fc42016-01-19 19:15:56 +00005712/// \brief Check for existence of a map clause in the list of clauses.
5713static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5714 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5715 I != E; ++I) {
5716 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5717 return true;
5718 }
5719 }
5720
5721 return false;
5722}
5723
Michael Wong65f367f2015-07-21 13:44:28 +00005724StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5725 Stmt *AStmt,
5726 SourceLocation StartLoc,
5727 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005728 if (!AStmt)
5729 return StmtError();
5730
5731 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5732
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005733 // OpenMP [2.10.1, Restrictions, p. 97]
5734 // At least one map clause must appear on the directive.
5735 if (!HasMapClause(Clauses)) {
5736 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5737 getOpenMPDirectiveName(OMPD_target_data);
5738 return StmtError();
5739 }
5740
Michael Wong65f367f2015-07-21 13:44:28 +00005741 getCurFunction()->setHasBranchProtectedScope();
5742
5743 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5744 AStmt);
5745}
5746
Samuel Antaodf67fc42016-01-19 19:15:56 +00005747StmtResult
5748Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5749 SourceLocation StartLoc,
5750 SourceLocation EndLoc) {
5751 // OpenMP [2.10.2, Restrictions, p. 99]
5752 // At least one map clause must appear on the directive.
5753 if (!HasMapClause(Clauses)) {
5754 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5755 << getOpenMPDirectiveName(OMPD_target_enter_data);
5756 return StmtError();
5757 }
5758
5759 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5760 Clauses);
5761}
5762
Samuel Antao72590762016-01-19 20:04:50 +00005763StmtResult
5764Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5765 SourceLocation StartLoc,
5766 SourceLocation EndLoc) {
5767 // OpenMP [2.10.3, Restrictions, p. 102]
5768 // At least one map clause must appear on the directive.
5769 if (!HasMapClause(Clauses)) {
5770 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5771 << getOpenMPDirectiveName(OMPD_target_exit_data);
5772 return StmtError();
5773 }
5774
5775 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5776}
5777
Alexey Bataev13314bf2014-10-09 04:18:56 +00005778StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5779 Stmt *AStmt, SourceLocation StartLoc,
5780 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005781 if (!AStmt)
5782 return StmtError();
5783
Alexey Bataev13314bf2014-10-09 04:18:56 +00005784 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5785 // 1.2.2 OpenMP Language Terminology
5786 // Structured block - An executable statement with a single entry at the
5787 // top and a single exit at the bottom.
5788 // The point of exit cannot be a branch out of the structured block.
5789 // longjmp() and throw() must not violate the entry/exit criteria.
5790 CS->getCapturedDecl()->setNothrow();
5791
5792 getCurFunction()->setHasBranchProtectedScope();
5793
5794 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5795}
5796
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005797StmtResult
5798Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5799 SourceLocation EndLoc,
5800 OpenMPDirectiveKind CancelRegion) {
5801 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5802 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5803 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5804 << getOpenMPDirectiveName(CancelRegion);
5805 return StmtError();
5806 }
5807 if (DSAStack->isParentNowaitRegion()) {
5808 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5809 return StmtError();
5810 }
5811 if (DSAStack->isParentOrderedRegion()) {
5812 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5813 return StmtError();
5814 }
5815 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5816 CancelRegion);
5817}
5818
Alexey Bataev87933c72015-09-18 08:07:34 +00005819StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5820 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005821 SourceLocation EndLoc,
5822 OpenMPDirectiveKind CancelRegion) {
5823 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5824 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5825 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5826 << getOpenMPDirectiveName(CancelRegion);
5827 return StmtError();
5828 }
5829 if (DSAStack->isParentNowaitRegion()) {
5830 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5831 return StmtError();
5832 }
5833 if (DSAStack->isParentOrderedRegion()) {
5834 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5835 return StmtError();
5836 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005837 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005838 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5839 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005840}
5841
Alexey Bataev382967a2015-12-08 12:06:20 +00005842static bool checkGrainsizeNumTasksClauses(Sema &S,
5843 ArrayRef<OMPClause *> Clauses) {
5844 OMPClause *PrevClause = nullptr;
5845 bool ErrorFound = false;
5846 for (auto *C : Clauses) {
5847 if (C->getClauseKind() == OMPC_grainsize ||
5848 C->getClauseKind() == OMPC_num_tasks) {
5849 if (!PrevClause)
5850 PrevClause = C;
5851 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5852 S.Diag(C->getLocStart(),
5853 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5854 << getOpenMPClauseName(C->getClauseKind())
5855 << getOpenMPClauseName(PrevClause->getClauseKind());
5856 S.Diag(PrevClause->getLocStart(),
5857 diag::note_omp_previous_grainsize_num_tasks)
5858 << getOpenMPClauseName(PrevClause->getClauseKind());
5859 ErrorFound = true;
5860 }
5861 }
5862 }
5863 return ErrorFound;
5864}
5865
Alexey Bataev49f6e782015-12-01 04:18:41 +00005866StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5867 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5868 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005869 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005870 if (!AStmt)
5871 return StmtError();
5872
5873 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5874 OMPLoopDirective::HelperExprs B;
5875 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5876 // define the nested loops number.
5877 unsigned NestedLoopCount =
5878 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005879 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005880 VarsWithImplicitDSA, B);
5881 if (NestedLoopCount == 0)
5882 return StmtError();
5883
5884 assert((CurContext->isDependentContext() || B.builtAll()) &&
5885 "omp for loop exprs were not built");
5886
Alexey Bataev382967a2015-12-08 12:06:20 +00005887 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5888 // The grainsize clause and num_tasks clause are mutually exclusive and may
5889 // not appear on the same taskloop directive.
5890 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5891 return StmtError();
5892
Alexey Bataev49f6e782015-12-01 04:18:41 +00005893 getCurFunction()->setHasBranchProtectedScope();
5894 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5895 NestedLoopCount, Clauses, AStmt, B);
5896}
5897
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005898StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5899 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5900 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005901 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005902 if (!AStmt)
5903 return StmtError();
5904
5905 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5906 OMPLoopDirective::HelperExprs B;
5907 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5908 // define the nested loops number.
5909 unsigned NestedLoopCount =
5910 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5911 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5912 VarsWithImplicitDSA, B);
5913 if (NestedLoopCount == 0)
5914 return StmtError();
5915
5916 assert((CurContext->isDependentContext() || B.builtAll()) &&
5917 "omp for loop exprs were not built");
5918
Alexey Bataev382967a2015-12-08 12:06:20 +00005919 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5920 // The grainsize clause and num_tasks clause are mutually exclusive and may
5921 // not appear on the same taskloop directive.
5922 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5923 return StmtError();
5924
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005925 getCurFunction()->setHasBranchProtectedScope();
5926 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5927 NestedLoopCount, Clauses, AStmt, B);
5928}
5929
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005930StmtResult Sema::ActOnOpenMPDistributeDirective(
5931 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5932 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005933 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005934 if (!AStmt)
5935 return StmtError();
5936
5937 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5938 OMPLoopDirective::HelperExprs B;
5939 // In presence of clause 'collapse' with number of loops, it will
5940 // define the nested loops number.
5941 unsigned NestedLoopCount =
5942 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5943 nullptr /*ordered not a clause on distribute*/, AStmt,
5944 *this, *DSAStack, VarsWithImplicitDSA, B);
5945 if (NestedLoopCount == 0)
5946 return StmtError();
5947
5948 assert((CurContext->isDependentContext() || B.builtAll()) &&
5949 "omp for loop exprs were not built");
5950
5951 getCurFunction()->setHasBranchProtectedScope();
5952 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5953 NestedLoopCount, Clauses, AStmt, B);
5954}
5955
Alexey Bataeved09d242014-05-28 05:53:51 +00005956OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005957 SourceLocation StartLoc,
5958 SourceLocation LParenLoc,
5959 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005960 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005961 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005962 case OMPC_final:
5963 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5964 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005965 case OMPC_num_threads:
5966 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5967 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005968 case OMPC_safelen:
5969 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5970 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005971 case OMPC_simdlen:
5972 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5973 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005974 case OMPC_collapse:
5975 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5976 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005977 case OMPC_ordered:
5978 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5979 break;
Michael Wonge710d542015-08-07 16:16:36 +00005980 case OMPC_device:
5981 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5982 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005983 case OMPC_num_teams:
5984 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5985 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005986 case OMPC_thread_limit:
5987 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5988 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005989 case OMPC_priority:
5990 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5991 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005992 case OMPC_grainsize:
5993 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5994 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005995 case OMPC_num_tasks:
5996 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5997 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005998 case OMPC_hint:
5999 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6000 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006001 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006002 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006003 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006004 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006005 case OMPC_private:
6006 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006007 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006008 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006009 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006010 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006011 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006012 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006013 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006014 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006015 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006016 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006017 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006018 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006019 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006020 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006021 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006022 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006023 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006024 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006025 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006026 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006027 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006028 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006029 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006030 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006031 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006032 llvm_unreachable("Clause is not allowed.");
6033 }
6034 return Res;
6035}
6036
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006037OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6038 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006039 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006040 SourceLocation NameModifierLoc,
6041 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006042 SourceLocation EndLoc) {
6043 Expr *ValExpr = Condition;
6044 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6045 !Condition->isInstantiationDependent() &&
6046 !Condition->containsUnexpandedParameterPack()) {
6047 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006048 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006049 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006050 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006051
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006052 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006053 }
6054
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006055 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6056 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006057}
6058
Alexey Bataev3778b602014-07-17 07:32:53 +00006059OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6060 SourceLocation StartLoc,
6061 SourceLocation LParenLoc,
6062 SourceLocation EndLoc) {
6063 Expr *ValExpr = Condition;
6064 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6065 !Condition->isInstantiationDependent() &&
6066 !Condition->containsUnexpandedParameterPack()) {
6067 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6068 Condition->getExprLoc(), Condition);
6069 if (Val.isInvalid())
6070 return nullptr;
6071
6072 ValExpr = Val.get();
6073 }
6074
6075 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6076}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006077ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6078 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006079 if (!Op)
6080 return ExprError();
6081
6082 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6083 public:
6084 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006085 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006086 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6087 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006088 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6089 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006090 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6091 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006092 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6093 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006094 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6095 QualType T,
6096 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006097 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6098 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006099 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6100 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006101 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006102 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006103 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006104 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6105 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006106 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6107 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006108 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6109 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006110 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006111 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006112 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006113 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6114 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006115 llvm_unreachable("conversion functions are permitted");
6116 }
6117 } ConvertDiagnoser;
6118 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6119}
6120
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006121static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006122 OpenMPClauseKind CKind,
6123 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006124 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6125 !ValExpr->isInstantiationDependent()) {
6126 SourceLocation Loc = ValExpr->getExprLoc();
6127 ExprResult Value =
6128 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6129 if (Value.isInvalid())
6130 return false;
6131
6132 ValExpr = Value.get();
6133 // The expression must evaluate to a non-negative integer value.
6134 llvm::APSInt Result;
6135 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006136 Result.isSigned() &&
6137 !((!StrictlyPositive && Result.isNonNegative()) ||
6138 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006139 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006140 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6141 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006142 return false;
6143 }
6144 }
6145 return true;
6146}
6147
Alexey Bataev568a8332014-03-06 06:15:19 +00006148OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6149 SourceLocation StartLoc,
6150 SourceLocation LParenLoc,
6151 SourceLocation EndLoc) {
6152 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006153
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006154 // OpenMP [2.5, Restrictions]
6155 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006156 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6157 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006158 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006159
Alexey Bataeved09d242014-05-28 05:53:51 +00006160 return new (Context)
6161 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006162}
6163
Alexey Bataev62c87d22014-03-21 04:51:18 +00006164ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006165 OpenMPClauseKind CKind,
6166 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006167 if (!E)
6168 return ExprError();
6169 if (E->isValueDependent() || E->isTypeDependent() ||
6170 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006171 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006172 llvm::APSInt Result;
6173 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6174 if (ICE.isInvalid())
6175 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006176 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6177 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006178 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006179 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6180 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006181 return ExprError();
6182 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006183 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6184 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6185 << E->getSourceRange();
6186 return ExprError();
6187 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006188 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6189 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006190 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006191 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006192 return ICE;
6193}
6194
6195OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6196 SourceLocation LParenLoc,
6197 SourceLocation EndLoc) {
6198 // OpenMP [2.8.1, simd construct, Description]
6199 // The parameter of the safelen clause must be a constant
6200 // positive integer expression.
6201 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6202 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006203 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006204 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006205 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006206}
6207
Alexey Bataev66b15b52015-08-21 11:14:16 +00006208OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6209 SourceLocation LParenLoc,
6210 SourceLocation EndLoc) {
6211 // OpenMP [2.8.1, simd construct, Description]
6212 // The parameter of the simdlen clause must be a constant
6213 // positive integer expression.
6214 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6215 if (Simdlen.isInvalid())
6216 return nullptr;
6217 return new (Context)
6218 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6219}
6220
Alexander Musman64d33f12014-06-04 07:53:32 +00006221OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6222 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006223 SourceLocation LParenLoc,
6224 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006225 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006226 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006227 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006228 // The parameter of the collapse clause must be a constant
6229 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006230 ExprResult NumForLoopsResult =
6231 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6232 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006233 return nullptr;
6234 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006235 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006236}
6237
Alexey Bataev10e775f2015-07-30 11:36:16 +00006238OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6239 SourceLocation EndLoc,
6240 SourceLocation LParenLoc,
6241 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006242 // OpenMP [2.7.1, loop construct, Description]
6243 // OpenMP [2.8.1, simd construct, Description]
6244 // OpenMP [2.9.6, distribute construct, Description]
6245 // The parameter of the ordered clause must be a constant
6246 // positive integer expression if any.
6247 if (NumForLoops && LParenLoc.isValid()) {
6248 ExprResult NumForLoopsResult =
6249 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6250 if (NumForLoopsResult.isInvalid())
6251 return nullptr;
6252 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006253 } else
6254 NumForLoops = nullptr;
6255 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006256 return new (Context)
6257 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6258}
6259
Alexey Bataeved09d242014-05-28 05:53:51 +00006260OMPClause *Sema::ActOnOpenMPSimpleClause(
6261 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6262 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006263 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006264 switch (Kind) {
6265 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006266 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006267 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6268 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006269 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006270 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006271 Res = ActOnOpenMPProcBindClause(
6272 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6273 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006274 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006275 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006276 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006277 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006278 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006279 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006280 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006281 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006282 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006283 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006284 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006285 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006286 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006287 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006288 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006289 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006290 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006291 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006292 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006293 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006294 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006295 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006296 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006297 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006298 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006299 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006300 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006301 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006302 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006303 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006304 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006305 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006306 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006307 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006308 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006309 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006310 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006311 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006312 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006313 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006314 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006315 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006316 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006317 llvm_unreachable("Clause is not allowed.");
6318 }
6319 return Res;
6320}
6321
Alexey Bataev6402bca2015-12-28 07:25:51 +00006322static std::string
6323getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6324 ArrayRef<unsigned> Exclude = llvm::None) {
6325 std::string Values;
6326 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6327 unsigned Skipped = Exclude.size();
6328 auto S = Exclude.begin(), E = Exclude.end();
6329 for (unsigned i = First; i < Last; ++i) {
6330 if (std::find(S, E, i) != E) {
6331 --Skipped;
6332 continue;
6333 }
6334 Values += "'";
6335 Values += getOpenMPSimpleClauseTypeName(K, i);
6336 Values += "'";
6337 if (i == Bound - Skipped)
6338 Values += " or ";
6339 else if (i != Bound + 1 - Skipped)
6340 Values += ", ";
6341 }
6342 return Values;
6343}
6344
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006345OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6346 SourceLocation KindKwLoc,
6347 SourceLocation StartLoc,
6348 SourceLocation LParenLoc,
6349 SourceLocation EndLoc) {
6350 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006351 static_assert(OMPC_DEFAULT_unknown > 0,
6352 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006353 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006354 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6355 /*Last=*/OMPC_DEFAULT_unknown)
6356 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006357 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006358 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006359 switch (Kind) {
6360 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006361 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006362 break;
6363 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006364 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006365 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006366 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006367 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006368 break;
6369 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006370 return new (Context)
6371 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006372}
6373
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006374OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6375 SourceLocation KindKwLoc,
6376 SourceLocation StartLoc,
6377 SourceLocation LParenLoc,
6378 SourceLocation EndLoc) {
6379 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006380 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006381 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6382 /*Last=*/OMPC_PROC_BIND_unknown)
6383 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006384 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006385 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006386 return new (Context)
6387 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006388}
6389
Alexey Bataev56dafe82014-06-20 07:16:17 +00006390OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006391 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006392 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006393 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006394 SourceLocation EndLoc) {
6395 OMPClause *Res = nullptr;
6396 switch (Kind) {
6397 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006398 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6399 assert(Argument.size() == NumberOfElements &&
6400 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006401 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006402 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6403 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6404 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6405 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6406 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006407 break;
6408 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006409 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6410 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6411 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6412 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006413 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006414 case OMPC_dist_schedule:
6415 Res = ActOnOpenMPDistScheduleClause(
6416 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6417 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6418 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006419 case OMPC_defaultmap:
6420 enum { Modifier, DefaultmapKind };
6421 Res = ActOnOpenMPDefaultmapClause(
6422 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6423 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6424 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6425 ArgumentLoc[DefaultmapKind], EndLoc);
6426 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006427 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006428 case OMPC_num_threads:
6429 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006430 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006431 case OMPC_collapse:
6432 case OMPC_default:
6433 case OMPC_proc_bind:
6434 case OMPC_private:
6435 case OMPC_firstprivate:
6436 case OMPC_lastprivate:
6437 case OMPC_shared:
6438 case OMPC_reduction:
6439 case OMPC_linear:
6440 case OMPC_aligned:
6441 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006442 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006443 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006444 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006445 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006446 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006447 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006448 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006449 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006450 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006451 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006452 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006453 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006454 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006455 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006456 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006457 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006458 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006459 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006460 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006461 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006462 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006463 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006464 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006465 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006466 case OMPC_unknown:
6467 llvm_unreachable("Clause is not allowed.");
6468 }
6469 return Res;
6470}
6471
Alexey Bataev6402bca2015-12-28 07:25:51 +00006472static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6473 OpenMPScheduleClauseModifier M2,
6474 SourceLocation M1Loc, SourceLocation M2Loc) {
6475 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6476 SmallVector<unsigned, 2> Excluded;
6477 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6478 Excluded.push_back(M2);
6479 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6480 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6481 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6482 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6483 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6484 << getListOfPossibleValues(OMPC_schedule,
6485 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6486 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6487 Excluded)
6488 << getOpenMPClauseName(OMPC_schedule);
6489 return true;
6490 }
6491 return false;
6492}
6493
Alexey Bataev56dafe82014-06-20 07:16:17 +00006494OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006495 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006496 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006497 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6498 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6499 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6500 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6501 return nullptr;
6502 // OpenMP, 2.7.1, Loop Construct, Restrictions
6503 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6504 // but not both.
6505 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6506 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6507 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6508 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6509 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6510 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6511 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6512 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6513 return nullptr;
6514 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006515 if (Kind == OMPC_SCHEDULE_unknown) {
6516 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006517 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6518 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6519 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6520 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6521 Exclude);
6522 } else {
6523 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6524 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006525 }
6526 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6527 << Values << getOpenMPClauseName(OMPC_schedule);
6528 return nullptr;
6529 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006530 // OpenMP, 2.7.1, Loop Construct, Restrictions
6531 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6532 // schedule(guided).
6533 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6534 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6535 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6536 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6537 diag::err_omp_schedule_nonmonotonic_static);
6538 return nullptr;
6539 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006540 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006541 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006542 if (ChunkSize) {
6543 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6544 !ChunkSize->isInstantiationDependent() &&
6545 !ChunkSize->containsUnexpandedParameterPack()) {
6546 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6547 ExprResult Val =
6548 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6549 if (Val.isInvalid())
6550 return nullptr;
6551
6552 ValExpr = Val.get();
6553
6554 // OpenMP [2.7.1, Restrictions]
6555 // chunk_size must be a loop invariant integer expression with a positive
6556 // value.
6557 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006558 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6559 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6560 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006561 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006562 return nullptr;
6563 }
6564 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6565 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6566 ChunkSize->getType(), ".chunk.");
6567 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6568 ChunkSize->getExprLoc(),
6569 /*RefersToCapture=*/true);
6570 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006571 }
6572 }
6573 }
6574
Alexey Bataev6402bca2015-12-28 07:25:51 +00006575 return new (Context)
6576 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6577 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006578}
6579
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006580OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6581 SourceLocation StartLoc,
6582 SourceLocation EndLoc) {
6583 OMPClause *Res = nullptr;
6584 switch (Kind) {
6585 case OMPC_ordered:
6586 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6587 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006588 case OMPC_nowait:
6589 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6590 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006591 case OMPC_untied:
6592 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6593 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006594 case OMPC_mergeable:
6595 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6596 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006597 case OMPC_read:
6598 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6599 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006600 case OMPC_write:
6601 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6602 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006603 case OMPC_update:
6604 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6605 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006606 case OMPC_capture:
6607 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6608 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006609 case OMPC_seq_cst:
6610 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6611 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006612 case OMPC_threads:
6613 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6614 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006615 case OMPC_simd:
6616 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6617 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006618 case OMPC_nogroup:
6619 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6620 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006621 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006622 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006623 case OMPC_num_threads:
6624 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006625 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006626 case OMPC_collapse:
6627 case OMPC_schedule:
6628 case OMPC_private:
6629 case OMPC_firstprivate:
6630 case OMPC_lastprivate:
6631 case OMPC_shared:
6632 case OMPC_reduction:
6633 case OMPC_linear:
6634 case OMPC_aligned:
6635 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006636 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006637 case OMPC_default:
6638 case OMPC_proc_bind:
6639 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006640 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006641 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006642 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006643 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006644 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006645 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006646 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006647 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006648 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006649 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006650 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006651 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006652 case OMPC_unknown:
6653 llvm_unreachable("Clause is not allowed.");
6654 }
6655 return Res;
6656}
6657
Alexey Bataev236070f2014-06-20 11:19:47 +00006658OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6659 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006660 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006661 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6662}
6663
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006664OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6665 SourceLocation EndLoc) {
6666 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6667}
6668
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006669OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6670 SourceLocation EndLoc) {
6671 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6672}
6673
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006674OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6675 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006676 return new (Context) OMPReadClause(StartLoc, EndLoc);
6677}
6678
Alexey Bataevdea47612014-07-23 07:46:59 +00006679OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6680 SourceLocation EndLoc) {
6681 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6682}
6683
Alexey Bataev67a4f222014-07-23 10:25:33 +00006684OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6685 SourceLocation EndLoc) {
6686 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6687}
6688
Alexey Bataev459dec02014-07-24 06:46:57 +00006689OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6690 SourceLocation EndLoc) {
6691 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6692}
6693
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006694OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6695 SourceLocation EndLoc) {
6696 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6697}
6698
Alexey Bataev346265e2015-09-25 10:37:12 +00006699OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6700 SourceLocation EndLoc) {
6701 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6702}
6703
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006704OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6705 SourceLocation EndLoc) {
6706 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6707}
6708
Alexey Bataevb825de12015-12-07 10:51:44 +00006709OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6710 SourceLocation EndLoc) {
6711 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6712}
6713
Alexey Bataevc5e02582014-06-16 07:08:35 +00006714OMPClause *Sema::ActOnOpenMPVarListClause(
6715 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6716 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6717 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006718 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006719 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6720 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6721 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006722 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006723 switch (Kind) {
6724 case OMPC_private:
6725 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6726 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006727 case OMPC_firstprivate:
6728 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6729 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006730 case OMPC_lastprivate:
6731 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6732 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006733 case OMPC_shared:
6734 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6735 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006736 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006737 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6738 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006739 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006740 case OMPC_linear:
6741 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006742 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006743 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006744 case OMPC_aligned:
6745 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6746 ColonLoc, EndLoc);
6747 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006748 case OMPC_copyin:
6749 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6750 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006751 case OMPC_copyprivate:
6752 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6753 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006754 case OMPC_flush:
6755 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6756 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006757 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006758 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6759 StartLoc, LParenLoc, EndLoc);
6760 break;
6761 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006762 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6763 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6764 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006765 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006766 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006767 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006768 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006769 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006770 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006771 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006772 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006773 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006774 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006775 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006776 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006777 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006778 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006779 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006780 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006781 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006782 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006783 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006784 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006785 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006786 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006787 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006788 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006789 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006790 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006791 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006792 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006793 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006794 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006795 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006796 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006797 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006798 llvm_unreachable("Clause is not allowed.");
6799 }
6800 return Res;
6801}
6802
6803OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6804 SourceLocation StartLoc,
6805 SourceLocation LParenLoc,
6806 SourceLocation EndLoc) {
6807 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006808 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006809 for (auto &RefExpr : VarList) {
6810 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006811 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6812 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006813 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006814 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006815 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006816 continue;
6817 }
6818
Alexey Bataeved09d242014-05-28 05:53:51 +00006819 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006820 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006821 // A list item is a variable name.
6822 // OpenMP [2.9.3.3, Restrictions, p.1]
6823 // A variable that is part of another variable (as an array or
6824 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006825 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6826 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6827 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6828 (getCurrentThisType().isNull() || !ME ||
6829 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6830 !isa<FieldDecl>(ME->getMemberDecl()))) {
6831 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6832 << (getCurrentThisType().isNull() ? 0 : 1)
6833 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006834 continue;
6835 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006836 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6837 QualType Type = D->getType();
6838 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006839
6840 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6841 // A variable that appears in a private clause must not have an incomplete
6842 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006843 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006844 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006845 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006846
Alexey Bataev758e55e2013-09-06 18:03:48 +00006847 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6848 // in a Construct]
6849 // Variables with the predetermined data-sharing attributes may not be
6850 // listed in data-sharing attributes clauses, except for the cases
6851 // listed below. For these exceptions only, listing a predetermined
6852 // variable in a data-sharing attribute clause is allowed and overrides
6853 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006854 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006855 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006856 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6857 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006858 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006859 continue;
6860 }
6861
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006862 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006863 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006864 DSAStack->getCurrentDirective() == OMPD_task) {
6865 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6866 << getOpenMPClauseName(OMPC_private) << Type
6867 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6868 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006869 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006870 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006871 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006872 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006873 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006874 continue;
6875 }
6876
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006877 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6878 // A variable of class type (or array thereof) that appears in a private
6879 // clause requires an accessible, unambiguous default constructor for the
6880 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006881 // Generate helper private variable and initialize it with the default
6882 // value. The address of the original variable is replaced by the address of
6883 // the new private variable in CodeGen. This new variable is not added to
6884 // IdResolver, so the code in the OpenMP region uses original variable for
6885 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006886 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006887 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
6888 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006889 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006890 if (VDPrivate->isInvalidDecl())
6891 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006892 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006893 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006894
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006895 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
6896 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006897 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006898 }
6899
Alexey Bataeved09d242014-05-28 05:53:51 +00006900 if (Vars.empty())
6901 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006902
Alexey Bataev03b340a2014-10-21 03:16:40 +00006903 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6904 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006905}
6906
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006907namespace {
6908class DiagsUninitializedSeveretyRAII {
6909private:
6910 DiagnosticsEngine &Diags;
6911 SourceLocation SavedLoc;
6912 bool IsIgnored;
6913
6914public:
6915 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6916 bool IsIgnored)
6917 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6918 if (!IsIgnored) {
6919 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6920 /*Map*/ diag::Severity::Ignored, Loc);
6921 }
6922 }
6923 ~DiagsUninitializedSeveretyRAII() {
6924 if (!IsIgnored)
6925 Diags.popMappings(SavedLoc);
6926 }
6927};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006928}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006929
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006930OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6931 SourceLocation StartLoc,
6932 SourceLocation LParenLoc,
6933 SourceLocation EndLoc) {
6934 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006935 SmallVector<Expr *, 8> PrivateCopies;
6936 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006937 bool IsImplicitClause =
6938 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6939 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6940
Alexey Bataeved09d242014-05-28 05:53:51 +00006941 for (auto &RefExpr : VarList) {
6942 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6943 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006944 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006945 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006946 PrivateCopies.push_back(nullptr);
6947 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006948 continue;
6949 }
6950
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006951 SourceLocation ELoc =
6952 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006953 // OpenMP [2.1, C/C++]
6954 // A list item is a variable name.
6955 // OpenMP [2.9.3.3, Restrictions, p.1]
6956 // A variable that is part of another variable (as an array or
6957 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006958 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006959 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006960 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6961 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006962 continue;
6963 }
6964 Decl *D = DE->getDecl();
6965 VarDecl *VD = cast<VarDecl>(D);
6966
6967 QualType Type = VD->getType();
6968 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6969 // It will be analyzed later.
6970 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006971 PrivateCopies.push_back(nullptr);
6972 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006973 continue;
6974 }
6975
6976 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6977 // A variable that appears in a private clause must not have an incomplete
6978 // type or a reference type.
6979 if (RequireCompleteType(ELoc, Type,
6980 diag::err_omp_firstprivate_incomplete_type)) {
6981 continue;
6982 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006983 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006984
6985 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6986 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006987 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006988 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006989 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006990
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006991 // If an implicit firstprivate variable found it was checked already.
6992 if (!IsImplicitClause) {
6993 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006994 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006995 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6996 // A list item that specifies a given variable may not appear in more
6997 // than one clause on the same directive, except that a variable may be
6998 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006999 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007000 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007001 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007002 << getOpenMPClauseName(DVar.CKind)
7003 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007004 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007005 continue;
7006 }
7007
7008 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7009 // in a Construct]
7010 // Variables with the predetermined data-sharing attributes may not be
7011 // listed in data-sharing attributes clauses, except for the cases
7012 // listed below. For these exceptions only, listing a predetermined
7013 // variable in a data-sharing attribute clause is allowed and overrides
7014 // the variable's predetermined data-sharing attributes.
7015 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7016 // in a Construct, C/C++, p.2]
7017 // Variables with const-qualified type having no mutable member may be
7018 // listed in a firstprivate clause, even if they are static data members.
7019 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7020 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7021 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007022 << getOpenMPClauseName(DVar.CKind)
7023 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007024 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007025 continue;
7026 }
7027
Alexey Bataevf29276e2014-06-18 04:14:57 +00007028 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007029 // OpenMP [2.9.3.4, Restrictions, p.2]
7030 // A list item that is private within a parallel region must not appear
7031 // in a firstprivate clause on a worksharing construct if any of the
7032 // worksharing regions arising from the worksharing construct ever bind
7033 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007034 if (isOpenMPWorksharingDirective(CurrDir) &&
7035 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007036 DVar = DSAStack->getImplicitDSA(VD, true);
7037 if (DVar.CKind != OMPC_shared &&
7038 (isOpenMPParallelDirective(DVar.DKind) ||
7039 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007040 Diag(ELoc, diag::err_omp_required_access)
7041 << getOpenMPClauseName(OMPC_firstprivate)
7042 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007043 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007044 continue;
7045 }
7046 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007047 // OpenMP [2.9.3.4, Restrictions, p.3]
7048 // A list item that appears in a reduction clause of a parallel construct
7049 // must not appear in a firstprivate clause on a worksharing or task
7050 // construct if any of the worksharing or task regions arising from the
7051 // worksharing or task construct ever bind to any of the parallel regions
7052 // arising from the parallel construct.
7053 // OpenMP [2.9.3.4, Restrictions, p.4]
7054 // A list item that appears in a reduction clause in worksharing
7055 // construct must not appear in a firstprivate clause in a task construct
7056 // encountered during execution of any of the worksharing regions arising
7057 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007058 if (CurrDir == OMPD_task) {
7059 DVar =
7060 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7061 [](OpenMPDirectiveKind K) -> bool {
7062 return isOpenMPParallelDirective(K) ||
7063 isOpenMPWorksharingDirective(K);
7064 },
7065 false);
7066 if (DVar.CKind == OMPC_reduction &&
7067 (isOpenMPParallelDirective(DVar.DKind) ||
7068 isOpenMPWorksharingDirective(DVar.DKind))) {
7069 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7070 << getOpenMPDirectiveName(DVar.DKind);
7071 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7072 continue;
7073 }
7074 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007075
7076 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7077 // A list item that is private within a teams region must not appear in a
7078 // firstprivate clause on a distribute construct if any of the distribute
7079 // regions arising from the distribute construct ever bind to any of the
7080 // teams regions arising from the teams construct.
7081 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7082 // A list item that appears in a reduction clause of a teams construct
7083 // must not appear in a firstprivate clause on a distribute construct if
7084 // any of the distribute regions arising from the distribute construct
7085 // ever bind to any of the teams regions arising from the teams construct.
7086 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7087 // A list item may appear in a firstprivate or lastprivate clause but not
7088 // both.
7089 if (CurrDir == OMPD_distribute) {
7090 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7091 [](OpenMPDirectiveKind K) -> bool {
7092 return isOpenMPTeamsDirective(K);
7093 },
7094 false);
7095 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7096 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7097 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7098 continue;
7099 }
7100 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7101 [](OpenMPDirectiveKind K) -> bool {
7102 return isOpenMPTeamsDirective(K);
7103 },
7104 false);
7105 if (DVar.CKind == OMPC_reduction &&
7106 isOpenMPTeamsDirective(DVar.DKind)) {
7107 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7108 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7109 continue;
7110 }
7111 DVar = DSAStack->getTopDSA(VD, false);
7112 if (DVar.CKind == OMPC_lastprivate) {
7113 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7114 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7115 continue;
7116 }
7117 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007118 }
7119
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007120 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007121 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007122 DSAStack->getCurrentDirective() == OMPD_task) {
7123 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7124 << getOpenMPClauseName(OMPC_firstprivate) << Type
7125 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7126 bool IsDecl =
7127 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7128 Diag(VD->getLocation(),
7129 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7130 << VD;
7131 continue;
7132 }
7133
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007134 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007135 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7136 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007137 // Generate helper private variable and initialize it with the value of the
7138 // original variable. The address of the original variable is replaced by
7139 // the address of the new private variable in the CodeGen. This new variable
7140 // is not added to IdResolver, so the code in the OpenMP region uses
7141 // original variable for proper diagnostics and variable capturing.
7142 Expr *VDInitRefExpr = nullptr;
7143 // For arrays generate initializer for single element and replace it by the
7144 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007145 if (Type->isArrayType()) {
7146 auto VDInit =
7147 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7148 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007149 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007150 ElemType = ElemType.getUnqualifiedType();
7151 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7152 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007153 InitializedEntity Entity =
7154 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007155 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7156
7157 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7158 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7159 if (Result.isInvalid())
7160 VDPrivate->setInvalidDecl();
7161 else
7162 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007163 // Remove temp variable declaration.
7164 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007165 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007166 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007167 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007168 VDInitRefExpr =
7169 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007170 AddInitializerToDecl(VDPrivate,
7171 DefaultLvalueConversion(VDInitRefExpr).get(),
7172 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007173 }
7174 if (VDPrivate->isInvalidDecl()) {
7175 if (IsImplicitClause) {
7176 Diag(DE->getExprLoc(),
7177 diag::note_omp_task_predetermined_firstprivate_here);
7178 }
7179 continue;
7180 }
7181 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007182 auto VDPrivateRefExpr = buildDeclRefExpr(
7183 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007184 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7185 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007186 PrivateCopies.push_back(VDPrivateRefExpr);
7187 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007188 }
7189
Alexey Bataeved09d242014-05-28 05:53:51 +00007190 if (Vars.empty())
7191 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007192
7193 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007194 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007195}
7196
Alexander Musman1bb328c2014-06-04 13:06:39 +00007197OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7198 SourceLocation StartLoc,
7199 SourceLocation LParenLoc,
7200 SourceLocation EndLoc) {
7201 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007202 SmallVector<Expr *, 8> SrcExprs;
7203 SmallVector<Expr *, 8> DstExprs;
7204 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007205 for (auto &RefExpr : VarList) {
7206 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7207 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7208 // It will be analyzed later.
7209 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007210 SrcExprs.push_back(nullptr);
7211 DstExprs.push_back(nullptr);
7212 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007213 continue;
7214 }
7215
7216 SourceLocation ELoc = RefExpr->getExprLoc();
7217 // OpenMP [2.1, C/C++]
7218 // A list item is a variable name.
7219 // OpenMP [2.14.3.5, Restrictions, p.1]
7220 // A variable that is part of another variable (as an array or structure
7221 // element) cannot appear in a lastprivate clause.
7222 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7223 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007224 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7225 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007226 continue;
7227 }
7228 Decl *D = DE->getDecl();
7229 VarDecl *VD = cast<VarDecl>(D);
7230
7231 QualType Type = VD->getType();
7232 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7233 // It will be analyzed later.
7234 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007235 SrcExprs.push_back(nullptr);
7236 DstExprs.push_back(nullptr);
7237 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007238 continue;
7239 }
7240
7241 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7242 // A variable that appears in a lastprivate clause must not have an
7243 // incomplete type or a reference type.
7244 if (RequireCompleteType(ELoc, Type,
7245 diag::err_omp_lastprivate_incomplete_type)) {
7246 continue;
7247 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007248 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007249
7250 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7251 // in a Construct]
7252 // Variables with the predetermined data-sharing attributes may not be
7253 // listed in data-sharing attributes clauses, except for the cases
7254 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007255 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007256 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7257 DVar.CKind != OMPC_firstprivate &&
7258 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7259 Diag(ELoc, diag::err_omp_wrong_dsa)
7260 << getOpenMPClauseName(DVar.CKind)
7261 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007262 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007263 continue;
7264 }
7265
Alexey Bataevf29276e2014-06-18 04:14:57 +00007266 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7267 // OpenMP [2.14.3.5, Restrictions, p.2]
7268 // A list item that is private within a parallel region, or that appears in
7269 // the reduction clause of a parallel construct, must not appear in a
7270 // lastprivate clause on a worksharing construct if any of the corresponding
7271 // worksharing regions ever binds to any of the corresponding parallel
7272 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007273 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007274 if (isOpenMPWorksharingDirective(CurrDir) &&
7275 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007276 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007277 if (DVar.CKind != OMPC_shared) {
7278 Diag(ELoc, diag::err_omp_required_access)
7279 << getOpenMPClauseName(OMPC_lastprivate)
7280 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007281 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007282 continue;
7283 }
7284 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007285 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007286 // A variable of class type (or array thereof) that appears in a
7287 // lastprivate clause requires an accessible, unambiguous default
7288 // constructor for the class type, unless the list item is also specified
7289 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007290 // A variable of class type (or array thereof) that appears in a
7291 // lastprivate clause requires an accessible, unambiguous copy assignment
7292 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007293 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007294 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007295 Type.getUnqualifiedType(), ".lastprivate.src",
7296 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007297 auto *PseudoSrcExpr = buildDeclRefExpr(
7298 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007299 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007300 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7301 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007302 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007303 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007304 // For arrays generate assignment operation for single element and replace
7305 // it by the original array element in CodeGen.
7306 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7307 PseudoDstExpr, PseudoSrcExpr);
7308 if (AssignmentOp.isInvalid())
7309 continue;
7310 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7311 /*DiscardedValue=*/true);
7312 if (AssignmentOp.isInvalid())
7313 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007314
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007315 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7316 // A list item may appear in a firstprivate or lastprivate clause but not
7317 // both.
7318 if (CurrDir == OMPD_distribute) {
7319 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7320 if (DVar.CKind == OMPC_firstprivate) {
7321 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7322 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7323 continue;
7324 }
7325 }
7326
Alexey Bataev39f915b82015-05-08 10:41:21 +00007327 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007328 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007329 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007330 SrcExprs.push_back(PseudoSrcExpr);
7331 DstExprs.push_back(PseudoDstExpr);
7332 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007333 }
7334
7335 if (Vars.empty())
7336 return nullptr;
7337
7338 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007339 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007340}
7341
Alexey Bataev758e55e2013-09-06 18:03:48 +00007342OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7343 SourceLocation StartLoc,
7344 SourceLocation LParenLoc,
7345 SourceLocation EndLoc) {
7346 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007347 for (auto &RefExpr : VarList) {
7348 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7349 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007350 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007351 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007352 continue;
7353 }
7354
Alexey Bataeved09d242014-05-28 05:53:51 +00007355 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007356 // OpenMP [2.1, C/C++]
7357 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007358 // OpenMP [2.14.3.2, Restrictions, p.1]
7359 // A variable that is part of another variable (as an array or structure
7360 // element) cannot appear in a shared unless it is a static data member
7361 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007362 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007363 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007364 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7365 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007366 continue;
7367 }
7368 Decl *D = DE->getDecl();
7369 VarDecl *VD = cast<VarDecl>(D);
7370
7371 QualType Type = VD->getType();
7372 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7373 // It will be analyzed later.
7374 Vars.push_back(DE);
7375 continue;
7376 }
7377
7378 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7379 // in a Construct]
7380 // Variables with the predetermined data-sharing attributes may not be
7381 // listed in data-sharing attributes clauses, except for the cases
7382 // listed below. For these exceptions only, listing a predetermined
7383 // variable in a data-sharing attribute clause is allowed and overrides
7384 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007385 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007386 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7387 DVar.RefExpr) {
7388 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7389 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007390 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007391 continue;
7392 }
7393
7394 DSAStack->addDSA(VD, DE, OMPC_shared);
7395 Vars.push_back(DE);
7396 }
7397
Alexey Bataeved09d242014-05-28 05:53:51 +00007398 if (Vars.empty())
7399 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007400
7401 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7402}
7403
Alexey Bataevc5e02582014-06-16 07:08:35 +00007404namespace {
7405class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7406 DSAStackTy *Stack;
7407
7408public:
7409 bool VisitDeclRefExpr(DeclRefExpr *E) {
7410 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007411 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007412 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7413 return false;
7414 if (DVar.CKind != OMPC_unknown)
7415 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007416 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007417 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007418 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007419 return true;
7420 return false;
7421 }
7422 return false;
7423 }
7424 bool VisitStmt(Stmt *S) {
7425 for (auto Child : S->children()) {
7426 if (Child && Visit(Child))
7427 return true;
7428 }
7429 return false;
7430 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007431 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007432};
Alexey Bataev23b69422014-06-18 07:08:49 +00007433} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007434
7435OMPClause *Sema::ActOnOpenMPReductionClause(
7436 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7437 SourceLocation ColonLoc, SourceLocation EndLoc,
7438 CXXScopeSpec &ReductionIdScopeSpec,
7439 const DeclarationNameInfo &ReductionId) {
7440 // TODO: Allow scope specification search when 'declare reduction' is
7441 // supported.
7442 assert(ReductionIdScopeSpec.isEmpty() &&
7443 "No support for scoped reduction identifiers yet.");
7444
7445 auto DN = ReductionId.getName();
7446 auto OOK = DN.getCXXOverloadedOperator();
7447 BinaryOperatorKind BOK = BO_Comma;
7448
7449 // OpenMP [2.14.3.6, reduction clause]
7450 // C
7451 // reduction-identifier is either an identifier or one of the following
7452 // operators: +, -, *, &, |, ^, && and ||
7453 // C++
7454 // reduction-identifier is either an id-expression or one of the following
7455 // operators: +, -, *, &, |, ^, && and ||
7456 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7457 switch (OOK) {
7458 case OO_Plus:
7459 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007460 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007461 break;
7462 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007463 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007464 break;
7465 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007466 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007467 break;
7468 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007469 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007470 break;
7471 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007472 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007473 break;
7474 case OO_AmpAmp:
7475 BOK = BO_LAnd;
7476 break;
7477 case OO_PipePipe:
7478 BOK = BO_LOr;
7479 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007480 case OO_New:
7481 case OO_Delete:
7482 case OO_Array_New:
7483 case OO_Array_Delete:
7484 case OO_Slash:
7485 case OO_Percent:
7486 case OO_Tilde:
7487 case OO_Exclaim:
7488 case OO_Equal:
7489 case OO_Less:
7490 case OO_Greater:
7491 case OO_LessEqual:
7492 case OO_GreaterEqual:
7493 case OO_PlusEqual:
7494 case OO_MinusEqual:
7495 case OO_StarEqual:
7496 case OO_SlashEqual:
7497 case OO_PercentEqual:
7498 case OO_CaretEqual:
7499 case OO_AmpEqual:
7500 case OO_PipeEqual:
7501 case OO_LessLess:
7502 case OO_GreaterGreater:
7503 case OO_LessLessEqual:
7504 case OO_GreaterGreaterEqual:
7505 case OO_EqualEqual:
7506 case OO_ExclaimEqual:
7507 case OO_PlusPlus:
7508 case OO_MinusMinus:
7509 case OO_Comma:
7510 case OO_ArrowStar:
7511 case OO_Arrow:
7512 case OO_Call:
7513 case OO_Subscript:
7514 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007515 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007516 case NUM_OVERLOADED_OPERATORS:
7517 llvm_unreachable("Unexpected reduction identifier");
7518 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007519 if (auto II = DN.getAsIdentifierInfo()) {
7520 if (II->isStr("max"))
7521 BOK = BO_GT;
7522 else if (II->isStr("min"))
7523 BOK = BO_LT;
7524 }
7525 break;
7526 }
7527 SourceRange ReductionIdRange;
7528 if (ReductionIdScopeSpec.isValid()) {
7529 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7530 }
7531 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7532 if (BOK == BO_Comma) {
7533 // Not allowed reduction identifier is found.
7534 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7535 << ReductionIdRange;
7536 return nullptr;
7537 }
7538
7539 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007540 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007541 SmallVector<Expr *, 8> LHSs;
7542 SmallVector<Expr *, 8> RHSs;
7543 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007544 for (auto RefExpr : VarList) {
7545 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7546 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7547 // It will be analyzed later.
7548 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007549 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007550 LHSs.push_back(nullptr);
7551 RHSs.push_back(nullptr);
7552 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007553 continue;
7554 }
7555
7556 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7557 RefExpr->isInstantiationDependent() ||
7558 RefExpr->containsUnexpandedParameterPack()) {
7559 // It will be analyzed later.
7560 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007561 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007562 LHSs.push_back(nullptr);
7563 RHSs.push_back(nullptr);
7564 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007565 continue;
7566 }
7567
7568 auto ELoc = RefExpr->getExprLoc();
7569 auto ERange = RefExpr->getSourceRange();
7570 // OpenMP [2.1, C/C++]
7571 // A list item is a variable or array section, subject to the restrictions
7572 // specified in Section 2.4 on page 42 and in each of the sections
7573 // describing clauses and directives for which a list appears.
7574 // OpenMP [2.14.3.3, Restrictions, p.1]
7575 // A variable that is part of another variable (as an array or
7576 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007577 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7578 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7579 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7580 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007581 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7582 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007583 continue;
7584 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007585 QualType Type;
7586 VarDecl *VD = nullptr;
7587 if (DE) {
7588 auto D = DE->getDecl();
7589 VD = cast<VarDecl>(D);
Alexey Bataev1189bd02016-01-26 12:20:39 +00007590 Type = Context.getBaseElementType(VD->getType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007591 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007592 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007593 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7594 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7595 Base = TempASE->getBase()->IgnoreParenImpCasts();
7596 DE = dyn_cast<DeclRefExpr>(Base);
7597 if (DE)
7598 VD = dyn_cast<VarDecl>(DE->getDecl());
7599 if (!VD) {
7600 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7601 << 0 << Base->getSourceRange();
7602 continue;
7603 }
7604 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007605 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7606 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7607 Type = ATy->getElementType();
7608 else
7609 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007610 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7611 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7612 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7613 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7614 Base = TempASE->getBase()->IgnoreParenImpCasts();
7615 DE = dyn_cast<DeclRefExpr>(Base);
7616 if (DE)
7617 VD = dyn_cast<VarDecl>(DE->getDecl());
7618 if (!VD) {
7619 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7620 << 1 << Base->getSourceRange();
7621 continue;
7622 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007623 }
7624
Alexey Bataevc5e02582014-06-16 07:08:35 +00007625 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7626 // A variable that appears in a private clause must not have an incomplete
7627 // type or a reference type.
7628 if (RequireCompleteType(ELoc, Type,
7629 diag::err_omp_reduction_incomplete_type))
7630 continue;
7631 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007632 // A list item that appears in a reduction clause must not be
7633 // const-qualified.
7634 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007635 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007636 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007637 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007638 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7639 VarDecl::DeclarationOnly;
7640 Diag(VD->getLocation(),
7641 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7642 << VD;
7643 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007644 continue;
7645 }
7646 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7647 // If a list-item is a reference type then it must bind to the same object
7648 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007649 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007650 VarDecl *VDDef = VD->getDefinition();
7651 if (Type->isReferenceType() && VDDef) {
7652 DSARefChecker Check(DSAStack);
7653 if (Check.Visit(VDDef->getInit())) {
7654 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7655 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7656 continue;
7657 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007658 }
7659 }
7660 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7661 // The type of a list item that appears in a reduction clause must be valid
7662 // for the reduction-identifier. For a max or min reduction in C, the type
7663 // of the list item must be an allowed arithmetic data type: char, int,
7664 // float, double, or _Bool, possibly modified with long, short, signed, or
7665 // unsigned. For a max or min reduction in C++, the type of the list item
7666 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7667 // double, or bool, possibly modified with long, short, signed, or unsigned.
7668 if ((BOK == BO_GT || BOK == BO_LT) &&
7669 !(Type->isScalarType() ||
7670 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7671 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7672 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007673 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007674 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7675 VarDecl::DeclarationOnly;
7676 Diag(VD->getLocation(),
7677 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7678 << VD;
7679 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007680 continue;
7681 }
7682 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7683 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7684 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007685 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007686 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7687 VarDecl::DeclarationOnly;
7688 Diag(VD->getLocation(),
7689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7690 << VD;
7691 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007692 continue;
7693 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007694 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7695 // in a Construct]
7696 // Variables with the predetermined data-sharing attributes may not be
7697 // listed in data-sharing attributes clauses, except for the cases
7698 // listed below. For these exceptions only, listing a predetermined
7699 // variable in a data-sharing attribute clause is allowed and overrides
7700 // the variable's predetermined data-sharing attributes.
7701 // OpenMP [2.14.3.6, Restrictions, p.3]
7702 // Any number of reduction clauses can be specified on the directive,
7703 // but a list item can appear only once in the reduction clauses for that
7704 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007705 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007706 DVar = DSAStack->getTopDSA(VD, false);
7707 if (DVar.CKind == OMPC_reduction) {
7708 Diag(ELoc, diag::err_omp_once_referenced)
7709 << getOpenMPClauseName(OMPC_reduction);
7710 if (DVar.RefExpr) {
7711 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007712 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007713 } else if (DVar.CKind != OMPC_unknown) {
7714 Diag(ELoc, diag::err_omp_wrong_dsa)
7715 << getOpenMPClauseName(DVar.CKind)
7716 << getOpenMPClauseName(OMPC_reduction);
7717 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7718 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007719 }
7720
7721 // OpenMP [2.14.3.6, Restrictions, p.1]
7722 // A list item that appears in a reduction clause of a worksharing
7723 // construct must be shared in the parallel regions to which any of the
7724 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007725 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7726 if (isOpenMPWorksharingDirective(CurrDir) &&
7727 !isOpenMPParallelDirective(CurrDir)) {
7728 DVar = DSAStack->getImplicitDSA(VD, true);
7729 if (DVar.CKind != OMPC_shared) {
7730 Diag(ELoc, diag::err_omp_required_access)
7731 << getOpenMPClauseName(OMPC_reduction)
7732 << getOpenMPClauseName(OMPC_shared);
7733 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7734 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007735 }
7736 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007737
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007738 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007739 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7740 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7741 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7742 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7743 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007744 if (OASE ||
7745 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7746 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007747 // Create pseudo array type for private copy. The size for this array will
7748 // be generated during codegen.
7749 // For array subscripts or single variables Private Ty is the same as Type
7750 // (type of the variable or single array element).
7751 PrivateTy = Context.getVariableArrayType(
7752 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7753 Context.getSizeType(), VK_RValue),
7754 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007755 } else if (DE &&
7756 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7757 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007758 // Private copy.
7759 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7760 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007761 // Add initializer for private variable.
7762 Expr *Init = nullptr;
7763 switch (BOK) {
7764 case BO_Add:
7765 case BO_Xor:
7766 case BO_Or:
7767 case BO_LOr:
7768 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7769 if (Type->isScalarType() || Type->isAnyComplexType()) {
7770 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007771 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007772 break;
7773 case BO_Mul:
7774 case BO_LAnd:
7775 if (Type->isScalarType() || Type->isAnyComplexType()) {
7776 // '*' and '&&' reduction ops - initializer is '1'.
7777 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7778 }
7779 break;
7780 case BO_And: {
7781 // '&' reduction op - initializer is '~0'.
7782 QualType OrigType = Type;
7783 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7784 Type = ComplexTy->getElementType();
7785 }
7786 if (Type->isRealFloatingType()) {
7787 llvm::APFloat InitValue =
7788 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7789 /*isIEEE=*/true);
7790 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7791 Type, ELoc);
7792 } else if (Type->isScalarType()) {
7793 auto Size = Context.getTypeSize(Type);
7794 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7795 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7796 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7797 }
7798 if (Init && OrigType->isAnyComplexType()) {
7799 // Init = 0xFFFF + 0xFFFFi;
7800 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7801 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7802 }
7803 Type = OrigType;
7804 break;
7805 }
7806 case BO_LT:
7807 case BO_GT: {
7808 // 'min' reduction op - initializer is 'Largest representable number in
7809 // the reduction list item type'.
7810 // 'max' reduction op - initializer is 'Least representable number in
7811 // the reduction list item type'.
7812 if (Type->isIntegerType() || Type->isPointerType()) {
7813 bool IsSigned = Type->hasSignedIntegerRepresentation();
7814 auto Size = Context.getTypeSize(Type);
7815 QualType IntTy =
7816 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7817 llvm::APInt InitValue =
7818 (BOK != BO_LT)
7819 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7820 : llvm::APInt::getMinValue(Size)
7821 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7822 : llvm::APInt::getMaxValue(Size);
7823 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7824 if (Type->isPointerType()) {
7825 // Cast to pointer type.
7826 auto CastExpr = BuildCStyleCastExpr(
7827 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7828 SourceLocation(), Init);
7829 if (CastExpr.isInvalid())
7830 continue;
7831 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007832 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007833 } else if (Type->isRealFloatingType()) {
7834 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7835 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7836 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7837 Type, ELoc);
7838 }
7839 break;
7840 }
7841 case BO_PtrMemD:
7842 case BO_PtrMemI:
7843 case BO_MulAssign:
7844 case BO_Div:
7845 case BO_Rem:
7846 case BO_Sub:
7847 case BO_Shl:
7848 case BO_Shr:
7849 case BO_LE:
7850 case BO_GE:
7851 case BO_EQ:
7852 case BO_NE:
7853 case BO_AndAssign:
7854 case BO_XorAssign:
7855 case BO_OrAssign:
7856 case BO_Assign:
7857 case BO_AddAssign:
7858 case BO_SubAssign:
7859 case BO_DivAssign:
7860 case BO_RemAssign:
7861 case BO_ShlAssign:
7862 case BO_ShrAssign:
7863 case BO_Comma:
7864 llvm_unreachable("Unexpected reduction operation");
7865 }
7866 if (Init) {
7867 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7868 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007869 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007870 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007871 if (!RHSVD->hasInit()) {
7872 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7873 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007874 if (VD) {
7875 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7876 VarDecl::DeclarationOnly;
7877 Diag(VD->getLocation(),
7878 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7879 << VD;
7880 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007881 continue;
7882 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007883 // Store initializer for single element in private copy. Will be used during
7884 // codegen.
7885 PrivateVD->setInit(RHSVD->getInit());
7886 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007887 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7888 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007889 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007890 ExprResult ReductionOp =
7891 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7892 LHSDRE, RHSDRE);
7893 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007894 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007895 ReductionOp =
7896 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7897 BO_Assign, LHSDRE, ReductionOp.get());
7898 } else {
7899 auto *ConditionalOp = new (Context) ConditionalOperator(
7900 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7901 RHSDRE, Type, VK_LValue, OK_Ordinary);
7902 ReductionOp =
7903 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7904 BO_Assign, LHSDRE, ConditionalOp);
7905 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007906 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007907 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007908 if (ReductionOp.isInvalid())
7909 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007910
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007911 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007912 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007913 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007914 LHSs.push_back(LHSDRE);
7915 RHSs.push_back(RHSDRE);
7916 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007917 }
7918
7919 if (Vars.empty())
7920 return nullptr;
7921
7922 return OMPReductionClause::Create(
7923 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007924 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7925 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007926}
7927
Alexey Bataev182227b2015-08-20 10:54:39 +00007928OMPClause *Sema::ActOnOpenMPLinearClause(
7929 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7930 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7931 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007932 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007933 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007934 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007935 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7936 LinKind == OMPC_LINEAR_unknown) {
7937 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7938 LinKind = OMPC_LINEAR_val;
7939 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007940 for (auto &RefExpr : VarList) {
7941 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7942 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007943 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007944 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007945 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007946 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007947 continue;
7948 }
7949
7950 // OpenMP [2.14.3.7, linear clause]
7951 // A list item that appears in a linear clause is subject to the private
7952 // clause semantics described in Section 2.14.3.3 on page 159 except as
7953 // noted. In addition, the value of the new list item on each iteration
7954 // of the associated loop(s) corresponds to the value of the original
7955 // list item before entering the construct plus the logical number of
7956 // the iteration times linear-step.
7957
Alexey Bataeved09d242014-05-28 05:53:51 +00007958 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007959 // OpenMP [2.1, C/C++]
7960 // A list item is a variable name.
7961 // OpenMP [2.14.3.3, Restrictions, p.1]
7962 // A variable that is part of another variable (as an array or
7963 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007964 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007965 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007966 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7967 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007968 continue;
7969 }
7970
7971 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7972
7973 // OpenMP [2.14.3.7, linear clause]
7974 // A list-item cannot appear in more than one linear clause.
7975 // A list-item that appears in a linear clause cannot appear in any
7976 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007977 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007978 if (DVar.RefExpr) {
7979 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7980 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007981 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007982 continue;
7983 }
7984
7985 QualType QType = VD->getType();
7986 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7987 // It will be analyzed later.
7988 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007989 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007990 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007991 continue;
7992 }
7993
7994 // A variable must not have an incomplete type or a reference type.
7995 if (RequireCompleteType(ELoc, QType,
7996 diag::err_omp_linear_incomplete_type)) {
7997 continue;
7998 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007999 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8000 !QType->isReferenceType()) {
8001 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8002 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8003 continue;
8004 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008005 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008006
8007 // A list item must not be const-qualified.
8008 if (QType.isConstant(Context)) {
8009 Diag(ELoc, diag::err_omp_const_variable)
8010 << getOpenMPClauseName(OMPC_linear);
8011 bool IsDecl =
8012 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8013 Diag(VD->getLocation(),
8014 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8015 << VD;
8016 continue;
8017 }
8018
8019 // A list item must be of integral or pointer type.
8020 QType = QType.getUnqualifiedType().getCanonicalType();
8021 const Type *Ty = QType.getTypePtrOrNull();
8022 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8023 !Ty->isPointerType())) {
8024 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8025 bool IsDecl =
8026 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8027 Diag(VD->getLocation(),
8028 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8029 << VD;
8030 continue;
8031 }
8032
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008033 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008034 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8035 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008036 auto *PrivateRef = buildDeclRefExpr(
8037 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008038 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008039 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008040 Expr *InitExpr;
8041 if (LinKind == OMPC_LINEAR_uval)
8042 InitExpr = VD->getInit();
8043 else
8044 InitExpr = DE;
8045 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008046 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008047 auto InitRef = buildDeclRefExpr(
8048 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008049 DSAStack->addDSA(VD, DE, OMPC_linear);
8050 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008051 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008052 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008053 }
8054
8055 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008056 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008057
8058 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008059 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008060 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8061 !Step->isInstantiationDependent() &&
8062 !Step->containsUnexpandedParameterPack()) {
8063 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008064 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008065 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008066 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008067 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008068
Alexander Musman3276a272015-03-21 10:12:56 +00008069 // Build var to save the step value.
8070 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008071 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008072 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008073 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008074 ExprResult CalcStep =
8075 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008076 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008077
Alexander Musman8dba6642014-04-22 13:09:42 +00008078 // Warn about zero linear step (it would be probably better specified as
8079 // making corresponding variables 'const').
8080 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008081 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8082 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008083 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8084 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008085 if (!IsConstant && CalcStep.isUsable()) {
8086 // Calculate the step beforehand instead of doing this on each iteration.
8087 // (This is not used if the number of iterations may be kfold-ed).
8088 CalcStepExpr = CalcStep.get();
8089 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008090 }
8091
Alexey Bataev182227b2015-08-20 10:54:39 +00008092 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8093 ColonLoc, EndLoc, Vars, Privates, Inits,
8094 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008095}
8096
8097static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8098 Expr *NumIterations, Sema &SemaRef,
8099 Scope *S) {
8100 // Walk the vars and build update/final expressions for the CodeGen.
8101 SmallVector<Expr *, 8> Updates;
8102 SmallVector<Expr *, 8> Finals;
8103 Expr *Step = Clause.getStep();
8104 Expr *CalcStep = Clause.getCalcStep();
8105 // OpenMP [2.14.3.7, linear clause]
8106 // If linear-step is not specified it is assumed to be 1.
8107 if (Step == nullptr)
8108 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8109 else if (CalcStep)
8110 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8111 bool HasErrors = false;
8112 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008113 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008114 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008115 for (auto &RefExpr : Clause.varlists()) {
8116 Expr *InitExpr = *CurInit;
8117
8118 // Build privatized reference to the current linear var.
8119 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008120 Expr *CapturedRef;
8121 if (LinKind == OMPC_LINEAR_uval)
8122 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8123 else
8124 CapturedRef =
8125 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8126 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8127 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008128
8129 // Build update: Var = InitExpr + IV * Step
8130 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008131 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008132 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008133 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8134 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008135
8136 // Build final: Var = InitExpr + NumIterations * Step
8137 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008138 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008139 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008140 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8141 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008142 if (!Update.isUsable() || !Final.isUsable()) {
8143 Updates.push_back(nullptr);
8144 Finals.push_back(nullptr);
8145 HasErrors = true;
8146 } else {
8147 Updates.push_back(Update.get());
8148 Finals.push_back(Final.get());
8149 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008150 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008151 }
8152 Clause.setUpdates(Updates);
8153 Clause.setFinals(Finals);
8154 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008155}
8156
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008157OMPClause *Sema::ActOnOpenMPAlignedClause(
8158 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8159 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8160
8161 SmallVector<Expr *, 8> Vars;
8162 for (auto &RefExpr : VarList) {
8163 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8164 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8165 // It will be analyzed later.
8166 Vars.push_back(RefExpr);
8167 continue;
8168 }
8169
8170 SourceLocation ELoc = RefExpr->getExprLoc();
8171 // OpenMP [2.1, C/C++]
8172 // A list item is a variable name.
8173 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8174 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008175 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8176 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008177 continue;
8178 }
8179
8180 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8181
8182 // OpenMP [2.8.1, simd construct, Restrictions]
8183 // The type of list items appearing in the aligned clause must be
8184 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008185 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008186 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008187 const Type *Ty = QType.getTypePtrOrNull();
8188 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8189 !Ty->isPointerType())) {
8190 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8191 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8192 bool IsDecl =
8193 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8194 Diag(VD->getLocation(),
8195 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8196 << VD;
8197 continue;
8198 }
8199
8200 // OpenMP [2.8.1, simd construct, Restrictions]
8201 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008202 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008203 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8204 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8205 << getOpenMPClauseName(OMPC_aligned);
8206 continue;
8207 }
8208
8209 Vars.push_back(DE);
8210 }
8211
8212 // OpenMP [2.8.1, simd construct, Description]
8213 // The parameter of the aligned clause, alignment, must be a constant
8214 // positive integer expression.
8215 // If no optional parameter is specified, implementation-defined default
8216 // alignments for SIMD instructions on the target platforms are assumed.
8217 if (Alignment != nullptr) {
8218 ExprResult AlignResult =
8219 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8220 if (AlignResult.isInvalid())
8221 return nullptr;
8222 Alignment = AlignResult.get();
8223 }
8224 if (Vars.empty())
8225 return nullptr;
8226
8227 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8228 EndLoc, Vars, Alignment);
8229}
8230
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008231OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8232 SourceLocation StartLoc,
8233 SourceLocation LParenLoc,
8234 SourceLocation EndLoc) {
8235 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008236 SmallVector<Expr *, 8> SrcExprs;
8237 SmallVector<Expr *, 8> DstExprs;
8238 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008239 for (auto &RefExpr : VarList) {
8240 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8241 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008242 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008243 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008244 SrcExprs.push_back(nullptr);
8245 DstExprs.push_back(nullptr);
8246 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008247 continue;
8248 }
8249
Alexey Bataeved09d242014-05-28 05:53:51 +00008250 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008251 // OpenMP [2.1, C/C++]
8252 // A list item is a variable name.
8253 // OpenMP [2.14.4.1, Restrictions, p.1]
8254 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008255 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008256 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008257 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8258 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008259 continue;
8260 }
8261
8262 Decl *D = DE->getDecl();
8263 VarDecl *VD = cast<VarDecl>(D);
8264
8265 QualType Type = VD->getType();
8266 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8267 // It will be analyzed later.
8268 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008269 SrcExprs.push_back(nullptr);
8270 DstExprs.push_back(nullptr);
8271 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008272 continue;
8273 }
8274
8275 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8276 // A list item that appears in a copyin clause must be threadprivate.
8277 if (!DSAStack->isThreadPrivate(VD)) {
8278 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008279 << getOpenMPClauseName(OMPC_copyin)
8280 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008281 continue;
8282 }
8283
8284 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8285 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008286 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008287 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008288 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008289 auto *SrcVD =
8290 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8291 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008292 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008293 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8294 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008295 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8296 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008297 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008298 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008299 // For arrays generate assignment operation for single element and replace
8300 // it by the original array element in CodeGen.
8301 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8302 PseudoDstExpr, PseudoSrcExpr);
8303 if (AssignmentOp.isInvalid())
8304 continue;
8305 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8306 /*DiscardedValue=*/true);
8307 if (AssignmentOp.isInvalid())
8308 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008309
8310 DSAStack->addDSA(VD, DE, OMPC_copyin);
8311 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008312 SrcExprs.push_back(PseudoSrcExpr);
8313 DstExprs.push_back(PseudoDstExpr);
8314 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008315 }
8316
Alexey Bataeved09d242014-05-28 05:53:51 +00008317 if (Vars.empty())
8318 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008319
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008320 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8321 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008322}
8323
Alexey Bataevbae9a792014-06-27 10:37:06 +00008324OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8325 SourceLocation StartLoc,
8326 SourceLocation LParenLoc,
8327 SourceLocation EndLoc) {
8328 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008329 SmallVector<Expr *, 8> SrcExprs;
8330 SmallVector<Expr *, 8> DstExprs;
8331 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008332 for (auto &RefExpr : VarList) {
8333 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8334 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8335 // It will be analyzed later.
8336 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008337 SrcExprs.push_back(nullptr);
8338 DstExprs.push_back(nullptr);
8339 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008340 continue;
8341 }
8342
8343 SourceLocation ELoc = RefExpr->getExprLoc();
8344 // OpenMP [2.1, C/C++]
8345 // A list item is a variable name.
8346 // OpenMP [2.14.4.1, Restrictions, p.1]
8347 // A list item that appears in a copyin clause must be threadprivate.
8348 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8349 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008350 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8351 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008352 continue;
8353 }
8354
8355 Decl *D = DE->getDecl();
8356 VarDecl *VD = cast<VarDecl>(D);
8357
8358 QualType Type = VD->getType();
8359 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8360 // It will be analyzed later.
8361 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008362 SrcExprs.push_back(nullptr);
8363 DstExprs.push_back(nullptr);
8364 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008365 continue;
8366 }
8367
8368 // OpenMP [2.14.4.2, Restrictions, p.2]
8369 // A list item that appears in a copyprivate clause may not appear in a
8370 // private or firstprivate clause on the single construct.
8371 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008372 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008373 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8374 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008375 Diag(ELoc, diag::err_omp_wrong_dsa)
8376 << getOpenMPClauseName(DVar.CKind)
8377 << getOpenMPClauseName(OMPC_copyprivate);
8378 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8379 continue;
8380 }
8381
8382 // OpenMP [2.11.4.2, Restrictions, p.1]
8383 // All list items that appear in a copyprivate clause must be either
8384 // threadprivate or private in the enclosing context.
8385 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008386 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008387 if (DVar.CKind == OMPC_shared) {
8388 Diag(ELoc, diag::err_omp_required_access)
8389 << getOpenMPClauseName(OMPC_copyprivate)
8390 << "threadprivate or private in the enclosing context";
8391 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8392 continue;
8393 }
8394 }
8395 }
8396
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008397 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008398 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008399 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008400 << getOpenMPClauseName(OMPC_copyprivate) << Type
8401 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008402 bool IsDecl =
8403 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8404 Diag(VD->getLocation(),
8405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8406 << VD;
8407 continue;
8408 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008409
Alexey Bataevbae9a792014-06-27 10:37:06 +00008410 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8411 // A variable of class type (or array thereof) that appears in a
8412 // copyin clause requires an accessible, unambiguous copy assignment
8413 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008414 Type = Context.getBaseElementType(Type.getNonReferenceType())
8415 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008416 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008417 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8418 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008419 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008420 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008421 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008422 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8423 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008424 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008425 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008426 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8427 PseudoDstExpr, PseudoSrcExpr);
8428 if (AssignmentOp.isInvalid())
8429 continue;
8430 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8431 /*DiscardedValue=*/true);
8432 if (AssignmentOp.isInvalid())
8433 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008434
8435 // No need to mark vars as copyprivate, they are already threadprivate or
8436 // implicitly private.
8437 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008438 SrcExprs.push_back(PseudoSrcExpr);
8439 DstExprs.push_back(PseudoDstExpr);
8440 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008441 }
8442
8443 if (Vars.empty())
8444 return nullptr;
8445
Alexey Bataeva63048e2015-03-23 06:18:07 +00008446 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8447 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008448}
8449
Alexey Bataev6125da92014-07-21 11:26:11 +00008450OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8451 SourceLocation StartLoc,
8452 SourceLocation LParenLoc,
8453 SourceLocation EndLoc) {
8454 if (VarList.empty())
8455 return nullptr;
8456
8457 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8458}
Alexey Bataevdea47612014-07-23 07:46:59 +00008459
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008460OMPClause *
8461Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8462 SourceLocation DepLoc, SourceLocation ColonLoc,
8463 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8464 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008465 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008466 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008467 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008468 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008469 return nullptr;
8470 }
8471 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008472 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8473 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008474 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008475 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008476 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8477 /*Last=*/OMPC_DEPEND_unknown, Except)
8478 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008479 return nullptr;
8480 }
8481 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008482 llvm::APSInt DepCounter(/*BitWidth=*/32);
8483 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8484 if (DepKind == OMPC_DEPEND_sink) {
8485 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8486 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8487 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008488 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008489 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008490 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8491 DSAStack->getParentOrderedRegionParam()) {
8492 for (auto &RefExpr : VarList) {
8493 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8494 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8495 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8496 // It will be analyzed later.
8497 Vars.push_back(RefExpr);
8498 continue;
8499 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008500
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008501 SourceLocation ELoc = RefExpr->getExprLoc();
8502 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8503 if (DepKind == OMPC_DEPEND_sink) {
8504 if (DepCounter >= TotalDepCount) {
8505 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8506 continue;
8507 }
8508 ++DepCounter;
8509 // OpenMP [2.13.9, Summary]
8510 // depend(dependence-type : vec), where dependence-type is:
8511 // 'sink' and where vec is the iteration vector, which has the form:
8512 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8513 // where n is the value specified by the ordered clause in the loop
8514 // directive, xi denotes the loop iteration variable of the i-th nested
8515 // loop associated with the loop directive, and di is a constant
8516 // non-negative integer.
8517 SimpleExpr = SimpleExpr->IgnoreImplicit();
8518 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8519 if (!DE) {
8520 OverloadedOperatorKind OOK = OO_None;
8521 SourceLocation OOLoc;
8522 Expr *LHS, *RHS;
8523 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8524 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8525 OOLoc = BO->getOperatorLoc();
8526 LHS = BO->getLHS()->IgnoreParenImpCasts();
8527 RHS = BO->getRHS()->IgnoreParenImpCasts();
8528 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8529 OOK = OCE->getOperator();
8530 OOLoc = OCE->getOperatorLoc();
8531 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8532 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8533 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8534 OOK = MCE->getMethodDecl()
8535 ->getNameInfo()
8536 .getName()
8537 .getCXXOverloadedOperator();
8538 OOLoc = MCE->getCallee()->getExprLoc();
8539 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8540 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8541 } else {
8542 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8543 continue;
8544 }
8545 DE = dyn_cast<DeclRefExpr>(LHS);
8546 if (!DE) {
8547 Diag(LHS->getExprLoc(),
8548 diag::err_omp_depend_sink_expected_loop_iteration)
8549 << DSAStack->getParentLoopControlVariable(
8550 DepCounter.getZExtValue());
8551 continue;
8552 }
8553 if (OOK != OO_Plus && OOK != OO_Minus) {
8554 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8555 continue;
8556 }
8557 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8558 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8559 if (Res.isInvalid())
8560 continue;
8561 }
8562 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8563 if (!CurContext->isDependentContext() &&
8564 DSAStack->getParentOrderedRegionParam() &&
8565 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8566 Diag(DE->getExprLoc(),
8567 diag::err_omp_depend_sink_expected_loop_iteration)
8568 << DSAStack->getParentLoopControlVariable(
8569 DepCounter.getZExtValue());
8570 continue;
8571 }
8572 } else {
8573 // OpenMP [2.11.1.1, Restrictions, p.3]
8574 // A variable that is part of another variable (such as a field of a
8575 // structure) but is not an array element or an array section cannot
8576 // appear in a depend clause.
8577 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8578 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8579 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8580 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8581 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8582 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8583 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008584 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8585 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008586 continue;
8587 }
8588 }
8589
8590 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8591 }
8592
8593 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8594 TotalDepCount > VarList.size() &&
8595 DSAStack->getParentOrderedRegionParam()) {
8596 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8597 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8598 }
8599 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8600 Vars.empty())
8601 return nullptr;
8602 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008603
8604 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8605 DepLoc, ColonLoc, Vars);
8606}
Michael Wonge710d542015-08-07 16:16:36 +00008607
8608OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8609 SourceLocation LParenLoc,
8610 SourceLocation EndLoc) {
8611 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008612
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008613 // OpenMP [2.9.1, Restrictions]
8614 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008615 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8616 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008617 return nullptr;
8618
Michael Wonge710d542015-08-07 16:16:36 +00008619 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8620}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008621
8622static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8623 DSAStackTy *Stack, CXXRecordDecl *RD) {
8624 if (!RD || RD->isInvalidDecl())
8625 return true;
8626
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008627 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8628 if (auto *CTD = CTSD->getSpecializedTemplate())
8629 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008630 auto QTy = SemaRef.Context.getRecordType(RD);
8631 if (RD->isDynamicClass()) {
8632 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8633 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8634 return false;
8635 }
8636 auto *DC = RD;
8637 bool IsCorrect = true;
8638 for (auto *I : DC->decls()) {
8639 if (I) {
8640 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8641 if (MD->isStatic()) {
8642 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8643 SemaRef.Diag(MD->getLocation(),
8644 diag::note_omp_static_member_in_target);
8645 IsCorrect = false;
8646 }
8647 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8648 if (VD->isStaticDataMember()) {
8649 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8650 SemaRef.Diag(VD->getLocation(),
8651 diag::note_omp_static_member_in_target);
8652 IsCorrect = false;
8653 }
8654 }
8655 }
8656 }
8657
8658 for (auto &I : RD->bases()) {
8659 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8660 I.getType()->getAsCXXRecordDecl()))
8661 IsCorrect = false;
8662 }
8663 return IsCorrect;
8664}
8665
8666static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8667 DSAStackTy *Stack, QualType QTy) {
8668 NamedDecl *ND;
8669 if (QTy->isIncompleteType(&ND)) {
8670 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8671 return false;
8672 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8673 if (!RD->isInvalidDecl() &&
8674 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8675 return false;
8676 }
8677 return true;
8678}
8679
Samuel Antao5de996e2016-01-22 20:21:36 +00008680// Return the expression of the base of the map clause or null if it cannot
8681// be determined and do all the necessary checks to see if the expression is
8682// valid as a standalone map clause expression.
8683static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8684 SourceLocation ELoc = E->getExprLoc();
8685 SourceRange ERange = E->getSourceRange();
8686
8687 // The base of elements of list in a map clause have to be either:
8688 // - a reference to variable or field.
8689 // - a member expression.
8690 // - an array expression.
8691 //
8692 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8693 // reference to 'r'.
8694 //
8695 // If we have:
8696 //
8697 // struct SS {
8698 // Bla S;
8699 // foo() {
8700 // #pragma omp target map (S.Arr[:12]);
8701 // }
8702 // }
8703 //
8704 // We want to retrieve the member expression 'this->S';
8705
8706 Expr *RelevantExpr = nullptr;
8707
8708 // Flags to help capture some memory
8709
8710 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8711 // If a list item is an array section, it must specify contiguous storage.
8712 //
8713 // For this restriction it is sufficient that we make sure only references
8714 // to variables or fields and array expressions, and that no array sections
8715 // exist except in the rightmost expression. E.g. these would be invalid:
8716 //
8717 // r.ArrS[3:5].Arr[6:7]
8718 //
8719 // r.ArrS[3:5].x
8720 //
8721 // but these would be valid:
8722 // r.ArrS[3].Arr[6:7]
8723 //
8724 // r.ArrS[3].x
8725
8726 bool IsRightMostExpression = true;
8727
8728 while (!RelevantExpr) {
8729 auto AllowArraySection = IsRightMostExpression;
8730 IsRightMostExpression = false;
8731
8732 E = E->IgnoreParenImpCasts();
8733
8734 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8735 if (!isa<VarDecl>(CurE->getDecl()))
8736 break;
8737
8738 RelevantExpr = CurE;
8739 continue;
8740 }
8741
8742 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8743 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8744
8745 if (isa<CXXThisExpr>(BaseE))
8746 // We found a base expression: this->Val.
8747 RelevantExpr = CurE;
8748 else
8749 E = BaseE;
8750
8751 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8752 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8753 << CurE->getSourceRange();
8754 break;
8755 }
8756
8757 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8758
8759 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8760 // A bit-field cannot appear in a map clause.
8761 //
8762 if (FD->isBitField()) {
8763 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8764 << CurE->getSourceRange();
8765 break;
8766 }
8767
8768 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8769 // If the type of a list item is a reference to a type T then the type
8770 // will be considered to be T for all purposes of this clause.
8771 QualType CurType = BaseE->getType();
8772 if (CurType->isReferenceType())
8773 CurType = CurType->getPointeeType();
8774
8775 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8776 // A list item cannot be a variable that is a member of a structure with
8777 // a union type.
8778 //
8779 if (auto *RT = CurType->getAs<RecordType>())
8780 if (RT->isUnionType()) {
8781 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
8782 << CurE->getSourceRange();
8783 break;
8784 }
8785
8786 continue;
8787 }
8788
8789 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
8790 E = CurE->getBase()->IgnoreParenImpCasts();
8791
8792 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
8793 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8794 << 0 << CurE->getSourceRange();
8795 break;
8796 }
8797 continue;
8798 }
8799
8800 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
8801 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
8802 // If a list item is an element of a structure, only the rightmost symbol
8803 // of the variable reference can be an array section.
8804 //
8805 if (!AllowArraySection) {
8806 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
8807 << CurE->getSourceRange();
8808 break;
8809 }
8810
8811 E = CurE->getBase()->IgnoreParenImpCasts();
8812
8813 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8814 // If the type of a list item is a reference to a type T then the type
8815 // will be considered to be T for all purposes of this clause.
8816 QualType CurType = E->getType();
8817 if (CurType->isReferenceType())
8818 CurType = CurType->getPointeeType();
8819
8820 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
8821 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8822 << 0 << CurE->getSourceRange();
8823 break;
8824 }
8825
8826 continue;
8827 }
8828
8829 // If nothing else worked, this is not a valid map clause expression.
8830 SemaRef.Diag(ELoc,
8831 diag::err_omp_expected_named_var_member_or_array_expression)
8832 << ERange;
8833 break;
8834 }
8835
8836 return RelevantExpr;
8837}
8838
8839// Return true if expression E associated with value VD has conflicts with other
8840// map information.
8841static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
8842 Expr *E, bool CurrentRegionOnly) {
8843 assert(VD && E);
8844
8845 // Types used to organize the components of a valid map clause.
8846 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
8847 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
8848
8849 // Helper to extract the components in the map clause expression E and store
8850 // them into MEC. This assumes that E is a valid map clause expression, i.e.
8851 // it has already passed the single clause checks.
8852 auto ExtractMapExpressionComponents = [](Expr *TE,
8853 MapExpressionComponents &MEC) {
8854 while (true) {
8855 TE = TE->IgnoreParenImpCasts();
8856
8857 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
8858 MEC.push_back(
8859 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
8860 break;
8861 }
8862
8863 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
8864 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8865
8866 MEC.push_back(MapExpressionComponent(
8867 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
8868 if (isa<CXXThisExpr>(BaseE))
8869 break;
8870
8871 TE = BaseE;
8872 continue;
8873 }
8874
8875 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
8876 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8877 TE = CurE->getBase()->IgnoreParenImpCasts();
8878 continue;
8879 }
8880
8881 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
8882 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8883 TE = CurE->getBase()->IgnoreParenImpCasts();
8884 continue;
8885 }
8886
8887 llvm_unreachable(
8888 "Expecting only valid map clause expressions at this point!");
8889 }
8890 };
8891
8892 SourceLocation ELoc = E->getExprLoc();
8893 SourceRange ERange = E->getSourceRange();
8894
8895 // In order to easily check the conflicts we need to match each component of
8896 // the expression under test with the components of the expressions that are
8897 // already in the stack.
8898
8899 MapExpressionComponents CurComponents;
8900 ExtractMapExpressionComponents(E, CurComponents);
8901
8902 assert(!CurComponents.empty() && "Map clause expression with no components!");
8903 assert(CurComponents.back().second == VD &&
8904 "Map clause expression with unexpected base!");
8905
8906 // Variables to help detecting enclosing problems in data environment nests.
8907 bool IsEnclosedByDataEnvironmentExpr = false;
8908 Expr *EnclosingExpr = nullptr;
8909
8910 bool FoundError =
8911 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
8912 MapExpressionComponents StackComponents;
8913 ExtractMapExpressionComponents(RE, StackComponents);
8914 assert(!StackComponents.empty() &&
8915 "Map clause expression with no components!");
8916 assert(StackComponents.back().second == VD &&
8917 "Map clause expression with unexpected base!");
8918
8919 // Expressions must start from the same base. Here we detect at which
8920 // point both expressions diverge from each other and see if we can
8921 // detect if the memory referred to both expressions is contiguous and
8922 // do not overlap.
8923 auto CI = CurComponents.rbegin();
8924 auto CE = CurComponents.rend();
8925 auto SI = StackComponents.rbegin();
8926 auto SE = StackComponents.rend();
8927 for (; CI != CE && SI != SE; ++CI, ++SI) {
8928
8929 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
8930 // At most one list item can be an array item derived from a given
8931 // variable in map clauses of the same construct.
8932 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
8933 isa<OMPArraySectionExpr>(CI->first)) &&
8934 (isa<ArraySubscriptExpr>(SI->first) ||
8935 isa<OMPArraySectionExpr>(SI->first))) {
8936 SemaRef.Diag(CI->first->getExprLoc(),
8937 diag::err_omp_multiple_array_items_in_map_clause)
8938 << CI->first->getSourceRange();
8939 ;
8940 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
8941 << SI->first->getSourceRange();
8942 return true;
8943 }
8944
8945 // Do both expressions have the same kind?
8946 if (CI->first->getStmtClass() != SI->first->getStmtClass())
8947 break;
8948
8949 // Are we dealing with different variables/fields?
8950 if (CI->second != SI->second)
8951 break;
8952 }
8953
8954 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8955 // List items of map clauses in the same construct must not share
8956 // original storage.
8957 //
8958 // If the expressions are exactly the same or one is a subset of the
8959 // other, it means they are sharing storage.
8960 if (CI == CE && SI == SE) {
8961 if (CurrentRegionOnly) {
8962 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8963 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8964 << RE->getSourceRange();
8965 return true;
8966 } else {
8967 // If we find the same expression in the enclosing data environment,
8968 // that is legal.
8969 IsEnclosedByDataEnvironmentExpr = true;
8970 return false;
8971 }
8972 }
8973
8974 QualType DerivedType = std::prev(CI)->first->getType();
8975 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
8976
8977 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8978 // If the type of a list item is a reference to a type T then the type
8979 // will be considered to be T for all purposes of this clause.
8980 if (DerivedType->isReferenceType())
8981 DerivedType = DerivedType->getPointeeType();
8982
8983 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
8984 // A variable for which the type is pointer and an array section
8985 // derived from that variable must not appear as list items of map
8986 // clauses of the same construct.
8987 //
8988 // Also, cover one of the cases in:
8989 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
8990 // If any part of the original storage of a list item has corresponding
8991 // storage in the device data environment, all of the original storage
8992 // must have corresponding storage in the device data environment.
8993 //
8994 if (DerivedType->isAnyPointerType()) {
8995 if (CI == CE || SI == SE) {
8996 SemaRef.Diag(
8997 DerivedLoc,
8998 diag::err_omp_pointer_mapped_along_with_derived_section)
8999 << DerivedLoc;
9000 } else {
9001 assert(CI != CE && SI != SE);
9002 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9003 << DerivedLoc;
9004 }
9005 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9006 << RE->getSourceRange();
9007 return true;
9008 }
9009
9010 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9011 // List items of map clauses in the same construct must not share
9012 // original storage.
9013 //
9014 // An expression is a subset of the other.
9015 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9016 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9017 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9018 << RE->getSourceRange();
9019 return true;
9020 }
9021
9022 // The current expression uses the same base as other expression in the
9023 // data environment but does not contain it completelly.
9024 if (!CurrentRegionOnly && SI != SE)
9025 EnclosingExpr = RE;
9026
9027 // The current expression is a subset of the expression in the data
9028 // environment.
9029 IsEnclosedByDataEnvironmentExpr |=
9030 (!CurrentRegionOnly && CI != CE && SI == SE);
9031
9032 return false;
9033 });
9034
9035 if (CurrentRegionOnly)
9036 return FoundError;
9037
9038 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9039 // If any part of the original storage of a list item has corresponding
9040 // storage in the device data environment, all of the original storage must
9041 // have corresponding storage in the device data environment.
9042 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9043 // If a list item is an element of a structure, and a different element of
9044 // the structure has a corresponding list item in the device data environment
9045 // prior to a task encountering the construct associated with the map clause,
9046 // then the list item must also have a correspnding list item in the device
9047 // data environment prior to the task encountering the construct.
9048 //
9049 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9050 SemaRef.Diag(ELoc,
9051 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9052 << ERange;
9053 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9054 << EnclosingExpr->getSourceRange();
9055 return true;
9056 }
9057
9058 return FoundError;
9059}
9060
Samuel Antao23abd722016-01-19 20:40:49 +00009061OMPClause *
9062Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9063 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9064 SourceLocation MapLoc, SourceLocation ColonLoc,
9065 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9066 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009067 SmallVector<Expr *, 4> Vars;
9068
9069 for (auto &RE : VarList) {
9070 assert(RE && "Null expr in omp map");
9071 if (isa<DependentScopeDeclRefExpr>(RE)) {
9072 // It will be analyzed later.
9073 Vars.push_back(RE);
9074 continue;
9075 }
9076 SourceLocation ELoc = RE->getExprLoc();
9077
Kelvin Li0bff7af2015-11-23 05:32:03 +00009078 auto *VE = RE->IgnoreParenLValueCasts();
9079
9080 if (VE->isValueDependent() || VE->isTypeDependent() ||
9081 VE->isInstantiationDependent() ||
9082 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009083 // We can only analyze this information once the missing information is
9084 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009085 Vars.push_back(RE);
9086 continue;
9087 }
9088
9089 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009090
Samuel Antao5de996e2016-01-22 20:21:36 +00009091 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9092 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9093 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009094 continue;
9095 }
9096
Samuel Antao5de996e2016-01-22 20:21:36 +00009097 // Obtain the array or member expression bases if required.
9098 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9099 if (!BE)
9100 continue;
9101
9102 // If the base is a reference to a variable, we rely on that variable for
9103 // the following checks. If it is a 'this' expression we rely on the field.
9104 ValueDecl *D = nullptr;
9105 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9106 D = DRE->getDecl();
9107 } else {
9108 auto *ME = cast<MemberExpr>(BE);
9109 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9110 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009111 }
9112 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009113
Samuel Antao5de996e2016-01-22 20:21:36 +00009114 auto *VD = dyn_cast<VarDecl>(D);
9115 auto *FD = dyn_cast<FieldDecl>(D);
9116
9117 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009118 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009119
9120 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9121 // threadprivate variables cannot appear in a map clause.
9122 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009123 auto DVar = DSAStack->getTopDSA(VD, false);
9124 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9125 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9126 continue;
9127 }
9128
Samuel Antao5de996e2016-01-22 20:21:36 +00009129 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9130 // A list item cannot appear in both a map clause and a data-sharing
9131 // attribute clause on the same construct.
9132 //
9133 // TODO: Implement this check - it cannot currently be tested because of
9134 // missing implementation of the other data sharing clauses in target
9135 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009136
Samuel Antao5de996e2016-01-22 20:21:36 +00009137 // Check conflicts with other map clause expressions. We check the conflicts
9138 // with the current construct separately from the enclosing data
9139 // environment, because the restrictions are different.
9140 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9141 /*CurrentRegionOnly=*/true))
9142 break;
9143 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9144 /*CurrentRegionOnly=*/false))
9145 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009146
Samuel Antao5de996e2016-01-22 20:21:36 +00009147 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9148 // If the type of a list item is a reference to a type T then the type will
9149 // be considered to be T for all purposes of this clause.
9150 QualType Type = D->getType();
9151 if (Type->isReferenceType())
9152 Type = Type->getPointeeType();
9153
9154 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009155 // A list item must have a mappable type.
9156 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9157 DSAStack, Type))
9158 continue;
9159
Samuel Antaodf67fc42016-01-19 19:15:56 +00009160 // target enter data
9161 // OpenMP [2.10.2, Restrictions, p. 99]
9162 // A map-type must be specified in all map clauses and must be either
9163 // to or alloc.
9164 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9165 if (DKind == OMPD_target_enter_data &&
9166 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9167 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009168 << (IsMapTypeImplicit ? 1 : 0)
9169 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009170 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009171 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009172 }
9173
Samuel Antao72590762016-01-19 20:04:50 +00009174 // target exit_data
9175 // OpenMP [2.10.3, Restrictions, p. 102]
9176 // A map-type must be specified in all map clauses and must be either
9177 // from, release, or delete.
9178 DKind = DSAStack->getCurrentDirective();
9179 if (DKind == OMPD_target_exit_data &&
9180 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9181 MapType == OMPC_MAP_delete)) {
9182 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009183 << (IsMapTypeImplicit ? 1 : 0)
9184 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009185 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009186 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009187 }
9188
Kelvin Li0bff7af2015-11-23 05:32:03 +00009189 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009190 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009191 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009192
Samuel Antao5de996e2016-01-22 20:21:36 +00009193 // We need to produce a map clause even if we don't have variables so that
9194 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009195 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009196 MapTypeModifier, MapType, IsMapTypeImplicit,
9197 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009198}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009199
9200OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9201 SourceLocation StartLoc,
9202 SourceLocation LParenLoc,
9203 SourceLocation EndLoc) {
9204 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009205
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009206 // OpenMP [teams Constrcut, Restrictions]
9207 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009208 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9209 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009210 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009211
9212 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9213}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009214
9215OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9216 SourceLocation StartLoc,
9217 SourceLocation LParenLoc,
9218 SourceLocation EndLoc) {
9219 Expr *ValExpr = ThreadLimit;
9220
9221 // OpenMP [teams Constrcut, Restrictions]
9222 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009223 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9224 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009225 return nullptr;
9226
9227 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9228 EndLoc);
9229}
Alexey Bataeva0569352015-12-01 10:17:31 +00009230
9231OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9232 SourceLocation StartLoc,
9233 SourceLocation LParenLoc,
9234 SourceLocation EndLoc) {
9235 Expr *ValExpr = Priority;
9236
9237 // OpenMP [2.9.1, task Constrcut]
9238 // The priority-value is a non-negative numerical scalar expression.
9239 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9240 /*StrictlyPositive=*/false))
9241 return nullptr;
9242
9243 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9244}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009245
9246OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9247 SourceLocation StartLoc,
9248 SourceLocation LParenLoc,
9249 SourceLocation EndLoc) {
9250 Expr *ValExpr = Grainsize;
9251
9252 // OpenMP [2.9.2, taskloop Constrcut]
9253 // The parameter of the grainsize clause must be a positive integer
9254 // expression.
9255 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9256 /*StrictlyPositive=*/true))
9257 return nullptr;
9258
9259 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9260}
Alexey Bataev382967a2015-12-08 12:06:20 +00009261
9262OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9263 SourceLocation StartLoc,
9264 SourceLocation LParenLoc,
9265 SourceLocation EndLoc) {
9266 Expr *ValExpr = NumTasks;
9267
9268 // OpenMP [2.9.2, taskloop Constrcut]
9269 // The parameter of the num_tasks clause must be a positive integer
9270 // expression.
9271 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9272 /*StrictlyPositive=*/true))
9273 return nullptr;
9274
9275 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9276}
9277
Alexey Bataev28c75412015-12-15 08:19:24 +00009278OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9279 SourceLocation LParenLoc,
9280 SourceLocation EndLoc) {
9281 // OpenMP [2.13.2, critical construct, Description]
9282 // ... where hint-expression is an integer constant expression that evaluates
9283 // to a valid lock hint.
9284 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9285 if (HintExpr.isInvalid())
9286 return nullptr;
9287 return new (Context)
9288 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9289}
9290
Carlo Bertollib4adf552016-01-15 18:50:31 +00009291OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9292 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9293 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9294 SourceLocation EndLoc) {
9295 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9296 std::string Values;
9297 Values += "'";
9298 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9299 Values += "'";
9300 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9301 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9302 return nullptr;
9303 }
9304 Expr *ValExpr = ChunkSize;
9305 Expr *HelperValExpr = nullptr;
9306 if (ChunkSize) {
9307 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9308 !ChunkSize->isInstantiationDependent() &&
9309 !ChunkSize->containsUnexpandedParameterPack()) {
9310 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9311 ExprResult Val =
9312 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9313 if (Val.isInvalid())
9314 return nullptr;
9315
9316 ValExpr = Val.get();
9317
9318 // OpenMP [2.7.1, Restrictions]
9319 // chunk_size must be a loop invariant integer expression with a positive
9320 // value.
9321 llvm::APSInt Result;
9322 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9323 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9324 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9325 << "dist_schedule" << ChunkSize->getSourceRange();
9326 return nullptr;
9327 }
9328 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9329 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9330 ChunkSize->getType(), ".chunk.");
9331 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9332 ChunkSize->getExprLoc(),
9333 /*RefersToCapture=*/true);
9334 HelperValExpr = ImpVarRef;
9335 }
9336 }
9337 }
9338
9339 return new (Context)
9340 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9341 Kind, ValExpr, HelperValExpr);
9342}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009343
9344OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9345 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9346 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9347 SourceLocation KindLoc, SourceLocation EndLoc) {
9348 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9349 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9350 Kind != OMPC_DEFAULTMAP_scalar) {
9351 std::string Value;
9352 SourceLocation Loc;
9353 Value += "'";
9354 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9355 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9356 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9357 Loc = MLoc;
9358 } else {
9359 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9360 OMPC_DEFAULTMAP_scalar);
9361 Loc = KindLoc;
9362 }
9363 Value += "'";
9364 Diag(Loc, diag::err_omp_unexpected_clause_value)
9365 << Value << getOpenMPClauseName(OMPC_defaultmap);
9366 return nullptr;
9367 }
9368
9369 return new (Context)
9370 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9371}