blob: d9ea8fb0ccaf9f6f501028506994abd6e0f7f4eb [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
812 if (isOpenMPTargetDirective(DKind)) {
813 // 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 {
910 return isOpenMPTargetDirective(K);
911 },
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() &&
Samuel Antao4be30e92015-10-02 17:14:03 +0000947 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
948}
949
Alexey Bataeved09d242014-05-28 05:53:51 +0000950void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000951
952void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
953 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000954 Scope *CurScope, SourceLocation Loc) {
955 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000956 PushExpressionEvaluationContext(PotentiallyEvaluated);
957}
958
Alexey Bataevaac108a2015-06-23 04:51:00 +0000959void Sema::StartOpenMPClause(OpenMPClauseKind K) {
960 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000961}
962
Alexey Bataevaac108a2015-06-23 04:51:00 +0000963void Sema::EndOpenMPClause() {
964 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000965}
966
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000968 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
969 // A variable of class type (or array thereof) that appears in a lastprivate
970 // clause requires an accessible, unambiguous default constructor for the
971 // class type, unless the list item is also specified in a firstprivate
972 // clause.
973 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000974 for (auto *C : D->clauses()) {
975 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
976 SmallVector<Expr *, 8> PrivateCopies;
977 for (auto *DE : Clause->varlists()) {
978 if (DE->isValueDependent() || DE->isTypeDependent()) {
979 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000980 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000981 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000982 DE = DE->IgnoreParens();
983 VarDecl *VD = nullptr;
984 FieldDecl *FD = nullptr;
985 ValueDecl *D;
986 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
987 VD = cast<VarDecl>(DRE->getDecl());
988 D = VD;
989 } else {
990 assert(isa<MemberExpr>(DE));
991 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
992 D = FD;
993 }
994 QualType Type = D->getType().getNonReferenceType();
995 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000996 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000997 // Generate helper private variable and initialize it with the
998 // default value. The address of the original variable is replaced
999 // by the address of the new private variable in CodeGen. This new
1000 // variable is not added to IdResolver, so the code in the OpenMP
1001 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001002 auto *VDPrivate = buildVarDecl(
1003 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001004 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001005 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1006 if (VDPrivate->isInvalidDecl())
1007 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001008 PrivateCopies.push_back(buildDeclRefExpr(
1009 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001010 } else {
1011 // The variable is also a firstprivate, so initialization sequence
1012 // for private copy is generated already.
1013 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001014 }
1015 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001016 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001017 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001018 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001019 }
1020 }
1021 }
1022
Alexey Bataev758e55e2013-09-06 18:03:48 +00001023 DSAStack->pop();
1024 DiscardCleanupsInEvaluationContext();
1025 PopExpressionEvaluationContext();
1026}
1027
Alexander Musman3276a272015-03-21 10:12:56 +00001028static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1029 Expr *NumIterations, Sema &SemaRef,
1030 Scope *S);
1031
Alexey Bataeva769e072013-03-22 06:34:35 +00001032namespace {
1033
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001034class VarDeclFilterCCC : public CorrectionCandidateCallback {
1035private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001036 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001037
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001038public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001039 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001040 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041 NamedDecl *ND = Candidate.getCorrectionDecl();
1042 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1043 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001044 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1045 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001046 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001047 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001048 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049};
Alexey Bataeved09d242014-05-28 05:53:51 +00001050} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001051
1052ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1053 CXXScopeSpec &ScopeSpec,
1054 const DeclarationNameInfo &Id) {
1055 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1056 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1057
1058 if (Lookup.isAmbiguous())
1059 return ExprError();
1060
1061 VarDecl *VD;
1062 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001063 if (TypoCorrection Corrected = CorrectTypo(
1064 Id, LookupOrdinaryName, CurScope, nullptr,
1065 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001066 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001067 PDiag(Lookup.empty()
1068 ? diag::err_undeclared_var_use_suggest
1069 : diag::err_omp_expected_var_arg_suggest)
1070 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001071 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001072 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001073 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1074 : diag::err_omp_expected_var_arg)
1075 << Id.getName();
1076 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001077 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078 } else {
1079 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001080 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1082 return ExprError();
1083 }
1084 }
1085 Lookup.suppressDiagnostics();
1086
1087 // OpenMP [2.9.2, Syntax, C/C++]
1088 // Variables must be file-scope, namespace-scope, or static block-scope.
1089 if (!VD->hasGlobalStorage()) {
1090 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001091 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1092 bool IsDecl =
1093 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001094 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001095 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1096 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001097 return ExprError();
1098 }
1099
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001100 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1101 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001102 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1103 // A threadprivate directive for file-scope variables must appear outside
1104 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001105 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1106 !getCurLexicalContext()->isTranslationUnit()) {
1107 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001108 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1109 bool IsDecl =
1110 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1111 Diag(VD->getLocation(),
1112 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1113 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001114 return ExprError();
1115 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001116 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1117 // A threadprivate directive for static class member variables must appear
1118 // in the class definition, in the same scope in which the member
1119 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001120 if (CanonicalVD->isStaticDataMember() &&
1121 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1122 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001123 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1124 bool IsDecl =
1125 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1126 Diag(VD->getLocation(),
1127 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1128 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001129 return ExprError();
1130 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1132 // A threadprivate directive for namespace-scope variables must appear
1133 // outside any definition or declaration other than the namespace
1134 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001135 if (CanonicalVD->getDeclContext()->isNamespace() &&
1136 (!getCurLexicalContext()->isFileContext() ||
1137 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1138 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001139 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1140 bool IsDecl =
1141 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1142 Diag(VD->getLocation(),
1143 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1144 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001145 return ExprError();
1146 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001147 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1148 // A threadprivate directive for static block-scope variables must appear
1149 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001150 if (CanonicalVD->isStaticLocal() && CurScope &&
1151 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001152 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001153 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1154 bool IsDecl =
1155 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1156 Diag(VD->getLocation(),
1157 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1158 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 return ExprError();
1160 }
1161
1162 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1163 // A threadprivate directive must lexically precede all references to any
1164 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001165 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001166 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001167 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001168 return ExprError();
1169 }
1170
1171 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001172 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001173 return DE;
1174}
1175
Alexey Bataeved09d242014-05-28 05:53:51 +00001176Sema::DeclGroupPtrTy
1177Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1178 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001180 CurContext->addDecl(D);
1181 return DeclGroupPtrTy::make(DeclGroupRef(D));
1182 }
David Blaikie0403cb12016-01-15 23:43:25 +00001183 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001184}
1185
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001186namespace {
1187class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1188 Sema &SemaRef;
1189
1190public:
1191 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1192 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1193 if (VD->hasLocalStorage()) {
1194 SemaRef.Diag(E->getLocStart(),
1195 diag::err_omp_local_var_in_threadprivate_init)
1196 << E->getSourceRange();
1197 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1198 << VD << VD->getSourceRange();
1199 return true;
1200 }
1201 }
1202 return false;
1203 }
1204 bool VisitStmt(const Stmt *S) {
1205 for (auto Child : S->children()) {
1206 if (Child && Visit(Child))
1207 return true;
1208 }
1209 return false;
1210 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001211 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001212};
1213} // namespace
1214
Alexey Bataeved09d242014-05-28 05:53:51 +00001215OMPThreadPrivateDecl *
1216Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001217 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001218 for (auto &RefExpr : VarList) {
1219 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001220 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1221 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001222
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001223 QualType QType = VD->getType();
1224 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1225 // It will be analyzed later.
1226 Vars.push_back(DE);
1227 continue;
1228 }
1229
Alexey Bataeva769e072013-03-22 06:34:35 +00001230 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1231 // A threadprivate variable must not have an incomplete type.
1232 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001233 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001234 continue;
1235 }
1236
1237 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1238 // A threadprivate variable must not have a reference type.
1239 if (VD->getType()->isReferenceType()) {
1240 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001241 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1242 bool IsDecl =
1243 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1244 Diag(VD->getLocation(),
1245 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1246 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001247 continue;
1248 }
1249
Samuel Antaof8b50122015-07-13 22:54:53 +00001250 // Check if this is a TLS variable. If TLS is not being supported, produce
1251 // the corresponding diagnostic.
1252 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1253 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1254 getLangOpts().OpenMPUseTLS &&
1255 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001256 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1257 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001258 Diag(ILoc, diag::err_omp_var_thread_local)
1259 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001260 bool IsDecl =
1261 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1262 Diag(VD->getLocation(),
1263 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1264 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001265 continue;
1266 }
1267
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001268 // Check if initial value of threadprivate variable reference variable with
1269 // local storage (it is not supported by runtime).
1270 if (auto Init = VD->getAnyInitializer()) {
1271 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001272 if (Checker.Visit(Init))
1273 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001274 }
1275
Alexey Bataeved09d242014-05-28 05:53:51 +00001276 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001277 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001278 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1279 Context, SourceRange(Loc, Loc)));
1280 if (auto *ML = Context.getASTMutationListener())
1281 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001282 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001283 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001284 if (!Vars.empty()) {
1285 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1286 Vars);
1287 D->setAccess(AS_public);
1288 }
1289 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001290}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001291
Alexey Bataev7ff55242014-06-19 09:13:45 +00001292static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001293 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001294 bool IsLoopIterVar = false) {
1295 if (DVar.RefExpr) {
1296 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1297 << getOpenMPClauseName(DVar.CKind);
1298 return;
1299 }
1300 enum {
1301 PDSA_StaticMemberShared,
1302 PDSA_StaticLocalVarShared,
1303 PDSA_LoopIterVarPrivate,
1304 PDSA_LoopIterVarLinear,
1305 PDSA_LoopIterVarLastprivate,
1306 PDSA_ConstVarShared,
1307 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001308 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001309 PDSA_LocalVarPrivate,
1310 PDSA_Implicit
1311 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001312 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001313 auto ReportLoc = D->getLocation();
1314 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001315 if (IsLoopIterVar) {
1316 if (DVar.CKind == OMPC_private)
1317 Reason = PDSA_LoopIterVarPrivate;
1318 else if (DVar.CKind == OMPC_lastprivate)
1319 Reason = PDSA_LoopIterVarLastprivate;
1320 else
1321 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1323 Reason = PDSA_TaskVarFirstprivate;
1324 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001325 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001327 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001328 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001329 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001330 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001331 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001332 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001333 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001334 ReportHint = true;
1335 Reason = PDSA_LocalVarPrivate;
1336 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001337 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001339 << Reason << ReportHint
1340 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1341 } else if (DVar.ImplicitDSALoc.isValid()) {
1342 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1343 << getOpenMPClauseName(DVar.CKind);
1344 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345}
1346
Alexey Bataev758e55e2013-09-06 18:03:48 +00001347namespace {
1348class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1349 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001350 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001351 bool ErrorFound;
1352 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001353 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001354 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001355
Alexey Bataev758e55e2013-09-06 18:03:48 +00001356public:
1357 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001358 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001359 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001360 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1361 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001362
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001363 auto DVar = Stack->getTopDSA(VD, false);
1364 // Check if the variable has explicit DSA set and stop analysis if it so.
1365 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001366
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001367 auto ELoc = E->getExprLoc();
1368 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369 // The default(none) clause requires that each variable that is referenced
1370 // in the construct, and does not have a predetermined data-sharing
1371 // attribute, must have its data-sharing attribute explicitly determined
1372 // by being listed in a data-sharing attribute clause.
1373 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001374 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001375 VarsWithInheritedDSA.count(VD) == 0) {
1376 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377 return;
1378 }
1379
1380 // OpenMP [2.9.3.6, Restrictions, p.2]
1381 // A list item that appears in a reduction clause of the innermost
1382 // enclosing worksharing or parallel construct may not be accessed in an
1383 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001384 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001385 [](OpenMPDirectiveKind K) -> bool {
1386 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001387 isOpenMPWorksharingDirective(K) ||
1388 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001389 },
1390 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001391 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1392 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001393 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1394 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001395 return;
1396 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001397
1398 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001399 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001400 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001401 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001402 }
1403 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001404 void VisitMemberExpr(MemberExpr *E) {
1405 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1406 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1407 auto DVar = Stack->getTopDSA(FD, false);
1408 // Check if the variable has explicit DSA set and stop analysis if it
1409 // so.
1410 if (DVar.RefExpr)
1411 return;
1412
1413 auto ELoc = E->getExprLoc();
1414 auto DKind = Stack->getCurrentDirective();
1415 // OpenMP [2.9.3.6, Restrictions, p.2]
1416 // A list item that appears in a reduction clause of the innermost
1417 // enclosing worksharing or parallel construct may not be accessed in
1418 // an
1419 // explicit task.
1420 DVar =
1421 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1422 [](OpenMPDirectiveKind K) -> bool {
1423 return isOpenMPParallelDirective(K) ||
1424 isOpenMPWorksharingDirective(K) ||
1425 isOpenMPTeamsDirective(K);
1426 },
1427 false);
1428 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1429 ErrorFound = true;
1430 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1431 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1432 return;
1433 }
1434
1435 // Define implicit data-sharing attributes for task.
1436 DVar = Stack->getImplicitDSA(FD, false);
1437 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1438 ImplicitFirstprivate.push_back(E);
1439 }
1440 }
1441 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001442 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001443 for (auto *C : S->clauses()) {
1444 // Skip analysis of arguments of implicitly defined firstprivate clause
1445 // for task directives.
1446 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1447 for (auto *CC : C->children()) {
1448 if (CC)
1449 Visit(CC);
1450 }
1451 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001452 }
1453 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001454 for (auto *C : S->children()) {
1455 if (C && !isa<OMPExecutableDirective>(C))
1456 Visit(C);
1457 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459
1460 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001461 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001462 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001463 return VarsWithInheritedDSA;
1464 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001465
Alexey Bataev7ff55242014-06-19 09:13:45 +00001466 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1467 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468};
Alexey Bataeved09d242014-05-28 05:53:51 +00001469} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001470
Alexey Bataevbae9a792014-06-27 10:37:06 +00001471void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001472 switch (DKind) {
1473 case OMPD_parallel: {
1474 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001475 QualType KmpInt32PtrTy =
1476 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001477 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001478 std::make_pair(".global_tid.", KmpInt32PtrTy),
1479 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1480 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001481 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001482 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1483 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001484 break;
1485 }
1486 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001487 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001488 std::make_pair(StringRef(), QualType()) // __context with shared vars
1489 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001490 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1491 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001492 break;
1493 }
1494 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001495 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001496 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001500 break;
1501 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001502 case OMPD_for_simd: {
1503 Sema::CapturedParamNameType Params[] = {
1504 std::make_pair(StringRef(), QualType()) // __context with shared vars
1505 };
1506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
1508 break;
1509 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001510 case OMPD_sections: {
1511 Sema::CapturedParamNameType Params[] = {
1512 std::make_pair(StringRef(), QualType()) // __context with shared vars
1513 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001516 break;
1517 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001518 case OMPD_section: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001524 break;
1525 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001526 case OMPD_single: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001532 break;
1533 }
Alexander Musman80c22892014-07-17 08:54:58 +00001534 case OMPD_master: {
1535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(StringRef(), QualType()) // __context with shared vars
1537 };
1538 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1539 Params);
1540 break;
1541 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001542 case OMPD_critical: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
1546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
1548 break;
1549 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001550 case OMPD_parallel_for: {
1551 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001552 QualType KmpInt32PtrTy =
1553 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001554 Sema::CapturedParamNameType Params[] = {
1555 std::make_pair(".global_tid.", KmpInt32PtrTy),
1556 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1557 std::make_pair(StringRef(), QualType()) // __context with shared vars
1558 };
1559 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1560 Params);
1561 break;
1562 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001563 case OMPD_parallel_for_simd: {
1564 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001565 QualType KmpInt32PtrTy =
1566 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001567 Sema::CapturedParamNameType Params[] = {
1568 std::make_pair(".global_tid.", KmpInt32PtrTy),
1569 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1570 std::make_pair(StringRef(), QualType()) // __context with shared vars
1571 };
1572 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1573 Params);
1574 break;
1575 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001576 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001577 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001578 QualType KmpInt32PtrTy =
1579 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001580 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001581 std::make_pair(".global_tid.", KmpInt32PtrTy),
1582 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001583 std::make_pair(StringRef(), QualType()) // __context with shared vars
1584 };
1585 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1586 Params);
1587 break;
1588 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001589 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001590 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001591 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1592 FunctionProtoType::ExtProtoInfo EPI;
1593 EPI.Variadic = true;
1594 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001595 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001596 std::make_pair(".global_tid.", KmpInt32Ty),
1597 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001598 std::make_pair(".privates.",
1599 Context.VoidPtrTy.withConst().withRestrict()),
1600 std::make_pair(
1601 ".copy_fn.",
1602 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001603 std::make_pair(StringRef(), QualType()) // __context with shared vars
1604 };
1605 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1606 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001607 // Mark this captured region as inlined, because we don't use outlined
1608 // function directly.
1609 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1610 AlwaysInlineAttr::CreateImplicit(
1611 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001612 break;
1613 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001614 case OMPD_ordered: {
1615 Sema::CapturedParamNameType Params[] = {
1616 std::make_pair(StringRef(), QualType()) // __context with shared vars
1617 };
1618 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1619 Params);
1620 break;
1621 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001622 case OMPD_atomic: {
1623 Sema::CapturedParamNameType Params[] = {
1624 std::make_pair(StringRef(), QualType()) // __context with shared vars
1625 };
1626 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1627 Params);
1628 break;
1629 }
Michael Wong65f367f2015-07-21 13:44:28 +00001630 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001631 case OMPD_target: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
1635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
1637 break;
1638 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001639 case OMPD_teams: {
1640 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001641 QualType KmpInt32PtrTy =
1642 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001643 Sema::CapturedParamNameType Params[] = {
1644 std::make_pair(".global_tid.", KmpInt32PtrTy),
1645 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1646 std::make_pair(StringRef(), QualType()) // __context with shared vars
1647 };
1648 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1649 Params);
1650 break;
1651 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001652 case OMPD_taskgroup: {
1653 Sema::CapturedParamNameType Params[] = {
1654 std::make_pair(StringRef(), QualType()) // __context with shared vars
1655 };
1656 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1657 Params);
1658 break;
1659 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001660 case OMPD_taskloop: {
1661 Sema::CapturedParamNameType Params[] = {
1662 std::make_pair(StringRef(), QualType()) // __context with shared vars
1663 };
1664 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1665 Params);
1666 break;
1667 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001668 case OMPD_taskloop_simd: {
1669 Sema::CapturedParamNameType Params[] = {
1670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
1674 break;
1675 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001676 case OMPD_distribute: {
1677 Sema::CapturedParamNameType Params[] = {
1678 std::make_pair(StringRef(), QualType()) // __context with shared vars
1679 };
1680 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1681 Params);
1682 break;
1683 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001684 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001685 case OMPD_taskyield:
1686 case OMPD_barrier:
1687 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001688 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001689 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001690 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001691 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001692 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001693 llvm_unreachable("OpenMP Directive is not allowed");
1694 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001695 llvm_unreachable("Unknown OpenMP directive");
1696 }
1697}
1698
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001699StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1700 ArrayRef<OMPClause *> Clauses) {
1701 if (!S.isUsable()) {
1702 ActOnCapturedRegionError();
1703 return StmtError();
1704 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001705
1706 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001707 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001708 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001709 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001710 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001711 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001712 Clause->getClauseKind() == OMPC_copyprivate ||
1713 (getLangOpts().OpenMPUseTLS &&
1714 getASTContext().getTargetInfo().isTLSSupported() &&
1715 Clause->getClauseKind() == OMPC_copyin)) {
1716 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001717 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001718 for (auto *VarRef : Clause->children()) {
1719 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001720 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001721 }
1722 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001723 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001724 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1725 Clause->getClauseKind() == OMPC_schedule) {
1726 // Mark all variables in private list clauses as used in inner region.
1727 // Required for proper codegen of combined directives.
1728 // TODO: add processing for other clauses.
1729 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001730 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1731 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001732 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001733 if (Clause->getClauseKind() == OMPC_schedule)
1734 SC = cast<OMPScheduleClause>(Clause);
1735 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001736 OC = cast<OMPOrderedClause>(Clause);
1737 else if (Clause->getClauseKind() == OMPC_linear)
1738 LCs.push_back(cast<OMPLinearClause>(Clause));
1739 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001740 bool ErrorFound = false;
1741 // OpenMP, 2.7.1 Loop Construct, Restrictions
1742 // The nonmonotonic modifier cannot be specified if an ordered clause is
1743 // specified.
1744 if (SC &&
1745 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1746 SC->getSecondScheduleModifier() ==
1747 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1748 OC) {
1749 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1750 ? SC->getFirstScheduleModifierLoc()
1751 : SC->getSecondScheduleModifierLoc(),
1752 diag::err_omp_schedule_nonmonotonic_ordered)
1753 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1754 ErrorFound = true;
1755 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001756 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1757 for (auto *C : LCs) {
1758 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1759 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1760 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001761 ErrorFound = true;
1762 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001763 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1764 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1765 OC->getNumForLoops()) {
1766 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1767 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1768 ErrorFound = true;
1769 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001770 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001771 ActOnCapturedRegionError();
1772 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001773 }
1774 return ActOnCapturedRegionEnd(S.get());
1775}
1776
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001777static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1778 OpenMPDirectiveKind CurrentRegion,
1779 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001780 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001781 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001782 // Allowed nesting of constructs
1783 // +------------------+-----------------+------------------------------------+
1784 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1785 // +------------------+-----------------+------------------------------------+
1786 // | parallel | parallel | * |
1787 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001788 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001789 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001790 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001791 // | parallel | simd | * |
1792 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001793 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001794 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001795 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001796 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001797 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001798 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001799 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001800 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001801 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001802 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001803 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001804 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001805 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001806 // | parallel | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001807 // | parallel | target enter | * |
1808 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001809 // | parallel | target exit | * |
1810 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001811 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001812 // | parallel | cancellation | |
1813 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001814 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001815 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001816 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001817 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001818 // +------------------+-----------------+------------------------------------+
1819 // | for | parallel | * |
1820 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001821 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001822 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001823 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001824 // | for | simd | * |
1825 // | for | sections | + |
1826 // | for | section | + |
1827 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001828 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001829 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001830 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001831 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001832 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001833 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001834 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001835 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001836 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001837 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001838 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001839 // | for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001840 // | for | target enter | * |
1841 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001842 // | for | target exit | * |
1843 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001844 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001845 // | for | cancellation | |
1846 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001847 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001848 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001849 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001850 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001851 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001852 // | master | parallel | * |
1853 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001854 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001855 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001856 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001857 // | master | simd | * |
1858 // | master | sections | + |
1859 // | master | section | + |
1860 // | master | single | + |
1861 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001862 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001863 // | master |parallel sections| * |
1864 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001865 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001866 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001867 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001868 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001869 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001870 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001871 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001872 // | master | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001873 // | master | target enter | * |
1874 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001875 // | master | target exit | * |
1876 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001877 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001878 // | master | cancellation | |
1879 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001880 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001881 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001882 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001883 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001884 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001885 // | critical | parallel | * |
1886 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001887 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001888 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001889 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001890 // | critical | simd | * |
1891 // | critical | sections | + |
1892 // | critical | section | + |
1893 // | critical | single | + |
1894 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001895 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001896 // | critical |parallel sections| * |
1897 // | critical | task | * |
1898 // | critical | taskyield | * |
1899 // | critical | barrier | + |
1900 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001901 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001902 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001903 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001904 // | critical | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001905 // | critical | target enter | * |
1906 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001907 // | critical | target exit | * |
1908 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001909 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001910 // | critical | cancellation | |
1911 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001912 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001913 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001914 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001915 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001916 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001917 // | simd | parallel | |
1918 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001919 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001920 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001921 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001922 // | simd | simd | |
1923 // | simd | sections | |
1924 // | simd | section | |
1925 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001926 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001927 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001928 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001929 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001930 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001931 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001932 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001933 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001934 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001935 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001936 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001937 // | simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001938 // | simd | target enter | |
1939 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001940 // | simd | target exit | |
1941 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001942 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001943 // | simd | cancellation | |
1944 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001945 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001946 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001947 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001948 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001949 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001950 // | for simd | parallel | |
1951 // | for simd | for | |
1952 // | for simd | for simd | |
1953 // | for simd | master | |
1954 // | for simd | critical | |
1955 // | for simd | simd | |
1956 // | for simd | sections | |
1957 // | for simd | section | |
1958 // | for simd | single | |
1959 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001960 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001961 // | for simd |parallel sections| |
1962 // | for simd | task | |
1963 // | for simd | taskyield | |
1964 // | for simd | barrier | |
1965 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001966 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001967 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001968 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001969 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001970 // | for simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001971 // | for simd | target enter | |
1972 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001973 // | for simd | target exit | |
1974 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001975 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001976 // | for simd | cancellation | |
1977 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001978 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001979 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001980 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001981 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001982 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001983 // | parallel for simd| parallel | |
1984 // | parallel for simd| for | |
1985 // | parallel for simd| for simd | |
1986 // | parallel for simd| master | |
1987 // | parallel for simd| critical | |
1988 // | parallel for simd| simd | |
1989 // | parallel for simd| sections | |
1990 // | parallel for simd| section | |
1991 // | parallel for simd| single | |
1992 // | parallel for simd| parallel for | |
1993 // | parallel for simd|parallel for simd| |
1994 // | parallel for simd|parallel sections| |
1995 // | parallel for simd| task | |
1996 // | parallel for simd| taskyield | |
1997 // | parallel for simd| barrier | |
1998 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001999 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002000 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002001 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002002 // | parallel for simd| atomic | |
2003 // | parallel for simd| target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002004 // | parallel for simd| target enter | |
2005 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002006 // | parallel for simd| target exit | |
2007 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002008 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002009 // | parallel for simd| cancellation | |
2010 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002011 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002012 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002013 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002014 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002015 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002016 // | sections | parallel | * |
2017 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002018 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002019 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002020 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002021 // | sections | simd | * |
2022 // | sections | sections | + |
2023 // | sections | section | * |
2024 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002025 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002026 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002027 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002028 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002029 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002030 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002031 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002032 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002033 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002034 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002035 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002036 // | sections | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002037 // | sections | target enter | * |
2038 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002039 // | sections | target exit | * |
2040 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002041 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002042 // | sections | cancellation | |
2043 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002044 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002045 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002046 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002047 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002048 // +------------------+-----------------+------------------------------------+
2049 // | section | parallel | * |
2050 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002051 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002052 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002053 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002054 // | section | simd | * |
2055 // | section | sections | + |
2056 // | section | section | + |
2057 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002058 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002059 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002060 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002061 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002062 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002063 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002064 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002065 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002066 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002067 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002068 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002069 // | section | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002070 // | section | target enter | * |
2071 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002072 // | section | target exit | * |
2073 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002074 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002075 // | section | cancellation | |
2076 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002077 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002078 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002079 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002080 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002081 // +------------------+-----------------+------------------------------------+
2082 // | single | parallel | * |
2083 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002084 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002085 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002086 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002087 // | single | simd | * |
2088 // | single | sections | + |
2089 // | single | section | + |
2090 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002091 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002092 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002093 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002094 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002095 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002096 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002097 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002098 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002099 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002100 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002101 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002102 // | single | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002103 // | single | target enter | * |
2104 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002105 // | single | target exit | * |
2106 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002107 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002108 // | single | cancellation | |
2109 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002110 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002111 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002112 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002113 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002114 // +------------------+-----------------+------------------------------------+
2115 // | parallel for | parallel | * |
2116 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002117 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002118 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002119 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002120 // | parallel for | simd | * |
2121 // | parallel for | sections | + |
2122 // | parallel for | section | + |
2123 // | parallel for | single | + |
2124 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002125 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002126 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002127 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002128 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002129 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002130 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002131 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002132 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002133 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002134 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002135 // | parallel for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002136 // | parallel for | target enter | * |
2137 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002138 // | parallel for | target exit | * |
2139 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002140 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002141 // | parallel for | cancellation | |
2142 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002143 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002144 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002145 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002146 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002147 // +------------------+-----------------+------------------------------------+
2148 // | parallel sections| parallel | * |
2149 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002150 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002151 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002152 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002153 // | parallel sections| simd | * |
2154 // | parallel sections| sections | + |
2155 // | parallel sections| section | * |
2156 // | parallel sections| single | + |
2157 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002158 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002159 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002160 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002161 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002162 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002163 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002164 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002165 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002166 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002167 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002168 // | parallel sections| target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002169 // | parallel sections| target enter | * |
2170 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002171 // | parallel sections| target exit | * |
2172 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002173 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002174 // | parallel sections| cancellation | |
2175 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002176 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002177 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002178 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002179 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002180 // +------------------+-----------------+------------------------------------+
2181 // | task | parallel | * |
2182 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002183 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002184 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002185 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002186 // | task | simd | * |
2187 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002188 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002189 // | task | single | + |
2190 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002191 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002192 // | task |parallel sections| * |
2193 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002194 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002195 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002196 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002197 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002198 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002199 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002200 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002201 // | task | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002202 // | task | target enter | * |
2203 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002204 // | task | target exit | * |
2205 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002206 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002207 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002208 // | | point | ! |
2209 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002210 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002211 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002212 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002213 // +------------------+-----------------+------------------------------------+
2214 // | ordered | parallel | * |
2215 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002216 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002217 // | ordered | master | * |
2218 // | ordered | critical | * |
2219 // | ordered | simd | * |
2220 // | ordered | sections | + |
2221 // | ordered | section | + |
2222 // | ordered | single | + |
2223 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002224 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002225 // | ordered |parallel sections| * |
2226 // | ordered | task | * |
2227 // | ordered | taskyield | * |
2228 // | ordered | barrier | + |
2229 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002230 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002231 // | ordered | flush | * |
2232 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002233 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002234 // | ordered | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002235 // | ordered | target enter | * |
2236 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002237 // | ordered | target exit | * |
2238 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002239 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002240 // | ordered | cancellation | |
2241 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002242 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002243 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002244 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002245 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002246 // +------------------+-----------------+------------------------------------+
2247 // | atomic | parallel | |
2248 // | atomic | for | |
2249 // | atomic | for simd | |
2250 // | atomic | master | |
2251 // | atomic | critical | |
2252 // | atomic | simd | |
2253 // | atomic | sections | |
2254 // | atomic | section | |
2255 // | atomic | single | |
2256 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002257 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002258 // | atomic |parallel sections| |
2259 // | atomic | task | |
2260 // | atomic | taskyield | |
2261 // | atomic | barrier | |
2262 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002263 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002264 // | atomic | flush | |
2265 // | atomic | ordered | |
2266 // | atomic | atomic | |
2267 // | atomic | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002268 // | atomic | target enter | |
2269 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002270 // | atomic | target exit | |
2271 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002272 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002273 // | atomic | cancellation | |
2274 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002275 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002276 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002277 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002278 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002279 // +------------------+-----------------+------------------------------------+
2280 // | target | parallel | * |
2281 // | target | for | * |
2282 // | target | for simd | * |
2283 // | target | master | * |
2284 // | target | critical | * |
2285 // | target | simd | * |
2286 // | target | sections | * |
2287 // | target | section | * |
2288 // | target | single | * |
2289 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002290 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002291 // | target |parallel sections| * |
2292 // | target | task | * |
2293 // | target | taskyield | * |
2294 // | target | barrier | * |
2295 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002296 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002297 // | target | flush | * |
2298 // | target | ordered | * |
2299 // | target | atomic | * |
2300 // | target | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002301 // | target | target enter | * |
2302 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002303 // | target | target exit | * |
2304 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002305 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002306 // | target | cancellation | |
2307 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002308 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002309 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002310 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002311 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002312 // +------------------+-----------------+------------------------------------+
2313 // | teams | parallel | * |
2314 // | teams | for | + |
2315 // | teams | for simd | + |
2316 // | teams | master | + |
2317 // | teams | critical | + |
2318 // | teams | simd | + |
2319 // | teams | sections | + |
2320 // | teams | section | + |
2321 // | teams | single | + |
2322 // | teams | parallel for | * |
2323 // | teams |parallel for simd| * |
2324 // | teams |parallel sections| * |
2325 // | teams | task | + |
2326 // | teams | taskyield | + |
2327 // | teams | barrier | + |
2328 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002329 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002330 // | teams | flush | + |
2331 // | teams | ordered | + |
2332 // | teams | atomic | + |
2333 // | teams | target | + |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002334 // | teams | target enter | + |
2335 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002336 // | teams | target exit | + |
2337 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002338 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002339 // | teams | cancellation | |
2340 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002341 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002342 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002343 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002344 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002345 // +------------------+-----------------+------------------------------------+
2346 // | taskloop | parallel | * |
2347 // | taskloop | for | + |
2348 // | taskloop | for simd | + |
2349 // | taskloop | master | + |
2350 // | taskloop | critical | * |
2351 // | taskloop | simd | * |
2352 // | taskloop | sections | + |
2353 // | taskloop | section | + |
2354 // | taskloop | single | + |
2355 // | taskloop | parallel for | * |
2356 // | taskloop |parallel for simd| * |
2357 // | taskloop |parallel sections| * |
2358 // | taskloop | task | * |
2359 // | taskloop | taskyield | * |
2360 // | taskloop | barrier | + |
2361 // | taskloop | taskwait | * |
2362 // | taskloop | taskgroup | * |
2363 // | taskloop | flush | * |
2364 // | taskloop | ordered | + |
2365 // | taskloop | atomic | * |
2366 // | taskloop | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002367 // | taskloop | target enter | * |
2368 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002369 // | taskloop | target exit | * |
2370 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002371 // | taskloop | teams | + |
2372 // | taskloop | cancellation | |
2373 // | | point | |
2374 // | taskloop | cancel | |
2375 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002376 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002377 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002378 // | taskloop simd | parallel | |
2379 // | taskloop simd | for | |
2380 // | taskloop simd | for simd | |
2381 // | taskloop simd | master | |
2382 // | taskloop simd | critical | |
2383 // | taskloop simd | simd | |
2384 // | taskloop simd | sections | |
2385 // | taskloop simd | section | |
2386 // | taskloop simd | single | |
2387 // | taskloop simd | parallel for | |
2388 // | taskloop simd |parallel for simd| |
2389 // | taskloop simd |parallel sections| |
2390 // | taskloop simd | task | |
2391 // | taskloop simd | taskyield | |
2392 // | taskloop simd | barrier | |
2393 // | taskloop simd | taskwait | |
2394 // | taskloop simd | taskgroup | |
2395 // | taskloop simd | flush | |
2396 // | taskloop simd | ordered | + (with simd clause) |
2397 // | taskloop simd | atomic | |
2398 // | taskloop simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002399 // | taskloop simd | target enter | |
2400 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002401 // | taskloop simd | target exit | |
2402 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002403 // | taskloop simd | teams | |
2404 // | taskloop simd | cancellation | |
2405 // | | point | |
2406 // | taskloop simd | cancel | |
2407 // | taskloop simd | taskloop | |
2408 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002409 // | taskloop simd | distribute | |
2410 // +------------------+-----------------+------------------------------------+
2411 // | distribute | parallel | * |
2412 // | distribute | for | * |
2413 // | distribute | for simd | * |
2414 // | distribute | master | * |
2415 // | distribute | critical | * |
2416 // | distribute | simd | * |
2417 // | distribute | sections | * |
2418 // | distribute | section | * |
2419 // | distribute | single | * |
2420 // | distribute | parallel for | * |
2421 // | distribute |parallel for simd| * |
2422 // | distribute |parallel sections| * |
2423 // | distribute | task | * |
2424 // | distribute | taskyield | * |
2425 // | distribute | barrier | * |
2426 // | distribute | taskwait | * |
2427 // | distribute | taskgroup | * |
2428 // | distribute | flush | * |
2429 // | distribute | ordered | + |
2430 // | distribute | atomic | * |
2431 // | distribute | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002432 // | distribute | target enter | |
2433 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002434 // | distribute | target exit | |
2435 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002436 // | distribute | teams | |
2437 // | distribute | cancellation | + |
2438 // | | point | |
2439 // | distribute | cancel | + |
2440 // | distribute | taskloop | * |
2441 // | distribute | taskloop simd | * |
2442 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002443 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002444 if (Stack->getCurScope()) {
2445 auto ParentRegion = Stack->getParentDirective();
2446 bool NestingProhibited = false;
2447 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002448 enum {
2449 NoRecommend,
2450 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002451 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002452 ShouldBeInTargetRegion,
2453 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002454 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002455 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002456 // OpenMP [2.16, Nesting of Regions]
2457 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002458 // OpenMP [2.8.1,simd Construct, Restrictions]
2459 // An ordered construct with the simd clause is the only OpenMP construct
2460 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002461 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2462 return true;
2463 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002464 if (ParentRegion == OMPD_atomic) {
2465 // OpenMP [2.16, Nesting of Regions]
2466 // OpenMP constructs may not be nested inside an atomic region.
2467 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2468 return true;
2469 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002470 if (CurrentRegion == OMPD_section) {
2471 // OpenMP [2.7.2, sections Construct, Restrictions]
2472 // Orphaned section directives are prohibited. That is, the section
2473 // directives must appear within the sections construct and must not be
2474 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002475 if (ParentRegion != OMPD_sections &&
2476 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002477 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2478 << (ParentRegion != OMPD_unknown)
2479 << getOpenMPDirectiveName(ParentRegion);
2480 return true;
2481 }
2482 return false;
2483 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002484 // Allow some constructs to be orphaned (they could be used in functions,
2485 // called from OpenMP regions with the required preconditions).
2486 if (ParentRegion == OMPD_unknown)
2487 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002488 if (CurrentRegion == OMPD_cancellation_point ||
2489 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002490 // OpenMP [2.16, Nesting of Regions]
2491 // A cancellation point construct for which construct-type-clause is
2492 // taskgroup must be nested inside a task construct. A cancellation
2493 // point construct for which construct-type-clause is not taskgroup must
2494 // be closely nested inside an OpenMP construct that matches the type
2495 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002496 // A cancel construct for which construct-type-clause is taskgroup must be
2497 // nested inside a task construct. A cancel construct for which
2498 // construct-type-clause is not taskgroup must be closely nested inside an
2499 // OpenMP construct that matches the type specified in
2500 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002501 NestingProhibited =
2502 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002503 (CancelRegion == OMPD_for &&
2504 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002505 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2506 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002507 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2508 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002509 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002510 // OpenMP [2.16, Nesting of Regions]
2511 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002512 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002513 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002514 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002515 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002516 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2517 // OpenMP [2.16, Nesting of Regions]
2518 // A critical region may not be nested (closely or otherwise) inside a
2519 // critical region with the same name. Note that this restriction is not
2520 // sufficient to prevent deadlock.
2521 SourceLocation PreviousCriticalLoc;
2522 bool DeadLock =
2523 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2524 OpenMPDirectiveKind K,
2525 const DeclarationNameInfo &DNI,
2526 SourceLocation Loc)
2527 ->bool {
2528 if (K == OMPD_critical &&
2529 DNI.getName() == CurrentName.getName()) {
2530 PreviousCriticalLoc = Loc;
2531 return true;
2532 } else
2533 return false;
2534 },
2535 false /* skip top directive */);
2536 if (DeadLock) {
2537 SemaRef.Diag(StartLoc,
2538 diag::err_omp_prohibited_region_critical_same_name)
2539 << CurrentName.getName();
2540 if (PreviousCriticalLoc.isValid())
2541 SemaRef.Diag(PreviousCriticalLoc,
2542 diag::note_omp_previous_critical_region);
2543 return true;
2544 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002545 } else if (CurrentRegion == OMPD_barrier) {
2546 // OpenMP [2.16, Nesting of Regions]
2547 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002548 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002549 NestingProhibited =
2550 isOpenMPWorksharingDirective(ParentRegion) ||
2551 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002552 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002553 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002554 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002555 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002556 // OpenMP [2.16, Nesting of Regions]
2557 // A worksharing region may not be closely nested inside a worksharing,
2558 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002559 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002560 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002561 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002562 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002563 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002564 Recommend = ShouldBeInParallelRegion;
2565 } else if (CurrentRegion == OMPD_ordered) {
2566 // OpenMP [2.16, Nesting of Regions]
2567 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002568 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002569 // An ordered region must be closely nested inside a loop region (or
2570 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002571 // OpenMP [2.8.1,simd Construct, Restrictions]
2572 // An ordered construct with the simd clause is the only OpenMP construct
2573 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002574 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002575 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002576 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002577 !(isOpenMPSimdDirective(ParentRegion) ||
2578 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002579 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002580 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2581 // OpenMP [2.16, Nesting of Regions]
2582 // If specified, a teams construct must be contained within a target
2583 // construct.
2584 NestingProhibited = ParentRegion != OMPD_target;
2585 Recommend = ShouldBeInTargetRegion;
2586 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2587 }
2588 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2589 // OpenMP [2.16, Nesting of Regions]
2590 // distribute, parallel, parallel sections, parallel workshare, and the
2591 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2592 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002593 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2594 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002595 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002596 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002597 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2598 // OpenMP 4.5 [2.17 Nesting of Regions]
2599 // The region associated with the distribute construct must be strictly
2600 // nested inside a teams region
2601 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2602 Recommend = ShouldBeInTeamsRegion;
2603 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002604 if (NestingProhibited) {
2605 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002606 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2607 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002608 return true;
2609 }
2610 }
2611 return false;
2612}
2613
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002614static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2615 ArrayRef<OMPClause *> Clauses,
2616 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2617 bool ErrorFound = false;
2618 unsigned NamedModifiersNumber = 0;
2619 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2620 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002621 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002622 for (const auto *C : Clauses) {
2623 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2624 // At most one if clause without a directive-name-modifier can appear on
2625 // the directive.
2626 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2627 if (FoundNameModifiers[CurNM]) {
2628 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2629 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2630 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2631 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002632 } else if (CurNM != OMPD_unknown) {
2633 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002634 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002635 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002636 FoundNameModifiers[CurNM] = IC;
2637 if (CurNM == OMPD_unknown)
2638 continue;
2639 // Check if the specified name modifier is allowed for the current
2640 // directive.
2641 // At most one if clause with the particular directive-name-modifier can
2642 // appear on the directive.
2643 bool MatchFound = false;
2644 for (auto NM : AllowedNameModifiers) {
2645 if (CurNM == NM) {
2646 MatchFound = true;
2647 break;
2648 }
2649 }
2650 if (!MatchFound) {
2651 S.Diag(IC->getNameModifierLoc(),
2652 diag::err_omp_wrong_if_directive_name_modifier)
2653 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2654 ErrorFound = true;
2655 }
2656 }
2657 }
2658 // If any if clause on the directive includes a directive-name-modifier then
2659 // all if clauses on the directive must include a directive-name-modifier.
2660 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2661 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2662 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2663 diag::err_omp_no_more_if_clause);
2664 } else {
2665 std::string Values;
2666 std::string Sep(", ");
2667 unsigned AllowedCnt = 0;
2668 unsigned TotalAllowedNum =
2669 AllowedNameModifiers.size() - NamedModifiersNumber;
2670 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2671 ++Cnt) {
2672 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2673 if (!FoundNameModifiers[NM]) {
2674 Values += "'";
2675 Values += getOpenMPDirectiveName(NM);
2676 Values += "'";
2677 if (AllowedCnt + 2 == TotalAllowedNum)
2678 Values += " or ";
2679 else if (AllowedCnt + 1 != TotalAllowedNum)
2680 Values += Sep;
2681 ++AllowedCnt;
2682 }
2683 }
2684 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2685 diag::err_omp_unnamed_if_clause)
2686 << (TotalAllowedNum > 1) << Values;
2687 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002688 for (auto Loc : NameModifierLoc) {
2689 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2690 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002691 ErrorFound = true;
2692 }
2693 return ErrorFound;
2694}
2695
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002696StmtResult Sema::ActOnOpenMPExecutableDirective(
2697 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2698 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2699 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002700 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002701 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2702 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002703 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002704
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002705 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002706 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002707 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002708 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002709 if (AStmt) {
2710 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2711
2712 // Check default data sharing attributes for referenced variables.
2713 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2714 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2715 if (DSAChecker.isErrorFound())
2716 return StmtError();
2717 // Generate list of implicitly defined firstprivate variables.
2718 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002719
2720 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2721 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2722 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2723 SourceLocation(), SourceLocation())) {
2724 ClausesWithImplicit.push_back(Implicit);
2725 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2726 DSAChecker.getImplicitFirstprivate().size();
2727 } else
2728 ErrorFound = true;
2729 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002730 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002731
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002732 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002733 switch (Kind) {
2734 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002735 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2736 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002737 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002738 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002739 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002740 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2741 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002742 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002743 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002744 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2745 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002746 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002747 case OMPD_for_simd:
2748 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2749 EndLoc, VarsWithInheritedDSA);
2750 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002751 case OMPD_sections:
2752 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2753 EndLoc);
2754 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002755 case OMPD_section:
2756 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002757 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002758 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2759 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002760 case OMPD_single:
2761 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2762 EndLoc);
2763 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002764 case OMPD_master:
2765 assert(ClausesWithImplicit.empty() &&
2766 "No clauses are allowed for 'omp master' directive");
2767 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2768 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002769 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002770 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2771 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002772 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002773 case OMPD_parallel_for:
2774 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2775 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002776 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002777 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002778 case OMPD_parallel_for_simd:
2779 Res = ActOnOpenMPParallelForSimdDirective(
2780 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002781 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002782 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002783 case OMPD_parallel_sections:
2784 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2785 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002786 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002787 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002788 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002789 Res =
2790 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002791 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002792 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002793 case OMPD_taskyield:
2794 assert(ClausesWithImplicit.empty() &&
2795 "No clauses are allowed for 'omp taskyield' directive");
2796 assert(AStmt == nullptr &&
2797 "No associated statement allowed for 'omp taskyield' directive");
2798 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2799 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002800 case OMPD_barrier:
2801 assert(ClausesWithImplicit.empty() &&
2802 "No clauses are allowed for 'omp barrier' directive");
2803 assert(AStmt == nullptr &&
2804 "No associated statement allowed for 'omp barrier' directive");
2805 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2806 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002807 case OMPD_taskwait:
2808 assert(ClausesWithImplicit.empty() &&
2809 "No clauses are allowed for 'omp taskwait' directive");
2810 assert(AStmt == nullptr &&
2811 "No associated statement allowed for 'omp taskwait' directive");
2812 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2813 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002814 case OMPD_taskgroup:
2815 assert(ClausesWithImplicit.empty() &&
2816 "No clauses are allowed for 'omp taskgroup' directive");
2817 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2818 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002819 case OMPD_flush:
2820 assert(AStmt == nullptr &&
2821 "No associated statement allowed for 'omp flush' directive");
2822 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2823 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002824 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002825 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2826 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002827 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002828 case OMPD_atomic:
2829 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2830 EndLoc);
2831 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002832 case OMPD_teams:
2833 Res =
2834 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2835 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002836 case OMPD_target:
2837 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2838 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002839 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002840 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002841 case OMPD_cancellation_point:
2842 assert(ClausesWithImplicit.empty() &&
2843 "No clauses are allowed for 'omp cancellation point' directive");
2844 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2845 "cancellation point' directive");
2846 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2847 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002848 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002849 assert(AStmt == nullptr &&
2850 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002851 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2852 CancelRegion);
2853 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002854 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002855 case OMPD_target_data:
2856 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2857 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002858 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002859 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002860 case OMPD_target_enter_data:
2861 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2862 EndLoc);
2863 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2864 break;
Samuel Antao72590762016-01-19 20:04:50 +00002865 case OMPD_target_exit_data:
2866 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2867 EndLoc);
2868 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2869 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002870 case OMPD_taskloop:
2871 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2872 EndLoc, VarsWithInheritedDSA);
2873 AllowedNameModifiers.push_back(OMPD_taskloop);
2874 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002875 case OMPD_taskloop_simd:
2876 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2877 EndLoc, VarsWithInheritedDSA);
2878 AllowedNameModifiers.push_back(OMPD_taskloop);
2879 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002880 case OMPD_distribute:
2881 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2882 EndLoc, VarsWithInheritedDSA);
2883 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002884 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002885 llvm_unreachable("OpenMP Directive is not allowed");
2886 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002887 llvm_unreachable("Unknown OpenMP directive");
2888 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002889
Alexey Bataev4acb8592014-07-07 13:01:15 +00002890 for (auto P : VarsWithInheritedDSA) {
2891 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2892 << P.first << P.second->getSourceRange();
2893 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002894 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2895
2896 if (!AllowedNameModifiers.empty())
2897 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2898 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002899
Alexey Bataeved09d242014-05-28 05:53:51 +00002900 if (ErrorFound)
2901 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002902 return Res;
2903}
2904
2905StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2906 Stmt *AStmt,
2907 SourceLocation StartLoc,
2908 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002909 if (!AStmt)
2910 return StmtError();
2911
Alexey Bataev9959db52014-05-06 10:08:46 +00002912 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2913 // 1.2.2 OpenMP Language Terminology
2914 // Structured block - An executable statement with a single entry at the
2915 // top and a single exit at the bottom.
2916 // The point of exit cannot be a branch out of the structured block.
2917 // longjmp() and throw() must not violate the entry/exit criteria.
2918 CS->getCapturedDecl()->setNothrow();
2919
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002920 getCurFunction()->setHasBranchProtectedScope();
2921
Alexey Bataev25e5b442015-09-15 12:52:43 +00002922 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2923 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002924}
2925
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002926namespace {
2927/// \brief Helper class for checking canonical form of the OpenMP loops and
2928/// extracting iteration space of each loop in the loop nest, that will be used
2929/// for IR generation.
2930class OpenMPIterationSpaceChecker {
2931 /// \brief Reference to Sema.
2932 Sema &SemaRef;
2933 /// \brief A location for diagnostics (when there is no some better location).
2934 SourceLocation DefaultLoc;
2935 /// \brief A location for diagnostics (when increment is not compatible).
2936 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002937 /// \brief A source location for referring to loop init later.
2938 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002939 /// \brief A source location for referring to condition later.
2940 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002941 /// \brief A source location for referring to increment later.
2942 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002943 /// \brief Loop variable.
2944 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002945 /// \brief Reference to loop variable.
2946 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002947 /// \brief Lower bound (initializer for the var).
2948 Expr *LB;
2949 /// \brief Upper bound.
2950 Expr *UB;
2951 /// \brief Loop step (increment).
2952 Expr *Step;
2953 /// \brief This flag is true when condition is one of:
2954 /// Var < UB
2955 /// Var <= UB
2956 /// UB > Var
2957 /// UB >= Var
2958 bool TestIsLessOp;
2959 /// \brief This flag is true when condition is strict ( < or > ).
2960 bool TestIsStrictOp;
2961 /// \brief This flag is true when step is subtracted on each iteration.
2962 bool SubtractStep;
2963
2964public:
2965 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2966 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002967 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2968 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002969 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2970 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002971 /// \brief Check init-expr for canonical loop form and save loop counter
2972 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002973 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002974 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2975 /// for less/greater and for strict/non-strict comparison.
2976 bool CheckCond(Expr *S);
2977 /// \brief Check incr-expr for canonical loop form and return true if it
2978 /// does not conform, otherwise save loop step (#Step).
2979 bool CheckInc(Expr *S);
2980 /// \brief Return the loop counter variable.
2981 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002982 /// \brief Return the reference expression to loop counter variable.
2983 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002984 /// \brief Source range of the loop init.
2985 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2986 /// \brief Source range of the loop condition.
2987 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2988 /// \brief Source range of the loop increment.
2989 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2990 /// \brief True if the step should be subtracted.
2991 bool ShouldSubtractStep() const { return SubtractStep; }
2992 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002993 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002994 /// \brief Build the precondition expression for the loops.
2995 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002996 /// \brief Build reference expression to the counter be used for codegen.
2997 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002998 /// \brief Build reference expression to the private counter be used for
2999 /// codegen.
3000 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003001 /// \brief Build initization of the counter be used for codegen.
3002 Expr *BuildCounterInit() const;
3003 /// \brief Build step of the counter be used for codegen.
3004 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003005 /// \brief Return true if any expression is dependent.
3006 bool Dependent() const;
3007
3008private:
3009 /// \brief Check the right-hand side of an assignment in the increment
3010 /// expression.
3011 bool CheckIncRHS(Expr *RHS);
3012 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003013 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003014 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003015 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003016 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003017 /// \brief Helper to set loop increment.
3018 bool SetStep(Expr *NewStep, bool Subtract);
3019};
3020
3021bool OpenMPIterationSpaceChecker::Dependent() const {
3022 if (!Var) {
3023 assert(!LB && !UB && !Step);
3024 return false;
3025 }
3026 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3027 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3028}
3029
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003030template <typename T>
3031static T *getExprAsWritten(T *E) {
3032 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3033 E = ExprTemp->getSubExpr();
3034
3035 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3036 E = MTE->GetTemporaryExpr();
3037
3038 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3039 E = Binder->getSubExpr();
3040
3041 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3042 E = ICE->getSubExprAsWritten();
3043 return E->IgnoreParens();
3044}
3045
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003046bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3047 DeclRefExpr *NewVarRefExpr,
3048 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003049 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003050 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3051 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003052 if (!NewVar || !NewLB)
3053 return true;
3054 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003055 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003056 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3057 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003058 if ((Ctor->isCopyOrMoveConstructor() ||
3059 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3060 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003061 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003062 LB = NewLB;
3063 return false;
3064}
3065
3066bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003067 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003068 // State consistency checking to ensure correct usage.
3069 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3070 !TestIsLessOp && !TestIsStrictOp);
3071 if (!NewUB)
3072 return true;
3073 UB = NewUB;
3074 TestIsLessOp = LessOp;
3075 TestIsStrictOp = StrictOp;
3076 ConditionSrcRange = SR;
3077 ConditionLoc = SL;
3078 return false;
3079}
3080
3081bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3082 // State consistency checking to ensure correct usage.
3083 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3084 if (!NewStep)
3085 return true;
3086 if (!NewStep->isValueDependent()) {
3087 // Check that the step is integer expression.
3088 SourceLocation StepLoc = NewStep->getLocStart();
3089 ExprResult Val =
3090 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3091 if (Val.isInvalid())
3092 return true;
3093 NewStep = Val.get();
3094
3095 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3096 // If test-expr is of form var relational-op b and relational-op is < or
3097 // <= then incr-expr must cause var to increase on each iteration of the
3098 // loop. If test-expr is of form var relational-op b and relational-op is
3099 // > or >= then incr-expr must cause var to decrease on each iteration of
3100 // the loop.
3101 // If test-expr is of form b relational-op var and relational-op is < or
3102 // <= then incr-expr must cause var to decrease on each iteration of the
3103 // loop. If test-expr is of form b relational-op var and relational-op is
3104 // > or >= then incr-expr must cause var to increase on each iteration of
3105 // the loop.
3106 llvm::APSInt Result;
3107 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3108 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3109 bool IsConstNeg =
3110 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003111 bool IsConstPos =
3112 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003113 bool IsConstZero = IsConstant && !Result.getBoolValue();
3114 if (UB && (IsConstZero ||
3115 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003116 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003117 SemaRef.Diag(NewStep->getExprLoc(),
3118 diag::err_omp_loop_incr_not_compatible)
3119 << Var << TestIsLessOp << NewStep->getSourceRange();
3120 SemaRef.Diag(ConditionLoc,
3121 diag::note_omp_loop_cond_requres_compatible_incr)
3122 << TestIsLessOp << ConditionSrcRange;
3123 return true;
3124 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003125 if (TestIsLessOp == Subtract) {
3126 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3127 NewStep).get();
3128 Subtract = !Subtract;
3129 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003130 }
3131
3132 Step = NewStep;
3133 SubtractStep = Subtract;
3134 return false;
3135}
3136
Alexey Bataev9c821032015-04-30 04:23:23 +00003137bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003138 // Check init-expr for canonical loop form and save loop counter
3139 // variable - #Var and its initialization value - #LB.
3140 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3141 // var = lb
3142 // integer-type var = lb
3143 // random-access-iterator-type var = lb
3144 // pointer-type var = lb
3145 //
3146 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003147 if (EmitDiags) {
3148 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3149 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003150 return true;
3151 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003152 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003153 if (Expr *E = dyn_cast<Expr>(S))
3154 S = E->IgnoreParens();
3155 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3156 if (BO->getOpcode() == BO_Assign)
3157 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003158 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003159 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003160 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3161 if (DS->isSingleDecl()) {
3162 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003163 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003164 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003165 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003166 SemaRef.Diag(S->getLocStart(),
3167 diag::ext_omp_loop_not_canonical_init)
3168 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003169 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003170 }
3171 }
3172 }
3173 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3174 if (CE->getOperator() == OO_Equal)
3175 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003176 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3177 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003178
Alexey Bataev9c821032015-04-30 04:23:23 +00003179 if (EmitDiags) {
3180 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3181 << S->getSourceRange();
3182 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003183 return true;
3184}
3185
Alexey Bataev23b69422014-06-18 07:08:49 +00003186/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003187/// variable (which may be the loop variable) if possible.
3188static const VarDecl *GetInitVarDecl(const Expr *E) {
3189 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003190 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003191 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003192 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3193 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003194 if ((Ctor->isCopyOrMoveConstructor() ||
3195 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3196 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003197 E = CE->getArg(0)->IgnoreParenImpCasts();
3198 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3199 if (!DRE)
3200 return nullptr;
3201 return dyn_cast<VarDecl>(DRE->getDecl());
3202}
3203
3204bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3205 // Check test-expr for canonical form, save upper-bound UB, flags for
3206 // less/greater and for strict/non-strict comparison.
3207 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3208 // var relational-op b
3209 // b relational-op var
3210 //
3211 if (!S) {
3212 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3213 return true;
3214 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003215 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003216 SourceLocation CondLoc = S->getLocStart();
3217 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3218 if (BO->isRelationalOp()) {
3219 if (GetInitVarDecl(BO->getLHS()) == Var)
3220 return SetUB(BO->getRHS(),
3221 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3222 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3223 BO->getSourceRange(), BO->getOperatorLoc());
3224 if (GetInitVarDecl(BO->getRHS()) == Var)
3225 return SetUB(BO->getLHS(),
3226 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3227 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3228 BO->getSourceRange(), BO->getOperatorLoc());
3229 }
3230 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3231 if (CE->getNumArgs() == 2) {
3232 auto Op = CE->getOperator();
3233 switch (Op) {
3234 case OO_Greater:
3235 case OO_GreaterEqual:
3236 case OO_Less:
3237 case OO_LessEqual:
3238 if (GetInitVarDecl(CE->getArg(0)) == Var)
3239 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3240 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3241 CE->getOperatorLoc());
3242 if (GetInitVarDecl(CE->getArg(1)) == Var)
3243 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3244 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3245 CE->getOperatorLoc());
3246 break;
3247 default:
3248 break;
3249 }
3250 }
3251 }
3252 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3253 << S->getSourceRange() << Var;
3254 return true;
3255}
3256
3257bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3258 // RHS of canonical loop form increment can be:
3259 // var + incr
3260 // incr + var
3261 // var - incr
3262 //
3263 RHS = RHS->IgnoreParenImpCasts();
3264 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3265 if (BO->isAdditiveOp()) {
3266 bool IsAdd = BO->getOpcode() == BO_Add;
3267 if (GetInitVarDecl(BO->getLHS()) == Var)
3268 return SetStep(BO->getRHS(), !IsAdd);
3269 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3270 return SetStep(BO->getLHS(), false);
3271 }
3272 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3273 bool IsAdd = CE->getOperator() == OO_Plus;
3274 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3275 if (GetInitVarDecl(CE->getArg(0)) == Var)
3276 return SetStep(CE->getArg(1), !IsAdd);
3277 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3278 return SetStep(CE->getArg(0), false);
3279 }
3280 }
3281 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3282 << RHS->getSourceRange() << Var;
3283 return true;
3284}
3285
3286bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3287 // Check incr-expr for canonical loop form and return true if it
3288 // does not conform.
3289 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3290 // ++var
3291 // var++
3292 // --var
3293 // var--
3294 // var += incr
3295 // var -= incr
3296 // var = var + incr
3297 // var = incr + var
3298 // var = var - incr
3299 //
3300 if (!S) {
3301 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3302 return true;
3303 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003304 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003305 S = S->IgnoreParens();
3306 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3307 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3308 return SetStep(
3309 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3310 (UO->isDecrementOp() ? -1 : 1)).get(),
3311 false);
3312 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3313 switch (BO->getOpcode()) {
3314 case BO_AddAssign:
3315 case BO_SubAssign:
3316 if (GetInitVarDecl(BO->getLHS()) == Var)
3317 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3318 break;
3319 case BO_Assign:
3320 if (GetInitVarDecl(BO->getLHS()) == Var)
3321 return CheckIncRHS(BO->getRHS());
3322 break;
3323 default:
3324 break;
3325 }
3326 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3327 switch (CE->getOperator()) {
3328 case OO_PlusPlus:
3329 case OO_MinusMinus:
3330 if (GetInitVarDecl(CE->getArg(0)) == Var)
3331 return SetStep(
3332 SemaRef.ActOnIntegerConstant(
3333 CE->getLocStart(),
3334 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3335 false);
3336 break;
3337 case OO_PlusEqual:
3338 case OO_MinusEqual:
3339 if (GetInitVarDecl(CE->getArg(0)) == Var)
3340 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3341 break;
3342 case OO_Equal:
3343 if (GetInitVarDecl(CE->getArg(0)) == Var)
3344 return CheckIncRHS(CE->getArg(1));
3345 break;
3346 default:
3347 break;
3348 }
3349 }
3350 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3351 << S->getSourceRange() << Var;
3352 return true;
3353}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003354
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003355namespace {
3356// Transform variables declared in GNU statement expressions to new ones to
3357// avoid crash on codegen.
3358class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3359 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3360
3361public:
3362 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3363
3364 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3365 if (auto *VD = cast<VarDecl>(D))
3366 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3367 !isa<ImplicitParamDecl>(D)) {
3368 auto *NewVD = VarDecl::Create(
3369 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3370 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3371 VD->getTypeSourceInfo(), VD->getStorageClass());
3372 NewVD->setTSCSpec(VD->getTSCSpec());
3373 NewVD->setInit(VD->getInit());
3374 NewVD->setInitStyle(VD->getInitStyle());
3375 NewVD->setExceptionVariable(VD->isExceptionVariable());
3376 NewVD->setNRVOVariable(VD->isNRVOVariable());
3377 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3378 NewVD->setConstexpr(VD->isConstexpr());
3379 NewVD->setInitCapture(VD->isInitCapture());
3380 NewVD->setPreviousDeclInSameBlockScope(
3381 VD->isPreviousDeclInSameBlockScope());
3382 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003383 if (VD->hasAttrs())
3384 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003385 transformedLocalDecl(VD, NewVD);
3386 return NewVD;
3387 }
3388 return BaseTransform::TransformDefinition(Loc, D);
3389 }
3390
3391 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3392 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3393 if (E->getDecl() != NewD) {
3394 NewD->setReferenced();
3395 NewD->markUsed(SemaRef.Context);
3396 return DeclRefExpr::Create(
3397 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3398 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3399 E->getNameInfo(), E->getType(), E->getValueKind());
3400 }
3401 return BaseTransform::TransformDeclRefExpr(E);
3402 }
3403};
3404}
3405
Alexander Musmana5f070a2014-10-01 06:03:56 +00003406/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003407Expr *
3408OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3409 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003410 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003411 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003412 auto VarType = Var->getType().getNonReferenceType();
3413 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003414 SemaRef.getLangOpts().CPlusPlus) {
3415 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003416 auto *UBExpr = TestIsLessOp ? UB : LB;
3417 auto *LBExpr = TestIsLessOp ? LB : UB;
3418 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3419 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3420 if (!Upper || !Lower)
3421 return nullptr;
3422 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3423 Sema::AA_Converting,
3424 /*AllowExplicit=*/true)
3425 .get();
3426 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3427 Sema::AA_Converting,
3428 /*AllowExplicit=*/true)
3429 .get();
3430 if (!Upper || !Lower)
3431 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003432
3433 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3434
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003435 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003436 // BuildBinOp already emitted error, this one is to point user to upper
3437 // and lower bound, and to tell what is passed to 'operator-'.
3438 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3439 << Upper->getSourceRange() << Lower->getSourceRange();
3440 return nullptr;
3441 }
3442 }
3443
3444 if (!Diff.isUsable())
3445 return nullptr;
3446
3447 // Upper - Lower [- 1]
3448 if (TestIsStrictOp)
3449 Diff = SemaRef.BuildBinOp(
3450 S, DefaultLoc, BO_Sub, Diff.get(),
3451 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3452 if (!Diff.isUsable())
3453 return nullptr;
3454
3455 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003456 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3457 if (NewStep.isInvalid())
3458 return nullptr;
3459 NewStep = SemaRef.PerformImplicitConversion(
3460 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3461 /*AllowExplicit=*/true);
3462 if (NewStep.isInvalid())
3463 return nullptr;
3464 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003465 if (!Diff.isUsable())
3466 return nullptr;
3467
3468 // Parentheses (for dumping/debugging purposes only).
3469 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3470 if (!Diff.isUsable())
3471 return nullptr;
3472
3473 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003474 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3475 if (NewStep.isInvalid())
3476 return nullptr;
3477 NewStep = SemaRef.PerformImplicitConversion(
3478 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3479 /*AllowExplicit=*/true);
3480 if (NewStep.isInvalid())
3481 return nullptr;
3482 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003483 if (!Diff.isUsable())
3484 return nullptr;
3485
Alexander Musman174b3ca2014-10-06 11:16:29 +00003486 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003487 QualType Type = Diff.get()->getType();
3488 auto &C = SemaRef.Context;
3489 bool UseVarType = VarType->hasIntegerRepresentation() &&
3490 C.getTypeSize(Type) > C.getTypeSize(VarType);
3491 if (!Type->isIntegerType() || UseVarType) {
3492 unsigned NewSize =
3493 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3494 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3495 : Type->hasSignedIntegerRepresentation();
3496 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3497 Diff = SemaRef.PerformImplicitConversion(
3498 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3499 if (!Diff.isUsable())
3500 return nullptr;
3501 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003502 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003503 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3504 if (NewSize != C.getTypeSize(Type)) {
3505 if (NewSize < C.getTypeSize(Type)) {
3506 assert(NewSize == 64 && "incorrect loop var size");
3507 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3508 << InitSrcRange << ConditionSrcRange;
3509 }
3510 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003511 NewSize, Type->hasSignedIntegerRepresentation() ||
3512 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003513 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3514 Sema::AA_Converting, true);
3515 if (!Diff.isUsable())
3516 return nullptr;
3517 }
3518 }
3519
Alexander Musmana5f070a2014-10-01 06:03:56 +00003520 return Diff.get();
3521}
3522
Alexey Bataev62dbb972015-04-22 11:59:37 +00003523Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3524 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3525 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3526 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003527 TransformToNewDefs Transform(SemaRef);
3528
3529 auto NewLB = Transform.TransformExpr(LB);
3530 auto NewUB = Transform.TransformExpr(UB);
3531 if (NewLB.isInvalid() || NewUB.isInvalid())
3532 return Cond;
3533 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3534 Sema::AA_Converting,
3535 /*AllowExplicit=*/true);
3536 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3537 Sema::AA_Converting,
3538 /*AllowExplicit=*/true);
3539 if (NewLB.isInvalid() || NewUB.isInvalid())
3540 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003541 auto CondExpr = SemaRef.BuildBinOp(
3542 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3543 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003544 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003545 if (CondExpr.isUsable()) {
3546 CondExpr = SemaRef.PerformImplicitConversion(
3547 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3548 /*AllowExplicit=*/true);
3549 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003550 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3551 // Otherwise use original loop conditon and evaluate it in runtime.
3552 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3553}
3554
Alexander Musmana5f070a2014-10-01 06:03:56 +00003555/// \brief Build reference expression to the counter be used for codegen.
3556Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003557 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3558 DefaultLoc);
3559}
3560
3561Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3562 if (Var && !Var->isInvalidDecl()) {
3563 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003564 auto *PrivateVar =
3565 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3566 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003567 if (PrivateVar->isInvalidDecl())
3568 return nullptr;
3569 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3570 }
3571 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003572}
3573
3574/// \brief Build initization of the counter be used for codegen.
3575Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3576
3577/// \brief Build step of the counter be used for codegen.
3578Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3579
3580/// \brief Iteration space of a single for loop.
3581struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003582 /// \brief Condition of the loop.
3583 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003584 /// \brief This expression calculates the number of iterations in the loop.
3585 /// It is always possible to calculate it before starting the loop.
3586 Expr *NumIterations;
3587 /// \brief The loop counter variable.
3588 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003589 /// \brief Private loop counter variable.
3590 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003591 /// \brief This is initializer for the initial value of #CounterVar.
3592 Expr *CounterInit;
3593 /// \brief This is step for the #CounterVar used to generate its update:
3594 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3595 Expr *CounterStep;
3596 /// \brief Should step be subtracted?
3597 bool Subtract;
3598 /// \brief Source range of the loop init.
3599 SourceRange InitSrcRange;
3600 /// \brief Source range of the loop condition.
3601 SourceRange CondSrcRange;
3602 /// \brief Source range of the loop increment.
3603 SourceRange IncSrcRange;
3604};
3605
Alexey Bataev23b69422014-06-18 07:08:49 +00003606} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003607
Alexey Bataev9c821032015-04-30 04:23:23 +00003608void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3609 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3610 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003611 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3612 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003613 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3614 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003615 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003616 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003617 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003618 }
3619}
3620
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003621/// \brief Called on a for stmt to check and extract its iteration space
3622/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003623static bool CheckOpenMPIterationSpace(
3624 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3625 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003626 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003627 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003628 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003629 // OpenMP [2.6, Canonical Loop Form]
3630 // for (init-expr; test-expr; incr-expr) structured-block
3631 auto For = dyn_cast_or_null<ForStmt>(S);
3632 if (!For) {
3633 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003634 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3635 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3636 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3637 if (NestedLoopCount > 1) {
3638 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3639 SemaRef.Diag(DSA.getConstructLoc(),
3640 diag::note_omp_collapse_ordered_expr)
3641 << 2 << CollapseLoopCountExpr->getSourceRange()
3642 << OrderedLoopCountExpr->getSourceRange();
3643 else if (CollapseLoopCountExpr)
3644 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3645 diag::note_omp_collapse_ordered_expr)
3646 << 0 << CollapseLoopCountExpr->getSourceRange();
3647 else
3648 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3649 diag::note_omp_collapse_ordered_expr)
3650 << 1 << OrderedLoopCountExpr->getSourceRange();
3651 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003652 return true;
3653 }
3654 assert(For->getBody());
3655
3656 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3657
3658 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003659 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003660 if (ISC.CheckInit(Init)) {
3661 return true;
3662 }
3663
3664 bool HasErrors = false;
3665
3666 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003667 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003668
3669 // OpenMP [2.6, Canonical Loop Form]
3670 // Var is one of the following:
3671 // A variable of signed or unsigned integer type.
3672 // For C++, a variable of a random access iterator type.
3673 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003674 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003675 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3676 !VarType->isPointerType() &&
3677 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3678 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3679 << SemaRef.getLangOpts().CPlusPlus;
3680 HasErrors = true;
3681 }
3682
Alexey Bataev4acb8592014-07-07 13:01:15 +00003683 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3684 // Construct
3685 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3686 // parallel for construct is (are) private.
3687 // The loop iteration variable in the associated for-loop of a simd construct
3688 // with just one associated for-loop is linear with a constant-linear-step
3689 // that is the increment of the associated for-loop.
3690 // Exclude loop var from the list of variables with implicitly defined data
3691 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003692 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003693
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003694 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3695 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003696 // The loop iteration variable in the associated for-loop of a simd construct
3697 // with just one associated for-loop may be listed in a linear clause with a
3698 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003699 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3700 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003701 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003702 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3703 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3704 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003705 auto PredeterminedCKind =
3706 isOpenMPSimdDirective(DKind)
3707 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3708 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003709 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003710 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003711 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003712 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003713 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003714 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3715 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003716 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003717 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3718 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003719 if (DVar.RefExpr == nullptr)
3720 DVar.CKind = PredeterminedCKind;
3721 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003722 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003723 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003724 // Make the loop iteration variable private (for worksharing constructs),
3725 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003726 // lastprivate (for simd directives with several collapsed or ordered
3727 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003728 if (DVar.CKind == OMPC_unknown)
3729 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3730 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003731 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003732 }
3733
Alexey Bataev7ff55242014-06-19 09:13:45 +00003734 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003735
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003736 // Check test-expr.
3737 HasErrors |= ISC.CheckCond(For->getCond());
3738
3739 // Check incr-expr.
3740 HasErrors |= ISC.CheckInc(For->getInc());
3741
Alexander Musmana5f070a2014-10-01 06:03:56 +00003742 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003743 return HasErrors;
3744
Alexander Musmana5f070a2014-10-01 06:03:56 +00003745 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003746 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003747 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003748 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003749 isOpenMPTaskLoopDirective(DKind) ||
3750 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003751 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003752 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003753 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3754 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3755 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3756 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3757 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3758 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3759
Alexey Bataev62dbb972015-04-22 11:59:37 +00003760 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3761 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003762 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003763 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003764 ResultIterSpace.CounterInit == nullptr ||
3765 ResultIterSpace.CounterStep == nullptr);
3766
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003767 return HasErrors;
3768}
3769
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003770/// \brief Build 'VarRef = Start.
3771static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3772 ExprResult VarRef, ExprResult Start) {
3773 TransformToNewDefs Transform(SemaRef);
3774 // Build 'VarRef = Start.
3775 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3776 if (NewStart.isInvalid())
3777 return ExprError();
3778 NewStart = SemaRef.PerformImplicitConversion(
3779 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3780 Sema::AA_Converting,
3781 /*AllowExplicit=*/true);
3782 if (NewStart.isInvalid())
3783 return ExprError();
3784 NewStart = SemaRef.PerformImplicitConversion(
3785 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3786 /*AllowExplicit=*/true);
3787 if (!NewStart.isUsable())
3788 return ExprError();
3789
3790 auto Init =
3791 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3792 return Init;
3793}
3794
Alexander Musmana5f070a2014-10-01 06:03:56 +00003795/// \brief Build 'VarRef = Start + Iter * Step'.
3796static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3797 SourceLocation Loc, ExprResult VarRef,
3798 ExprResult Start, ExprResult Iter,
3799 ExprResult Step, bool Subtract) {
3800 // Add parentheses (for debugging purposes only).
3801 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3802 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3803 !Step.isUsable())
3804 return ExprError();
3805
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003806 TransformToNewDefs Transform(SemaRef);
3807 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3808 if (NewStep.isInvalid())
3809 return ExprError();
3810 NewStep = SemaRef.PerformImplicitConversion(
3811 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3812 Sema::AA_Converting,
3813 /*AllowExplicit=*/true);
3814 if (NewStep.isInvalid())
3815 return ExprError();
3816 ExprResult Update =
3817 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003818 if (!Update.isUsable())
3819 return ExprError();
3820
3821 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003822 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3823 if (NewStart.isInvalid())
3824 return ExprError();
3825 NewStart = SemaRef.PerformImplicitConversion(
3826 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3827 Sema::AA_Converting,
3828 /*AllowExplicit=*/true);
3829 if (NewStart.isInvalid())
3830 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003831 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003832 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003833 if (!Update.isUsable())
3834 return ExprError();
3835
3836 Update = SemaRef.PerformImplicitConversion(
3837 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3838 if (!Update.isUsable())
3839 return ExprError();
3840
3841 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3842 return Update;
3843}
3844
3845/// \brief Convert integer expression \a E to make it have at least \a Bits
3846/// bits.
3847static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3848 Sema &SemaRef) {
3849 if (E == nullptr)
3850 return ExprError();
3851 auto &C = SemaRef.Context;
3852 QualType OldType = E->getType();
3853 unsigned HasBits = C.getTypeSize(OldType);
3854 if (HasBits >= Bits)
3855 return ExprResult(E);
3856 // OK to convert to signed, because new type has more bits than old.
3857 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3858 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3859 true);
3860}
3861
3862/// \brief Check if the given expression \a E is a constant integer that fits
3863/// into \a Bits bits.
3864static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3865 if (E == nullptr)
3866 return false;
3867 llvm::APSInt Result;
3868 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3869 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3870 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003871}
3872
3873/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003874/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3875/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003876static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003877CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3878 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3879 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003880 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003881 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003882 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003883 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003884 // Found 'collapse' clause - calculate collapse number.
3885 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003886 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003887 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003888 }
3889 if (OrderedLoopCountExpr) {
3890 // Found 'ordered' clause - calculate collapse number.
3891 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003892 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3893 if (Result.getLimitedValue() < NestedLoopCount) {
3894 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3895 diag::err_omp_wrong_ordered_loop_count)
3896 << OrderedLoopCountExpr->getSourceRange();
3897 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3898 diag::note_collapse_loop_count)
3899 << CollapseLoopCountExpr->getSourceRange();
3900 }
3901 NestedLoopCount = Result.getLimitedValue();
3902 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003903 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003904 // This is helper routine for loop directives (e.g., 'for', 'simd',
3905 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003906 SmallVector<LoopIterationSpace, 4> IterSpaces;
3907 IterSpaces.resize(NestedLoopCount);
3908 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003909 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003910 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003911 NestedLoopCount, CollapseLoopCountExpr,
3912 OrderedLoopCountExpr, VarsWithImplicitDSA,
3913 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003914 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003915 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003916 // OpenMP [2.8.1, simd construct, Restrictions]
3917 // All loops associated with the construct must be perfectly nested; that
3918 // is, there must be no intervening code nor any OpenMP directive between
3919 // any two loops.
3920 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003921 }
3922
Alexander Musmana5f070a2014-10-01 06:03:56 +00003923 Built.clear(/* size */ NestedLoopCount);
3924
3925 if (SemaRef.CurContext->isDependentContext())
3926 return NestedLoopCount;
3927
3928 // An example of what is generated for the following code:
3929 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003930 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003931 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003932 // for (k = 0; k < NK; ++k)
3933 // for (j = J0; j < NJ; j+=2) {
3934 // <loop body>
3935 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003936 //
3937 // We generate the code below.
3938 // Note: the loop body may be outlined in CodeGen.
3939 // Note: some counters may be C++ classes, operator- is used to find number of
3940 // iterations and operator+= to calculate counter value.
3941 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3942 // or i64 is currently supported).
3943 //
3944 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3945 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3946 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3947 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3948 // // similar updates for vars in clauses (e.g. 'linear')
3949 // <loop body (using local i and j)>
3950 // }
3951 // i = NI; // assign final values of counters
3952 // j = NJ;
3953 //
3954
3955 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3956 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003957 // Precondition tests if there is at least one iteration (all conditions are
3958 // true).
3959 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003960 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003961 ExprResult LastIteration32 = WidenIterationCount(
3962 32 /* Bits */, SemaRef.PerformImplicitConversion(
3963 N0->IgnoreImpCasts(), N0->getType(),
3964 Sema::AA_Converting, /*AllowExplicit=*/true)
3965 .get(),
3966 SemaRef);
3967 ExprResult LastIteration64 = WidenIterationCount(
3968 64 /* Bits */, SemaRef.PerformImplicitConversion(
3969 N0->IgnoreImpCasts(), N0->getType(),
3970 Sema::AA_Converting, /*AllowExplicit=*/true)
3971 .get(),
3972 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003973
3974 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3975 return NestedLoopCount;
3976
3977 auto &C = SemaRef.Context;
3978 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3979
3980 Scope *CurScope = DSA.getCurScope();
3981 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003982 if (PreCond.isUsable()) {
3983 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3984 PreCond.get(), IterSpaces[Cnt].PreCond);
3985 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003986 auto N = IterSpaces[Cnt].NumIterations;
3987 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3988 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003989 LastIteration32 = SemaRef.BuildBinOp(
3990 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3991 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3992 Sema::AA_Converting,
3993 /*AllowExplicit=*/true)
3994 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003995 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003996 LastIteration64 = SemaRef.BuildBinOp(
3997 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3998 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3999 Sema::AA_Converting,
4000 /*AllowExplicit=*/true)
4001 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004002 }
4003
4004 // Choose either the 32-bit or 64-bit version.
4005 ExprResult LastIteration = LastIteration64;
4006 if (LastIteration32.isUsable() &&
4007 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4008 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4009 FitsInto(
4010 32 /* Bits */,
4011 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4012 LastIteration64.get(), SemaRef)))
4013 LastIteration = LastIteration32;
4014
4015 if (!LastIteration.isUsable())
4016 return 0;
4017
4018 // Save the number of iterations.
4019 ExprResult NumIterations = LastIteration;
4020 {
4021 LastIteration = SemaRef.BuildBinOp(
4022 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4023 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4024 if (!LastIteration.isUsable())
4025 return 0;
4026 }
4027
4028 // Calculate the last iteration number beforehand instead of doing this on
4029 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4030 llvm::APSInt Result;
4031 bool IsConstant =
4032 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4033 ExprResult CalcLastIteration;
4034 if (!IsConstant) {
4035 SourceLocation SaveLoc;
4036 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004037 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004038 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004039 ExprResult SaveRef = buildDeclRefExpr(
4040 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004041 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4042 SaveRef.get(), LastIteration.get());
4043 LastIteration = SaveRef;
4044
4045 // Prepare SaveRef + 1.
4046 NumIterations = SemaRef.BuildBinOp(
4047 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4048 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4049 if (!NumIterations.isUsable())
4050 return 0;
4051 }
4052
4053 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4054
Alexander Musmanc6388682014-12-15 07:07:06 +00004055 QualType VType = LastIteration.get()->getType();
4056 // Build variables passed into runtime, nesessary for worksharing directives.
4057 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004058 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4059 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004060 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004061 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4062 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004063 SemaRef.AddInitializerToDecl(
4064 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4065 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4066
4067 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004068 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4069 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004070 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4071 /*DirectInit*/ false,
4072 /*TypeMayContainAuto*/ false);
4073
4074 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4075 // This will be used to implement clause 'lastprivate'.
4076 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004077 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4078 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004079 SemaRef.AddInitializerToDecl(
4080 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4081 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4082
4083 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004084 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4085 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004086 SemaRef.AddInitializerToDecl(
4087 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4088 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4089
4090 // Build expression: UB = min(UB, LastIteration)
4091 // It is nesessary for CodeGen of directives with static scheduling.
4092 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4093 UB.get(), LastIteration.get());
4094 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4095 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4096 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4097 CondOp.get());
4098 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4099 }
4100
4101 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004102 ExprResult IV;
4103 ExprResult Init;
4104 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004105 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4106 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004107 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004108 isOpenMPTaskLoopDirective(DKind) ||
4109 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004110 ? LB.get()
4111 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4112 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4113 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004114 }
4115
Alexander Musmanc6388682014-12-15 07:07:06 +00004116 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004117 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004118 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004119 (isOpenMPWorksharingDirective(DKind) ||
4120 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004121 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4122 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4123 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004124
4125 // Loop increment (IV = IV + 1)
4126 SourceLocation IncLoc;
4127 ExprResult Inc =
4128 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4129 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4130 if (!Inc.isUsable())
4131 return 0;
4132 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004133 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4134 if (!Inc.isUsable())
4135 return 0;
4136
4137 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4138 // Used for directives with static scheduling.
4139 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004140 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4141 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004142 // LB + ST
4143 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4144 if (!NextLB.isUsable())
4145 return 0;
4146 // LB = LB + ST
4147 NextLB =
4148 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4149 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4150 if (!NextLB.isUsable())
4151 return 0;
4152 // UB + ST
4153 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4154 if (!NextUB.isUsable())
4155 return 0;
4156 // UB = UB + ST
4157 NextUB =
4158 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4159 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4160 if (!NextUB.isUsable())
4161 return 0;
4162 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004163
4164 // Build updates and final values of the loop counters.
4165 bool HasErrors = false;
4166 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004167 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004168 Built.Updates.resize(NestedLoopCount);
4169 Built.Finals.resize(NestedLoopCount);
4170 {
4171 ExprResult Div;
4172 // Go from inner nested loop to outer.
4173 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4174 LoopIterationSpace &IS = IterSpaces[Cnt];
4175 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4176 // Build: Iter = (IV / Div) % IS.NumIters
4177 // where Div is product of previous iterations' IS.NumIters.
4178 ExprResult Iter;
4179 if (Div.isUsable()) {
4180 Iter =
4181 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4182 } else {
4183 Iter = IV;
4184 assert((Cnt == (int)NestedLoopCount - 1) &&
4185 "unusable div expected on first iteration only");
4186 }
4187
4188 if (Cnt != 0 && Iter.isUsable())
4189 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4190 IS.NumIterations);
4191 if (!Iter.isUsable()) {
4192 HasErrors = true;
4193 break;
4194 }
4195
Alexey Bataev39f915b82015-05-08 10:41:21 +00004196 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4197 auto *CounterVar = buildDeclRefExpr(
4198 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4199 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4200 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004201 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4202 IS.CounterInit);
4203 if (!Init.isUsable()) {
4204 HasErrors = true;
4205 break;
4206 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004207 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004208 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004209 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4210 if (!Update.isUsable()) {
4211 HasErrors = true;
4212 break;
4213 }
4214
4215 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4216 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004217 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004218 IS.NumIterations, IS.CounterStep, IS.Subtract);
4219 if (!Final.isUsable()) {
4220 HasErrors = true;
4221 break;
4222 }
4223
4224 // Build Div for the next iteration: Div <- Div * IS.NumIters
4225 if (Cnt != 0) {
4226 if (Div.isUnset())
4227 Div = IS.NumIterations;
4228 else
4229 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4230 IS.NumIterations);
4231
4232 // Add parentheses (for debugging purposes only).
4233 if (Div.isUsable())
4234 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4235 if (!Div.isUsable()) {
4236 HasErrors = true;
4237 break;
4238 }
4239 }
4240 if (!Update.isUsable() || !Final.isUsable()) {
4241 HasErrors = true;
4242 break;
4243 }
4244 // Save results
4245 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004246 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004247 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004248 Built.Updates[Cnt] = Update.get();
4249 Built.Finals[Cnt] = Final.get();
4250 }
4251 }
4252
4253 if (HasErrors)
4254 return 0;
4255
4256 // Save results
4257 Built.IterationVarRef = IV.get();
4258 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004259 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004260 Built.CalcLastIteration =
4261 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004262 Built.PreCond = PreCond.get();
4263 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004264 Built.Init = Init.get();
4265 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004266 Built.LB = LB.get();
4267 Built.UB = UB.get();
4268 Built.IL = IL.get();
4269 Built.ST = ST.get();
4270 Built.EUB = EUB.get();
4271 Built.NLB = NextLB.get();
4272 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004273
Alexey Bataevabfc0692014-06-25 06:52:00 +00004274 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004275}
4276
Alexey Bataev10e775f2015-07-30 11:36:16 +00004277static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004278 auto CollapseClauses =
4279 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4280 if (CollapseClauses.begin() != CollapseClauses.end())
4281 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004282 return nullptr;
4283}
4284
Alexey Bataev10e775f2015-07-30 11:36:16 +00004285static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004286 auto OrderedClauses =
4287 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4288 if (OrderedClauses.begin() != OrderedClauses.end())
4289 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004290 return nullptr;
4291}
4292
Alexey Bataev66b15b52015-08-21 11:14:16 +00004293static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4294 const Expr *Safelen) {
4295 llvm::APSInt SimdlenRes, SafelenRes;
4296 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4297 Simdlen->isInstantiationDependent() ||
4298 Simdlen->containsUnexpandedParameterPack())
4299 return false;
4300 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4301 Safelen->isInstantiationDependent() ||
4302 Safelen->containsUnexpandedParameterPack())
4303 return false;
4304 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4305 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4306 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4307 // If both simdlen and safelen clauses are specified, the value of the simdlen
4308 // parameter must be less than or equal to the value of the safelen parameter.
4309 if (SimdlenRes > SafelenRes) {
4310 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4311 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4312 return true;
4313 }
4314 return false;
4315}
4316
Alexey Bataev4acb8592014-07-07 13:01:15 +00004317StmtResult Sema::ActOnOpenMPSimdDirective(
4318 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4319 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004320 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004321 if (!AStmt)
4322 return StmtError();
4323
4324 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004325 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004326 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4327 // define the nested loops number.
4328 unsigned NestedLoopCount = CheckOpenMPLoop(
4329 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4330 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004331 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004332 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004333
Alexander Musmana5f070a2014-10-01 06:03:56 +00004334 assert((CurContext->isDependentContext() || B.builtAll()) &&
4335 "omp simd loop exprs were not built");
4336
Alexander Musman3276a272015-03-21 10:12:56 +00004337 if (!CurContext->isDependentContext()) {
4338 // Finalize the clauses that need pre-built expressions for CodeGen.
4339 for (auto C : Clauses) {
4340 if (auto LC = dyn_cast<OMPLinearClause>(C))
4341 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4342 B.NumIterations, *this, CurScope))
4343 return StmtError();
4344 }
4345 }
4346
Alexey Bataev66b15b52015-08-21 11:14:16 +00004347 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4348 // If both simdlen and safelen clauses are specified, the value of the simdlen
4349 // parameter must be less than or equal to the value of the safelen parameter.
4350 OMPSafelenClause *Safelen = nullptr;
4351 OMPSimdlenClause *Simdlen = nullptr;
4352 for (auto *Clause : Clauses) {
4353 if (Clause->getClauseKind() == OMPC_safelen)
4354 Safelen = cast<OMPSafelenClause>(Clause);
4355 else if (Clause->getClauseKind() == OMPC_simdlen)
4356 Simdlen = cast<OMPSimdlenClause>(Clause);
4357 if (Safelen && Simdlen)
4358 break;
4359 }
4360 if (Simdlen && Safelen &&
4361 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4362 Safelen->getSafelen()))
4363 return StmtError();
4364
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004365 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004366 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4367 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004368}
4369
Alexey Bataev4acb8592014-07-07 13:01:15 +00004370StmtResult Sema::ActOnOpenMPForDirective(
4371 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4372 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004373 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004374 if (!AStmt)
4375 return StmtError();
4376
4377 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004378 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004379 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4380 // define the nested loops number.
4381 unsigned NestedLoopCount = CheckOpenMPLoop(
4382 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4383 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004384 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004385 return StmtError();
4386
Alexander Musmana5f070a2014-10-01 06:03:56 +00004387 assert((CurContext->isDependentContext() || B.builtAll()) &&
4388 "omp for loop exprs were not built");
4389
Alexey Bataev54acd402015-08-04 11:18:19 +00004390 if (!CurContext->isDependentContext()) {
4391 // Finalize the clauses that need pre-built expressions for CodeGen.
4392 for (auto C : Clauses) {
4393 if (auto LC = dyn_cast<OMPLinearClause>(C))
4394 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4395 B.NumIterations, *this, CurScope))
4396 return StmtError();
4397 }
4398 }
4399
Alexey Bataevf29276e2014-06-18 04:14:57 +00004400 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004401 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004402 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004403}
4404
Alexander Musmanf82886e2014-09-18 05:12:34 +00004405StmtResult Sema::ActOnOpenMPForSimdDirective(
4406 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4407 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004408 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004409 if (!AStmt)
4410 return StmtError();
4411
4412 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004413 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004414 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4415 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004416 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004417 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4418 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4419 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004420 if (NestedLoopCount == 0)
4421 return StmtError();
4422
Alexander Musmanc6388682014-12-15 07:07:06 +00004423 assert((CurContext->isDependentContext() || B.builtAll()) &&
4424 "omp for simd loop exprs were not built");
4425
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004426 if (!CurContext->isDependentContext()) {
4427 // Finalize the clauses that need pre-built expressions for CodeGen.
4428 for (auto C : Clauses) {
4429 if (auto LC = dyn_cast<OMPLinearClause>(C))
4430 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4431 B.NumIterations, *this, CurScope))
4432 return StmtError();
4433 }
4434 }
4435
Alexey Bataev66b15b52015-08-21 11:14:16 +00004436 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4437 // If both simdlen and safelen clauses are specified, the value of the simdlen
4438 // parameter must be less than or equal to the value of the safelen parameter.
4439 OMPSafelenClause *Safelen = nullptr;
4440 OMPSimdlenClause *Simdlen = nullptr;
4441 for (auto *Clause : Clauses) {
4442 if (Clause->getClauseKind() == OMPC_safelen)
4443 Safelen = cast<OMPSafelenClause>(Clause);
4444 else if (Clause->getClauseKind() == OMPC_simdlen)
4445 Simdlen = cast<OMPSimdlenClause>(Clause);
4446 if (Safelen && Simdlen)
4447 break;
4448 }
4449 if (Simdlen && Safelen &&
4450 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4451 Safelen->getSafelen()))
4452 return StmtError();
4453
Alexander Musmanf82886e2014-09-18 05:12:34 +00004454 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004455 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4456 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004457}
4458
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004459StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4460 Stmt *AStmt,
4461 SourceLocation StartLoc,
4462 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004463 if (!AStmt)
4464 return StmtError();
4465
4466 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004467 auto BaseStmt = AStmt;
4468 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4469 BaseStmt = CS->getCapturedStmt();
4470 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4471 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004472 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004473 return StmtError();
4474 // All associated statements must be '#pragma omp section' except for
4475 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004476 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004477 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4478 if (SectionStmt)
4479 Diag(SectionStmt->getLocStart(),
4480 diag::err_omp_sections_substmt_not_section);
4481 return StmtError();
4482 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004483 cast<OMPSectionDirective>(SectionStmt)
4484 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004485 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004486 } else {
4487 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4488 return StmtError();
4489 }
4490
4491 getCurFunction()->setHasBranchProtectedScope();
4492
Alexey Bataev25e5b442015-09-15 12:52:43 +00004493 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4494 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004495}
4496
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004497StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4498 SourceLocation StartLoc,
4499 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004500 if (!AStmt)
4501 return StmtError();
4502
4503 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004504
4505 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004506 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004507
Alexey Bataev25e5b442015-09-15 12:52:43 +00004508 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4509 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004510}
4511
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004512StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4513 Stmt *AStmt,
4514 SourceLocation StartLoc,
4515 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004516 if (!AStmt)
4517 return StmtError();
4518
4519 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004520
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004521 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004522
Alexey Bataev3255bf32015-01-19 05:20:46 +00004523 // OpenMP [2.7.3, single Construct, Restrictions]
4524 // The copyprivate clause must not be used with the nowait clause.
4525 OMPClause *Nowait = nullptr;
4526 OMPClause *Copyprivate = nullptr;
4527 for (auto *Clause : Clauses) {
4528 if (Clause->getClauseKind() == OMPC_nowait)
4529 Nowait = Clause;
4530 else if (Clause->getClauseKind() == OMPC_copyprivate)
4531 Copyprivate = Clause;
4532 if (Copyprivate && Nowait) {
4533 Diag(Copyprivate->getLocStart(),
4534 diag::err_omp_single_copyprivate_with_nowait);
4535 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4536 return StmtError();
4537 }
4538 }
4539
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004540 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4541}
4542
Alexander Musman80c22892014-07-17 08:54:58 +00004543StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4544 SourceLocation StartLoc,
4545 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004546 if (!AStmt)
4547 return StmtError();
4548
4549 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004550
4551 getCurFunction()->setHasBranchProtectedScope();
4552
4553 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4554}
4555
Alexey Bataev28c75412015-12-15 08:19:24 +00004556StmtResult Sema::ActOnOpenMPCriticalDirective(
4557 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4558 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004559 if (!AStmt)
4560 return StmtError();
4561
4562 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004563
Alexey Bataev28c75412015-12-15 08:19:24 +00004564 bool ErrorFound = false;
4565 llvm::APSInt Hint;
4566 SourceLocation HintLoc;
4567 bool DependentHint = false;
4568 for (auto *C : Clauses) {
4569 if (C->getClauseKind() == OMPC_hint) {
4570 if (!DirName.getName()) {
4571 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4572 ErrorFound = true;
4573 }
4574 Expr *E = cast<OMPHintClause>(C)->getHint();
4575 if (E->isTypeDependent() || E->isValueDependent() ||
4576 E->isInstantiationDependent())
4577 DependentHint = true;
4578 else {
4579 Hint = E->EvaluateKnownConstInt(Context);
4580 HintLoc = C->getLocStart();
4581 }
4582 }
4583 }
4584 if (ErrorFound)
4585 return StmtError();
4586 auto Pair = DSAStack->getCriticalWithHint(DirName);
4587 if (Pair.first && DirName.getName() && !DependentHint) {
4588 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4589 Diag(StartLoc, diag::err_omp_critical_with_hint);
4590 if (HintLoc.isValid()) {
4591 Diag(HintLoc, diag::note_omp_critical_hint_here)
4592 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4593 } else
4594 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4595 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4596 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4597 << 1
4598 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4599 /*Radix=*/10, /*Signed=*/false);
4600 } else
4601 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4602 }
4603 }
4604
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004605 getCurFunction()->setHasBranchProtectedScope();
4606
Alexey Bataev28c75412015-12-15 08:19:24 +00004607 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4608 Clauses, AStmt);
4609 if (!Pair.first && DirName.getName() && !DependentHint)
4610 DSAStack->addCriticalWithHint(Dir, Hint);
4611 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004612}
4613
Alexey Bataev4acb8592014-07-07 13:01:15 +00004614StmtResult Sema::ActOnOpenMPParallelForDirective(
4615 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4616 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004617 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004618 if (!AStmt)
4619 return StmtError();
4620
Alexey Bataev4acb8592014-07-07 13:01:15 +00004621 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4622 // 1.2.2 OpenMP Language Terminology
4623 // Structured block - An executable statement with a single entry at the
4624 // top and a single exit at the bottom.
4625 // The point of exit cannot be a branch out of the structured block.
4626 // longjmp() and throw() must not violate the entry/exit criteria.
4627 CS->getCapturedDecl()->setNothrow();
4628
Alexander Musmanc6388682014-12-15 07:07:06 +00004629 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004630 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4631 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004632 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004633 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4634 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4635 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004636 if (NestedLoopCount == 0)
4637 return StmtError();
4638
Alexander Musmana5f070a2014-10-01 06:03:56 +00004639 assert((CurContext->isDependentContext() || B.builtAll()) &&
4640 "omp parallel for loop exprs were not built");
4641
Alexey Bataev54acd402015-08-04 11:18:19 +00004642 if (!CurContext->isDependentContext()) {
4643 // Finalize the clauses that need pre-built expressions for CodeGen.
4644 for (auto C : Clauses) {
4645 if (auto LC = dyn_cast<OMPLinearClause>(C))
4646 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4647 B.NumIterations, *this, CurScope))
4648 return StmtError();
4649 }
4650 }
4651
Alexey Bataev4acb8592014-07-07 13:01:15 +00004652 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004653 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004654 NestedLoopCount, Clauses, AStmt, B,
4655 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004656}
4657
Alexander Musmane4e893b2014-09-23 09:33:00 +00004658StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4659 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4660 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004661 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004662 if (!AStmt)
4663 return StmtError();
4664
Alexander Musmane4e893b2014-09-23 09:33:00 +00004665 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4666 // 1.2.2 OpenMP Language Terminology
4667 // Structured block - An executable statement with a single entry at the
4668 // top and a single exit at the bottom.
4669 // The point of exit cannot be a branch out of the structured block.
4670 // longjmp() and throw() must not violate the entry/exit criteria.
4671 CS->getCapturedDecl()->setNothrow();
4672
Alexander Musmanc6388682014-12-15 07:07:06 +00004673 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004674 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4675 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004676 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004677 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4678 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4679 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004680 if (NestedLoopCount == 0)
4681 return StmtError();
4682
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004683 if (!CurContext->isDependentContext()) {
4684 // Finalize the clauses that need pre-built expressions for CodeGen.
4685 for (auto C : Clauses) {
4686 if (auto LC = dyn_cast<OMPLinearClause>(C))
4687 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4688 B.NumIterations, *this, CurScope))
4689 return StmtError();
4690 }
4691 }
4692
Alexey Bataev66b15b52015-08-21 11:14:16 +00004693 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4694 // If both simdlen and safelen clauses are specified, the value of the simdlen
4695 // parameter must be less than or equal to the value of the safelen parameter.
4696 OMPSafelenClause *Safelen = nullptr;
4697 OMPSimdlenClause *Simdlen = nullptr;
4698 for (auto *Clause : Clauses) {
4699 if (Clause->getClauseKind() == OMPC_safelen)
4700 Safelen = cast<OMPSafelenClause>(Clause);
4701 else if (Clause->getClauseKind() == OMPC_simdlen)
4702 Simdlen = cast<OMPSimdlenClause>(Clause);
4703 if (Safelen && Simdlen)
4704 break;
4705 }
4706 if (Simdlen && Safelen &&
4707 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4708 Safelen->getSafelen()))
4709 return StmtError();
4710
Alexander Musmane4e893b2014-09-23 09:33:00 +00004711 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004712 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004713 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004714}
4715
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004716StmtResult
4717Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4718 Stmt *AStmt, SourceLocation StartLoc,
4719 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004720 if (!AStmt)
4721 return StmtError();
4722
4723 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004724 auto BaseStmt = AStmt;
4725 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4726 BaseStmt = CS->getCapturedStmt();
4727 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4728 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004729 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004730 return StmtError();
4731 // All associated statements must be '#pragma omp section' except for
4732 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004733 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004734 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4735 if (SectionStmt)
4736 Diag(SectionStmt->getLocStart(),
4737 diag::err_omp_parallel_sections_substmt_not_section);
4738 return StmtError();
4739 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004740 cast<OMPSectionDirective>(SectionStmt)
4741 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004742 }
4743 } else {
4744 Diag(AStmt->getLocStart(),
4745 diag::err_omp_parallel_sections_not_compound_stmt);
4746 return StmtError();
4747 }
4748
4749 getCurFunction()->setHasBranchProtectedScope();
4750
Alexey Bataev25e5b442015-09-15 12:52:43 +00004751 return OMPParallelSectionsDirective::Create(
4752 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004753}
4754
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004755StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4756 Stmt *AStmt, SourceLocation StartLoc,
4757 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004758 if (!AStmt)
4759 return StmtError();
4760
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004761 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4762 // 1.2.2 OpenMP Language Terminology
4763 // Structured block - An executable statement with a single entry at the
4764 // top and a single exit at the bottom.
4765 // The point of exit cannot be a branch out of the structured block.
4766 // longjmp() and throw() must not violate the entry/exit criteria.
4767 CS->getCapturedDecl()->setNothrow();
4768
4769 getCurFunction()->setHasBranchProtectedScope();
4770
Alexey Bataev25e5b442015-09-15 12:52:43 +00004771 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4772 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004773}
4774
Alexey Bataev68446b72014-07-18 07:47:19 +00004775StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4776 SourceLocation EndLoc) {
4777 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4778}
4779
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004780StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4781 SourceLocation EndLoc) {
4782 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4783}
4784
Alexey Bataev2df347a2014-07-18 10:17:07 +00004785StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4786 SourceLocation EndLoc) {
4787 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4788}
4789
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004790StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4791 SourceLocation StartLoc,
4792 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004793 if (!AStmt)
4794 return StmtError();
4795
4796 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004797
4798 getCurFunction()->setHasBranchProtectedScope();
4799
4800 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4801}
4802
Alexey Bataev6125da92014-07-21 11:26:11 +00004803StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4804 SourceLocation StartLoc,
4805 SourceLocation EndLoc) {
4806 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4807 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4808}
4809
Alexey Bataev346265e2015-09-25 10:37:12 +00004810StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4811 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004812 SourceLocation StartLoc,
4813 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004814 OMPClause *DependFound = nullptr;
4815 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004816 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004817 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004818 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004819 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004820 for (auto *C : Clauses) {
4821 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4822 DependFound = C;
4823 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4824 if (DependSourceClause) {
4825 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4826 << getOpenMPDirectiveName(OMPD_ordered)
4827 << getOpenMPClauseName(OMPC_depend) << 2;
4828 ErrorFound = true;
4829 } else
4830 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004831 if (DependSinkClause) {
4832 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4833 << 0;
4834 ErrorFound = true;
4835 }
4836 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4837 if (DependSourceClause) {
4838 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4839 << 1;
4840 ErrorFound = true;
4841 }
4842 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004843 }
4844 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004845 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004846 else if (C->getClauseKind() == OMPC_simd)
4847 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004848 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004849 if (!ErrorFound && !SC &&
4850 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004851 // OpenMP [2.8.1,simd Construct, Restrictions]
4852 // An ordered construct with the simd clause is the only OpenMP construct
4853 // that can appear in the simd region.
4854 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004855 ErrorFound = true;
4856 } else if (DependFound && (TC || SC)) {
4857 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4858 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4859 ErrorFound = true;
4860 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4861 Diag(DependFound->getLocStart(),
4862 diag::err_omp_ordered_directive_without_param);
4863 ErrorFound = true;
4864 } else if (TC || Clauses.empty()) {
4865 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4866 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4867 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4868 << (TC != nullptr);
4869 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4870 ErrorFound = true;
4871 }
4872 }
4873 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004874 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004875
4876 if (AStmt) {
4877 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4878
4879 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004880 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004881
4882 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004883}
4884
Alexey Bataev1d160b12015-03-13 12:27:31 +00004885namespace {
4886/// \brief Helper class for checking expression in 'omp atomic [update]'
4887/// construct.
4888class OpenMPAtomicUpdateChecker {
4889 /// \brief Error results for atomic update expressions.
4890 enum ExprAnalysisErrorCode {
4891 /// \brief A statement is not an expression statement.
4892 NotAnExpression,
4893 /// \brief Expression is not builtin binary or unary operation.
4894 NotABinaryOrUnaryExpression,
4895 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4896 NotAnUnaryIncDecExpression,
4897 /// \brief An expression is not of scalar type.
4898 NotAScalarType,
4899 /// \brief A binary operation is not an assignment operation.
4900 NotAnAssignmentOp,
4901 /// \brief RHS part of the binary operation is not a binary expression.
4902 NotABinaryExpression,
4903 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4904 /// expression.
4905 NotABinaryOperator,
4906 /// \brief RHS binary operation does not have reference to the updated LHS
4907 /// part.
4908 NotAnUpdateExpression,
4909 /// \brief No errors is found.
4910 NoError
4911 };
4912 /// \brief Reference to Sema.
4913 Sema &SemaRef;
4914 /// \brief A location for note diagnostics (when error is found).
4915 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004916 /// \brief 'x' lvalue part of the source atomic expression.
4917 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004918 /// \brief 'expr' rvalue part of the source atomic expression.
4919 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004920 /// \brief Helper expression of the form
4921 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4922 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4923 Expr *UpdateExpr;
4924 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4925 /// important for non-associative operations.
4926 bool IsXLHSInRHSPart;
4927 BinaryOperatorKind Op;
4928 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004929 /// \brief true if the source expression is a postfix unary operation, false
4930 /// if it is a prefix unary operation.
4931 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004932
4933public:
4934 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004935 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004936 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004937 /// \brief Check specified statement that it is suitable for 'atomic update'
4938 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004939 /// expression. If DiagId and NoteId == 0, then only check is performed
4940 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004941 /// \param DiagId Diagnostic which should be emitted if error is found.
4942 /// \param NoteId Diagnostic note for the main error message.
4943 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004944 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004945 /// \brief Return the 'x' lvalue part of the source atomic expression.
4946 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004947 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4948 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004949 /// \brief Return the update expression used in calculation of the updated
4950 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4951 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4952 Expr *getUpdateExpr() const { return UpdateExpr; }
4953 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4954 /// false otherwise.
4955 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4956
Alexey Bataevb78ca832015-04-01 03:33:17 +00004957 /// \brief true if the source expression is a postfix unary operation, false
4958 /// if it is a prefix unary operation.
4959 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4960
Alexey Bataev1d160b12015-03-13 12:27:31 +00004961private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004962 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4963 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004964};
4965} // namespace
4966
4967bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4968 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4969 ExprAnalysisErrorCode ErrorFound = NoError;
4970 SourceLocation ErrorLoc, NoteLoc;
4971 SourceRange ErrorRange, NoteRange;
4972 // Allowed constructs are:
4973 // x = x binop expr;
4974 // x = expr binop x;
4975 if (AtomicBinOp->getOpcode() == BO_Assign) {
4976 X = AtomicBinOp->getLHS();
4977 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4978 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4979 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4980 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4981 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004982 Op = AtomicInnerBinOp->getOpcode();
4983 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004984 auto *LHS = AtomicInnerBinOp->getLHS();
4985 auto *RHS = AtomicInnerBinOp->getRHS();
4986 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4987 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4988 /*Canonical=*/true);
4989 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4990 /*Canonical=*/true);
4991 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4992 /*Canonical=*/true);
4993 if (XId == LHSId) {
4994 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004995 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004996 } else if (XId == RHSId) {
4997 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004998 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004999 } else {
5000 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5001 ErrorRange = AtomicInnerBinOp->getSourceRange();
5002 NoteLoc = X->getExprLoc();
5003 NoteRange = X->getSourceRange();
5004 ErrorFound = NotAnUpdateExpression;
5005 }
5006 } else {
5007 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5008 ErrorRange = AtomicInnerBinOp->getSourceRange();
5009 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5010 NoteRange = SourceRange(NoteLoc, NoteLoc);
5011 ErrorFound = NotABinaryOperator;
5012 }
5013 } else {
5014 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5015 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5016 ErrorFound = NotABinaryExpression;
5017 }
5018 } else {
5019 ErrorLoc = AtomicBinOp->getExprLoc();
5020 ErrorRange = AtomicBinOp->getSourceRange();
5021 NoteLoc = AtomicBinOp->getOperatorLoc();
5022 NoteRange = SourceRange(NoteLoc, NoteLoc);
5023 ErrorFound = NotAnAssignmentOp;
5024 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005025 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005026 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5027 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5028 return true;
5029 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005030 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005031 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005032}
5033
5034bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5035 unsigned NoteId) {
5036 ExprAnalysisErrorCode ErrorFound = NoError;
5037 SourceLocation ErrorLoc, NoteLoc;
5038 SourceRange ErrorRange, NoteRange;
5039 // Allowed constructs are:
5040 // x++;
5041 // x--;
5042 // ++x;
5043 // --x;
5044 // x binop= expr;
5045 // x = x binop expr;
5046 // x = expr binop x;
5047 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5048 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5049 if (AtomicBody->getType()->isScalarType() ||
5050 AtomicBody->isInstantiationDependent()) {
5051 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5052 AtomicBody->IgnoreParenImpCasts())) {
5053 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005054 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005055 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005056 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005057 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005058 X = AtomicCompAssignOp->getLHS();
5059 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005060 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5061 AtomicBody->IgnoreParenImpCasts())) {
5062 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005063 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5064 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005065 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005066 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5067 // Check for Unary Operation
5068 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005069 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005070 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5071 OpLoc = AtomicUnaryOp->getOperatorLoc();
5072 X = AtomicUnaryOp->getSubExpr();
5073 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5074 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005075 } else {
5076 ErrorFound = NotAnUnaryIncDecExpression;
5077 ErrorLoc = AtomicUnaryOp->getExprLoc();
5078 ErrorRange = AtomicUnaryOp->getSourceRange();
5079 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5080 NoteRange = SourceRange(NoteLoc, NoteLoc);
5081 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005082 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005083 ErrorFound = NotABinaryOrUnaryExpression;
5084 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5085 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5086 }
5087 } else {
5088 ErrorFound = NotAScalarType;
5089 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5090 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5091 }
5092 } else {
5093 ErrorFound = NotAnExpression;
5094 NoteLoc = ErrorLoc = S->getLocStart();
5095 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5096 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005097 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005098 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5099 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5100 return true;
5101 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005102 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005103 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005104 // Build an update expression of form 'OpaqueValueExpr(x) binop
5105 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5106 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5107 auto *OVEX = new (SemaRef.getASTContext())
5108 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5109 auto *OVEExpr = new (SemaRef.getASTContext())
5110 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5111 auto Update =
5112 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5113 IsXLHSInRHSPart ? OVEExpr : OVEX);
5114 if (Update.isInvalid())
5115 return true;
5116 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5117 Sema::AA_Casting);
5118 if (Update.isInvalid())
5119 return true;
5120 UpdateExpr = Update.get();
5121 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005122 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005123}
5124
Alexey Bataev0162e452014-07-22 10:10:35 +00005125StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5126 Stmt *AStmt,
5127 SourceLocation StartLoc,
5128 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005129 if (!AStmt)
5130 return StmtError();
5131
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005132 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005133 // 1.2.2 OpenMP Language Terminology
5134 // Structured block - An executable statement with a single entry at the
5135 // top and a single exit at the bottom.
5136 // The point of exit cannot be a branch out of the structured block.
5137 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005138 OpenMPClauseKind AtomicKind = OMPC_unknown;
5139 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005140 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005141 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005142 C->getClauseKind() == OMPC_update ||
5143 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005144 if (AtomicKind != OMPC_unknown) {
5145 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5146 << SourceRange(C->getLocStart(), C->getLocEnd());
5147 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5148 << getOpenMPClauseName(AtomicKind);
5149 } else {
5150 AtomicKind = C->getClauseKind();
5151 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005152 }
5153 }
5154 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005155
Alexey Bataev459dec02014-07-24 06:46:57 +00005156 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005157 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5158 Body = EWC->getSubExpr();
5159
Alexey Bataev62cec442014-11-18 10:14:22 +00005160 Expr *X = nullptr;
5161 Expr *V = nullptr;
5162 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005163 Expr *UE = nullptr;
5164 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005165 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005166 // OpenMP [2.12.6, atomic Construct]
5167 // In the next expressions:
5168 // * x and v (as applicable) are both l-value expressions with scalar type.
5169 // * During the execution of an atomic region, multiple syntactic
5170 // occurrences of x must designate the same storage location.
5171 // * Neither of v and expr (as applicable) may access the storage location
5172 // designated by x.
5173 // * Neither of x and expr (as applicable) may access the storage location
5174 // designated by v.
5175 // * expr is an expression with scalar type.
5176 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5177 // * binop, binop=, ++, and -- are not overloaded operators.
5178 // * The expression x binop expr must be numerically equivalent to x binop
5179 // (expr). This requirement is satisfied if the operators in expr have
5180 // precedence greater than binop, or by using parentheses around expr or
5181 // subexpressions of expr.
5182 // * The expression expr binop x must be numerically equivalent to (expr)
5183 // binop x. This requirement is satisfied if the operators in expr have
5184 // precedence equal to or greater than binop, or by using parentheses around
5185 // expr or subexpressions of expr.
5186 // * For forms that allow multiple occurrences of x, the number of times
5187 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005188 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005189 enum {
5190 NotAnExpression,
5191 NotAnAssignmentOp,
5192 NotAScalarType,
5193 NotAnLValue,
5194 NoError
5195 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005196 SourceLocation ErrorLoc, NoteLoc;
5197 SourceRange ErrorRange, NoteRange;
5198 // If clause is read:
5199 // v = x;
5200 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5201 auto AtomicBinOp =
5202 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5203 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5204 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5205 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5206 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5207 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5208 if (!X->isLValue() || !V->isLValue()) {
5209 auto NotLValueExpr = X->isLValue() ? V : X;
5210 ErrorFound = NotAnLValue;
5211 ErrorLoc = AtomicBinOp->getExprLoc();
5212 ErrorRange = AtomicBinOp->getSourceRange();
5213 NoteLoc = NotLValueExpr->getExprLoc();
5214 NoteRange = NotLValueExpr->getSourceRange();
5215 }
5216 } else if (!X->isInstantiationDependent() ||
5217 !V->isInstantiationDependent()) {
5218 auto NotScalarExpr =
5219 (X->isInstantiationDependent() || X->getType()->isScalarType())
5220 ? V
5221 : X;
5222 ErrorFound = NotAScalarType;
5223 ErrorLoc = AtomicBinOp->getExprLoc();
5224 ErrorRange = AtomicBinOp->getSourceRange();
5225 NoteLoc = NotScalarExpr->getExprLoc();
5226 NoteRange = NotScalarExpr->getSourceRange();
5227 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005228 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005229 ErrorFound = NotAnAssignmentOp;
5230 ErrorLoc = AtomicBody->getExprLoc();
5231 ErrorRange = AtomicBody->getSourceRange();
5232 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5233 : AtomicBody->getExprLoc();
5234 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5235 : AtomicBody->getSourceRange();
5236 }
5237 } else {
5238 ErrorFound = NotAnExpression;
5239 NoteLoc = ErrorLoc = Body->getLocStart();
5240 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005241 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005242 if (ErrorFound != NoError) {
5243 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5244 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005245 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5246 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005247 return StmtError();
5248 } else if (CurContext->isDependentContext())
5249 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005250 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005251 enum {
5252 NotAnExpression,
5253 NotAnAssignmentOp,
5254 NotAScalarType,
5255 NotAnLValue,
5256 NoError
5257 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005258 SourceLocation ErrorLoc, NoteLoc;
5259 SourceRange ErrorRange, NoteRange;
5260 // If clause is write:
5261 // x = expr;
5262 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5263 auto AtomicBinOp =
5264 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5265 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005266 X = AtomicBinOp->getLHS();
5267 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005268 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5269 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5270 if (!X->isLValue()) {
5271 ErrorFound = NotAnLValue;
5272 ErrorLoc = AtomicBinOp->getExprLoc();
5273 ErrorRange = AtomicBinOp->getSourceRange();
5274 NoteLoc = X->getExprLoc();
5275 NoteRange = X->getSourceRange();
5276 }
5277 } else if (!X->isInstantiationDependent() ||
5278 !E->isInstantiationDependent()) {
5279 auto NotScalarExpr =
5280 (X->isInstantiationDependent() || X->getType()->isScalarType())
5281 ? E
5282 : X;
5283 ErrorFound = NotAScalarType;
5284 ErrorLoc = AtomicBinOp->getExprLoc();
5285 ErrorRange = AtomicBinOp->getSourceRange();
5286 NoteLoc = NotScalarExpr->getExprLoc();
5287 NoteRange = NotScalarExpr->getSourceRange();
5288 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005289 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005290 ErrorFound = NotAnAssignmentOp;
5291 ErrorLoc = AtomicBody->getExprLoc();
5292 ErrorRange = AtomicBody->getSourceRange();
5293 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5294 : AtomicBody->getExprLoc();
5295 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5296 : AtomicBody->getSourceRange();
5297 }
5298 } else {
5299 ErrorFound = NotAnExpression;
5300 NoteLoc = ErrorLoc = Body->getLocStart();
5301 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005302 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005303 if (ErrorFound != NoError) {
5304 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5305 << ErrorRange;
5306 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5307 << NoteRange;
5308 return StmtError();
5309 } else if (CurContext->isDependentContext())
5310 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005311 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005312 // If clause is update:
5313 // x++;
5314 // x--;
5315 // ++x;
5316 // --x;
5317 // x binop= expr;
5318 // x = x binop expr;
5319 // x = expr binop x;
5320 OpenMPAtomicUpdateChecker Checker(*this);
5321 if (Checker.checkStatement(
5322 Body, (AtomicKind == OMPC_update)
5323 ? diag::err_omp_atomic_update_not_expression_statement
5324 : diag::err_omp_atomic_not_expression_statement,
5325 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005326 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005327 if (!CurContext->isDependentContext()) {
5328 E = Checker.getExpr();
5329 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005330 UE = Checker.getUpdateExpr();
5331 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005332 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005333 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005334 enum {
5335 NotAnAssignmentOp,
5336 NotACompoundStatement,
5337 NotTwoSubstatements,
5338 NotASpecificExpression,
5339 NoError
5340 } ErrorFound = NoError;
5341 SourceLocation ErrorLoc, NoteLoc;
5342 SourceRange ErrorRange, NoteRange;
5343 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5344 // If clause is a capture:
5345 // v = x++;
5346 // v = x--;
5347 // v = ++x;
5348 // v = --x;
5349 // v = x binop= expr;
5350 // v = x = x binop expr;
5351 // v = x = expr binop x;
5352 auto *AtomicBinOp =
5353 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5354 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5355 V = AtomicBinOp->getLHS();
5356 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5357 OpenMPAtomicUpdateChecker Checker(*this);
5358 if (Checker.checkStatement(
5359 Body, diag::err_omp_atomic_capture_not_expression_statement,
5360 diag::note_omp_atomic_update))
5361 return StmtError();
5362 E = Checker.getExpr();
5363 X = Checker.getX();
5364 UE = Checker.getUpdateExpr();
5365 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5366 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005367 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005368 ErrorLoc = AtomicBody->getExprLoc();
5369 ErrorRange = AtomicBody->getSourceRange();
5370 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5371 : AtomicBody->getExprLoc();
5372 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5373 : AtomicBody->getSourceRange();
5374 ErrorFound = NotAnAssignmentOp;
5375 }
5376 if (ErrorFound != NoError) {
5377 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5378 << ErrorRange;
5379 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5380 return StmtError();
5381 } else if (CurContext->isDependentContext()) {
5382 UE = V = E = X = nullptr;
5383 }
5384 } else {
5385 // If clause is a capture:
5386 // { v = x; x = expr; }
5387 // { v = x; x++; }
5388 // { v = x; x--; }
5389 // { v = x; ++x; }
5390 // { v = x; --x; }
5391 // { v = x; x binop= expr; }
5392 // { v = x; x = x binop expr; }
5393 // { v = x; x = expr binop x; }
5394 // { x++; v = x; }
5395 // { x--; v = x; }
5396 // { ++x; v = x; }
5397 // { --x; v = x; }
5398 // { x binop= expr; v = x; }
5399 // { x = x binop expr; v = x; }
5400 // { x = expr binop x; v = x; }
5401 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5402 // Check that this is { expr1; expr2; }
5403 if (CS->size() == 2) {
5404 auto *First = CS->body_front();
5405 auto *Second = CS->body_back();
5406 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5407 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5408 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5409 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5410 // Need to find what subexpression is 'v' and what is 'x'.
5411 OpenMPAtomicUpdateChecker Checker(*this);
5412 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5413 BinaryOperator *BinOp = nullptr;
5414 if (IsUpdateExprFound) {
5415 BinOp = dyn_cast<BinaryOperator>(First);
5416 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5417 }
5418 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5419 // { v = x; x++; }
5420 // { v = x; x--; }
5421 // { v = x; ++x; }
5422 // { v = x; --x; }
5423 // { v = x; x binop= expr; }
5424 // { v = x; x = x binop expr; }
5425 // { v = x; x = expr binop x; }
5426 // Check that the first expression has form v = x.
5427 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5428 llvm::FoldingSetNodeID XId, PossibleXId;
5429 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5430 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5431 IsUpdateExprFound = XId == PossibleXId;
5432 if (IsUpdateExprFound) {
5433 V = BinOp->getLHS();
5434 X = Checker.getX();
5435 E = Checker.getExpr();
5436 UE = Checker.getUpdateExpr();
5437 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005438 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005439 }
5440 }
5441 if (!IsUpdateExprFound) {
5442 IsUpdateExprFound = !Checker.checkStatement(First);
5443 BinOp = nullptr;
5444 if (IsUpdateExprFound) {
5445 BinOp = dyn_cast<BinaryOperator>(Second);
5446 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5447 }
5448 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5449 // { x++; v = x; }
5450 // { x--; v = x; }
5451 // { ++x; v = x; }
5452 // { --x; v = x; }
5453 // { x binop= expr; v = x; }
5454 // { x = x binop expr; v = x; }
5455 // { x = expr binop x; v = x; }
5456 // Check that the second expression has form v = x.
5457 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5458 llvm::FoldingSetNodeID XId, PossibleXId;
5459 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5460 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5461 IsUpdateExprFound = XId == PossibleXId;
5462 if (IsUpdateExprFound) {
5463 V = BinOp->getLHS();
5464 X = Checker.getX();
5465 E = Checker.getExpr();
5466 UE = Checker.getUpdateExpr();
5467 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005468 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005469 }
5470 }
5471 }
5472 if (!IsUpdateExprFound) {
5473 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005474 auto *FirstExpr = dyn_cast<Expr>(First);
5475 auto *SecondExpr = dyn_cast<Expr>(Second);
5476 if (!FirstExpr || !SecondExpr ||
5477 !(FirstExpr->isInstantiationDependent() ||
5478 SecondExpr->isInstantiationDependent())) {
5479 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5480 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005481 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005482 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5483 : First->getLocStart();
5484 NoteRange = ErrorRange = FirstBinOp
5485 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005486 : SourceRange(ErrorLoc, ErrorLoc);
5487 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005488 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5489 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5490 ErrorFound = NotAnAssignmentOp;
5491 NoteLoc = ErrorLoc = SecondBinOp
5492 ? SecondBinOp->getOperatorLoc()
5493 : Second->getLocStart();
5494 NoteRange = ErrorRange =
5495 SecondBinOp ? SecondBinOp->getSourceRange()
5496 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005497 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005498 auto *PossibleXRHSInFirst =
5499 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5500 auto *PossibleXLHSInSecond =
5501 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5502 llvm::FoldingSetNodeID X1Id, X2Id;
5503 PossibleXRHSInFirst->Profile(X1Id, Context,
5504 /*Canonical=*/true);
5505 PossibleXLHSInSecond->Profile(X2Id, Context,
5506 /*Canonical=*/true);
5507 IsUpdateExprFound = X1Id == X2Id;
5508 if (IsUpdateExprFound) {
5509 V = FirstBinOp->getLHS();
5510 X = SecondBinOp->getLHS();
5511 E = SecondBinOp->getRHS();
5512 UE = nullptr;
5513 IsXLHSInRHSPart = false;
5514 IsPostfixUpdate = true;
5515 } else {
5516 ErrorFound = NotASpecificExpression;
5517 ErrorLoc = FirstBinOp->getExprLoc();
5518 ErrorRange = FirstBinOp->getSourceRange();
5519 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5520 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5521 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005522 }
5523 }
5524 }
5525 }
5526 } else {
5527 NoteLoc = ErrorLoc = Body->getLocStart();
5528 NoteRange = ErrorRange =
5529 SourceRange(Body->getLocStart(), Body->getLocStart());
5530 ErrorFound = NotTwoSubstatements;
5531 }
5532 } else {
5533 NoteLoc = ErrorLoc = Body->getLocStart();
5534 NoteRange = ErrorRange =
5535 SourceRange(Body->getLocStart(), Body->getLocStart());
5536 ErrorFound = NotACompoundStatement;
5537 }
5538 if (ErrorFound != NoError) {
5539 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5540 << ErrorRange;
5541 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5542 return StmtError();
5543 } else if (CurContext->isDependentContext()) {
5544 UE = V = E = X = nullptr;
5545 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005546 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005547 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005548
5549 getCurFunction()->setHasBranchProtectedScope();
5550
Alexey Bataev62cec442014-11-18 10:14:22 +00005551 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005552 X, V, E, UE, IsXLHSInRHSPart,
5553 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005554}
5555
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005556StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5557 Stmt *AStmt,
5558 SourceLocation StartLoc,
5559 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005560 if (!AStmt)
5561 return StmtError();
5562
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005563 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5564 // 1.2.2 OpenMP Language Terminology
5565 // Structured block - An executable statement with a single entry at the
5566 // top and a single exit at the bottom.
5567 // The point of exit cannot be a branch out of the structured block.
5568 // longjmp() and throw() must not violate the entry/exit criteria.
5569 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005570
Alexey Bataev13314bf2014-10-09 04:18:56 +00005571 // OpenMP [2.16, Nesting of Regions]
5572 // If specified, a teams construct must be contained within a target
5573 // construct. That target construct must contain no statements or directives
5574 // outside of the teams construct.
5575 if (DSAStack->hasInnerTeamsRegion()) {
5576 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5577 bool OMPTeamsFound = true;
5578 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5579 auto I = CS->body_begin();
5580 while (I != CS->body_end()) {
5581 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5582 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5583 OMPTeamsFound = false;
5584 break;
5585 }
5586 ++I;
5587 }
5588 assert(I != CS->body_end() && "Not found statement");
5589 S = *I;
5590 }
5591 if (!OMPTeamsFound) {
5592 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5593 Diag(DSAStack->getInnerTeamsRegionLoc(),
5594 diag::note_omp_nested_teams_construct_here);
5595 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5596 << isa<OMPExecutableDirective>(S);
5597 return StmtError();
5598 }
5599 }
5600
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005601 getCurFunction()->setHasBranchProtectedScope();
5602
5603 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5604}
5605
Samuel Antaodf67fc42016-01-19 19:15:56 +00005606/// \brief Check for existence of a map clause in the list of clauses.
5607static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5608 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5609 I != E; ++I) {
5610 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5611 return true;
5612 }
5613 }
5614
5615 return false;
5616}
5617
Michael Wong65f367f2015-07-21 13:44:28 +00005618StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5619 Stmt *AStmt,
5620 SourceLocation StartLoc,
5621 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005622 if (!AStmt)
5623 return StmtError();
5624
5625 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5626
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005627 // OpenMP [2.10.1, Restrictions, p. 97]
5628 // At least one map clause must appear on the directive.
5629 if (!HasMapClause(Clauses)) {
5630 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5631 getOpenMPDirectiveName(OMPD_target_data);
5632 return StmtError();
5633 }
5634
Michael Wong65f367f2015-07-21 13:44:28 +00005635 getCurFunction()->setHasBranchProtectedScope();
5636
5637 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5638 AStmt);
5639}
5640
Samuel Antaodf67fc42016-01-19 19:15:56 +00005641StmtResult
5642Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5643 SourceLocation StartLoc,
5644 SourceLocation EndLoc) {
5645 // OpenMP [2.10.2, Restrictions, p. 99]
5646 // At least one map clause must appear on the directive.
5647 if (!HasMapClause(Clauses)) {
5648 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5649 << getOpenMPDirectiveName(OMPD_target_enter_data);
5650 return StmtError();
5651 }
5652
5653 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5654 Clauses);
5655}
5656
Samuel Antao72590762016-01-19 20:04:50 +00005657StmtResult
5658Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5659 SourceLocation StartLoc,
5660 SourceLocation EndLoc) {
5661 // OpenMP [2.10.3, Restrictions, p. 102]
5662 // At least one map clause must appear on the directive.
5663 if (!HasMapClause(Clauses)) {
5664 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5665 << getOpenMPDirectiveName(OMPD_target_exit_data);
5666 return StmtError();
5667 }
5668
5669 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5670}
5671
Alexey Bataev13314bf2014-10-09 04:18:56 +00005672StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5673 Stmt *AStmt, SourceLocation StartLoc,
5674 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005675 if (!AStmt)
5676 return StmtError();
5677
Alexey Bataev13314bf2014-10-09 04:18:56 +00005678 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5679 // 1.2.2 OpenMP Language Terminology
5680 // Structured block - An executable statement with a single entry at the
5681 // top and a single exit at the bottom.
5682 // The point of exit cannot be a branch out of the structured block.
5683 // longjmp() and throw() must not violate the entry/exit criteria.
5684 CS->getCapturedDecl()->setNothrow();
5685
5686 getCurFunction()->setHasBranchProtectedScope();
5687
5688 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5689}
5690
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005691StmtResult
5692Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5693 SourceLocation EndLoc,
5694 OpenMPDirectiveKind CancelRegion) {
5695 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5696 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5697 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5698 << getOpenMPDirectiveName(CancelRegion);
5699 return StmtError();
5700 }
5701 if (DSAStack->isParentNowaitRegion()) {
5702 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5703 return StmtError();
5704 }
5705 if (DSAStack->isParentOrderedRegion()) {
5706 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5707 return StmtError();
5708 }
5709 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5710 CancelRegion);
5711}
5712
Alexey Bataev87933c72015-09-18 08:07:34 +00005713StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5714 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005715 SourceLocation EndLoc,
5716 OpenMPDirectiveKind CancelRegion) {
5717 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5718 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5719 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5720 << getOpenMPDirectiveName(CancelRegion);
5721 return StmtError();
5722 }
5723 if (DSAStack->isParentNowaitRegion()) {
5724 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5725 return StmtError();
5726 }
5727 if (DSAStack->isParentOrderedRegion()) {
5728 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5729 return StmtError();
5730 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005731 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005732 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5733 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005734}
5735
Alexey Bataev382967a2015-12-08 12:06:20 +00005736static bool checkGrainsizeNumTasksClauses(Sema &S,
5737 ArrayRef<OMPClause *> Clauses) {
5738 OMPClause *PrevClause = nullptr;
5739 bool ErrorFound = false;
5740 for (auto *C : Clauses) {
5741 if (C->getClauseKind() == OMPC_grainsize ||
5742 C->getClauseKind() == OMPC_num_tasks) {
5743 if (!PrevClause)
5744 PrevClause = C;
5745 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5746 S.Diag(C->getLocStart(),
5747 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5748 << getOpenMPClauseName(C->getClauseKind())
5749 << getOpenMPClauseName(PrevClause->getClauseKind());
5750 S.Diag(PrevClause->getLocStart(),
5751 diag::note_omp_previous_grainsize_num_tasks)
5752 << getOpenMPClauseName(PrevClause->getClauseKind());
5753 ErrorFound = true;
5754 }
5755 }
5756 }
5757 return ErrorFound;
5758}
5759
Alexey Bataev49f6e782015-12-01 04:18:41 +00005760StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5761 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5762 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005763 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005764 if (!AStmt)
5765 return StmtError();
5766
5767 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5768 OMPLoopDirective::HelperExprs B;
5769 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5770 // define the nested loops number.
5771 unsigned NestedLoopCount =
5772 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005773 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005774 VarsWithImplicitDSA, B);
5775 if (NestedLoopCount == 0)
5776 return StmtError();
5777
5778 assert((CurContext->isDependentContext() || B.builtAll()) &&
5779 "omp for loop exprs were not built");
5780
Alexey Bataev382967a2015-12-08 12:06:20 +00005781 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5782 // The grainsize clause and num_tasks clause are mutually exclusive and may
5783 // not appear on the same taskloop directive.
5784 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5785 return StmtError();
5786
Alexey Bataev49f6e782015-12-01 04:18:41 +00005787 getCurFunction()->setHasBranchProtectedScope();
5788 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5789 NestedLoopCount, Clauses, AStmt, B);
5790}
5791
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005792StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5793 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5794 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005795 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005796 if (!AStmt)
5797 return StmtError();
5798
5799 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5800 OMPLoopDirective::HelperExprs B;
5801 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5802 // define the nested loops number.
5803 unsigned NestedLoopCount =
5804 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5805 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5806 VarsWithImplicitDSA, B);
5807 if (NestedLoopCount == 0)
5808 return StmtError();
5809
5810 assert((CurContext->isDependentContext() || B.builtAll()) &&
5811 "omp for loop exprs were not built");
5812
Alexey Bataev382967a2015-12-08 12:06:20 +00005813 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5814 // The grainsize clause and num_tasks clause are mutually exclusive and may
5815 // not appear on the same taskloop directive.
5816 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5817 return StmtError();
5818
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005819 getCurFunction()->setHasBranchProtectedScope();
5820 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5821 NestedLoopCount, Clauses, AStmt, B);
5822}
5823
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005824StmtResult Sema::ActOnOpenMPDistributeDirective(
5825 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5826 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005827 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005828 if (!AStmt)
5829 return StmtError();
5830
5831 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5832 OMPLoopDirective::HelperExprs B;
5833 // In presence of clause 'collapse' with number of loops, it will
5834 // define the nested loops number.
5835 unsigned NestedLoopCount =
5836 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5837 nullptr /*ordered not a clause on distribute*/, AStmt,
5838 *this, *DSAStack, VarsWithImplicitDSA, B);
5839 if (NestedLoopCount == 0)
5840 return StmtError();
5841
5842 assert((CurContext->isDependentContext() || B.builtAll()) &&
5843 "omp for loop exprs were not built");
5844
5845 getCurFunction()->setHasBranchProtectedScope();
5846 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5847 NestedLoopCount, Clauses, AStmt, B);
5848}
5849
Alexey Bataeved09d242014-05-28 05:53:51 +00005850OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005851 SourceLocation StartLoc,
5852 SourceLocation LParenLoc,
5853 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005854 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005855 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005856 case OMPC_final:
5857 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5858 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005859 case OMPC_num_threads:
5860 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5861 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005862 case OMPC_safelen:
5863 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5864 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005865 case OMPC_simdlen:
5866 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5867 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005868 case OMPC_collapse:
5869 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5870 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005871 case OMPC_ordered:
5872 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5873 break;
Michael Wonge710d542015-08-07 16:16:36 +00005874 case OMPC_device:
5875 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5876 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005877 case OMPC_num_teams:
5878 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5879 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005880 case OMPC_thread_limit:
5881 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5882 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005883 case OMPC_priority:
5884 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5885 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005886 case OMPC_grainsize:
5887 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5888 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005889 case OMPC_num_tasks:
5890 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5891 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005892 case OMPC_hint:
5893 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5894 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005895 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005896 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005897 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005898 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005899 case OMPC_private:
5900 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005901 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005902 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005903 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005904 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005905 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005906 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005907 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005908 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005909 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005910 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005911 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005912 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005913 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005914 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005915 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005916 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005917 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005918 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005919 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005920 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005921 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005922 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00005923 case OMPC_dist_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005924 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005925 llvm_unreachable("Clause is not allowed.");
5926 }
5927 return Res;
5928}
5929
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005930OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5931 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005932 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005933 SourceLocation NameModifierLoc,
5934 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005935 SourceLocation EndLoc) {
5936 Expr *ValExpr = Condition;
5937 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5938 !Condition->isInstantiationDependent() &&
5939 !Condition->containsUnexpandedParameterPack()) {
5940 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005941 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005942 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005943 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005944
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005945 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005946 }
5947
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005948 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5949 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005950}
5951
Alexey Bataev3778b602014-07-17 07:32:53 +00005952OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5953 SourceLocation StartLoc,
5954 SourceLocation LParenLoc,
5955 SourceLocation EndLoc) {
5956 Expr *ValExpr = Condition;
5957 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5958 !Condition->isInstantiationDependent() &&
5959 !Condition->containsUnexpandedParameterPack()) {
5960 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5961 Condition->getExprLoc(), Condition);
5962 if (Val.isInvalid())
5963 return nullptr;
5964
5965 ValExpr = Val.get();
5966 }
5967
5968 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5969}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005970ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5971 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005972 if (!Op)
5973 return ExprError();
5974
5975 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5976 public:
5977 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005978 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005979 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5980 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005981 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5982 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005983 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5984 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005985 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5986 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005987 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5988 QualType T,
5989 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005990 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5991 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005992 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5993 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005994 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005995 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005996 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005997 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5998 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005999 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6000 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006001 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6002 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006003 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006004 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006005 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006006 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6007 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006008 llvm_unreachable("conversion functions are permitted");
6009 }
6010 } ConvertDiagnoser;
6011 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6012}
6013
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006014static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006015 OpenMPClauseKind CKind,
6016 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006017 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6018 !ValExpr->isInstantiationDependent()) {
6019 SourceLocation Loc = ValExpr->getExprLoc();
6020 ExprResult Value =
6021 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6022 if (Value.isInvalid())
6023 return false;
6024
6025 ValExpr = Value.get();
6026 // The expression must evaluate to a non-negative integer value.
6027 llvm::APSInt Result;
6028 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006029 Result.isSigned() &&
6030 !((!StrictlyPositive && Result.isNonNegative()) ||
6031 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006032 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006033 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6034 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006035 return false;
6036 }
6037 }
6038 return true;
6039}
6040
Alexey Bataev568a8332014-03-06 06:15:19 +00006041OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6042 SourceLocation StartLoc,
6043 SourceLocation LParenLoc,
6044 SourceLocation EndLoc) {
6045 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006046
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006047 // OpenMP [2.5, Restrictions]
6048 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006049 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6050 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006051 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006052
Alexey Bataeved09d242014-05-28 05:53:51 +00006053 return new (Context)
6054 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006055}
6056
Alexey Bataev62c87d22014-03-21 04:51:18 +00006057ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006058 OpenMPClauseKind CKind,
6059 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006060 if (!E)
6061 return ExprError();
6062 if (E->isValueDependent() || E->isTypeDependent() ||
6063 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006064 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006065 llvm::APSInt Result;
6066 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6067 if (ICE.isInvalid())
6068 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006069 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6070 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006071 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006072 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6073 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006074 return ExprError();
6075 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006076 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6077 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6078 << E->getSourceRange();
6079 return ExprError();
6080 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006081 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6082 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006083 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006084 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006085 return ICE;
6086}
6087
6088OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6089 SourceLocation LParenLoc,
6090 SourceLocation EndLoc) {
6091 // OpenMP [2.8.1, simd construct, Description]
6092 // The parameter of the safelen clause must be a constant
6093 // positive integer expression.
6094 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6095 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006096 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006097 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006098 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006099}
6100
Alexey Bataev66b15b52015-08-21 11:14:16 +00006101OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6102 SourceLocation LParenLoc,
6103 SourceLocation EndLoc) {
6104 // OpenMP [2.8.1, simd construct, Description]
6105 // The parameter of the simdlen clause must be a constant
6106 // positive integer expression.
6107 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6108 if (Simdlen.isInvalid())
6109 return nullptr;
6110 return new (Context)
6111 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6112}
6113
Alexander Musman64d33f12014-06-04 07:53:32 +00006114OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6115 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006116 SourceLocation LParenLoc,
6117 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006118 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006119 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006120 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006121 // The parameter of the collapse clause must be a constant
6122 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006123 ExprResult NumForLoopsResult =
6124 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6125 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006126 return nullptr;
6127 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006128 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006129}
6130
Alexey Bataev10e775f2015-07-30 11:36:16 +00006131OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6132 SourceLocation EndLoc,
6133 SourceLocation LParenLoc,
6134 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006135 // OpenMP [2.7.1, loop construct, Description]
6136 // OpenMP [2.8.1, simd construct, Description]
6137 // OpenMP [2.9.6, distribute construct, Description]
6138 // The parameter of the ordered clause must be a constant
6139 // positive integer expression if any.
6140 if (NumForLoops && LParenLoc.isValid()) {
6141 ExprResult NumForLoopsResult =
6142 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6143 if (NumForLoopsResult.isInvalid())
6144 return nullptr;
6145 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006146 } else
6147 NumForLoops = nullptr;
6148 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006149 return new (Context)
6150 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6151}
6152
Alexey Bataeved09d242014-05-28 05:53:51 +00006153OMPClause *Sema::ActOnOpenMPSimpleClause(
6154 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6155 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006156 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006157 switch (Kind) {
6158 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006159 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006160 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6161 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006162 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006163 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006164 Res = ActOnOpenMPProcBindClause(
6165 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6166 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006167 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006168 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006169 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006170 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006171 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006172 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006173 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006174 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006175 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006176 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006177 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006178 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006179 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006180 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006181 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006182 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006183 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006184 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006185 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006186 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006187 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006188 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006189 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006190 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006191 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006192 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006193 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006194 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006195 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006196 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006197 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006198 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006199 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006200 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006201 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006202 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006203 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006204 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006205 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006206 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006207 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006208 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006209 llvm_unreachable("Clause is not allowed.");
6210 }
6211 return Res;
6212}
6213
Alexey Bataev6402bca2015-12-28 07:25:51 +00006214static std::string
6215getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6216 ArrayRef<unsigned> Exclude = llvm::None) {
6217 std::string Values;
6218 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6219 unsigned Skipped = Exclude.size();
6220 auto S = Exclude.begin(), E = Exclude.end();
6221 for (unsigned i = First; i < Last; ++i) {
6222 if (std::find(S, E, i) != E) {
6223 --Skipped;
6224 continue;
6225 }
6226 Values += "'";
6227 Values += getOpenMPSimpleClauseTypeName(K, i);
6228 Values += "'";
6229 if (i == Bound - Skipped)
6230 Values += " or ";
6231 else if (i != Bound + 1 - Skipped)
6232 Values += ", ";
6233 }
6234 return Values;
6235}
6236
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006237OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6238 SourceLocation KindKwLoc,
6239 SourceLocation StartLoc,
6240 SourceLocation LParenLoc,
6241 SourceLocation EndLoc) {
6242 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006243 static_assert(OMPC_DEFAULT_unknown > 0,
6244 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006245 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006246 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6247 /*Last=*/OMPC_DEFAULT_unknown)
6248 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006249 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006250 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006251 switch (Kind) {
6252 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006253 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006254 break;
6255 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006256 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006257 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006258 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006259 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006260 break;
6261 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006262 return new (Context)
6263 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006264}
6265
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006266OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6267 SourceLocation KindKwLoc,
6268 SourceLocation StartLoc,
6269 SourceLocation LParenLoc,
6270 SourceLocation EndLoc) {
6271 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006272 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006273 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6274 /*Last=*/OMPC_PROC_BIND_unknown)
6275 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006276 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006277 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006278 return new (Context)
6279 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006280}
6281
Alexey Bataev56dafe82014-06-20 07:16:17 +00006282OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006283 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006284 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006285 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006286 SourceLocation EndLoc) {
6287 OMPClause *Res = nullptr;
6288 switch (Kind) {
6289 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006290 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6291 assert(Argument.size() == NumberOfElements &&
6292 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006293 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006294 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6295 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6296 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6297 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6298 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006299 break;
6300 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006301 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6302 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6303 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6304 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006305 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006306 case OMPC_dist_schedule:
6307 Res = ActOnOpenMPDistScheduleClause(
6308 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6309 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6310 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006311 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006312 case OMPC_num_threads:
6313 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006314 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006315 case OMPC_collapse:
6316 case OMPC_default:
6317 case OMPC_proc_bind:
6318 case OMPC_private:
6319 case OMPC_firstprivate:
6320 case OMPC_lastprivate:
6321 case OMPC_shared:
6322 case OMPC_reduction:
6323 case OMPC_linear:
6324 case OMPC_aligned:
6325 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006326 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006327 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006328 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006329 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006330 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006331 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006332 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006333 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006334 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006335 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006336 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006337 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006338 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006339 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006340 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006341 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006342 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006343 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006344 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006345 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006346 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006347 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006348 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006349 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006350 case OMPC_unknown:
6351 llvm_unreachable("Clause is not allowed.");
6352 }
6353 return Res;
6354}
6355
Alexey Bataev6402bca2015-12-28 07:25:51 +00006356static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6357 OpenMPScheduleClauseModifier M2,
6358 SourceLocation M1Loc, SourceLocation M2Loc) {
6359 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6360 SmallVector<unsigned, 2> Excluded;
6361 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6362 Excluded.push_back(M2);
6363 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6364 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6365 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6366 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6367 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6368 << getListOfPossibleValues(OMPC_schedule,
6369 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6370 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6371 Excluded)
6372 << getOpenMPClauseName(OMPC_schedule);
6373 return true;
6374 }
6375 return false;
6376}
6377
Alexey Bataev56dafe82014-06-20 07:16:17 +00006378OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006379 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006380 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006381 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6382 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6383 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6384 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6385 return nullptr;
6386 // OpenMP, 2.7.1, Loop Construct, Restrictions
6387 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6388 // but not both.
6389 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6390 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6391 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6392 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6393 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6394 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6395 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6396 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6397 return nullptr;
6398 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006399 if (Kind == OMPC_SCHEDULE_unknown) {
6400 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006401 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6402 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6403 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6404 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6405 Exclude);
6406 } else {
6407 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6408 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006409 }
6410 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6411 << Values << getOpenMPClauseName(OMPC_schedule);
6412 return nullptr;
6413 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006414 // OpenMP, 2.7.1, Loop Construct, Restrictions
6415 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6416 // schedule(guided).
6417 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6418 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6419 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6420 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6421 diag::err_omp_schedule_nonmonotonic_static);
6422 return nullptr;
6423 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006424 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006425 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006426 if (ChunkSize) {
6427 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6428 !ChunkSize->isInstantiationDependent() &&
6429 !ChunkSize->containsUnexpandedParameterPack()) {
6430 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6431 ExprResult Val =
6432 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6433 if (Val.isInvalid())
6434 return nullptr;
6435
6436 ValExpr = Val.get();
6437
6438 // OpenMP [2.7.1, Restrictions]
6439 // chunk_size must be a loop invariant integer expression with a positive
6440 // value.
6441 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006442 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6443 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6444 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006445 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006446 return nullptr;
6447 }
6448 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6449 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6450 ChunkSize->getType(), ".chunk.");
6451 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6452 ChunkSize->getExprLoc(),
6453 /*RefersToCapture=*/true);
6454 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006455 }
6456 }
6457 }
6458
Alexey Bataev6402bca2015-12-28 07:25:51 +00006459 return new (Context)
6460 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6461 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006462}
6463
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006464OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6465 SourceLocation StartLoc,
6466 SourceLocation EndLoc) {
6467 OMPClause *Res = nullptr;
6468 switch (Kind) {
6469 case OMPC_ordered:
6470 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6471 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006472 case OMPC_nowait:
6473 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6474 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006475 case OMPC_untied:
6476 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6477 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006478 case OMPC_mergeable:
6479 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6480 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006481 case OMPC_read:
6482 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6483 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006484 case OMPC_write:
6485 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6486 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006487 case OMPC_update:
6488 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6489 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006490 case OMPC_capture:
6491 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6492 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006493 case OMPC_seq_cst:
6494 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6495 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006496 case OMPC_threads:
6497 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6498 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006499 case OMPC_simd:
6500 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6501 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006502 case OMPC_nogroup:
6503 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6504 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006505 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006506 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006507 case OMPC_num_threads:
6508 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006509 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006510 case OMPC_collapse:
6511 case OMPC_schedule:
6512 case OMPC_private:
6513 case OMPC_firstprivate:
6514 case OMPC_lastprivate:
6515 case OMPC_shared:
6516 case OMPC_reduction:
6517 case OMPC_linear:
6518 case OMPC_aligned:
6519 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006520 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006521 case OMPC_default:
6522 case OMPC_proc_bind:
6523 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006524 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006525 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006526 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006527 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006528 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006529 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006530 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006531 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006532 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006533 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006534 case OMPC_dist_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006535 case OMPC_unknown:
6536 llvm_unreachable("Clause is not allowed.");
6537 }
6538 return Res;
6539}
6540
Alexey Bataev236070f2014-06-20 11:19:47 +00006541OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6542 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006543 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006544 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6545}
6546
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006547OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6548 SourceLocation EndLoc) {
6549 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6550}
6551
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006552OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6553 SourceLocation EndLoc) {
6554 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6555}
6556
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006557OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6558 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006559 return new (Context) OMPReadClause(StartLoc, EndLoc);
6560}
6561
Alexey Bataevdea47612014-07-23 07:46:59 +00006562OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6563 SourceLocation EndLoc) {
6564 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6565}
6566
Alexey Bataev67a4f222014-07-23 10:25:33 +00006567OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6568 SourceLocation EndLoc) {
6569 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6570}
6571
Alexey Bataev459dec02014-07-24 06:46:57 +00006572OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6573 SourceLocation EndLoc) {
6574 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6575}
6576
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006577OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6578 SourceLocation EndLoc) {
6579 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6580}
6581
Alexey Bataev346265e2015-09-25 10:37:12 +00006582OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6583 SourceLocation EndLoc) {
6584 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6585}
6586
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006587OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6588 SourceLocation EndLoc) {
6589 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6590}
6591
Alexey Bataevb825de12015-12-07 10:51:44 +00006592OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6593 SourceLocation EndLoc) {
6594 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6595}
6596
Alexey Bataevc5e02582014-06-16 07:08:35 +00006597OMPClause *Sema::ActOnOpenMPVarListClause(
6598 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6599 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6600 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006601 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006602 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6603 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6604 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006605 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006606 switch (Kind) {
6607 case OMPC_private:
6608 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6609 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006610 case OMPC_firstprivate:
6611 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6612 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006613 case OMPC_lastprivate:
6614 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6615 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006616 case OMPC_shared:
6617 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6618 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006619 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006620 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6621 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006622 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006623 case OMPC_linear:
6624 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006625 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006626 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006627 case OMPC_aligned:
6628 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6629 ColonLoc, EndLoc);
6630 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006631 case OMPC_copyin:
6632 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6633 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006634 case OMPC_copyprivate:
6635 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6636 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006637 case OMPC_flush:
6638 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6639 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006640 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006641 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6642 StartLoc, LParenLoc, EndLoc);
6643 break;
6644 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006645 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6646 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6647 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006648 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006649 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006650 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006651 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006652 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006653 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006654 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006655 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006656 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006657 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006658 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006659 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006660 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006661 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006662 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006663 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006664 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006665 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006666 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006667 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006668 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006669 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006670 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006671 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006672 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006673 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006674 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006675 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006676 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006677 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006678 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006679 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006680 llvm_unreachable("Clause is not allowed.");
6681 }
6682 return Res;
6683}
6684
6685OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6686 SourceLocation StartLoc,
6687 SourceLocation LParenLoc,
6688 SourceLocation EndLoc) {
6689 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006690 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006691 for (auto &RefExpr : VarList) {
6692 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006693 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6694 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006695 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006696 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006697 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006698 continue;
6699 }
6700
Alexey Bataeved09d242014-05-28 05:53:51 +00006701 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006702 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006703 // A list item is a variable name.
6704 // OpenMP [2.9.3.3, Restrictions, p.1]
6705 // A variable that is part of another variable (as an array or
6706 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006707 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6708 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6709 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6710 (getCurrentThisType().isNull() || !ME ||
6711 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6712 !isa<FieldDecl>(ME->getMemberDecl()))) {
6713 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6714 << (getCurrentThisType().isNull() ? 0 : 1)
6715 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006716 continue;
6717 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006718 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6719 QualType Type = D->getType();
6720 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006721
6722 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6723 // A variable that appears in a private clause must not have an incomplete
6724 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006725 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006726 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006727 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006728
Alexey Bataev758e55e2013-09-06 18:03:48 +00006729 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6730 // in a Construct]
6731 // Variables with the predetermined data-sharing attributes may not be
6732 // listed in data-sharing attributes clauses, except for the cases
6733 // listed below. For these exceptions only, listing a predetermined
6734 // variable in a data-sharing attribute clause is allowed and overrides
6735 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006736 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006737 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006738 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6739 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006740 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006741 continue;
6742 }
6743
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006744 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006745 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006746 DSAStack->getCurrentDirective() == OMPD_task) {
6747 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6748 << getOpenMPClauseName(OMPC_private) << Type
6749 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6750 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006751 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006752 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006753 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006754 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006755 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006756 continue;
6757 }
6758
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006759 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6760 // A variable of class type (or array thereof) that appears in a private
6761 // clause requires an accessible, unambiguous default constructor for the
6762 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006763 // Generate helper private variable and initialize it with the default
6764 // value. The address of the original variable is replaced by the address of
6765 // the new private variable in CodeGen. This new variable is not added to
6766 // IdResolver, so the code in the OpenMP region uses original variable for
6767 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006768 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006769 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
6770 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006771 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006772 if (VDPrivate->isInvalidDecl())
6773 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006774 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006775 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006776
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006777 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
6778 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006779 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006780 }
6781
Alexey Bataeved09d242014-05-28 05:53:51 +00006782 if (Vars.empty())
6783 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006784
Alexey Bataev03b340a2014-10-21 03:16:40 +00006785 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6786 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006787}
6788
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006789namespace {
6790class DiagsUninitializedSeveretyRAII {
6791private:
6792 DiagnosticsEngine &Diags;
6793 SourceLocation SavedLoc;
6794 bool IsIgnored;
6795
6796public:
6797 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6798 bool IsIgnored)
6799 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6800 if (!IsIgnored) {
6801 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6802 /*Map*/ diag::Severity::Ignored, Loc);
6803 }
6804 }
6805 ~DiagsUninitializedSeveretyRAII() {
6806 if (!IsIgnored)
6807 Diags.popMappings(SavedLoc);
6808 }
6809};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006810}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006811
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006812OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6813 SourceLocation StartLoc,
6814 SourceLocation LParenLoc,
6815 SourceLocation EndLoc) {
6816 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006817 SmallVector<Expr *, 8> PrivateCopies;
6818 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006819 bool IsImplicitClause =
6820 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6821 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6822
Alexey Bataeved09d242014-05-28 05:53:51 +00006823 for (auto &RefExpr : VarList) {
6824 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6825 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006826 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006827 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006828 PrivateCopies.push_back(nullptr);
6829 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006830 continue;
6831 }
6832
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006833 SourceLocation ELoc =
6834 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006835 // OpenMP [2.1, C/C++]
6836 // A list item is a variable name.
6837 // OpenMP [2.9.3.3, Restrictions, p.1]
6838 // A variable that is part of another variable (as an array or
6839 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006840 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006841 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006842 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6843 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006844 continue;
6845 }
6846 Decl *D = DE->getDecl();
6847 VarDecl *VD = cast<VarDecl>(D);
6848
6849 QualType Type = VD->getType();
6850 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6851 // It will be analyzed later.
6852 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006853 PrivateCopies.push_back(nullptr);
6854 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006855 continue;
6856 }
6857
6858 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6859 // A variable that appears in a private clause must not have an incomplete
6860 // type or a reference type.
6861 if (RequireCompleteType(ELoc, Type,
6862 diag::err_omp_firstprivate_incomplete_type)) {
6863 continue;
6864 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006865 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006866
6867 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6868 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006869 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006870 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006871 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006872
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006873 // If an implicit firstprivate variable found it was checked already.
6874 if (!IsImplicitClause) {
6875 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006876 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006877 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6878 // A list item that specifies a given variable may not appear in more
6879 // than one clause on the same directive, except that a variable may be
6880 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006881 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006882 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006883 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006884 << getOpenMPClauseName(DVar.CKind)
6885 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006886 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006887 continue;
6888 }
6889
6890 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6891 // in a Construct]
6892 // Variables with the predetermined data-sharing attributes may not be
6893 // listed in data-sharing attributes clauses, except for the cases
6894 // listed below. For these exceptions only, listing a predetermined
6895 // variable in a data-sharing attribute clause is allowed and overrides
6896 // the variable's predetermined data-sharing attributes.
6897 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6898 // in a Construct, C/C++, p.2]
6899 // Variables with const-qualified type having no mutable member may be
6900 // listed in a firstprivate clause, even if they are static data members.
6901 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6902 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6903 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006904 << getOpenMPClauseName(DVar.CKind)
6905 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006906 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006907 continue;
6908 }
6909
Alexey Bataevf29276e2014-06-18 04:14:57 +00006910 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006911 // OpenMP [2.9.3.4, Restrictions, p.2]
6912 // A list item that is private within a parallel region must not appear
6913 // in a firstprivate clause on a worksharing construct if any of the
6914 // worksharing regions arising from the worksharing construct ever bind
6915 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006916 if (isOpenMPWorksharingDirective(CurrDir) &&
6917 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006918 DVar = DSAStack->getImplicitDSA(VD, true);
6919 if (DVar.CKind != OMPC_shared &&
6920 (isOpenMPParallelDirective(DVar.DKind) ||
6921 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006922 Diag(ELoc, diag::err_omp_required_access)
6923 << getOpenMPClauseName(OMPC_firstprivate)
6924 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006925 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006926 continue;
6927 }
6928 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006929 // OpenMP [2.9.3.4, Restrictions, p.3]
6930 // A list item that appears in a reduction clause of a parallel construct
6931 // must not appear in a firstprivate clause on a worksharing or task
6932 // construct if any of the worksharing or task regions arising from the
6933 // worksharing or task construct ever bind to any of the parallel regions
6934 // arising from the parallel construct.
6935 // OpenMP [2.9.3.4, Restrictions, p.4]
6936 // A list item that appears in a reduction clause in worksharing
6937 // construct must not appear in a firstprivate clause in a task construct
6938 // encountered during execution of any of the worksharing regions arising
6939 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006940 if (CurrDir == OMPD_task) {
6941 DVar =
6942 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6943 [](OpenMPDirectiveKind K) -> bool {
6944 return isOpenMPParallelDirective(K) ||
6945 isOpenMPWorksharingDirective(K);
6946 },
6947 false);
6948 if (DVar.CKind == OMPC_reduction &&
6949 (isOpenMPParallelDirective(DVar.DKind) ||
6950 isOpenMPWorksharingDirective(DVar.DKind))) {
6951 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6952 << getOpenMPDirectiveName(DVar.DKind);
6953 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6954 continue;
6955 }
6956 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006957
6958 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6959 // A list item that is private within a teams region must not appear in a
6960 // firstprivate clause on a distribute construct if any of the distribute
6961 // regions arising from the distribute construct ever bind to any of the
6962 // teams regions arising from the teams construct.
6963 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6964 // A list item that appears in a reduction clause of a teams construct
6965 // must not appear in a firstprivate clause on a distribute construct if
6966 // any of the distribute regions arising from the distribute construct
6967 // ever bind to any of the teams regions arising from the teams construct.
6968 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6969 // A list item may appear in a firstprivate or lastprivate clause but not
6970 // both.
6971 if (CurrDir == OMPD_distribute) {
6972 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6973 [](OpenMPDirectiveKind K) -> bool {
6974 return isOpenMPTeamsDirective(K);
6975 },
6976 false);
6977 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6978 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6979 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6980 continue;
6981 }
6982 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6983 [](OpenMPDirectiveKind K) -> bool {
6984 return isOpenMPTeamsDirective(K);
6985 },
6986 false);
6987 if (DVar.CKind == OMPC_reduction &&
6988 isOpenMPTeamsDirective(DVar.DKind)) {
6989 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6990 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6991 continue;
6992 }
6993 DVar = DSAStack->getTopDSA(VD, false);
6994 if (DVar.CKind == OMPC_lastprivate) {
6995 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6996 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6997 continue;
6998 }
6999 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007000 }
7001
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007002 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007003 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007004 DSAStack->getCurrentDirective() == OMPD_task) {
7005 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7006 << getOpenMPClauseName(OMPC_firstprivate) << Type
7007 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7008 bool IsDecl =
7009 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7010 Diag(VD->getLocation(),
7011 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7012 << VD;
7013 continue;
7014 }
7015
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007016 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007017 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7018 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007019 // Generate helper private variable and initialize it with the value of the
7020 // original variable. The address of the original variable is replaced by
7021 // the address of the new private variable in the CodeGen. This new variable
7022 // is not added to IdResolver, so the code in the OpenMP region uses
7023 // original variable for proper diagnostics and variable capturing.
7024 Expr *VDInitRefExpr = nullptr;
7025 // For arrays generate initializer for single element and replace it by the
7026 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007027 if (Type->isArrayType()) {
7028 auto VDInit =
7029 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7030 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007031 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007032 ElemType = ElemType.getUnqualifiedType();
7033 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7034 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007035 InitializedEntity Entity =
7036 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007037 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7038
7039 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7040 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7041 if (Result.isInvalid())
7042 VDPrivate->setInvalidDecl();
7043 else
7044 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007045 // Remove temp variable declaration.
7046 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007047 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007048 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007049 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007050 VDInitRefExpr =
7051 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007052 AddInitializerToDecl(VDPrivate,
7053 DefaultLvalueConversion(VDInitRefExpr).get(),
7054 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007055 }
7056 if (VDPrivate->isInvalidDecl()) {
7057 if (IsImplicitClause) {
7058 Diag(DE->getExprLoc(),
7059 diag::note_omp_task_predetermined_firstprivate_here);
7060 }
7061 continue;
7062 }
7063 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007064 auto VDPrivateRefExpr = buildDeclRefExpr(
7065 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007066 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7067 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007068 PrivateCopies.push_back(VDPrivateRefExpr);
7069 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007070 }
7071
Alexey Bataeved09d242014-05-28 05:53:51 +00007072 if (Vars.empty())
7073 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007074
7075 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007076 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007077}
7078
Alexander Musman1bb328c2014-06-04 13:06:39 +00007079OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7080 SourceLocation StartLoc,
7081 SourceLocation LParenLoc,
7082 SourceLocation EndLoc) {
7083 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007084 SmallVector<Expr *, 8> SrcExprs;
7085 SmallVector<Expr *, 8> DstExprs;
7086 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007087 for (auto &RefExpr : VarList) {
7088 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7089 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7090 // It will be analyzed later.
7091 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007092 SrcExprs.push_back(nullptr);
7093 DstExprs.push_back(nullptr);
7094 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007095 continue;
7096 }
7097
7098 SourceLocation ELoc = RefExpr->getExprLoc();
7099 // OpenMP [2.1, C/C++]
7100 // A list item is a variable name.
7101 // OpenMP [2.14.3.5, Restrictions, p.1]
7102 // A variable that is part of another variable (as an array or structure
7103 // element) cannot appear in a lastprivate clause.
7104 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7105 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007106 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7107 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007108 continue;
7109 }
7110 Decl *D = DE->getDecl();
7111 VarDecl *VD = cast<VarDecl>(D);
7112
7113 QualType Type = VD->getType();
7114 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7115 // It will be analyzed later.
7116 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007117 SrcExprs.push_back(nullptr);
7118 DstExprs.push_back(nullptr);
7119 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007120 continue;
7121 }
7122
7123 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7124 // A variable that appears in a lastprivate clause must not have an
7125 // incomplete type or a reference type.
7126 if (RequireCompleteType(ELoc, Type,
7127 diag::err_omp_lastprivate_incomplete_type)) {
7128 continue;
7129 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007130 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007131
7132 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7133 // in a Construct]
7134 // Variables with the predetermined data-sharing attributes may not be
7135 // listed in data-sharing attributes clauses, except for the cases
7136 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007137 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007138 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7139 DVar.CKind != OMPC_firstprivate &&
7140 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7141 Diag(ELoc, diag::err_omp_wrong_dsa)
7142 << getOpenMPClauseName(DVar.CKind)
7143 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007144 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007145 continue;
7146 }
7147
Alexey Bataevf29276e2014-06-18 04:14:57 +00007148 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7149 // OpenMP [2.14.3.5, Restrictions, p.2]
7150 // A list item that is private within a parallel region, or that appears in
7151 // the reduction clause of a parallel construct, must not appear in a
7152 // lastprivate clause on a worksharing construct if any of the corresponding
7153 // worksharing regions ever binds to any of the corresponding parallel
7154 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007155 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007156 if (isOpenMPWorksharingDirective(CurrDir) &&
7157 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007158 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007159 if (DVar.CKind != OMPC_shared) {
7160 Diag(ELoc, diag::err_omp_required_access)
7161 << getOpenMPClauseName(OMPC_lastprivate)
7162 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007163 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007164 continue;
7165 }
7166 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007167 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007168 // A variable of class type (or array thereof) that appears in a
7169 // lastprivate clause requires an accessible, unambiguous default
7170 // constructor for the class type, unless the list item is also specified
7171 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007172 // A variable of class type (or array thereof) that appears in a
7173 // lastprivate clause requires an accessible, unambiguous copy assignment
7174 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007175 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007176 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007177 Type.getUnqualifiedType(), ".lastprivate.src",
7178 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007179 auto *PseudoSrcExpr = buildDeclRefExpr(
7180 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007181 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007182 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7183 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007184 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007185 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007186 // For arrays generate assignment operation for single element and replace
7187 // it by the original array element in CodeGen.
7188 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7189 PseudoDstExpr, PseudoSrcExpr);
7190 if (AssignmentOp.isInvalid())
7191 continue;
7192 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7193 /*DiscardedValue=*/true);
7194 if (AssignmentOp.isInvalid())
7195 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007196
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007197 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7198 // A list item may appear in a firstprivate or lastprivate clause but not
7199 // both.
7200 if (CurrDir == OMPD_distribute) {
7201 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7202 if (DVar.CKind == OMPC_firstprivate) {
7203 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7204 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7205 continue;
7206 }
7207 }
7208
Alexey Bataev39f915b82015-05-08 10:41:21 +00007209 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007210 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007211 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007212 SrcExprs.push_back(PseudoSrcExpr);
7213 DstExprs.push_back(PseudoDstExpr);
7214 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007215 }
7216
7217 if (Vars.empty())
7218 return nullptr;
7219
7220 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007221 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007222}
7223
Alexey Bataev758e55e2013-09-06 18:03:48 +00007224OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7225 SourceLocation StartLoc,
7226 SourceLocation LParenLoc,
7227 SourceLocation EndLoc) {
7228 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007229 for (auto &RefExpr : VarList) {
7230 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7231 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007232 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007233 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007234 continue;
7235 }
7236
Alexey Bataeved09d242014-05-28 05:53:51 +00007237 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007238 // OpenMP [2.1, C/C++]
7239 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007240 // OpenMP [2.14.3.2, Restrictions, p.1]
7241 // A variable that is part of another variable (as an array or structure
7242 // element) cannot appear in a shared unless it is a static data member
7243 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007244 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007245 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007246 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7247 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007248 continue;
7249 }
7250 Decl *D = DE->getDecl();
7251 VarDecl *VD = cast<VarDecl>(D);
7252
7253 QualType Type = VD->getType();
7254 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7255 // It will be analyzed later.
7256 Vars.push_back(DE);
7257 continue;
7258 }
7259
7260 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7261 // in a Construct]
7262 // Variables with the predetermined data-sharing attributes may not be
7263 // listed in data-sharing attributes clauses, except for the cases
7264 // listed below. For these exceptions only, listing a predetermined
7265 // variable in a data-sharing attribute clause is allowed and overrides
7266 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007267 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007268 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7269 DVar.RefExpr) {
7270 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7271 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007272 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007273 continue;
7274 }
7275
7276 DSAStack->addDSA(VD, DE, OMPC_shared);
7277 Vars.push_back(DE);
7278 }
7279
Alexey Bataeved09d242014-05-28 05:53:51 +00007280 if (Vars.empty())
7281 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007282
7283 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7284}
7285
Alexey Bataevc5e02582014-06-16 07:08:35 +00007286namespace {
7287class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7288 DSAStackTy *Stack;
7289
7290public:
7291 bool VisitDeclRefExpr(DeclRefExpr *E) {
7292 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007293 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007294 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7295 return false;
7296 if (DVar.CKind != OMPC_unknown)
7297 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007298 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007299 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007300 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007301 return true;
7302 return false;
7303 }
7304 return false;
7305 }
7306 bool VisitStmt(Stmt *S) {
7307 for (auto Child : S->children()) {
7308 if (Child && Visit(Child))
7309 return true;
7310 }
7311 return false;
7312 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007313 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007314};
Alexey Bataev23b69422014-06-18 07:08:49 +00007315} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007316
7317OMPClause *Sema::ActOnOpenMPReductionClause(
7318 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7319 SourceLocation ColonLoc, SourceLocation EndLoc,
7320 CXXScopeSpec &ReductionIdScopeSpec,
7321 const DeclarationNameInfo &ReductionId) {
7322 // TODO: Allow scope specification search when 'declare reduction' is
7323 // supported.
7324 assert(ReductionIdScopeSpec.isEmpty() &&
7325 "No support for scoped reduction identifiers yet.");
7326
7327 auto DN = ReductionId.getName();
7328 auto OOK = DN.getCXXOverloadedOperator();
7329 BinaryOperatorKind BOK = BO_Comma;
7330
7331 // OpenMP [2.14.3.6, reduction clause]
7332 // C
7333 // reduction-identifier is either an identifier or one of the following
7334 // operators: +, -, *, &, |, ^, && and ||
7335 // C++
7336 // reduction-identifier is either an id-expression or one of the following
7337 // operators: +, -, *, &, |, ^, && and ||
7338 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7339 switch (OOK) {
7340 case OO_Plus:
7341 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007342 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007343 break;
7344 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007345 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007346 break;
7347 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007348 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007349 break;
7350 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007351 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007352 break;
7353 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007354 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007355 break;
7356 case OO_AmpAmp:
7357 BOK = BO_LAnd;
7358 break;
7359 case OO_PipePipe:
7360 BOK = BO_LOr;
7361 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007362 case OO_New:
7363 case OO_Delete:
7364 case OO_Array_New:
7365 case OO_Array_Delete:
7366 case OO_Slash:
7367 case OO_Percent:
7368 case OO_Tilde:
7369 case OO_Exclaim:
7370 case OO_Equal:
7371 case OO_Less:
7372 case OO_Greater:
7373 case OO_LessEqual:
7374 case OO_GreaterEqual:
7375 case OO_PlusEqual:
7376 case OO_MinusEqual:
7377 case OO_StarEqual:
7378 case OO_SlashEqual:
7379 case OO_PercentEqual:
7380 case OO_CaretEqual:
7381 case OO_AmpEqual:
7382 case OO_PipeEqual:
7383 case OO_LessLess:
7384 case OO_GreaterGreater:
7385 case OO_LessLessEqual:
7386 case OO_GreaterGreaterEqual:
7387 case OO_EqualEqual:
7388 case OO_ExclaimEqual:
7389 case OO_PlusPlus:
7390 case OO_MinusMinus:
7391 case OO_Comma:
7392 case OO_ArrowStar:
7393 case OO_Arrow:
7394 case OO_Call:
7395 case OO_Subscript:
7396 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007397 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007398 case NUM_OVERLOADED_OPERATORS:
7399 llvm_unreachable("Unexpected reduction identifier");
7400 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007401 if (auto II = DN.getAsIdentifierInfo()) {
7402 if (II->isStr("max"))
7403 BOK = BO_GT;
7404 else if (II->isStr("min"))
7405 BOK = BO_LT;
7406 }
7407 break;
7408 }
7409 SourceRange ReductionIdRange;
7410 if (ReductionIdScopeSpec.isValid()) {
7411 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7412 }
7413 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7414 if (BOK == BO_Comma) {
7415 // Not allowed reduction identifier is found.
7416 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7417 << ReductionIdRange;
7418 return nullptr;
7419 }
7420
7421 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007422 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007423 SmallVector<Expr *, 8> LHSs;
7424 SmallVector<Expr *, 8> RHSs;
7425 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007426 for (auto RefExpr : VarList) {
7427 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7428 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7429 // It will be analyzed later.
7430 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007431 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007432 LHSs.push_back(nullptr);
7433 RHSs.push_back(nullptr);
7434 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007435 continue;
7436 }
7437
7438 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7439 RefExpr->isInstantiationDependent() ||
7440 RefExpr->containsUnexpandedParameterPack()) {
7441 // It will be analyzed later.
7442 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007443 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007444 LHSs.push_back(nullptr);
7445 RHSs.push_back(nullptr);
7446 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007447 continue;
7448 }
7449
7450 auto ELoc = RefExpr->getExprLoc();
7451 auto ERange = RefExpr->getSourceRange();
7452 // OpenMP [2.1, C/C++]
7453 // A list item is a variable or array section, subject to the restrictions
7454 // specified in Section 2.4 on page 42 and in each of the sections
7455 // describing clauses and directives for which a list appears.
7456 // OpenMP [2.14.3.3, Restrictions, p.1]
7457 // A variable that is part of another variable (as an array or
7458 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007459 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7460 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7461 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7462 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007463 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7464 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007465 continue;
7466 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007467 QualType Type;
7468 VarDecl *VD = nullptr;
7469 if (DE) {
7470 auto D = DE->getDecl();
7471 VD = cast<VarDecl>(D);
Alexey Bataev1189bd02016-01-26 12:20:39 +00007472 Type = Context.getBaseElementType(VD->getType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007473 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007474 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007475 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7476 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7477 Base = TempASE->getBase()->IgnoreParenImpCasts();
7478 DE = dyn_cast<DeclRefExpr>(Base);
7479 if (DE)
7480 VD = dyn_cast<VarDecl>(DE->getDecl());
7481 if (!VD) {
7482 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7483 << 0 << Base->getSourceRange();
7484 continue;
7485 }
7486 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007487 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7488 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7489 Type = ATy->getElementType();
7490 else
7491 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007492 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7493 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7494 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7495 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7496 Base = TempASE->getBase()->IgnoreParenImpCasts();
7497 DE = dyn_cast<DeclRefExpr>(Base);
7498 if (DE)
7499 VD = dyn_cast<VarDecl>(DE->getDecl());
7500 if (!VD) {
7501 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7502 << 1 << Base->getSourceRange();
7503 continue;
7504 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007505 }
7506
Alexey Bataevc5e02582014-06-16 07:08:35 +00007507 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7508 // A variable that appears in a private clause must not have an incomplete
7509 // type or a reference type.
7510 if (RequireCompleteType(ELoc, Type,
7511 diag::err_omp_reduction_incomplete_type))
7512 continue;
7513 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007514 // A list item that appears in a reduction clause must not be
7515 // const-qualified.
7516 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007517 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007518 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007519 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007520 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7521 VarDecl::DeclarationOnly;
7522 Diag(VD->getLocation(),
7523 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7524 << VD;
7525 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007526 continue;
7527 }
7528 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7529 // If a list-item is a reference type then it must bind to the same object
7530 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007531 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007532 VarDecl *VDDef = VD->getDefinition();
7533 if (Type->isReferenceType() && VDDef) {
7534 DSARefChecker Check(DSAStack);
7535 if (Check.Visit(VDDef->getInit())) {
7536 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7537 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7538 continue;
7539 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007540 }
7541 }
7542 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7543 // The type of a list item that appears in a reduction clause must be valid
7544 // for the reduction-identifier. For a max or min reduction in C, the type
7545 // of the list item must be an allowed arithmetic data type: char, int,
7546 // float, double, or _Bool, possibly modified with long, short, signed, or
7547 // unsigned. For a max or min reduction in C++, the type of the list item
7548 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7549 // double, or bool, possibly modified with long, short, signed, or unsigned.
7550 if ((BOK == BO_GT || BOK == BO_LT) &&
7551 !(Type->isScalarType() ||
7552 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7553 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7554 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007555 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007556 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7557 VarDecl::DeclarationOnly;
7558 Diag(VD->getLocation(),
7559 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7560 << VD;
7561 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007562 continue;
7563 }
7564 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7565 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7566 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007567 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007568 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7569 VarDecl::DeclarationOnly;
7570 Diag(VD->getLocation(),
7571 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7572 << VD;
7573 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007574 continue;
7575 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007576 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7577 // in a Construct]
7578 // Variables with the predetermined data-sharing attributes may not be
7579 // listed in data-sharing attributes clauses, except for the cases
7580 // listed below. For these exceptions only, listing a predetermined
7581 // variable in a data-sharing attribute clause is allowed and overrides
7582 // the variable's predetermined data-sharing attributes.
7583 // OpenMP [2.14.3.6, Restrictions, p.3]
7584 // Any number of reduction clauses can be specified on the directive,
7585 // but a list item can appear only once in the reduction clauses for that
7586 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007587 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007588 DVar = DSAStack->getTopDSA(VD, false);
7589 if (DVar.CKind == OMPC_reduction) {
7590 Diag(ELoc, diag::err_omp_once_referenced)
7591 << getOpenMPClauseName(OMPC_reduction);
7592 if (DVar.RefExpr) {
7593 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007594 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007595 } else if (DVar.CKind != OMPC_unknown) {
7596 Diag(ELoc, diag::err_omp_wrong_dsa)
7597 << getOpenMPClauseName(DVar.CKind)
7598 << getOpenMPClauseName(OMPC_reduction);
7599 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7600 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007601 }
7602
7603 // OpenMP [2.14.3.6, Restrictions, p.1]
7604 // A list item that appears in a reduction clause of a worksharing
7605 // construct must be shared in the parallel regions to which any of the
7606 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007607 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7608 if (isOpenMPWorksharingDirective(CurrDir) &&
7609 !isOpenMPParallelDirective(CurrDir)) {
7610 DVar = DSAStack->getImplicitDSA(VD, true);
7611 if (DVar.CKind != OMPC_shared) {
7612 Diag(ELoc, diag::err_omp_required_access)
7613 << getOpenMPClauseName(OMPC_reduction)
7614 << getOpenMPClauseName(OMPC_shared);
7615 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7616 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007617 }
7618 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007619
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007620 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007621 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7622 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7623 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7624 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7625 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007626 if (OASE ||
7627 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7628 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007629 // Create pseudo array type for private copy. The size for this array will
7630 // be generated during codegen.
7631 // For array subscripts or single variables Private Ty is the same as Type
7632 // (type of the variable or single array element).
7633 PrivateTy = Context.getVariableArrayType(
7634 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7635 Context.getSizeType(), VK_RValue),
7636 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007637 } else if (DE &&
7638 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7639 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007640 // Private copy.
7641 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7642 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007643 // Add initializer for private variable.
7644 Expr *Init = nullptr;
7645 switch (BOK) {
7646 case BO_Add:
7647 case BO_Xor:
7648 case BO_Or:
7649 case BO_LOr:
7650 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7651 if (Type->isScalarType() || Type->isAnyComplexType()) {
7652 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007653 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007654 break;
7655 case BO_Mul:
7656 case BO_LAnd:
7657 if (Type->isScalarType() || Type->isAnyComplexType()) {
7658 // '*' and '&&' reduction ops - initializer is '1'.
7659 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7660 }
7661 break;
7662 case BO_And: {
7663 // '&' reduction op - initializer is '~0'.
7664 QualType OrigType = Type;
7665 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7666 Type = ComplexTy->getElementType();
7667 }
7668 if (Type->isRealFloatingType()) {
7669 llvm::APFloat InitValue =
7670 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7671 /*isIEEE=*/true);
7672 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7673 Type, ELoc);
7674 } else if (Type->isScalarType()) {
7675 auto Size = Context.getTypeSize(Type);
7676 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7677 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7678 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7679 }
7680 if (Init && OrigType->isAnyComplexType()) {
7681 // Init = 0xFFFF + 0xFFFFi;
7682 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7683 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7684 }
7685 Type = OrigType;
7686 break;
7687 }
7688 case BO_LT:
7689 case BO_GT: {
7690 // 'min' reduction op - initializer is 'Largest representable number in
7691 // the reduction list item type'.
7692 // 'max' reduction op - initializer is 'Least representable number in
7693 // the reduction list item type'.
7694 if (Type->isIntegerType() || Type->isPointerType()) {
7695 bool IsSigned = Type->hasSignedIntegerRepresentation();
7696 auto Size = Context.getTypeSize(Type);
7697 QualType IntTy =
7698 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7699 llvm::APInt InitValue =
7700 (BOK != BO_LT)
7701 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7702 : llvm::APInt::getMinValue(Size)
7703 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7704 : llvm::APInt::getMaxValue(Size);
7705 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7706 if (Type->isPointerType()) {
7707 // Cast to pointer type.
7708 auto CastExpr = BuildCStyleCastExpr(
7709 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7710 SourceLocation(), Init);
7711 if (CastExpr.isInvalid())
7712 continue;
7713 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007714 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007715 } else if (Type->isRealFloatingType()) {
7716 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7717 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7718 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7719 Type, ELoc);
7720 }
7721 break;
7722 }
7723 case BO_PtrMemD:
7724 case BO_PtrMemI:
7725 case BO_MulAssign:
7726 case BO_Div:
7727 case BO_Rem:
7728 case BO_Sub:
7729 case BO_Shl:
7730 case BO_Shr:
7731 case BO_LE:
7732 case BO_GE:
7733 case BO_EQ:
7734 case BO_NE:
7735 case BO_AndAssign:
7736 case BO_XorAssign:
7737 case BO_OrAssign:
7738 case BO_Assign:
7739 case BO_AddAssign:
7740 case BO_SubAssign:
7741 case BO_DivAssign:
7742 case BO_RemAssign:
7743 case BO_ShlAssign:
7744 case BO_ShrAssign:
7745 case BO_Comma:
7746 llvm_unreachable("Unexpected reduction operation");
7747 }
7748 if (Init) {
7749 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7750 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007751 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007752 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007753 if (!RHSVD->hasInit()) {
7754 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7755 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007756 if (VD) {
7757 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7758 VarDecl::DeclarationOnly;
7759 Diag(VD->getLocation(),
7760 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7761 << VD;
7762 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007763 continue;
7764 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007765 // Store initializer for single element in private copy. Will be used during
7766 // codegen.
7767 PrivateVD->setInit(RHSVD->getInit());
7768 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007769 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7770 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007771 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007772 ExprResult ReductionOp =
7773 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7774 LHSDRE, RHSDRE);
7775 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007776 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007777 ReductionOp =
7778 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7779 BO_Assign, LHSDRE, ReductionOp.get());
7780 } else {
7781 auto *ConditionalOp = new (Context) ConditionalOperator(
7782 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7783 RHSDRE, Type, VK_LValue, OK_Ordinary);
7784 ReductionOp =
7785 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7786 BO_Assign, LHSDRE, ConditionalOp);
7787 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007788 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007789 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007790 if (ReductionOp.isInvalid())
7791 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007792
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007793 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007794 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007795 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007796 LHSs.push_back(LHSDRE);
7797 RHSs.push_back(RHSDRE);
7798 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007799 }
7800
7801 if (Vars.empty())
7802 return nullptr;
7803
7804 return OMPReductionClause::Create(
7805 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007806 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7807 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007808}
7809
Alexey Bataev182227b2015-08-20 10:54:39 +00007810OMPClause *Sema::ActOnOpenMPLinearClause(
7811 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7812 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7813 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007814 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007815 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007816 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007817 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7818 LinKind == OMPC_LINEAR_unknown) {
7819 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7820 LinKind = OMPC_LINEAR_val;
7821 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007822 for (auto &RefExpr : VarList) {
7823 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7824 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007825 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007826 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007827 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007828 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007829 continue;
7830 }
7831
7832 // OpenMP [2.14.3.7, linear clause]
7833 // A list item that appears in a linear clause is subject to the private
7834 // clause semantics described in Section 2.14.3.3 on page 159 except as
7835 // noted. In addition, the value of the new list item on each iteration
7836 // of the associated loop(s) corresponds to the value of the original
7837 // list item before entering the construct plus the logical number of
7838 // the iteration times linear-step.
7839
Alexey Bataeved09d242014-05-28 05:53:51 +00007840 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007841 // OpenMP [2.1, C/C++]
7842 // A list item is a variable name.
7843 // OpenMP [2.14.3.3, Restrictions, p.1]
7844 // A variable that is part of another variable (as an array or
7845 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007846 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007847 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007848 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7849 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007850 continue;
7851 }
7852
7853 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7854
7855 // OpenMP [2.14.3.7, linear clause]
7856 // A list-item cannot appear in more than one linear clause.
7857 // A list-item that appears in a linear clause cannot appear in any
7858 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007859 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007860 if (DVar.RefExpr) {
7861 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7862 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007863 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007864 continue;
7865 }
7866
7867 QualType QType = VD->getType();
7868 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7869 // It will be analyzed later.
7870 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007871 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007872 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007873 continue;
7874 }
7875
7876 // A variable must not have an incomplete type or a reference type.
7877 if (RequireCompleteType(ELoc, QType,
7878 diag::err_omp_linear_incomplete_type)) {
7879 continue;
7880 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007881 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7882 !QType->isReferenceType()) {
7883 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7884 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7885 continue;
7886 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007887 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007888
7889 // A list item must not be const-qualified.
7890 if (QType.isConstant(Context)) {
7891 Diag(ELoc, diag::err_omp_const_variable)
7892 << getOpenMPClauseName(OMPC_linear);
7893 bool IsDecl =
7894 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7895 Diag(VD->getLocation(),
7896 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7897 << VD;
7898 continue;
7899 }
7900
7901 // A list item must be of integral or pointer type.
7902 QType = QType.getUnqualifiedType().getCanonicalType();
7903 const Type *Ty = QType.getTypePtrOrNull();
7904 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7905 !Ty->isPointerType())) {
7906 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7907 bool IsDecl =
7908 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7909 Diag(VD->getLocation(),
7910 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7911 << VD;
7912 continue;
7913 }
7914
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007915 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007916 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7917 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007918 auto *PrivateRef = buildDeclRefExpr(
7919 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007920 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007921 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007922 Expr *InitExpr;
7923 if (LinKind == OMPC_LINEAR_uval)
7924 InitExpr = VD->getInit();
7925 else
7926 InitExpr = DE;
7927 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007928 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007929 auto InitRef = buildDeclRefExpr(
7930 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007931 DSAStack->addDSA(VD, DE, OMPC_linear);
7932 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007933 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007934 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007935 }
7936
7937 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007938 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007939
7940 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007941 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007942 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7943 !Step->isInstantiationDependent() &&
7944 !Step->containsUnexpandedParameterPack()) {
7945 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007946 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007947 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007948 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007949 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007950
Alexander Musman3276a272015-03-21 10:12:56 +00007951 // Build var to save the step value.
7952 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007953 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007954 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007955 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007956 ExprResult CalcStep =
7957 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007958 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007959
Alexander Musman8dba6642014-04-22 13:09:42 +00007960 // Warn about zero linear step (it would be probably better specified as
7961 // making corresponding variables 'const').
7962 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007963 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7964 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007965 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7966 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007967 if (!IsConstant && CalcStep.isUsable()) {
7968 // Calculate the step beforehand instead of doing this on each iteration.
7969 // (This is not used if the number of iterations may be kfold-ed).
7970 CalcStepExpr = CalcStep.get();
7971 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007972 }
7973
Alexey Bataev182227b2015-08-20 10:54:39 +00007974 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7975 ColonLoc, EndLoc, Vars, Privates, Inits,
7976 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007977}
7978
7979static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7980 Expr *NumIterations, Sema &SemaRef,
7981 Scope *S) {
7982 // Walk the vars and build update/final expressions for the CodeGen.
7983 SmallVector<Expr *, 8> Updates;
7984 SmallVector<Expr *, 8> Finals;
7985 Expr *Step = Clause.getStep();
7986 Expr *CalcStep = Clause.getCalcStep();
7987 // OpenMP [2.14.3.7, linear clause]
7988 // If linear-step is not specified it is assumed to be 1.
7989 if (Step == nullptr)
7990 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7991 else if (CalcStep)
7992 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7993 bool HasErrors = false;
7994 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007995 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007996 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007997 for (auto &RefExpr : Clause.varlists()) {
7998 Expr *InitExpr = *CurInit;
7999
8000 // Build privatized reference to the current linear var.
8001 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008002 Expr *CapturedRef;
8003 if (LinKind == OMPC_LINEAR_uval)
8004 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8005 else
8006 CapturedRef =
8007 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8008 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8009 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008010
8011 // Build update: Var = InitExpr + IV * Step
8012 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008013 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008014 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008015 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8016 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008017
8018 // Build final: Var = InitExpr + NumIterations * Step
8019 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008020 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008021 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008022 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8023 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008024 if (!Update.isUsable() || !Final.isUsable()) {
8025 Updates.push_back(nullptr);
8026 Finals.push_back(nullptr);
8027 HasErrors = true;
8028 } else {
8029 Updates.push_back(Update.get());
8030 Finals.push_back(Final.get());
8031 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008032 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008033 }
8034 Clause.setUpdates(Updates);
8035 Clause.setFinals(Finals);
8036 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008037}
8038
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008039OMPClause *Sema::ActOnOpenMPAlignedClause(
8040 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8041 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8042
8043 SmallVector<Expr *, 8> Vars;
8044 for (auto &RefExpr : VarList) {
8045 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8046 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8047 // It will be analyzed later.
8048 Vars.push_back(RefExpr);
8049 continue;
8050 }
8051
8052 SourceLocation ELoc = RefExpr->getExprLoc();
8053 // OpenMP [2.1, C/C++]
8054 // A list item is a variable name.
8055 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8056 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008057 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8058 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008059 continue;
8060 }
8061
8062 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8063
8064 // OpenMP [2.8.1, simd construct, Restrictions]
8065 // The type of list items appearing in the aligned clause must be
8066 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008067 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008068 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008069 const Type *Ty = QType.getTypePtrOrNull();
8070 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8071 !Ty->isPointerType())) {
8072 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8073 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8074 bool IsDecl =
8075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8076 Diag(VD->getLocation(),
8077 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8078 << VD;
8079 continue;
8080 }
8081
8082 // OpenMP [2.8.1, simd construct, Restrictions]
8083 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008084 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008085 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8086 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8087 << getOpenMPClauseName(OMPC_aligned);
8088 continue;
8089 }
8090
8091 Vars.push_back(DE);
8092 }
8093
8094 // OpenMP [2.8.1, simd construct, Description]
8095 // The parameter of the aligned clause, alignment, must be a constant
8096 // positive integer expression.
8097 // If no optional parameter is specified, implementation-defined default
8098 // alignments for SIMD instructions on the target platforms are assumed.
8099 if (Alignment != nullptr) {
8100 ExprResult AlignResult =
8101 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8102 if (AlignResult.isInvalid())
8103 return nullptr;
8104 Alignment = AlignResult.get();
8105 }
8106 if (Vars.empty())
8107 return nullptr;
8108
8109 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8110 EndLoc, Vars, Alignment);
8111}
8112
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008113OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8114 SourceLocation StartLoc,
8115 SourceLocation LParenLoc,
8116 SourceLocation EndLoc) {
8117 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008118 SmallVector<Expr *, 8> SrcExprs;
8119 SmallVector<Expr *, 8> DstExprs;
8120 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008121 for (auto &RefExpr : VarList) {
8122 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8123 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008124 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008125 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008126 SrcExprs.push_back(nullptr);
8127 DstExprs.push_back(nullptr);
8128 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008129 continue;
8130 }
8131
Alexey Bataeved09d242014-05-28 05:53:51 +00008132 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008133 // OpenMP [2.1, C/C++]
8134 // A list item is a variable name.
8135 // OpenMP [2.14.4.1, Restrictions, p.1]
8136 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008137 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008138 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008139 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8140 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008141 continue;
8142 }
8143
8144 Decl *D = DE->getDecl();
8145 VarDecl *VD = cast<VarDecl>(D);
8146
8147 QualType Type = VD->getType();
8148 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8149 // It will be analyzed later.
8150 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008151 SrcExprs.push_back(nullptr);
8152 DstExprs.push_back(nullptr);
8153 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008154 continue;
8155 }
8156
8157 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8158 // A list item that appears in a copyin clause must be threadprivate.
8159 if (!DSAStack->isThreadPrivate(VD)) {
8160 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008161 << getOpenMPClauseName(OMPC_copyin)
8162 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008163 continue;
8164 }
8165
8166 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8167 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008168 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008169 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008170 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008171 auto *SrcVD =
8172 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8173 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008174 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008175 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8176 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008177 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8178 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008179 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008180 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008181 // For arrays generate assignment operation for single element and replace
8182 // it by the original array element in CodeGen.
8183 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8184 PseudoDstExpr, PseudoSrcExpr);
8185 if (AssignmentOp.isInvalid())
8186 continue;
8187 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8188 /*DiscardedValue=*/true);
8189 if (AssignmentOp.isInvalid())
8190 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008191
8192 DSAStack->addDSA(VD, DE, OMPC_copyin);
8193 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008194 SrcExprs.push_back(PseudoSrcExpr);
8195 DstExprs.push_back(PseudoDstExpr);
8196 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008197 }
8198
Alexey Bataeved09d242014-05-28 05:53:51 +00008199 if (Vars.empty())
8200 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008201
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008202 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8203 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008204}
8205
Alexey Bataevbae9a792014-06-27 10:37:06 +00008206OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8207 SourceLocation StartLoc,
8208 SourceLocation LParenLoc,
8209 SourceLocation EndLoc) {
8210 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008211 SmallVector<Expr *, 8> SrcExprs;
8212 SmallVector<Expr *, 8> DstExprs;
8213 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008214 for (auto &RefExpr : VarList) {
8215 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8216 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8217 // It will be analyzed later.
8218 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008219 SrcExprs.push_back(nullptr);
8220 DstExprs.push_back(nullptr);
8221 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008222 continue;
8223 }
8224
8225 SourceLocation ELoc = RefExpr->getExprLoc();
8226 // OpenMP [2.1, C/C++]
8227 // A list item is a variable name.
8228 // OpenMP [2.14.4.1, Restrictions, p.1]
8229 // A list item that appears in a copyin clause must be threadprivate.
8230 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8231 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008232 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8233 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008234 continue;
8235 }
8236
8237 Decl *D = DE->getDecl();
8238 VarDecl *VD = cast<VarDecl>(D);
8239
8240 QualType Type = VD->getType();
8241 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8242 // It will be analyzed later.
8243 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008244 SrcExprs.push_back(nullptr);
8245 DstExprs.push_back(nullptr);
8246 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008247 continue;
8248 }
8249
8250 // OpenMP [2.14.4.2, Restrictions, p.2]
8251 // A list item that appears in a copyprivate clause may not appear in a
8252 // private or firstprivate clause on the single construct.
8253 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008254 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008255 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8256 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008257 Diag(ELoc, diag::err_omp_wrong_dsa)
8258 << getOpenMPClauseName(DVar.CKind)
8259 << getOpenMPClauseName(OMPC_copyprivate);
8260 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8261 continue;
8262 }
8263
8264 // OpenMP [2.11.4.2, Restrictions, p.1]
8265 // All list items that appear in a copyprivate clause must be either
8266 // threadprivate or private in the enclosing context.
8267 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008268 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008269 if (DVar.CKind == OMPC_shared) {
8270 Diag(ELoc, diag::err_omp_required_access)
8271 << getOpenMPClauseName(OMPC_copyprivate)
8272 << "threadprivate or private in the enclosing context";
8273 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8274 continue;
8275 }
8276 }
8277 }
8278
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008279 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008280 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008281 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008282 << getOpenMPClauseName(OMPC_copyprivate) << Type
8283 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008284 bool IsDecl =
8285 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8286 Diag(VD->getLocation(),
8287 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8288 << VD;
8289 continue;
8290 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008291
Alexey Bataevbae9a792014-06-27 10:37:06 +00008292 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8293 // A variable of class type (or array thereof) that appears in a
8294 // copyin clause requires an accessible, unambiguous copy assignment
8295 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008296 Type = Context.getBaseElementType(Type.getNonReferenceType())
8297 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008298 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008299 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8300 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008301 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008302 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008303 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008304 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8305 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008306 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008307 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008308 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8309 PseudoDstExpr, PseudoSrcExpr);
8310 if (AssignmentOp.isInvalid())
8311 continue;
8312 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8313 /*DiscardedValue=*/true);
8314 if (AssignmentOp.isInvalid())
8315 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008316
8317 // No need to mark vars as copyprivate, they are already threadprivate or
8318 // implicitly private.
8319 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008320 SrcExprs.push_back(PseudoSrcExpr);
8321 DstExprs.push_back(PseudoDstExpr);
8322 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008323 }
8324
8325 if (Vars.empty())
8326 return nullptr;
8327
Alexey Bataeva63048e2015-03-23 06:18:07 +00008328 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8329 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008330}
8331
Alexey Bataev6125da92014-07-21 11:26:11 +00008332OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8333 SourceLocation StartLoc,
8334 SourceLocation LParenLoc,
8335 SourceLocation EndLoc) {
8336 if (VarList.empty())
8337 return nullptr;
8338
8339 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8340}
Alexey Bataevdea47612014-07-23 07:46:59 +00008341
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008342OMPClause *
8343Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8344 SourceLocation DepLoc, SourceLocation ColonLoc,
8345 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8346 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008347 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008348 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008349 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008350 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008351 return nullptr;
8352 }
8353 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008354 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8355 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008356 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008357 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008358 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8359 /*Last=*/OMPC_DEPEND_unknown, Except)
8360 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008361 return nullptr;
8362 }
8363 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008364 llvm::APSInt DepCounter(/*BitWidth=*/32);
8365 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8366 if (DepKind == OMPC_DEPEND_sink) {
8367 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8368 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8369 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008370 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008371 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008372 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8373 DSAStack->getParentOrderedRegionParam()) {
8374 for (auto &RefExpr : VarList) {
8375 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8376 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8377 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8378 // It will be analyzed later.
8379 Vars.push_back(RefExpr);
8380 continue;
8381 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008382
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008383 SourceLocation ELoc = RefExpr->getExprLoc();
8384 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8385 if (DepKind == OMPC_DEPEND_sink) {
8386 if (DepCounter >= TotalDepCount) {
8387 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8388 continue;
8389 }
8390 ++DepCounter;
8391 // OpenMP [2.13.9, Summary]
8392 // depend(dependence-type : vec), where dependence-type is:
8393 // 'sink' and where vec is the iteration vector, which has the form:
8394 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8395 // where n is the value specified by the ordered clause in the loop
8396 // directive, xi denotes the loop iteration variable of the i-th nested
8397 // loop associated with the loop directive, and di is a constant
8398 // non-negative integer.
8399 SimpleExpr = SimpleExpr->IgnoreImplicit();
8400 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8401 if (!DE) {
8402 OverloadedOperatorKind OOK = OO_None;
8403 SourceLocation OOLoc;
8404 Expr *LHS, *RHS;
8405 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8406 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8407 OOLoc = BO->getOperatorLoc();
8408 LHS = BO->getLHS()->IgnoreParenImpCasts();
8409 RHS = BO->getRHS()->IgnoreParenImpCasts();
8410 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8411 OOK = OCE->getOperator();
8412 OOLoc = OCE->getOperatorLoc();
8413 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8414 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8415 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8416 OOK = MCE->getMethodDecl()
8417 ->getNameInfo()
8418 .getName()
8419 .getCXXOverloadedOperator();
8420 OOLoc = MCE->getCallee()->getExprLoc();
8421 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8422 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8423 } else {
8424 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8425 continue;
8426 }
8427 DE = dyn_cast<DeclRefExpr>(LHS);
8428 if (!DE) {
8429 Diag(LHS->getExprLoc(),
8430 diag::err_omp_depend_sink_expected_loop_iteration)
8431 << DSAStack->getParentLoopControlVariable(
8432 DepCounter.getZExtValue());
8433 continue;
8434 }
8435 if (OOK != OO_Plus && OOK != OO_Minus) {
8436 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8437 continue;
8438 }
8439 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8440 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8441 if (Res.isInvalid())
8442 continue;
8443 }
8444 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8445 if (!CurContext->isDependentContext() &&
8446 DSAStack->getParentOrderedRegionParam() &&
8447 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8448 Diag(DE->getExprLoc(),
8449 diag::err_omp_depend_sink_expected_loop_iteration)
8450 << DSAStack->getParentLoopControlVariable(
8451 DepCounter.getZExtValue());
8452 continue;
8453 }
8454 } else {
8455 // OpenMP [2.11.1.1, Restrictions, p.3]
8456 // A variable that is part of another variable (such as a field of a
8457 // structure) but is not an array element or an array section cannot
8458 // appear in a depend clause.
8459 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8460 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8461 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8462 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8463 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8464 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8465 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008466 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8467 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008468 continue;
8469 }
8470 }
8471
8472 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8473 }
8474
8475 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8476 TotalDepCount > VarList.size() &&
8477 DSAStack->getParentOrderedRegionParam()) {
8478 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8479 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8480 }
8481 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8482 Vars.empty())
8483 return nullptr;
8484 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008485
8486 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8487 DepLoc, ColonLoc, Vars);
8488}
Michael Wonge710d542015-08-07 16:16:36 +00008489
8490OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8491 SourceLocation LParenLoc,
8492 SourceLocation EndLoc) {
8493 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008494
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008495 // OpenMP [2.9.1, Restrictions]
8496 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008497 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8498 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008499 return nullptr;
8500
Michael Wonge710d542015-08-07 16:16:36 +00008501 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8502}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008503
8504static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8505 DSAStackTy *Stack, CXXRecordDecl *RD) {
8506 if (!RD || RD->isInvalidDecl())
8507 return true;
8508
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008509 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8510 if (auto *CTD = CTSD->getSpecializedTemplate())
8511 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008512 auto QTy = SemaRef.Context.getRecordType(RD);
8513 if (RD->isDynamicClass()) {
8514 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8515 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8516 return false;
8517 }
8518 auto *DC = RD;
8519 bool IsCorrect = true;
8520 for (auto *I : DC->decls()) {
8521 if (I) {
8522 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8523 if (MD->isStatic()) {
8524 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8525 SemaRef.Diag(MD->getLocation(),
8526 diag::note_omp_static_member_in_target);
8527 IsCorrect = false;
8528 }
8529 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8530 if (VD->isStaticDataMember()) {
8531 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8532 SemaRef.Diag(VD->getLocation(),
8533 diag::note_omp_static_member_in_target);
8534 IsCorrect = false;
8535 }
8536 }
8537 }
8538 }
8539
8540 for (auto &I : RD->bases()) {
8541 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8542 I.getType()->getAsCXXRecordDecl()))
8543 IsCorrect = false;
8544 }
8545 return IsCorrect;
8546}
8547
8548static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8549 DSAStackTy *Stack, QualType QTy) {
8550 NamedDecl *ND;
8551 if (QTy->isIncompleteType(&ND)) {
8552 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8553 return false;
8554 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8555 if (!RD->isInvalidDecl() &&
8556 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8557 return false;
8558 }
8559 return true;
8560}
8561
Samuel Antao5de996e2016-01-22 20:21:36 +00008562// Return the expression of the base of the map clause or null if it cannot
8563// be determined and do all the necessary checks to see if the expression is
8564// valid as a standalone map clause expression.
8565static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8566 SourceLocation ELoc = E->getExprLoc();
8567 SourceRange ERange = E->getSourceRange();
8568
8569 // The base of elements of list in a map clause have to be either:
8570 // - a reference to variable or field.
8571 // - a member expression.
8572 // - an array expression.
8573 //
8574 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8575 // reference to 'r'.
8576 //
8577 // If we have:
8578 //
8579 // struct SS {
8580 // Bla S;
8581 // foo() {
8582 // #pragma omp target map (S.Arr[:12]);
8583 // }
8584 // }
8585 //
8586 // We want to retrieve the member expression 'this->S';
8587
8588 Expr *RelevantExpr = nullptr;
8589
8590 // Flags to help capture some memory
8591
8592 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8593 // If a list item is an array section, it must specify contiguous storage.
8594 //
8595 // For this restriction it is sufficient that we make sure only references
8596 // to variables or fields and array expressions, and that no array sections
8597 // exist except in the rightmost expression. E.g. these would be invalid:
8598 //
8599 // r.ArrS[3:5].Arr[6:7]
8600 //
8601 // r.ArrS[3:5].x
8602 //
8603 // but these would be valid:
8604 // r.ArrS[3].Arr[6:7]
8605 //
8606 // r.ArrS[3].x
8607
8608 bool IsRightMostExpression = true;
8609
8610 while (!RelevantExpr) {
8611 auto AllowArraySection = IsRightMostExpression;
8612 IsRightMostExpression = false;
8613
8614 E = E->IgnoreParenImpCasts();
8615
8616 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8617 if (!isa<VarDecl>(CurE->getDecl()))
8618 break;
8619
8620 RelevantExpr = CurE;
8621 continue;
8622 }
8623
8624 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8625 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8626
8627 if (isa<CXXThisExpr>(BaseE))
8628 // We found a base expression: this->Val.
8629 RelevantExpr = CurE;
8630 else
8631 E = BaseE;
8632
8633 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8634 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8635 << CurE->getSourceRange();
8636 break;
8637 }
8638
8639 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8640
8641 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8642 // A bit-field cannot appear in a map clause.
8643 //
8644 if (FD->isBitField()) {
8645 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8646 << CurE->getSourceRange();
8647 break;
8648 }
8649
8650 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8651 // If the type of a list item is a reference to a type T then the type
8652 // will be considered to be T for all purposes of this clause.
8653 QualType CurType = BaseE->getType();
8654 if (CurType->isReferenceType())
8655 CurType = CurType->getPointeeType();
8656
8657 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8658 // A list item cannot be a variable that is a member of a structure with
8659 // a union type.
8660 //
8661 if (auto *RT = CurType->getAs<RecordType>())
8662 if (RT->isUnionType()) {
8663 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
8664 << CurE->getSourceRange();
8665 break;
8666 }
8667
8668 continue;
8669 }
8670
8671 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
8672 E = CurE->getBase()->IgnoreParenImpCasts();
8673
8674 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
8675 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8676 << 0 << CurE->getSourceRange();
8677 break;
8678 }
8679 continue;
8680 }
8681
8682 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
8683 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
8684 // If a list item is an element of a structure, only the rightmost symbol
8685 // of the variable reference can be an array section.
8686 //
8687 if (!AllowArraySection) {
8688 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
8689 << CurE->getSourceRange();
8690 break;
8691 }
8692
8693 E = CurE->getBase()->IgnoreParenImpCasts();
8694
8695 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8696 // If the type of a list item is a reference to a type T then the type
8697 // will be considered to be T for all purposes of this clause.
8698 QualType CurType = E->getType();
8699 if (CurType->isReferenceType())
8700 CurType = CurType->getPointeeType();
8701
8702 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
8703 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8704 << 0 << CurE->getSourceRange();
8705 break;
8706 }
8707
8708 continue;
8709 }
8710
8711 // If nothing else worked, this is not a valid map clause expression.
8712 SemaRef.Diag(ELoc,
8713 diag::err_omp_expected_named_var_member_or_array_expression)
8714 << ERange;
8715 break;
8716 }
8717
8718 return RelevantExpr;
8719}
8720
8721// Return true if expression E associated with value VD has conflicts with other
8722// map information.
8723static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
8724 Expr *E, bool CurrentRegionOnly) {
8725 assert(VD && E);
8726
8727 // Types used to organize the components of a valid map clause.
8728 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
8729 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
8730
8731 // Helper to extract the components in the map clause expression E and store
8732 // them into MEC. This assumes that E is a valid map clause expression, i.e.
8733 // it has already passed the single clause checks.
8734 auto ExtractMapExpressionComponents = [](Expr *TE,
8735 MapExpressionComponents &MEC) {
8736 while (true) {
8737 TE = TE->IgnoreParenImpCasts();
8738
8739 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
8740 MEC.push_back(
8741 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
8742 break;
8743 }
8744
8745 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
8746 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8747
8748 MEC.push_back(MapExpressionComponent(
8749 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
8750 if (isa<CXXThisExpr>(BaseE))
8751 break;
8752
8753 TE = BaseE;
8754 continue;
8755 }
8756
8757 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
8758 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8759 TE = CurE->getBase()->IgnoreParenImpCasts();
8760 continue;
8761 }
8762
8763 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
8764 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8765 TE = CurE->getBase()->IgnoreParenImpCasts();
8766 continue;
8767 }
8768
8769 llvm_unreachable(
8770 "Expecting only valid map clause expressions at this point!");
8771 }
8772 };
8773
8774 SourceLocation ELoc = E->getExprLoc();
8775 SourceRange ERange = E->getSourceRange();
8776
8777 // In order to easily check the conflicts we need to match each component of
8778 // the expression under test with the components of the expressions that are
8779 // already in the stack.
8780
8781 MapExpressionComponents CurComponents;
8782 ExtractMapExpressionComponents(E, CurComponents);
8783
8784 assert(!CurComponents.empty() && "Map clause expression with no components!");
8785 assert(CurComponents.back().second == VD &&
8786 "Map clause expression with unexpected base!");
8787
8788 // Variables to help detecting enclosing problems in data environment nests.
8789 bool IsEnclosedByDataEnvironmentExpr = false;
8790 Expr *EnclosingExpr = nullptr;
8791
8792 bool FoundError =
8793 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
8794 MapExpressionComponents StackComponents;
8795 ExtractMapExpressionComponents(RE, StackComponents);
8796 assert(!StackComponents.empty() &&
8797 "Map clause expression with no components!");
8798 assert(StackComponents.back().second == VD &&
8799 "Map clause expression with unexpected base!");
8800
8801 // Expressions must start from the same base. Here we detect at which
8802 // point both expressions diverge from each other and see if we can
8803 // detect if the memory referred to both expressions is contiguous and
8804 // do not overlap.
8805 auto CI = CurComponents.rbegin();
8806 auto CE = CurComponents.rend();
8807 auto SI = StackComponents.rbegin();
8808 auto SE = StackComponents.rend();
8809 for (; CI != CE && SI != SE; ++CI, ++SI) {
8810
8811 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
8812 // At most one list item can be an array item derived from a given
8813 // variable in map clauses of the same construct.
8814 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
8815 isa<OMPArraySectionExpr>(CI->first)) &&
8816 (isa<ArraySubscriptExpr>(SI->first) ||
8817 isa<OMPArraySectionExpr>(SI->first))) {
8818 SemaRef.Diag(CI->first->getExprLoc(),
8819 diag::err_omp_multiple_array_items_in_map_clause)
8820 << CI->first->getSourceRange();
8821 ;
8822 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
8823 << SI->first->getSourceRange();
8824 return true;
8825 }
8826
8827 // Do both expressions have the same kind?
8828 if (CI->first->getStmtClass() != SI->first->getStmtClass())
8829 break;
8830
8831 // Are we dealing with different variables/fields?
8832 if (CI->second != SI->second)
8833 break;
8834 }
8835
8836 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8837 // List items of map clauses in the same construct must not share
8838 // original storage.
8839 //
8840 // If the expressions are exactly the same or one is a subset of the
8841 // other, it means they are sharing storage.
8842 if (CI == CE && SI == SE) {
8843 if (CurrentRegionOnly) {
8844 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8845 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8846 << RE->getSourceRange();
8847 return true;
8848 } else {
8849 // If we find the same expression in the enclosing data environment,
8850 // that is legal.
8851 IsEnclosedByDataEnvironmentExpr = true;
8852 return false;
8853 }
8854 }
8855
8856 QualType DerivedType = std::prev(CI)->first->getType();
8857 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
8858
8859 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8860 // If the type of a list item is a reference to a type T then the type
8861 // will be considered to be T for all purposes of this clause.
8862 if (DerivedType->isReferenceType())
8863 DerivedType = DerivedType->getPointeeType();
8864
8865 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
8866 // A variable for which the type is pointer and an array section
8867 // derived from that variable must not appear as list items of map
8868 // clauses of the same construct.
8869 //
8870 // Also, cover one of the cases in:
8871 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
8872 // If any part of the original storage of a list item has corresponding
8873 // storage in the device data environment, all of the original storage
8874 // must have corresponding storage in the device data environment.
8875 //
8876 if (DerivedType->isAnyPointerType()) {
8877 if (CI == CE || SI == SE) {
8878 SemaRef.Diag(
8879 DerivedLoc,
8880 diag::err_omp_pointer_mapped_along_with_derived_section)
8881 << DerivedLoc;
8882 } else {
8883 assert(CI != CE && SI != SE);
8884 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
8885 << DerivedLoc;
8886 }
8887 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8888 << RE->getSourceRange();
8889 return true;
8890 }
8891
8892 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8893 // List items of map clauses in the same construct must not share
8894 // original storage.
8895 //
8896 // An expression is a subset of the other.
8897 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
8898 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8899 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8900 << RE->getSourceRange();
8901 return true;
8902 }
8903
8904 // The current expression uses the same base as other expression in the
8905 // data environment but does not contain it completelly.
8906 if (!CurrentRegionOnly && SI != SE)
8907 EnclosingExpr = RE;
8908
8909 // The current expression is a subset of the expression in the data
8910 // environment.
8911 IsEnclosedByDataEnvironmentExpr |=
8912 (!CurrentRegionOnly && CI != CE && SI == SE);
8913
8914 return false;
8915 });
8916
8917 if (CurrentRegionOnly)
8918 return FoundError;
8919
8920 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
8921 // If any part of the original storage of a list item has corresponding
8922 // storage in the device data environment, all of the original storage must
8923 // have corresponding storage in the device data environment.
8924 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
8925 // If a list item is an element of a structure, and a different element of
8926 // the structure has a corresponding list item in the device data environment
8927 // prior to a task encountering the construct associated with the map clause,
8928 // then the list item must also have a correspnding list item in the device
8929 // data environment prior to the task encountering the construct.
8930 //
8931 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
8932 SemaRef.Diag(ELoc,
8933 diag::err_omp_original_storage_is_shared_and_does_not_contain)
8934 << ERange;
8935 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
8936 << EnclosingExpr->getSourceRange();
8937 return true;
8938 }
8939
8940 return FoundError;
8941}
8942
Samuel Antao23abd722016-01-19 20:40:49 +00008943OMPClause *
8944Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
8945 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8946 SourceLocation MapLoc, SourceLocation ColonLoc,
8947 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8948 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00008949 SmallVector<Expr *, 4> Vars;
8950
8951 for (auto &RE : VarList) {
8952 assert(RE && "Null expr in omp map");
8953 if (isa<DependentScopeDeclRefExpr>(RE)) {
8954 // It will be analyzed later.
8955 Vars.push_back(RE);
8956 continue;
8957 }
8958 SourceLocation ELoc = RE->getExprLoc();
8959
Kelvin Li0bff7af2015-11-23 05:32:03 +00008960 auto *VE = RE->IgnoreParenLValueCasts();
8961
8962 if (VE->isValueDependent() || VE->isTypeDependent() ||
8963 VE->isInstantiationDependent() ||
8964 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00008965 // We can only analyze this information once the missing information is
8966 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00008967 Vars.push_back(RE);
8968 continue;
8969 }
8970
8971 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008972
Samuel Antao5de996e2016-01-22 20:21:36 +00008973 if (!RE->IgnoreParenImpCasts()->isLValue()) {
8974 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
8975 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008976 continue;
8977 }
8978
Samuel Antao5de996e2016-01-22 20:21:36 +00008979 // Obtain the array or member expression bases if required.
8980 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
8981 if (!BE)
8982 continue;
8983
8984 // If the base is a reference to a variable, we rely on that variable for
8985 // the following checks. If it is a 'this' expression we rely on the field.
8986 ValueDecl *D = nullptr;
8987 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
8988 D = DRE->getDecl();
8989 } else {
8990 auto *ME = cast<MemberExpr>(BE);
8991 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
8992 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008993 }
8994 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00008995
Samuel Antao5de996e2016-01-22 20:21:36 +00008996 auto *VD = dyn_cast<VarDecl>(D);
8997 auto *FD = dyn_cast<FieldDecl>(D);
8998
8999 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009000 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009001
9002 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9003 // threadprivate variables cannot appear in a map clause.
9004 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009005 auto DVar = DSAStack->getTopDSA(VD, false);
9006 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9007 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9008 continue;
9009 }
9010
Samuel Antao5de996e2016-01-22 20:21:36 +00009011 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9012 // A list item cannot appear in both a map clause and a data-sharing
9013 // attribute clause on the same construct.
9014 //
9015 // TODO: Implement this check - it cannot currently be tested because of
9016 // missing implementation of the other data sharing clauses in target
9017 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009018
Samuel Antao5de996e2016-01-22 20:21:36 +00009019 // Check conflicts with other map clause expressions. We check the conflicts
9020 // with the current construct separately from the enclosing data
9021 // environment, because the restrictions are different.
9022 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9023 /*CurrentRegionOnly=*/true))
9024 break;
9025 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9026 /*CurrentRegionOnly=*/false))
9027 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009028
Samuel Antao5de996e2016-01-22 20:21:36 +00009029 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9030 // If the type of a list item is a reference to a type T then the type will
9031 // be considered to be T for all purposes of this clause.
9032 QualType Type = D->getType();
9033 if (Type->isReferenceType())
9034 Type = Type->getPointeeType();
9035
9036 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009037 // A list item must have a mappable type.
9038 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9039 DSAStack, Type))
9040 continue;
9041
Samuel Antaodf67fc42016-01-19 19:15:56 +00009042 // target enter data
9043 // OpenMP [2.10.2, Restrictions, p. 99]
9044 // A map-type must be specified in all map clauses and must be either
9045 // to or alloc.
9046 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9047 if (DKind == OMPD_target_enter_data &&
9048 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9049 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009050 << (IsMapTypeImplicit ? 1 : 0)
9051 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009052 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009053 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009054 }
9055
Samuel Antao72590762016-01-19 20:04:50 +00009056 // target exit_data
9057 // OpenMP [2.10.3, Restrictions, p. 102]
9058 // A map-type must be specified in all map clauses and must be either
9059 // from, release, or delete.
9060 DKind = DSAStack->getCurrentDirective();
9061 if (DKind == OMPD_target_exit_data &&
9062 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9063 MapType == OMPC_MAP_delete)) {
9064 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009065 << (IsMapTypeImplicit ? 1 : 0)
9066 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009067 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009068 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009069 }
9070
Kelvin Li0bff7af2015-11-23 05:32:03 +00009071 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009072 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009073 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009074
Samuel Antao5de996e2016-01-22 20:21:36 +00009075 // We need to produce a map clause even if we don't have variables so that
9076 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009077 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009078 MapTypeModifier, MapType, IsMapTypeImplicit,
9079 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009080}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009081
9082OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9083 SourceLocation StartLoc,
9084 SourceLocation LParenLoc,
9085 SourceLocation EndLoc) {
9086 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009087
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009088 // OpenMP [teams Constrcut, Restrictions]
9089 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009090 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9091 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009092 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009093
9094 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9095}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009096
9097OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9098 SourceLocation StartLoc,
9099 SourceLocation LParenLoc,
9100 SourceLocation EndLoc) {
9101 Expr *ValExpr = ThreadLimit;
9102
9103 // OpenMP [teams Constrcut, Restrictions]
9104 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009105 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9106 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009107 return nullptr;
9108
9109 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9110 EndLoc);
9111}
Alexey Bataeva0569352015-12-01 10:17:31 +00009112
9113OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9114 SourceLocation StartLoc,
9115 SourceLocation LParenLoc,
9116 SourceLocation EndLoc) {
9117 Expr *ValExpr = Priority;
9118
9119 // OpenMP [2.9.1, task Constrcut]
9120 // The priority-value is a non-negative numerical scalar expression.
9121 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9122 /*StrictlyPositive=*/false))
9123 return nullptr;
9124
9125 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9126}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009127
9128OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9129 SourceLocation StartLoc,
9130 SourceLocation LParenLoc,
9131 SourceLocation EndLoc) {
9132 Expr *ValExpr = Grainsize;
9133
9134 // OpenMP [2.9.2, taskloop Constrcut]
9135 // The parameter of the grainsize clause must be a positive integer
9136 // expression.
9137 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9138 /*StrictlyPositive=*/true))
9139 return nullptr;
9140
9141 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9142}
Alexey Bataev382967a2015-12-08 12:06:20 +00009143
9144OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9145 SourceLocation StartLoc,
9146 SourceLocation LParenLoc,
9147 SourceLocation EndLoc) {
9148 Expr *ValExpr = NumTasks;
9149
9150 // OpenMP [2.9.2, taskloop Constrcut]
9151 // The parameter of the num_tasks clause must be a positive integer
9152 // expression.
9153 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9154 /*StrictlyPositive=*/true))
9155 return nullptr;
9156
9157 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9158}
9159
Alexey Bataev28c75412015-12-15 08:19:24 +00009160OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9161 SourceLocation LParenLoc,
9162 SourceLocation EndLoc) {
9163 // OpenMP [2.13.2, critical construct, Description]
9164 // ... where hint-expression is an integer constant expression that evaluates
9165 // to a valid lock hint.
9166 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9167 if (HintExpr.isInvalid())
9168 return nullptr;
9169 return new (Context)
9170 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9171}
9172
Carlo Bertollib4adf552016-01-15 18:50:31 +00009173OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9174 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9175 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9176 SourceLocation EndLoc) {
9177 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9178 std::string Values;
9179 Values += "'";
9180 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9181 Values += "'";
9182 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9183 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9184 return nullptr;
9185 }
9186 Expr *ValExpr = ChunkSize;
9187 Expr *HelperValExpr = nullptr;
9188 if (ChunkSize) {
9189 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9190 !ChunkSize->isInstantiationDependent() &&
9191 !ChunkSize->containsUnexpandedParameterPack()) {
9192 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9193 ExprResult Val =
9194 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9195 if (Val.isInvalid())
9196 return nullptr;
9197
9198 ValExpr = Val.get();
9199
9200 // OpenMP [2.7.1, Restrictions]
9201 // chunk_size must be a loop invariant integer expression with a positive
9202 // value.
9203 llvm::APSInt Result;
9204 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9205 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9206 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9207 << "dist_schedule" << ChunkSize->getSourceRange();
9208 return nullptr;
9209 }
9210 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9211 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9212 ChunkSize->getType(), ".chunk.");
9213 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9214 ChunkSize->getExprLoc(),
9215 /*RefersToCapture=*/true);
9216 HelperValExpr = ImpVarRef;
9217 }
9218 }
9219 }
9220
9221 return new (Context)
9222 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9223 Kind, ValExpr, HelperValExpr);
9224}