blob: 577772301f0770e163d751e0b3b78df46ec48b51 [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:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001631 case OMPD_target:
1632 case OMPD_target_parallel: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001633 Sema::CapturedParamNameType Params[] = {
1634 std::make_pair(StringRef(), QualType()) // __context with shared vars
1635 };
1636 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1637 Params);
1638 break;
1639 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001640 case OMPD_teams: {
1641 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001642 QualType KmpInt32PtrTy =
1643 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001644 Sema::CapturedParamNameType Params[] = {
1645 std::make_pair(".global_tid.", KmpInt32PtrTy),
1646 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1647 std::make_pair(StringRef(), QualType()) // __context with shared vars
1648 };
1649 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1650 Params);
1651 break;
1652 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001653 case OMPD_taskgroup: {
1654 Sema::CapturedParamNameType Params[] = {
1655 std::make_pair(StringRef(), QualType()) // __context with shared vars
1656 };
1657 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1658 Params);
1659 break;
1660 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001661 case OMPD_taskloop: {
1662 Sema::CapturedParamNameType Params[] = {
1663 std::make_pair(StringRef(), QualType()) // __context with shared vars
1664 };
1665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1666 Params);
1667 break;
1668 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001669 case OMPD_taskloop_simd: {
1670 Sema::CapturedParamNameType Params[] = {
1671 std::make_pair(StringRef(), QualType()) // __context with shared vars
1672 };
1673 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1674 Params);
1675 break;
1676 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001677 case OMPD_distribute: {
1678 Sema::CapturedParamNameType Params[] = {
1679 std::make_pair(StringRef(), QualType()) // __context with shared vars
1680 };
1681 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1682 Params);
1683 break;
1684 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001685 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001686 case OMPD_taskyield:
1687 case OMPD_barrier:
1688 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001689 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001690 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001691 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001692 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001693 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001694 llvm_unreachable("OpenMP Directive is not allowed");
1695 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001696 llvm_unreachable("Unknown OpenMP directive");
1697 }
1698}
1699
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001700StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1701 ArrayRef<OMPClause *> Clauses) {
1702 if (!S.isUsable()) {
1703 ActOnCapturedRegionError();
1704 return StmtError();
1705 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001706
1707 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001708 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001709 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001710 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001711 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001712 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001713 Clause->getClauseKind() == OMPC_copyprivate ||
1714 (getLangOpts().OpenMPUseTLS &&
1715 getASTContext().getTargetInfo().isTLSSupported() &&
1716 Clause->getClauseKind() == OMPC_copyin)) {
1717 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001718 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001719 for (auto *VarRef : Clause->children()) {
1720 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001721 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001722 }
1723 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001724 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001725 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1726 Clause->getClauseKind() == OMPC_schedule) {
1727 // Mark all variables in private list clauses as used in inner region.
1728 // Required for proper codegen of combined directives.
1729 // TODO: add processing for other clauses.
1730 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001731 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1732 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001733 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001734 if (Clause->getClauseKind() == OMPC_schedule)
1735 SC = cast<OMPScheduleClause>(Clause);
1736 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001737 OC = cast<OMPOrderedClause>(Clause);
1738 else if (Clause->getClauseKind() == OMPC_linear)
1739 LCs.push_back(cast<OMPLinearClause>(Clause));
1740 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001741 bool ErrorFound = false;
1742 // OpenMP, 2.7.1 Loop Construct, Restrictions
1743 // The nonmonotonic modifier cannot be specified if an ordered clause is
1744 // specified.
1745 if (SC &&
1746 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1747 SC->getSecondScheduleModifier() ==
1748 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1749 OC) {
1750 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1751 ? SC->getFirstScheduleModifierLoc()
1752 : SC->getSecondScheduleModifierLoc(),
1753 diag::err_omp_schedule_nonmonotonic_ordered)
1754 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1755 ErrorFound = true;
1756 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001757 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1758 for (auto *C : LCs) {
1759 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1760 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1761 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001762 ErrorFound = true;
1763 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001764 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1765 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1766 OC->getNumForLoops()) {
1767 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1768 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1769 ErrorFound = true;
1770 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001771 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001772 ActOnCapturedRegionError();
1773 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001774 }
1775 return ActOnCapturedRegionEnd(S.get());
1776}
1777
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001778static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1779 OpenMPDirectiveKind CurrentRegion,
1780 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001781 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001782 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001783 // Allowed nesting of constructs
1784 // +------------------+-----------------+------------------------------------+
1785 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1786 // +------------------+-----------------+------------------------------------+
1787 // | parallel | parallel | * |
1788 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001789 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001790 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001791 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001792 // | parallel | simd | * |
1793 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001794 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001795 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001796 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001797 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001798 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001799 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001800 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001801 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001802 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001803 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001804 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001805 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001806 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001807 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001808 // | parallel | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001809 // | parallel | target enter | * |
1810 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001811 // | parallel | target exit | * |
1812 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001813 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001814 // | parallel | cancellation | |
1815 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001816 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001817 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001818 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001819 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001820 // +------------------+-----------------+------------------------------------+
1821 // | for | parallel | * |
1822 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001823 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001824 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001825 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001826 // | for | simd | * |
1827 // | for | sections | + |
1828 // | for | section | + |
1829 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001830 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001831 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001832 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001833 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001834 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001835 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001836 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001837 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001838 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001839 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001840 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001841 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001842 // | for | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001843 // | for | target enter | * |
1844 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001845 // | for | target exit | * |
1846 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001847 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001848 // | for | cancellation | |
1849 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001850 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001851 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001852 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001853 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001854 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001855 // | master | parallel | * |
1856 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001857 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001858 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001859 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001860 // | master | simd | * |
1861 // | master | sections | + |
1862 // | master | section | + |
1863 // | master | single | + |
1864 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001865 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001866 // | master |parallel sections| * |
1867 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001868 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001869 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001870 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001871 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001872 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001873 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001874 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001875 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001876 // | master | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001877 // | master | target enter | * |
1878 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001879 // | master | target exit | * |
1880 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001881 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001882 // | master | cancellation | |
1883 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001884 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001885 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001886 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001887 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001888 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001889 // | critical | parallel | * |
1890 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001891 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001892 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001893 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001894 // | critical | simd | * |
1895 // | critical | sections | + |
1896 // | critical | section | + |
1897 // | critical | single | + |
1898 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001899 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001900 // | critical |parallel sections| * |
1901 // | critical | task | * |
1902 // | critical | taskyield | * |
1903 // | critical | barrier | + |
1904 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001905 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001906 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001907 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001908 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001909 // | critical | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001910 // | critical | target enter | * |
1911 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001912 // | critical | target exit | * |
1913 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001914 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001915 // | critical | cancellation | |
1916 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001917 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001918 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001919 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001920 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001921 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001922 // | simd | parallel | |
1923 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001924 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001925 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001926 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001927 // | simd | simd | |
1928 // | simd | sections | |
1929 // | simd | section | |
1930 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001931 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001932 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001933 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001934 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001935 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001936 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001937 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001938 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001939 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001940 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001941 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001942 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001943 // | simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001944 // | simd | target enter | |
1945 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001946 // | simd | target exit | |
1947 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001948 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001949 // | simd | cancellation | |
1950 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001951 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001952 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001953 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001954 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001955 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001956 // | for simd | parallel | |
1957 // | for simd | for | |
1958 // | for simd | for simd | |
1959 // | for simd | master | |
1960 // | for simd | critical | |
1961 // | for simd | simd | |
1962 // | for simd | sections | |
1963 // | for simd | section | |
1964 // | for simd | single | |
1965 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001966 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001967 // | for simd |parallel sections| |
1968 // | for simd | task | |
1969 // | for simd | taskyield | |
1970 // | for simd | barrier | |
1971 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001972 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001973 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001974 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001975 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001976 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001977 // | for simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001978 // | for simd | target enter | |
1979 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001980 // | for simd | target exit | |
1981 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001982 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001983 // | for simd | cancellation | |
1984 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001985 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001986 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001987 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001988 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001989 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001990 // | parallel for simd| parallel | |
1991 // | parallel for simd| for | |
1992 // | parallel for simd| for simd | |
1993 // | parallel for simd| master | |
1994 // | parallel for simd| critical | |
1995 // | parallel for simd| simd | |
1996 // | parallel for simd| sections | |
1997 // | parallel for simd| section | |
1998 // | parallel for simd| single | |
1999 // | parallel for simd| parallel for | |
2000 // | parallel for simd|parallel for simd| |
2001 // | parallel for simd|parallel sections| |
2002 // | parallel for simd| task | |
2003 // | parallel for simd| taskyield | |
2004 // | parallel for simd| barrier | |
2005 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002006 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002007 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002008 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002009 // | parallel for simd| atomic | |
2010 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002011 // | parallel for simd| target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002012 // | parallel for simd| target enter | |
2013 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002014 // | parallel for simd| target exit | |
2015 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002016 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002017 // | parallel for simd| cancellation | |
2018 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002019 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002020 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002021 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002022 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002023 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002024 // | sections | parallel | * |
2025 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002026 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002027 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002028 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002029 // | sections | simd | * |
2030 // | sections | sections | + |
2031 // | sections | section | * |
2032 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002033 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002034 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002035 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002036 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002037 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002038 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002039 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002040 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002041 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002042 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002043 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002044 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002045 // | sections | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002046 // | sections | target enter | * |
2047 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002048 // | sections | target exit | * |
2049 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002050 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002051 // | sections | cancellation | |
2052 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002053 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002054 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002055 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002056 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002057 // +------------------+-----------------+------------------------------------+
2058 // | section | parallel | * |
2059 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002060 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002061 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002062 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002063 // | section | simd | * |
2064 // | section | sections | + |
2065 // | section | section | + |
2066 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002067 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002068 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002069 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002070 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002071 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002072 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002073 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002074 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002075 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002076 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002077 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002078 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002079 // | section | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002080 // | section | target enter | * |
2081 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002082 // | section | target exit | * |
2083 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002084 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002085 // | section | cancellation | |
2086 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002087 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002088 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002089 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002090 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002091 // +------------------+-----------------+------------------------------------+
2092 // | single | parallel | * |
2093 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002094 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002095 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002096 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002097 // | single | simd | * |
2098 // | single | sections | + |
2099 // | single | section | + |
2100 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002101 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002102 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002103 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002104 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002105 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002106 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002107 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002108 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002109 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002110 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002111 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002112 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002113 // | single | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002114 // | single | target enter | * |
2115 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002116 // | single | target exit | * |
2117 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002118 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002119 // | single | cancellation | |
2120 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002121 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002122 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002123 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002124 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002125 // +------------------+-----------------+------------------------------------+
2126 // | parallel for | parallel | * |
2127 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002128 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002129 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002130 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002131 // | parallel for | simd | * |
2132 // | parallel for | sections | + |
2133 // | parallel for | section | + |
2134 // | parallel for | single | + |
2135 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002136 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002137 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002138 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002139 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002140 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002141 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002142 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002143 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002144 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002145 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002146 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002147 // | parallel for | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002148 // | parallel for | target enter | * |
2149 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002150 // | parallel for | target exit | * |
2151 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002152 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002153 // | parallel for | cancellation | |
2154 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002155 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002156 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002157 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002158 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002159 // +------------------+-----------------+------------------------------------+
2160 // | parallel sections| parallel | * |
2161 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002162 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002163 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002164 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002165 // | parallel sections| simd | * |
2166 // | parallel sections| sections | + |
2167 // | parallel sections| section | * |
2168 // | parallel sections| single | + |
2169 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002170 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002171 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002172 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002173 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002174 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002175 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002176 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002177 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002178 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002179 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002180 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002181 // | parallel sections| target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002182 // | parallel sections| target enter | * |
2183 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002184 // | parallel sections| target exit | * |
2185 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002186 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002187 // | parallel sections| cancellation | |
2188 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002189 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002190 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002191 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002192 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002193 // +------------------+-----------------+------------------------------------+
2194 // | task | parallel | * |
2195 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002196 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002197 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002198 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002199 // | task | simd | * |
2200 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002201 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002202 // | task | single | + |
2203 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002204 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002205 // | task |parallel sections| * |
2206 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002207 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002208 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002209 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002210 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002211 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002212 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002213 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002214 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002215 // | task | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002216 // | task | target enter | * |
2217 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002218 // | task | target exit | * |
2219 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002220 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002221 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002222 // | | point | ! |
2223 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002224 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002225 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002226 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002227 // +------------------+-----------------+------------------------------------+
2228 // | ordered | parallel | * |
2229 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002230 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002231 // | ordered | master | * |
2232 // | ordered | critical | * |
2233 // | ordered | simd | * |
2234 // | ordered | sections | + |
2235 // | ordered | section | + |
2236 // | ordered | single | + |
2237 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002238 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002239 // | ordered |parallel sections| * |
2240 // | ordered | task | * |
2241 // | ordered | taskyield | * |
2242 // | ordered | barrier | + |
2243 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002244 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002245 // | ordered | flush | * |
2246 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002247 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002248 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002249 // | ordered | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002250 // | ordered | target enter | * |
2251 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002252 // | ordered | target exit | * |
2253 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002254 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002255 // | ordered | cancellation | |
2256 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002257 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002258 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002259 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002260 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002261 // +------------------+-----------------+------------------------------------+
2262 // | atomic | parallel | |
2263 // | atomic | for | |
2264 // | atomic | for simd | |
2265 // | atomic | master | |
2266 // | atomic | critical | |
2267 // | atomic | simd | |
2268 // | atomic | sections | |
2269 // | atomic | section | |
2270 // | atomic | single | |
2271 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002272 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002273 // | atomic |parallel sections| |
2274 // | atomic | task | |
2275 // | atomic | taskyield | |
2276 // | atomic | barrier | |
2277 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002278 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002279 // | atomic | flush | |
2280 // | atomic | ordered | |
2281 // | atomic | atomic | |
2282 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002283 // | atomic | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002284 // | atomic | target enter | |
2285 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002286 // | atomic | target exit | |
2287 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002288 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002289 // | atomic | cancellation | |
2290 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002291 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002292 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002293 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002294 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002295 // +------------------+-----------------+------------------------------------+
2296 // | target | parallel | * |
2297 // | target | for | * |
2298 // | target | for simd | * |
2299 // | target | master | * |
2300 // | target | critical | * |
2301 // | target | simd | * |
2302 // | target | sections | * |
2303 // | target | section | * |
2304 // | target | single | * |
2305 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002306 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002307 // | target |parallel sections| * |
2308 // | target | task | * |
2309 // | target | taskyield | * |
2310 // | target | barrier | * |
2311 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002312 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002313 // | target | flush | * |
2314 // | target | ordered | * |
2315 // | target | atomic | * |
2316 // | target | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002317 // | target | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002318 // | target | target enter | * |
2319 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002320 // | target | target exit | * |
2321 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002322 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002323 // | target | cancellation | |
2324 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002325 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002326 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002327 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002328 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002329 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002330 // | target parallel | parallel | * |
2331 // | target parallel | for | * |
2332 // | target parallel | for simd | * |
2333 // | target parallel | master | * |
2334 // | target parallel | critical | * |
2335 // | target parallel | simd | * |
2336 // | target parallel | sections | * |
2337 // | target parallel | section | * |
2338 // | target parallel | single | * |
2339 // | target parallel | parallel for | * |
2340 // | target parallel |parallel for simd| * |
2341 // | target parallel |parallel sections| * |
2342 // | target parallel | task | * |
2343 // | target parallel | taskyield | * |
2344 // | target parallel | barrier | * |
2345 // | target parallel | taskwait | * |
2346 // | target parallel | taskgroup | * |
2347 // | target parallel | flush | * |
2348 // | target parallel | ordered | * |
2349 // | target parallel | atomic | * |
2350 // | target parallel | target | * |
2351 // | target parallel | target parallel | * |
2352 // | target parallel | target enter | * |
2353 // | | data | |
2354 // | target parallel | target exit | * |
2355 // | | data | |
2356 // | target parallel | teams | |
2357 // | target parallel | cancellation | |
2358 // | | point | ! |
2359 // | target parallel | cancel | ! |
2360 // | target parallel | taskloop | * |
2361 // | target parallel | taskloop simd | * |
2362 // | target parallel | distribute | |
2363 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002364 // | teams | parallel | * |
2365 // | teams | for | + |
2366 // | teams | for simd | + |
2367 // | teams | master | + |
2368 // | teams | critical | + |
2369 // | teams | simd | + |
2370 // | teams | sections | + |
2371 // | teams | section | + |
2372 // | teams | single | + |
2373 // | teams | parallel for | * |
2374 // | teams |parallel for simd| * |
2375 // | teams |parallel sections| * |
2376 // | teams | task | + |
2377 // | teams | taskyield | + |
2378 // | teams | barrier | + |
2379 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002380 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002381 // | teams | flush | + |
2382 // | teams | ordered | + |
2383 // | teams | atomic | + |
2384 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002385 // | teams | target parallel | + |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002386 // | teams | target enter | + |
2387 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002388 // | teams | target exit | + |
2389 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002390 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002391 // | teams | cancellation | |
2392 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002393 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002394 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002395 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002396 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002397 // +------------------+-----------------+------------------------------------+
2398 // | taskloop | parallel | * |
2399 // | taskloop | for | + |
2400 // | taskloop | for simd | + |
2401 // | taskloop | master | + |
2402 // | taskloop | critical | * |
2403 // | taskloop | simd | * |
2404 // | taskloop | sections | + |
2405 // | taskloop | section | + |
2406 // | taskloop | single | + |
2407 // | taskloop | parallel for | * |
2408 // | taskloop |parallel for simd| * |
2409 // | taskloop |parallel sections| * |
2410 // | taskloop | task | * |
2411 // | taskloop | taskyield | * |
2412 // | taskloop | barrier | + |
2413 // | taskloop | taskwait | * |
2414 // | taskloop | taskgroup | * |
2415 // | taskloop | flush | * |
2416 // | taskloop | ordered | + |
2417 // | taskloop | atomic | * |
2418 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002419 // | taskloop | target parallel | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002420 // | taskloop | target enter | * |
2421 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002422 // | taskloop | target exit | * |
2423 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002424 // | taskloop | teams | + |
2425 // | taskloop | cancellation | |
2426 // | | point | |
2427 // | taskloop | cancel | |
2428 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002429 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002430 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002431 // | taskloop simd | parallel | |
2432 // | taskloop simd | for | |
2433 // | taskloop simd | for simd | |
2434 // | taskloop simd | master | |
2435 // | taskloop simd | critical | |
2436 // | taskloop simd | simd | |
2437 // | taskloop simd | sections | |
2438 // | taskloop simd | section | |
2439 // | taskloop simd | single | |
2440 // | taskloop simd | parallel for | |
2441 // | taskloop simd |parallel for simd| |
2442 // | taskloop simd |parallel sections| |
2443 // | taskloop simd | task | |
2444 // | taskloop simd | taskyield | |
2445 // | taskloop simd | barrier | |
2446 // | taskloop simd | taskwait | |
2447 // | taskloop simd | taskgroup | |
2448 // | taskloop simd | flush | |
2449 // | taskloop simd | ordered | + (with simd clause) |
2450 // | taskloop simd | atomic | |
2451 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002452 // | taskloop simd | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002453 // | taskloop simd | target enter | |
2454 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002455 // | taskloop simd | target exit | |
2456 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002457 // | taskloop simd | teams | |
2458 // | taskloop simd | cancellation | |
2459 // | | point | |
2460 // | taskloop simd | cancel | |
2461 // | taskloop simd | taskloop | |
2462 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002463 // | taskloop simd | distribute | |
2464 // +------------------+-----------------+------------------------------------+
2465 // | distribute | parallel | * |
2466 // | distribute | for | * |
2467 // | distribute | for simd | * |
2468 // | distribute | master | * |
2469 // | distribute | critical | * |
2470 // | distribute | simd | * |
2471 // | distribute | sections | * |
2472 // | distribute | section | * |
2473 // | distribute | single | * |
2474 // | distribute | parallel for | * |
2475 // | distribute |parallel for simd| * |
2476 // | distribute |parallel sections| * |
2477 // | distribute | task | * |
2478 // | distribute | taskyield | * |
2479 // | distribute | barrier | * |
2480 // | distribute | taskwait | * |
2481 // | distribute | taskgroup | * |
2482 // | distribute | flush | * |
2483 // | distribute | ordered | + |
2484 // | distribute | atomic | * |
2485 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002486 // | distribute | target parallel | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002487 // | distribute | target enter | |
2488 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002489 // | distribute | target exit | |
2490 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002491 // | distribute | teams | |
2492 // | distribute | cancellation | + |
2493 // | | point | |
2494 // | distribute | cancel | + |
2495 // | distribute | taskloop | * |
2496 // | distribute | taskloop simd | * |
2497 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002498 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002499 if (Stack->getCurScope()) {
2500 auto ParentRegion = Stack->getParentDirective();
2501 bool NestingProhibited = false;
2502 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002503 enum {
2504 NoRecommend,
2505 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002506 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002507 ShouldBeInTargetRegion,
2508 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002509 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002510 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002511 // OpenMP [2.16, Nesting of Regions]
2512 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002513 // OpenMP [2.8.1,simd Construct, Restrictions]
2514 // An ordered construct with the simd clause is the only OpenMP construct
2515 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002516 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2517 return true;
2518 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002519 if (ParentRegion == OMPD_atomic) {
2520 // OpenMP [2.16, Nesting of Regions]
2521 // OpenMP constructs may not be nested inside an atomic region.
2522 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2523 return true;
2524 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002525 if (CurrentRegion == OMPD_section) {
2526 // OpenMP [2.7.2, sections Construct, Restrictions]
2527 // Orphaned section directives are prohibited. That is, the section
2528 // directives must appear within the sections construct and must not be
2529 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002530 if (ParentRegion != OMPD_sections &&
2531 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002532 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2533 << (ParentRegion != OMPD_unknown)
2534 << getOpenMPDirectiveName(ParentRegion);
2535 return true;
2536 }
2537 return false;
2538 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002539 // Allow some constructs to be orphaned (they could be used in functions,
2540 // called from OpenMP regions with the required preconditions).
2541 if (ParentRegion == OMPD_unknown)
2542 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002543 if (CurrentRegion == OMPD_cancellation_point ||
2544 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002545 // OpenMP [2.16, Nesting of Regions]
2546 // A cancellation point construct for which construct-type-clause is
2547 // taskgroup must be nested inside a task construct. A cancellation
2548 // point construct for which construct-type-clause is not taskgroup must
2549 // be closely nested inside an OpenMP construct that matches the type
2550 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002551 // A cancel construct for which construct-type-clause is taskgroup must be
2552 // nested inside a task construct. A cancel construct for which
2553 // construct-type-clause is not taskgroup must be closely nested inside an
2554 // OpenMP construct that matches the type specified in
2555 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002556 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002557 !((CancelRegion == OMPD_parallel &&
2558 (ParentRegion == OMPD_parallel ||
2559 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002560 (CancelRegion == OMPD_for &&
2561 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002562 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2563 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002564 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2565 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002566 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002567 // OpenMP [2.16, Nesting of Regions]
2568 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002569 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002570 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002571 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002572 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002573 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2574 // OpenMP [2.16, Nesting of Regions]
2575 // A critical region may not be nested (closely or otherwise) inside a
2576 // critical region with the same name. Note that this restriction is not
2577 // sufficient to prevent deadlock.
2578 SourceLocation PreviousCriticalLoc;
2579 bool DeadLock =
2580 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2581 OpenMPDirectiveKind K,
2582 const DeclarationNameInfo &DNI,
2583 SourceLocation Loc)
2584 ->bool {
2585 if (K == OMPD_critical &&
2586 DNI.getName() == CurrentName.getName()) {
2587 PreviousCriticalLoc = Loc;
2588 return true;
2589 } else
2590 return false;
2591 },
2592 false /* skip top directive */);
2593 if (DeadLock) {
2594 SemaRef.Diag(StartLoc,
2595 diag::err_omp_prohibited_region_critical_same_name)
2596 << CurrentName.getName();
2597 if (PreviousCriticalLoc.isValid())
2598 SemaRef.Diag(PreviousCriticalLoc,
2599 diag::note_omp_previous_critical_region);
2600 return true;
2601 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002602 } else if (CurrentRegion == OMPD_barrier) {
2603 // OpenMP [2.16, Nesting of Regions]
2604 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002605 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002606 NestingProhibited =
2607 isOpenMPWorksharingDirective(ParentRegion) ||
2608 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002609 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002610 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002611 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002612 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002613 // OpenMP [2.16, Nesting of Regions]
2614 // A worksharing region may not be closely nested inside a worksharing,
2615 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002616 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002617 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002618 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002619 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002620 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002621 Recommend = ShouldBeInParallelRegion;
2622 } else if (CurrentRegion == OMPD_ordered) {
2623 // OpenMP [2.16, Nesting of Regions]
2624 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002625 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002626 // An ordered region must be closely nested inside a loop region (or
2627 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002628 // OpenMP [2.8.1,simd Construct, Restrictions]
2629 // An ordered construct with the simd clause is the only OpenMP construct
2630 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002631 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002632 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002633 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002634 !(isOpenMPSimdDirective(ParentRegion) ||
2635 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002636 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002637 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2638 // OpenMP [2.16, Nesting of Regions]
2639 // If specified, a teams construct must be contained within a target
2640 // construct.
2641 NestingProhibited = ParentRegion != OMPD_target;
2642 Recommend = ShouldBeInTargetRegion;
2643 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2644 }
2645 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2646 // OpenMP [2.16, Nesting of Regions]
2647 // distribute, parallel, parallel sections, parallel workshare, and the
2648 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2649 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002650 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2651 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002652 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002653 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002654 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2655 // OpenMP 4.5 [2.17 Nesting of Regions]
2656 // The region associated with the distribute construct must be strictly
2657 // nested inside a teams region
2658 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2659 Recommend = ShouldBeInTeamsRegion;
2660 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002661 if (NestingProhibited) {
2662 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002663 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2664 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002665 return true;
2666 }
2667 }
2668 return false;
2669}
2670
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002671static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2672 ArrayRef<OMPClause *> Clauses,
2673 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2674 bool ErrorFound = false;
2675 unsigned NamedModifiersNumber = 0;
2676 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2677 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002678 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002679 for (const auto *C : Clauses) {
2680 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2681 // At most one if clause without a directive-name-modifier can appear on
2682 // the directive.
2683 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2684 if (FoundNameModifiers[CurNM]) {
2685 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2686 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2687 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2688 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002689 } else if (CurNM != OMPD_unknown) {
2690 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002691 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002692 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002693 FoundNameModifiers[CurNM] = IC;
2694 if (CurNM == OMPD_unknown)
2695 continue;
2696 // Check if the specified name modifier is allowed for the current
2697 // directive.
2698 // At most one if clause with the particular directive-name-modifier can
2699 // appear on the directive.
2700 bool MatchFound = false;
2701 for (auto NM : AllowedNameModifiers) {
2702 if (CurNM == NM) {
2703 MatchFound = true;
2704 break;
2705 }
2706 }
2707 if (!MatchFound) {
2708 S.Diag(IC->getNameModifierLoc(),
2709 diag::err_omp_wrong_if_directive_name_modifier)
2710 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2711 ErrorFound = true;
2712 }
2713 }
2714 }
2715 // If any if clause on the directive includes a directive-name-modifier then
2716 // all if clauses on the directive must include a directive-name-modifier.
2717 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2718 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2719 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2720 diag::err_omp_no_more_if_clause);
2721 } else {
2722 std::string Values;
2723 std::string Sep(", ");
2724 unsigned AllowedCnt = 0;
2725 unsigned TotalAllowedNum =
2726 AllowedNameModifiers.size() - NamedModifiersNumber;
2727 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2728 ++Cnt) {
2729 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2730 if (!FoundNameModifiers[NM]) {
2731 Values += "'";
2732 Values += getOpenMPDirectiveName(NM);
2733 Values += "'";
2734 if (AllowedCnt + 2 == TotalAllowedNum)
2735 Values += " or ";
2736 else if (AllowedCnt + 1 != TotalAllowedNum)
2737 Values += Sep;
2738 ++AllowedCnt;
2739 }
2740 }
2741 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2742 diag::err_omp_unnamed_if_clause)
2743 << (TotalAllowedNum > 1) << Values;
2744 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002745 for (auto Loc : NameModifierLoc) {
2746 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2747 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002748 ErrorFound = true;
2749 }
2750 return ErrorFound;
2751}
2752
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002753StmtResult Sema::ActOnOpenMPExecutableDirective(
2754 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2755 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2756 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002757 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002758 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2759 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002760 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002761
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002762 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002763 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002764 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002765 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002766 if (AStmt) {
2767 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2768
2769 // Check default data sharing attributes for referenced variables.
2770 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2771 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2772 if (DSAChecker.isErrorFound())
2773 return StmtError();
2774 // Generate list of implicitly defined firstprivate variables.
2775 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002776
2777 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2778 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2779 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2780 SourceLocation(), SourceLocation())) {
2781 ClausesWithImplicit.push_back(Implicit);
2782 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2783 DSAChecker.getImplicitFirstprivate().size();
2784 } else
2785 ErrorFound = true;
2786 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002787 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002788
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002789 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002790 switch (Kind) {
2791 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002792 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2793 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002794 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002795 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002796 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002797 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2798 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002799 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002800 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002801 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2802 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002803 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002804 case OMPD_for_simd:
2805 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2806 EndLoc, VarsWithInheritedDSA);
2807 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002808 case OMPD_sections:
2809 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2810 EndLoc);
2811 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002812 case OMPD_section:
2813 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002814 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002815 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2816 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002817 case OMPD_single:
2818 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2819 EndLoc);
2820 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002821 case OMPD_master:
2822 assert(ClausesWithImplicit.empty() &&
2823 "No clauses are allowed for 'omp master' directive");
2824 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2825 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002826 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002827 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2828 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002829 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002830 case OMPD_parallel_for:
2831 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2832 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002833 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002834 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002835 case OMPD_parallel_for_simd:
2836 Res = ActOnOpenMPParallelForSimdDirective(
2837 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002838 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002839 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002840 case OMPD_parallel_sections:
2841 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2842 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002843 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002844 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002845 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002846 Res =
2847 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002848 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002849 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002850 case OMPD_taskyield:
2851 assert(ClausesWithImplicit.empty() &&
2852 "No clauses are allowed for 'omp taskyield' directive");
2853 assert(AStmt == nullptr &&
2854 "No associated statement allowed for 'omp taskyield' directive");
2855 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2856 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002857 case OMPD_barrier:
2858 assert(ClausesWithImplicit.empty() &&
2859 "No clauses are allowed for 'omp barrier' directive");
2860 assert(AStmt == nullptr &&
2861 "No associated statement allowed for 'omp barrier' directive");
2862 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2863 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002864 case OMPD_taskwait:
2865 assert(ClausesWithImplicit.empty() &&
2866 "No clauses are allowed for 'omp taskwait' directive");
2867 assert(AStmt == nullptr &&
2868 "No associated statement allowed for 'omp taskwait' directive");
2869 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2870 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002871 case OMPD_taskgroup:
2872 assert(ClausesWithImplicit.empty() &&
2873 "No clauses are allowed for 'omp taskgroup' directive");
2874 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2875 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002876 case OMPD_flush:
2877 assert(AStmt == nullptr &&
2878 "No associated statement allowed for 'omp flush' directive");
2879 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2880 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002881 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002882 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2883 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002884 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002885 case OMPD_atomic:
2886 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2887 EndLoc);
2888 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002889 case OMPD_teams:
2890 Res =
2891 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2892 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002893 case OMPD_target:
2894 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2895 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002896 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002897 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002898 case OMPD_target_parallel:
2899 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2900 StartLoc, EndLoc);
2901 AllowedNameModifiers.push_back(OMPD_target);
2902 AllowedNameModifiers.push_back(OMPD_parallel);
2903 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002904 case OMPD_cancellation_point:
2905 assert(ClausesWithImplicit.empty() &&
2906 "No clauses are allowed for 'omp cancellation point' directive");
2907 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2908 "cancellation point' directive");
2909 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2910 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002911 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002912 assert(AStmt == nullptr &&
2913 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002914 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2915 CancelRegion);
2916 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002917 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002918 case OMPD_target_data:
2919 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2920 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002921 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002922 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002923 case OMPD_target_enter_data:
2924 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2925 EndLoc);
2926 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2927 break;
Samuel Antao72590762016-01-19 20:04:50 +00002928 case OMPD_target_exit_data:
2929 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2930 EndLoc);
2931 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2932 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002933 case OMPD_taskloop:
2934 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2935 EndLoc, VarsWithInheritedDSA);
2936 AllowedNameModifiers.push_back(OMPD_taskloop);
2937 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002938 case OMPD_taskloop_simd:
2939 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2940 EndLoc, VarsWithInheritedDSA);
2941 AllowedNameModifiers.push_back(OMPD_taskloop);
2942 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002943 case OMPD_distribute:
2944 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2945 EndLoc, VarsWithInheritedDSA);
2946 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002947 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002948 llvm_unreachable("OpenMP Directive is not allowed");
2949 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002950 llvm_unreachable("Unknown OpenMP directive");
2951 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002952
Alexey Bataev4acb8592014-07-07 13:01:15 +00002953 for (auto P : VarsWithInheritedDSA) {
2954 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2955 << P.first << P.second->getSourceRange();
2956 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002957 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2958
2959 if (!AllowedNameModifiers.empty())
2960 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2961 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002962
Alexey Bataeved09d242014-05-28 05:53:51 +00002963 if (ErrorFound)
2964 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002965 return Res;
2966}
2967
2968StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2969 Stmt *AStmt,
2970 SourceLocation StartLoc,
2971 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002972 if (!AStmt)
2973 return StmtError();
2974
Alexey Bataev9959db52014-05-06 10:08:46 +00002975 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2976 // 1.2.2 OpenMP Language Terminology
2977 // Structured block - An executable statement with a single entry at the
2978 // top and a single exit at the bottom.
2979 // The point of exit cannot be a branch out of the structured block.
2980 // longjmp() and throw() must not violate the entry/exit criteria.
2981 CS->getCapturedDecl()->setNothrow();
2982
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002983 getCurFunction()->setHasBranchProtectedScope();
2984
Alexey Bataev25e5b442015-09-15 12:52:43 +00002985 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2986 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002987}
2988
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002989namespace {
2990/// \brief Helper class for checking canonical form of the OpenMP loops and
2991/// extracting iteration space of each loop in the loop nest, that will be used
2992/// for IR generation.
2993class OpenMPIterationSpaceChecker {
2994 /// \brief Reference to Sema.
2995 Sema &SemaRef;
2996 /// \brief A location for diagnostics (when there is no some better location).
2997 SourceLocation DefaultLoc;
2998 /// \brief A location for diagnostics (when increment is not compatible).
2999 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003000 /// \brief A source location for referring to loop init later.
3001 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003002 /// \brief A source location for referring to condition later.
3003 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003004 /// \brief A source location for referring to increment later.
3005 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003006 /// \brief Loop variable.
3007 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003008 /// \brief Reference to loop variable.
3009 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003010 /// \brief Lower bound (initializer for the var).
3011 Expr *LB;
3012 /// \brief Upper bound.
3013 Expr *UB;
3014 /// \brief Loop step (increment).
3015 Expr *Step;
3016 /// \brief This flag is true when condition is one of:
3017 /// Var < UB
3018 /// Var <= UB
3019 /// UB > Var
3020 /// UB >= Var
3021 bool TestIsLessOp;
3022 /// \brief This flag is true when condition is strict ( < or > ).
3023 bool TestIsStrictOp;
3024 /// \brief This flag is true when step is subtracted on each iteration.
3025 bool SubtractStep;
3026
3027public:
3028 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3029 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003030 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3031 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003032 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3033 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003034 /// \brief Check init-expr for canonical loop form and save loop counter
3035 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003036 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003037 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3038 /// for less/greater and for strict/non-strict comparison.
3039 bool CheckCond(Expr *S);
3040 /// \brief Check incr-expr for canonical loop form and return true if it
3041 /// does not conform, otherwise save loop step (#Step).
3042 bool CheckInc(Expr *S);
3043 /// \brief Return the loop counter variable.
3044 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003045 /// \brief Return the reference expression to loop counter variable.
3046 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003047 /// \brief Source range of the loop init.
3048 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3049 /// \brief Source range of the loop condition.
3050 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3051 /// \brief Source range of the loop increment.
3052 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3053 /// \brief True if the step should be subtracted.
3054 bool ShouldSubtractStep() const { return SubtractStep; }
3055 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003056 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003057 /// \brief Build the precondition expression for the loops.
3058 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003059 /// \brief Build reference expression to the counter be used for codegen.
3060 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003061 /// \brief Build reference expression to the private counter be used for
3062 /// codegen.
3063 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003064 /// \brief Build initization of the counter be used for codegen.
3065 Expr *BuildCounterInit() const;
3066 /// \brief Build step of the counter be used for codegen.
3067 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003068 /// \brief Return true if any expression is dependent.
3069 bool Dependent() const;
3070
3071private:
3072 /// \brief Check the right-hand side of an assignment in the increment
3073 /// expression.
3074 bool CheckIncRHS(Expr *RHS);
3075 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003076 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003077 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003078 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003079 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003080 /// \brief Helper to set loop increment.
3081 bool SetStep(Expr *NewStep, bool Subtract);
3082};
3083
3084bool OpenMPIterationSpaceChecker::Dependent() const {
3085 if (!Var) {
3086 assert(!LB && !UB && !Step);
3087 return false;
3088 }
3089 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3090 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3091}
3092
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003093template <typename T>
3094static T *getExprAsWritten(T *E) {
3095 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3096 E = ExprTemp->getSubExpr();
3097
3098 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3099 E = MTE->GetTemporaryExpr();
3100
3101 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3102 E = Binder->getSubExpr();
3103
3104 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3105 E = ICE->getSubExprAsWritten();
3106 return E->IgnoreParens();
3107}
3108
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003109bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3110 DeclRefExpr *NewVarRefExpr,
3111 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003112 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003113 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3114 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003115 if (!NewVar || !NewLB)
3116 return true;
3117 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003118 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003119 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3120 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003121 if ((Ctor->isCopyOrMoveConstructor() ||
3122 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3123 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003124 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003125 LB = NewLB;
3126 return false;
3127}
3128
3129bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003130 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003131 // State consistency checking to ensure correct usage.
3132 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3133 !TestIsLessOp && !TestIsStrictOp);
3134 if (!NewUB)
3135 return true;
3136 UB = NewUB;
3137 TestIsLessOp = LessOp;
3138 TestIsStrictOp = StrictOp;
3139 ConditionSrcRange = SR;
3140 ConditionLoc = SL;
3141 return false;
3142}
3143
3144bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3145 // State consistency checking to ensure correct usage.
3146 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3147 if (!NewStep)
3148 return true;
3149 if (!NewStep->isValueDependent()) {
3150 // Check that the step is integer expression.
3151 SourceLocation StepLoc = NewStep->getLocStart();
3152 ExprResult Val =
3153 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3154 if (Val.isInvalid())
3155 return true;
3156 NewStep = Val.get();
3157
3158 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3159 // If test-expr is of form var relational-op b and relational-op is < or
3160 // <= then incr-expr must cause var to increase on each iteration of the
3161 // loop. If test-expr is of form var relational-op b and relational-op is
3162 // > or >= then incr-expr must cause var to decrease on each iteration of
3163 // the loop.
3164 // If test-expr is of form b relational-op var and relational-op is < or
3165 // <= then incr-expr must cause var to decrease on each iteration of the
3166 // loop. If test-expr is of form b relational-op var and relational-op is
3167 // > or >= then incr-expr must cause var to increase on each iteration of
3168 // the loop.
3169 llvm::APSInt Result;
3170 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3171 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3172 bool IsConstNeg =
3173 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003174 bool IsConstPos =
3175 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003176 bool IsConstZero = IsConstant && !Result.getBoolValue();
3177 if (UB && (IsConstZero ||
3178 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003179 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003180 SemaRef.Diag(NewStep->getExprLoc(),
3181 diag::err_omp_loop_incr_not_compatible)
3182 << Var << TestIsLessOp << NewStep->getSourceRange();
3183 SemaRef.Diag(ConditionLoc,
3184 diag::note_omp_loop_cond_requres_compatible_incr)
3185 << TestIsLessOp << ConditionSrcRange;
3186 return true;
3187 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003188 if (TestIsLessOp == Subtract) {
3189 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3190 NewStep).get();
3191 Subtract = !Subtract;
3192 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003193 }
3194
3195 Step = NewStep;
3196 SubtractStep = Subtract;
3197 return false;
3198}
3199
Alexey Bataev9c821032015-04-30 04:23:23 +00003200bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003201 // Check init-expr for canonical loop form and save loop counter
3202 // variable - #Var and its initialization value - #LB.
3203 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3204 // var = lb
3205 // integer-type var = lb
3206 // random-access-iterator-type var = lb
3207 // pointer-type var = lb
3208 //
3209 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003210 if (EmitDiags) {
3211 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3212 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003213 return true;
3214 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003215 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003216 if (Expr *E = dyn_cast<Expr>(S))
3217 S = E->IgnoreParens();
3218 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3219 if (BO->getOpcode() == BO_Assign)
3220 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003221 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003222 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003223 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3224 if (DS->isSingleDecl()) {
3225 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003226 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003227 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003228 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003229 SemaRef.Diag(S->getLocStart(),
3230 diag::ext_omp_loop_not_canonical_init)
3231 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003232 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003233 }
3234 }
3235 }
3236 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3237 if (CE->getOperator() == OO_Equal)
3238 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003239 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3240 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003241
Alexey Bataev9c821032015-04-30 04:23:23 +00003242 if (EmitDiags) {
3243 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3244 << S->getSourceRange();
3245 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003246 return true;
3247}
3248
Alexey Bataev23b69422014-06-18 07:08:49 +00003249/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003250/// variable (which may be the loop variable) if possible.
3251static const VarDecl *GetInitVarDecl(const Expr *E) {
3252 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003253 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003254 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003255 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3256 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003257 if ((Ctor->isCopyOrMoveConstructor() ||
3258 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3259 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003260 E = CE->getArg(0)->IgnoreParenImpCasts();
3261 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3262 if (!DRE)
3263 return nullptr;
3264 return dyn_cast<VarDecl>(DRE->getDecl());
3265}
3266
3267bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3268 // Check test-expr for canonical form, save upper-bound UB, flags for
3269 // less/greater and for strict/non-strict comparison.
3270 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3271 // var relational-op b
3272 // b relational-op var
3273 //
3274 if (!S) {
3275 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3276 return true;
3277 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003278 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003279 SourceLocation CondLoc = S->getLocStart();
3280 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3281 if (BO->isRelationalOp()) {
3282 if (GetInitVarDecl(BO->getLHS()) == Var)
3283 return SetUB(BO->getRHS(),
3284 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3285 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3286 BO->getSourceRange(), BO->getOperatorLoc());
3287 if (GetInitVarDecl(BO->getRHS()) == Var)
3288 return SetUB(BO->getLHS(),
3289 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3290 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3291 BO->getSourceRange(), BO->getOperatorLoc());
3292 }
3293 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3294 if (CE->getNumArgs() == 2) {
3295 auto Op = CE->getOperator();
3296 switch (Op) {
3297 case OO_Greater:
3298 case OO_GreaterEqual:
3299 case OO_Less:
3300 case OO_LessEqual:
3301 if (GetInitVarDecl(CE->getArg(0)) == Var)
3302 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3303 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3304 CE->getOperatorLoc());
3305 if (GetInitVarDecl(CE->getArg(1)) == Var)
3306 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3307 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3308 CE->getOperatorLoc());
3309 break;
3310 default:
3311 break;
3312 }
3313 }
3314 }
3315 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3316 << S->getSourceRange() << Var;
3317 return true;
3318}
3319
3320bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3321 // RHS of canonical loop form increment can be:
3322 // var + incr
3323 // incr + var
3324 // var - incr
3325 //
3326 RHS = RHS->IgnoreParenImpCasts();
3327 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3328 if (BO->isAdditiveOp()) {
3329 bool IsAdd = BO->getOpcode() == BO_Add;
3330 if (GetInitVarDecl(BO->getLHS()) == Var)
3331 return SetStep(BO->getRHS(), !IsAdd);
3332 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3333 return SetStep(BO->getLHS(), false);
3334 }
3335 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3336 bool IsAdd = CE->getOperator() == OO_Plus;
3337 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3338 if (GetInitVarDecl(CE->getArg(0)) == Var)
3339 return SetStep(CE->getArg(1), !IsAdd);
3340 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3341 return SetStep(CE->getArg(0), false);
3342 }
3343 }
3344 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3345 << RHS->getSourceRange() << Var;
3346 return true;
3347}
3348
3349bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3350 // Check incr-expr for canonical loop form and return true if it
3351 // does not conform.
3352 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3353 // ++var
3354 // var++
3355 // --var
3356 // var--
3357 // var += incr
3358 // var -= incr
3359 // var = var + incr
3360 // var = incr + var
3361 // var = var - incr
3362 //
3363 if (!S) {
3364 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3365 return true;
3366 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003367 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003368 S = S->IgnoreParens();
3369 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3370 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3371 return SetStep(
3372 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3373 (UO->isDecrementOp() ? -1 : 1)).get(),
3374 false);
3375 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3376 switch (BO->getOpcode()) {
3377 case BO_AddAssign:
3378 case BO_SubAssign:
3379 if (GetInitVarDecl(BO->getLHS()) == Var)
3380 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3381 break;
3382 case BO_Assign:
3383 if (GetInitVarDecl(BO->getLHS()) == Var)
3384 return CheckIncRHS(BO->getRHS());
3385 break;
3386 default:
3387 break;
3388 }
3389 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3390 switch (CE->getOperator()) {
3391 case OO_PlusPlus:
3392 case OO_MinusMinus:
3393 if (GetInitVarDecl(CE->getArg(0)) == Var)
3394 return SetStep(
3395 SemaRef.ActOnIntegerConstant(
3396 CE->getLocStart(),
3397 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3398 false);
3399 break;
3400 case OO_PlusEqual:
3401 case OO_MinusEqual:
3402 if (GetInitVarDecl(CE->getArg(0)) == Var)
3403 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3404 break;
3405 case OO_Equal:
3406 if (GetInitVarDecl(CE->getArg(0)) == Var)
3407 return CheckIncRHS(CE->getArg(1));
3408 break;
3409 default:
3410 break;
3411 }
3412 }
3413 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3414 << S->getSourceRange() << Var;
3415 return true;
3416}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003417
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003418namespace {
3419// Transform variables declared in GNU statement expressions to new ones to
3420// avoid crash on codegen.
3421class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3422 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3423
3424public:
3425 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3426
3427 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3428 if (auto *VD = cast<VarDecl>(D))
3429 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3430 !isa<ImplicitParamDecl>(D)) {
3431 auto *NewVD = VarDecl::Create(
3432 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3433 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3434 VD->getTypeSourceInfo(), VD->getStorageClass());
3435 NewVD->setTSCSpec(VD->getTSCSpec());
3436 NewVD->setInit(VD->getInit());
3437 NewVD->setInitStyle(VD->getInitStyle());
3438 NewVD->setExceptionVariable(VD->isExceptionVariable());
3439 NewVD->setNRVOVariable(VD->isNRVOVariable());
3440 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3441 NewVD->setConstexpr(VD->isConstexpr());
3442 NewVD->setInitCapture(VD->isInitCapture());
3443 NewVD->setPreviousDeclInSameBlockScope(
3444 VD->isPreviousDeclInSameBlockScope());
3445 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003446 if (VD->hasAttrs())
3447 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003448 transformedLocalDecl(VD, NewVD);
3449 return NewVD;
3450 }
3451 return BaseTransform::TransformDefinition(Loc, D);
3452 }
3453
3454 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3455 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3456 if (E->getDecl() != NewD) {
3457 NewD->setReferenced();
3458 NewD->markUsed(SemaRef.Context);
3459 return DeclRefExpr::Create(
3460 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3461 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3462 E->getNameInfo(), E->getType(), E->getValueKind());
3463 }
3464 return BaseTransform::TransformDeclRefExpr(E);
3465 }
3466};
3467}
3468
Alexander Musmana5f070a2014-10-01 06:03:56 +00003469/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003470Expr *
3471OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3472 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003473 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003474 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003475 auto VarType = Var->getType().getNonReferenceType();
3476 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003477 SemaRef.getLangOpts().CPlusPlus) {
3478 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003479 auto *UBExpr = TestIsLessOp ? UB : LB;
3480 auto *LBExpr = TestIsLessOp ? LB : UB;
3481 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3482 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3483 if (!Upper || !Lower)
3484 return nullptr;
3485 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3486 Sema::AA_Converting,
3487 /*AllowExplicit=*/true)
3488 .get();
3489 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3490 Sema::AA_Converting,
3491 /*AllowExplicit=*/true)
3492 .get();
3493 if (!Upper || !Lower)
3494 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003495
3496 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3497
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003498 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003499 // BuildBinOp already emitted error, this one is to point user to upper
3500 // and lower bound, and to tell what is passed to 'operator-'.
3501 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3502 << Upper->getSourceRange() << Lower->getSourceRange();
3503 return nullptr;
3504 }
3505 }
3506
3507 if (!Diff.isUsable())
3508 return nullptr;
3509
3510 // Upper - Lower [- 1]
3511 if (TestIsStrictOp)
3512 Diff = SemaRef.BuildBinOp(
3513 S, DefaultLoc, BO_Sub, Diff.get(),
3514 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3515 if (!Diff.isUsable())
3516 return nullptr;
3517
3518 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003519 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3520 if (NewStep.isInvalid())
3521 return nullptr;
3522 NewStep = SemaRef.PerformImplicitConversion(
3523 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3524 /*AllowExplicit=*/true);
3525 if (NewStep.isInvalid())
3526 return nullptr;
3527 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003528 if (!Diff.isUsable())
3529 return nullptr;
3530
3531 // Parentheses (for dumping/debugging purposes only).
3532 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3533 if (!Diff.isUsable())
3534 return nullptr;
3535
3536 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003537 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3538 if (NewStep.isInvalid())
3539 return nullptr;
3540 NewStep = SemaRef.PerformImplicitConversion(
3541 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3542 /*AllowExplicit=*/true);
3543 if (NewStep.isInvalid())
3544 return nullptr;
3545 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003546 if (!Diff.isUsable())
3547 return nullptr;
3548
Alexander Musman174b3ca2014-10-06 11:16:29 +00003549 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003550 QualType Type = Diff.get()->getType();
3551 auto &C = SemaRef.Context;
3552 bool UseVarType = VarType->hasIntegerRepresentation() &&
3553 C.getTypeSize(Type) > C.getTypeSize(VarType);
3554 if (!Type->isIntegerType() || UseVarType) {
3555 unsigned NewSize =
3556 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3557 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3558 : Type->hasSignedIntegerRepresentation();
3559 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3560 Diff = SemaRef.PerformImplicitConversion(
3561 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3562 if (!Diff.isUsable())
3563 return nullptr;
3564 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003565 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003566 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3567 if (NewSize != C.getTypeSize(Type)) {
3568 if (NewSize < C.getTypeSize(Type)) {
3569 assert(NewSize == 64 && "incorrect loop var size");
3570 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3571 << InitSrcRange << ConditionSrcRange;
3572 }
3573 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003574 NewSize, Type->hasSignedIntegerRepresentation() ||
3575 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003576 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3577 Sema::AA_Converting, true);
3578 if (!Diff.isUsable())
3579 return nullptr;
3580 }
3581 }
3582
Alexander Musmana5f070a2014-10-01 06:03:56 +00003583 return Diff.get();
3584}
3585
Alexey Bataev62dbb972015-04-22 11:59:37 +00003586Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3587 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3588 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3589 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003590 TransformToNewDefs Transform(SemaRef);
3591
3592 auto NewLB = Transform.TransformExpr(LB);
3593 auto NewUB = Transform.TransformExpr(UB);
3594 if (NewLB.isInvalid() || NewUB.isInvalid())
3595 return Cond;
3596 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3597 Sema::AA_Converting,
3598 /*AllowExplicit=*/true);
3599 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3600 Sema::AA_Converting,
3601 /*AllowExplicit=*/true);
3602 if (NewLB.isInvalid() || NewUB.isInvalid())
3603 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003604 auto CondExpr = SemaRef.BuildBinOp(
3605 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3606 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003607 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003608 if (CondExpr.isUsable()) {
3609 CondExpr = SemaRef.PerformImplicitConversion(
3610 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3611 /*AllowExplicit=*/true);
3612 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003613 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3614 // Otherwise use original loop conditon and evaluate it in runtime.
3615 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3616}
3617
Alexander Musmana5f070a2014-10-01 06:03:56 +00003618/// \brief Build reference expression to the counter be used for codegen.
3619Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003620 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3621 DefaultLoc);
3622}
3623
3624Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3625 if (Var && !Var->isInvalidDecl()) {
3626 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003627 auto *PrivateVar =
3628 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3629 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003630 if (PrivateVar->isInvalidDecl())
3631 return nullptr;
3632 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3633 }
3634 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003635}
3636
3637/// \brief Build initization of the counter be used for codegen.
3638Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3639
3640/// \brief Build step of the counter be used for codegen.
3641Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3642
3643/// \brief Iteration space of a single for loop.
3644struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003645 /// \brief Condition of the loop.
3646 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003647 /// \brief This expression calculates the number of iterations in the loop.
3648 /// It is always possible to calculate it before starting the loop.
3649 Expr *NumIterations;
3650 /// \brief The loop counter variable.
3651 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003652 /// \brief Private loop counter variable.
3653 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003654 /// \brief This is initializer for the initial value of #CounterVar.
3655 Expr *CounterInit;
3656 /// \brief This is step for the #CounterVar used to generate its update:
3657 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3658 Expr *CounterStep;
3659 /// \brief Should step be subtracted?
3660 bool Subtract;
3661 /// \brief Source range of the loop init.
3662 SourceRange InitSrcRange;
3663 /// \brief Source range of the loop condition.
3664 SourceRange CondSrcRange;
3665 /// \brief Source range of the loop increment.
3666 SourceRange IncSrcRange;
3667};
3668
Alexey Bataev23b69422014-06-18 07:08:49 +00003669} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003670
Alexey Bataev9c821032015-04-30 04:23:23 +00003671void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3672 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3673 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003674 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3675 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003676 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3677 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003678 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003679 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003680 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003681 }
3682}
3683
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003684/// \brief Called on a for stmt to check and extract its iteration space
3685/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003686static bool CheckOpenMPIterationSpace(
3687 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3688 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003689 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003690 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003691 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003692 // OpenMP [2.6, Canonical Loop Form]
3693 // for (init-expr; test-expr; incr-expr) structured-block
3694 auto For = dyn_cast_or_null<ForStmt>(S);
3695 if (!For) {
3696 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003697 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3698 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3699 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3700 if (NestedLoopCount > 1) {
3701 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3702 SemaRef.Diag(DSA.getConstructLoc(),
3703 diag::note_omp_collapse_ordered_expr)
3704 << 2 << CollapseLoopCountExpr->getSourceRange()
3705 << OrderedLoopCountExpr->getSourceRange();
3706 else if (CollapseLoopCountExpr)
3707 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3708 diag::note_omp_collapse_ordered_expr)
3709 << 0 << CollapseLoopCountExpr->getSourceRange();
3710 else
3711 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3712 diag::note_omp_collapse_ordered_expr)
3713 << 1 << OrderedLoopCountExpr->getSourceRange();
3714 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003715 return true;
3716 }
3717 assert(For->getBody());
3718
3719 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3720
3721 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003722 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003723 if (ISC.CheckInit(Init)) {
3724 return true;
3725 }
3726
3727 bool HasErrors = false;
3728
3729 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003730 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003731
3732 // OpenMP [2.6, Canonical Loop Form]
3733 // Var is one of the following:
3734 // A variable of signed or unsigned integer type.
3735 // For C++, a variable of a random access iterator type.
3736 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003737 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003738 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3739 !VarType->isPointerType() &&
3740 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3741 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3742 << SemaRef.getLangOpts().CPlusPlus;
3743 HasErrors = true;
3744 }
3745
Alexey Bataev4acb8592014-07-07 13:01:15 +00003746 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3747 // Construct
3748 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3749 // parallel for construct is (are) private.
3750 // The loop iteration variable in the associated for-loop of a simd construct
3751 // with just one associated for-loop is linear with a constant-linear-step
3752 // that is the increment of the associated for-loop.
3753 // Exclude loop var from the list of variables with implicitly defined data
3754 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003755 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003756
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003757 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3758 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003759 // The loop iteration variable in the associated for-loop of a simd construct
3760 // with just one associated for-loop may be listed in a linear clause with a
3761 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003762 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3763 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003764 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003765 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3766 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3767 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003768 auto PredeterminedCKind =
3769 isOpenMPSimdDirective(DKind)
3770 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3771 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003772 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003773 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003774 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003775 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003776 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003777 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3778 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003779 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003780 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3781 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003782 if (DVar.RefExpr == nullptr)
3783 DVar.CKind = PredeterminedCKind;
3784 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003785 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003786 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003787 // Make the loop iteration variable private (for worksharing constructs),
3788 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003789 // lastprivate (for simd directives with several collapsed or ordered
3790 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003791 if (DVar.CKind == OMPC_unknown)
3792 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3793 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003794 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003795 }
3796
Alexey Bataev7ff55242014-06-19 09:13:45 +00003797 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003798
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003799 // Check test-expr.
3800 HasErrors |= ISC.CheckCond(For->getCond());
3801
3802 // Check incr-expr.
3803 HasErrors |= ISC.CheckInc(For->getInc());
3804
Alexander Musmana5f070a2014-10-01 06:03:56 +00003805 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003806 return HasErrors;
3807
Alexander Musmana5f070a2014-10-01 06:03:56 +00003808 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003809 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003810 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003811 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003812 isOpenMPTaskLoopDirective(DKind) ||
3813 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003814 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003815 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003816 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3817 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3818 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3819 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3820 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3821 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3822
Alexey Bataev62dbb972015-04-22 11:59:37 +00003823 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3824 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003825 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003826 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003827 ResultIterSpace.CounterInit == nullptr ||
3828 ResultIterSpace.CounterStep == nullptr);
3829
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003830 return HasErrors;
3831}
3832
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003833/// \brief Build 'VarRef = Start.
3834static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3835 ExprResult VarRef, ExprResult Start) {
3836 TransformToNewDefs Transform(SemaRef);
3837 // Build 'VarRef = Start.
3838 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3839 if (NewStart.isInvalid())
3840 return ExprError();
3841 NewStart = SemaRef.PerformImplicitConversion(
3842 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3843 Sema::AA_Converting,
3844 /*AllowExplicit=*/true);
3845 if (NewStart.isInvalid())
3846 return ExprError();
3847 NewStart = SemaRef.PerformImplicitConversion(
3848 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3849 /*AllowExplicit=*/true);
3850 if (!NewStart.isUsable())
3851 return ExprError();
3852
3853 auto Init =
3854 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3855 return Init;
3856}
3857
Alexander Musmana5f070a2014-10-01 06:03:56 +00003858/// \brief Build 'VarRef = Start + Iter * Step'.
3859static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3860 SourceLocation Loc, ExprResult VarRef,
3861 ExprResult Start, ExprResult Iter,
3862 ExprResult Step, bool Subtract) {
3863 // Add parentheses (for debugging purposes only).
3864 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3865 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3866 !Step.isUsable())
3867 return ExprError();
3868
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003869 TransformToNewDefs Transform(SemaRef);
3870 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3871 if (NewStep.isInvalid())
3872 return ExprError();
3873 NewStep = SemaRef.PerformImplicitConversion(
3874 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3875 Sema::AA_Converting,
3876 /*AllowExplicit=*/true);
3877 if (NewStep.isInvalid())
3878 return ExprError();
3879 ExprResult Update =
3880 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881 if (!Update.isUsable())
3882 return ExprError();
3883
3884 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003885 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3886 if (NewStart.isInvalid())
3887 return ExprError();
3888 NewStart = SemaRef.PerformImplicitConversion(
3889 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3890 Sema::AA_Converting,
3891 /*AllowExplicit=*/true);
3892 if (NewStart.isInvalid())
3893 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003894 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003895 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003896 if (!Update.isUsable())
3897 return ExprError();
3898
3899 Update = SemaRef.PerformImplicitConversion(
3900 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3901 if (!Update.isUsable())
3902 return ExprError();
3903
3904 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3905 return Update;
3906}
3907
3908/// \brief Convert integer expression \a E to make it have at least \a Bits
3909/// bits.
3910static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3911 Sema &SemaRef) {
3912 if (E == nullptr)
3913 return ExprError();
3914 auto &C = SemaRef.Context;
3915 QualType OldType = E->getType();
3916 unsigned HasBits = C.getTypeSize(OldType);
3917 if (HasBits >= Bits)
3918 return ExprResult(E);
3919 // OK to convert to signed, because new type has more bits than old.
3920 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3921 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3922 true);
3923}
3924
3925/// \brief Check if the given expression \a E is a constant integer that fits
3926/// into \a Bits bits.
3927static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3928 if (E == nullptr)
3929 return false;
3930 llvm::APSInt Result;
3931 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3932 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3933 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003934}
3935
3936/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003937/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3938/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003939static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003940CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3941 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3942 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003943 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003944 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003945 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003946 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003947 // Found 'collapse' clause - calculate collapse number.
3948 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003949 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003950 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003951 }
3952 if (OrderedLoopCountExpr) {
3953 // Found 'ordered' clause - calculate collapse number.
3954 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003955 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3956 if (Result.getLimitedValue() < NestedLoopCount) {
3957 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3958 diag::err_omp_wrong_ordered_loop_count)
3959 << OrderedLoopCountExpr->getSourceRange();
3960 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3961 diag::note_collapse_loop_count)
3962 << CollapseLoopCountExpr->getSourceRange();
3963 }
3964 NestedLoopCount = Result.getLimitedValue();
3965 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003966 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003967 // This is helper routine for loop directives (e.g., 'for', 'simd',
3968 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003969 SmallVector<LoopIterationSpace, 4> IterSpaces;
3970 IterSpaces.resize(NestedLoopCount);
3971 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003972 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003973 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003974 NestedLoopCount, CollapseLoopCountExpr,
3975 OrderedLoopCountExpr, VarsWithImplicitDSA,
3976 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003977 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003978 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003979 // OpenMP [2.8.1, simd construct, Restrictions]
3980 // All loops associated with the construct must be perfectly nested; that
3981 // is, there must be no intervening code nor any OpenMP directive between
3982 // any two loops.
3983 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003984 }
3985
Alexander Musmana5f070a2014-10-01 06:03:56 +00003986 Built.clear(/* size */ NestedLoopCount);
3987
3988 if (SemaRef.CurContext->isDependentContext())
3989 return NestedLoopCount;
3990
3991 // An example of what is generated for the following code:
3992 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003993 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003994 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003995 // for (k = 0; k < NK; ++k)
3996 // for (j = J0; j < NJ; j+=2) {
3997 // <loop body>
3998 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003999 //
4000 // We generate the code below.
4001 // Note: the loop body may be outlined in CodeGen.
4002 // Note: some counters may be C++ classes, operator- is used to find number of
4003 // iterations and operator+= to calculate counter value.
4004 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4005 // or i64 is currently supported).
4006 //
4007 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4008 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4009 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4010 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4011 // // similar updates for vars in clauses (e.g. 'linear')
4012 // <loop body (using local i and j)>
4013 // }
4014 // i = NI; // assign final values of counters
4015 // j = NJ;
4016 //
4017
4018 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4019 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004020 // Precondition tests if there is at least one iteration (all conditions are
4021 // true).
4022 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004023 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004024 ExprResult LastIteration32 = WidenIterationCount(
4025 32 /* Bits */, SemaRef.PerformImplicitConversion(
4026 N0->IgnoreImpCasts(), N0->getType(),
4027 Sema::AA_Converting, /*AllowExplicit=*/true)
4028 .get(),
4029 SemaRef);
4030 ExprResult LastIteration64 = WidenIterationCount(
4031 64 /* Bits */, SemaRef.PerformImplicitConversion(
4032 N0->IgnoreImpCasts(), N0->getType(),
4033 Sema::AA_Converting, /*AllowExplicit=*/true)
4034 .get(),
4035 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004036
4037 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4038 return NestedLoopCount;
4039
4040 auto &C = SemaRef.Context;
4041 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4042
4043 Scope *CurScope = DSA.getCurScope();
4044 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004045 if (PreCond.isUsable()) {
4046 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4047 PreCond.get(), IterSpaces[Cnt].PreCond);
4048 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004049 auto N = IterSpaces[Cnt].NumIterations;
4050 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4051 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004052 LastIteration32 = SemaRef.BuildBinOp(
4053 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4054 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4055 Sema::AA_Converting,
4056 /*AllowExplicit=*/true)
4057 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004058 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004059 LastIteration64 = SemaRef.BuildBinOp(
4060 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4061 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4062 Sema::AA_Converting,
4063 /*AllowExplicit=*/true)
4064 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004065 }
4066
4067 // Choose either the 32-bit or 64-bit version.
4068 ExprResult LastIteration = LastIteration64;
4069 if (LastIteration32.isUsable() &&
4070 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4071 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4072 FitsInto(
4073 32 /* Bits */,
4074 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4075 LastIteration64.get(), SemaRef)))
4076 LastIteration = LastIteration32;
4077
4078 if (!LastIteration.isUsable())
4079 return 0;
4080
4081 // Save the number of iterations.
4082 ExprResult NumIterations = LastIteration;
4083 {
4084 LastIteration = SemaRef.BuildBinOp(
4085 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4086 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4087 if (!LastIteration.isUsable())
4088 return 0;
4089 }
4090
4091 // Calculate the last iteration number beforehand instead of doing this on
4092 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4093 llvm::APSInt Result;
4094 bool IsConstant =
4095 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4096 ExprResult CalcLastIteration;
4097 if (!IsConstant) {
4098 SourceLocation SaveLoc;
4099 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004100 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004101 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004102 ExprResult SaveRef = buildDeclRefExpr(
4103 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004104 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4105 SaveRef.get(), LastIteration.get());
4106 LastIteration = SaveRef;
4107
4108 // Prepare SaveRef + 1.
4109 NumIterations = SemaRef.BuildBinOp(
4110 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4111 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4112 if (!NumIterations.isUsable())
4113 return 0;
4114 }
4115
4116 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4117
Alexander Musmanc6388682014-12-15 07:07:06 +00004118 QualType VType = LastIteration.get()->getType();
4119 // Build variables passed into runtime, nesessary for worksharing directives.
4120 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004121 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4122 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004123 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004124 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4125 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004126 SemaRef.AddInitializerToDecl(
4127 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4128 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4129
4130 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004131 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4132 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004133 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4134 /*DirectInit*/ false,
4135 /*TypeMayContainAuto*/ false);
4136
4137 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4138 // This will be used to implement clause 'lastprivate'.
4139 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004140 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4141 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004142 SemaRef.AddInitializerToDecl(
4143 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4144 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4145
4146 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004147 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4148 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004149 SemaRef.AddInitializerToDecl(
4150 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4151 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4152
4153 // Build expression: UB = min(UB, LastIteration)
4154 // It is nesessary for CodeGen of directives with static scheduling.
4155 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4156 UB.get(), LastIteration.get());
4157 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4158 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4159 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4160 CondOp.get());
4161 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4162 }
4163
4164 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004165 ExprResult IV;
4166 ExprResult Init;
4167 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004168 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4169 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004170 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004171 isOpenMPTaskLoopDirective(DKind) ||
4172 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004173 ? LB.get()
4174 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4175 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4176 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004177 }
4178
Alexander Musmanc6388682014-12-15 07:07:06 +00004179 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004180 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004181 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004182 (isOpenMPWorksharingDirective(DKind) ||
4183 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004184 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4185 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4186 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004187
4188 // Loop increment (IV = IV + 1)
4189 SourceLocation IncLoc;
4190 ExprResult Inc =
4191 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4192 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4193 if (!Inc.isUsable())
4194 return 0;
4195 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004196 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4197 if (!Inc.isUsable())
4198 return 0;
4199
4200 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4201 // Used for directives with static scheduling.
4202 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004203 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4204 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004205 // LB + ST
4206 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4207 if (!NextLB.isUsable())
4208 return 0;
4209 // LB = LB + ST
4210 NextLB =
4211 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4212 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4213 if (!NextLB.isUsable())
4214 return 0;
4215 // UB + ST
4216 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4217 if (!NextUB.isUsable())
4218 return 0;
4219 // UB = UB + ST
4220 NextUB =
4221 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4222 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4223 if (!NextUB.isUsable())
4224 return 0;
4225 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004226
4227 // Build updates and final values of the loop counters.
4228 bool HasErrors = false;
4229 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004230 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004231 Built.Updates.resize(NestedLoopCount);
4232 Built.Finals.resize(NestedLoopCount);
4233 {
4234 ExprResult Div;
4235 // Go from inner nested loop to outer.
4236 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4237 LoopIterationSpace &IS = IterSpaces[Cnt];
4238 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4239 // Build: Iter = (IV / Div) % IS.NumIters
4240 // where Div is product of previous iterations' IS.NumIters.
4241 ExprResult Iter;
4242 if (Div.isUsable()) {
4243 Iter =
4244 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4245 } else {
4246 Iter = IV;
4247 assert((Cnt == (int)NestedLoopCount - 1) &&
4248 "unusable div expected on first iteration only");
4249 }
4250
4251 if (Cnt != 0 && Iter.isUsable())
4252 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4253 IS.NumIterations);
4254 if (!Iter.isUsable()) {
4255 HasErrors = true;
4256 break;
4257 }
4258
Alexey Bataev39f915b82015-05-08 10:41:21 +00004259 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4260 auto *CounterVar = buildDeclRefExpr(
4261 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4262 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4263 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004264 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4265 IS.CounterInit);
4266 if (!Init.isUsable()) {
4267 HasErrors = true;
4268 break;
4269 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004270 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004271 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004272 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4273 if (!Update.isUsable()) {
4274 HasErrors = true;
4275 break;
4276 }
4277
4278 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4279 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004280 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004281 IS.NumIterations, IS.CounterStep, IS.Subtract);
4282 if (!Final.isUsable()) {
4283 HasErrors = true;
4284 break;
4285 }
4286
4287 // Build Div for the next iteration: Div <- Div * IS.NumIters
4288 if (Cnt != 0) {
4289 if (Div.isUnset())
4290 Div = IS.NumIterations;
4291 else
4292 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4293 IS.NumIterations);
4294
4295 // Add parentheses (for debugging purposes only).
4296 if (Div.isUsable())
4297 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4298 if (!Div.isUsable()) {
4299 HasErrors = true;
4300 break;
4301 }
4302 }
4303 if (!Update.isUsable() || !Final.isUsable()) {
4304 HasErrors = true;
4305 break;
4306 }
4307 // Save results
4308 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004309 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004310 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004311 Built.Updates[Cnt] = Update.get();
4312 Built.Finals[Cnt] = Final.get();
4313 }
4314 }
4315
4316 if (HasErrors)
4317 return 0;
4318
4319 // Save results
4320 Built.IterationVarRef = IV.get();
4321 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004322 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004323 Built.CalcLastIteration =
4324 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004325 Built.PreCond = PreCond.get();
4326 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004327 Built.Init = Init.get();
4328 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004329 Built.LB = LB.get();
4330 Built.UB = UB.get();
4331 Built.IL = IL.get();
4332 Built.ST = ST.get();
4333 Built.EUB = EUB.get();
4334 Built.NLB = NextLB.get();
4335 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004336
Alexey Bataevabfc0692014-06-25 06:52:00 +00004337 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004338}
4339
Alexey Bataev10e775f2015-07-30 11:36:16 +00004340static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004341 auto CollapseClauses =
4342 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4343 if (CollapseClauses.begin() != CollapseClauses.end())
4344 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004345 return nullptr;
4346}
4347
Alexey Bataev10e775f2015-07-30 11:36:16 +00004348static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004349 auto OrderedClauses =
4350 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4351 if (OrderedClauses.begin() != OrderedClauses.end())
4352 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004353 return nullptr;
4354}
4355
Alexey Bataev66b15b52015-08-21 11:14:16 +00004356static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4357 const Expr *Safelen) {
4358 llvm::APSInt SimdlenRes, SafelenRes;
4359 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4360 Simdlen->isInstantiationDependent() ||
4361 Simdlen->containsUnexpandedParameterPack())
4362 return false;
4363 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4364 Safelen->isInstantiationDependent() ||
4365 Safelen->containsUnexpandedParameterPack())
4366 return false;
4367 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4368 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4369 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4370 // If both simdlen and safelen clauses are specified, the value of the simdlen
4371 // parameter must be less than or equal to the value of the safelen parameter.
4372 if (SimdlenRes > SafelenRes) {
4373 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4374 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4375 return true;
4376 }
4377 return false;
4378}
4379
Alexey Bataev4acb8592014-07-07 13:01:15 +00004380StmtResult Sema::ActOnOpenMPSimdDirective(
4381 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4382 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004383 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004384 if (!AStmt)
4385 return StmtError();
4386
4387 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004388 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004389 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4390 // define the nested loops number.
4391 unsigned NestedLoopCount = CheckOpenMPLoop(
4392 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4393 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004394 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004395 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004396
Alexander Musmana5f070a2014-10-01 06:03:56 +00004397 assert((CurContext->isDependentContext() || B.builtAll()) &&
4398 "omp simd loop exprs were not built");
4399
Alexander Musman3276a272015-03-21 10:12:56 +00004400 if (!CurContext->isDependentContext()) {
4401 // Finalize the clauses that need pre-built expressions for CodeGen.
4402 for (auto C : Clauses) {
4403 if (auto LC = dyn_cast<OMPLinearClause>(C))
4404 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4405 B.NumIterations, *this, CurScope))
4406 return StmtError();
4407 }
4408 }
4409
Alexey Bataev66b15b52015-08-21 11:14:16 +00004410 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4411 // If both simdlen and safelen clauses are specified, the value of the simdlen
4412 // parameter must be less than or equal to the value of the safelen parameter.
4413 OMPSafelenClause *Safelen = nullptr;
4414 OMPSimdlenClause *Simdlen = nullptr;
4415 for (auto *Clause : Clauses) {
4416 if (Clause->getClauseKind() == OMPC_safelen)
4417 Safelen = cast<OMPSafelenClause>(Clause);
4418 else if (Clause->getClauseKind() == OMPC_simdlen)
4419 Simdlen = cast<OMPSimdlenClause>(Clause);
4420 if (Safelen && Simdlen)
4421 break;
4422 }
4423 if (Simdlen && Safelen &&
4424 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4425 Safelen->getSafelen()))
4426 return StmtError();
4427
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004428 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004429 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4430 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004431}
4432
Alexey Bataev4acb8592014-07-07 13:01:15 +00004433StmtResult Sema::ActOnOpenMPForDirective(
4434 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4435 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004436 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004437 if (!AStmt)
4438 return StmtError();
4439
4440 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004441 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004442 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4443 // define the nested loops number.
4444 unsigned NestedLoopCount = CheckOpenMPLoop(
4445 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4446 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004447 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004448 return StmtError();
4449
Alexander Musmana5f070a2014-10-01 06:03:56 +00004450 assert((CurContext->isDependentContext() || B.builtAll()) &&
4451 "omp for loop exprs were not built");
4452
Alexey Bataev54acd402015-08-04 11:18:19 +00004453 if (!CurContext->isDependentContext()) {
4454 // Finalize the clauses that need pre-built expressions for CodeGen.
4455 for (auto C : Clauses) {
4456 if (auto LC = dyn_cast<OMPLinearClause>(C))
4457 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4458 B.NumIterations, *this, CurScope))
4459 return StmtError();
4460 }
4461 }
4462
Alexey Bataevf29276e2014-06-18 04:14:57 +00004463 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004464 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004465 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004466}
4467
Alexander Musmanf82886e2014-09-18 05:12:34 +00004468StmtResult Sema::ActOnOpenMPForSimdDirective(
4469 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4470 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004471 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004472 if (!AStmt)
4473 return StmtError();
4474
4475 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004476 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004477 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4478 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004479 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004480 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4481 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4482 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004483 if (NestedLoopCount == 0)
4484 return StmtError();
4485
Alexander Musmanc6388682014-12-15 07:07:06 +00004486 assert((CurContext->isDependentContext() || B.builtAll()) &&
4487 "omp for simd loop exprs were not built");
4488
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004489 if (!CurContext->isDependentContext()) {
4490 // Finalize the clauses that need pre-built expressions for CodeGen.
4491 for (auto C : Clauses) {
4492 if (auto LC = dyn_cast<OMPLinearClause>(C))
4493 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4494 B.NumIterations, *this, CurScope))
4495 return StmtError();
4496 }
4497 }
4498
Alexey Bataev66b15b52015-08-21 11:14:16 +00004499 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4500 // If both simdlen and safelen clauses are specified, the value of the simdlen
4501 // parameter must be less than or equal to the value of the safelen parameter.
4502 OMPSafelenClause *Safelen = nullptr;
4503 OMPSimdlenClause *Simdlen = nullptr;
4504 for (auto *Clause : Clauses) {
4505 if (Clause->getClauseKind() == OMPC_safelen)
4506 Safelen = cast<OMPSafelenClause>(Clause);
4507 else if (Clause->getClauseKind() == OMPC_simdlen)
4508 Simdlen = cast<OMPSimdlenClause>(Clause);
4509 if (Safelen && Simdlen)
4510 break;
4511 }
4512 if (Simdlen && Safelen &&
4513 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4514 Safelen->getSafelen()))
4515 return StmtError();
4516
Alexander Musmanf82886e2014-09-18 05:12:34 +00004517 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004518 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4519 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004520}
4521
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004522StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4523 Stmt *AStmt,
4524 SourceLocation StartLoc,
4525 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004526 if (!AStmt)
4527 return StmtError();
4528
4529 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004530 auto BaseStmt = AStmt;
4531 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4532 BaseStmt = CS->getCapturedStmt();
4533 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4534 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004535 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004536 return StmtError();
4537 // All associated statements must be '#pragma omp section' except for
4538 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004539 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004540 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4541 if (SectionStmt)
4542 Diag(SectionStmt->getLocStart(),
4543 diag::err_omp_sections_substmt_not_section);
4544 return StmtError();
4545 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004546 cast<OMPSectionDirective>(SectionStmt)
4547 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004548 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004549 } else {
4550 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4551 return StmtError();
4552 }
4553
4554 getCurFunction()->setHasBranchProtectedScope();
4555
Alexey Bataev25e5b442015-09-15 12:52:43 +00004556 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4557 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004558}
4559
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004560StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4561 SourceLocation StartLoc,
4562 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004563 if (!AStmt)
4564 return StmtError();
4565
4566 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004567
4568 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004569 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004570
Alexey Bataev25e5b442015-09-15 12:52:43 +00004571 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4572 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004573}
4574
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004575StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4576 Stmt *AStmt,
4577 SourceLocation StartLoc,
4578 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004579 if (!AStmt)
4580 return StmtError();
4581
4582 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004583
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004584 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004585
Alexey Bataev3255bf32015-01-19 05:20:46 +00004586 // OpenMP [2.7.3, single Construct, Restrictions]
4587 // The copyprivate clause must not be used with the nowait clause.
4588 OMPClause *Nowait = nullptr;
4589 OMPClause *Copyprivate = nullptr;
4590 for (auto *Clause : Clauses) {
4591 if (Clause->getClauseKind() == OMPC_nowait)
4592 Nowait = Clause;
4593 else if (Clause->getClauseKind() == OMPC_copyprivate)
4594 Copyprivate = Clause;
4595 if (Copyprivate && Nowait) {
4596 Diag(Copyprivate->getLocStart(),
4597 diag::err_omp_single_copyprivate_with_nowait);
4598 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4599 return StmtError();
4600 }
4601 }
4602
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004603 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4604}
4605
Alexander Musman80c22892014-07-17 08:54:58 +00004606StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4607 SourceLocation StartLoc,
4608 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004609 if (!AStmt)
4610 return StmtError();
4611
4612 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004613
4614 getCurFunction()->setHasBranchProtectedScope();
4615
4616 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4617}
4618
Alexey Bataev28c75412015-12-15 08:19:24 +00004619StmtResult Sema::ActOnOpenMPCriticalDirective(
4620 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4621 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004622 if (!AStmt)
4623 return StmtError();
4624
4625 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004626
Alexey Bataev28c75412015-12-15 08:19:24 +00004627 bool ErrorFound = false;
4628 llvm::APSInt Hint;
4629 SourceLocation HintLoc;
4630 bool DependentHint = false;
4631 for (auto *C : Clauses) {
4632 if (C->getClauseKind() == OMPC_hint) {
4633 if (!DirName.getName()) {
4634 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4635 ErrorFound = true;
4636 }
4637 Expr *E = cast<OMPHintClause>(C)->getHint();
4638 if (E->isTypeDependent() || E->isValueDependent() ||
4639 E->isInstantiationDependent())
4640 DependentHint = true;
4641 else {
4642 Hint = E->EvaluateKnownConstInt(Context);
4643 HintLoc = C->getLocStart();
4644 }
4645 }
4646 }
4647 if (ErrorFound)
4648 return StmtError();
4649 auto Pair = DSAStack->getCriticalWithHint(DirName);
4650 if (Pair.first && DirName.getName() && !DependentHint) {
4651 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4652 Diag(StartLoc, diag::err_omp_critical_with_hint);
4653 if (HintLoc.isValid()) {
4654 Diag(HintLoc, diag::note_omp_critical_hint_here)
4655 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4656 } else
4657 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4658 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4659 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4660 << 1
4661 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4662 /*Radix=*/10, /*Signed=*/false);
4663 } else
4664 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4665 }
4666 }
4667
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004668 getCurFunction()->setHasBranchProtectedScope();
4669
Alexey Bataev28c75412015-12-15 08:19:24 +00004670 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4671 Clauses, AStmt);
4672 if (!Pair.first && DirName.getName() && !DependentHint)
4673 DSAStack->addCriticalWithHint(Dir, Hint);
4674 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004675}
4676
Alexey Bataev4acb8592014-07-07 13:01:15 +00004677StmtResult Sema::ActOnOpenMPParallelForDirective(
4678 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4679 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004680 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004681 if (!AStmt)
4682 return StmtError();
4683
Alexey Bataev4acb8592014-07-07 13:01:15 +00004684 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4685 // 1.2.2 OpenMP Language Terminology
4686 // Structured block - An executable statement with a single entry at the
4687 // top and a single exit at the bottom.
4688 // The point of exit cannot be a branch out of the structured block.
4689 // longjmp() and throw() must not violate the entry/exit criteria.
4690 CS->getCapturedDecl()->setNothrow();
4691
Alexander Musmanc6388682014-12-15 07:07:06 +00004692 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004693 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4694 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004695 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004696 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4697 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4698 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004699 if (NestedLoopCount == 0)
4700 return StmtError();
4701
Alexander Musmana5f070a2014-10-01 06:03:56 +00004702 assert((CurContext->isDependentContext() || B.builtAll()) &&
4703 "omp parallel for loop exprs were not built");
4704
Alexey Bataev54acd402015-08-04 11:18:19 +00004705 if (!CurContext->isDependentContext()) {
4706 // Finalize the clauses that need pre-built expressions for CodeGen.
4707 for (auto C : Clauses) {
4708 if (auto LC = dyn_cast<OMPLinearClause>(C))
4709 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4710 B.NumIterations, *this, CurScope))
4711 return StmtError();
4712 }
4713 }
4714
Alexey Bataev4acb8592014-07-07 13:01:15 +00004715 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004716 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004717 NestedLoopCount, Clauses, AStmt, B,
4718 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004719}
4720
Alexander Musmane4e893b2014-09-23 09:33:00 +00004721StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4722 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4723 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004724 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004725 if (!AStmt)
4726 return StmtError();
4727
Alexander Musmane4e893b2014-09-23 09:33:00 +00004728 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4729 // 1.2.2 OpenMP Language Terminology
4730 // Structured block - An executable statement with a single entry at the
4731 // top and a single exit at the bottom.
4732 // The point of exit cannot be a branch out of the structured block.
4733 // longjmp() and throw() must not violate the entry/exit criteria.
4734 CS->getCapturedDecl()->setNothrow();
4735
Alexander Musmanc6388682014-12-15 07:07:06 +00004736 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004737 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4738 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004739 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004740 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4741 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4742 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004743 if (NestedLoopCount == 0)
4744 return StmtError();
4745
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004746 if (!CurContext->isDependentContext()) {
4747 // Finalize the clauses that need pre-built expressions for CodeGen.
4748 for (auto C : Clauses) {
4749 if (auto LC = dyn_cast<OMPLinearClause>(C))
4750 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4751 B.NumIterations, *this, CurScope))
4752 return StmtError();
4753 }
4754 }
4755
Alexey Bataev66b15b52015-08-21 11:14:16 +00004756 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4757 // If both simdlen and safelen clauses are specified, the value of the simdlen
4758 // parameter must be less than or equal to the value of the safelen parameter.
4759 OMPSafelenClause *Safelen = nullptr;
4760 OMPSimdlenClause *Simdlen = nullptr;
4761 for (auto *Clause : Clauses) {
4762 if (Clause->getClauseKind() == OMPC_safelen)
4763 Safelen = cast<OMPSafelenClause>(Clause);
4764 else if (Clause->getClauseKind() == OMPC_simdlen)
4765 Simdlen = cast<OMPSimdlenClause>(Clause);
4766 if (Safelen && Simdlen)
4767 break;
4768 }
4769 if (Simdlen && Safelen &&
4770 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4771 Safelen->getSafelen()))
4772 return StmtError();
4773
Alexander Musmane4e893b2014-09-23 09:33:00 +00004774 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004775 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004776 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004777}
4778
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004779StmtResult
4780Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4781 Stmt *AStmt, SourceLocation StartLoc,
4782 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004783 if (!AStmt)
4784 return StmtError();
4785
4786 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004787 auto BaseStmt = AStmt;
4788 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4789 BaseStmt = CS->getCapturedStmt();
4790 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4791 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004792 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004793 return StmtError();
4794 // All associated statements must be '#pragma omp section' except for
4795 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004796 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004797 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4798 if (SectionStmt)
4799 Diag(SectionStmt->getLocStart(),
4800 diag::err_omp_parallel_sections_substmt_not_section);
4801 return StmtError();
4802 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004803 cast<OMPSectionDirective>(SectionStmt)
4804 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004805 }
4806 } else {
4807 Diag(AStmt->getLocStart(),
4808 diag::err_omp_parallel_sections_not_compound_stmt);
4809 return StmtError();
4810 }
4811
4812 getCurFunction()->setHasBranchProtectedScope();
4813
Alexey Bataev25e5b442015-09-15 12:52:43 +00004814 return OMPParallelSectionsDirective::Create(
4815 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004816}
4817
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004818StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4819 Stmt *AStmt, SourceLocation StartLoc,
4820 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004821 if (!AStmt)
4822 return StmtError();
4823
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004824 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4825 // 1.2.2 OpenMP Language Terminology
4826 // Structured block - An executable statement with a single entry at the
4827 // top and a single exit at the bottom.
4828 // The point of exit cannot be a branch out of the structured block.
4829 // longjmp() and throw() must not violate the entry/exit criteria.
4830 CS->getCapturedDecl()->setNothrow();
4831
4832 getCurFunction()->setHasBranchProtectedScope();
4833
Alexey Bataev25e5b442015-09-15 12:52:43 +00004834 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4835 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004836}
4837
Alexey Bataev68446b72014-07-18 07:47:19 +00004838StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4839 SourceLocation EndLoc) {
4840 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4841}
4842
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004843StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4844 SourceLocation EndLoc) {
4845 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4846}
4847
Alexey Bataev2df347a2014-07-18 10:17:07 +00004848StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4849 SourceLocation EndLoc) {
4850 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4851}
4852
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004853StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4854 SourceLocation StartLoc,
4855 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004856 if (!AStmt)
4857 return StmtError();
4858
4859 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004860
4861 getCurFunction()->setHasBranchProtectedScope();
4862
4863 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4864}
4865
Alexey Bataev6125da92014-07-21 11:26:11 +00004866StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4867 SourceLocation StartLoc,
4868 SourceLocation EndLoc) {
4869 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4870 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4871}
4872
Alexey Bataev346265e2015-09-25 10:37:12 +00004873StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4874 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004875 SourceLocation StartLoc,
4876 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004877 OMPClause *DependFound = nullptr;
4878 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004879 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004880 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004881 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004882 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004883 for (auto *C : Clauses) {
4884 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4885 DependFound = C;
4886 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4887 if (DependSourceClause) {
4888 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4889 << getOpenMPDirectiveName(OMPD_ordered)
4890 << getOpenMPClauseName(OMPC_depend) << 2;
4891 ErrorFound = true;
4892 } else
4893 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004894 if (DependSinkClause) {
4895 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4896 << 0;
4897 ErrorFound = true;
4898 }
4899 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4900 if (DependSourceClause) {
4901 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4902 << 1;
4903 ErrorFound = true;
4904 }
4905 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004906 }
4907 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004908 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004909 else if (C->getClauseKind() == OMPC_simd)
4910 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004911 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004912 if (!ErrorFound && !SC &&
4913 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004914 // OpenMP [2.8.1,simd Construct, Restrictions]
4915 // An ordered construct with the simd clause is the only OpenMP construct
4916 // that can appear in the simd region.
4917 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004918 ErrorFound = true;
4919 } else if (DependFound && (TC || SC)) {
4920 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4921 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4922 ErrorFound = true;
4923 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4924 Diag(DependFound->getLocStart(),
4925 diag::err_omp_ordered_directive_without_param);
4926 ErrorFound = true;
4927 } else if (TC || Clauses.empty()) {
4928 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4929 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4930 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4931 << (TC != nullptr);
4932 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4933 ErrorFound = true;
4934 }
4935 }
4936 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004937 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004938
4939 if (AStmt) {
4940 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4941
4942 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004943 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004944
4945 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004946}
4947
Alexey Bataev1d160b12015-03-13 12:27:31 +00004948namespace {
4949/// \brief Helper class for checking expression in 'omp atomic [update]'
4950/// construct.
4951class OpenMPAtomicUpdateChecker {
4952 /// \brief Error results for atomic update expressions.
4953 enum ExprAnalysisErrorCode {
4954 /// \brief A statement is not an expression statement.
4955 NotAnExpression,
4956 /// \brief Expression is not builtin binary or unary operation.
4957 NotABinaryOrUnaryExpression,
4958 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4959 NotAnUnaryIncDecExpression,
4960 /// \brief An expression is not of scalar type.
4961 NotAScalarType,
4962 /// \brief A binary operation is not an assignment operation.
4963 NotAnAssignmentOp,
4964 /// \brief RHS part of the binary operation is not a binary expression.
4965 NotABinaryExpression,
4966 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4967 /// expression.
4968 NotABinaryOperator,
4969 /// \brief RHS binary operation does not have reference to the updated LHS
4970 /// part.
4971 NotAnUpdateExpression,
4972 /// \brief No errors is found.
4973 NoError
4974 };
4975 /// \brief Reference to Sema.
4976 Sema &SemaRef;
4977 /// \brief A location for note diagnostics (when error is found).
4978 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004979 /// \brief 'x' lvalue part of the source atomic expression.
4980 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004981 /// \brief 'expr' rvalue part of the source atomic expression.
4982 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004983 /// \brief Helper expression of the form
4984 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4985 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4986 Expr *UpdateExpr;
4987 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4988 /// important for non-associative operations.
4989 bool IsXLHSInRHSPart;
4990 BinaryOperatorKind Op;
4991 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004992 /// \brief true if the source expression is a postfix unary operation, false
4993 /// if it is a prefix unary operation.
4994 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004995
4996public:
4997 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004998 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004999 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005000 /// \brief Check specified statement that it is suitable for 'atomic update'
5001 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005002 /// expression. If DiagId and NoteId == 0, then only check is performed
5003 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005004 /// \param DiagId Diagnostic which should be emitted if error is found.
5005 /// \param NoteId Diagnostic note for the main error message.
5006 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005007 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005008 /// \brief Return the 'x' lvalue part of the source atomic expression.
5009 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005010 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5011 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005012 /// \brief Return the update expression used in calculation of the updated
5013 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5014 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5015 Expr *getUpdateExpr() const { return UpdateExpr; }
5016 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5017 /// false otherwise.
5018 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5019
Alexey Bataevb78ca832015-04-01 03:33:17 +00005020 /// \brief true if the source expression is a postfix unary operation, false
5021 /// if it is a prefix unary operation.
5022 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5023
Alexey Bataev1d160b12015-03-13 12:27:31 +00005024private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005025 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5026 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005027};
5028} // namespace
5029
5030bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5031 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5032 ExprAnalysisErrorCode ErrorFound = NoError;
5033 SourceLocation ErrorLoc, NoteLoc;
5034 SourceRange ErrorRange, NoteRange;
5035 // Allowed constructs are:
5036 // x = x binop expr;
5037 // x = expr binop x;
5038 if (AtomicBinOp->getOpcode() == BO_Assign) {
5039 X = AtomicBinOp->getLHS();
5040 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5041 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5042 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5043 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5044 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005045 Op = AtomicInnerBinOp->getOpcode();
5046 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005047 auto *LHS = AtomicInnerBinOp->getLHS();
5048 auto *RHS = AtomicInnerBinOp->getRHS();
5049 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5050 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5051 /*Canonical=*/true);
5052 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5053 /*Canonical=*/true);
5054 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5055 /*Canonical=*/true);
5056 if (XId == LHSId) {
5057 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005058 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005059 } else if (XId == RHSId) {
5060 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005061 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005062 } else {
5063 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5064 ErrorRange = AtomicInnerBinOp->getSourceRange();
5065 NoteLoc = X->getExprLoc();
5066 NoteRange = X->getSourceRange();
5067 ErrorFound = NotAnUpdateExpression;
5068 }
5069 } else {
5070 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5071 ErrorRange = AtomicInnerBinOp->getSourceRange();
5072 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5073 NoteRange = SourceRange(NoteLoc, NoteLoc);
5074 ErrorFound = NotABinaryOperator;
5075 }
5076 } else {
5077 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5078 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5079 ErrorFound = NotABinaryExpression;
5080 }
5081 } else {
5082 ErrorLoc = AtomicBinOp->getExprLoc();
5083 ErrorRange = AtomicBinOp->getSourceRange();
5084 NoteLoc = AtomicBinOp->getOperatorLoc();
5085 NoteRange = SourceRange(NoteLoc, NoteLoc);
5086 ErrorFound = NotAnAssignmentOp;
5087 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005088 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005089 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5090 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5091 return true;
5092 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005093 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005094 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005095}
5096
5097bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5098 unsigned NoteId) {
5099 ExprAnalysisErrorCode ErrorFound = NoError;
5100 SourceLocation ErrorLoc, NoteLoc;
5101 SourceRange ErrorRange, NoteRange;
5102 // Allowed constructs are:
5103 // x++;
5104 // x--;
5105 // ++x;
5106 // --x;
5107 // x binop= expr;
5108 // x = x binop expr;
5109 // x = expr binop x;
5110 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5111 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5112 if (AtomicBody->getType()->isScalarType() ||
5113 AtomicBody->isInstantiationDependent()) {
5114 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5115 AtomicBody->IgnoreParenImpCasts())) {
5116 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005117 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005118 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005119 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005120 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005121 X = AtomicCompAssignOp->getLHS();
5122 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005123 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5124 AtomicBody->IgnoreParenImpCasts())) {
5125 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005126 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5127 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005128 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005129 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5130 // Check for Unary Operation
5131 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005132 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005133 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5134 OpLoc = AtomicUnaryOp->getOperatorLoc();
5135 X = AtomicUnaryOp->getSubExpr();
5136 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5137 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005138 } else {
5139 ErrorFound = NotAnUnaryIncDecExpression;
5140 ErrorLoc = AtomicUnaryOp->getExprLoc();
5141 ErrorRange = AtomicUnaryOp->getSourceRange();
5142 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5143 NoteRange = SourceRange(NoteLoc, NoteLoc);
5144 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005145 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005146 ErrorFound = NotABinaryOrUnaryExpression;
5147 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5148 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5149 }
5150 } else {
5151 ErrorFound = NotAScalarType;
5152 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5153 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5154 }
5155 } else {
5156 ErrorFound = NotAnExpression;
5157 NoteLoc = ErrorLoc = S->getLocStart();
5158 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5159 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005160 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005161 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5162 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5163 return true;
5164 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005165 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005166 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005167 // Build an update expression of form 'OpaqueValueExpr(x) binop
5168 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5169 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5170 auto *OVEX = new (SemaRef.getASTContext())
5171 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5172 auto *OVEExpr = new (SemaRef.getASTContext())
5173 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5174 auto Update =
5175 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5176 IsXLHSInRHSPart ? OVEExpr : OVEX);
5177 if (Update.isInvalid())
5178 return true;
5179 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5180 Sema::AA_Casting);
5181 if (Update.isInvalid())
5182 return true;
5183 UpdateExpr = Update.get();
5184 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005185 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005186}
5187
Alexey Bataev0162e452014-07-22 10:10:35 +00005188StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5189 Stmt *AStmt,
5190 SourceLocation StartLoc,
5191 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005192 if (!AStmt)
5193 return StmtError();
5194
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005195 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005196 // 1.2.2 OpenMP Language Terminology
5197 // Structured block - An executable statement with a single entry at the
5198 // top and a single exit at the bottom.
5199 // The point of exit cannot be a branch out of the structured block.
5200 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005201 OpenMPClauseKind AtomicKind = OMPC_unknown;
5202 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005203 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005204 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005205 C->getClauseKind() == OMPC_update ||
5206 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005207 if (AtomicKind != OMPC_unknown) {
5208 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5209 << SourceRange(C->getLocStart(), C->getLocEnd());
5210 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5211 << getOpenMPClauseName(AtomicKind);
5212 } else {
5213 AtomicKind = C->getClauseKind();
5214 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005215 }
5216 }
5217 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005218
Alexey Bataev459dec02014-07-24 06:46:57 +00005219 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005220 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5221 Body = EWC->getSubExpr();
5222
Alexey Bataev62cec442014-11-18 10:14:22 +00005223 Expr *X = nullptr;
5224 Expr *V = nullptr;
5225 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005226 Expr *UE = nullptr;
5227 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005228 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005229 // OpenMP [2.12.6, atomic Construct]
5230 // In the next expressions:
5231 // * x and v (as applicable) are both l-value expressions with scalar type.
5232 // * During the execution of an atomic region, multiple syntactic
5233 // occurrences of x must designate the same storage location.
5234 // * Neither of v and expr (as applicable) may access the storage location
5235 // designated by x.
5236 // * Neither of x and expr (as applicable) may access the storage location
5237 // designated by v.
5238 // * expr is an expression with scalar type.
5239 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5240 // * binop, binop=, ++, and -- are not overloaded operators.
5241 // * The expression x binop expr must be numerically equivalent to x binop
5242 // (expr). This requirement is satisfied if the operators in expr have
5243 // precedence greater than binop, or by using parentheses around expr or
5244 // subexpressions of expr.
5245 // * The expression expr binop x must be numerically equivalent to (expr)
5246 // binop x. This requirement is satisfied if the operators in expr have
5247 // precedence equal to or greater than binop, or by using parentheses around
5248 // expr or subexpressions of expr.
5249 // * For forms that allow multiple occurrences of x, the number of times
5250 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005251 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005252 enum {
5253 NotAnExpression,
5254 NotAnAssignmentOp,
5255 NotAScalarType,
5256 NotAnLValue,
5257 NoError
5258 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005259 SourceLocation ErrorLoc, NoteLoc;
5260 SourceRange ErrorRange, NoteRange;
5261 // If clause is read:
5262 // v = x;
5263 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5264 auto AtomicBinOp =
5265 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5266 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5267 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5268 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5269 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5270 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5271 if (!X->isLValue() || !V->isLValue()) {
5272 auto NotLValueExpr = X->isLValue() ? V : X;
5273 ErrorFound = NotAnLValue;
5274 ErrorLoc = AtomicBinOp->getExprLoc();
5275 ErrorRange = AtomicBinOp->getSourceRange();
5276 NoteLoc = NotLValueExpr->getExprLoc();
5277 NoteRange = NotLValueExpr->getSourceRange();
5278 }
5279 } else if (!X->isInstantiationDependent() ||
5280 !V->isInstantiationDependent()) {
5281 auto NotScalarExpr =
5282 (X->isInstantiationDependent() || X->getType()->isScalarType())
5283 ? V
5284 : X;
5285 ErrorFound = NotAScalarType;
5286 ErrorLoc = AtomicBinOp->getExprLoc();
5287 ErrorRange = AtomicBinOp->getSourceRange();
5288 NoteLoc = NotScalarExpr->getExprLoc();
5289 NoteRange = NotScalarExpr->getSourceRange();
5290 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005291 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005292 ErrorFound = NotAnAssignmentOp;
5293 ErrorLoc = AtomicBody->getExprLoc();
5294 ErrorRange = AtomicBody->getSourceRange();
5295 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5296 : AtomicBody->getExprLoc();
5297 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5298 : AtomicBody->getSourceRange();
5299 }
5300 } else {
5301 ErrorFound = NotAnExpression;
5302 NoteLoc = ErrorLoc = Body->getLocStart();
5303 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005304 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005305 if (ErrorFound != NoError) {
5306 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5307 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005308 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5309 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005310 return StmtError();
5311 } else if (CurContext->isDependentContext())
5312 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005313 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005314 enum {
5315 NotAnExpression,
5316 NotAnAssignmentOp,
5317 NotAScalarType,
5318 NotAnLValue,
5319 NoError
5320 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005321 SourceLocation ErrorLoc, NoteLoc;
5322 SourceRange ErrorRange, NoteRange;
5323 // If clause is write:
5324 // x = expr;
5325 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5326 auto AtomicBinOp =
5327 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5328 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005329 X = AtomicBinOp->getLHS();
5330 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005331 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5332 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5333 if (!X->isLValue()) {
5334 ErrorFound = NotAnLValue;
5335 ErrorLoc = AtomicBinOp->getExprLoc();
5336 ErrorRange = AtomicBinOp->getSourceRange();
5337 NoteLoc = X->getExprLoc();
5338 NoteRange = X->getSourceRange();
5339 }
5340 } else if (!X->isInstantiationDependent() ||
5341 !E->isInstantiationDependent()) {
5342 auto NotScalarExpr =
5343 (X->isInstantiationDependent() || X->getType()->isScalarType())
5344 ? E
5345 : X;
5346 ErrorFound = NotAScalarType;
5347 ErrorLoc = AtomicBinOp->getExprLoc();
5348 ErrorRange = AtomicBinOp->getSourceRange();
5349 NoteLoc = NotScalarExpr->getExprLoc();
5350 NoteRange = NotScalarExpr->getSourceRange();
5351 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005352 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005353 ErrorFound = NotAnAssignmentOp;
5354 ErrorLoc = AtomicBody->getExprLoc();
5355 ErrorRange = AtomicBody->getSourceRange();
5356 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5357 : AtomicBody->getExprLoc();
5358 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5359 : AtomicBody->getSourceRange();
5360 }
5361 } else {
5362 ErrorFound = NotAnExpression;
5363 NoteLoc = ErrorLoc = Body->getLocStart();
5364 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005365 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005366 if (ErrorFound != NoError) {
5367 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5368 << ErrorRange;
5369 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5370 << NoteRange;
5371 return StmtError();
5372 } else if (CurContext->isDependentContext())
5373 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005374 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005375 // If clause is update:
5376 // x++;
5377 // x--;
5378 // ++x;
5379 // --x;
5380 // x binop= expr;
5381 // x = x binop expr;
5382 // x = expr binop x;
5383 OpenMPAtomicUpdateChecker Checker(*this);
5384 if (Checker.checkStatement(
5385 Body, (AtomicKind == OMPC_update)
5386 ? diag::err_omp_atomic_update_not_expression_statement
5387 : diag::err_omp_atomic_not_expression_statement,
5388 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005389 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005390 if (!CurContext->isDependentContext()) {
5391 E = Checker.getExpr();
5392 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005393 UE = Checker.getUpdateExpr();
5394 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005395 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005396 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005397 enum {
5398 NotAnAssignmentOp,
5399 NotACompoundStatement,
5400 NotTwoSubstatements,
5401 NotASpecificExpression,
5402 NoError
5403 } ErrorFound = NoError;
5404 SourceLocation ErrorLoc, NoteLoc;
5405 SourceRange ErrorRange, NoteRange;
5406 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5407 // If clause is a capture:
5408 // v = x++;
5409 // v = x--;
5410 // v = ++x;
5411 // v = --x;
5412 // v = x binop= expr;
5413 // v = x = x binop expr;
5414 // v = x = expr binop x;
5415 auto *AtomicBinOp =
5416 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5417 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5418 V = AtomicBinOp->getLHS();
5419 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5420 OpenMPAtomicUpdateChecker Checker(*this);
5421 if (Checker.checkStatement(
5422 Body, diag::err_omp_atomic_capture_not_expression_statement,
5423 diag::note_omp_atomic_update))
5424 return StmtError();
5425 E = Checker.getExpr();
5426 X = Checker.getX();
5427 UE = Checker.getUpdateExpr();
5428 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5429 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005430 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005431 ErrorLoc = AtomicBody->getExprLoc();
5432 ErrorRange = AtomicBody->getSourceRange();
5433 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5434 : AtomicBody->getExprLoc();
5435 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5436 : AtomicBody->getSourceRange();
5437 ErrorFound = NotAnAssignmentOp;
5438 }
5439 if (ErrorFound != NoError) {
5440 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5441 << ErrorRange;
5442 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5443 return StmtError();
5444 } else if (CurContext->isDependentContext()) {
5445 UE = V = E = X = nullptr;
5446 }
5447 } else {
5448 // If clause is a capture:
5449 // { v = x; x = expr; }
5450 // { v = x; x++; }
5451 // { v = x; x--; }
5452 // { v = x; ++x; }
5453 // { v = x; --x; }
5454 // { v = x; x binop= expr; }
5455 // { v = x; x = x binop expr; }
5456 // { v = x; x = expr binop x; }
5457 // { x++; v = x; }
5458 // { x--; v = x; }
5459 // { ++x; v = x; }
5460 // { --x; v = x; }
5461 // { x binop= expr; v = x; }
5462 // { x = x binop expr; v = x; }
5463 // { x = expr binop x; v = x; }
5464 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5465 // Check that this is { expr1; expr2; }
5466 if (CS->size() == 2) {
5467 auto *First = CS->body_front();
5468 auto *Second = CS->body_back();
5469 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5470 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5471 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5472 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5473 // Need to find what subexpression is 'v' and what is 'x'.
5474 OpenMPAtomicUpdateChecker Checker(*this);
5475 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5476 BinaryOperator *BinOp = nullptr;
5477 if (IsUpdateExprFound) {
5478 BinOp = dyn_cast<BinaryOperator>(First);
5479 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5480 }
5481 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5482 // { v = x; x++; }
5483 // { v = x; x--; }
5484 // { v = x; ++x; }
5485 // { v = x; --x; }
5486 // { v = x; x binop= expr; }
5487 // { v = x; x = x binop expr; }
5488 // { v = x; x = expr binop x; }
5489 // Check that the first expression has form v = x.
5490 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5491 llvm::FoldingSetNodeID XId, PossibleXId;
5492 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5493 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5494 IsUpdateExprFound = XId == PossibleXId;
5495 if (IsUpdateExprFound) {
5496 V = BinOp->getLHS();
5497 X = Checker.getX();
5498 E = Checker.getExpr();
5499 UE = Checker.getUpdateExpr();
5500 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005501 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005502 }
5503 }
5504 if (!IsUpdateExprFound) {
5505 IsUpdateExprFound = !Checker.checkStatement(First);
5506 BinOp = nullptr;
5507 if (IsUpdateExprFound) {
5508 BinOp = dyn_cast<BinaryOperator>(Second);
5509 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5510 }
5511 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5512 // { x++; v = x; }
5513 // { x--; v = x; }
5514 // { ++x; v = x; }
5515 // { --x; v = x; }
5516 // { x binop= expr; v = x; }
5517 // { x = x binop expr; v = x; }
5518 // { x = expr binop x; v = x; }
5519 // Check that the second expression has form v = x.
5520 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5521 llvm::FoldingSetNodeID XId, PossibleXId;
5522 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5523 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5524 IsUpdateExprFound = XId == PossibleXId;
5525 if (IsUpdateExprFound) {
5526 V = BinOp->getLHS();
5527 X = Checker.getX();
5528 E = Checker.getExpr();
5529 UE = Checker.getUpdateExpr();
5530 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005531 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005532 }
5533 }
5534 }
5535 if (!IsUpdateExprFound) {
5536 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005537 auto *FirstExpr = dyn_cast<Expr>(First);
5538 auto *SecondExpr = dyn_cast<Expr>(Second);
5539 if (!FirstExpr || !SecondExpr ||
5540 !(FirstExpr->isInstantiationDependent() ||
5541 SecondExpr->isInstantiationDependent())) {
5542 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5543 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005544 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005545 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5546 : First->getLocStart();
5547 NoteRange = ErrorRange = FirstBinOp
5548 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005549 : SourceRange(ErrorLoc, ErrorLoc);
5550 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005551 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5552 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5553 ErrorFound = NotAnAssignmentOp;
5554 NoteLoc = ErrorLoc = SecondBinOp
5555 ? SecondBinOp->getOperatorLoc()
5556 : Second->getLocStart();
5557 NoteRange = ErrorRange =
5558 SecondBinOp ? SecondBinOp->getSourceRange()
5559 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005560 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005561 auto *PossibleXRHSInFirst =
5562 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5563 auto *PossibleXLHSInSecond =
5564 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5565 llvm::FoldingSetNodeID X1Id, X2Id;
5566 PossibleXRHSInFirst->Profile(X1Id, Context,
5567 /*Canonical=*/true);
5568 PossibleXLHSInSecond->Profile(X2Id, Context,
5569 /*Canonical=*/true);
5570 IsUpdateExprFound = X1Id == X2Id;
5571 if (IsUpdateExprFound) {
5572 V = FirstBinOp->getLHS();
5573 X = SecondBinOp->getLHS();
5574 E = SecondBinOp->getRHS();
5575 UE = nullptr;
5576 IsXLHSInRHSPart = false;
5577 IsPostfixUpdate = true;
5578 } else {
5579 ErrorFound = NotASpecificExpression;
5580 ErrorLoc = FirstBinOp->getExprLoc();
5581 ErrorRange = FirstBinOp->getSourceRange();
5582 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5583 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5584 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005585 }
5586 }
5587 }
5588 }
5589 } else {
5590 NoteLoc = ErrorLoc = Body->getLocStart();
5591 NoteRange = ErrorRange =
5592 SourceRange(Body->getLocStart(), Body->getLocStart());
5593 ErrorFound = NotTwoSubstatements;
5594 }
5595 } else {
5596 NoteLoc = ErrorLoc = Body->getLocStart();
5597 NoteRange = ErrorRange =
5598 SourceRange(Body->getLocStart(), Body->getLocStart());
5599 ErrorFound = NotACompoundStatement;
5600 }
5601 if (ErrorFound != NoError) {
5602 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5603 << ErrorRange;
5604 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5605 return StmtError();
5606 } else if (CurContext->isDependentContext()) {
5607 UE = V = E = X = nullptr;
5608 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005609 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005610 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005611
5612 getCurFunction()->setHasBranchProtectedScope();
5613
Alexey Bataev62cec442014-11-18 10:14:22 +00005614 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005615 X, V, E, UE, IsXLHSInRHSPart,
5616 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005617}
5618
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005619StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5620 Stmt *AStmt,
5621 SourceLocation StartLoc,
5622 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005623 if (!AStmt)
5624 return StmtError();
5625
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005626 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5627 // 1.2.2 OpenMP Language Terminology
5628 // Structured block - An executable statement with a single entry at the
5629 // top and a single exit at the bottom.
5630 // The point of exit cannot be a branch out of the structured block.
5631 // longjmp() and throw() must not violate the entry/exit criteria.
5632 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005633
Alexey Bataev13314bf2014-10-09 04:18:56 +00005634 // OpenMP [2.16, Nesting of Regions]
5635 // If specified, a teams construct must be contained within a target
5636 // construct. That target construct must contain no statements or directives
5637 // outside of the teams construct.
5638 if (DSAStack->hasInnerTeamsRegion()) {
5639 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5640 bool OMPTeamsFound = true;
5641 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5642 auto I = CS->body_begin();
5643 while (I != CS->body_end()) {
5644 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5645 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5646 OMPTeamsFound = false;
5647 break;
5648 }
5649 ++I;
5650 }
5651 assert(I != CS->body_end() && "Not found statement");
5652 S = *I;
5653 }
5654 if (!OMPTeamsFound) {
5655 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5656 Diag(DSAStack->getInnerTeamsRegionLoc(),
5657 diag::note_omp_nested_teams_construct_here);
5658 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5659 << isa<OMPExecutableDirective>(S);
5660 return StmtError();
5661 }
5662 }
5663
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005664 getCurFunction()->setHasBranchProtectedScope();
5665
5666 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5667}
5668
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005669StmtResult
5670Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5671 Stmt *AStmt, SourceLocation StartLoc,
5672 SourceLocation EndLoc) {
5673 if (!AStmt)
5674 return StmtError();
5675
5676 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5677 // 1.2.2 OpenMP Language Terminology
5678 // Structured block - An executable statement with a single entry at the
5679 // top and a single exit at the bottom.
5680 // The point of exit cannot be a branch out of the structured block.
5681 // longjmp() and throw() must not violate the entry/exit criteria.
5682 CS->getCapturedDecl()->setNothrow();
5683
5684 getCurFunction()->setHasBranchProtectedScope();
5685
5686 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5687 AStmt);
5688}
5689
Samuel Antaodf67fc42016-01-19 19:15:56 +00005690/// \brief Check for existence of a map clause in the list of clauses.
5691static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5692 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5693 I != E; ++I) {
5694 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5695 return true;
5696 }
5697 }
5698
5699 return false;
5700}
5701
Michael Wong65f367f2015-07-21 13:44:28 +00005702StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5703 Stmt *AStmt,
5704 SourceLocation StartLoc,
5705 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005706 if (!AStmt)
5707 return StmtError();
5708
5709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5710
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005711 // OpenMP [2.10.1, Restrictions, p. 97]
5712 // At least one map clause must appear on the directive.
5713 if (!HasMapClause(Clauses)) {
5714 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5715 getOpenMPDirectiveName(OMPD_target_data);
5716 return StmtError();
5717 }
5718
Michael Wong65f367f2015-07-21 13:44:28 +00005719 getCurFunction()->setHasBranchProtectedScope();
5720
5721 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5722 AStmt);
5723}
5724
Samuel Antaodf67fc42016-01-19 19:15:56 +00005725StmtResult
5726Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5727 SourceLocation StartLoc,
5728 SourceLocation EndLoc) {
5729 // OpenMP [2.10.2, Restrictions, p. 99]
5730 // At least one map clause must appear on the directive.
5731 if (!HasMapClause(Clauses)) {
5732 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5733 << getOpenMPDirectiveName(OMPD_target_enter_data);
5734 return StmtError();
5735 }
5736
5737 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5738 Clauses);
5739}
5740
Samuel Antao72590762016-01-19 20:04:50 +00005741StmtResult
5742Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5743 SourceLocation StartLoc,
5744 SourceLocation EndLoc) {
5745 // OpenMP [2.10.3, Restrictions, p. 102]
5746 // At least one map clause must appear on the directive.
5747 if (!HasMapClause(Clauses)) {
5748 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5749 << getOpenMPDirectiveName(OMPD_target_exit_data);
5750 return StmtError();
5751 }
5752
5753 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5754}
5755
Alexey Bataev13314bf2014-10-09 04:18:56 +00005756StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5757 Stmt *AStmt, SourceLocation StartLoc,
5758 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005759 if (!AStmt)
5760 return StmtError();
5761
Alexey Bataev13314bf2014-10-09 04:18:56 +00005762 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5763 // 1.2.2 OpenMP Language Terminology
5764 // Structured block - An executable statement with a single entry at the
5765 // top and a single exit at the bottom.
5766 // The point of exit cannot be a branch out of the structured block.
5767 // longjmp() and throw() must not violate the entry/exit criteria.
5768 CS->getCapturedDecl()->setNothrow();
5769
5770 getCurFunction()->setHasBranchProtectedScope();
5771
5772 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5773}
5774
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005775StmtResult
5776Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5777 SourceLocation EndLoc,
5778 OpenMPDirectiveKind CancelRegion) {
5779 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5780 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5781 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5782 << getOpenMPDirectiveName(CancelRegion);
5783 return StmtError();
5784 }
5785 if (DSAStack->isParentNowaitRegion()) {
5786 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5787 return StmtError();
5788 }
5789 if (DSAStack->isParentOrderedRegion()) {
5790 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5791 return StmtError();
5792 }
5793 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5794 CancelRegion);
5795}
5796
Alexey Bataev87933c72015-09-18 08:07:34 +00005797StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5798 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005799 SourceLocation EndLoc,
5800 OpenMPDirectiveKind CancelRegion) {
5801 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5802 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5803 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5804 << getOpenMPDirectiveName(CancelRegion);
5805 return StmtError();
5806 }
5807 if (DSAStack->isParentNowaitRegion()) {
5808 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5809 return StmtError();
5810 }
5811 if (DSAStack->isParentOrderedRegion()) {
5812 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5813 return StmtError();
5814 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005815 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005816 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5817 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005818}
5819
Alexey Bataev382967a2015-12-08 12:06:20 +00005820static bool checkGrainsizeNumTasksClauses(Sema &S,
5821 ArrayRef<OMPClause *> Clauses) {
5822 OMPClause *PrevClause = nullptr;
5823 bool ErrorFound = false;
5824 for (auto *C : Clauses) {
5825 if (C->getClauseKind() == OMPC_grainsize ||
5826 C->getClauseKind() == OMPC_num_tasks) {
5827 if (!PrevClause)
5828 PrevClause = C;
5829 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5830 S.Diag(C->getLocStart(),
5831 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5832 << getOpenMPClauseName(C->getClauseKind())
5833 << getOpenMPClauseName(PrevClause->getClauseKind());
5834 S.Diag(PrevClause->getLocStart(),
5835 diag::note_omp_previous_grainsize_num_tasks)
5836 << getOpenMPClauseName(PrevClause->getClauseKind());
5837 ErrorFound = true;
5838 }
5839 }
5840 }
5841 return ErrorFound;
5842}
5843
Alexey Bataev49f6e782015-12-01 04:18:41 +00005844StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5845 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5846 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005847 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005848 if (!AStmt)
5849 return StmtError();
5850
5851 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5852 OMPLoopDirective::HelperExprs B;
5853 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5854 // define the nested loops number.
5855 unsigned NestedLoopCount =
5856 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005857 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005858 VarsWithImplicitDSA, B);
5859 if (NestedLoopCount == 0)
5860 return StmtError();
5861
5862 assert((CurContext->isDependentContext() || B.builtAll()) &&
5863 "omp for loop exprs were not built");
5864
Alexey Bataev382967a2015-12-08 12:06:20 +00005865 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5866 // The grainsize clause and num_tasks clause are mutually exclusive and may
5867 // not appear on the same taskloop directive.
5868 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5869 return StmtError();
5870
Alexey Bataev49f6e782015-12-01 04:18:41 +00005871 getCurFunction()->setHasBranchProtectedScope();
5872 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5873 NestedLoopCount, Clauses, AStmt, B);
5874}
5875
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005876StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5877 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5878 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005879 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005880 if (!AStmt)
5881 return StmtError();
5882
5883 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5884 OMPLoopDirective::HelperExprs B;
5885 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5886 // define the nested loops number.
5887 unsigned NestedLoopCount =
5888 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5889 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5890 VarsWithImplicitDSA, B);
5891 if (NestedLoopCount == 0)
5892 return StmtError();
5893
5894 assert((CurContext->isDependentContext() || B.builtAll()) &&
5895 "omp for loop exprs were not built");
5896
Alexey Bataev382967a2015-12-08 12:06:20 +00005897 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5898 // The grainsize clause and num_tasks clause are mutually exclusive and may
5899 // not appear on the same taskloop directive.
5900 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5901 return StmtError();
5902
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005903 getCurFunction()->setHasBranchProtectedScope();
5904 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5905 NestedLoopCount, Clauses, AStmt, B);
5906}
5907
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005908StmtResult Sema::ActOnOpenMPDistributeDirective(
5909 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5910 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005911 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005912 if (!AStmt)
5913 return StmtError();
5914
5915 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5916 OMPLoopDirective::HelperExprs B;
5917 // In presence of clause 'collapse' with number of loops, it will
5918 // define the nested loops number.
5919 unsigned NestedLoopCount =
5920 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5921 nullptr /*ordered not a clause on distribute*/, AStmt,
5922 *this, *DSAStack, VarsWithImplicitDSA, B);
5923 if (NestedLoopCount == 0)
5924 return StmtError();
5925
5926 assert((CurContext->isDependentContext() || B.builtAll()) &&
5927 "omp for loop exprs were not built");
5928
5929 getCurFunction()->setHasBranchProtectedScope();
5930 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5931 NestedLoopCount, Clauses, AStmt, B);
5932}
5933
Alexey Bataeved09d242014-05-28 05:53:51 +00005934OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005935 SourceLocation StartLoc,
5936 SourceLocation LParenLoc,
5937 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005938 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005939 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005940 case OMPC_final:
5941 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5942 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005943 case OMPC_num_threads:
5944 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5945 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005946 case OMPC_safelen:
5947 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5948 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005949 case OMPC_simdlen:
5950 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5951 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005952 case OMPC_collapse:
5953 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5954 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005955 case OMPC_ordered:
5956 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5957 break;
Michael Wonge710d542015-08-07 16:16:36 +00005958 case OMPC_device:
5959 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5960 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005961 case OMPC_num_teams:
5962 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5963 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005964 case OMPC_thread_limit:
5965 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5966 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005967 case OMPC_priority:
5968 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5969 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005970 case OMPC_grainsize:
5971 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5972 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005973 case OMPC_num_tasks:
5974 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5975 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005976 case OMPC_hint:
5977 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5978 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005979 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005980 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005981 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005982 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005983 case OMPC_private:
5984 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005985 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005986 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005987 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005988 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005989 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005990 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005991 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005992 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005993 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005994 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005995 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005996 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005997 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005998 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005999 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006000 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006001 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006002 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006003 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006004 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006005 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006006 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006007 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006008 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006009 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006010 llvm_unreachable("Clause is not allowed.");
6011 }
6012 return Res;
6013}
6014
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006015OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6016 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006017 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006018 SourceLocation NameModifierLoc,
6019 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006020 SourceLocation EndLoc) {
6021 Expr *ValExpr = Condition;
6022 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6023 !Condition->isInstantiationDependent() &&
6024 !Condition->containsUnexpandedParameterPack()) {
6025 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006026 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006027 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006028 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006029
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006030 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006031 }
6032
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006033 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6034 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006035}
6036
Alexey Bataev3778b602014-07-17 07:32:53 +00006037OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6038 SourceLocation StartLoc,
6039 SourceLocation LParenLoc,
6040 SourceLocation EndLoc) {
6041 Expr *ValExpr = Condition;
6042 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6043 !Condition->isInstantiationDependent() &&
6044 !Condition->containsUnexpandedParameterPack()) {
6045 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6046 Condition->getExprLoc(), Condition);
6047 if (Val.isInvalid())
6048 return nullptr;
6049
6050 ValExpr = Val.get();
6051 }
6052
6053 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6054}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006055ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6056 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006057 if (!Op)
6058 return ExprError();
6059
6060 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6061 public:
6062 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006063 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006064 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6065 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006066 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6067 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006068 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6069 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006070 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6071 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006072 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6073 QualType T,
6074 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006075 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6076 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006077 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6078 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006079 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006080 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006081 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006082 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6083 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006084 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6085 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006086 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6087 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006088 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006089 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006090 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006091 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6092 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006093 llvm_unreachable("conversion functions are permitted");
6094 }
6095 } ConvertDiagnoser;
6096 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6097}
6098
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006099static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006100 OpenMPClauseKind CKind,
6101 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006102 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6103 !ValExpr->isInstantiationDependent()) {
6104 SourceLocation Loc = ValExpr->getExprLoc();
6105 ExprResult Value =
6106 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6107 if (Value.isInvalid())
6108 return false;
6109
6110 ValExpr = Value.get();
6111 // The expression must evaluate to a non-negative integer value.
6112 llvm::APSInt Result;
6113 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006114 Result.isSigned() &&
6115 !((!StrictlyPositive && Result.isNonNegative()) ||
6116 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006117 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006118 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6119 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006120 return false;
6121 }
6122 }
6123 return true;
6124}
6125
Alexey Bataev568a8332014-03-06 06:15:19 +00006126OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6127 SourceLocation StartLoc,
6128 SourceLocation LParenLoc,
6129 SourceLocation EndLoc) {
6130 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006131
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006132 // OpenMP [2.5, Restrictions]
6133 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006134 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6135 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006136 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006137
Alexey Bataeved09d242014-05-28 05:53:51 +00006138 return new (Context)
6139 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006140}
6141
Alexey Bataev62c87d22014-03-21 04:51:18 +00006142ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006143 OpenMPClauseKind CKind,
6144 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006145 if (!E)
6146 return ExprError();
6147 if (E->isValueDependent() || E->isTypeDependent() ||
6148 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006149 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006150 llvm::APSInt Result;
6151 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6152 if (ICE.isInvalid())
6153 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006154 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6155 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006156 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006157 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6158 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006159 return ExprError();
6160 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006161 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6162 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6163 << E->getSourceRange();
6164 return ExprError();
6165 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006166 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6167 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006168 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006169 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006170 return ICE;
6171}
6172
6173OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6174 SourceLocation LParenLoc,
6175 SourceLocation EndLoc) {
6176 // OpenMP [2.8.1, simd construct, Description]
6177 // The parameter of the safelen clause must be a constant
6178 // positive integer expression.
6179 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6180 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006181 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006182 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006183 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006184}
6185
Alexey Bataev66b15b52015-08-21 11:14:16 +00006186OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6187 SourceLocation LParenLoc,
6188 SourceLocation EndLoc) {
6189 // OpenMP [2.8.1, simd construct, Description]
6190 // The parameter of the simdlen clause must be a constant
6191 // positive integer expression.
6192 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6193 if (Simdlen.isInvalid())
6194 return nullptr;
6195 return new (Context)
6196 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6197}
6198
Alexander Musman64d33f12014-06-04 07:53:32 +00006199OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6200 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006201 SourceLocation LParenLoc,
6202 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006203 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006204 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006205 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006206 // The parameter of the collapse clause must be a constant
6207 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006208 ExprResult NumForLoopsResult =
6209 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6210 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006211 return nullptr;
6212 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006213 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006214}
6215
Alexey Bataev10e775f2015-07-30 11:36:16 +00006216OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6217 SourceLocation EndLoc,
6218 SourceLocation LParenLoc,
6219 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006220 // OpenMP [2.7.1, loop construct, Description]
6221 // OpenMP [2.8.1, simd construct, Description]
6222 // OpenMP [2.9.6, distribute construct, Description]
6223 // The parameter of the ordered clause must be a constant
6224 // positive integer expression if any.
6225 if (NumForLoops && LParenLoc.isValid()) {
6226 ExprResult NumForLoopsResult =
6227 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6228 if (NumForLoopsResult.isInvalid())
6229 return nullptr;
6230 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006231 } else
6232 NumForLoops = nullptr;
6233 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006234 return new (Context)
6235 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6236}
6237
Alexey Bataeved09d242014-05-28 05:53:51 +00006238OMPClause *Sema::ActOnOpenMPSimpleClause(
6239 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6240 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006241 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006242 switch (Kind) {
6243 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006244 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006245 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6246 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006247 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006248 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006249 Res = ActOnOpenMPProcBindClause(
6250 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6251 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006252 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006253 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006254 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006255 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006256 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006257 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006258 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006259 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006260 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006261 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006262 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006263 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006264 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006265 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006266 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006267 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006268 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006269 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006270 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006271 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006272 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006273 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006274 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006275 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006276 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006277 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006278 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006279 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006280 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006281 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006282 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006283 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006284 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006285 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006286 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006287 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006288 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006289 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006290 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006291 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006292 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006293 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006294 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006295 llvm_unreachable("Clause is not allowed.");
6296 }
6297 return Res;
6298}
6299
Alexey Bataev6402bca2015-12-28 07:25:51 +00006300static std::string
6301getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6302 ArrayRef<unsigned> Exclude = llvm::None) {
6303 std::string Values;
6304 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6305 unsigned Skipped = Exclude.size();
6306 auto S = Exclude.begin(), E = Exclude.end();
6307 for (unsigned i = First; i < Last; ++i) {
6308 if (std::find(S, E, i) != E) {
6309 --Skipped;
6310 continue;
6311 }
6312 Values += "'";
6313 Values += getOpenMPSimpleClauseTypeName(K, i);
6314 Values += "'";
6315 if (i == Bound - Skipped)
6316 Values += " or ";
6317 else if (i != Bound + 1 - Skipped)
6318 Values += ", ";
6319 }
6320 return Values;
6321}
6322
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006323OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6324 SourceLocation KindKwLoc,
6325 SourceLocation StartLoc,
6326 SourceLocation LParenLoc,
6327 SourceLocation EndLoc) {
6328 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006329 static_assert(OMPC_DEFAULT_unknown > 0,
6330 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006331 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006332 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6333 /*Last=*/OMPC_DEFAULT_unknown)
6334 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006335 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006336 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006337 switch (Kind) {
6338 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006339 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006340 break;
6341 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006342 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006343 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006344 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006345 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006346 break;
6347 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006348 return new (Context)
6349 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006350}
6351
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006352OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6353 SourceLocation KindKwLoc,
6354 SourceLocation StartLoc,
6355 SourceLocation LParenLoc,
6356 SourceLocation EndLoc) {
6357 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006358 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006359 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6360 /*Last=*/OMPC_PROC_BIND_unknown)
6361 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006362 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006363 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006364 return new (Context)
6365 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006366}
6367
Alexey Bataev56dafe82014-06-20 07:16:17 +00006368OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006369 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006370 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006371 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006372 SourceLocation EndLoc) {
6373 OMPClause *Res = nullptr;
6374 switch (Kind) {
6375 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006376 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6377 assert(Argument.size() == NumberOfElements &&
6378 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006379 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006380 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6381 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6382 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6383 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6384 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006385 break;
6386 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006387 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6388 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6389 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6390 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006391 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006392 case OMPC_dist_schedule:
6393 Res = ActOnOpenMPDistScheduleClause(
6394 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6395 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6396 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006397 case OMPC_defaultmap:
6398 enum { Modifier, DefaultmapKind };
6399 Res = ActOnOpenMPDefaultmapClause(
6400 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6401 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6402 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6403 ArgumentLoc[DefaultmapKind], EndLoc);
6404 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006405 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006406 case OMPC_num_threads:
6407 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006408 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006409 case OMPC_collapse:
6410 case OMPC_default:
6411 case OMPC_proc_bind:
6412 case OMPC_private:
6413 case OMPC_firstprivate:
6414 case OMPC_lastprivate:
6415 case OMPC_shared:
6416 case OMPC_reduction:
6417 case OMPC_linear:
6418 case OMPC_aligned:
6419 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006420 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006421 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006422 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006423 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006424 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006425 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006426 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006427 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006428 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006429 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006430 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006431 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006432 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006433 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006434 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006435 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006436 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006437 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006438 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006439 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006440 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006441 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006442 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006443 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006444 case OMPC_unknown:
6445 llvm_unreachable("Clause is not allowed.");
6446 }
6447 return Res;
6448}
6449
Alexey Bataev6402bca2015-12-28 07:25:51 +00006450static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6451 OpenMPScheduleClauseModifier M2,
6452 SourceLocation M1Loc, SourceLocation M2Loc) {
6453 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6454 SmallVector<unsigned, 2> Excluded;
6455 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6456 Excluded.push_back(M2);
6457 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6458 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6459 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6460 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6461 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6462 << getListOfPossibleValues(OMPC_schedule,
6463 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6464 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6465 Excluded)
6466 << getOpenMPClauseName(OMPC_schedule);
6467 return true;
6468 }
6469 return false;
6470}
6471
Alexey Bataev56dafe82014-06-20 07:16:17 +00006472OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006473 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006474 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006475 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6476 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6477 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6478 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6479 return nullptr;
6480 // OpenMP, 2.7.1, Loop Construct, Restrictions
6481 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6482 // but not both.
6483 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6484 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6485 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6486 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6487 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6488 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6489 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6490 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6491 return nullptr;
6492 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006493 if (Kind == OMPC_SCHEDULE_unknown) {
6494 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006495 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6496 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6497 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6498 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6499 Exclude);
6500 } else {
6501 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6502 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006503 }
6504 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6505 << Values << getOpenMPClauseName(OMPC_schedule);
6506 return nullptr;
6507 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006508 // OpenMP, 2.7.1, Loop Construct, Restrictions
6509 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6510 // schedule(guided).
6511 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6512 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6513 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6514 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6515 diag::err_omp_schedule_nonmonotonic_static);
6516 return nullptr;
6517 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006518 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006519 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006520 if (ChunkSize) {
6521 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6522 !ChunkSize->isInstantiationDependent() &&
6523 !ChunkSize->containsUnexpandedParameterPack()) {
6524 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6525 ExprResult Val =
6526 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6527 if (Val.isInvalid())
6528 return nullptr;
6529
6530 ValExpr = Val.get();
6531
6532 // OpenMP [2.7.1, Restrictions]
6533 // chunk_size must be a loop invariant integer expression with a positive
6534 // value.
6535 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006536 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6537 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6538 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006539 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006540 return nullptr;
6541 }
6542 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6543 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6544 ChunkSize->getType(), ".chunk.");
6545 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6546 ChunkSize->getExprLoc(),
6547 /*RefersToCapture=*/true);
6548 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006549 }
6550 }
6551 }
6552
Alexey Bataev6402bca2015-12-28 07:25:51 +00006553 return new (Context)
6554 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6555 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006556}
6557
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006558OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6559 SourceLocation StartLoc,
6560 SourceLocation EndLoc) {
6561 OMPClause *Res = nullptr;
6562 switch (Kind) {
6563 case OMPC_ordered:
6564 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6565 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006566 case OMPC_nowait:
6567 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6568 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006569 case OMPC_untied:
6570 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6571 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006572 case OMPC_mergeable:
6573 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6574 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006575 case OMPC_read:
6576 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6577 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006578 case OMPC_write:
6579 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6580 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006581 case OMPC_update:
6582 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6583 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006584 case OMPC_capture:
6585 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6586 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006587 case OMPC_seq_cst:
6588 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6589 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006590 case OMPC_threads:
6591 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6592 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006593 case OMPC_simd:
6594 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6595 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006596 case OMPC_nogroup:
6597 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6598 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006599 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006600 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006601 case OMPC_num_threads:
6602 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006603 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006604 case OMPC_collapse:
6605 case OMPC_schedule:
6606 case OMPC_private:
6607 case OMPC_firstprivate:
6608 case OMPC_lastprivate:
6609 case OMPC_shared:
6610 case OMPC_reduction:
6611 case OMPC_linear:
6612 case OMPC_aligned:
6613 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006614 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006615 case OMPC_default:
6616 case OMPC_proc_bind:
6617 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006618 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006619 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006620 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006621 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006622 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006623 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006624 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006625 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006626 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006627 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006628 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006629 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006630 case OMPC_unknown:
6631 llvm_unreachable("Clause is not allowed.");
6632 }
6633 return Res;
6634}
6635
Alexey Bataev236070f2014-06-20 11:19:47 +00006636OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6637 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006638 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006639 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6640}
6641
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006642OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6643 SourceLocation EndLoc) {
6644 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6645}
6646
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006647OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6648 SourceLocation EndLoc) {
6649 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6650}
6651
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006652OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6653 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006654 return new (Context) OMPReadClause(StartLoc, EndLoc);
6655}
6656
Alexey Bataevdea47612014-07-23 07:46:59 +00006657OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6658 SourceLocation EndLoc) {
6659 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6660}
6661
Alexey Bataev67a4f222014-07-23 10:25:33 +00006662OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6663 SourceLocation EndLoc) {
6664 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6665}
6666
Alexey Bataev459dec02014-07-24 06:46:57 +00006667OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6668 SourceLocation EndLoc) {
6669 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6670}
6671
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006672OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6673 SourceLocation EndLoc) {
6674 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6675}
6676
Alexey Bataev346265e2015-09-25 10:37:12 +00006677OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6678 SourceLocation EndLoc) {
6679 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6680}
6681
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006682OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6683 SourceLocation EndLoc) {
6684 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6685}
6686
Alexey Bataevb825de12015-12-07 10:51:44 +00006687OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6688 SourceLocation EndLoc) {
6689 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6690}
6691
Alexey Bataevc5e02582014-06-16 07:08:35 +00006692OMPClause *Sema::ActOnOpenMPVarListClause(
6693 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6694 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6695 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006696 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006697 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6698 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6699 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006700 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006701 switch (Kind) {
6702 case OMPC_private:
6703 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6704 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006705 case OMPC_firstprivate:
6706 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6707 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006708 case OMPC_lastprivate:
6709 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6710 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006711 case OMPC_shared:
6712 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6713 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006714 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006715 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6716 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006717 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006718 case OMPC_linear:
6719 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006720 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006721 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006722 case OMPC_aligned:
6723 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6724 ColonLoc, EndLoc);
6725 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006726 case OMPC_copyin:
6727 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6728 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006729 case OMPC_copyprivate:
6730 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6731 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006732 case OMPC_flush:
6733 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6734 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006735 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006736 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6737 StartLoc, LParenLoc, EndLoc);
6738 break;
6739 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006740 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6741 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6742 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006743 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006744 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006745 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006746 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006747 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006748 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006749 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006750 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006751 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006752 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006753 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006754 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006755 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006756 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006757 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006758 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006759 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006760 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006761 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006762 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006763 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006764 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006765 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006766 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006767 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006768 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006769 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006770 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006771 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006772 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006773 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006774 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006775 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006776 llvm_unreachable("Clause is not allowed.");
6777 }
6778 return Res;
6779}
6780
6781OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6782 SourceLocation StartLoc,
6783 SourceLocation LParenLoc,
6784 SourceLocation EndLoc) {
6785 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006786 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006787 for (auto &RefExpr : VarList) {
6788 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006789 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6790 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006791 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006792 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006793 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006794 continue;
6795 }
6796
Alexey Bataeved09d242014-05-28 05:53:51 +00006797 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006798 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006799 // A list item is a variable name.
6800 // OpenMP [2.9.3.3, Restrictions, p.1]
6801 // A variable that is part of another variable (as an array or
6802 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006803 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6804 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6805 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6806 (getCurrentThisType().isNull() || !ME ||
6807 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6808 !isa<FieldDecl>(ME->getMemberDecl()))) {
6809 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6810 << (getCurrentThisType().isNull() ? 0 : 1)
6811 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006812 continue;
6813 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006814 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6815 QualType Type = D->getType();
6816 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006817
6818 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6819 // A variable that appears in a private clause must not have an incomplete
6820 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006821 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006822 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006823 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006824
Alexey Bataev758e55e2013-09-06 18:03:48 +00006825 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6826 // in a Construct]
6827 // Variables with the predetermined data-sharing attributes may not be
6828 // listed in data-sharing attributes clauses, except for the cases
6829 // listed below. For these exceptions only, listing a predetermined
6830 // variable in a data-sharing attribute clause is allowed and overrides
6831 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006832 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006833 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006834 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6835 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006836 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006837 continue;
6838 }
6839
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006840 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006841 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006842 DSAStack->getCurrentDirective() == OMPD_task) {
6843 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6844 << getOpenMPClauseName(OMPC_private) << Type
6845 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6846 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006847 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006848 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006849 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006850 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006851 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006852 continue;
6853 }
6854
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006855 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6856 // A variable of class type (or array thereof) that appears in a private
6857 // clause requires an accessible, unambiguous default constructor for the
6858 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006859 // Generate helper private variable and initialize it with the default
6860 // value. The address of the original variable is replaced by the address of
6861 // the new private variable in CodeGen. This new variable is not added to
6862 // IdResolver, so the code in the OpenMP region uses original variable for
6863 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006864 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006865 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
6866 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006867 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006868 if (VDPrivate->isInvalidDecl())
6869 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006870 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006871 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006872
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006873 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
6874 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006875 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006876 }
6877
Alexey Bataeved09d242014-05-28 05:53:51 +00006878 if (Vars.empty())
6879 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006880
Alexey Bataev03b340a2014-10-21 03:16:40 +00006881 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6882 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006883}
6884
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006885namespace {
6886class DiagsUninitializedSeveretyRAII {
6887private:
6888 DiagnosticsEngine &Diags;
6889 SourceLocation SavedLoc;
6890 bool IsIgnored;
6891
6892public:
6893 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6894 bool IsIgnored)
6895 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6896 if (!IsIgnored) {
6897 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6898 /*Map*/ diag::Severity::Ignored, Loc);
6899 }
6900 }
6901 ~DiagsUninitializedSeveretyRAII() {
6902 if (!IsIgnored)
6903 Diags.popMappings(SavedLoc);
6904 }
6905};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006906}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006907
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006908OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6909 SourceLocation StartLoc,
6910 SourceLocation LParenLoc,
6911 SourceLocation EndLoc) {
6912 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006913 SmallVector<Expr *, 8> PrivateCopies;
6914 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006915 bool IsImplicitClause =
6916 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6917 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6918
Alexey Bataeved09d242014-05-28 05:53:51 +00006919 for (auto &RefExpr : VarList) {
6920 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6921 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006922 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006923 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006924 PrivateCopies.push_back(nullptr);
6925 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006926 continue;
6927 }
6928
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006929 SourceLocation ELoc =
6930 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006931 // OpenMP [2.1, C/C++]
6932 // A list item is a variable name.
6933 // OpenMP [2.9.3.3, Restrictions, p.1]
6934 // A variable that is part of another variable (as an array or
6935 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006936 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006937 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006938 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6939 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006940 continue;
6941 }
6942 Decl *D = DE->getDecl();
6943 VarDecl *VD = cast<VarDecl>(D);
6944
6945 QualType Type = VD->getType();
6946 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6947 // It will be analyzed later.
6948 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006949 PrivateCopies.push_back(nullptr);
6950 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006951 continue;
6952 }
6953
6954 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6955 // A variable that appears in a private clause must not have an incomplete
6956 // type or a reference type.
6957 if (RequireCompleteType(ELoc, Type,
6958 diag::err_omp_firstprivate_incomplete_type)) {
6959 continue;
6960 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006961 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006962
6963 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6964 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006965 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006966 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006967 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006968
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006969 // If an implicit firstprivate variable found it was checked already.
6970 if (!IsImplicitClause) {
6971 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006972 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006973 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6974 // A list item that specifies a given variable may not appear in more
6975 // than one clause on the same directive, except that a variable may be
6976 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006977 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006978 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006979 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006980 << getOpenMPClauseName(DVar.CKind)
6981 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006982 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006983 continue;
6984 }
6985
6986 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6987 // in a Construct]
6988 // Variables with the predetermined data-sharing attributes may not be
6989 // listed in data-sharing attributes clauses, except for the cases
6990 // listed below. For these exceptions only, listing a predetermined
6991 // variable in a data-sharing attribute clause is allowed and overrides
6992 // the variable's predetermined data-sharing attributes.
6993 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6994 // in a Construct, C/C++, p.2]
6995 // Variables with const-qualified type having no mutable member may be
6996 // listed in a firstprivate clause, even if they are static data members.
6997 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6998 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6999 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007000 << getOpenMPClauseName(DVar.CKind)
7001 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007002 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007003 continue;
7004 }
7005
Alexey Bataevf29276e2014-06-18 04:14:57 +00007006 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007007 // OpenMP [2.9.3.4, Restrictions, p.2]
7008 // A list item that is private within a parallel region must not appear
7009 // in a firstprivate clause on a worksharing construct if any of the
7010 // worksharing regions arising from the worksharing construct ever bind
7011 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007012 if (isOpenMPWorksharingDirective(CurrDir) &&
7013 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007014 DVar = DSAStack->getImplicitDSA(VD, true);
7015 if (DVar.CKind != OMPC_shared &&
7016 (isOpenMPParallelDirective(DVar.DKind) ||
7017 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007018 Diag(ELoc, diag::err_omp_required_access)
7019 << getOpenMPClauseName(OMPC_firstprivate)
7020 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007021 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007022 continue;
7023 }
7024 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007025 // OpenMP [2.9.3.4, Restrictions, p.3]
7026 // A list item that appears in a reduction clause of a parallel construct
7027 // must not appear in a firstprivate clause on a worksharing or task
7028 // construct if any of the worksharing or task regions arising from the
7029 // worksharing or task construct ever bind to any of the parallel regions
7030 // arising from the parallel construct.
7031 // OpenMP [2.9.3.4, Restrictions, p.4]
7032 // A list item that appears in a reduction clause in worksharing
7033 // construct must not appear in a firstprivate clause in a task construct
7034 // encountered during execution of any of the worksharing regions arising
7035 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007036 if (CurrDir == OMPD_task) {
7037 DVar =
7038 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7039 [](OpenMPDirectiveKind K) -> bool {
7040 return isOpenMPParallelDirective(K) ||
7041 isOpenMPWorksharingDirective(K);
7042 },
7043 false);
7044 if (DVar.CKind == OMPC_reduction &&
7045 (isOpenMPParallelDirective(DVar.DKind) ||
7046 isOpenMPWorksharingDirective(DVar.DKind))) {
7047 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7048 << getOpenMPDirectiveName(DVar.DKind);
7049 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7050 continue;
7051 }
7052 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007053
7054 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7055 // A list item that is private within a teams region must not appear in a
7056 // firstprivate clause on a distribute construct if any of the distribute
7057 // regions arising from the distribute construct ever bind to any of the
7058 // teams regions arising from the teams construct.
7059 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7060 // A list item that appears in a reduction clause of a teams construct
7061 // must not appear in a firstprivate clause on a distribute construct if
7062 // any of the distribute regions arising from the distribute construct
7063 // ever bind to any of the teams regions arising from the teams construct.
7064 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7065 // A list item may appear in a firstprivate or lastprivate clause but not
7066 // both.
7067 if (CurrDir == OMPD_distribute) {
7068 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7069 [](OpenMPDirectiveKind K) -> bool {
7070 return isOpenMPTeamsDirective(K);
7071 },
7072 false);
7073 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7074 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7075 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7076 continue;
7077 }
7078 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7079 [](OpenMPDirectiveKind K) -> bool {
7080 return isOpenMPTeamsDirective(K);
7081 },
7082 false);
7083 if (DVar.CKind == OMPC_reduction &&
7084 isOpenMPTeamsDirective(DVar.DKind)) {
7085 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7086 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7087 continue;
7088 }
7089 DVar = DSAStack->getTopDSA(VD, false);
7090 if (DVar.CKind == OMPC_lastprivate) {
7091 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7092 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7093 continue;
7094 }
7095 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007096 }
7097
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007098 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007099 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007100 DSAStack->getCurrentDirective() == OMPD_task) {
7101 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7102 << getOpenMPClauseName(OMPC_firstprivate) << Type
7103 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7104 bool IsDecl =
7105 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7106 Diag(VD->getLocation(),
7107 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7108 << VD;
7109 continue;
7110 }
7111
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007112 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007113 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7114 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007115 // Generate helper private variable and initialize it with the value of the
7116 // original variable. The address of the original variable is replaced by
7117 // the address of the new private variable in the CodeGen. This new variable
7118 // is not added to IdResolver, so the code in the OpenMP region uses
7119 // original variable for proper diagnostics and variable capturing.
7120 Expr *VDInitRefExpr = nullptr;
7121 // For arrays generate initializer for single element and replace it by the
7122 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007123 if (Type->isArrayType()) {
7124 auto VDInit =
7125 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7126 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007127 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007128 ElemType = ElemType.getUnqualifiedType();
7129 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7130 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007131 InitializedEntity Entity =
7132 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007133 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7134
7135 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7136 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7137 if (Result.isInvalid())
7138 VDPrivate->setInvalidDecl();
7139 else
7140 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007141 // Remove temp variable declaration.
7142 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007143 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007144 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007145 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007146 VDInitRefExpr =
7147 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007148 AddInitializerToDecl(VDPrivate,
7149 DefaultLvalueConversion(VDInitRefExpr).get(),
7150 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007151 }
7152 if (VDPrivate->isInvalidDecl()) {
7153 if (IsImplicitClause) {
7154 Diag(DE->getExprLoc(),
7155 diag::note_omp_task_predetermined_firstprivate_here);
7156 }
7157 continue;
7158 }
7159 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007160 auto VDPrivateRefExpr = buildDeclRefExpr(
7161 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007162 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7163 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007164 PrivateCopies.push_back(VDPrivateRefExpr);
7165 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007166 }
7167
Alexey Bataeved09d242014-05-28 05:53:51 +00007168 if (Vars.empty())
7169 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007170
7171 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007172 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007173}
7174
Alexander Musman1bb328c2014-06-04 13:06:39 +00007175OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7176 SourceLocation StartLoc,
7177 SourceLocation LParenLoc,
7178 SourceLocation EndLoc) {
7179 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007180 SmallVector<Expr *, 8> SrcExprs;
7181 SmallVector<Expr *, 8> DstExprs;
7182 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007183 for (auto &RefExpr : VarList) {
7184 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7185 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7186 // It will be analyzed later.
7187 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007188 SrcExprs.push_back(nullptr);
7189 DstExprs.push_back(nullptr);
7190 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007191 continue;
7192 }
7193
7194 SourceLocation ELoc = RefExpr->getExprLoc();
7195 // OpenMP [2.1, C/C++]
7196 // A list item is a variable name.
7197 // OpenMP [2.14.3.5, Restrictions, p.1]
7198 // A variable that is part of another variable (as an array or structure
7199 // element) cannot appear in a lastprivate clause.
7200 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7201 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007202 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7203 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007204 continue;
7205 }
7206 Decl *D = DE->getDecl();
7207 VarDecl *VD = cast<VarDecl>(D);
7208
7209 QualType Type = VD->getType();
7210 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7211 // It will be analyzed later.
7212 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007213 SrcExprs.push_back(nullptr);
7214 DstExprs.push_back(nullptr);
7215 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007216 continue;
7217 }
7218
7219 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7220 // A variable that appears in a lastprivate clause must not have an
7221 // incomplete type or a reference type.
7222 if (RequireCompleteType(ELoc, Type,
7223 diag::err_omp_lastprivate_incomplete_type)) {
7224 continue;
7225 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007226 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007227
7228 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7229 // in a Construct]
7230 // Variables with the predetermined data-sharing attributes may not be
7231 // listed in data-sharing attributes clauses, except for the cases
7232 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007233 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007234 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7235 DVar.CKind != OMPC_firstprivate &&
7236 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7237 Diag(ELoc, diag::err_omp_wrong_dsa)
7238 << getOpenMPClauseName(DVar.CKind)
7239 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007240 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007241 continue;
7242 }
7243
Alexey Bataevf29276e2014-06-18 04:14:57 +00007244 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7245 // OpenMP [2.14.3.5, Restrictions, p.2]
7246 // A list item that is private within a parallel region, or that appears in
7247 // the reduction clause of a parallel construct, must not appear in a
7248 // lastprivate clause on a worksharing construct if any of the corresponding
7249 // worksharing regions ever binds to any of the corresponding parallel
7250 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007251 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007252 if (isOpenMPWorksharingDirective(CurrDir) &&
7253 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007254 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007255 if (DVar.CKind != OMPC_shared) {
7256 Diag(ELoc, diag::err_omp_required_access)
7257 << getOpenMPClauseName(OMPC_lastprivate)
7258 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007259 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007260 continue;
7261 }
7262 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007263 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007264 // A variable of class type (or array thereof) that appears in a
7265 // lastprivate clause requires an accessible, unambiguous default
7266 // constructor for the class type, unless the list item is also specified
7267 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007268 // A variable of class type (or array thereof) that appears in a
7269 // lastprivate clause requires an accessible, unambiguous copy assignment
7270 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007271 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007272 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007273 Type.getUnqualifiedType(), ".lastprivate.src",
7274 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007275 auto *PseudoSrcExpr = buildDeclRefExpr(
7276 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007277 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007278 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7279 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007280 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007281 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007282 // For arrays generate assignment operation for single element and replace
7283 // it by the original array element in CodeGen.
7284 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7285 PseudoDstExpr, PseudoSrcExpr);
7286 if (AssignmentOp.isInvalid())
7287 continue;
7288 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7289 /*DiscardedValue=*/true);
7290 if (AssignmentOp.isInvalid())
7291 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007292
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007293 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7294 // A list item may appear in a firstprivate or lastprivate clause but not
7295 // both.
7296 if (CurrDir == OMPD_distribute) {
7297 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7298 if (DVar.CKind == OMPC_firstprivate) {
7299 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7300 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7301 continue;
7302 }
7303 }
7304
Alexey Bataev39f915b82015-05-08 10:41:21 +00007305 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007306 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007307 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007308 SrcExprs.push_back(PseudoSrcExpr);
7309 DstExprs.push_back(PseudoDstExpr);
7310 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007311 }
7312
7313 if (Vars.empty())
7314 return nullptr;
7315
7316 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007317 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007318}
7319
Alexey Bataev758e55e2013-09-06 18:03:48 +00007320OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7321 SourceLocation StartLoc,
7322 SourceLocation LParenLoc,
7323 SourceLocation EndLoc) {
7324 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007325 for (auto &RefExpr : VarList) {
7326 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7327 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007328 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007329 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007330 continue;
7331 }
7332
Alexey Bataeved09d242014-05-28 05:53:51 +00007333 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007334 // OpenMP [2.1, C/C++]
7335 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007336 // OpenMP [2.14.3.2, Restrictions, p.1]
7337 // A variable that is part of another variable (as an array or structure
7338 // element) cannot appear in a shared unless it is a static data member
7339 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007340 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007341 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007342 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7343 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007344 continue;
7345 }
7346 Decl *D = DE->getDecl();
7347 VarDecl *VD = cast<VarDecl>(D);
7348
7349 QualType Type = VD->getType();
7350 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7351 // It will be analyzed later.
7352 Vars.push_back(DE);
7353 continue;
7354 }
7355
7356 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7357 // in a Construct]
7358 // Variables with the predetermined data-sharing attributes may not be
7359 // listed in data-sharing attributes clauses, except for the cases
7360 // listed below. For these exceptions only, listing a predetermined
7361 // variable in a data-sharing attribute clause is allowed and overrides
7362 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007363 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007364 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7365 DVar.RefExpr) {
7366 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7367 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007368 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007369 continue;
7370 }
7371
7372 DSAStack->addDSA(VD, DE, OMPC_shared);
7373 Vars.push_back(DE);
7374 }
7375
Alexey Bataeved09d242014-05-28 05:53:51 +00007376 if (Vars.empty())
7377 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007378
7379 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7380}
7381
Alexey Bataevc5e02582014-06-16 07:08:35 +00007382namespace {
7383class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7384 DSAStackTy *Stack;
7385
7386public:
7387 bool VisitDeclRefExpr(DeclRefExpr *E) {
7388 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007389 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007390 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7391 return false;
7392 if (DVar.CKind != OMPC_unknown)
7393 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007394 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007395 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007396 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007397 return true;
7398 return false;
7399 }
7400 return false;
7401 }
7402 bool VisitStmt(Stmt *S) {
7403 for (auto Child : S->children()) {
7404 if (Child && Visit(Child))
7405 return true;
7406 }
7407 return false;
7408 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007409 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007410};
Alexey Bataev23b69422014-06-18 07:08:49 +00007411} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007412
7413OMPClause *Sema::ActOnOpenMPReductionClause(
7414 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7415 SourceLocation ColonLoc, SourceLocation EndLoc,
7416 CXXScopeSpec &ReductionIdScopeSpec,
7417 const DeclarationNameInfo &ReductionId) {
7418 // TODO: Allow scope specification search when 'declare reduction' is
7419 // supported.
7420 assert(ReductionIdScopeSpec.isEmpty() &&
7421 "No support for scoped reduction identifiers yet.");
7422
7423 auto DN = ReductionId.getName();
7424 auto OOK = DN.getCXXOverloadedOperator();
7425 BinaryOperatorKind BOK = BO_Comma;
7426
7427 // OpenMP [2.14.3.6, reduction clause]
7428 // C
7429 // reduction-identifier is either an identifier or one of the following
7430 // operators: +, -, *, &, |, ^, && and ||
7431 // C++
7432 // reduction-identifier is either an id-expression or one of the following
7433 // operators: +, -, *, &, |, ^, && and ||
7434 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7435 switch (OOK) {
7436 case OO_Plus:
7437 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007438 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007439 break;
7440 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007441 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007442 break;
7443 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007444 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007445 break;
7446 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007447 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007448 break;
7449 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007450 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007451 break;
7452 case OO_AmpAmp:
7453 BOK = BO_LAnd;
7454 break;
7455 case OO_PipePipe:
7456 BOK = BO_LOr;
7457 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007458 case OO_New:
7459 case OO_Delete:
7460 case OO_Array_New:
7461 case OO_Array_Delete:
7462 case OO_Slash:
7463 case OO_Percent:
7464 case OO_Tilde:
7465 case OO_Exclaim:
7466 case OO_Equal:
7467 case OO_Less:
7468 case OO_Greater:
7469 case OO_LessEqual:
7470 case OO_GreaterEqual:
7471 case OO_PlusEqual:
7472 case OO_MinusEqual:
7473 case OO_StarEqual:
7474 case OO_SlashEqual:
7475 case OO_PercentEqual:
7476 case OO_CaretEqual:
7477 case OO_AmpEqual:
7478 case OO_PipeEqual:
7479 case OO_LessLess:
7480 case OO_GreaterGreater:
7481 case OO_LessLessEqual:
7482 case OO_GreaterGreaterEqual:
7483 case OO_EqualEqual:
7484 case OO_ExclaimEqual:
7485 case OO_PlusPlus:
7486 case OO_MinusMinus:
7487 case OO_Comma:
7488 case OO_ArrowStar:
7489 case OO_Arrow:
7490 case OO_Call:
7491 case OO_Subscript:
7492 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007493 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007494 case NUM_OVERLOADED_OPERATORS:
7495 llvm_unreachable("Unexpected reduction identifier");
7496 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007497 if (auto II = DN.getAsIdentifierInfo()) {
7498 if (II->isStr("max"))
7499 BOK = BO_GT;
7500 else if (II->isStr("min"))
7501 BOK = BO_LT;
7502 }
7503 break;
7504 }
7505 SourceRange ReductionIdRange;
7506 if (ReductionIdScopeSpec.isValid()) {
7507 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7508 }
7509 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7510 if (BOK == BO_Comma) {
7511 // Not allowed reduction identifier is found.
7512 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7513 << ReductionIdRange;
7514 return nullptr;
7515 }
7516
7517 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007518 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007519 SmallVector<Expr *, 8> LHSs;
7520 SmallVector<Expr *, 8> RHSs;
7521 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007522 for (auto RefExpr : VarList) {
7523 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7524 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7525 // It will be analyzed later.
7526 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007527 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007528 LHSs.push_back(nullptr);
7529 RHSs.push_back(nullptr);
7530 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007531 continue;
7532 }
7533
7534 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7535 RefExpr->isInstantiationDependent() ||
7536 RefExpr->containsUnexpandedParameterPack()) {
7537 // It will be analyzed later.
7538 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007539 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007540 LHSs.push_back(nullptr);
7541 RHSs.push_back(nullptr);
7542 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007543 continue;
7544 }
7545
7546 auto ELoc = RefExpr->getExprLoc();
7547 auto ERange = RefExpr->getSourceRange();
7548 // OpenMP [2.1, C/C++]
7549 // A list item is a variable or array section, subject to the restrictions
7550 // specified in Section 2.4 on page 42 and in each of the sections
7551 // describing clauses and directives for which a list appears.
7552 // OpenMP [2.14.3.3, Restrictions, p.1]
7553 // A variable that is part of another variable (as an array or
7554 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007555 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7556 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7557 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7558 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007559 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7560 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007561 continue;
7562 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007563 QualType Type;
7564 VarDecl *VD = nullptr;
7565 if (DE) {
7566 auto D = DE->getDecl();
7567 VD = cast<VarDecl>(D);
Alexey Bataev1189bd02016-01-26 12:20:39 +00007568 Type = Context.getBaseElementType(VD->getType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007569 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007570 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007571 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7572 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7573 Base = TempASE->getBase()->IgnoreParenImpCasts();
7574 DE = dyn_cast<DeclRefExpr>(Base);
7575 if (DE)
7576 VD = dyn_cast<VarDecl>(DE->getDecl());
7577 if (!VD) {
7578 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7579 << 0 << Base->getSourceRange();
7580 continue;
7581 }
7582 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007583 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7584 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7585 Type = ATy->getElementType();
7586 else
7587 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007588 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7589 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7590 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7591 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7592 Base = TempASE->getBase()->IgnoreParenImpCasts();
7593 DE = dyn_cast<DeclRefExpr>(Base);
7594 if (DE)
7595 VD = dyn_cast<VarDecl>(DE->getDecl());
7596 if (!VD) {
7597 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7598 << 1 << Base->getSourceRange();
7599 continue;
7600 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007601 }
7602
Alexey Bataevc5e02582014-06-16 07:08:35 +00007603 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7604 // A variable that appears in a private clause must not have an incomplete
7605 // type or a reference type.
7606 if (RequireCompleteType(ELoc, Type,
7607 diag::err_omp_reduction_incomplete_type))
7608 continue;
7609 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007610 // A list item that appears in a reduction clause must not be
7611 // const-qualified.
7612 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007613 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007614 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007615 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007616 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7617 VarDecl::DeclarationOnly;
7618 Diag(VD->getLocation(),
7619 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7620 << VD;
7621 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007622 continue;
7623 }
7624 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7625 // If a list-item is a reference type then it must bind to the same object
7626 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007627 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007628 VarDecl *VDDef = VD->getDefinition();
7629 if (Type->isReferenceType() && VDDef) {
7630 DSARefChecker Check(DSAStack);
7631 if (Check.Visit(VDDef->getInit())) {
7632 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7633 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7634 continue;
7635 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007636 }
7637 }
7638 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7639 // The type of a list item that appears in a reduction clause must be valid
7640 // for the reduction-identifier. For a max or min reduction in C, the type
7641 // of the list item must be an allowed arithmetic data type: char, int,
7642 // float, double, or _Bool, possibly modified with long, short, signed, or
7643 // unsigned. For a max or min reduction in C++, the type of the list item
7644 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7645 // double, or bool, possibly modified with long, short, signed, or unsigned.
7646 if ((BOK == BO_GT || BOK == BO_LT) &&
7647 !(Type->isScalarType() ||
7648 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7649 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7650 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007651 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007652 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7653 VarDecl::DeclarationOnly;
7654 Diag(VD->getLocation(),
7655 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7656 << VD;
7657 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007658 continue;
7659 }
7660 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7661 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7662 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007663 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007664 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7665 VarDecl::DeclarationOnly;
7666 Diag(VD->getLocation(),
7667 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7668 << VD;
7669 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007670 continue;
7671 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007672 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7673 // in a Construct]
7674 // Variables with the predetermined data-sharing attributes may not be
7675 // listed in data-sharing attributes clauses, except for the cases
7676 // listed below. For these exceptions only, listing a predetermined
7677 // variable in a data-sharing attribute clause is allowed and overrides
7678 // the variable's predetermined data-sharing attributes.
7679 // OpenMP [2.14.3.6, Restrictions, p.3]
7680 // Any number of reduction clauses can be specified on the directive,
7681 // but a list item can appear only once in the reduction clauses for that
7682 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007683 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007684 DVar = DSAStack->getTopDSA(VD, false);
7685 if (DVar.CKind == OMPC_reduction) {
7686 Diag(ELoc, diag::err_omp_once_referenced)
7687 << getOpenMPClauseName(OMPC_reduction);
7688 if (DVar.RefExpr) {
7689 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007690 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007691 } else if (DVar.CKind != OMPC_unknown) {
7692 Diag(ELoc, diag::err_omp_wrong_dsa)
7693 << getOpenMPClauseName(DVar.CKind)
7694 << getOpenMPClauseName(OMPC_reduction);
7695 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7696 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007697 }
7698
7699 // OpenMP [2.14.3.6, Restrictions, p.1]
7700 // A list item that appears in a reduction clause of a worksharing
7701 // construct must be shared in the parallel regions to which any of the
7702 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007703 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7704 if (isOpenMPWorksharingDirective(CurrDir) &&
7705 !isOpenMPParallelDirective(CurrDir)) {
7706 DVar = DSAStack->getImplicitDSA(VD, true);
7707 if (DVar.CKind != OMPC_shared) {
7708 Diag(ELoc, diag::err_omp_required_access)
7709 << getOpenMPClauseName(OMPC_reduction)
7710 << getOpenMPClauseName(OMPC_shared);
7711 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7712 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007713 }
7714 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007715
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007716 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007717 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7718 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7719 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7720 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7721 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007722 if (OASE ||
7723 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7724 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007725 // Create pseudo array type for private copy. The size for this array will
7726 // be generated during codegen.
7727 // For array subscripts or single variables Private Ty is the same as Type
7728 // (type of the variable or single array element).
7729 PrivateTy = Context.getVariableArrayType(
7730 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7731 Context.getSizeType(), VK_RValue),
7732 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007733 } else if (DE &&
7734 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7735 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007736 // Private copy.
7737 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7738 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007739 // Add initializer for private variable.
7740 Expr *Init = nullptr;
7741 switch (BOK) {
7742 case BO_Add:
7743 case BO_Xor:
7744 case BO_Or:
7745 case BO_LOr:
7746 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7747 if (Type->isScalarType() || Type->isAnyComplexType()) {
7748 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007749 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007750 break;
7751 case BO_Mul:
7752 case BO_LAnd:
7753 if (Type->isScalarType() || Type->isAnyComplexType()) {
7754 // '*' and '&&' reduction ops - initializer is '1'.
7755 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7756 }
7757 break;
7758 case BO_And: {
7759 // '&' reduction op - initializer is '~0'.
7760 QualType OrigType = Type;
7761 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7762 Type = ComplexTy->getElementType();
7763 }
7764 if (Type->isRealFloatingType()) {
7765 llvm::APFloat InitValue =
7766 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7767 /*isIEEE=*/true);
7768 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7769 Type, ELoc);
7770 } else if (Type->isScalarType()) {
7771 auto Size = Context.getTypeSize(Type);
7772 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7773 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7774 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7775 }
7776 if (Init && OrigType->isAnyComplexType()) {
7777 // Init = 0xFFFF + 0xFFFFi;
7778 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7779 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7780 }
7781 Type = OrigType;
7782 break;
7783 }
7784 case BO_LT:
7785 case BO_GT: {
7786 // 'min' reduction op - initializer is 'Largest representable number in
7787 // the reduction list item type'.
7788 // 'max' reduction op - initializer is 'Least representable number in
7789 // the reduction list item type'.
7790 if (Type->isIntegerType() || Type->isPointerType()) {
7791 bool IsSigned = Type->hasSignedIntegerRepresentation();
7792 auto Size = Context.getTypeSize(Type);
7793 QualType IntTy =
7794 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7795 llvm::APInt InitValue =
7796 (BOK != BO_LT)
7797 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7798 : llvm::APInt::getMinValue(Size)
7799 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7800 : llvm::APInt::getMaxValue(Size);
7801 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7802 if (Type->isPointerType()) {
7803 // Cast to pointer type.
7804 auto CastExpr = BuildCStyleCastExpr(
7805 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7806 SourceLocation(), Init);
7807 if (CastExpr.isInvalid())
7808 continue;
7809 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007810 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007811 } else if (Type->isRealFloatingType()) {
7812 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7813 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7814 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7815 Type, ELoc);
7816 }
7817 break;
7818 }
7819 case BO_PtrMemD:
7820 case BO_PtrMemI:
7821 case BO_MulAssign:
7822 case BO_Div:
7823 case BO_Rem:
7824 case BO_Sub:
7825 case BO_Shl:
7826 case BO_Shr:
7827 case BO_LE:
7828 case BO_GE:
7829 case BO_EQ:
7830 case BO_NE:
7831 case BO_AndAssign:
7832 case BO_XorAssign:
7833 case BO_OrAssign:
7834 case BO_Assign:
7835 case BO_AddAssign:
7836 case BO_SubAssign:
7837 case BO_DivAssign:
7838 case BO_RemAssign:
7839 case BO_ShlAssign:
7840 case BO_ShrAssign:
7841 case BO_Comma:
7842 llvm_unreachable("Unexpected reduction operation");
7843 }
7844 if (Init) {
7845 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7846 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007847 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007848 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007849 if (!RHSVD->hasInit()) {
7850 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7851 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007852 if (VD) {
7853 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7854 VarDecl::DeclarationOnly;
7855 Diag(VD->getLocation(),
7856 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7857 << VD;
7858 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007859 continue;
7860 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007861 // Store initializer for single element in private copy. Will be used during
7862 // codegen.
7863 PrivateVD->setInit(RHSVD->getInit());
7864 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007865 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7866 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007867 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007868 ExprResult ReductionOp =
7869 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7870 LHSDRE, RHSDRE);
7871 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007872 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007873 ReductionOp =
7874 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7875 BO_Assign, LHSDRE, ReductionOp.get());
7876 } else {
7877 auto *ConditionalOp = new (Context) ConditionalOperator(
7878 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7879 RHSDRE, Type, VK_LValue, OK_Ordinary);
7880 ReductionOp =
7881 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7882 BO_Assign, LHSDRE, ConditionalOp);
7883 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007884 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007885 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007886 if (ReductionOp.isInvalid())
7887 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007888
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007889 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007890 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007891 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007892 LHSs.push_back(LHSDRE);
7893 RHSs.push_back(RHSDRE);
7894 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007895 }
7896
7897 if (Vars.empty())
7898 return nullptr;
7899
7900 return OMPReductionClause::Create(
7901 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007902 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7903 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007904}
7905
Alexey Bataev182227b2015-08-20 10:54:39 +00007906OMPClause *Sema::ActOnOpenMPLinearClause(
7907 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7908 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7909 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007910 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007911 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007912 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007913 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7914 LinKind == OMPC_LINEAR_unknown) {
7915 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7916 LinKind = OMPC_LINEAR_val;
7917 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007918 for (auto &RefExpr : VarList) {
7919 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7920 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007921 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007922 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007923 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007924 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007925 continue;
7926 }
7927
7928 // OpenMP [2.14.3.7, linear clause]
7929 // A list item that appears in a linear clause is subject to the private
7930 // clause semantics described in Section 2.14.3.3 on page 159 except as
7931 // noted. In addition, the value of the new list item on each iteration
7932 // of the associated loop(s) corresponds to the value of the original
7933 // list item before entering the construct plus the logical number of
7934 // the iteration times linear-step.
7935
Alexey Bataeved09d242014-05-28 05:53:51 +00007936 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007937 // OpenMP [2.1, C/C++]
7938 // A list item is a variable name.
7939 // OpenMP [2.14.3.3, Restrictions, p.1]
7940 // A variable that is part of another variable (as an array or
7941 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007942 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007943 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007944 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7945 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007946 continue;
7947 }
7948
7949 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7950
7951 // OpenMP [2.14.3.7, linear clause]
7952 // A list-item cannot appear in more than one linear clause.
7953 // A list-item that appears in a linear clause cannot appear in any
7954 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007955 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007956 if (DVar.RefExpr) {
7957 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7958 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007959 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007960 continue;
7961 }
7962
7963 QualType QType = VD->getType();
7964 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7965 // It will be analyzed later.
7966 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007967 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007968 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007969 continue;
7970 }
7971
7972 // A variable must not have an incomplete type or a reference type.
7973 if (RequireCompleteType(ELoc, QType,
7974 diag::err_omp_linear_incomplete_type)) {
7975 continue;
7976 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007977 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7978 !QType->isReferenceType()) {
7979 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7980 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7981 continue;
7982 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007983 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007984
7985 // A list item must not be const-qualified.
7986 if (QType.isConstant(Context)) {
7987 Diag(ELoc, diag::err_omp_const_variable)
7988 << getOpenMPClauseName(OMPC_linear);
7989 bool IsDecl =
7990 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7991 Diag(VD->getLocation(),
7992 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7993 << VD;
7994 continue;
7995 }
7996
7997 // A list item must be of integral or pointer type.
7998 QType = QType.getUnqualifiedType().getCanonicalType();
7999 const Type *Ty = QType.getTypePtrOrNull();
8000 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8001 !Ty->isPointerType())) {
8002 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8003 bool IsDecl =
8004 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8005 Diag(VD->getLocation(),
8006 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8007 << VD;
8008 continue;
8009 }
8010
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008011 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008012 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8013 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008014 auto *PrivateRef = buildDeclRefExpr(
8015 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008016 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008017 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008018 Expr *InitExpr;
8019 if (LinKind == OMPC_LINEAR_uval)
8020 InitExpr = VD->getInit();
8021 else
8022 InitExpr = DE;
8023 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008024 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008025 auto InitRef = buildDeclRefExpr(
8026 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008027 DSAStack->addDSA(VD, DE, OMPC_linear);
8028 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008029 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008030 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008031 }
8032
8033 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008034 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008035
8036 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008037 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008038 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8039 !Step->isInstantiationDependent() &&
8040 !Step->containsUnexpandedParameterPack()) {
8041 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008042 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008043 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008044 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008045 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008046
Alexander Musman3276a272015-03-21 10:12:56 +00008047 // Build var to save the step value.
8048 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008049 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008050 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008051 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008052 ExprResult CalcStep =
8053 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008054 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008055
Alexander Musman8dba6642014-04-22 13:09:42 +00008056 // Warn about zero linear step (it would be probably better specified as
8057 // making corresponding variables 'const').
8058 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008059 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8060 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008061 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8062 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008063 if (!IsConstant && CalcStep.isUsable()) {
8064 // Calculate the step beforehand instead of doing this on each iteration.
8065 // (This is not used if the number of iterations may be kfold-ed).
8066 CalcStepExpr = CalcStep.get();
8067 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008068 }
8069
Alexey Bataev182227b2015-08-20 10:54:39 +00008070 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8071 ColonLoc, EndLoc, Vars, Privates, Inits,
8072 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008073}
8074
8075static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8076 Expr *NumIterations, Sema &SemaRef,
8077 Scope *S) {
8078 // Walk the vars and build update/final expressions for the CodeGen.
8079 SmallVector<Expr *, 8> Updates;
8080 SmallVector<Expr *, 8> Finals;
8081 Expr *Step = Clause.getStep();
8082 Expr *CalcStep = Clause.getCalcStep();
8083 // OpenMP [2.14.3.7, linear clause]
8084 // If linear-step is not specified it is assumed to be 1.
8085 if (Step == nullptr)
8086 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8087 else if (CalcStep)
8088 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8089 bool HasErrors = false;
8090 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008091 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008092 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008093 for (auto &RefExpr : Clause.varlists()) {
8094 Expr *InitExpr = *CurInit;
8095
8096 // Build privatized reference to the current linear var.
8097 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008098 Expr *CapturedRef;
8099 if (LinKind == OMPC_LINEAR_uval)
8100 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8101 else
8102 CapturedRef =
8103 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8104 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8105 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008106
8107 // Build update: Var = InitExpr + IV * Step
8108 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008109 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008110 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008111 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8112 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008113
8114 // Build final: Var = InitExpr + NumIterations * Step
8115 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008116 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008117 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008118 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8119 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008120 if (!Update.isUsable() || !Final.isUsable()) {
8121 Updates.push_back(nullptr);
8122 Finals.push_back(nullptr);
8123 HasErrors = true;
8124 } else {
8125 Updates.push_back(Update.get());
8126 Finals.push_back(Final.get());
8127 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008128 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008129 }
8130 Clause.setUpdates(Updates);
8131 Clause.setFinals(Finals);
8132 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008133}
8134
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008135OMPClause *Sema::ActOnOpenMPAlignedClause(
8136 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8137 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8138
8139 SmallVector<Expr *, 8> Vars;
8140 for (auto &RefExpr : VarList) {
8141 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8142 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8143 // It will be analyzed later.
8144 Vars.push_back(RefExpr);
8145 continue;
8146 }
8147
8148 SourceLocation ELoc = RefExpr->getExprLoc();
8149 // OpenMP [2.1, C/C++]
8150 // A list item is a variable name.
8151 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8152 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008153 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8154 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008155 continue;
8156 }
8157
8158 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8159
8160 // OpenMP [2.8.1, simd construct, Restrictions]
8161 // The type of list items appearing in the aligned clause must be
8162 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008163 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008164 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008165 const Type *Ty = QType.getTypePtrOrNull();
8166 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8167 !Ty->isPointerType())) {
8168 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8169 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8170 bool IsDecl =
8171 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8172 Diag(VD->getLocation(),
8173 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8174 << VD;
8175 continue;
8176 }
8177
8178 // OpenMP [2.8.1, simd construct, Restrictions]
8179 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008180 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008181 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8182 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8183 << getOpenMPClauseName(OMPC_aligned);
8184 continue;
8185 }
8186
8187 Vars.push_back(DE);
8188 }
8189
8190 // OpenMP [2.8.1, simd construct, Description]
8191 // The parameter of the aligned clause, alignment, must be a constant
8192 // positive integer expression.
8193 // If no optional parameter is specified, implementation-defined default
8194 // alignments for SIMD instructions on the target platforms are assumed.
8195 if (Alignment != nullptr) {
8196 ExprResult AlignResult =
8197 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8198 if (AlignResult.isInvalid())
8199 return nullptr;
8200 Alignment = AlignResult.get();
8201 }
8202 if (Vars.empty())
8203 return nullptr;
8204
8205 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8206 EndLoc, Vars, Alignment);
8207}
8208
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008209OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8210 SourceLocation StartLoc,
8211 SourceLocation LParenLoc,
8212 SourceLocation EndLoc) {
8213 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008214 SmallVector<Expr *, 8> SrcExprs;
8215 SmallVector<Expr *, 8> DstExprs;
8216 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008217 for (auto &RefExpr : VarList) {
8218 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8219 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008220 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008221 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008222 SrcExprs.push_back(nullptr);
8223 DstExprs.push_back(nullptr);
8224 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008225 continue;
8226 }
8227
Alexey Bataeved09d242014-05-28 05:53:51 +00008228 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008229 // OpenMP [2.1, C/C++]
8230 // A list item is a variable name.
8231 // OpenMP [2.14.4.1, Restrictions, p.1]
8232 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008233 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008234 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008235 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8236 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008237 continue;
8238 }
8239
8240 Decl *D = DE->getDecl();
8241 VarDecl *VD = cast<VarDecl>(D);
8242
8243 QualType Type = VD->getType();
8244 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8245 // It will be analyzed later.
8246 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008247 SrcExprs.push_back(nullptr);
8248 DstExprs.push_back(nullptr);
8249 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008250 continue;
8251 }
8252
8253 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8254 // A list item that appears in a copyin clause must be threadprivate.
8255 if (!DSAStack->isThreadPrivate(VD)) {
8256 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008257 << getOpenMPClauseName(OMPC_copyin)
8258 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008259 continue;
8260 }
8261
8262 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8263 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008264 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008265 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008266 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008267 auto *SrcVD =
8268 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8269 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008270 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008271 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8272 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008273 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8274 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008275 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008276 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008277 // For arrays generate assignment operation for single element and replace
8278 // it by the original array element in CodeGen.
8279 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8280 PseudoDstExpr, PseudoSrcExpr);
8281 if (AssignmentOp.isInvalid())
8282 continue;
8283 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8284 /*DiscardedValue=*/true);
8285 if (AssignmentOp.isInvalid())
8286 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008287
8288 DSAStack->addDSA(VD, DE, OMPC_copyin);
8289 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008290 SrcExprs.push_back(PseudoSrcExpr);
8291 DstExprs.push_back(PseudoDstExpr);
8292 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008293 }
8294
Alexey Bataeved09d242014-05-28 05:53:51 +00008295 if (Vars.empty())
8296 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008297
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008298 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8299 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008300}
8301
Alexey Bataevbae9a792014-06-27 10:37:06 +00008302OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8303 SourceLocation StartLoc,
8304 SourceLocation LParenLoc,
8305 SourceLocation EndLoc) {
8306 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008307 SmallVector<Expr *, 8> SrcExprs;
8308 SmallVector<Expr *, 8> DstExprs;
8309 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008310 for (auto &RefExpr : VarList) {
8311 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8312 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8313 // It will be analyzed later.
8314 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008315 SrcExprs.push_back(nullptr);
8316 DstExprs.push_back(nullptr);
8317 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008318 continue;
8319 }
8320
8321 SourceLocation ELoc = RefExpr->getExprLoc();
8322 // OpenMP [2.1, C/C++]
8323 // A list item is a variable name.
8324 // OpenMP [2.14.4.1, Restrictions, p.1]
8325 // A list item that appears in a copyin clause must be threadprivate.
8326 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8327 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008328 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8329 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008330 continue;
8331 }
8332
8333 Decl *D = DE->getDecl();
8334 VarDecl *VD = cast<VarDecl>(D);
8335
8336 QualType Type = VD->getType();
8337 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8338 // It will be analyzed later.
8339 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008340 SrcExprs.push_back(nullptr);
8341 DstExprs.push_back(nullptr);
8342 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008343 continue;
8344 }
8345
8346 // OpenMP [2.14.4.2, Restrictions, p.2]
8347 // A list item that appears in a copyprivate clause may not appear in a
8348 // private or firstprivate clause on the single construct.
8349 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008350 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008351 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8352 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008353 Diag(ELoc, diag::err_omp_wrong_dsa)
8354 << getOpenMPClauseName(DVar.CKind)
8355 << getOpenMPClauseName(OMPC_copyprivate);
8356 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8357 continue;
8358 }
8359
8360 // OpenMP [2.11.4.2, Restrictions, p.1]
8361 // All list items that appear in a copyprivate clause must be either
8362 // threadprivate or private in the enclosing context.
8363 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008364 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008365 if (DVar.CKind == OMPC_shared) {
8366 Diag(ELoc, diag::err_omp_required_access)
8367 << getOpenMPClauseName(OMPC_copyprivate)
8368 << "threadprivate or private in the enclosing context";
8369 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8370 continue;
8371 }
8372 }
8373 }
8374
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008375 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008376 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008377 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008378 << getOpenMPClauseName(OMPC_copyprivate) << Type
8379 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008380 bool IsDecl =
8381 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8382 Diag(VD->getLocation(),
8383 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8384 << VD;
8385 continue;
8386 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008387
Alexey Bataevbae9a792014-06-27 10:37:06 +00008388 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8389 // A variable of class type (or array thereof) that appears in a
8390 // copyin clause requires an accessible, unambiguous copy assignment
8391 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008392 Type = Context.getBaseElementType(Type.getNonReferenceType())
8393 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008394 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008395 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8396 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008397 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008398 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008399 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008400 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8401 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008402 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008403 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008404 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8405 PseudoDstExpr, PseudoSrcExpr);
8406 if (AssignmentOp.isInvalid())
8407 continue;
8408 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8409 /*DiscardedValue=*/true);
8410 if (AssignmentOp.isInvalid())
8411 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008412
8413 // No need to mark vars as copyprivate, they are already threadprivate or
8414 // implicitly private.
8415 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008416 SrcExprs.push_back(PseudoSrcExpr);
8417 DstExprs.push_back(PseudoDstExpr);
8418 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008419 }
8420
8421 if (Vars.empty())
8422 return nullptr;
8423
Alexey Bataeva63048e2015-03-23 06:18:07 +00008424 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8425 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008426}
8427
Alexey Bataev6125da92014-07-21 11:26:11 +00008428OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8429 SourceLocation StartLoc,
8430 SourceLocation LParenLoc,
8431 SourceLocation EndLoc) {
8432 if (VarList.empty())
8433 return nullptr;
8434
8435 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8436}
Alexey Bataevdea47612014-07-23 07:46:59 +00008437
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008438OMPClause *
8439Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8440 SourceLocation DepLoc, SourceLocation ColonLoc,
8441 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8442 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008443 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008444 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008445 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008446 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008447 return nullptr;
8448 }
8449 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008450 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8451 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008452 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008453 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008454 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8455 /*Last=*/OMPC_DEPEND_unknown, Except)
8456 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008457 return nullptr;
8458 }
8459 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008460 llvm::APSInt DepCounter(/*BitWidth=*/32);
8461 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8462 if (DepKind == OMPC_DEPEND_sink) {
8463 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8464 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8465 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008466 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008467 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008468 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8469 DSAStack->getParentOrderedRegionParam()) {
8470 for (auto &RefExpr : VarList) {
8471 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8472 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8473 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8474 // It will be analyzed later.
8475 Vars.push_back(RefExpr);
8476 continue;
8477 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008478
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008479 SourceLocation ELoc = RefExpr->getExprLoc();
8480 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8481 if (DepKind == OMPC_DEPEND_sink) {
8482 if (DepCounter >= TotalDepCount) {
8483 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8484 continue;
8485 }
8486 ++DepCounter;
8487 // OpenMP [2.13.9, Summary]
8488 // depend(dependence-type : vec), where dependence-type is:
8489 // 'sink' and where vec is the iteration vector, which has the form:
8490 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8491 // where n is the value specified by the ordered clause in the loop
8492 // directive, xi denotes the loop iteration variable of the i-th nested
8493 // loop associated with the loop directive, and di is a constant
8494 // non-negative integer.
8495 SimpleExpr = SimpleExpr->IgnoreImplicit();
8496 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8497 if (!DE) {
8498 OverloadedOperatorKind OOK = OO_None;
8499 SourceLocation OOLoc;
8500 Expr *LHS, *RHS;
8501 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8502 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8503 OOLoc = BO->getOperatorLoc();
8504 LHS = BO->getLHS()->IgnoreParenImpCasts();
8505 RHS = BO->getRHS()->IgnoreParenImpCasts();
8506 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8507 OOK = OCE->getOperator();
8508 OOLoc = OCE->getOperatorLoc();
8509 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8510 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8511 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8512 OOK = MCE->getMethodDecl()
8513 ->getNameInfo()
8514 .getName()
8515 .getCXXOverloadedOperator();
8516 OOLoc = MCE->getCallee()->getExprLoc();
8517 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8518 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8519 } else {
8520 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8521 continue;
8522 }
8523 DE = dyn_cast<DeclRefExpr>(LHS);
8524 if (!DE) {
8525 Diag(LHS->getExprLoc(),
8526 diag::err_omp_depend_sink_expected_loop_iteration)
8527 << DSAStack->getParentLoopControlVariable(
8528 DepCounter.getZExtValue());
8529 continue;
8530 }
8531 if (OOK != OO_Plus && OOK != OO_Minus) {
8532 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8533 continue;
8534 }
8535 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8536 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8537 if (Res.isInvalid())
8538 continue;
8539 }
8540 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8541 if (!CurContext->isDependentContext() &&
8542 DSAStack->getParentOrderedRegionParam() &&
8543 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8544 Diag(DE->getExprLoc(),
8545 diag::err_omp_depend_sink_expected_loop_iteration)
8546 << DSAStack->getParentLoopControlVariable(
8547 DepCounter.getZExtValue());
8548 continue;
8549 }
8550 } else {
8551 // OpenMP [2.11.1.1, Restrictions, p.3]
8552 // A variable that is part of another variable (such as a field of a
8553 // structure) but is not an array element or an array section cannot
8554 // appear in a depend clause.
8555 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8556 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8557 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8558 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8559 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8560 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8561 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008562 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8563 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008564 continue;
8565 }
8566 }
8567
8568 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8569 }
8570
8571 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8572 TotalDepCount > VarList.size() &&
8573 DSAStack->getParentOrderedRegionParam()) {
8574 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8575 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8576 }
8577 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8578 Vars.empty())
8579 return nullptr;
8580 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008581
8582 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8583 DepLoc, ColonLoc, Vars);
8584}
Michael Wonge710d542015-08-07 16:16:36 +00008585
8586OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8587 SourceLocation LParenLoc,
8588 SourceLocation EndLoc) {
8589 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008590
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008591 // OpenMP [2.9.1, Restrictions]
8592 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008593 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8594 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008595 return nullptr;
8596
Michael Wonge710d542015-08-07 16:16:36 +00008597 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8598}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008599
8600static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8601 DSAStackTy *Stack, CXXRecordDecl *RD) {
8602 if (!RD || RD->isInvalidDecl())
8603 return true;
8604
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008605 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8606 if (auto *CTD = CTSD->getSpecializedTemplate())
8607 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008608 auto QTy = SemaRef.Context.getRecordType(RD);
8609 if (RD->isDynamicClass()) {
8610 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8611 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8612 return false;
8613 }
8614 auto *DC = RD;
8615 bool IsCorrect = true;
8616 for (auto *I : DC->decls()) {
8617 if (I) {
8618 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8619 if (MD->isStatic()) {
8620 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8621 SemaRef.Diag(MD->getLocation(),
8622 diag::note_omp_static_member_in_target);
8623 IsCorrect = false;
8624 }
8625 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8626 if (VD->isStaticDataMember()) {
8627 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8628 SemaRef.Diag(VD->getLocation(),
8629 diag::note_omp_static_member_in_target);
8630 IsCorrect = false;
8631 }
8632 }
8633 }
8634 }
8635
8636 for (auto &I : RD->bases()) {
8637 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8638 I.getType()->getAsCXXRecordDecl()))
8639 IsCorrect = false;
8640 }
8641 return IsCorrect;
8642}
8643
8644static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8645 DSAStackTy *Stack, QualType QTy) {
8646 NamedDecl *ND;
8647 if (QTy->isIncompleteType(&ND)) {
8648 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8649 return false;
8650 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8651 if (!RD->isInvalidDecl() &&
8652 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8653 return false;
8654 }
8655 return true;
8656}
8657
Samuel Antao5de996e2016-01-22 20:21:36 +00008658// Return the expression of the base of the map clause or null if it cannot
8659// be determined and do all the necessary checks to see if the expression is
8660// valid as a standalone map clause expression.
8661static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8662 SourceLocation ELoc = E->getExprLoc();
8663 SourceRange ERange = E->getSourceRange();
8664
8665 // The base of elements of list in a map clause have to be either:
8666 // - a reference to variable or field.
8667 // - a member expression.
8668 // - an array expression.
8669 //
8670 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8671 // reference to 'r'.
8672 //
8673 // If we have:
8674 //
8675 // struct SS {
8676 // Bla S;
8677 // foo() {
8678 // #pragma omp target map (S.Arr[:12]);
8679 // }
8680 // }
8681 //
8682 // We want to retrieve the member expression 'this->S';
8683
8684 Expr *RelevantExpr = nullptr;
8685
8686 // Flags to help capture some memory
8687
8688 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8689 // If a list item is an array section, it must specify contiguous storage.
8690 //
8691 // For this restriction it is sufficient that we make sure only references
8692 // to variables or fields and array expressions, and that no array sections
8693 // exist except in the rightmost expression. E.g. these would be invalid:
8694 //
8695 // r.ArrS[3:5].Arr[6:7]
8696 //
8697 // r.ArrS[3:5].x
8698 //
8699 // but these would be valid:
8700 // r.ArrS[3].Arr[6:7]
8701 //
8702 // r.ArrS[3].x
8703
8704 bool IsRightMostExpression = true;
8705
8706 while (!RelevantExpr) {
8707 auto AllowArraySection = IsRightMostExpression;
8708 IsRightMostExpression = false;
8709
8710 E = E->IgnoreParenImpCasts();
8711
8712 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8713 if (!isa<VarDecl>(CurE->getDecl()))
8714 break;
8715
8716 RelevantExpr = CurE;
8717 continue;
8718 }
8719
8720 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8721 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8722
8723 if (isa<CXXThisExpr>(BaseE))
8724 // We found a base expression: this->Val.
8725 RelevantExpr = CurE;
8726 else
8727 E = BaseE;
8728
8729 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8730 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8731 << CurE->getSourceRange();
8732 break;
8733 }
8734
8735 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8736
8737 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8738 // A bit-field cannot appear in a map clause.
8739 //
8740 if (FD->isBitField()) {
8741 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8742 << CurE->getSourceRange();
8743 break;
8744 }
8745
8746 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8747 // If the type of a list item is a reference to a type T then the type
8748 // will be considered to be T for all purposes of this clause.
8749 QualType CurType = BaseE->getType();
8750 if (CurType->isReferenceType())
8751 CurType = CurType->getPointeeType();
8752
8753 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8754 // A list item cannot be a variable that is a member of a structure with
8755 // a union type.
8756 //
8757 if (auto *RT = CurType->getAs<RecordType>())
8758 if (RT->isUnionType()) {
8759 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
8760 << CurE->getSourceRange();
8761 break;
8762 }
8763
8764 continue;
8765 }
8766
8767 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
8768 E = CurE->getBase()->IgnoreParenImpCasts();
8769
8770 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
8771 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8772 << 0 << CurE->getSourceRange();
8773 break;
8774 }
8775 continue;
8776 }
8777
8778 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
8779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
8780 // If a list item is an element of a structure, only the rightmost symbol
8781 // of the variable reference can be an array section.
8782 //
8783 if (!AllowArraySection) {
8784 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
8785 << CurE->getSourceRange();
8786 break;
8787 }
8788
8789 E = CurE->getBase()->IgnoreParenImpCasts();
8790
8791 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8792 // If the type of a list item is a reference to a type T then the type
8793 // will be considered to be T for all purposes of this clause.
8794 QualType CurType = E->getType();
8795 if (CurType->isReferenceType())
8796 CurType = CurType->getPointeeType();
8797
8798 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
8799 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8800 << 0 << CurE->getSourceRange();
8801 break;
8802 }
8803
8804 continue;
8805 }
8806
8807 // If nothing else worked, this is not a valid map clause expression.
8808 SemaRef.Diag(ELoc,
8809 diag::err_omp_expected_named_var_member_or_array_expression)
8810 << ERange;
8811 break;
8812 }
8813
8814 return RelevantExpr;
8815}
8816
8817// Return true if expression E associated with value VD has conflicts with other
8818// map information.
8819static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
8820 Expr *E, bool CurrentRegionOnly) {
8821 assert(VD && E);
8822
8823 // Types used to organize the components of a valid map clause.
8824 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
8825 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
8826
8827 // Helper to extract the components in the map clause expression E and store
8828 // them into MEC. This assumes that E is a valid map clause expression, i.e.
8829 // it has already passed the single clause checks.
8830 auto ExtractMapExpressionComponents = [](Expr *TE,
8831 MapExpressionComponents &MEC) {
8832 while (true) {
8833 TE = TE->IgnoreParenImpCasts();
8834
8835 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
8836 MEC.push_back(
8837 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
8838 break;
8839 }
8840
8841 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
8842 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8843
8844 MEC.push_back(MapExpressionComponent(
8845 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
8846 if (isa<CXXThisExpr>(BaseE))
8847 break;
8848
8849 TE = BaseE;
8850 continue;
8851 }
8852
8853 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
8854 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8855 TE = CurE->getBase()->IgnoreParenImpCasts();
8856 continue;
8857 }
8858
8859 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
8860 MEC.push_back(MapExpressionComponent(CurE, nullptr));
8861 TE = CurE->getBase()->IgnoreParenImpCasts();
8862 continue;
8863 }
8864
8865 llvm_unreachable(
8866 "Expecting only valid map clause expressions at this point!");
8867 }
8868 };
8869
8870 SourceLocation ELoc = E->getExprLoc();
8871 SourceRange ERange = E->getSourceRange();
8872
8873 // In order to easily check the conflicts we need to match each component of
8874 // the expression under test with the components of the expressions that are
8875 // already in the stack.
8876
8877 MapExpressionComponents CurComponents;
8878 ExtractMapExpressionComponents(E, CurComponents);
8879
8880 assert(!CurComponents.empty() && "Map clause expression with no components!");
8881 assert(CurComponents.back().second == VD &&
8882 "Map clause expression with unexpected base!");
8883
8884 // Variables to help detecting enclosing problems in data environment nests.
8885 bool IsEnclosedByDataEnvironmentExpr = false;
8886 Expr *EnclosingExpr = nullptr;
8887
8888 bool FoundError =
8889 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
8890 MapExpressionComponents StackComponents;
8891 ExtractMapExpressionComponents(RE, StackComponents);
8892 assert(!StackComponents.empty() &&
8893 "Map clause expression with no components!");
8894 assert(StackComponents.back().second == VD &&
8895 "Map clause expression with unexpected base!");
8896
8897 // Expressions must start from the same base. Here we detect at which
8898 // point both expressions diverge from each other and see if we can
8899 // detect if the memory referred to both expressions is contiguous and
8900 // do not overlap.
8901 auto CI = CurComponents.rbegin();
8902 auto CE = CurComponents.rend();
8903 auto SI = StackComponents.rbegin();
8904 auto SE = StackComponents.rend();
8905 for (; CI != CE && SI != SE; ++CI, ++SI) {
8906
8907 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
8908 // At most one list item can be an array item derived from a given
8909 // variable in map clauses of the same construct.
8910 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
8911 isa<OMPArraySectionExpr>(CI->first)) &&
8912 (isa<ArraySubscriptExpr>(SI->first) ||
8913 isa<OMPArraySectionExpr>(SI->first))) {
8914 SemaRef.Diag(CI->first->getExprLoc(),
8915 diag::err_omp_multiple_array_items_in_map_clause)
8916 << CI->first->getSourceRange();
8917 ;
8918 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
8919 << SI->first->getSourceRange();
8920 return true;
8921 }
8922
8923 // Do both expressions have the same kind?
8924 if (CI->first->getStmtClass() != SI->first->getStmtClass())
8925 break;
8926
8927 // Are we dealing with different variables/fields?
8928 if (CI->second != SI->second)
8929 break;
8930 }
8931
8932 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8933 // List items of map clauses in the same construct must not share
8934 // original storage.
8935 //
8936 // If the expressions are exactly the same or one is a subset of the
8937 // other, it means they are sharing storage.
8938 if (CI == CE && SI == SE) {
8939 if (CurrentRegionOnly) {
8940 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8941 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8942 << RE->getSourceRange();
8943 return true;
8944 } else {
8945 // If we find the same expression in the enclosing data environment,
8946 // that is legal.
8947 IsEnclosedByDataEnvironmentExpr = true;
8948 return false;
8949 }
8950 }
8951
8952 QualType DerivedType = std::prev(CI)->first->getType();
8953 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
8954
8955 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8956 // If the type of a list item is a reference to a type T then the type
8957 // will be considered to be T for all purposes of this clause.
8958 if (DerivedType->isReferenceType())
8959 DerivedType = DerivedType->getPointeeType();
8960
8961 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
8962 // A variable for which the type is pointer and an array section
8963 // derived from that variable must not appear as list items of map
8964 // clauses of the same construct.
8965 //
8966 // Also, cover one of the cases in:
8967 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
8968 // If any part of the original storage of a list item has corresponding
8969 // storage in the device data environment, all of the original storage
8970 // must have corresponding storage in the device data environment.
8971 //
8972 if (DerivedType->isAnyPointerType()) {
8973 if (CI == CE || SI == SE) {
8974 SemaRef.Diag(
8975 DerivedLoc,
8976 diag::err_omp_pointer_mapped_along_with_derived_section)
8977 << DerivedLoc;
8978 } else {
8979 assert(CI != CE && SI != SE);
8980 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
8981 << DerivedLoc;
8982 }
8983 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8984 << RE->getSourceRange();
8985 return true;
8986 }
8987
8988 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
8989 // List items of map clauses in the same construct must not share
8990 // original storage.
8991 //
8992 // An expression is a subset of the other.
8993 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
8994 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
8995 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
8996 << RE->getSourceRange();
8997 return true;
8998 }
8999
9000 // The current expression uses the same base as other expression in the
9001 // data environment but does not contain it completelly.
9002 if (!CurrentRegionOnly && SI != SE)
9003 EnclosingExpr = RE;
9004
9005 // The current expression is a subset of the expression in the data
9006 // environment.
9007 IsEnclosedByDataEnvironmentExpr |=
9008 (!CurrentRegionOnly && CI != CE && SI == SE);
9009
9010 return false;
9011 });
9012
9013 if (CurrentRegionOnly)
9014 return FoundError;
9015
9016 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9017 // If any part of the original storage of a list item has corresponding
9018 // storage in the device data environment, all of the original storage must
9019 // have corresponding storage in the device data environment.
9020 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9021 // If a list item is an element of a structure, and a different element of
9022 // the structure has a corresponding list item in the device data environment
9023 // prior to a task encountering the construct associated with the map clause,
9024 // then the list item must also have a correspnding list item in the device
9025 // data environment prior to the task encountering the construct.
9026 //
9027 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9028 SemaRef.Diag(ELoc,
9029 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9030 << ERange;
9031 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9032 << EnclosingExpr->getSourceRange();
9033 return true;
9034 }
9035
9036 return FoundError;
9037}
9038
Samuel Antao23abd722016-01-19 20:40:49 +00009039OMPClause *
9040Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9041 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9042 SourceLocation MapLoc, SourceLocation ColonLoc,
9043 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9044 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009045 SmallVector<Expr *, 4> Vars;
9046
9047 for (auto &RE : VarList) {
9048 assert(RE && "Null expr in omp map");
9049 if (isa<DependentScopeDeclRefExpr>(RE)) {
9050 // It will be analyzed later.
9051 Vars.push_back(RE);
9052 continue;
9053 }
9054 SourceLocation ELoc = RE->getExprLoc();
9055
Kelvin Li0bff7af2015-11-23 05:32:03 +00009056 auto *VE = RE->IgnoreParenLValueCasts();
9057
9058 if (VE->isValueDependent() || VE->isTypeDependent() ||
9059 VE->isInstantiationDependent() ||
9060 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009061 // We can only analyze this information once the missing information is
9062 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009063 Vars.push_back(RE);
9064 continue;
9065 }
9066
9067 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009068
Samuel Antao5de996e2016-01-22 20:21:36 +00009069 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9070 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9071 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009072 continue;
9073 }
9074
Samuel Antao5de996e2016-01-22 20:21:36 +00009075 // Obtain the array or member expression bases if required.
9076 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9077 if (!BE)
9078 continue;
9079
9080 // If the base is a reference to a variable, we rely on that variable for
9081 // the following checks. If it is a 'this' expression we rely on the field.
9082 ValueDecl *D = nullptr;
9083 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9084 D = DRE->getDecl();
9085 } else {
9086 auto *ME = cast<MemberExpr>(BE);
9087 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9088 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009089 }
9090 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009091
Samuel Antao5de996e2016-01-22 20:21:36 +00009092 auto *VD = dyn_cast<VarDecl>(D);
9093 auto *FD = dyn_cast<FieldDecl>(D);
9094
9095 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009096 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009097
9098 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9099 // threadprivate variables cannot appear in a map clause.
9100 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009101 auto DVar = DSAStack->getTopDSA(VD, false);
9102 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9103 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9104 continue;
9105 }
9106
Samuel Antao5de996e2016-01-22 20:21:36 +00009107 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9108 // A list item cannot appear in both a map clause and a data-sharing
9109 // attribute clause on the same construct.
9110 //
9111 // TODO: Implement this check - it cannot currently be tested because of
9112 // missing implementation of the other data sharing clauses in target
9113 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009114
Samuel Antao5de996e2016-01-22 20:21:36 +00009115 // Check conflicts with other map clause expressions. We check the conflicts
9116 // with the current construct separately from the enclosing data
9117 // environment, because the restrictions are different.
9118 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9119 /*CurrentRegionOnly=*/true))
9120 break;
9121 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9122 /*CurrentRegionOnly=*/false))
9123 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009124
Samuel Antao5de996e2016-01-22 20:21:36 +00009125 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9126 // If the type of a list item is a reference to a type T then the type will
9127 // be considered to be T for all purposes of this clause.
9128 QualType Type = D->getType();
9129 if (Type->isReferenceType())
9130 Type = Type->getPointeeType();
9131
9132 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009133 // A list item must have a mappable type.
9134 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9135 DSAStack, Type))
9136 continue;
9137
Samuel Antaodf67fc42016-01-19 19:15:56 +00009138 // target enter data
9139 // OpenMP [2.10.2, Restrictions, p. 99]
9140 // A map-type must be specified in all map clauses and must be either
9141 // to or alloc.
9142 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9143 if (DKind == OMPD_target_enter_data &&
9144 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9145 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009146 << (IsMapTypeImplicit ? 1 : 0)
9147 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009148 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009149 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009150 }
9151
Samuel Antao72590762016-01-19 20:04:50 +00009152 // target exit_data
9153 // OpenMP [2.10.3, Restrictions, p. 102]
9154 // A map-type must be specified in all map clauses and must be either
9155 // from, release, or delete.
9156 DKind = DSAStack->getCurrentDirective();
9157 if (DKind == OMPD_target_exit_data &&
9158 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9159 MapType == OMPC_MAP_delete)) {
9160 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009161 << (IsMapTypeImplicit ? 1 : 0)
9162 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009163 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009164 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009165 }
9166
Kelvin Li0bff7af2015-11-23 05:32:03 +00009167 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009168 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009169 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009170
Samuel Antao5de996e2016-01-22 20:21:36 +00009171 // We need to produce a map clause even if we don't have variables so that
9172 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009173 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009174 MapTypeModifier, MapType, IsMapTypeImplicit,
9175 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009176}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009177
9178OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9179 SourceLocation StartLoc,
9180 SourceLocation LParenLoc,
9181 SourceLocation EndLoc) {
9182 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009183
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009184 // OpenMP [teams Constrcut, Restrictions]
9185 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009186 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9187 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009188 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009189
9190 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9191}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009192
9193OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9194 SourceLocation StartLoc,
9195 SourceLocation LParenLoc,
9196 SourceLocation EndLoc) {
9197 Expr *ValExpr = ThreadLimit;
9198
9199 // OpenMP [teams Constrcut, Restrictions]
9200 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009201 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9202 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009203 return nullptr;
9204
9205 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9206 EndLoc);
9207}
Alexey Bataeva0569352015-12-01 10:17:31 +00009208
9209OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9210 SourceLocation StartLoc,
9211 SourceLocation LParenLoc,
9212 SourceLocation EndLoc) {
9213 Expr *ValExpr = Priority;
9214
9215 // OpenMP [2.9.1, task Constrcut]
9216 // The priority-value is a non-negative numerical scalar expression.
9217 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9218 /*StrictlyPositive=*/false))
9219 return nullptr;
9220
9221 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9222}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009223
9224OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9225 SourceLocation StartLoc,
9226 SourceLocation LParenLoc,
9227 SourceLocation EndLoc) {
9228 Expr *ValExpr = Grainsize;
9229
9230 // OpenMP [2.9.2, taskloop Constrcut]
9231 // The parameter of the grainsize clause must be a positive integer
9232 // expression.
9233 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9234 /*StrictlyPositive=*/true))
9235 return nullptr;
9236
9237 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9238}
Alexey Bataev382967a2015-12-08 12:06:20 +00009239
9240OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9241 SourceLocation StartLoc,
9242 SourceLocation LParenLoc,
9243 SourceLocation EndLoc) {
9244 Expr *ValExpr = NumTasks;
9245
9246 // OpenMP [2.9.2, taskloop Constrcut]
9247 // The parameter of the num_tasks clause must be a positive integer
9248 // expression.
9249 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9250 /*StrictlyPositive=*/true))
9251 return nullptr;
9252
9253 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9254}
9255
Alexey Bataev28c75412015-12-15 08:19:24 +00009256OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9257 SourceLocation LParenLoc,
9258 SourceLocation EndLoc) {
9259 // OpenMP [2.13.2, critical construct, Description]
9260 // ... where hint-expression is an integer constant expression that evaluates
9261 // to a valid lock hint.
9262 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9263 if (HintExpr.isInvalid())
9264 return nullptr;
9265 return new (Context)
9266 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9267}
9268
Carlo Bertollib4adf552016-01-15 18:50:31 +00009269OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9270 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9271 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9272 SourceLocation EndLoc) {
9273 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9274 std::string Values;
9275 Values += "'";
9276 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9277 Values += "'";
9278 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9279 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9280 return nullptr;
9281 }
9282 Expr *ValExpr = ChunkSize;
9283 Expr *HelperValExpr = nullptr;
9284 if (ChunkSize) {
9285 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9286 !ChunkSize->isInstantiationDependent() &&
9287 !ChunkSize->containsUnexpandedParameterPack()) {
9288 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9289 ExprResult Val =
9290 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9291 if (Val.isInvalid())
9292 return nullptr;
9293
9294 ValExpr = Val.get();
9295
9296 // OpenMP [2.7.1, Restrictions]
9297 // chunk_size must be a loop invariant integer expression with a positive
9298 // value.
9299 llvm::APSInt Result;
9300 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9301 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9302 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9303 << "dist_schedule" << ChunkSize->getSourceRange();
9304 return nullptr;
9305 }
9306 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9307 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9308 ChunkSize->getType(), ".chunk.");
9309 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9310 ChunkSize->getExprLoc(),
9311 /*RefersToCapture=*/true);
9312 HelperValExpr = ImpVarRef;
9313 }
9314 }
9315 }
9316
9317 return new (Context)
9318 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9319 Kind, ValExpr, HelperValExpr);
9320}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009321
9322OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9323 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9324 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9325 SourceLocation KindLoc, SourceLocation EndLoc) {
9326 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9327 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9328 Kind != OMPC_DEFAULTMAP_scalar) {
9329 std::string Value;
9330 SourceLocation Loc;
9331 Value += "'";
9332 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9333 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9334 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9335 Loc = MLoc;
9336 } else {
9337 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9338 OMPC_DEFAULTMAP_scalar);
9339 Loc = KindLoc;
9340 }
9341 Value += "'";
9342 Diag(Loc, diag::err_omp_unexpected_clause_value)
9343 << Value << getOpenMPClauseName(OMPC_defaultmap);
9344 return nullptr;
9345 }
9346
9347 return new (Context)
9348 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9349}