blob: e63b40b80d0c15a3ea9e2490d08ebe6d52898f24 [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 Bataev90c228f2016-02-08 09:29:13 +000074 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000075 SourceLocation ImplicitDSALoc;
76 DSAVarData()
77 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000078 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000079 };
Alexey Bataeved09d242014-05-28 05:53:51 +000080
Alexey Bataev758e55e2013-09-06 18:03:48 +000081private:
Samuel Antao5de996e2016-01-22 20:21:36 +000082 typedef SmallVector<Expr *, 4> MapInfo;
83
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 struct DSAInfo {
85 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000086 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000087 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
90 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000091 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000092 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000093 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
94 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095
96 struct SharingMapTy {
97 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000098 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000099 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000100 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 OpenMPDirectiveKind Directive;
104 DeclarationNameInfo DirectiveName;
105 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000106 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000107 /// \brief first argument (Expr *) contains optional argument of the
108 /// 'ordered' clause, the second one is true if the regions has 'ordered'
109 /// clause, false otherwise.
110 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000111 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000112 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000113 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000114 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000115 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000116 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000117 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000118 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000119 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000122 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000124 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 };
127
Axel Naumann323862e2016-02-03 10:45:22 +0000128 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129
130 /// \brief Stack of used declaration and their data-sharing attributes.
131 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000132 /// \brief true, if check for DSA must be from parent directive, false, if
133 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000135 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000136 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000137 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138
139 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
140
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000141 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000142
143 /// \brief Checks if the variable is a local for OpenMP region.
144 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000145
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000147 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000148 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
149 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000150
Alexey Bataevaac108a2015-06-23 04:51:00 +0000151 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
152 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000154 bool isForceVarCapturing() const { return ForceCapturing; }
155 void setForceVarCapturing(bool V) { ForceCapturing = V; }
156
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000158 Scope *CurScope, SourceLocation Loc) {
159 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
160 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161 }
162
163 void pop() {
164 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
165 Stack.pop_back();
166 }
167
Alexey Bataev28c75412015-12-15 08:19:24 +0000168 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
169 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
170 }
171 const std::pair<OMPCriticalDirective *, llvm::APSInt>
172 getCriticalWithHint(const DeclarationNameInfo &Name) const {
173 auto I = Criticals.find(Name.getAsString());
174 if (I != Criticals.end())
175 return I->second;
176 return std::make_pair(nullptr, llvm::APSInt());
177 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000178 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000179 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000181 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000182
Alexey Bataev9c821032015-04-30 04:23:23 +0000183 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000185 /// \brief Check if the specified variable is a loop control variable for
186 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000187 /// \return The index of the loop control variable in the list of associated
188 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000189 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \brief Check if the specified variable is a loop control variable for
191 /// parent region.
192 /// \return The index of the loop control variable in the list of associated
193 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000194 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000195 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
196 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000197 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000198
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000200 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
201 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data sharing attributes from top of the stack for the
204 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000205 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 /// \brief Checks if the specified variables has data-sharing attributes which
209 /// match specified \a CPred predicate in any directive which matches \a DPred
210 /// predicate.
211 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000212 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000213 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000214 /// \brief Checks if the specified variables has data-sharing attributes which
215 /// match specified \a CPred predicate in any innermost directive which
216 /// matches \a DPred predicate.
217 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000218 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
219 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000220 /// \brief Checks if the specified variables has explicit data-sharing
221 /// attributes which match specified \a CPred predicate at the specified
222 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000223 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000224 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
225 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000226
227 /// \brief Returns true if the directive at level \Level matches in the
228 /// specified \a DPred predicate.
229 bool hasExplicitDirective(
230 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
231 unsigned Level);
232
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000233 /// \brief Finds a directive which matches specified \a DPred predicate.
234 template <class NamedDirectivesPredicate>
235 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 /// \brief Returns currently analyzed directive.
238 OpenMPDirectiveKind getCurrentDirective() const {
239 return Stack.back().Directive;
240 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000241 /// \brief Returns parent directive.
242 OpenMPDirectiveKind getParentDirective() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].Directive;
245 return OMPD_unknown;
246 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000247 /// \brief Return the directive associated with the provided scope.
248 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249
250 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000251 void setDefaultDSANone(SourceLocation Loc) {
252 Stack.back().DefaultAttr = DSA_none;
253 Stack.back().DefaultAttrLoc = Loc;
254 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000256 void setDefaultDSAShared(SourceLocation Loc) {
257 Stack.back().DefaultAttr = DSA_shared;
258 Stack.back().DefaultAttrLoc = Loc;
259 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260
261 DefaultDataSharingAttributes getDefaultDSA() const {
262 return Stack.back().DefaultAttr;
263 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000264 SourceLocation getDefaultDSALocation() const {
265 return Stack.back().DefaultAttrLoc;
266 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000270 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 }
273
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000274 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000275 void setOrderedRegion(bool IsOrdered, Expr *Param) {
276 Stack.back().OrderedRegion.setInt(IsOrdered);
277 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000278 }
279 /// \brief Returns true, if parent region is ordered (has associated
280 /// 'ordered' clause), false - otherwise.
281 bool isParentOrderedRegion() const {
282 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000284 return false;
285 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 /// \brief Returns optional parameter for the ordered region.
287 Expr *getParentOrderedRegionParam() const {
288 if (Stack.size() > 2)
289 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
290 return nullptr;
291 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000292 /// \brief Marks current region as nowait (it has a 'nowait' clause).
293 void setNowaitRegion(bool IsNowait = true) {
294 Stack.back().NowaitRegion = IsNowait;
295 }
296 /// \brief Returns true, if parent region is nowait (has associated
297 /// 'nowait' clause), false - otherwise.
298 bool isParentNowaitRegion() const {
299 if (Stack.size() > 2)
300 return Stack[Stack.size() - 2].NowaitRegion;
301 return false;
302 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000303 /// \brief Marks parent region as cancel region.
304 void setParentCancelRegion(bool Cancel = true) {
305 if (Stack.size() > 2)
306 Stack[Stack.size() - 2].CancelRegion =
307 Stack[Stack.size() - 2].CancelRegion || Cancel;
308 }
309 /// \brief Return true if current region has inner cancel construct.
310 bool isCancelRegion() const {
311 return Stack.back().CancelRegion;
312 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000313
Alexey Bataev9c821032015-04-30 04:23:23 +0000314 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000315 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318
Alexey Bataev13314bf2014-10-09 04:18:56 +0000319 /// \brief Marks current target region as one with closely nested teams
320 /// region.
321 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
322 if (Stack.size() > 2)
323 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
324 }
325 /// \brief Returns true, if current region has closely nested teams region.
326 bool hasInnerTeamsRegion() const {
327 return getInnerTeamsRegionLoc().isValid();
328 }
329 /// \brief Returns location of the nested teams region (if any).
330 SourceLocation getInnerTeamsRegionLoc() const {
331 if (Stack.size() > 1)
332 return Stack.back().InnerTeamsRegionLoc;
333 return SourceLocation();
334 }
335
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000336 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000338 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000339
Samuel Antao5de996e2016-01-22 20:21:36 +0000340 // Do the check specified in MapInfoCheck and return true if any issue is
341 // found.
342 template <class MapInfoCheck>
343 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
344 MapInfoCheck Check) {
345 auto SI = Stack.rbegin();
346 auto SE = Stack.rend();
347
348 if (SI == SE)
349 return false;
350
351 if (CurrentRegionOnly) {
352 SE = std::next(SI);
353 } else {
354 ++SI;
355 }
356
357 for (; SI != SE; ++SI) {
358 auto MI = SI->MappedDecls.find(VD);
359 if (MI != SI->MappedDecls.end()) {
360 for (Expr *E : MI->second) {
361 if (Check(E))
362 return true;
363 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000364 }
365 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000371 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000372 }
373 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000375bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
376 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000377 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000378 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000379}
Alexey Bataeved09d242014-05-28 05:53:51 +0000380} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000382static ValueDecl *getCanonicalDecl(ValueDecl *D) {
383 auto *VD = dyn_cast<VarDecl>(D);
384 auto *FD = dyn_cast<FieldDecl>(D);
385 if (VD != nullptr) {
386 VD = VD->getCanonicalDecl();
387 D = VD;
388 } else {
389 assert(FD);
390 FD = FD->getCanonicalDecl();
391 D = FD;
392 }
393 return D;
394}
395
Alexey Bataev758e55e2013-09-06 18:03:48 +0000396DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000397 ValueDecl *D) {
398 D = getCanonicalDecl(D);
399 auto *VD = dyn_cast<VarDecl>(D);
400 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000402 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a region but not in construct]
405 // File-scope or namespace-scope variables referenced in called routines
406 // in the region are shared unless they appear in a threadprivate
407 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000409 DVar.CKind = OMPC_shared;
410
411 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
412 // in a region but not in construct]
413 // Variables with static storage duration that are declared in called
414 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000415 if (VD && VD->hasGlobalStorage())
416 DVar.CKind = OMPC_shared;
417
418 // Non-static data members are shared by default.
419 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 return DVar;
423 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000424
Alexey Bataev758e55e2013-09-06 18:03:48 +0000425 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427 // in a Construct, C/C++, predetermined, p.1]
428 // Variables with automatic storage duration that are declared in a scope
429 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000430 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
431 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000432 DVar.CKind = OMPC_private;
433 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000434 }
435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
438 if (Iter->SharingMap.count(D)) {
439 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000440 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000442 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 return DVar;
444 }
445
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, C/C++, implicitly determined, p.1]
448 // In a parallel or task construct, the data-sharing attributes of these
449 // variables are determined by the default clause, if present.
450 switch (Iter->DefaultAttr) {
451 case DSA_shared:
452 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 case DSA_none:
456 return DVar;
457 case DSA_unspecified:
458 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
459 // in a Construct, implicitly determined, p.2]
460 // In a parallel construct, if no default clause is present, these
461 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000462 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000463 if (isOpenMPParallelDirective(DVar.DKind) ||
464 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 DVar.CKind = OMPC_shared;
466 return DVar;
467 }
468
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.4]
471 // In a task construct, if no default clause is present, a variable that in
472 // the enclosing context is determined to be shared by all implicit tasks
473 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 if (DVar.DKind == OMPD_task) {
475 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000476 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000478 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
479 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 // in a Construct, implicitly determined, p.6]
481 // In a task construct, if no default clause is present, a variable
482 // whose data-sharing attribute is not determined by the rules above is
483 // firstprivate.
484 DVarTemp = getDSA(I, D);
485 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000486 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 return DVar;
490 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 }
494 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000496 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 return DVar;
498 }
499 }
500 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
501 // in a Construct, implicitly determined, p.3]
502 // For constructs other than task, if no default clause is present, these
503 // variables inherit their data-sharing attributes from the enclosing
504 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000505 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000506}
507
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000508Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000509 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000511 auto It = Stack.back().AlignedMap.find(D);
512 if (It == Stack.back().AlignedMap.end()) {
513 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
514 Stack.back().AlignedMap[D] = NewDE;
515 return nullptr;
516 } else {
517 assert(It->second && "Unexpected nullptr expr in the aligned map");
518 return It->second;
519 }
520 return nullptr;
521}
522
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000523void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000524 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000526 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000527}
528
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000529unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000530 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000532 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
533}
534
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000535unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000536 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
539 ? Stack[Stack.size() - 2].LCVMap[D]
540 : 0;
541}
542
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000543ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000544 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
545 if (Stack[Stack.size() - 2].LCVMap.size() < I)
546 return nullptr;
547 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
548 if (Pair.second == I)
549 return Pair.first;
550 }
551 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000552}
553
Alexey Bataev90c228f2016-02-08 09:29:13 +0000554void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
555 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000556 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000557 if (A == OMPC_threadprivate) {
558 Stack[0].SharingMap[D].Attributes = A;
559 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000560 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000561 } else {
562 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
563 Stack.back().SharingMap[D].Attributes = A;
564 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000565 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
566 if (PrivateCopy)
567 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568 }
569}
570
Alexey Bataeved09d242014-05-28 05:53:51 +0000571bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000572 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000574 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000575 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000577 ++I;
578 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000579 if (I == E)
580 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000581 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000582 Scope *CurScope = getCurScope();
583 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000584 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000585 }
586 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000588 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589}
590
Alexey Bataev39f915b82015-05-08 10:41:21 +0000591/// \brief Build a variable declaration for OpenMP loop iteration variable.
592static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000593 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000594 DeclContext *DC = SemaRef.CurContext;
595 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
596 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
597 VarDecl *Decl =
598 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000599 if (Attrs) {
600 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
601 I != E; ++I)
602 Decl->addAttr(*I);
603 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000604 Decl->setImplicit();
605 return Decl;
606}
607
608static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
609 SourceLocation Loc,
610 bool RefersToCapture = false) {
611 D->setReferenced();
612 D->markUsed(S.Context);
613 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
614 SourceLocation(), D, RefersToCapture, Loc, Ty,
615 VK_LValue);
616}
617
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000618DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
619 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620 DSAVarData DVar;
621
622 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
623 // in a Construct, C/C++, predetermined, p.1]
624 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000625 auto *VD = dyn_cast<VarDecl>(D);
626 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
627 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000628 SemaRef.getLangOpts().OpenMPUseTLS &&
629 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000630 (VD && VD->getStorageClass() == SC_Register &&
631 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
632 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000633 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000634 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000635 }
636 if (Stack[0].SharingMap.count(D)) {
637 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
638 DVar.CKind = OMPC_threadprivate;
639 return DVar;
640 }
641
642 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000643 // in a Construct, C/C++, predetermined, p.4]
644 // Static data members are shared.
645 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
646 // in a Construct, C/C++, predetermined, p.7]
647 // Variables with static storage duration that are declared in a scope
648 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000649 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000650 DSAVarData DVarTemp =
651 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
652 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000653 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000655 DVar.CKind = OMPC_shared;
656 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657 }
658
659 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000660 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
661 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
663 // in a Construct, C/C++, predetermined, p.6]
664 // Variables with const qualified type having no mutable member are
665 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000666 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000667 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000668 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
669 if (auto *CTD = CTSD->getSpecializedTemplate())
670 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000672 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
673 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674 // Variables with const-qualified type having no mutable member may be
675 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000676 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
677 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000678 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
679 return DVar;
680
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 DVar.CKind = OMPC_shared;
682 return DVar;
683 }
684
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685 // Explicitly specified attributes and local variables with predetermined
686 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000687 auto StartI = std::next(Stack.rbegin());
688 auto EndI = std::prev(Stack.rend());
689 if (FromParent && StartI != EndI) {
690 StartI = std::next(StartI);
691 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000692 auto I = std::prev(StartI);
693 if (I->SharingMap.count(D)) {
694 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000695 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000696 DVar.CKind = I->SharingMap[D].Attributes;
697 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000698 }
699
700 return DVar;
701}
702
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000703DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
704 bool FromParent) {
705 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 auto StartI = Stack.rbegin();
707 auto EndI = std::prev(Stack.rend());
708 if (FromParent && StartI != EndI) {
709 StartI = std::next(StartI);
710 }
711 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000712}
713
Alexey Bataevf29276e2014-06-18 04:14:57 +0000714template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000715DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000716 DirectivesPredicate DPred,
717 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000718 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
724 for (auto I = StartI, EE = EndI; I != EE; ++I) {
725 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000727 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000728 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 return DVar;
730 }
731 return DSAVarData();
732}
733
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000736DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000737 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000738 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000739 auto StartI = std::next(Stack.rbegin());
740 auto EndI = std::prev(Stack.rend());
741 if (FromParent && StartI != EndI) {
742 StartI = std::next(StartI);
743 }
744 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000745 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000746 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000747 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000748 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000749 return DVar;
750 return DSAVarData();
751 }
752 return DSAVarData();
753}
754
Alexey Bataevaac108a2015-06-23 04:51:00 +0000755bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000756 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000757 unsigned Level) {
758 if (CPred(ClauseKindMode))
759 return true;
760 if (isClauseParsingMode())
761 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000762 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000763 auto StartI = Stack.rbegin();
764 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000765 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000766 return false;
767 std::advance(StartI, Level);
768 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
769 CPred(StartI->SharingMap[D].Attributes);
770}
771
Samuel Antao4be30e92015-10-02 17:14:03 +0000772bool DSAStackTy::hasExplicitDirective(
773 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
774 unsigned Level) {
775 if (isClauseParsingMode())
776 ++Level;
777 auto StartI = Stack.rbegin();
778 auto EndI = std::prev(Stack.rend());
779 if (std::distance(StartI, EndI) <= (int)Level)
780 return false;
781 std::advance(StartI, Level);
782 return DPred(StartI->Directive);
783}
784
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785template <class NamedDirectivesPredicate>
786bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
787 auto StartI = std::next(Stack.rbegin());
788 auto EndI = std::prev(Stack.rend());
789 if (FromParent && StartI != EndI) {
790 StartI = std::next(StartI);
791 }
792 for (auto I = StartI, EE = EndI; I != EE; ++I) {
793 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
794 return true;
795 }
796 return false;
797}
798
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000799OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
800 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
801 if (I->CurScope == S)
802 return I->Directive;
803 return OMPD_unknown;
804}
805
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806void Sema::InitDataSharingAttributesStack() {
807 VarDataSharingAttributesStack = new DSAStackTy(*this);
808}
809
810#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
811
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000812bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000813 const CapturedRegionScopeInfo *RSI) {
814 assert(LangOpts.OpenMP && "OpenMP is not allowed");
815
816 auto &Ctx = getASTContext();
817 bool IsByRef = true;
818
819 // Find the directive that is associated with the provided scope.
820 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000821 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000822
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000823 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824 // This table summarizes how a given variable should be passed to the device
825 // given its type and the clauses where it appears. This table is based on
826 // the description in OpenMP 4.5 [2.10.4, target Construct] and
827 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
828 //
829 // =========================================================================
830 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
831 // | |(tofrom:scalar)| | pvt | | | |
832 // =========================================================================
833 // | scl | | | | - | | bycopy|
834 // | scl | | - | x | - | - | bycopy|
835 // | scl | | x | - | - | - | null |
836 // | scl | x | | | - | | byref |
837 // | scl | x | - | x | - | - | bycopy|
838 // | scl | x | x | - | - | - | null |
839 // | scl | | - | - | - | x | byref |
840 // | scl | x | - | - | - | x | byref |
841 //
842 // | agg | n.a. | | | - | | byref |
843 // | agg | n.a. | - | x | - | - | byref |
844 // | agg | n.a. | x | - | - | - | null |
845 // | agg | n.a. | - | - | - | x | byref |
846 // | agg | n.a. | - | - | - | x[] | byref |
847 //
848 // | ptr | n.a. | | | - | | bycopy|
849 // | ptr | n.a. | - | x | - | - | bycopy|
850 // | ptr | n.a. | x | - | - | - | null |
851 // | ptr | n.a. | - | - | - | x | byref |
852 // | ptr | n.a. | - | - | - | x[] | bycopy|
853 // | ptr | n.a. | - | - | x | | bycopy|
854 // | ptr | n.a. | - | - | x | x | bycopy|
855 // | ptr | n.a. | - | - | x | x[] | bycopy|
856 // =========================================================================
857 // Legend:
858 // scl - scalar
859 // ptr - pointer
860 // agg - aggregate
861 // x - applies
862 // - - invalid in this combination
863 // [] - mapped with an array section
864 // byref - should be mapped by reference
865 // byval - should be mapped by value
866 // null - initialize a local variable to null on the device
867 //
868 // Observations:
869 // - All scalar declarations that show up in a map clause have to be passed
870 // by reference, because they may have been mapped in the enclosing data
871 // environment.
872 // - If the scalar value does not fit the size of uintptr, it has to be
873 // passed by reference, regardless the result in the table above.
874 // - For pointers mapped by value that have either an implicit map or an
875 // array section, the runtime library may pass the NULL value to the
876 // device instead of the value passed to it by the compiler.
877
878 // FIXME: Right now, only implicit maps are implemented. Properly mapping
879 // values requires having the map, private, and firstprivate clauses SEMA
880 // and parsing in place, which we don't yet.
881
882 if (Ty->isReferenceType())
883 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
884 IsByRef = !Ty->isScalarType();
885 }
886
887 // When passing data by value, we need to make sure it fits the uintptr size
888 // and alignment, because the runtime library only deals with uintptr types.
889 // If it does not fit the uintptr size, we need to pass the data by reference
890 // instead.
891 if (!IsByRef &&
892 (Ctx.getTypeSizeInChars(Ty) >
893 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000894 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000895 IsByRef = true;
896
897 return IsByRef;
898}
899
Alexey Bataev90c228f2016-02-08 09:29:13 +0000900VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000901 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000902 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000903
904 // If we are attempting to capture a global variable in a directive with
905 // 'target' we return true so that this global is also mapped to the device.
906 //
907 // FIXME: If the declaration is enclosed in a 'declare target' directive,
908 // then it should not be captured. Therefore, an extra check has to be
909 // inserted here once support for 'declare target' is added.
910 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000911 auto *VD = dyn_cast<VarDecl>(D);
912 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000913 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000914 !DSAStack->isClauseParsingMode())
915 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000916 if (DSAStack->getCurScope() &&
917 DSAStack->hasDirective(
918 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
919 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000920 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000921 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000922 false))
923 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000924 }
925
Alexey Bataev48977c32015-08-04 08:10:48 +0000926 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
927 (!DSAStack->isClauseParsingMode() ||
928 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000929 if (DSAStack->isLoopControlVariable(D) ||
930 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000931 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000932 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000933 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000935 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000936 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000937 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000938 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000939 if (DVarPrivate.CKind != OMPC_unknown)
940 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000941 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000942 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000943}
944
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000946 assert(LangOpts.OpenMP && "OpenMP is not allowed");
947 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000948 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000949}
950
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000951bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000952 assert(LangOpts.OpenMP && "OpenMP is not allowed");
953 // Return true if the current level is no longer enclosed in a target region.
954
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955 auto *VD = dyn_cast<VarDecl>(D);
956 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000957 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
958 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000959}
960
Alexey Bataeved09d242014-05-28 05:53:51 +0000961void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962
963void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
964 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000965 Scope *CurScope, SourceLocation Loc) {
966 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967 PushExpressionEvaluationContext(PotentiallyEvaluated);
968}
969
Alexey Bataevaac108a2015-06-23 04:51:00 +0000970void Sema::StartOpenMPClause(OpenMPClauseKind K) {
971 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000972}
973
Alexey Bataevaac108a2015-06-23 04:51:00 +0000974void Sema::EndOpenMPClause() {
975 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000976}
977
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000979 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
980 // A variable of class type (or array thereof) that appears in a lastprivate
981 // clause requires an accessible, unambiguous default constructor for the
982 // class type, unless the list item is also specified in a firstprivate
983 // clause.
984 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000985 for (auto *C : D->clauses()) {
986 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
987 SmallVector<Expr *, 8> PrivateCopies;
988 for (auto *DE : Clause->varlists()) {
989 if (DE->isValueDependent() || DE->isTypeDependent()) {
990 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000991 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000992 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000993 DE = DE->IgnoreParens();
994 VarDecl *VD = nullptr;
995 FieldDecl *FD = nullptr;
996 ValueDecl *D;
997 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
998 VD = cast<VarDecl>(DRE->getDecl());
999 D = VD;
1000 } else {
1001 assert(isa<MemberExpr>(DE));
1002 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
1003 D = FD;
1004 }
1005 QualType Type = D->getType().getNonReferenceType();
1006 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001007 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001008 // Generate helper private variable and initialize it with the
1009 // default value. The address of the original variable is replaced
1010 // by the address of the new private variable in CodeGen. This new
1011 // variable is not added to IdResolver, so the code in the OpenMP
1012 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001013 auto *VDPrivate = buildVarDecl(
1014 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001015 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001016 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1017 if (VDPrivate->isInvalidDecl())
1018 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001019 PrivateCopies.push_back(buildDeclRefExpr(
1020 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001021 } else {
1022 // The variable is also a firstprivate, so initialization sequence
1023 // for private copy is generated already.
1024 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001025 }
1026 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001027 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001028 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001029 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001030 }
1031 }
1032 }
1033
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034 DSAStack->pop();
1035 DiscardCleanupsInEvaluationContext();
1036 PopExpressionEvaluationContext();
1037}
1038
Alexander Musman3276a272015-03-21 10:12:56 +00001039static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1040 Expr *NumIterations, Sema &SemaRef,
1041 Scope *S);
1042
Alexey Bataeva769e072013-03-22 06:34:35 +00001043namespace {
1044
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001045class VarDeclFilterCCC : public CorrectionCandidateCallback {
1046private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001047 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001048
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001050 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001051 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052 NamedDecl *ND = Candidate.getCorrectionDecl();
1053 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1054 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001055 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1056 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001057 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001058 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001059 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001060};
Alexey Bataeved09d242014-05-28 05:53:51 +00001061} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001062
1063ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1064 CXXScopeSpec &ScopeSpec,
1065 const DeclarationNameInfo &Id) {
1066 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1067 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1068
1069 if (Lookup.isAmbiguous())
1070 return ExprError();
1071
1072 VarDecl *VD;
1073 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001074 if (TypoCorrection Corrected = CorrectTypo(
1075 Id, LookupOrdinaryName, CurScope, nullptr,
1076 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001077 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001078 PDiag(Lookup.empty()
1079 ? diag::err_undeclared_var_use_suggest
1080 : diag::err_omp_expected_var_arg_suggest)
1081 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001082 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001083 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001084 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1085 : diag::err_omp_expected_var_arg)
1086 << Id.getName();
1087 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001088 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 } else {
1090 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001091 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001092 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1093 return ExprError();
1094 }
1095 }
1096 Lookup.suppressDiagnostics();
1097
1098 // OpenMP [2.9.2, Syntax, C/C++]
1099 // Variables must be file-scope, namespace-scope, or static block-scope.
1100 if (!VD->hasGlobalStorage()) {
1101 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001102 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1103 bool IsDecl =
1104 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001106 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1107 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001108 return ExprError();
1109 }
1110
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001111 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1112 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001113 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1114 // A threadprivate directive for file-scope variables must appear outside
1115 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001116 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1117 !getCurLexicalContext()->isTranslationUnit()) {
1118 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001119 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1120 bool IsDecl =
1121 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1122 Diag(VD->getLocation(),
1123 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1124 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001125 return ExprError();
1126 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001127 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1128 // A threadprivate directive for static class member variables must appear
1129 // in the class definition, in the same scope in which the member
1130 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001131 if (CanonicalVD->isStaticDataMember() &&
1132 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1133 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001134 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1135 bool IsDecl =
1136 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1137 Diag(VD->getLocation(),
1138 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1139 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001140 return ExprError();
1141 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001142 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1143 // A threadprivate directive for namespace-scope variables must appear
1144 // outside any definition or declaration other than the namespace
1145 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001146 if (CanonicalVD->getDeclContext()->isNamespace() &&
1147 (!getCurLexicalContext()->isFileContext() ||
1148 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1149 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001150 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1151 bool IsDecl =
1152 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1153 Diag(VD->getLocation(),
1154 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1155 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001156 return ExprError();
1157 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001158 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1159 // A threadprivate directive for static block-scope variables must appear
1160 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001161 if (CanonicalVD->isStaticLocal() && CurScope &&
1162 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001163 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001164 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1165 bool IsDecl =
1166 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1167 Diag(VD->getLocation(),
1168 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1169 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001170 return ExprError();
1171 }
1172
1173 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1174 // A threadprivate directive must lexically precede all references to any
1175 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001176 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001178 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179 return ExprError();
1180 }
1181
1182 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001183 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1184 SourceLocation(), VD,
1185 /*RefersToEnclosingVariableOrCapture=*/false,
1186 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001187}
1188
Alexey Bataeved09d242014-05-28 05:53:51 +00001189Sema::DeclGroupPtrTy
1190Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1191 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001192 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001193 CurContext->addDecl(D);
1194 return DeclGroupPtrTy::make(DeclGroupRef(D));
1195 }
David Blaikie0403cb12016-01-15 23:43:25 +00001196 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001197}
1198
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001199namespace {
1200class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1201 Sema &SemaRef;
1202
1203public:
1204 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1205 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1206 if (VD->hasLocalStorage()) {
1207 SemaRef.Diag(E->getLocStart(),
1208 diag::err_omp_local_var_in_threadprivate_init)
1209 << E->getSourceRange();
1210 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1211 << VD << VD->getSourceRange();
1212 return true;
1213 }
1214 }
1215 return false;
1216 }
1217 bool VisitStmt(const Stmt *S) {
1218 for (auto Child : S->children()) {
1219 if (Child && Visit(Child))
1220 return true;
1221 }
1222 return false;
1223 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001224 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001225};
1226} // namespace
1227
Alexey Bataeved09d242014-05-28 05:53:51 +00001228OMPThreadPrivateDecl *
1229Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001231 for (auto &RefExpr : VarList) {
1232 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001233 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1234 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001235
Alexey Bataev376b4a42016-02-09 09:41:09 +00001236 // Mark variable as used.
1237 VD->setReferenced();
1238 VD->markUsed(Context);
1239
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001240 QualType QType = VD->getType();
1241 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1242 // It will be analyzed later.
1243 Vars.push_back(DE);
1244 continue;
1245 }
1246
Alexey Bataeva769e072013-03-22 06:34:35 +00001247 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1248 // A threadprivate variable must not have an incomplete type.
1249 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001250 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001251 continue;
1252 }
1253
1254 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1255 // A threadprivate variable must not have a reference type.
1256 if (VD->getType()->isReferenceType()) {
1257 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001258 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1259 bool IsDecl =
1260 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1261 Diag(VD->getLocation(),
1262 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1263 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001264 continue;
1265 }
1266
Samuel Antaof8b50122015-07-13 22:54:53 +00001267 // Check if this is a TLS variable. If TLS is not being supported, produce
1268 // the corresponding diagnostic.
1269 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1270 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1271 getLangOpts().OpenMPUseTLS &&
1272 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001273 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1274 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001275 Diag(ILoc, diag::err_omp_var_thread_local)
1276 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001277 bool IsDecl =
1278 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1279 Diag(VD->getLocation(),
1280 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1281 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001282 continue;
1283 }
1284
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001285 // Check if initial value of threadprivate variable reference variable with
1286 // local storage (it is not supported by runtime).
1287 if (auto Init = VD->getAnyInitializer()) {
1288 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001289 if (Checker.Visit(Init))
1290 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001291 }
1292
Alexey Bataeved09d242014-05-28 05:53:51 +00001293 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001294 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001295 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1296 Context, SourceRange(Loc, Loc)));
1297 if (auto *ML = Context.getASTMutationListener())
1298 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001299 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001300 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001301 if (!Vars.empty()) {
1302 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1303 Vars);
1304 D->setAccess(AS_public);
1305 }
1306 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001307}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001308
Alexey Bataev7ff55242014-06-19 09:13:45 +00001309static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001310 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001311 bool IsLoopIterVar = false) {
1312 if (DVar.RefExpr) {
1313 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1314 << getOpenMPClauseName(DVar.CKind);
1315 return;
1316 }
1317 enum {
1318 PDSA_StaticMemberShared,
1319 PDSA_StaticLocalVarShared,
1320 PDSA_LoopIterVarPrivate,
1321 PDSA_LoopIterVarLinear,
1322 PDSA_LoopIterVarLastprivate,
1323 PDSA_ConstVarShared,
1324 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001325 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001326 PDSA_LocalVarPrivate,
1327 PDSA_Implicit
1328 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001329 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001330 auto ReportLoc = D->getLocation();
1331 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001332 if (IsLoopIterVar) {
1333 if (DVar.CKind == OMPC_private)
1334 Reason = PDSA_LoopIterVarPrivate;
1335 else if (DVar.CKind == OMPC_lastprivate)
1336 Reason = PDSA_LoopIterVarLastprivate;
1337 else
1338 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001339 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1340 Reason = PDSA_TaskVarFirstprivate;
1341 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001344 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001346 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001347 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001348 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001349 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001350 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001351 ReportHint = true;
1352 Reason = PDSA_LocalVarPrivate;
1353 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001354 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001355 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001356 << Reason << ReportHint
1357 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1358 } else if (DVar.ImplicitDSALoc.isValid()) {
1359 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1360 << getOpenMPClauseName(DVar.CKind);
1361 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001362}
1363
Alexey Bataev758e55e2013-09-06 18:03:48 +00001364namespace {
1365class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1366 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001367 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001368 bool ErrorFound;
1369 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001370 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001371 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001372
Alexey Bataev758e55e2013-09-06 18:03:48 +00001373public:
1374 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001375 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001376 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001377 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1378 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001379
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001380 auto DVar = Stack->getTopDSA(VD, false);
1381 // Check if the variable has explicit DSA set and stop analysis if it so.
1382 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001383
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001384 auto ELoc = E->getExprLoc();
1385 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001386 // The default(none) clause requires that each variable that is referenced
1387 // in the construct, and does not have a predetermined data-sharing
1388 // attribute, must have its data-sharing attribute explicitly determined
1389 // by being listed in a data-sharing attribute clause.
1390 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001391 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001392 VarsWithInheritedDSA.count(VD) == 0) {
1393 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001394 return;
1395 }
1396
1397 // OpenMP [2.9.3.6, Restrictions, p.2]
1398 // A list item that appears in a reduction clause of the innermost
1399 // enclosing worksharing or parallel construct may not be accessed in an
1400 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001401 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001402 [](OpenMPDirectiveKind K) -> bool {
1403 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001404 isOpenMPWorksharingDirective(K) ||
1405 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001406 },
1407 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001408 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1409 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001410 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1411 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001412 return;
1413 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001414
1415 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001416 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001417 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001418 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001419 }
1420 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001421 void VisitMemberExpr(MemberExpr *E) {
1422 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1423 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1424 auto DVar = Stack->getTopDSA(FD, false);
1425 // Check if the variable has explicit DSA set and stop analysis if it
1426 // so.
1427 if (DVar.RefExpr)
1428 return;
1429
1430 auto ELoc = E->getExprLoc();
1431 auto DKind = Stack->getCurrentDirective();
1432 // OpenMP [2.9.3.6, Restrictions, p.2]
1433 // A list item that appears in a reduction clause of the innermost
1434 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001435 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001436 DVar =
1437 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1438 [](OpenMPDirectiveKind K) -> bool {
1439 return isOpenMPParallelDirective(K) ||
1440 isOpenMPWorksharingDirective(K) ||
1441 isOpenMPTeamsDirective(K);
1442 },
1443 false);
1444 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1445 ErrorFound = true;
1446 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1447 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1448 return;
1449 }
1450
1451 // Define implicit data-sharing attributes for task.
1452 DVar = Stack->getImplicitDSA(FD, false);
1453 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1454 ImplicitFirstprivate.push_back(E);
1455 }
1456 }
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 for (auto *C : S->clauses()) {
1460 // Skip analysis of arguments of implicitly defined firstprivate clause
1461 // for task directives.
1462 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1463 for (auto *CC : C->children()) {
1464 if (CC)
1465 Visit(CC);
1466 }
1467 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 }
1469 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 for (auto *C : S->children()) {
1471 if (C && !isa<OMPExecutableDirective>(C))
1472 Visit(C);
1473 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
1476 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001477 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001478 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001479 return VarsWithInheritedDSA;
1480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev7ff55242014-06-19 09:13:45 +00001482 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1483 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484};
Alexey Bataeved09d242014-05-28 05:53:51 +00001485} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486
Alexey Bataevbae9a792014-06-27 10:37:06 +00001487void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001488 switch (DKind) {
1489 case OMPD_parallel: {
1490 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001491 QualType KmpInt32PtrTy =
1492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 std::make_pair(".global_tid.", KmpInt32PtrTy),
1495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1496 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 }
1502 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001503 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001504 std::make_pair(StringRef(), QualType()) // __context with shared vars
1505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001508 break;
1509 }
1510 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001511 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001512 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001513 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001516 break;
1517 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001518 case OMPD_for_simd: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
1522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
1524 break;
1525 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001526 case OMPD_sections: {
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 Bataevd3f8dd22014-06-25 11:44:49 +00001532 break;
1533 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001534 case OMPD_section: {
1535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(StringRef(), QualType()) // __context with shared vars
1537 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001538 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1539 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001540 break;
1541 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542 case OMPD_single: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001548 break;
1549 }
Alexander Musman80c22892014-07-17 08:54:58 +00001550 case OMPD_master: {
1551 Sema::CapturedParamNameType Params[] = {
1552 std::make_pair(StringRef(), QualType()) // __context with shared vars
1553 };
1554 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1555 Params);
1556 break;
1557 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001558 case OMPD_critical: {
1559 Sema::CapturedParamNameType Params[] = {
1560 std::make_pair(StringRef(), QualType()) // __context with shared vars
1561 };
1562 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1563 Params);
1564 break;
1565 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001566 case OMPD_parallel_for: {
1567 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001568 QualType KmpInt32PtrTy =
1569 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 Sema::CapturedParamNameType Params[] = {
1571 std::make_pair(".global_tid.", KmpInt32PtrTy),
1572 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1573 std::make_pair(StringRef(), QualType()) // __context with shared vars
1574 };
1575 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1576 Params);
1577 break;
1578 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001579 case OMPD_parallel_for_simd: {
1580 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001581 QualType KmpInt32PtrTy =
1582 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 Sema::CapturedParamNameType Params[] = {
1584 std::make_pair(".global_tid.", KmpInt32PtrTy),
1585 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1586 std::make_pair(StringRef(), QualType()) // __context with shared vars
1587 };
1588 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1589 Params);
1590 break;
1591 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001592 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001593 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001594 QualType KmpInt32PtrTy =
1595 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001597 std::make_pair(".global_tid.", KmpInt32PtrTy),
1598 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001599 std::make_pair(StringRef(), QualType()) // __context with shared vars
1600 };
1601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1602 Params);
1603 break;
1604 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001605 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001606 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001607 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1608 FunctionProtoType::ExtProtoInfo EPI;
1609 EPI.Variadic = true;
1610 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001612 std::make_pair(".global_tid.", KmpInt32Ty),
1613 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001614 std::make_pair(".privates.",
1615 Context.VoidPtrTy.withConst().withRestrict()),
1616 std::make_pair(
1617 ".copy_fn.",
1618 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001619 std::make_pair(StringRef(), QualType()) // __context with shared vars
1620 };
1621 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1622 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001623 // Mark this captured region as inlined, because we don't use outlined
1624 // function directly.
1625 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1626 AlwaysInlineAttr::CreateImplicit(
1627 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001628 break;
1629 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001630 case OMPD_ordered: {
1631 Sema::CapturedParamNameType Params[] = {
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001638 case OMPD_atomic: {
1639 Sema::CapturedParamNameType Params[] = {
1640 std::make_pair(StringRef(), QualType()) // __context with shared vars
1641 };
1642 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1643 Params);
1644 break;
1645 }
Michael Wong65f367f2015-07-21 13:44:28 +00001646 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001647 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001648 case OMPD_target_parallel:
1649 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001650 Sema::CapturedParamNameType Params[] = {
1651 std::make_pair(StringRef(), QualType()) // __context with shared vars
1652 };
1653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1654 Params);
1655 break;
1656 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 case OMPD_teams: {
1658 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001659 QualType KmpInt32PtrTy =
1660 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001661 Sema::CapturedParamNameType Params[] = {
1662 std::make_pair(".global_tid.", KmpInt32PtrTy),
1663 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001670 case OMPD_taskgroup: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001678 case OMPD_taskloop: {
1679 Sema::CapturedParamNameType Params[] = {
1680 std::make_pair(StringRef(), QualType()) // __context with shared vars
1681 };
1682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1683 Params);
1684 break;
1685 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001686 case OMPD_taskloop_simd: {
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(StringRef(), QualType()) // __context with shared vars
1689 };
1690 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1691 Params);
1692 break;
1693 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001694 case OMPD_distribute: {
1695 Sema::CapturedParamNameType Params[] = {
1696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001702 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001703 case OMPD_taskyield:
1704 case OMPD_barrier:
1705 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001706 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001707 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001708 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001709 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001710 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001711 llvm_unreachable("OpenMP Directive is not allowed");
1712 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001713 llvm_unreachable("Unknown OpenMP directive");
1714 }
1715}
1716
Alexey Bataev3392d762016-02-16 11:18:12 +00001717static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1718 Expr *CaptureExpr) {
Alexey Bataev4244be22016-02-11 05:35:55 +00001719 ASTContext &C = S.getASTContext();
1720 Expr *Init = CaptureExpr->IgnoreImpCasts();
1721 QualType Ty = Init->getType();
1722 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1723 if (S.getLangOpts().CPlusPlus)
1724 Ty = C.getLValueReferenceType(Ty);
1725 else {
1726 Ty = C.getPointerType(Ty);
1727 ExprResult Res =
1728 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1729 if (!Res.isUsable())
1730 return nullptr;
1731 Init = Res.get();
1732 }
1733 }
1734 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1735 S.CurContext->addHiddenDecl(CED);
1736 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1737 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001738 return CED;
1739}
1740
1741static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
1742 Expr *CaptureExpr) {
1743 auto *CD = buildCaptureDecl(S, Id, CaptureExpr);
1744 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1745 SourceLocation());
1746}
1747
1748static DeclRefExpr *buildCapture(Sema &S, StringRef Name, Expr *CaptureExpr) {
1749 auto *CD =
1750 buildCaptureDecl(S, &S.getASTContext().Idents.get(Name), CaptureExpr);
1751 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1752 SourceLocation());
Alexey Bataev4244be22016-02-11 05:35:55 +00001753}
1754
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001755StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1756 ArrayRef<OMPClause *> Clauses) {
1757 if (!S.isUsable()) {
1758 ActOnCapturedRegionError();
1759 return StmtError();
1760 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001761
1762 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001763 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001764 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001765 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001766 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001767 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001768 Clause->getClauseKind() == OMPC_copyprivate ||
1769 (getLangOpts().OpenMPUseTLS &&
1770 getASTContext().getTargetInfo().isTLSSupported() &&
1771 Clause->getClauseKind() == OMPC_copyin)) {
1772 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001773 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001774 for (auto *VarRef : Clause->children()) {
1775 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001776 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001777 }
1778 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001779 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001780 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001781 // Mark all variables in private list clauses as used in inner region.
1782 // Required for proper codegen of combined directives.
1783 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001784 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1785 if (auto *S = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1786 for (auto *D : S->decls())
1787 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1788 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001789 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001790 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001791 if (Clause->getClauseKind() == OMPC_schedule)
1792 SC = cast<OMPScheduleClause>(Clause);
1793 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001794 OC = cast<OMPOrderedClause>(Clause);
1795 else if (Clause->getClauseKind() == OMPC_linear)
1796 LCs.push_back(cast<OMPLinearClause>(Clause));
1797 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001798 bool ErrorFound = false;
1799 // OpenMP, 2.7.1 Loop Construct, Restrictions
1800 // The nonmonotonic modifier cannot be specified if an ordered clause is
1801 // specified.
1802 if (SC &&
1803 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1804 SC->getSecondScheduleModifier() ==
1805 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1806 OC) {
1807 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1808 ? SC->getFirstScheduleModifierLoc()
1809 : SC->getSecondScheduleModifierLoc(),
1810 diag::err_omp_schedule_nonmonotonic_ordered)
1811 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1812 ErrorFound = true;
1813 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001814 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1815 for (auto *C : LCs) {
1816 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1817 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1818 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001819 ErrorFound = true;
1820 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001821 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1822 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1823 OC->getNumForLoops()) {
1824 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1825 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1826 ErrorFound = true;
1827 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001828 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001829 ActOnCapturedRegionError();
1830 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001831 }
1832 return ActOnCapturedRegionEnd(S.get());
1833}
1834
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001835static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1836 OpenMPDirectiveKind CurrentRegion,
1837 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001838 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001839 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001840 // Allowed nesting of constructs
1841 // +------------------+-----------------+------------------------------------+
1842 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1843 // +------------------+-----------------+------------------------------------+
1844 // | parallel | parallel | * |
1845 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001846 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001847 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001848 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001849 // | parallel | simd | * |
1850 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001851 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001852 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001853 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001854 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001855 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001856 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001857 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001858 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001859 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001860 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001861 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001862 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001863 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001864 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001865 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001866 // | parallel | target parallel | * |
1867 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001868 // | parallel | target enter | * |
1869 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001870 // | parallel | target exit | * |
1871 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001872 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001873 // | parallel | cancellation | |
1874 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001875 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001876 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001877 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001878 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001879 // +------------------+-----------------+------------------------------------+
1880 // | for | parallel | * |
1881 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001882 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001883 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001884 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001885 // | for | simd | * |
1886 // | for | sections | + |
1887 // | for | section | + |
1888 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001889 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001890 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001891 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001892 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001893 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001894 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001895 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001896 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001897 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001898 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001899 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001900 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001901 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001902 // | for | target parallel | * |
1903 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001904 // | for | target enter | * |
1905 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001906 // | for | target exit | * |
1907 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001908 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001909 // | for | cancellation | |
1910 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001911 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001912 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001913 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001914 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001915 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001916 // | master | parallel | * |
1917 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001918 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001919 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001920 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001921 // | master | simd | * |
1922 // | master | sections | + |
1923 // | master | section | + |
1924 // | master | single | + |
1925 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001926 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001927 // | master |parallel sections| * |
1928 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001929 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001930 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001931 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001932 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001933 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001934 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001935 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001936 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001937 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001938 // | master | target parallel | * |
1939 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001940 // | master | target enter | * |
1941 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001942 // | master | target exit | * |
1943 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001944 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001945 // | master | cancellation | |
1946 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001947 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001948 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001949 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001950 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001951 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001952 // | critical | parallel | * |
1953 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001954 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001955 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001956 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001957 // | critical | simd | * |
1958 // | critical | sections | + |
1959 // | critical | section | + |
1960 // | critical | single | + |
1961 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001962 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001963 // | critical |parallel sections| * |
1964 // | critical | task | * |
1965 // | critical | taskyield | * |
1966 // | critical | barrier | + |
1967 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001968 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001969 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001970 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001971 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001972 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001973 // | critical | target parallel | * |
1974 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001975 // | critical | target enter | * |
1976 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001977 // | critical | target exit | * |
1978 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001979 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001980 // | critical | cancellation | |
1981 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001982 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001983 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001984 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001985 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001986 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001987 // | simd | parallel | |
1988 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001989 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001990 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001991 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001992 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001993 // | simd | sections | |
1994 // | simd | section | |
1995 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001996 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001997 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001998 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001999 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002000 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002001 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002002 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002003 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002004 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002005 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002006 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002007 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002008 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002009 // | simd | target parallel | |
2010 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002011 // | simd | target enter | |
2012 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002013 // | simd | target exit | |
2014 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002015 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002016 // | simd | cancellation | |
2017 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002018 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002019 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002020 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002021 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002022 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002023 // | for simd | parallel | |
2024 // | for simd | for | |
2025 // | for simd | for simd | |
2026 // | for simd | master | |
2027 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002028 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002029 // | for simd | sections | |
2030 // | for simd | section | |
2031 // | for simd | single | |
2032 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002033 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002034 // | for simd |parallel sections| |
2035 // | for simd | task | |
2036 // | for simd | taskyield | |
2037 // | for simd | barrier | |
2038 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002039 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002040 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002041 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002042 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002043 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002044 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002045 // | for simd | target parallel | |
2046 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002047 // | for simd | target enter | |
2048 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002049 // | for simd | target exit | |
2050 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002051 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002052 // | for simd | cancellation | |
2053 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002054 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002055 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002056 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002057 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002058 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002059 // | parallel for simd| parallel | |
2060 // | parallel for simd| for | |
2061 // | parallel for simd| for simd | |
2062 // | parallel for simd| master | |
2063 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002064 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002065 // | parallel for simd| sections | |
2066 // | parallel for simd| section | |
2067 // | parallel for simd| single | |
2068 // | parallel for simd| parallel for | |
2069 // | parallel for simd|parallel for simd| |
2070 // | parallel for simd|parallel sections| |
2071 // | parallel for simd| task | |
2072 // | parallel for simd| taskyield | |
2073 // | parallel for simd| barrier | |
2074 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002075 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002076 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002077 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002078 // | parallel for simd| atomic | |
2079 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002080 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002081 // | parallel for simd| target parallel | |
2082 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002083 // | parallel for simd| target enter | |
2084 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002085 // | parallel for simd| target exit | |
2086 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002087 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002088 // | parallel for simd| cancellation | |
2089 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002090 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002091 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002092 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002093 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002094 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002095 // | sections | parallel | * |
2096 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002097 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002098 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002099 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002100 // | sections | simd | * |
2101 // | sections | sections | + |
2102 // | sections | section | * |
2103 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002104 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002105 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002106 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002107 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002108 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002109 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002110 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002111 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002112 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002113 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002114 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002115 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002116 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002117 // | sections | target parallel | * |
2118 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002119 // | sections | target enter | * |
2120 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002121 // | sections | target exit | * |
2122 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002123 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002124 // | sections | cancellation | |
2125 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002126 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002127 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002128 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002129 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002130 // +------------------+-----------------+------------------------------------+
2131 // | section | parallel | * |
2132 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002133 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002134 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002135 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002136 // | section | simd | * |
2137 // | section | sections | + |
2138 // | section | section | + |
2139 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002140 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002141 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002142 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002143 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002144 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002145 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002146 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002147 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002148 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002149 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002150 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002151 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002152 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002153 // | section | target parallel | * |
2154 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002155 // | section | target enter | * |
2156 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002157 // | section | target exit | * |
2158 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002159 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002160 // | section | cancellation | |
2161 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002162 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002163 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002164 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002165 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002166 // +------------------+-----------------+------------------------------------+
2167 // | single | parallel | * |
2168 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002169 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002170 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002171 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002172 // | single | simd | * |
2173 // | single | sections | + |
2174 // | single | section | + |
2175 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002176 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002177 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002178 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002179 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002180 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002181 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002182 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002183 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002184 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002185 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002186 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002187 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002188 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002189 // | single | target parallel | * |
2190 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002191 // | single | target enter | * |
2192 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002193 // | single | target exit | * |
2194 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002195 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002196 // | single | cancellation | |
2197 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002198 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002199 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002200 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002201 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002202 // +------------------+-----------------+------------------------------------+
2203 // | parallel for | parallel | * |
2204 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002205 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002206 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002207 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002208 // | parallel for | simd | * |
2209 // | parallel for | sections | + |
2210 // | parallel for | section | + |
2211 // | parallel for | single | + |
2212 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002213 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002214 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002215 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002216 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002217 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002218 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002219 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002220 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002221 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002222 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002223 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002224 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002225 // | parallel for | target parallel | * |
2226 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002227 // | parallel for | target enter | * |
2228 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002229 // | parallel for | target exit | * |
2230 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002231 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002232 // | parallel for | cancellation | |
2233 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002234 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002235 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002236 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002237 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002238 // +------------------+-----------------+------------------------------------+
2239 // | parallel sections| parallel | * |
2240 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002241 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002242 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002243 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002244 // | parallel sections| simd | * |
2245 // | parallel sections| sections | + |
2246 // | parallel sections| section | * |
2247 // | parallel sections| single | + |
2248 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002249 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002250 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002251 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002252 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002253 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002254 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002255 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002256 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002257 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002258 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002259 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002260 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002261 // | parallel sections| target parallel | * |
2262 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002263 // | parallel sections| target enter | * |
2264 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002265 // | parallel sections| target exit | * |
2266 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002267 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002268 // | parallel sections| cancellation | |
2269 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002270 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002271 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002272 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002273 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002274 // +------------------+-----------------+------------------------------------+
2275 // | task | parallel | * |
2276 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002277 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002278 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002279 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002280 // | task | simd | * |
2281 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002282 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002283 // | task | single | + |
2284 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002285 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002286 // | task |parallel sections| * |
2287 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002288 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002289 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002290 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002291 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002292 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002293 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002294 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002295 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002296 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002297 // | task | target parallel | * |
2298 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002299 // | task | target enter | * |
2300 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002301 // | task | target exit | * |
2302 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002303 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002304 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002305 // | | point | ! |
2306 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002307 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002308 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002309 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002310 // +------------------+-----------------+------------------------------------+
2311 // | ordered | parallel | * |
2312 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002313 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002314 // | ordered | master | * |
2315 // | ordered | critical | * |
2316 // | ordered | simd | * |
2317 // | ordered | sections | + |
2318 // | ordered | section | + |
2319 // | ordered | single | + |
2320 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002321 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002322 // | ordered |parallel sections| * |
2323 // | ordered | task | * |
2324 // | ordered | taskyield | * |
2325 // | ordered | barrier | + |
2326 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002327 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002328 // | ordered | flush | * |
2329 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002330 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002331 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002332 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002333 // | ordered | target parallel | * |
2334 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002335 // | ordered | target enter | * |
2336 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002337 // | ordered | target exit | * |
2338 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002339 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002340 // | ordered | cancellation | |
2341 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002342 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002343 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002344 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002345 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002346 // +------------------+-----------------+------------------------------------+
2347 // | atomic | parallel | |
2348 // | atomic | for | |
2349 // | atomic | for simd | |
2350 // | atomic | master | |
2351 // | atomic | critical | |
2352 // | atomic | simd | |
2353 // | atomic | sections | |
2354 // | atomic | section | |
2355 // | atomic | single | |
2356 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002357 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002358 // | atomic |parallel sections| |
2359 // | atomic | task | |
2360 // | atomic | taskyield | |
2361 // | atomic | barrier | |
2362 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002363 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002364 // | atomic | flush | |
2365 // | atomic | ordered | |
2366 // | atomic | atomic | |
2367 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002368 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002369 // | atomic | target parallel | |
2370 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002371 // | atomic | target enter | |
2372 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002373 // | atomic | target exit | |
2374 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002375 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002376 // | atomic | cancellation | |
2377 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002378 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002379 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002380 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002381 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002382 // +------------------+-----------------+------------------------------------+
2383 // | target | parallel | * |
2384 // | target | for | * |
2385 // | target | for simd | * |
2386 // | target | master | * |
2387 // | target | critical | * |
2388 // | target | simd | * |
2389 // | target | sections | * |
2390 // | target | section | * |
2391 // | target | single | * |
2392 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002393 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002394 // | target |parallel sections| * |
2395 // | target | task | * |
2396 // | target | taskyield | * |
2397 // | target | barrier | * |
2398 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002399 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002400 // | target | flush | * |
2401 // | target | ordered | * |
2402 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002403 // | target | target | |
2404 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002405 // | target | target parallel | |
2406 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002407 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002408 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002409 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002410 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002411 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002412 // | target | cancellation | |
2413 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002414 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002415 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002416 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002417 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002418 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002419 // | target parallel | parallel | * |
2420 // | target parallel | for | * |
2421 // | target parallel | for simd | * |
2422 // | target parallel | master | * |
2423 // | target parallel | critical | * |
2424 // | target parallel | simd | * |
2425 // | target parallel | sections | * |
2426 // | target parallel | section | * |
2427 // | target parallel | single | * |
2428 // | target parallel | parallel for | * |
2429 // | target parallel |parallel for simd| * |
2430 // | target parallel |parallel sections| * |
2431 // | target parallel | task | * |
2432 // | target parallel | taskyield | * |
2433 // | target parallel | barrier | * |
2434 // | target parallel | taskwait | * |
2435 // | target parallel | taskgroup | * |
2436 // | target parallel | flush | * |
2437 // | target parallel | ordered | * |
2438 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002439 // | target parallel | target | |
2440 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002441 // | target parallel | target parallel | |
2442 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002443 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002444 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002445 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002446 // | | data | |
2447 // | target parallel | teams | |
2448 // | target parallel | cancellation | |
2449 // | | point | ! |
2450 // | target parallel | cancel | ! |
2451 // | target parallel | taskloop | * |
2452 // | target parallel | taskloop simd | * |
2453 // | target parallel | distribute | |
2454 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002455 // | target parallel | parallel | * |
2456 // | for | | |
2457 // | target parallel | for | * |
2458 // | for | | |
2459 // | target parallel | for simd | * |
2460 // | for | | |
2461 // | target parallel | master | * |
2462 // | for | | |
2463 // | target parallel | critical | * |
2464 // | for | | |
2465 // | target parallel | simd | * |
2466 // | for | | |
2467 // | target parallel | sections | * |
2468 // | for | | |
2469 // | target parallel | section | * |
2470 // | for | | |
2471 // | target parallel | single | * |
2472 // | for | | |
2473 // | target parallel | parallel for | * |
2474 // | for | | |
2475 // | target parallel |parallel for simd| * |
2476 // | for | | |
2477 // | target parallel |parallel sections| * |
2478 // | for | | |
2479 // | target parallel | task | * |
2480 // | for | | |
2481 // | target parallel | taskyield | * |
2482 // | for | | |
2483 // | target parallel | barrier | * |
2484 // | for | | |
2485 // | target parallel | taskwait | * |
2486 // | for | | |
2487 // | target parallel | taskgroup | * |
2488 // | for | | |
2489 // | target parallel | flush | * |
2490 // | for | | |
2491 // | target parallel | ordered | * |
2492 // | for | | |
2493 // | target parallel | atomic | * |
2494 // | for | | |
2495 // | target parallel | target | |
2496 // | for | | |
2497 // | target parallel | target parallel | |
2498 // | for | | |
2499 // | target parallel | target parallel | |
2500 // | for | for | |
2501 // | target parallel | target enter | |
2502 // | for | data | |
2503 // | target parallel | target exit | |
2504 // | for | data | |
2505 // | target parallel | teams | |
2506 // | for | | |
2507 // | target parallel | cancellation | |
2508 // | for | point | ! |
2509 // | target parallel | cancel | ! |
2510 // | for | | |
2511 // | target parallel | taskloop | * |
2512 // | for | | |
2513 // | target parallel | taskloop simd | * |
2514 // | for | | |
2515 // | target parallel | distribute | |
2516 // | for | | |
2517 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002518 // | teams | parallel | * |
2519 // | teams | for | + |
2520 // | teams | for simd | + |
2521 // | teams | master | + |
2522 // | teams | critical | + |
2523 // | teams | simd | + |
2524 // | teams | sections | + |
2525 // | teams | section | + |
2526 // | teams | single | + |
2527 // | teams | parallel for | * |
2528 // | teams |parallel for simd| * |
2529 // | teams |parallel sections| * |
2530 // | teams | task | + |
2531 // | teams | taskyield | + |
2532 // | teams | barrier | + |
2533 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002534 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002535 // | teams | flush | + |
2536 // | teams | ordered | + |
2537 // | teams | atomic | + |
2538 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002539 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002540 // | teams | target parallel | + |
2541 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002542 // | teams | target enter | + |
2543 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002544 // | teams | target exit | + |
2545 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002546 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002547 // | teams | cancellation | |
2548 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002549 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002550 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002551 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002552 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002553 // +------------------+-----------------+------------------------------------+
2554 // | taskloop | parallel | * |
2555 // | taskloop | for | + |
2556 // | taskloop | for simd | + |
2557 // | taskloop | master | + |
2558 // | taskloop | critical | * |
2559 // | taskloop | simd | * |
2560 // | taskloop | sections | + |
2561 // | taskloop | section | + |
2562 // | taskloop | single | + |
2563 // | taskloop | parallel for | * |
2564 // | taskloop |parallel for simd| * |
2565 // | taskloop |parallel sections| * |
2566 // | taskloop | task | * |
2567 // | taskloop | taskyield | * |
2568 // | taskloop | barrier | + |
2569 // | taskloop | taskwait | * |
2570 // | taskloop | taskgroup | * |
2571 // | taskloop | flush | * |
2572 // | taskloop | ordered | + |
2573 // | taskloop | atomic | * |
2574 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002575 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002576 // | taskloop | target parallel | * |
2577 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002578 // | taskloop | target enter | * |
2579 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002580 // | taskloop | target exit | * |
2581 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002582 // | taskloop | teams | + |
2583 // | taskloop | cancellation | |
2584 // | | point | |
2585 // | taskloop | cancel | |
2586 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002587 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002588 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002589 // | taskloop simd | parallel | |
2590 // | taskloop simd | for | |
2591 // | taskloop simd | for simd | |
2592 // | taskloop simd | master | |
2593 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002594 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002595 // | taskloop simd | sections | |
2596 // | taskloop simd | section | |
2597 // | taskloop simd | single | |
2598 // | taskloop simd | parallel for | |
2599 // | taskloop simd |parallel for simd| |
2600 // | taskloop simd |parallel sections| |
2601 // | taskloop simd | task | |
2602 // | taskloop simd | taskyield | |
2603 // | taskloop simd | barrier | |
2604 // | taskloop simd | taskwait | |
2605 // | taskloop simd | taskgroup | |
2606 // | taskloop simd | flush | |
2607 // | taskloop simd | ordered | + (with simd clause) |
2608 // | taskloop simd | atomic | |
2609 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002610 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002611 // | taskloop simd | target parallel | |
2612 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002613 // | taskloop simd | target enter | |
2614 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002615 // | taskloop simd | target exit | |
2616 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002617 // | taskloop simd | teams | |
2618 // | taskloop simd | cancellation | |
2619 // | | point | |
2620 // | taskloop simd | cancel | |
2621 // | taskloop simd | taskloop | |
2622 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002623 // | taskloop simd | distribute | |
2624 // +------------------+-----------------+------------------------------------+
2625 // | distribute | parallel | * |
2626 // | distribute | for | * |
2627 // | distribute | for simd | * |
2628 // | distribute | master | * |
2629 // | distribute | critical | * |
2630 // | distribute | simd | * |
2631 // | distribute | sections | * |
2632 // | distribute | section | * |
2633 // | distribute | single | * |
2634 // | distribute | parallel for | * |
2635 // | distribute |parallel for simd| * |
2636 // | distribute |parallel sections| * |
2637 // | distribute | task | * |
2638 // | distribute | taskyield | * |
2639 // | distribute | barrier | * |
2640 // | distribute | taskwait | * |
2641 // | distribute | taskgroup | * |
2642 // | distribute | flush | * |
2643 // | distribute | ordered | + |
2644 // | distribute | atomic | * |
2645 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002646 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002647 // | distribute | target parallel | |
2648 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002649 // | distribute | target enter | |
2650 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002651 // | distribute | target exit | |
2652 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002653 // | distribute | teams | |
2654 // | distribute | cancellation | + |
2655 // | | point | |
2656 // | distribute | cancel | + |
2657 // | distribute | taskloop | * |
2658 // | distribute | taskloop simd | * |
2659 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002660 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002661 if (Stack->getCurScope()) {
2662 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002663 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002664 bool NestingProhibited = false;
2665 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002666 enum {
2667 NoRecommend,
2668 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002669 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002670 ShouldBeInTargetRegion,
2671 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002672 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002673 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2674 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002675 // OpenMP [2.16, Nesting of Regions]
2676 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002677 // OpenMP [2.8.1,simd Construct, Restrictions]
2678 // An ordered construct with the simd clause is the only OpenMP construct
2679 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002680 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2681 return true;
2682 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002683 if (ParentRegion == OMPD_atomic) {
2684 // OpenMP [2.16, Nesting of Regions]
2685 // OpenMP constructs may not be nested inside an atomic region.
2686 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2687 return true;
2688 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002689 if (CurrentRegion == OMPD_section) {
2690 // OpenMP [2.7.2, sections Construct, Restrictions]
2691 // Orphaned section directives are prohibited. That is, the section
2692 // directives must appear within the sections construct and must not be
2693 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002694 if (ParentRegion != OMPD_sections &&
2695 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002696 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2697 << (ParentRegion != OMPD_unknown)
2698 << getOpenMPDirectiveName(ParentRegion);
2699 return true;
2700 }
2701 return false;
2702 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002703 // Allow some constructs to be orphaned (they could be used in functions,
2704 // called from OpenMP regions with the required preconditions).
2705 if (ParentRegion == OMPD_unknown)
2706 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002707 if (CurrentRegion == OMPD_cancellation_point ||
2708 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002709 // OpenMP [2.16, Nesting of Regions]
2710 // A cancellation point construct for which construct-type-clause is
2711 // taskgroup must be nested inside a task construct. A cancellation
2712 // point construct for which construct-type-clause is not taskgroup must
2713 // be closely nested inside an OpenMP construct that matches the type
2714 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002715 // A cancel construct for which construct-type-clause is taskgroup must be
2716 // nested inside a task construct. A cancel construct for which
2717 // construct-type-clause is not taskgroup must be closely nested inside an
2718 // OpenMP construct that matches the type specified in
2719 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002720 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002721 !((CancelRegion == OMPD_parallel &&
2722 (ParentRegion == OMPD_parallel ||
2723 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002724 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002725 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2726 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002727 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2728 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002729 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2730 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002731 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002732 // OpenMP [2.16, Nesting of Regions]
2733 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002734 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002735 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002736 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002737 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002738 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2739 // OpenMP [2.16, Nesting of Regions]
2740 // A critical region may not be nested (closely or otherwise) inside a
2741 // critical region with the same name. Note that this restriction is not
2742 // sufficient to prevent deadlock.
2743 SourceLocation PreviousCriticalLoc;
2744 bool DeadLock =
2745 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2746 OpenMPDirectiveKind K,
2747 const DeclarationNameInfo &DNI,
2748 SourceLocation Loc)
2749 ->bool {
2750 if (K == OMPD_critical &&
2751 DNI.getName() == CurrentName.getName()) {
2752 PreviousCriticalLoc = Loc;
2753 return true;
2754 } else
2755 return false;
2756 },
2757 false /* skip top directive */);
2758 if (DeadLock) {
2759 SemaRef.Diag(StartLoc,
2760 diag::err_omp_prohibited_region_critical_same_name)
2761 << CurrentName.getName();
2762 if (PreviousCriticalLoc.isValid())
2763 SemaRef.Diag(PreviousCriticalLoc,
2764 diag::note_omp_previous_critical_region);
2765 return true;
2766 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002767 } else if (CurrentRegion == OMPD_barrier) {
2768 // OpenMP [2.16, Nesting of Regions]
2769 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002770 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002771 NestingProhibited =
2772 isOpenMPWorksharingDirective(ParentRegion) ||
2773 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002774 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002775 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002776 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002777 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002778 // OpenMP [2.16, Nesting of Regions]
2779 // A worksharing region may not be closely nested inside a worksharing,
2780 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002781 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002782 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002783 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002784 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002785 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002786 Recommend = ShouldBeInParallelRegion;
2787 } else if (CurrentRegion == OMPD_ordered) {
2788 // OpenMP [2.16, Nesting of Regions]
2789 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002790 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002791 // An ordered region must be closely nested inside a loop region (or
2792 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002793 // OpenMP [2.8.1,simd Construct, Restrictions]
2794 // An ordered construct with the simd clause is the only OpenMP construct
2795 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002796 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002797 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002798 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002799 !(isOpenMPSimdDirective(ParentRegion) ||
2800 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002801 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002802 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2803 // OpenMP [2.16, Nesting of Regions]
2804 // If specified, a teams construct must be contained within a target
2805 // construct.
2806 NestingProhibited = ParentRegion != OMPD_target;
2807 Recommend = ShouldBeInTargetRegion;
2808 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2809 }
2810 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2811 // OpenMP [2.16, Nesting of Regions]
2812 // distribute, parallel, parallel sections, parallel workshare, and the
2813 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2814 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002815 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2816 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002817 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002818 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002819 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2820 // OpenMP 4.5 [2.17 Nesting of Regions]
2821 // The region associated with the distribute construct must be strictly
2822 // nested inside a teams region
2823 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2824 Recommend = ShouldBeInTeamsRegion;
2825 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002826 if (!NestingProhibited &&
2827 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2828 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2829 // OpenMP 4.5 [2.17 Nesting of Regions]
2830 // If a target, target update, target data, target enter data, or
2831 // target exit data construct is encountered during execution of a
2832 // target region, the behavior is unspecified.
2833 NestingProhibited = Stack->hasDirective(
2834 [&OffendingRegion](OpenMPDirectiveKind K,
2835 const DeclarationNameInfo &DNI,
2836 SourceLocation Loc) -> bool {
2837 if (isOpenMPTargetExecutionDirective(K)) {
2838 OffendingRegion = K;
2839 return true;
2840 } else
2841 return false;
2842 },
2843 false /* don't skip top directive */);
2844 CloseNesting = false;
2845 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002846 if (NestingProhibited) {
2847 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002848 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2849 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002850 return true;
2851 }
2852 }
2853 return false;
2854}
2855
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002856static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2857 ArrayRef<OMPClause *> Clauses,
2858 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2859 bool ErrorFound = false;
2860 unsigned NamedModifiersNumber = 0;
2861 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2862 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002863 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002864 for (const auto *C : Clauses) {
2865 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2866 // At most one if clause without a directive-name-modifier can appear on
2867 // the directive.
2868 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2869 if (FoundNameModifiers[CurNM]) {
2870 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2871 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2872 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2873 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002874 } else if (CurNM != OMPD_unknown) {
2875 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002876 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002877 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002878 FoundNameModifiers[CurNM] = IC;
2879 if (CurNM == OMPD_unknown)
2880 continue;
2881 // Check if the specified name modifier is allowed for the current
2882 // directive.
2883 // At most one if clause with the particular directive-name-modifier can
2884 // appear on the directive.
2885 bool MatchFound = false;
2886 for (auto NM : AllowedNameModifiers) {
2887 if (CurNM == NM) {
2888 MatchFound = true;
2889 break;
2890 }
2891 }
2892 if (!MatchFound) {
2893 S.Diag(IC->getNameModifierLoc(),
2894 diag::err_omp_wrong_if_directive_name_modifier)
2895 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2896 ErrorFound = true;
2897 }
2898 }
2899 }
2900 // If any if clause on the directive includes a directive-name-modifier then
2901 // all if clauses on the directive must include a directive-name-modifier.
2902 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2903 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2904 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2905 diag::err_omp_no_more_if_clause);
2906 } else {
2907 std::string Values;
2908 std::string Sep(", ");
2909 unsigned AllowedCnt = 0;
2910 unsigned TotalAllowedNum =
2911 AllowedNameModifiers.size() - NamedModifiersNumber;
2912 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2913 ++Cnt) {
2914 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2915 if (!FoundNameModifiers[NM]) {
2916 Values += "'";
2917 Values += getOpenMPDirectiveName(NM);
2918 Values += "'";
2919 if (AllowedCnt + 2 == TotalAllowedNum)
2920 Values += " or ";
2921 else if (AllowedCnt + 1 != TotalAllowedNum)
2922 Values += Sep;
2923 ++AllowedCnt;
2924 }
2925 }
2926 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2927 diag::err_omp_unnamed_if_clause)
2928 << (TotalAllowedNum > 1) << Values;
2929 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002930 for (auto Loc : NameModifierLoc) {
2931 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2932 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002933 ErrorFound = true;
2934 }
2935 return ErrorFound;
2936}
2937
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002938StmtResult Sema::ActOnOpenMPExecutableDirective(
2939 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2940 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2941 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002942 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002943 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2944 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002945 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002946
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002947 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002948 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002949 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002950 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002951 if (AStmt) {
2952 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2953
2954 // Check default data sharing attributes for referenced variables.
2955 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2956 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2957 if (DSAChecker.isErrorFound())
2958 return StmtError();
2959 // Generate list of implicitly defined firstprivate variables.
2960 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002961
2962 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2963 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2964 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2965 SourceLocation(), SourceLocation())) {
2966 ClausesWithImplicit.push_back(Implicit);
2967 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2968 DSAChecker.getImplicitFirstprivate().size();
2969 } else
2970 ErrorFound = true;
2971 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002972 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002973
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002974 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002975 switch (Kind) {
2976 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002977 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2978 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002979 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002980 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002981 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002982 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2983 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002984 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002985 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002986 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2987 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002988 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002989 case OMPD_for_simd:
2990 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2991 EndLoc, VarsWithInheritedDSA);
2992 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002993 case OMPD_sections:
2994 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2995 EndLoc);
2996 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002997 case OMPD_section:
2998 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002999 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003000 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3001 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003002 case OMPD_single:
3003 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3004 EndLoc);
3005 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003006 case OMPD_master:
3007 assert(ClausesWithImplicit.empty() &&
3008 "No clauses are allowed for 'omp master' directive");
3009 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3010 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003011 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003012 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3013 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003014 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003015 case OMPD_parallel_for:
3016 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3017 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003018 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003019 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003020 case OMPD_parallel_for_simd:
3021 Res = ActOnOpenMPParallelForSimdDirective(
3022 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003023 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003024 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003025 case OMPD_parallel_sections:
3026 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3027 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003028 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003029 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003030 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003031 Res =
3032 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003033 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003034 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003035 case OMPD_taskyield:
3036 assert(ClausesWithImplicit.empty() &&
3037 "No clauses are allowed for 'omp taskyield' directive");
3038 assert(AStmt == nullptr &&
3039 "No associated statement allowed for 'omp taskyield' directive");
3040 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3041 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003042 case OMPD_barrier:
3043 assert(ClausesWithImplicit.empty() &&
3044 "No clauses are allowed for 'omp barrier' directive");
3045 assert(AStmt == nullptr &&
3046 "No associated statement allowed for 'omp barrier' directive");
3047 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3048 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003049 case OMPD_taskwait:
3050 assert(ClausesWithImplicit.empty() &&
3051 "No clauses are allowed for 'omp taskwait' directive");
3052 assert(AStmt == nullptr &&
3053 "No associated statement allowed for 'omp taskwait' directive");
3054 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3055 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003056 case OMPD_taskgroup:
3057 assert(ClausesWithImplicit.empty() &&
3058 "No clauses are allowed for 'omp taskgroup' directive");
3059 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3060 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003061 case OMPD_flush:
3062 assert(AStmt == nullptr &&
3063 "No associated statement allowed for 'omp flush' directive");
3064 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3065 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003066 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003067 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3068 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003069 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003070 case OMPD_atomic:
3071 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3072 EndLoc);
3073 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003074 case OMPD_teams:
3075 Res =
3076 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3077 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003078 case OMPD_target:
3079 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3080 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003081 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003082 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003083 case OMPD_target_parallel:
3084 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3085 StartLoc, EndLoc);
3086 AllowedNameModifiers.push_back(OMPD_target);
3087 AllowedNameModifiers.push_back(OMPD_parallel);
3088 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003089 case OMPD_target_parallel_for:
3090 Res = ActOnOpenMPTargetParallelForDirective(
3091 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3092 AllowedNameModifiers.push_back(OMPD_target);
3093 AllowedNameModifiers.push_back(OMPD_parallel);
3094 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003095 case OMPD_cancellation_point:
3096 assert(ClausesWithImplicit.empty() &&
3097 "No clauses are allowed for 'omp cancellation point' directive");
3098 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3099 "cancellation point' directive");
3100 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3101 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003102 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003103 assert(AStmt == nullptr &&
3104 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003105 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3106 CancelRegion);
3107 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003108 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003109 case OMPD_target_data:
3110 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3111 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003112 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003113 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003114 case OMPD_target_enter_data:
3115 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3116 EndLoc);
3117 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3118 break;
Samuel Antao72590762016-01-19 20:04:50 +00003119 case OMPD_target_exit_data:
3120 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3121 EndLoc);
3122 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3123 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003124 case OMPD_taskloop:
3125 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3126 EndLoc, VarsWithInheritedDSA);
3127 AllowedNameModifiers.push_back(OMPD_taskloop);
3128 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003129 case OMPD_taskloop_simd:
3130 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3131 EndLoc, VarsWithInheritedDSA);
3132 AllowedNameModifiers.push_back(OMPD_taskloop);
3133 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003134 case OMPD_distribute:
3135 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3136 EndLoc, VarsWithInheritedDSA);
3137 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003138 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003139 llvm_unreachable("OpenMP Directive is not allowed");
3140 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003141 llvm_unreachable("Unknown OpenMP directive");
3142 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003143
Alexey Bataev4acb8592014-07-07 13:01:15 +00003144 for (auto P : VarsWithInheritedDSA) {
3145 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3146 << P.first << P.second->getSourceRange();
3147 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003148 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3149
3150 if (!AllowedNameModifiers.empty())
3151 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3152 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003153
Alexey Bataeved09d242014-05-28 05:53:51 +00003154 if (ErrorFound)
3155 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003156 return Res;
3157}
3158
3159StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3160 Stmt *AStmt,
3161 SourceLocation StartLoc,
3162 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003163 if (!AStmt)
3164 return StmtError();
3165
Alexey Bataev9959db52014-05-06 10:08:46 +00003166 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3167 // 1.2.2 OpenMP Language Terminology
3168 // Structured block - An executable statement with a single entry at the
3169 // top and a single exit at the bottom.
3170 // The point of exit cannot be a branch out of the structured block.
3171 // longjmp() and throw() must not violate the entry/exit criteria.
3172 CS->getCapturedDecl()->setNothrow();
3173
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003174 getCurFunction()->setHasBranchProtectedScope();
3175
Alexey Bataev25e5b442015-09-15 12:52:43 +00003176 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3177 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003178}
3179
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003180namespace {
3181/// \brief Helper class for checking canonical form of the OpenMP loops and
3182/// extracting iteration space of each loop in the loop nest, that will be used
3183/// for IR generation.
3184class OpenMPIterationSpaceChecker {
3185 /// \brief Reference to Sema.
3186 Sema &SemaRef;
3187 /// \brief A location for diagnostics (when there is no some better location).
3188 SourceLocation DefaultLoc;
3189 /// \brief A location for diagnostics (when increment is not compatible).
3190 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003191 /// \brief A source location for referring to loop init later.
3192 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003193 /// \brief A source location for referring to condition later.
3194 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003195 /// \brief A source location for referring to increment later.
3196 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003197 /// \brief Loop variable.
3198 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003199 /// \brief Reference to loop variable.
3200 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003201 /// \brief Lower bound (initializer for the var).
3202 Expr *LB;
3203 /// \brief Upper bound.
3204 Expr *UB;
3205 /// \brief Loop step (increment).
3206 Expr *Step;
3207 /// \brief This flag is true when condition is one of:
3208 /// Var < UB
3209 /// Var <= UB
3210 /// UB > Var
3211 /// UB >= Var
3212 bool TestIsLessOp;
3213 /// \brief This flag is true when condition is strict ( < or > ).
3214 bool TestIsStrictOp;
3215 /// \brief This flag is true when step is subtracted on each iteration.
3216 bool SubtractStep;
3217
3218public:
3219 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3220 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003221 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3222 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003223 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3224 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003225 /// \brief Check init-expr for canonical loop form and save loop counter
3226 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003227 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003228 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3229 /// for less/greater and for strict/non-strict comparison.
3230 bool CheckCond(Expr *S);
3231 /// \brief Check incr-expr for canonical loop form and return true if it
3232 /// does not conform, otherwise save loop step (#Step).
3233 bool CheckInc(Expr *S);
3234 /// \brief Return the loop counter variable.
3235 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003236 /// \brief Return the reference expression to loop counter variable.
3237 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003238 /// \brief Source range of the loop init.
3239 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3240 /// \brief Source range of the loop condition.
3241 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3242 /// \brief Source range of the loop increment.
3243 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3244 /// \brief True if the step should be subtracted.
3245 bool ShouldSubtractStep() const { return SubtractStep; }
3246 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003247 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003248 /// \brief Build the precondition expression for the loops.
3249 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003250 /// \brief Build reference expression to the counter be used for codegen.
3251 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003252 /// \brief Build reference expression to the private counter be used for
3253 /// codegen.
3254 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003255 /// \brief Build initization of the counter be used for codegen.
3256 Expr *BuildCounterInit() const;
3257 /// \brief Build step of the counter be used for codegen.
3258 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003259 /// \brief Return true if any expression is dependent.
3260 bool Dependent() const;
3261
3262private:
3263 /// \brief Check the right-hand side of an assignment in the increment
3264 /// expression.
3265 bool CheckIncRHS(Expr *RHS);
3266 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003267 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003268 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003269 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003270 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003271 /// \brief Helper to set loop increment.
3272 bool SetStep(Expr *NewStep, bool Subtract);
3273};
3274
3275bool OpenMPIterationSpaceChecker::Dependent() const {
3276 if (!Var) {
3277 assert(!LB && !UB && !Step);
3278 return false;
3279 }
3280 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3281 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3282}
3283
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003284template <typename T>
3285static T *getExprAsWritten(T *E) {
3286 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3287 E = ExprTemp->getSubExpr();
3288
3289 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3290 E = MTE->GetTemporaryExpr();
3291
3292 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3293 E = Binder->getSubExpr();
3294
3295 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3296 E = ICE->getSubExprAsWritten();
3297 return E->IgnoreParens();
3298}
3299
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003300bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3301 DeclRefExpr *NewVarRefExpr,
3302 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003303 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003304 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3305 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003306 if (!NewVar || !NewLB)
3307 return true;
3308 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003309 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003310 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3311 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003312 if ((Ctor->isCopyOrMoveConstructor() ||
3313 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3314 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003315 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003316 LB = NewLB;
3317 return false;
3318}
3319
3320bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003321 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003322 // State consistency checking to ensure correct usage.
3323 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3324 !TestIsLessOp && !TestIsStrictOp);
3325 if (!NewUB)
3326 return true;
3327 UB = NewUB;
3328 TestIsLessOp = LessOp;
3329 TestIsStrictOp = StrictOp;
3330 ConditionSrcRange = SR;
3331 ConditionLoc = SL;
3332 return false;
3333}
3334
3335bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3336 // State consistency checking to ensure correct usage.
3337 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3338 if (!NewStep)
3339 return true;
3340 if (!NewStep->isValueDependent()) {
3341 // Check that the step is integer expression.
3342 SourceLocation StepLoc = NewStep->getLocStart();
3343 ExprResult Val =
3344 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3345 if (Val.isInvalid())
3346 return true;
3347 NewStep = Val.get();
3348
3349 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3350 // If test-expr is of form var relational-op b and relational-op is < or
3351 // <= then incr-expr must cause var to increase on each iteration of the
3352 // loop. If test-expr is of form var relational-op b and relational-op is
3353 // > or >= then incr-expr must cause var to decrease on each iteration of
3354 // the loop.
3355 // If test-expr is of form b relational-op var and relational-op is < or
3356 // <= then incr-expr must cause var to decrease on each iteration of the
3357 // loop. If test-expr is of form b relational-op var and relational-op is
3358 // > or >= then incr-expr must cause var to increase on each iteration of
3359 // the loop.
3360 llvm::APSInt Result;
3361 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3362 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3363 bool IsConstNeg =
3364 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003365 bool IsConstPos =
3366 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003367 bool IsConstZero = IsConstant && !Result.getBoolValue();
3368 if (UB && (IsConstZero ||
3369 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003370 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003371 SemaRef.Diag(NewStep->getExprLoc(),
3372 diag::err_omp_loop_incr_not_compatible)
3373 << Var << TestIsLessOp << NewStep->getSourceRange();
3374 SemaRef.Diag(ConditionLoc,
3375 diag::note_omp_loop_cond_requres_compatible_incr)
3376 << TestIsLessOp << ConditionSrcRange;
3377 return true;
3378 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003379 if (TestIsLessOp == Subtract) {
3380 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3381 NewStep).get();
3382 Subtract = !Subtract;
3383 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003384 }
3385
3386 Step = NewStep;
3387 SubtractStep = Subtract;
3388 return false;
3389}
3390
Alexey Bataev9c821032015-04-30 04:23:23 +00003391bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003392 // Check init-expr for canonical loop form and save loop counter
3393 // variable - #Var and its initialization value - #LB.
3394 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3395 // var = lb
3396 // integer-type var = lb
3397 // random-access-iterator-type var = lb
3398 // pointer-type var = lb
3399 //
3400 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003401 if (EmitDiags) {
3402 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3403 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003404 return true;
3405 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003406 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003407 if (Expr *E = dyn_cast<Expr>(S))
3408 S = E->IgnoreParens();
3409 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3410 if (BO->getOpcode() == BO_Assign)
3411 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003412 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003413 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003414 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3415 if (DS->isSingleDecl()) {
3416 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003417 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003418 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003419 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003420 SemaRef.Diag(S->getLocStart(),
3421 diag::ext_omp_loop_not_canonical_init)
3422 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003423 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003424 }
3425 }
3426 }
3427 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3428 if (CE->getOperator() == OO_Equal)
3429 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003430 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3431 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003432
Alexey Bataev9c821032015-04-30 04:23:23 +00003433 if (EmitDiags) {
3434 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3435 << S->getSourceRange();
3436 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003437 return true;
3438}
3439
Alexey Bataev23b69422014-06-18 07:08:49 +00003440/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003441/// variable (which may be the loop variable) if possible.
3442static const VarDecl *GetInitVarDecl(const Expr *E) {
3443 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003444 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003445 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003446 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3447 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003448 if ((Ctor->isCopyOrMoveConstructor() ||
3449 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3450 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003451 E = CE->getArg(0)->IgnoreParenImpCasts();
3452 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3453 if (!DRE)
3454 return nullptr;
3455 return dyn_cast<VarDecl>(DRE->getDecl());
3456}
3457
3458bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3459 // Check test-expr for canonical form, save upper-bound UB, flags for
3460 // less/greater and for strict/non-strict comparison.
3461 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3462 // var relational-op b
3463 // b relational-op var
3464 //
3465 if (!S) {
3466 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3467 return true;
3468 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003469 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003470 SourceLocation CondLoc = S->getLocStart();
3471 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3472 if (BO->isRelationalOp()) {
3473 if (GetInitVarDecl(BO->getLHS()) == Var)
3474 return SetUB(BO->getRHS(),
3475 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3476 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3477 BO->getSourceRange(), BO->getOperatorLoc());
3478 if (GetInitVarDecl(BO->getRHS()) == Var)
3479 return SetUB(BO->getLHS(),
3480 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3481 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3482 BO->getSourceRange(), BO->getOperatorLoc());
3483 }
3484 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3485 if (CE->getNumArgs() == 2) {
3486 auto Op = CE->getOperator();
3487 switch (Op) {
3488 case OO_Greater:
3489 case OO_GreaterEqual:
3490 case OO_Less:
3491 case OO_LessEqual:
3492 if (GetInitVarDecl(CE->getArg(0)) == Var)
3493 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3494 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3495 CE->getOperatorLoc());
3496 if (GetInitVarDecl(CE->getArg(1)) == Var)
3497 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3498 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3499 CE->getOperatorLoc());
3500 break;
3501 default:
3502 break;
3503 }
3504 }
3505 }
3506 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3507 << S->getSourceRange() << Var;
3508 return true;
3509}
3510
3511bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3512 // RHS of canonical loop form increment can be:
3513 // var + incr
3514 // incr + var
3515 // var - incr
3516 //
3517 RHS = RHS->IgnoreParenImpCasts();
3518 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3519 if (BO->isAdditiveOp()) {
3520 bool IsAdd = BO->getOpcode() == BO_Add;
3521 if (GetInitVarDecl(BO->getLHS()) == Var)
3522 return SetStep(BO->getRHS(), !IsAdd);
3523 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3524 return SetStep(BO->getLHS(), false);
3525 }
3526 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3527 bool IsAdd = CE->getOperator() == OO_Plus;
3528 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3529 if (GetInitVarDecl(CE->getArg(0)) == Var)
3530 return SetStep(CE->getArg(1), !IsAdd);
3531 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3532 return SetStep(CE->getArg(0), false);
3533 }
3534 }
3535 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3536 << RHS->getSourceRange() << Var;
3537 return true;
3538}
3539
3540bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3541 // Check incr-expr for canonical loop form and return true if it
3542 // does not conform.
3543 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3544 // ++var
3545 // var++
3546 // --var
3547 // var--
3548 // var += incr
3549 // var -= incr
3550 // var = var + incr
3551 // var = incr + var
3552 // var = var - incr
3553 //
3554 if (!S) {
3555 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3556 return true;
3557 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003558 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003559 S = S->IgnoreParens();
3560 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3561 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3562 return SetStep(
3563 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3564 (UO->isDecrementOp() ? -1 : 1)).get(),
3565 false);
3566 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3567 switch (BO->getOpcode()) {
3568 case BO_AddAssign:
3569 case BO_SubAssign:
3570 if (GetInitVarDecl(BO->getLHS()) == Var)
3571 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3572 break;
3573 case BO_Assign:
3574 if (GetInitVarDecl(BO->getLHS()) == Var)
3575 return CheckIncRHS(BO->getRHS());
3576 break;
3577 default:
3578 break;
3579 }
3580 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3581 switch (CE->getOperator()) {
3582 case OO_PlusPlus:
3583 case OO_MinusMinus:
3584 if (GetInitVarDecl(CE->getArg(0)) == Var)
3585 return SetStep(
3586 SemaRef.ActOnIntegerConstant(
3587 CE->getLocStart(),
3588 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3589 false);
3590 break;
3591 case OO_PlusEqual:
3592 case OO_MinusEqual:
3593 if (GetInitVarDecl(CE->getArg(0)) == Var)
3594 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3595 break;
3596 case OO_Equal:
3597 if (GetInitVarDecl(CE->getArg(0)) == Var)
3598 return CheckIncRHS(CE->getArg(1));
3599 break;
3600 default:
3601 break;
3602 }
3603 }
3604 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3605 << S->getSourceRange() << Var;
3606 return true;
3607}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003608
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003609namespace {
3610// Transform variables declared in GNU statement expressions to new ones to
3611// avoid crash on codegen.
3612class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3613 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3614
3615public:
3616 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3617
3618 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3619 if (auto *VD = cast<VarDecl>(D))
3620 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3621 !isa<ImplicitParamDecl>(D)) {
3622 auto *NewVD = VarDecl::Create(
3623 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3624 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3625 VD->getTypeSourceInfo(), VD->getStorageClass());
3626 NewVD->setTSCSpec(VD->getTSCSpec());
3627 NewVD->setInit(VD->getInit());
3628 NewVD->setInitStyle(VD->getInitStyle());
3629 NewVD->setExceptionVariable(VD->isExceptionVariable());
3630 NewVD->setNRVOVariable(VD->isNRVOVariable());
3631 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3632 NewVD->setConstexpr(VD->isConstexpr());
3633 NewVD->setInitCapture(VD->isInitCapture());
3634 NewVD->setPreviousDeclInSameBlockScope(
3635 VD->isPreviousDeclInSameBlockScope());
3636 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003637 if (VD->hasAttrs())
3638 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003639 transformedLocalDecl(VD, NewVD);
3640 return NewVD;
3641 }
3642 return BaseTransform::TransformDefinition(Loc, D);
3643 }
3644
3645 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3646 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3647 if (E->getDecl() != NewD) {
3648 NewD->setReferenced();
3649 NewD->markUsed(SemaRef.Context);
3650 return DeclRefExpr::Create(
3651 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3652 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3653 E->getNameInfo(), E->getType(), E->getValueKind());
3654 }
3655 return BaseTransform::TransformDeclRefExpr(E);
3656 }
3657};
3658}
3659
Alexander Musmana5f070a2014-10-01 06:03:56 +00003660/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003661Expr *
3662OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3663 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003664 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003665 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003666 auto VarType = Var->getType().getNonReferenceType();
3667 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003668 SemaRef.getLangOpts().CPlusPlus) {
3669 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003670 auto *UBExpr = TestIsLessOp ? UB : LB;
3671 auto *LBExpr = TestIsLessOp ? LB : UB;
3672 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3673 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3674 if (!Upper || !Lower)
3675 return nullptr;
3676 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3677 Sema::AA_Converting,
3678 /*AllowExplicit=*/true)
3679 .get();
3680 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3681 Sema::AA_Converting,
3682 /*AllowExplicit=*/true)
3683 .get();
3684 if (!Upper || !Lower)
3685 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003686
3687 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3688
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003689 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003690 // BuildBinOp already emitted error, this one is to point user to upper
3691 // and lower bound, and to tell what is passed to 'operator-'.
3692 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3693 << Upper->getSourceRange() << Lower->getSourceRange();
3694 return nullptr;
3695 }
3696 }
3697
3698 if (!Diff.isUsable())
3699 return nullptr;
3700
3701 // Upper - Lower [- 1]
3702 if (TestIsStrictOp)
3703 Diff = SemaRef.BuildBinOp(
3704 S, DefaultLoc, BO_Sub, Diff.get(),
3705 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3706 if (!Diff.isUsable())
3707 return nullptr;
3708
3709 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003710 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3711 if (NewStep.isInvalid())
3712 return nullptr;
3713 NewStep = SemaRef.PerformImplicitConversion(
3714 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3715 /*AllowExplicit=*/true);
3716 if (NewStep.isInvalid())
3717 return nullptr;
3718 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003719 if (!Diff.isUsable())
3720 return nullptr;
3721
3722 // Parentheses (for dumping/debugging purposes only).
3723 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3724 if (!Diff.isUsable())
3725 return nullptr;
3726
3727 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003728 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3729 if (NewStep.isInvalid())
3730 return nullptr;
3731 NewStep = SemaRef.PerformImplicitConversion(
3732 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3733 /*AllowExplicit=*/true);
3734 if (NewStep.isInvalid())
3735 return nullptr;
3736 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003737 if (!Diff.isUsable())
3738 return nullptr;
3739
Alexander Musman174b3ca2014-10-06 11:16:29 +00003740 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003741 QualType Type = Diff.get()->getType();
3742 auto &C = SemaRef.Context;
3743 bool UseVarType = VarType->hasIntegerRepresentation() &&
3744 C.getTypeSize(Type) > C.getTypeSize(VarType);
3745 if (!Type->isIntegerType() || UseVarType) {
3746 unsigned NewSize =
3747 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3748 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3749 : Type->hasSignedIntegerRepresentation();
3750 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3751 Diff = SemaRef.PerformImplicitConversion(
3752 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3753 if (!Diff.isUsable())
3754 return nullptr;
3755 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003756 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003757 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3758 if (NewSize != C.getTypeSize(Type)) {
3759 if (NewSize < C.getTypeSize(Type)) {
3760 assert(NewSize == 64 && "incorrect loop var size");
3761 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3762 << InitSrcRange << ConditionSrcRange;
3763 }
3764 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003765 NewSize, Type->hasSignedIntegerRepresentation() ||
3766 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003767 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3768 Sema::AA_Converting, true);
3769 if (!Diff.isUsable())
3770 return nullptr;
3771 }
3772 }
3773
Alexander Musmana5f070a2014-10-01 06:03:56 +00003774 return Diff.get();
3775}
3776
Alexey Bataev62dbb972015-04-22 11:59:37 +00003777Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3778 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3779 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3780 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003781 TransformToNewDefs Transform(SemaRef);
3782
3783 auto NewLB = Transform.TransformExpr(LB);
3784 auto NewUB = Transform.TransformExpr(UB);
3785 if (NewLB.isInvalid() || NewUB.isInvalid())
3786 return Cond;
3787 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3788 Sema::AA_Converting,
3789 /*AllowExplicit=*/true);
3790 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3791 Sema::AA_Converting,
3792 /*AllowExplicit=*/true);
3793 if (NewLB.isInvalid() || NewUB.isInvalid())
3794 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003795 auto CondExpr = SemaRef.BuildBinOp(
3796 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3797 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003798 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003799 if (CondExpr.isUsable()) {
3800 CondExpr = SemaRef.PerformImplicitConversion(
3801 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3802 /*AllowExplicit=*/true);
3803 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003804 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3805 // Otherwise use original loop conditon and evaluate it in runtime.
3806 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3807}
3808
Alexander Musmana5f070a2014-10-01 06:03:56 +00003809/// \brief Build reference expression to the counter be used for codegen.
3810Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003811 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3812 DefaultLoc);
3813}
3814
3815Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3816 if (Var && !Var->isInvalidDecl()) {
3817 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003818 auto *PrivateVar =
3819 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3820 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003821 if (PrivateVar->isInvalidDecl())
3822 return nullptr;
3823 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3824 }
3825 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003826}
3827
3828/// \brief Build initization of the counter be used for codegen.
3829Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3830
3831/// \brief Build step of the counter be used for codegen.
3832Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3833
3834/// \brief Iteration space of a single for loop.
3835struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003836 /// \brief Condition of the loop.
3837 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003838 /// \brief This expression calculates the number of iterations in the loop.
3839 /// It is always possible to calculate it before starting the loop.
3840 Expr *NumIterations;
3841 /// \brief The loop counter variable.
3842 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003843 /// \brief Private loop counter variable.
3844 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003845 /// \brief This is initializer for the initial value of #CounterVar.
3846 Expr *CounterInit;
3847 /// \brief This is step for the #CounterVar used to generate its update:
3848 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3849 Expr *CounterStep;
3850 /// \brief Should step be subtracted?
3851 bool Subtract;
3852 /// \brief Source range of the loop init.
3853 SourceRange InitSrcRange;
3854 /// \brief Source range of the loop condition.
3855 SourceRange CondSrcRange;
3856 /// \brief Source range of the loop increment.
3857 SourceRange IncSrcRange;
3858};
3859
Alexey Bataev23b69422014-06-18 07:08:49 +00003860} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003861
Alexey Bataev9c821032015-04-30 04:23:23 +00003862void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3863 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3864 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003865 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3866 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003867 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3868 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003869 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003870 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003871 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003872 }
3873}
3874
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003875/// \brief Called on a for stmt to check and extract its iteration space
3876/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003877static bool CheckOpenMPIterationSpace(
3878 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3879 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003880 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003881 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003882 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 // OpenMP [2.6, Canonical Loop Form]
3884 // for (init-expr; test-expr; incr-expr) structured-block
3885 auto For = dyn_cast_or_null<ForStmt>(S);
3886 if (!For) {
3887 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003888 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3889 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3890 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3891 if (NestedLoopCount > 1) {
3892 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3893 SemaRef.Diag(DSA.getConstructLoc(),
3894 diag::note_omp_collapse_ordered_expr)
3895 << 2 << CollapseLoopCountExpr->getSourceRange()
3896 << OrderedLoopCountExpr->getSourceRange();
3897 else if (CollapseLoopCountExpr)
3898 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3899 diag::note_omp_collapse_ordered_expr)
3900 << 0 << CollapseLoopCountExpr->getSourceRange();
3901 else
3902 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3903 diag::note_omp_collapse_ordered_expr)
3904 << 1 << OrderedLoopCountExpr->getSourceRange();
3905 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906 return true;
3907 }
3908 assert(For->getBody());
3909
3910 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3911
3912 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003913 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003914 if (ISC.CheckInit(Init)) {
3915 return true;
3916 }
3917
3918 bool HasErrors = false;
3919
3920 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003921 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003922
3923 // OpenMP [2.6, Canonical Loop Form]
3924 // Var is one of the following:
3925 // A variable of signed or unsigned integer type.
3926 // For C++, a variable of a random access iterator type.
3927 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003928 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003929 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3930 !VarType->isPointerType() &&
3931 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3932 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3933 << SemaRef.getLangOpts().CPlusPlus;
3934 HasErrors = true;
3935 }
3936
Alexey Bataev4acb8592014-07-07 13:01:15 +00003937 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3938 // Construct
3939 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3940 // parallel for construct is (are) private.
3941 // The loop iteration variable in the associated for-loop of a simd construct
3942 // with just one associated for-loop is linear with a constant-linear-step
3943 // that is the increment of the associated for-loop.
3944 // Exclude loop var from the list of variables with implicitly defined data
3945 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003946 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003947
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003948 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3949 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003950 // The loop iteration variable in the associated for-loop of a simd construct
3951 // with just one associated for-loop may be listed in a linear clause with a
3952 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003953 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3954 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003955 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003956 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3957 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3958 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003959 auto PredeterminedCKind =
3960 isOpenMPSimdDirective(DKind)
3961 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3962 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003963 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003964 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003965 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003966 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003967 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003968 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3969 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003970 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003971 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3972 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003973 if (DVar.RefExpr == nullptr)
3974 DVar.CKind = PredeterminedCKind;
3975 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003976 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003977 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003978 // Make the loop iteration variable private (for worksharing constructs),
3979 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003980 // lastprivate (for simd directives with several collapsed or ordered
3981 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003982 if (DVar.CKind == OMPC_unknown)
3983 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3984 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003985 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003986 }
3987
Alexey Bataev7ff55242014-06-19 09:13:45 +00003988 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003989
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003990 // Check test-expr.
3991 HasErrors |= ISC.CheckCond(For->getCond());
3992
3993 // Check incr-expr.
3994 HasErrors |= ISC.CheckInc(For->getInc());
3995
Alexander Musmana5f070a2014-10-01 06:03:56 +00003996 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003997 return HasErrors;
3998
Alexander Musmana5f070a2014-10-01 06:03:56 +00003999 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004000 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00004001 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004002 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004003 isOpenMPTaskLoopDirective(DKind) ||
4004 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004005 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00004006 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004007 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4008 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4009 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4010 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4011 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4012 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4013
Alexey Bataev62dbb972015-04-22 11:59:37 +00004014 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4015 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004016 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004017 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004018 ResultIterSpace.CounterInit == nullptr ||
4019 ResultIterSpace.CounterStep == nullptr);
4020
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004021 return HasErrors;
4022}
4023
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004024/// \brief Build 'VarRef = Start.
4025static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4026 ExprResult VarRef, ExprResult Start) {
4027 TransformToNewDefs Transform(SemaRef);
4028 // Build 'VarRef = Start.
4029 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4030 if (NewStart.isInvalid())
4031 return ExprError();
4032 NewStart = SemaRef.PerformImplicitConversion(
4033 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4034 Sema::AA_Converting,
4035 /*AllowExplicit=*/true);
4036 if (NewStart.isInvalid())
4037 return ExprError();
4038 NewStart = SemaRef.PerformImplicitConversion(
4039 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4040 /*AllowExplicit=*/true);
4041 if (!NewStart.isUsable())
4042 return ExprError();
4043
4044 auto Init =
4045 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4046 return Init;
4047}
4048
Alexander Musmana5f070a2014-10-01 06:03:56 +00004049/// \brief Build 'VarRef = Start + Iter * Step'.
4050static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4051 SourceLocation Loc, ExprResult VarRef,
4052 ExprResult Start, ExprResult Iter,
4053 ExprResult Step, bool Subtract) {
4054 // Add parentheses (for debugging purposes only).
4055 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4056 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4057 !Step.isUsable())
4058 return ExprError();
4059
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004060 TransformToNewDefs Transform(SemaRef);
4061 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
4062 if (NewStep.isInvalid())
4063 return ExprError();
4064 NewStep = SemaRef.PerformImplicitConversion(
4065 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
4066 Sema::AA_Converting,
4067 /*AllowExplicit=*/true);
4068 if (NewStep.isInvalid())
4069 return ExprError();
4070 ExprResult Update =
4071 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004072 if (!Update.isUsable())
4073 return ExprError();
4074
Alexey Bataevc0214e02016-02-16 12:13:49 +00004075 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4076 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004077 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4078 if (NewStart.isInvalid())
4079 return ExprError();
4080 NewStart = SemaRef.PerformImplicitConversion(
4081 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4082 Sema::AA_Converting,
4083 /*AllowExplicit=*/true);
4084 if (NewStart.isInvalid())
4085 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004086
Alexey Bataevc0214e02016-02-16 12:13:49 +00004087 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4088 ExprResult SavedUpdate = Update;
4089 ExprResult UpdateVal;
4090 if (VarRef.get()->getType()->isOverloadableType() ||
4091 NewStart.get()->getType()->isOverloadableType() ||
4092 Update.get()->getType()->isOverloadableType()) {
4093 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4094 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4095 Update =
4096 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4097 if (Update.isUsable()) {
4098 UpdateVal =
4099 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4100 VarRef.get(), SavedUpdate.get());
4101 if (UpdateVal.isUsable()) {
4102 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4103 UpdateVal.get());
4104 }
4105 }
4106 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4107 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004108
Alexey Bataevc0214e02016-02-16 12:13:49 +00004109 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4110 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4111 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4112 NewStart.get(), SavedUpdate.get());
4113 if (!Update.isUsable())
4114 return ExprError();
4115
4116 Update = SemaRef.PerformImplicitConversion(
4117 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4118 if (!Update.isUsable())
4119 return ExprError();
4120
4121 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4122 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004123 return Update;
4124}
4125
4126/// \brief Convert integer expression \a E to make it have at least \a Bits
4127/// bits.
4128static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4129 Sema &SemaRef) {
4130 if (E == nullptr)
4131 return ExprError();
4132 auto &C = SemaRef.Context;
4133 QualType OldType = E->getType();
4134 unsigned HasBits = C.getTypeSize(OldType);
4135 if (HasBits >= Bits)
4136 return ExprResult(E);
4137 // OK to convert to signed, because new type has more bits than old.
4138 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4139 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4140 true);
4141}
4142
4143/// \brief Check if the given expression \a E is a constant integer that fits
4144/// into \a Bits bits.
4145static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4146 if (E == nullptr)
4147 return false;
4148 llvm::APSInt Result;
4149 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4150 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4151 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004152}
4153
4154/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004155/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4156/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004157static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004158CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4159 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4160 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004161 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004162 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004163 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004164 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004165 // Found 'collapse' clause - calculate collapse number.
4166 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004167 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004168 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004169 }
4170 if (OrderedLoopCountExpr) {
4171 // Found 'ordered' clause - calculate collapse number.
4172 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004173 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4174 if (Result.getLimitedValue() < NestedLoopCount) {
4175 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4176 diag::err_omp_wrong_ordered_loop_count)
4177 << OrderedLoopCountExpr->getSourceRange();
4178 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4179 diag::note_collapse_loop_count)
4180 << CollapseLoopCountExpr->getSourceRange();
4181 }
4182 NestedLoopCount = Result.getLimitedValue();
4183 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004184 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004185 // This is helper routine for loop directives (e.g., 'for', 'simd',
4186 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004187 SmallVector<LoopIterationSpace, 4> IterSpaces;
4188 IterSpaces.resize(NestedLoopCount);
4189 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004190 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004191 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004192 NestedLoopCount, CollapseLoopCountExpr,
4193 OrderedLoopCountExpr, VarsWithImplicitDSA,
4194 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004195 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004196 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004197 // OpenMP [2.8.1, simd construct, Restrictions]
4198 // All loops associated with the construct must be perfectly nested; that
4199 // is, there must be no intervening code nor any OpenMP directive between
4200 // any two loops.
4201 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004202 }
4203
Alexander Musmana5f070a2014-10-01 06:03:56 +00004204 Built.clear(/* size */ NestedLoopCount);
4205
4206 if (SemaRef.CurContext->isDependentContext())
4207 return NestedLoopCount;
4208
4209 // An example of what is generated for the following code:
4210 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004211 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004212 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004213 // for (k = 0; k < NK; ++k)
4214 // for (j = J0; j < NJ; j+=2) {
4215 // <loop body>
4216 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004217 //
4218 // We generate the code below.
4219 // Note: the loop body may be outlined in CodeGen.
4220 // Note: some counters may be C++ classes, operator- is used to find number of
4221 // iterations and operator+= to calculate counter value.
4222 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4223 // or i64 is currently supported).
4224 //
4225 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4226 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4227 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4228 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4229 // // similar updates for vars in clauses (e.g. 'linear')
4230 // <loop body (using local i and j)>
4231 // }
4232 // i = NI; // assign final values of counters
4233 // j = NJ;
4234 //
4235
4236 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4237 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004238 // Precondition tests if there is at least one iteration (all conditions are
4239 // true).
4240 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004241 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004242 ExprResult LastIteration32 = WidenIterationCount(
4243 32 /* Bits */, SemaRef.PerformImplicitConversion(
4244 N0->IgnoreImpCasts(), N0->getType(),
4245 Sema::AA_Converting, /*AllowExplicit=*/true)
4246 .get(),
4247 SemaRef);
4248 ExprResult LastIteration64 = WidenIterationCount(
4249 64 /* Bits */, SemaRef.PerformImplicitConversion(
4250 N0->IgnoreImpCasts(), N0->getType(),
4251 Sema::AA_Converting, /*AllowExplicit=*/true)
4252 .get(),
4253 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004254
4255 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4256 return NestedLoopCount;
4257
4258 auto &C = SemaRef.Context;
4259 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4260
4261 Scope *CurScope = DSA.getCurScope();
4262 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004263 if (PreCond.isUsable()) {
4264 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4265 PreCond.get(), IterSpaces[Cnt].PreCond);
4266 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004267 auto N = IterSpaces[Cnt].NumIterations;
4268 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4269 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004270 LastIteration32 = SemaRef.BuildBinOp(
4271 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4272 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4273 Sema::AA_Converting,
4274 /*AllowExplicit=*/true)
4275 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004276 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004277 LastIteration64 = SemaRef.BuildBinOp(
4278 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4279 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4280 Sema::AA_Converting,
4281 /*AllowExplicit=*/true)
4282 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004283 }
4284
4285 // Choose either the 32-bit or 64-bit version.
4286 ExprResult LastIteration = LastIteration64;
4287 if (LastIteration32.isUsable() &&
4288 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4289 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4290 FitsInto(
4291 32 /* Bits */,
4292 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4293 LastIteration64.get(), SemaRef)))
4294 LastIteration = LastIteration32;
4295
4296 if (!LastIteration.isUsable())
4297 return 0;
4298
4299 // Save the number of iterations.
4300 ExprResult NumIterations = LastIteration;
4301 {
4302 LastIteration = SemaRef.BuildBinOp(
4303 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4304 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4305 if (!LastIteration.isUsable())
4306 return 0;
4307 }
4308
4309 // Calculate the last iteration number beforehand instead of doing this on
4310 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4311 llvm::APSInt Result;
4312 bool IsConstant =
4313 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4314 ExprResult CalcLastIteration;
4315 if (!IsConstant) {
4316 SourceLocation SaveLoc;
4317 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004318 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004319 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004320 ExprResult SaveRef = buildDeclRefExpr(
4321 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004322 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4323 SaveRef.get(), LastIteration.get());
4324 LastIteration = SaveRef;
4325
4326 // Prepare SaveRef + 1.
4327 NumIterations = SemaRef.BuildBinOp(
4328 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4329 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4330 if (!NumIterations.isUsable())
4331 return 0;
4332 }
4333
4334 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4335
Alexander Musmanc6388682014-12-15 07:07:06 +00004336 QualType VType = LastIteration.get()->getType();
4337 // Build variables passed into runtime, nesessary for worksharing directives.
4338 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004339 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4340 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004341 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004342 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4343 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004344 SemaRef.AddInitializerToDecl(
4345 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4346 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4347
4348 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004349 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4350 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004351 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4352 /*DirectInit*/ false,
4353 /*TypeMayContainAuto*/ false);
4354
4355 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4356 // This will be used to implement clause 'lastprivate'.
4357 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004358 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4359 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004360 SemaRef.AddInitializerToDecl(
4361 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4362 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4363
4364 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004365 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4366 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004367 SemaRef.AddInitializerToDecl(
4368 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4369 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4370
4371 // Build expression: UB = min(UB, LastIteration)
4372 // It is nesessary for CodeGen of directives with static scheduling.
4373 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4374 UB.get(), LastIteration.get());
4375 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4376 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4377 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4378 CondOp.get());
4379 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4380 }
4381
4382 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004383 ExprResult IV;
4384 ExprResult Init;
4385 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004386 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4387 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004388 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004389 isOpenMPTaskLoopDirective(DKind) ||
4390 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004391 ? LB.get()
4392 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4393 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4394 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004395 }
4396
Alexander Musmanc6388682014-12-15 07:07:06 +00004397 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004398 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004399 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004400 (isOpenMPWorksharingDirective(DKind) ||
4401 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004402 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4403 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4404 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004405
4406 // Loop increment (IV = IV + 1)
4407 SourceLocation IncLoc;
4408 ExprResult Inc =
4409 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4410 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4411 if (!Inc.isUsable())
4412 return 0;
4413 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004414 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4415 if (!Inc.isUsable())
4416 return 0;
4417
4418 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4419 // Used for directives with static scheduling.
4420 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004421 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4422 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004423 // LB + ST
4424 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4425 if (!NextLB.isUsable())
4426 return 0;
4427 // LB = LB + ST
4428 NextLB =
4429 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4430 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4431 if (!NextLB.isUsable())
4432 return 0;
4433 // UB + ST
4434 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4435 if (!NextUB.isUsable())
4436 return 0;
4437 // UB = UB + ST
4438 NextUB =
4439 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4440 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4441 if (!NextUB.isUsable())
4442 return 0;
4443 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004444
4445 // Build updates and final values of the loop counters.
4446 bool HasErrors = false;
4447 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004448 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004449 Built.Updates.resize(NestedLoopCount);
4450 Built.Finals.resize(NestedLoopCount);
4451 {
4452 ExprResult Div;
4453 // Go from inner nested loop to outer.
4454 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4455 LoopIterationSpace &IS = IterSpaces[Cnt];
4456 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4457 // Build: Iter = (IV / Div) % IS.NumIters
4458 // where Div is product of previous iterations' IS.NumIters.
4459 ExprResult Iter;
4460 if (Div.isUsable()) {
4461 Iter =
4462 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4463 } else {
4464 Iter = IV;
4465 assert((Cnt == (int)NestedLoopCount - 1) &&
4466 "unusable div expected on first iteration only");
4467 }
4468
4469 if (Cnt != 0 && Iter.isUsable())
4470 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4471 IS.NumIterations);
4472 if (!Iter.isUsable()) {
4473 HasErrors = true;
4474 break;
4475 }
4476
Alexey Bataev39f915b82015-05-08 10:41:21 +00004477 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4478 auto *CounterVar = buildDeclRefExpr(
4479 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4480 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4481 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004482 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4483 IS.CounterInit);
4484 if (!Init.isUsable()) {
4485 HasErrors = true;
4486 break;
4487 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004488 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004489 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004490 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4491 if (!Update.isUsable()) {
4492 HasErrors = true;
4493 break;
4494 }
4495
4496 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4497 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004498 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004499 IS.NumIterations, IS.CounterStep, IS.Subtract);
4500 if (!Final.isUsable()) {
4501 HasErrors = true;
4502 break;
4503 }
4504
4505 // Build Div for the next iteration: Div <- Div * IS.NumIters
4506 if (Cnt != 0) {
4507 if (Div.isUnset())
4508 Div = IS.NumIterations;
4509 else
4510 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4511 IS.NumIterations);
4512
4513 // Add parentheses (for debugging purposes only).
4514 if (Div.isUsable())
4515 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4516 if (!Div.isUsable()) {
4517 HasErrors = true;
4518 break;
4519 }
4520 }
4521 if (!Update.isUsable() || !Final.isUsable()) {
4522 HasErrors = true;
4523 break;
4524 }
4525 // Save results
4526 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004527 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004528 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004529 Built.Updates[Cnt] = Update.get();
4530 Built.Finals[Cnt] = Final.get();
4531 }
4532 }
4533
4534 if (HasErrors)
4535 return 0;
4536
4537 // Save results
4538 Built.IterationVarRef = IV.get();
4539 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004540 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004541 Built.CalcLastIteration =
4542 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004543 Built.PreCond = PreCond.get();
4544 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004545 Built.Init = Init.get();
4546 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004547 Built.LB = LB.get();
4548 Built.UB = UB.get();
4549 Built.IL = IL.get();
4550 Built.ST = ST.get();
4551 Built.EUB = EUB.get();
4552 Built.NLB = NextLB.get();
4553 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004554
Alexey Bataevabfc0692014-06-25 06:52:00 +00004555 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004556}
4557
Alexey Bataev10e775f2015-07-30 11:36:16 +00004558static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004559 auto CollapseClauses =
4560 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4561 if (CollapseClauses.begin() != CollapseClauses.end())
4562 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004563 return nullptr;
4564}
4565
Alexey Bataev10e775f2015-07-30 11:36:16 +00004566static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004567 auto OrderedClauses =
4568 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4569 if (OrderedClauses.begin() != OrderedClauses.end())
4570 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004571 return nullptr;
4572}
4573
Alexey Bataev66b15b52015-08-21 11:14:16 +00004574static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4575 const Expr *Safelen) {
4576 llvm::APSInt SimdlenRes, SafelenRes;
4577 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4578 Simdlen->isInstantiationDependent() ||
4579 Simdlen->containsUnexpandedParameterPack())
4580 return false;
4581 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4582 Safelen->isInstantiationDependent() ||
4583 Safelen->containsUnexpandedParameterPack())
4584 return false;
4585 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4586 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4587 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4588 // If both simdlen and safelen clauses are specified, the value of the simdlen
4589 // parameter must be less than or equal to the value of the safelen parameter.
4590 if (SimdlenRes > SafelenRes) {
4591 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4592 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4593 return true;
4594 }
4595 return false;
4596}
4597
Alexey Bataev4acb8592014-07-07 13:01:15 +00004598StmtResult Sema::ActOnOpenMPSimdDirective(
4599 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4600 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004601 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004602 if (!AStmt)
4603 return StmtError();
4604
4605 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004606 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004607 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4608 // define the nested loops number.
4609 unsigned NestedLoopCount = CheckOpenMPLoop(
4610 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4611 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004612 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004613 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004614
Alexander Musmana5f070a2014-10-01 06:03:56 +00004615 assert((CurContext->isDependentContext() || B.builtAll()) &&
4616 "omp simd loop exprs were not built");
4617
Alexander Musman3276a272015-03-21 10:12:56 +00004618 if (!CurContext->isDependentContext()) {
4619 // Finalize the clauses that need pre-built expressions for CodeGen.
4620 for (auto C : Clauses) {
4621 if (auto LC = dyn_cast<OMPLinearClause>(C))
4622 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4623 B.NumIterations, *this, CurScope))
4624 return StmtError();
4625 }
4626 }
4627
Alexey Bataev66b15b52015-08-21 11:14:16 +00004628 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4629 // If both simdlen and safelen clauses are specified, the value of the simdlen
4630 // parameter must be less than or equal to the value of the safelen parameter.
4631 OMPSafelenClause *Safelen = nullptr;
4632 OMPSimdlenClause *Simdlen = nullptr;
4633 for (auto *Clause : Clauses) {
4634 if (Clause->getClauseKind() == OMPC_safelen)
4635 Safelen = cast<OMPSafelenClause>(Clause);
4636 else if (Clause->getClauseKind() == OMPC_simdlen)
4637 Simdlen = cast<OMPSimdlenClause>(Clause);
4638 if (Safelen && Simdlen)
4639 break;
4640 }
4641 if (Simdlen && Safelen &&
4642 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4643 Safelen->getSafelen()))
4644 return StmtError();
4645
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004646 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004647 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4648 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004649}
4650
Alexey Bataev4acb8592014-07-07 13:01:15 +00004651StmtResult Sema::ActOnOpenMPForDirective(
4652 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4653 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004654 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004655 if (!AStmt)
4656 return StmtError();
4657
4658 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004659 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004660 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4661 // define the nested loops number.
4662 unsigned NestedLoopCount = CheckOpenMPLoop(
4663 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4664 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004665 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004666 return StmtError();
4667
Alexander Musmana5f070a2014-10-01 06:03:56 +00004668 assert((CurContext->isDependentContext() || B.builtAll()) &&
4669 "omp for loop exprs were not built");
4670
Alexey Bataev54acd402015-08-04 11:18:19 +00004671 if (!CurContext->isDependentContext()) {
4672 // Finalize the clauses that need pre-built expressions for CodeGen.
4673 for (auto C : Clauses) {
4674 if (auto LC = dyn_cast<OMPLinearClause>(C))
4675 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4676 B.NumIterations, *this, CurScope))
4677 return StmtError();
4678 }
4679 }
4680
Alexey Bataevf29276e2014-06-18 04:14:57 +00004681 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004682 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004683 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004684}
4685
Alexander Musmanf82886e2014-09-18 05:12:34 +00004686StmtResult Sema::ActOnOpenMPForSimdDirective(
4687 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4688 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004689 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004690 if (!AStmt)
4691 return StmtError();
4692
4693 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004694 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004695 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4696 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004697 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004698 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4699 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4700 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004701 if (NestedLoopCount == 0)
4702 return StmtError();
4703
Alexander Musmanc6388682014-12-15 07:07:06 +00004704 assert((CurContext->isDependentContext() || B.builtAll()) &&
4705 "omp for simd loop exprs were not built");
4706
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004707 if (!CurContext->isDependentContext()) {
4708 // Finalize the clauses that need pre-built expressions for CodeGen.
4709 for (auto C : Clauses) {
4710 if (auto LC = dyn_cast<OMPLinearClause>(C))
4711 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4712 B.NumIterations, *this, CurScope))
4713 return StmtError();
4714 }
4715 }
4716
Alexey Bataev66b15b52015-08-21 11:14:16 +00004717 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4718 // If both simdlen and safelen clauses are specified, the value of the simdlen
4719 // parameter must be less than or equal to the value of the safelen parameter.
4720 OMPSafelenClause *Safelen = nullptr;
4721 OMPSimdlenClause *Simdlen = nullptr;
4722 for (auto *Clause : Clauses) {
4723 if (Clause->getClauseKind() == OMPC_safelen)
4724 Safelen = cast<OMPSafelenClause>(Clause);
4725 else if (Clause->getClauseKind() == OMPC_simdlen)
4726 Simdlen = cast<OMPSimdlenClause>(Clause);
4727 if (Safelen && Simdlen)
4728 break;
4729 }
4730 if (Simdlen && Safelen &&
4731 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4732 Safelen->getSafelen()))
4733 return StmtError();
4734
Alexander Musmanf82886e2014-09-18 05:12:34 +00004735 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004736 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4737 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004738}
4739
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004740StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4741 Stmt *AStmt,
4742 SourceLocation StartLoc,
4743 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004744 if (!AStmt)
4745 return StmtError();
4746
4747 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004748 auto BaseStmt = AStmt;
4749 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4750 BaseStmt = CS->getCapturedStmt();
4751 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4752 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004753 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004754 return StmtError();
4755 // All associated statements must be '#pragma omp section' except for
4756 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004757 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004758 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4759 if (SectionStmt)
4760 Diag(SectionStmt->getLocStart(),
4761 diag::err_omp_sections_substmt_not_section);
4762 return StmtError();
4763 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004764 cast<OMPSectionDirective>(SectionStmt)
4765 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004766 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004767 } else {
4768 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4769 return StmtError();
4770 }
4771
4772 getCurFunction()->setHasBranchProtectedScope();
4773
Alexey Bataev25e5b442015-09-15 12:52:43 +00004774 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4775 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004776}
4777
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004778StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4779 SourceLocation StartLoc,
4780 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004781 if (!AStmt)
4782 return StmtError();
4783
4784 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004785
4786 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004787 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004788
Alexey Bataev25e5b442015-09-15 12:52:43 +00004789 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4790 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004791}
4792
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004793StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4794 Stmt *AStmt,
4795 SourceLocation StartLoc,
4796 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004797 if (!AStmt)
4798 return StmtError();
4799
4800 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004801
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004802 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004803
Alexey Bataev3255bf32015-01-19 05:20:46 +00004804 // OpenMP [2.7.3, single Construct, Restrictions]
4805 // The copyprivate clause must not be used with the nowait clause.
4806 OMPClause *Nowait = nullptr;
4807 OMPClause *Copyprivate = nullptr;
4808 for (auto *Clause : Clauses) {
4809 if (Clause->getClauseKind() == OMPC_nowait)
4810 Nowait = Clause;
4811 else if (Clause->getClauseKind() == OMPC_copyprivate)
4812 Copyprivate = Clause;
4813 if (Copyprivate && Nowait) {
4814 Diag(Copyprivate->getLocStart(),
4815 diag::err_omp_single_copyprivate_with_nowait);
4816 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4817 return StmtError();
4818 }
4819 }
4820
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004821 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4822}
4823
Alexander Musman80c22892014-07-17 08:54:58 +00004824StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4825 SourceLocation StartLoc,
4826 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004827 if (!AStmt)
4828 return StmtError();
4829
4830 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004831
4832 getCurFunction()->setHasBranchProtectedScope();
4833
4834 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4835}
4836
Alexey Bataev28c75412015-12-15 08:19:24 +00004837StmtResult Sema::ActOnOpenMPCriticalDirective(
4838 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4839 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004840 if (!AStmt)
4841 return StmtError();
4842
4843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004844
Alexey Bataev28c75412015-12-15 08:19:24 +00004845 bool ErrorFound = false;
4846 llvm::APSInt Hint;
4847 SourceLocation HintLoc;
4848 bool DependentHint = false;
4849 for (auto *C : Clauses) {
4850 if (C->getClauseKind() == OMPC_hint) {
4851 if (!DirName.getName()) {
4852 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4853 ErrorFound = true;
4854 }
4855 Expr *E = cast<OMPHintClause>(C)->getHint();
4856 if (E->isTypeDependent() || E->isValueDependent() ||
4857 E->isInstantiationDependent())
4858 DependentHint = true;
4859 else {
4860 Hint = E->EvaluateKnownConstInt(Context);
4861 HintLoc = C->getLocStart();
4862 }
4863 }
4864 }
4865 if (ErrorFound)
4866 return StmtError();
4867 auto Pair = DSAStack->getCriticalWithHint(DirName);
4868 if (Pair.first && DirName.getName() && !DependentHint) {
4869 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4870 Diag(StartLoc, diag::err_omp_critical_with_hint);
4871 if (HintLoc.isValid()) {
4872 Diag(HintLoc, diag::note_omp_critical_hint_here)
4873 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4874 } else
4875 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4876 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4877 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4878 << 1
4879 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4880 /*Radix=*/10, /*Signed=*/false);
4881 } else
4882 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4883 }
4884 }
4885
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004886 getCurFunction()->setHasBranchProtectedScope();
4887
Alexey Bataev28c75412015-12-15 08:19:24 +00004888 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4889 Clauses, AStmt);
4890 if (!Pair.first && DirName.getName() && !DependentHint)
4891 DSAStack->addCriticalWithHint(Dir, Hint);
4892 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004893}
4894
Alexey Bataev4acb8592014-07-07 13:01:15 +00004895StmtResult Sema::ActOnOpenMPParallelForDirective(
4896 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4897 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004898 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004899 if (!AStmt)
4900 return StmtError();
4901
Alexey Bataev4acb8592014-07-07 13:01:15 +00004902 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4903 // 1.2.2 OpenMP Language Terminology
4904 // Structured block - An executable statement with a single entry at the
4905 // top and a single exit at the bottom.
4906 // The point of exit cannot be a branch out of the structured block.
4907 // longjmp() and throw() must not violate the entry/exit criteria.
4908 CS->getCapturedDecl()->setNothrow();
4909
Alexander Musmanc6388682014-12-15 07:07:06 +00004910 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004911 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4912 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004913 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004914 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4915 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4916 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004917 if (NestedLoopCount == 0)
4918 return StmtError();
4919
Alexander Musmana5f070a2014-10-01 06:03:56 +00004920 assert((CurContext->isDependentContext() || B.builtAll()) &&
4921 "omp parallel for loop exprs were not built");
4922
Alexey Bataev54acd402015-08-04 11:18:19 +00004923 if (!CurContext->isDependentContext()) {
4924 // Finalize the clauses that need pre-built expressions for CodeGen.
4925 for (auto C : Clauses) {
4926 if (auto LC = dyn_cast<OMPLinearClause>(C))
4927 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4928 B.NumIterations, *this, CurScope))
4929 return StmtError();
4930 }
4931 }
4932
Alexey Bataev4acb8592014-07-07 13:01:15 +00004933 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004934 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004935 NestedLoopCount, Clauses, AStmt, B,
4936 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004937}
4938
Alexander Musmane4e893b2014-09-23 09:33:00 +00004939StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4940 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4941 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004942 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004943 if (!AStmt)
4944 return StmtError();
4945
Alexander Musmane4e893b2014-09-23 09:33:00 +00004946 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4947 // 1.2.2 OpenMP Language Terminology
4948 // Structured block - An executable statement with a single entry at the
4949 // top and a single exit at the bottom.
4950 // The point of exit cannot be a branch out of the structured block.
4951 // longjmp() and throw() must not violate the entry/exit criteria.
4952 CS->getCapturedDecl()->setNothrow();
4953
Alexander Musmanc6388682014-12-15 07:07:06 +00004954 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004955 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4956 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004957 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004958 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4959 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4960 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004961 if (NestedLoopCount == 0)
4962 return StmtError();
4963
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004964 if (!CurContext->isDependentContext()) {
4965 // Finalize the clauses that need pre-built expressions for CodeGen.
4966 for (auto C : Clauses) {
4967 if (auto LC = dyn_cast<OMPLinearClause>(C))
4968 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4969 B.NumIterations, *this, CurScope))
4970 return StmtError();
4971 }
4972 }
4973
Alexey Bataev66b15b52015-08-21 11:14:16 +00004974 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4975 // If both simdlen and safelen clauses are specified, the value of the simdlen
4976 // parameter must be less than or equal to the value of the safelen parameter.
4977 OMPSafelenClause *Safelen = nullptr;
4978 OMPSimdlenClause *Simdlen = nullptr;
4979 for (auto *Clause : Clauses) {
4980 if (Clause->getClauseKind() == OMPC_safelen)
4981 Safelen = cast<OMPSafelenClause>(Clause);
4982 else if (Clause->getClauseKind() == OMPC_simdlen)
4983 Simdlen = cast<OMPSimdlenClause>(Clause);
4984 if (Safelen && Simdlen)
4985 break;
4986 }
4987 if (Simdlen && Safelen &&
4988 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4989 Safelen->getSafelen()))
4990 return StmtError();
4991
Alexander Musmane4e893b2014-09-23 09:33:00 +00004992 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004993 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004994 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004995}
4996
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004997StmtResult
4998Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4999 Stmt *AStmt, SourceLocation StartLoc,
5000 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005001 if (!AStmt)
5002 return StmtError();
5003
5004 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005005 auto BaseStmt = AStmt;
5006 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5007 BaseStmt = CS->getCapturedStmt();
5008 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5009 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005010 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005011 return StmtError();
5012 // All associated statements must be '#pragma omp section' except for
5013 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005014 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005015 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5016 if (SectionStmt)
5017 Diag(SectionStmt->getLocStart(),
5018 diag::err_omp_parallel_sections_substmt_not_section);
5019 return StmtError();
5020 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005021 cast<OMPSectionDirective>(SectionStmt)
5022 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005023 }
5024 } else {
5025 Diag(AStmt->getLocStart(),
5026 diag::err_omp_parallel_sections_not_compound_stmt);
5027 return StmtError();
5028 }
5029
5030 getCurFunction()->setHasBranchProtectedScope();
5031
Alexey Bataev25e5b442015-09-15 12:52:43 +00005032 return OMPParallelSectionsDirective::Create(
5033 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005034}
5035
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005036StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5037 Stmt *AStmt, SourceLocation StartLoc,
5038 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005039 if (!AStmt)
5040 return StmtError();
5041
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005042 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5043 // 1.2.2 OpenMP Language Terminology
5044 // Structured block - An executable statement with a single entry at the
5045 // top and a single exit at the bottom.
5046 // The point of exit cannot be a branch out of the structured block.
5047 // longjmp() and throw() must not violate the entry/exit criteria.
5048 CS->getCapturedDecl()->setNothrow();
5049
5050 getCurFunction()->setHasBranchProtectedScope();
5051
Alexey Bataev25e5b442015-09-15 12:52:43 +00005052 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5053 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005054}
5055
Alexey Bataev68446b72014-07-18 07:47:19 +00005056StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5057 SourceLocation EndLoc) {
5058 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5059}
5060
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005061StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5062 SourceLocation EndLoc) {
5063 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5064}
5065
Alexey Bataev2df347a2014-07-18 10:17:07 +00005066StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5067 SourceLocation EndLoc) {
5068 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5069}
5070
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005071StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5072 SourceLocation StartLoc,
5073 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005074 if (!AStmt)
5075 return StmtError();
5076
5077 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005078
5079 getCurFunction()->setHasBranchProtectedScope();
5080
5081 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5082}
5083
Alexey Bataev6125da92014-07-21 11:26:11 +00005084StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5085 SourceLocation StartLoc,
5086 SourceLocation EndLoc) {
5087 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5088 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5089}
5090
Alexey Bataev346265e2015-09-25 10:37:12 +00005091StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5092 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005093 SourceLocation StartLoc,
5094 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005095 OMPClause *DependFound = nullptr;
5096 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005097 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005098 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005099 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005100 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005101 for (auto *C : Clauses) {
5102 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5103 DependFound = C;
5104 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5105 if (DependSourceClause) {
5106 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5107 << getOpenMPDirectiveName(OMPD_ordered)
5108 << getOpenMPClauseName(OMPC_depend) << 2;
5109 ErrorFound = true;
5110 } else
5111 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005112 if (DependSinkClause) {
5113 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5114 << 0;
5115 ErrorFound = true;
5116 }
5117 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5118 if (DependSourceClause) {
5119 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5120 << 1;
5121 ErrorFound = true;
5122 }
5123 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005124 }
5125 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005126 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005127 else if (C->getClauseKind() == OMPC_simd)
5128 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005129 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005130 if (!ErrorFound && !SC &&
5131 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005132 // OpenMP [2.8.1,simd Construct, Restrictions]
5133 // An ordered construct with the simd clause is the only OpenMP construct
5134 // that can appear in the simd region.
5135 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005136 ErrorFound = true;
5137 } else if (DependFound && (TC || SC)) {
5138 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5139 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5140 ErrorFound = true;
5141 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5142 Diag(DependFound->getLocStart(),
5143 diag::err_omp_ordered_directive_without_param);
5144 ErrorFound = true;
5145 } else if (TC || Clauses.empty()) {
5146 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5147 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5148 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5149 << (TC != nullptr);
5150 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5151 ErrorFound = true;
5152 }
5153 }
5154 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005155 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005156
5157 if (AStmt) {
5158 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5159
5160 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005161 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005162
5163 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005164}
5165
Alexey Bataev1d160b12015-03-13 12:27:31 +00005166namespace {
5167/// \brief Helper class for checking expression in 'omp atomic [update]'
5168/// construct.
5169class OpenMPAtomicUpdateChecker {
5170 /// \brief Error results for atomic update expressions.
5171 enum ExprAnalysisErrorCode {
5172 /// \brief A statement is not an expression statement.
5173 NotAnExpression,
5174 /// \brief Expression is not builtin binary or unary operation.
5175 NotABinaryOrUnaryExpression,
5176 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5177 NotAnUnaryIncDecExpression,
5178 /// \brief An expression is not of scalar type.
5179 NotAScalarType,
5180 /// \brief A binary operation is not an assignment operation.
5181 NotAnAssignmentOp,
5182 /// \brief RHS part of the binary operation is not a binary expression.
5183 NotABinaryExpression,
5184 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5185 /// expression.
5186 NotABinaryOperator,
5187 /// \brief RHS binary operation does not have reference to the updated LHS
5188 /// part.
5189 NotAnUpdateExpression,
5190 /// \brief No errors is found.
5191 NoError
5192 };
5193 /// \brief Reference to Sema.
5194 Sema &SemaRef;
5195 /// \brief A location for note diagnostics (when error is found).
5196 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005197 /// \brief 'x' lvalue part of the source atomic expression.
5198 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005199 /// \brief 'expr' rvalue part of the source atomic expression.
5200 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005201 /// \brief Helper expression of the form
5202 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5203 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5204 Expr *UpdateExpr;
5205 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5206 /// important for non-associative operations.
5207 bool IsXLHSInRHSPart;
5208 BinaryOperatorKind Op;
5209 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005210 /// \brief true if the source expression is a postfix unary operation, false
5211 /// if it is a prefix unary operation.
5212 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005213
5214public:
5215 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005216 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005217 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005218 /// \brief Check specified statement that it is suitable for 'atomic update'
5219 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005220 /// expression. If DiagId and NoteId == 0, then only check is performed
5221 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005222 /// \param DiagId Diagnostic which should be emitted if error is found.
5223 /// \param NoteId Diagnostic note for the main error message.
5224 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005225 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005226 /// \brief Return the 'x' lvalue part of the source atomic expression.
5227 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005228 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5229 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005230 /// \brief Return the update expression used in calculation of the updated
5231 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5232 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5233 Expr *getUpdateExpr() const { return UpdateExpr; }
5234 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5235 /// false otherwise.
5236 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5237
Alexey Bataevb78ca832015-04-01 03:33:17 +00005238 /// \brief true if the source expression is a postfix unary operation, false
5239 /// if it is a prefix unary operation.
5240 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5241
Alexey Bataev1d160b12015-03-13 12:27:31 +00005242private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005243 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5244 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005245};
5246} // namespace
5247
5248bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5249 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5250 ExprAnalysisErrorCode ErrorFound = NoError;
5251 SourceLocation ErrorLoc, NoteLoc;
5252 SourceRange ErrorRange, NoteRange;
5253 // Allowed constructs are:
5254 // x = x binop expr;
5255 // x = expr binop x;
5256 if (AtomicBinOp->getOpcode() == BO_Assign) {
5257 X = AtomicBinOp->getLHS();
5258 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5259 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5260 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5261 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5262 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005263 Op = AtomicInnerBinOp->getOpcode();
5264 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005265 auto *LHS = AtomicInnerBinOp->getLHS();
5266 auto *RHS = AtomicInnerBinOp->getRHS();
5267 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5268 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5269 /*Canonical=*/true);
5270 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5271 /*Canonical=*/true);
5272 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5273 /*Canonical=*/true);
5274 if (XId == LHSId) {
5275 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005276 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005277 } else if (XId == RHSId) {
5278 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005279 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005280 } else {
5281 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5282 ErrorRange = AtomicInnerBinOp->getSourceRange();
5283 NoteLoc = X->getExprLoc();
5284 NoteRange = X->getSourceRange();
5285 ErrorFound = NotAnUpdateExpression;
5286 }
5287 } else {
5288 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5289 ErrorRange = AtomicInnerBinOp->getSourceRange();
5290 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5291 NoteRange = SourceRange(NoteLoc, NoteLoc);
5292 ErrorFound = NotABinaryOperator;
5293 }
5294 } else {
5295 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5296 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5297 ErrorFound = NotABinaryExpression;
5298 }
5299 } else {
5300 ErrorLoc = AtomicBinOp->getExprLoc();
5301 ErrorRange = AtomicBinOp->getSourceRange();
5302 NoteLoc = AtomicBinOp->getOperatorLoc();
5303 NoteRange = SourceRange(NoteLoc, NoteLoc);
5304 ErrorFound = NotAnAssignmentOp;
5305 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005306 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005307 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5308 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5309 return true;
5310 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005311 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005312 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005313}
5314
5315bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5316 unsigned NoteId) {
5317 ExprAnalysisErrorCode ErrorFound = NoError;
5318 SourceLocation ErrorLoc, NoteLoc;
5319 SourceRange ErrorRange, NoteRange;
5320 // Allowed constructs are:
5321 // x++;
5322 // x--;
5323 // ++x;
5324 // --x;
5325 // x binop= expr;
5326 // x = x binop expr;
5327 // x = expr binop x;
5328 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5329 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5330 if (AtomicBody->getType()->isScalarType() ||
5331 AtomicBody->isInstantiationDependent()) {
5332 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5333 AtomicBody->IgnoreParenImpCasts())) {
5334 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005335 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005336 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005337 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005338 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005339 X = AtomicCompAssignOp->getLHS();
5340 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005341 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5342 AtomicBody->IgnoreParenImpCasts())) {
5343 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005344 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5345 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005346 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005347 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5348 // Check for Unary Operation
5349 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005350 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005351 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5352 OpLoc = AtomicUnaryOp->getOperatorLoc();
5353 X = AtomicUnaryOp->getSubExpr();
5354 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5355 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005356 } else {
5357 ErrorFound = NotAnUnaryIncDecExpression;
5358 ErrorLoc = AtomicUnaryOp->getExprLoc();
5359 ErrorRange = AtomicUnaryOp->getSourceRange();
5360 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5361 NoteRange = SourceRange(NoteLoc, NoteLoc);
5362 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005363 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005364 ErrorFound = NotABinaryOrUnaryExpression;
5365 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5366 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5367 }
5368 } else {
5369 ErrorFound = NotAScalarType;
5370 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5371 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5372 }
5373 } else {
5374 ErrorFound = NotAnExpression;
5375 NoteLoc = ErrorLoc = S->getLocStart();
5376 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5377 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005378 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005379 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5380 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5381 return true;
5382 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005383 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005384 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005385 // Build an update expression of form 'OpaqueValueExpr(x) binop
5386 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5387 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5388 auto *OVEX = new (SemaRef.getASTContext())
5389 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5390 auto *OVEExpr = new (SemaRef.getASTContext())
5391 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5392 auto Update =
5393 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5394 IsXLHSInRHSPart ? OVEExpr : OVEX);
5395 if (Update.isInvalid())
5396 return true;
5397 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5398 Sema::AA_Casting);
5399 if (Update.isInvalid())
5400 return true;
5401 UpdateExpr = Update.get();
5402 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005403 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005404}
5405
Alexey Bataev0162e452014-07-22 10:10:35 +00005406StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5407 Stmt *AStmt,
5408 SourceLocation StartLoc,
5409 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005410 if (!AStmt)
5411 return StmtError();
5412
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005413 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005414 // 1.2.2 OpenMP Language Terminology
5415 // Structured block - An executable statement with a single entry at the
5416 // top and a single exit at the bottom.
5417 // The point of exit cannot be a branch out of the structured block.
5418 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005419 OpenMPClauseKind AtomicKind = OMPC_unknown;
5420 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005421 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005422 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005423 C->getClauseKind() == OMPC_update ||
5424 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005425 if (AtomicKind != OMPC_unknown) {
5426 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5427 << SourceRange(C->getLocStart(), C->getLocEnd());
5428 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5429 << getOpenMPClauseName(AtomicKind);
5430 } else {
5431 AtomicKind = C->getClauseKind();
5432 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005433 }
5434 }
5435 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005436
Alexey Bataev459dec02014-07-24 06:46:57 +00005437 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005438 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5439 Body = EWC->getSubExpr();
5440
Alexey Bataev62cec442014-11-18 10:14:22 +00005441 Expr *X = nullptr;
5442 Expr *V = nullptr;
5443 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005444 Expr *UE = nullptr;
5445 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005446 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005447 // OpenMP [2.12.6, atomic Construct]
5448 // In the next expressions:
5449 // * x and v (as applicable) are both l-value expressions with scalar type.
5450 // * During the execution of an atomic region, multiple syntactic
5451 // occurrences of x must designate the same storage location.
5452 // * Neither of v and expr (as applicable) may access the storage location
5453 // designated by x.
5454 // * Neither of x and expr (as applicable) may access the storage location
5455 // designated by v.
5456 // * expr is an expression with scalar type.
5457 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5458 // * binop, binop=, ++, and -- are not overloaded operators.
5459 // * The expression x binop expr must be numerically equivalent to x binop
5460 // (expr). This requirement is satisfied if the operators in expr have
5461 // precedence greater than binop, or by using parentheses around expr or
5462 // subexpressions of expr.
5463 // * The expression expr binop x must be numerically equivalent to (expr)
5464 // binop x. This requirement is satisfied if the operators in expr have
5465 // precedence equal to or greater than binop, or by using parentheses around
5466 // expr or subexpressions of expr.
5467 // * For forms that allow multiple occurrences of x, the number of times
5468 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005469 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005470 enum {
5471 NotAnExpression,
5472 NotAnAssignmentOp,
5473 NotAScalarType,
5474 NotAnLValue,
5475 NoError
5476 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005477 SourceLocation ErrorLoc, NoteLoc;
5478 SourceRange ErrorRange, NoteRange;
5479 // If clause is read:
5480 // v = x;
5481 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5482 auto AtomicBinOp =
5483 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5484 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5485 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5486 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5487 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5488 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5489 if (!X->isLValue() || !V->isLValue()) {
5490 auto NotLValueExpr = X->isLValue() ? V : X;
5491 ErrorFound = NotAnLValue;
5492 ErrorLoc = AtomicBinOp->getExprLoc();
5493 ErrorRange = AtomicBinOp->getSourceRange();
5494 NoteLoc = NotLValueExpr->getExprLoc();
5495 NoteRange = NotLValueExpr->getSourceRange();
5496 }
5497 } else if (!X->isInstantiationDependent() ||
5498 !V->isInstantiationDependent()) {
5499 auto NotScalarExpr =
5500 (X->isInstantiationDependent() || X->getType()->isScalarType())
5501 ? V
5502 : X;
5503 ErrorFound = NotAScalarType;
5504 ErrorLoc = AtomicBinOp->getExprLoc();
5505 ErrorRange = AtomicBinOp->getSourceRange();
5506 NoteLoc = NotScalarExpr->getExprLoc();
5507 NoteRange = NotScalarExpr->getSourceRange();
5508 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005509 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005510 ErrorFound = NotAnAssignmentOp;
5511 ErrorLoc = AtomicBody->getExprLoc();
5512 ErrorRange = AtomicBody->getSourceRange();
5513 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5514 : AtomicBody->getExprLoc();
5515 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5516 : AtomicBody->getSourceRange();
5517 }
5518 } else {
5519 ErrorFound = NotAnExpression;
5520 NoteLoc = ErrorLoc = Body->getLocStart();
5521 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005522 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005523 if (ErrorFound != NoError) {
5524 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5525 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005526 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5527 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005528 return StmtError();
5529 } else if (CurContext->isDependentContext())
5530 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005531 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005532 enum {
5533 NotAnExpression,
5534 NotAnAssignmentOp,
5535 NotAScalarType,
5536 NotAnLValue,
5537 NoError
5538 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005539 SourceLocation ErrorLoc, NoteLoc;
5540 SourceRange ErrorRange, NoteRange;
5541 // If clause is write:
5542 // x = expr;
5543 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5544 auto AtomicBinOp =
5545 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5546 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005547 X = AtomicBinOp->getLHS();
5548 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005549 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5550 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5551 if (!X->isLValue()) {
5552 ErrorFound = NotAnLValue;
5553 ErrorLoc = AtomicBinOp->getExprLoc();
5554 ErrorRange = AtomicBinOp->getSourceRange();
5555 NoteLoc = X->getExprLoc();
5556 NoteRange = X->getSourceRange();
5557 }
5558 } else if (!X->isInstantiationDependent() ||
5559 !E->isInstantiationDependent()) {
5560 auto NotScalarExpr =
5561 (X->isInstantiationDependent() || X->getType()->isScalarType())
5562 ? E
5563 : X;
5564 ErrorFound = NotAScalarType;
5565 ErrorLoc = AtomicBinOp->getExprLoc();
5566 ErrorRange = AtomicBinOp->getSourceRange();
5567 NoteLoc = NotScalarExpr->getExprLoc();
5568 NoteRange = NotScalarExpr->getSourceRange();
5569 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005570 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005571 ErrorFound = NotAnAssignmentOp;
5572 ErrorLoc = AtomicBody->getExprLoc();
5573 ErrorRange = AtomicBody->getSourceRange();
5574 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5575 : AtomicBody->getExprLoc();
5576 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5577 : AtomicBody->getSourceRange();
5578 }
5579 } else {
5580 ErrorFound = NotAnExpression;
5581 NoteLoc = ErrorLoc = Body->getLocStart();
5582 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005583 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005584 if (ErrorFound != NoError) {
5585 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5586 << ErrorRange;
5587 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5588 << NoteRange;
5589 return StmtError();
5590 } else if (CurContext->isDependentContext())
5591 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005592 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005593 // If clause is update:
5594 // x++;
5595 // x--;
5596 // ++x;
5597 // --x;
5598 // x binop= expr;
5599 // x = x binop expr;
5600 // x = expr binop x;
5601 OpenMPAtomicUpdateChecker Checker(*this);
5602 if (Checker.checkStatement(
5603 Body, (AtomicKind == OMPC_update)
5604 ? diag::err_omp_atomic_update_not_expression_statement
5605 : diag::err_omp_atomic_not_expression_statement,
5606 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005607 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005608 if (!CurContext->isDependentContext()) {
5609 E = Checker.getExpr();
5610 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005611 UE = Checker.getUpdateExpr();
5612 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005613 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005614 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005615 enum {
5616 NotAnAssignmentOp,
5617 NotACompoundStatement,
5618 NotTwoSubstatements,
5619 NotASpecificExpression,
5620 NoError
5621 } ErrorFound = NoError;
5622 SourceLocation ErrorLoc, NoteLoc;
5623 SourceRange ErrorRange, NoteRange;
5624 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5625 // If clause is a capture:
5626 // v = x++;
5627 // v = x--;
5628 // v = ++x;
5629 // v = --x;
5630 // v = x binop= expr;
5631 // v = x = x binop expr;
5632 // v = x = expr binop x;
5633 auto *AtomicBinOp =
5634 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5635 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5636 V = AtomicBinOp->getLHS();
5637 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5638 OpenMPAtomicUpdateChecker Checker(*this);
5639 if (Checker.checkStatement(
5640 Body, diag::err_omp_atomic_capture_not_expression_statement,
5641 diag::note_omp_atomic_update))
5642 return StmtError();
5643 E = Checker.getExpr();
5644 X = Checker.getX();
5645 UE = Checker.getUpdateExpr();
5646 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5647 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005648 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005649 ErrorLoc = AtomicBody->getExprLoc();
5650 ErrorRange = AtomicBody->getSourceRange();
5651 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5652 : AtomicBody->getExprLoc();
5653 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5654 : AtomicBody->getSourceRange();
5655 ErrorFound = NotAnAssignmentOp;
5656 }
5657 if (ErrorFound != NoError) {
5658 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5659 << ErrorRange;
5660 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5661 return StmtError();
5662 } else if (CurContext->isDependentContext()) {
5663 UE = V = E = X = nullptr;
5664 }
5665 } else {
5666 // If clause is a capture:
5667 // { v = x; x = expr; }
5668 // { v = x; x++; }
5669 // { v = x; x--; }
5670 // { v = x; ++x; }
5671 // { v = x; --x; }
5672 // { v = x; x binop= expr; }
5673 // { v = x; x = x binop expr; }
5674 // { v = x; x = expr binop x; }
5675 // { x++; v = x; }
5676 // { x--; v = x; }
5677 // { ++x; v = x; }
5678 // { --x; v = x; }
5679 // { x binop= expr; v = x; }
5680 // { x = x binop expr; v = x; }
5681 // { x = expr binop x; v = x; }
5682 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5683 // Check that this is { expr1; expr2; }
5684 if (CS->size() == 2) {
5685 auto *First = CS->body_front();
5686 auto *Second = CS->body_back();
5687 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5688 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5689 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5690 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5691 // Need to find what subexpression is 'v' and what is 'x'.
5692 OpenMPAtomicUpdateChecker Checker(*this);
5693 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5694 BinaryOperator *BinOp = nullptr;
5695 if (IsUpdateExprFound) {
5696 BinOp = dyn_cast<BinaryOperator>(First);
5697 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5698 }
5699 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5700 // { v = x; x++; }
5701 // { v = x; x--; }
5702 // { v = x; ++x; }
5703 // { v = x; --x; }
5704 // { v = x; x binop= expr; }
5705 // { v = x; x = x binop expr; }
5706 // { v = x; x = expr binop x; }
5707 // Check that the first expression has form v = x.
5708 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5709 llvm::FoldingSetNodeID XId, PossibleXId;
5710 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5711 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5712 IsUpdateExprFound = XId == PossibleXId;
5713 if (IsUpdateExprFound) {
5714 V = BinOp->getLHS();
5715 X = Checker.getX();
5716 E = Checker.getExpr();
5717 UE = Checker.getUpdateExpr();
5718 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005719 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005720 }
5721 }
5722 if (!IsUpdateExprFound) {
5723 IsUpdateExprFound = !Checker.checkStatement(First);
5724 BinOp = nullptr;
5725 if (IsUpdateExprFound) {
5726 BinOp = dyn_cast<BinaryOperator>(Second);
5727 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5728 }
5729 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5730 // { x++; v = x; }
5731 // { x--; v = x; }
5732 // { ++x; v = x; }
5733 // { --x; v = x; }
5734 // { x binop= expr; v = x; }
5735 // { x = x binop expr; v = x; }
5736 // { x = expr binop x; v = x; }
5737 // Check that the second expression has form v = x.
5738 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5739 llvm::FoldingSetNodeID XId, PossibleXId;
5740 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5741 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5742 IsUpdateExprFound = XId == PossibleXId;
5743 if (IsUpdateExprFound) {
5744 V = BinOp->getLHS();
5745 X = Checker.getX();
5746 E = Checker.getExpr();
5747 UE = Checker.getUpdateExpr();
5748 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005749 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005750 }
5751 }
5752 }
5753 if (!IsUpdateExprFound) {
5754 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005755 auto *FirstExpr = dyn_cast<Expr>(First);
5756 auto *SecondExpr = dyn_cast<Expr>(Second);
5757 if (!FirstExpr || !SecondExpr ||
5758 !(FirstExpr->isInstantiationDependent() ||
5759 SecondExpr->isInstantiationDependent())) {
5760 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5761 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005762 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005763 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5764 : First->getLocStart();
5765 NoteRange = ErrorRange = FirstBinOp
5766 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005767 : SourceRange(ErrorLoc, ErrorLoc);
5768 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005769 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5770 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5771 ErrorFound = NotAnAssignmentOp;
5772 NoteLoc = ErrorLoc = SecondBinOp
5773 ? SecondBinOp->getOperatorLoc()
5774 : Second->getLocStart();
5775 NoteRange = ErrorRange =
5776 SecondBinOp ? SecondBinOp->getSourceRange()
5777 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005778 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005779 auto *PossibleXRHSInFirst =
5780 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5781 auto *PossibleXLHSInSecond =
5782 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5783 llvm::FoldingSetNodeID X1Id, X2Id;
5784 PossibleXRHSInFirst->Profile(X1Id, Context,
5785 /*Canonical=*/true);
5786 PossibleXLHSInSecond->Profile(X2Id, Context,
5787 /*Canonical=*/true);
5788 IsUpdateExprFound = X1Id == X2Id;
5789 if (IsUpdateExprFound) {
5790 V = FirstBinOp->getLHS();
5791 X = SecondBinOp->getLHS();
5792 E = SecondBinOp->getRHS();
5793 UE = nullptr;
5794 IsXLHSInRHSPart = false;
5795 IsPostfixUpdate = true;
5796 } else {
5797 ErrorFound = NotASpecificExpression;
5798 ErrorLoc = FirstBinOp->getExprLoc();
5799 ErrorRange = FirstBinOp->getSourceRange();
5800 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5801 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5802 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005803 }
5804 }
5805 }
5806 }
5807 } else {
5808 NoteLoc = ErrorLoc = Body->getLocStart();
5809 NoteRange = ErrorRange =
5810 SourceRange(Body->getLocStart(), Body->getLocStart());
5811 ErrorFound = NotTwoSubstatements;
5812 }
5813 } else {
5814 NoteLoc = ErrorLoc = Body->getLocStart();
5815 NoteRange = ErrorRange =
5816 SourceRange(Body->getLocStart(), Body->getLocStart());
5817 ErrorFound = NotACompoundStatement;
5818 }
5819 if (ErrorFound != NoError) {
5820 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5821 << ErrorRange;
5822 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5823 return StmtError();
5824 } else if (CurContext->isDependentContext()) {
5825 UE = V = E = X = nullptr;
5826 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005827 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005828 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005829
5830 getCurFunction()->setHasBranchProtectedScope();
5831
Alexey Bataev62cec442014-11-18 10:14:22 +00005832 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005833 X, V, E, UE, IsXLHSInRHSPart,
5834 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005835}
5836
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005837StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5838 Stmt *AStmt,
5839 SourceLocation StartLoc,
5840 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005841 if (!AStmt)
5842 return StmtError();
5843
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005844 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5845 // 1.2.2 OpenMP Language Terminology
5846 // Structured block - An executable statement with a single entry at the
5847 // top and a single exit at the bottom.
5848 // The point of exit cannot be a branch out of the structured block.
5849 // longjmp() and throw() must not violate the entry/exit criteria.
5850 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005851
Alexey Bataev13314bf2014-10-09 04:18:56 +00005852 // OpenMP [2.16, Nesting of Regions]
5853 // If specified, a teams construct must be contained within a target
5854 // construct. That target construct must contain no statements or directives
5855 // outside of the teams construct.
5856 if (DSAStack->hasInnerTeamsRegion()) {
5857 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5858 bool OMPTeamsFound = true;
5859 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5860 auto I = CS->body_begin();
5861 while (I != CS->body_end()) {
5862 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5863 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5864 OMPTeamsFound = false;
5865 break;
5866 }
5867 ++I;
5868 }
5869 assert(I != CS->body_end() && "Not found statement");
5870 S = *I;
5871 }
5872 if (!OMPTeamsFound) {
5873 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5874 Diag(DSAStack->getInnerTeamsRegionLoc(),
5875 diag::note_omp_nested_teams_construct_here);
5876 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5877 << isa<OMPExecutableDirective>(S);
5878 return StmtError();
5879 }
5880 }
5881
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005882 getCurFunction()->setHasBranchProtectedScope();
5883
5884 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5885}
5886
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005887StmtResult
5888Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5889 Stmt *AStmt, SourceLocation StartLoc,
5890 SourceLocation EndLoc) {
5891 if (!AStmt)
5892 return StmtError();
5893
5894 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5895 // 1.2.2 OpenMP Language Terminology
5896 // Structured block - An executable statement with a single entry at the
5897 // top and a single exit at the bottom.
5898 // The point of exit cannot be a branch out of the structured block.
5899 // longjmp() and throw() must not violate the entry/exit criteria.
5900 CS->getCapturedDecl()->setNothrow();
5901
5902 getCurFunction()->setHasBranchProtectedScope();
5903
5904 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5905 AStmt);
5906}
5907
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005908StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5909 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5910 SourceLocation EndLoc,
5911 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5912 if (!AStmt)
5913 return StmtError();
5914
5915 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5916 // 1.2.2 OpenMP Language Terminology
5917 // Structured block - An executable statement with a single entry at the
5918 // top and a single exit at the bottom.
5919 // The point of exit cannot be a branch out of the structured block.
5920 // longjmp() and throw() must not violate the entry/exit criteria.
5921 CS->getCapturedDecl()->setNothrow();
5922
5923 OMPLoopDirective::HelperExprs B;
5924 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5925 // define the nested loops number.
5926 unsigned NestedLoopCount =
5927 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5928 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5929 VarsWithImplicitDSA, B);
5930 if (NestedLoopCount == 0)
5931 return StmtError();
5932
5933 assert((CurContext->isDependentContext() || B.builtAll()) &&
5934 "omp target parallel for loop exprs were not built");
5935
5936 if (!CurContext->isDependentContext()) {
5937 // Finalize the clauses that need pre-built expressions for CodeGen.
5938 for (auto C : Clauses) {
5939 if (auto LC = dyn_cast<OMPLinearClause>(C))
5940 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5941 B.NumIterations, *this, CurScope))
5942 return StmtError();
5943 }
5944 }
5945
5946 getCurFunction()->setHasBranchProtectedScope();
5947 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5948 NestedLoopCount, Clauses, AStmt,
5949 B, DSAStack->isCancelRegion());
5950}
5951
Samuel Antaodf67fc42016-01-19 19:15:56 +00005952/// \brief Check for existence of a map clause in the list of clauses.
5953static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5954 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5955 I != E; ++I) {
5956 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5957 return true;
5958 }
5959 }
5960
5961 return false;
5962}
5963
Michael Wong65f367f2015-07-21 13:44:28 +00005964StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5965 Stmt *AStmt,
5966 SourceLocation StartLoc,
5967 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005968 if (!AStmt)
5969 return StmtError();
5970
5971 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5972
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005973 // OpenMP [2.10.1, Restrictions, p. 97]
5974 // At least one map clause must appear on the directive.
5975 if (!HasMapClause(Clauses)) {
5976 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5977 getOpenMPDirectiveName(OMPD_target_data);
5978 return StmtError();
5979 }
5980
Michael Wong65f367f2015-07-21 13:44:28 +00005981 getCurFunction()->setHasBranchProtectedScope();
5982
5983 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5984 AStmt);
5985}
5986
Samuel Antaodf67fc42016-01-19 19:15:56 +00005987StmtResult
5988Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5989 SourceLocation StartLoc,
5990 SourceLocation EndLoc) {
5991 // OpenMP [2.10.2, Restrictions, p. 99]
5992 // At least one map clause must appear on the directive.
5993 if (!HasMapClause(Clauses)) {
5994 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5995 << getOpenMPDirectiveName(OMPD_target_enter_data);
5996 return StmtError();
5997 }
5998
5999 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6000 Clauses);
6001}
6002
Samuel Antao72590762016-01-19 20:04:50 +00006003StmtResult
6004Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6005 SourceLocation StartLoc,
6006 SourceLocation EndLoc) {
6007 // OpenMP [2.10.3, Restrictions, p. 102]
6008 // At least one map clause must appear on the directive.
6009 if (!HasMapClause(Clauses)) {
6010 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6011 << getOpenMPDirectiveName(OMPD_target_exit_data);
6012 return StmtError();
6013 }
6014
6015 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6016}
6017
Alexey Bataev13314bf2014-10-09 04:18:56 +00006018StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6019 Stmt *AStmt, SourceLocation StartLoc,
6020 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006021 if (!AStmt)
6022 return StmtError();
6023
Alexey Bataev13314bf2014-10-09 04:18:56 +00006024 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6025 // 1.2.2 OpenMP Language Terminology
6026 // Structured block - An executable statement with a single entry at the
6027 // top and a single exit at the bottom.
6028 // The point of exit cannot be a branch out of the structured block.
6029 // longjmp() and throw() must not violate the entry/exit criteria.
6030 CS->getCapturedDecl()->setNothrow();
6031
6032 getCurFunction()->setHasBranchProtectedScope();
6033
6034 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6035}
6036
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006037StmtResult
6038Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6039 SourceLocation EndLoc,
6040 OpenMPDirectiveKind CancelRegion) {
6041 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6042 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6043 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6044 << getOpenMPDirectiveName(CancelRegion);
6045 return StmtError();
6046 }
6047 if (DSAStack->isParentNowaitRegion()) {
6048 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6049 return StmtError();
6050 }
6051 if (DSAStack->isParentOrderedRegion()) {
6052 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6053 return StmtError();
6054 }
6055 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6056 CancelRegion);
6057}
6058
Alexey Bataev87933c72015-09-18 08:07:34 +00006059StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6060 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006061 SourceLocation EndLoc,
6062 OpenMPDirectiveKind CancelRegion) {
6063 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6064 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6065 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6066 << getOpenMPDirectiveName(CancelRegion);
6067 return StmtError();
6068 }
6069 if (DSAStack->isParentNowaitRegion()) {
6070 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6071 return StmtError();
6072 }
6073 if (DSAStack->isParentOrderedRegion()) {
6074 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6075 return StmtError();
6076 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006077 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006078 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6079 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006080}
6081
Alexey Bataev382967a2015-12-08 12:06:20 +00006082static bool checkGrainsizeNumTasksClauses(Sema &S,
6083 ArrayRef<OMPClause *> Clauses) {
6084 OMPClause *PrevClause = nullptr;
6085 bool ErrorFound = false;
6086 for (auto *C : Clauses) {
6087 if (C->getClauseKind() == OMPC_grainsize ||
6088 C->getClauseKind() == OMPC_num_tasks) {
6089 if (!PrevClause)
6090 PrevClause = C;
6091 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6092 S.Diag(C->getLocStart(),
6093 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6094 << getOpenMPClauseName(C->getClauseKind())
6095 << getOpenMPClauseName(PrevClause->getClauseKind());
6096 S.Diag(PrevClause->getLocStart(),
6097 diag::note_omp_previous_grainsize_num_tasks)
6098 << getOpenMPClauseName(PrevClause->getClauseKind());
6099 ErrorFound = true;
6100 }
6101 }
6102 }
6103 return ErrorFound;
6104}
6105
Alexey Bataev49f6e782015-12-01 04:18:41 +00006106StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6107 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6108 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006109 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006110 if (!AStmt)
6111 return StmtError();
6112
6113 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6114 OMPLoopDirective::HelperExprs B;
6115 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6116 // define the nested loops number.
6117 unsigned NestedLoopCount =
6118 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006119 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006120 VarsWithImplicitDSA, B);
6121 if (NestedLoopCount == 0)
6122 return StmtError();
6123
6124 assert((CurContext->isDependentContext() || B.builtAll()) &&
6125 "omp for loop exprs were not built");
6126
Alexey Bataev382967a2015-12-08 12:06:20 +00006127 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6128 // The grainsize clause and num_tasks clause are mutually exclusive and may
6129 // not appear on the same taskloop directive.
6130 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6131 return StmtError();
6132
Alexey Bataev49f6e782015-12-01 04:18:41 +00006133 getCurFunction()->setHasBranchProtectedScope();
6134 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6135 NestedLoopCount, Clauses, AStmt, B);
6136}
6137
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006138StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6139 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6140 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006141 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006142 if (!AStmt)
6143 return StmtError();
6144
6145 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6146 OMPLoopDirective::HelperExprs B;
6147 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6148 // define the nested loops number.
6149 unsigned NestedLoopCount =
6150 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6151 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6152 VarsWithImplicitDSA, B);
6153 if (NestedLoopCount == 0)
6154 return StmtError();
6155
6156 assert((CurContext->isDependentContext() || B.builtAll()) &&
6157 "omp for loop exprs were not built");
6158
Alexey Bataev382967a2015-12-08 12:06:20 +00006159 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6160 // The grainsize clause and num_tasks clause are mutually exclusive and may
6161 // not appear on the same taskloop directive.
6162 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6163 return StmtError();
6164
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006165 getCurFunction()->setHasBranchProtectedScope();
6166 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6167 NestedLoopCount, Clauses, AStmt, B);
6168}
6169
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006170StmtResult Sema::ActOnOpenMPDistributeDirective(
6171 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6172 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006173 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006174 if (!AStmt)
6175 return StmtError();
6176
6177 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6178 OMPLoopDirective::HelperExprs B;
6179 // In presence of clause 'collapse' with number of loops, it will
6180 // define the nested loops number.
6181 unsigned NestedLoopCount =
6182 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6183 nullptr /*ordered not a clause on distribute*/, AStmt,
6184 *this, *DSAStack, VarsWithImplicitDSA, B);
6185 if (NestedLoopCount == 0)
6186 return StmtError();
6187
6188 assert((CurContext->isDependentContext() || B.builtAll()) &&
6189 "omp for loop exprs were not built");
6190
6191 getCurFunction()->setHasBranchProtectedScope();
6192 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6193 NestedLoopCount, Clauses, AStmt, B);
6194}
6195
Alexey Bataeved09d242014-05-28 05:53:51 +00006196OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006197 SourceLocation StartLoc,
6198 SourceLocation LParenLoc,
6199 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006200 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006201 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006202 case OMPC_final:
6203 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6204 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006205 case OMPC_num_threads:
6206 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6207 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006208 case OMPC_safelen:
6209 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6210 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006211 case OMPC_simdlen:
6212 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6213 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006214 case OMPC_collapse:
6215 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6216 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006217 case OMPC_ordered:
6218 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6219 break;
Michael Wonge710d542015-08-07 16:16:36 +00006220 case OMPC_device:
6221 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6222 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006223 case OMPC_num_teams:
6224 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6225 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006226 case OMPC_thread_limit:
6227 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6228 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006229 case OMPC_priority:
6230 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6231 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006232 case OMPC_grainsize:
6233 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6234 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006235 case OMPC_num_tasks:
6236 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6237 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006238 case OMPC_hint:
6239 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6240 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006241 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006242 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006243 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006244 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006245 case OMPC_private:
6246 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006247 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006248 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006249 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006250 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006251 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006252 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006253 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006254 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006255 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006256 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006257 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006258 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006259 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006260 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006261 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006262 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006263 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006264 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006265 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006266 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006267 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006268 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006269 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006270 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006271 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006272 llvm_unreachable("Clause is not allowed.");
6273 }
6274 return Res;
6275}
6276
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006277OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6278 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006279 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006280 SourceLocation NameModifierLoc,
6281 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006282 SourceLocation EndLoc) {
6283 Expr *ValExpr = Condition;
6284 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6285 !Condition->isInstantiationDependent() &&
6286 !Condition->containsUnexpandedParameterPack()) {
6287 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006288 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006289 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006290 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006291
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006292 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006293 }
6294
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006295 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6296 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006297}
6298
Alexey Bataev3778b602014-07-17 07:32:53 +00006299OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6300 SourceLocation StartLoc,
6301 SourceLocation LParenLoc,
6302 SourceLocation EndLoc) {
6303 Expr *ValExpr = Condition;
6304 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6305 !Condition->isInstantiationDependent() &&
6306 !Condition->containsUnexpandedParameterPack()) {
6307 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6308 Condition->getExprLoc(), Condition);
6309 if (Val.isInvalid())
6310 return nullptr;
6311
6312 ValExpr = Val.get();
6313 }
6314
6315 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6316}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006317ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6318 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006319 if (!Op)
6320 return ExprError();
6321
6322 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6323 public:
6324 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006325 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006326 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6327 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006328 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6329 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006330 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6331 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006332 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6333 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006334 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6335 QualType T,
6336 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006337 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6338 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006339 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6340 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006341 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006342 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006343 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006344 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6345 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006346 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6347 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006348 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6349 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006350 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006351 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006352 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006353 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6354 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006355 llvm_unreachable("conversion functions are permitted");
6356 }
6357 } ConvertDiagnoser;
6358 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6359}
6360
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006361static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006362 OpenMPClauseKind CKind,
6363 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006364 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6365 !ValExpr->isInstantiationDependent()) {
6366 SourceLocation Loc = ValExpr->getExprLoc();
6367 ExprResult Value =
6368 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6369 if (Value.isInvalid())
6370 return false;
6371
6372 ValExpr = Value.get();
6373 // The expression must evaluate to a non-negative integer value.
6374 llvm::APSInt Result;
6375 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006376 Result.isSigned() &&
6377 !((!StrictlyPositive && Result.isNonNegative()) ||
6378 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006379 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006380 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6381 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006382 return false;
6383 }
6384 }
6385 return true;
6386}
6387
Alexey Bataev568a8332014-03-06 06:15:19 +00006388OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6389 SourceLocation StartLoc,
6390 SourceLocation LParenLoc,
6391 SourceLocation EndLoc) {
6392 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006393
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006394 // OpenMP [2.5, Restrictions]
6395 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006396 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6397 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006398 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006399
Alexey Bataeved09d242014-05-28 05:53:51 +00006400 return new (Context)
6401 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006402}
6403
Alexey Bataev62c87d22014-03-21 04:51:18 +00006404ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006405 OpenMPClauseKind CKind,
6406 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006407 if (!E)
6408 return ExprError();
6409 if (E->isValueDependent() || E->isTypeDependent() ||
6410 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006411 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006412 llvm::APSInt Result;
6413 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6414 if (ICE.isInvalid())
6415 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006416 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6417 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006418 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006419 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6420 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006421 return ExprError();
6422 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006423 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6424 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6425 << E->getSourceRange();
6426 return ExprError();
6427 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006428 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6429 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006430 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006431 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006432 return ICE;
6433}
6434
6435OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6436 SourceLocation LParenLoc,
6437 SourceLocation EndLoc) {
6438 // OpenMP [2.8.1, simd construct, Description]
6439 // The parameter of the safelen clause must be a constant
6440 // positive integer expression.
6441 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6442 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006443 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006444 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006445 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006446}
6447
Alexey Bataev66b15b52015-08-21 11:14:16 +00006448OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6449 SourceLocation LParenLoc,
6450 SourceLocation EndLoc) {
6451 // OpenMP [2.8.1, simd construct, Description]
6452 // The parameter of the simdlen clause must be a constant
6453 // positive integer expression.
6454 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6455 if (Simdlen.isInvalid())
6456 return nullptr;
6457 return new (Context)
6458 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6459}
6460
Alexander Musman64d33f12014-06-04 07:53:32 +00006461OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6462 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006463 SourceLocation LParenLoc,
6464 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006465 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006466 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006467 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006468 // The parameter of the collapse clause must be a constant
6469 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006470 ExprResult NumForLoopsResult =
6471 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6472 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006473 return nullptr;
6474 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006475 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006476}
6477
Alexey Bataev10e775f2015-07-30 11:36:16 +00006478OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6479 SourceLocation EndLoc,
6480 SourceLocation LParenLoc,
6481 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006482 // OpenMP [2.7.1, loop construct, Description]
6483 // OpenMP [2.8.1, simd construct, Description]
6484 // OpenMP [2.9.6, distribute construct, Description]
6485 // The parameter of the ordered clause must be a constant
6486 // positive integer expression if any.
6487 if (NumForLoops && LParenLoc.isValid()) {
6488 ExprResult NumForLoopsResult =
6489 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6490 if (NumForLoopsResult.isInvalid())
6491 return nullptr;
6492 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006493 } else
6494 NumForLoops = nullptr;
6495 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006496 return new (Context)
6497 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6498}
6499
Alexey Bataeved09d242014-05-28 05:53:51 +00006500OMPClause *Sema::ActOnOpenMPSimpleClause(
6501 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6502 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006503 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006504 switch (Kind) {
6505 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006506 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006507 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6508 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006509 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006510 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006511 Res = ActOnOpenMPProcBindClause(
6512 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6513 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006514 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006515 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006516 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006517 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006518 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006519 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006520 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006521 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006522 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006523 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006524 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006525 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006526 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006527 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006528 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006529 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006530 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006531 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006532 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006533 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006534 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006535 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006536 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006537 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006538 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006539 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006540 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006541 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006542 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006543 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006544 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006545 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006546 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006547 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006548 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006549 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006550 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006551 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006552 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006553 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006554 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006555 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006556 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006557 llvm_unreachable("Clause is not allowed.");
6558 }
6559 return Res;
6560}
6561
Alexey Bataev6402bca2015-12-28 07:25:51 +00006562static std::string
6563getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6564 ArrayRef<unsigned> Exclude = llvm::None) {
6565 std::string Values;
6566 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6567 unsigned Skipped = Exclude.size();
6568 auto S = Exclude.begin(), E = Exclude.end();
6569 for (unsigned i = First; i < Last; ++i) {
6570 if (std::find(S, E, i) != E) {
6571 --Skipped;
6572 continue;
6573 }
6574 Values += "'";
6575 Values += getOpenMPSimpleClauseTypeName(K, i);
6576 Values += "'";
6577 if (i == Bound - Skipped)
6578 Values += " or ";
6579 else if (i != Bound + 1 - Skipped)
6580 Values += ", ";
6581 }
6582 return Values;
6583}
6584
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006585OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6586 SourceLocation KindKwLoc,
6587 SourceLocation StartLoc,
6588 SourceLocation LParenLoc,
6589 SourceLocation EndLoc) {
6590 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006591 static_assert(OMPC_DEFAULT_unknown > 0,
6592 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006593 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006594 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6595 /*Last=*/OMPC_DEFAULT_unknown)
6596 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006597 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006598 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006599 switch (Kind) {
6600 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006601 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006602 break;
6603 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006604 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006605 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006606 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006607 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006608 break;
6609 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006610 return new (Context)
6611 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006612}
6613
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006614OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6615 SourceLocation KindKwLoc,
6616 SourceLocation StartLoc,
6617 SourceLocation LParenLoc,
6618 SourceLocation EndLoc) {
6619 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006620 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006621 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6622 /*Last=*/OMPC_PROC_BIND_unknown)
6623 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006624 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006625 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006626 return new (Context)
6627 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006628}
6629
Alexey Bataev56dafe82014-06-20 07:16:17 +00006630OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006631 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006632 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006633 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006634 SourceLocation EndLoc) {
6635 OMPClause *Res = nullptr;
6636 switch (Kind) {
6637 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006638 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6639 assert(Argument.size() == NumberOfElements &&
6640 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006641 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006642 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6643 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6644 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6645 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6646 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006647 break;
6648 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006649 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6650 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6651 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6652 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006653 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006654 case OMPC_dist_schedule:
6655 Res = ActOnOpenMPDistScheduleClause(
6656 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6657 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6658 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006659 case OMPC_defaultmap:
6660 enum { Modifier, DefaultmapKind };
6661 Res = ActOnOpenMPDefaultmapClause(
6662 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6663 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6664 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6665 ArgumentLoc[DefaultmapKind], EndLoc);
6666 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006667 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006668 case OMPC_num_threads:
6669 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006670 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006671 case OMPC_collapse:
6672 case OMPC_default:
6673 case OMPC_proc_bind:
6674 case OMPC_private:
6675 case OMPC_firstprivate:
6676 case OMPC_lastprivate:
6677 case OMPC_shared:
6678 case OMPC_reduction:
6679 case OMPC_linear:
6680 case OMPC_aligned:
6681 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006682 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006683 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006684 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006685 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006686 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006687 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006688 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006689 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006690 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006691 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006692 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006693 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006694 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006695 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006696 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006697 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006698 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006699 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006700 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006701 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006702 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006703 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006704 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006705 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006706 case OMPC_unknown:
6707 llvm_unreachable("Clause is not allowed.");
6708 }
6709 return Res;
6710}
6711
Alexey Bataev6402bca2015-12-28 07:25:51 +00006712static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6713 OpenMPScheduleClauseModifier M2,
6714 SourceLocation M1Loc, SourceLocation M2Loc) {
6715 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6716 SmallVector<unsigned, 2> Excluded;
6717 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6718 Excluded.push_back(M2);
6719 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6720 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6721 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6722 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6723 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6724 << getListOfPossibleValues(OMPC_schedule,
6725 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6726 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6727 Excluded)
6728 << getOpenMPClauseName(OMPC_schedule);
6729 return true;
6730 }
6731 return false;
6732}
6733
Alexey Bataev56dafe82014-06-20 07:16:17 +00006734OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006735 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006736 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006737 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6738 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6739 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6740 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6741 return nullptr;
6742 // OpenMP, 2.7.1, Loop Construct, Restrictions
6743 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6744 // but not both.
6745 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6746 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6747 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6748 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6749 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6750 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6751 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6752 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6753 return nullptr;
6754 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006755 if (Kind == OMPC_SCHEDULE_unknown) {
6756 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006757 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6758 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6759 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6760 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6761 Exclude);
6762 } else {
6763 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6764 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006765 }
6766 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6767 << Values << getOpenMPClauseName(OMPC_schedule);
6768 return nullptr;
6769 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006770 // OpenMP, 2.7.1, Loop Construct, Restrictions
6771 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6772 // schedule(guided).
6773 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6774 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6775 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6776 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6777 diag::err_omp_schedule_nonmonotonic_static);
6778 return nullptr;
6779 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006780 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006781 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006782 if (ChunkSize) {
6783 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6784 !ChunkSize->isInstantiationDependent() &&
6785 !ChunkSize->containsUnexpandedParameterPack()) {
6786 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6787 ExprResult Val =
6788 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6789 if (Val.isInvalid())
6790 return nullptr;
6791
6792 ValExpr = Val.get();
6793
6794 // OpenMP [2.7.1, Restrictions]
6795 // chunk_size must be a loop invariant integer expression with a positive
6796 // value.
6797 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006798 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6799 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6800 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006801 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006802 return nullptr;
6803 }
6804 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev3392d762016-02-16 11:18:12 +00006805 ValExpr = buildCapture(*this, ".chunk.", ValExpr);
6806 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
6807 HelperValStmt =
6808 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
6809 /*NumDecls=*/1),
6810 SourceLocation(), SourceLocation());
6811 ValExpr = DefaultLvalueConversion(ValExpr).get();
Alexey Bataev56dafe82014-06-20 07:16:17 +00006812 }
6813 }
6814 }
6815
Alexey Bataev6402bca2015-12-28 07:25:51 +00006816 return new (Context)
6817 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006818 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006819}
6820
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006821OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6822 SourceLocation StartLoc,
6823 SourceLocation EndLoc) {
6824 OMPClause *Res = nullptr;
6825 switch (Kind) {
6826 case OMPC_ordered:
6827 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6828 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006829 case OMPC_nowait:
6830 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6831 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006832 case OMPC_untied:
6833 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6834 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006835 case OMPC_mergeable:
6836 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6837 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006838 case OMPC_read:
6839 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6840 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006841 case OMPC_write:
6842 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6843 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006844 case OMPC_update:
6845 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6846 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006847 case OMPC_capture:
6848 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6849 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006850 case OMPC_seq_cst:
6851 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6852 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006853 case OMPC_threads:
6854 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6855 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006856 case OMPC_simd:
6857 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6858 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006859 case OMPC_nogroup:
6860 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6861 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006862 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006863 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006864 case OMPC_num_threads:
6865 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006866 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006867 case OMPC_collapse:
6868 case OMPC_schedule:
6869 case OMPC_private:
6870 case OMPC_firstprivate:
6871 case OMPC_lastprivate:
6872 case OMPC_shared:
6873 case OMPC_reduction:
6874 case OMPC_linear:
6875 case OMPC_aligned:
6876 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006877 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006878 case OMPC_default:
6879 case OMPC_proc_bind:
6880 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006881 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006882 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006883 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006884 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006885 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006886 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006887 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006888 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006889 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006890 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006891 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006892 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006893 case OMPC_unknown:
6894 llvm_unreachable("Clause is not allowed.");
6895 }
6896 return Res;
6897}
6898
Alexey Bataev236070f2014-06-20 11:19:47 +00006899OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6900 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006901 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006902 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6903}
6904
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006905OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6906 SourceLocation EndLoc) {
6907 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6908}
6909
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006910OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6911 SourceLocation EndLoc) {
6912 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6913}
6914
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006915OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6916 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006917 return new (Context) OMPReadClause(StartLoc, EndLoc);
6918}
6919
Alexey Bataevdea47612014-07-23 07:46:59 +00006920OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6921 SourceLocation EndLoc) {
6922 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6923}
6924
Alexey Bataev67a4f222014-07-23 10:25:33 +00006925OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6926 SourceLocation EndLoc) {
6927 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6928}
6929
Alexey Bataev459dec02014-07-24 06:46:57 +00006930OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6931 SourceLocation EndLoc) {
6932 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6933}
6934
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006935OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6936 SourceLocation EndLoc) {
6937 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6938}
6939
Alexey Bataev346265e2015-09-25 10:37:12 +00006940OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6941 SourceLocation EndLoc) {
6942 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6943}
6944
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006945OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6946 SourceLocation EndLoc) {
6947 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6948}
6949
Alexey Bataevb825de12015-12-07 10:51:44 +00006950OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6951 SourceLocation EndLoc) {
6952 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6953}
6954
Alexey Bataevc5e02582014-06-16 07:08:35 +00006955OMPClause *Sema::ActOnOpenMPVarListClause(
6956 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6957 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6958 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006959 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006960 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6961 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6962 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006963 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006964 switch (Kind) {
6965 case OMPC_private:
6966 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6967 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006968 case OMPC_firstprivate:
6969 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6970 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006971 case OMPC_lastprivate:
6972 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6973 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006974 case OMPC_shared:
6975 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6976 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006977 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006978 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6979 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006980 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006981 case OMPC_linear:
6982 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006983 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006984 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006985 case OMPC_aligned:
6986 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6987 ColonLoc, EndLoc);
6988 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006989 case OMPC_copyin:
6990 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6991 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006992 case OMPC_copyprivate:
6993 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6994 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006995 case OMPC_flush:
6996 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6997 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006998 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006999 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7000 StartLoc, LParenLoc, EndLoc);
7001 break;
7002 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007003 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7004 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7005 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007006 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007007 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007008 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007009 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007010 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007011 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007012 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007013 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007014 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007015 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007016 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007017 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007018 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007019 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007020 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007021 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007022 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007023 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007024 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007025 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007026 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007027 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007028 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007029 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007030 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007031 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007032 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007033 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007034 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007035 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007036 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007037 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007038 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007039 llvm_unreachable("Clause is not allowed.");
7040 }
7041 return Res;
7042}
7043
Alexey Bataev90c228f2016-02-08 09:29:13 +00007044ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7045 ExprObjectKind OK) {
7046 SourceLocation Loc = Capture->getInit()->getExprLoc();
7047 ExprResult Res = BuildDeclRefExpr(
7048 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7049 if (!Res.isUsable())
7050 return ExprError();
7051 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7052 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7053 if (!Res.isUsable())
7054 return ExprError();
7055 }
7056 if (VK != VK_LValue && Res.get()->isGLValue()) {
7057 Res = DefaultLvalueConversion(Res.get());
7058 if (!Res.isUsable())
7059 return ExprError();
7060 }
7061 return Res;
7062}
7063
Alexey Bataevd985eda2016-02-10 11:29:16 +00007064static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *RefExpr) {
7065 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7066 RefExpr->containsUnexpandedParameterPack())
7067 return std::make_pair(nullptr, true);
7068
7069 SourceLocation ELoc = RefExpr->getExprLoc();
7070 SourceRange SR = RefExpr->getSourceRange();
7071 // OpenMP [3.1, C/C++]
7072 // A list item is a variable name.
7073 // OpenMP [2.9.3.3, Restrictions, p.1]
7074 // A variable that is part of another variable (as an array or
7075 // structure element) cannot appear in a private clause.
7076 RefExpr = RefExpr->IgnoreParens();
7077 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7078 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7079 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7080 (S.getCurrentThisType().isNull() || !ME ||
7081 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7082 !isa<FieldDecl>(ME->getMemberDecl()))) {
7083 S.Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7084 << (S.getCurrentThisType().isNull() ? 0 : 1) << SR;
7085 return std::make_pair(nullptr, false);
7086 }
7087 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7088}
7089
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007090OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7091 SourceLocation StartLoc,
7092 SourceLocation LParenLoc,
7093 SourceLocation EndLoc) {
7094 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007095 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007096 for (auto &RefExpr : VarList) {
7097 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007098 auto Res = getPrivateItem(*this, RefExpr);
7099 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007100 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007101 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007102 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007103 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007104 ValueDecl *D = Res.first;
7105 if (!D)
7106 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007107
Alexey Bataeved09d242014-05-28 05:53:51 +00007108 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007109 QualType Type = D->getType();
7110 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007111
7112 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7113 // A variable that appears in a private clause must not have an incomplete
7114 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007115 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007116 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007117 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007118
Alexey Bataev758e55e2013-09-06 18:03:48 +00007119 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7120 // in a Construct]
7121 // Variables with the predetermined data-sharing attributes may not be
7122 // listed in data-sharing attributes clauses, except for the cases
7123 // listed below. For these exceptions only, listing a predetermined
7124 // variable in a data-sharing attribute clause is allowed and overrides
7125 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007126 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007127 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007128 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7129 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007130 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007131 continue;
7132 }
7133
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007134 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007135 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007136 DSAStack->getCurrentDirective() == OMPD_task) {
7137 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7138 << getOpenMPClauseName(OMPC_private) << Type
7139 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7140 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007141 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007142 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007143 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007144 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007145 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007146 continue;
7147 }
7148
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007149 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7150 // A variable of class type (or array thereof) that appears in a private
7151 // clause requires an accessible, unambiguous default constructor for the
7152 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007153 // Generate helper private variable and initialize it with the default
7154 // value. The address of the original variable is replaced by the address of
7155 // the new private variable in CodeGen. This new variable is not added to
7156 // IdResolver, so the code in the OpenMP region uses original variable for
7157 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007158 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007159 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7160 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007161 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007162 if (VDPrivate->isInvalidDecl())
7163 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007164 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007165 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007166
Alexey Bataev90c228f2016-02-08 09:29:13 +00007167 DeclRefExpr *Ref = nullptr;
7168 if (!VD)
7169 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7170 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7171 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007172 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007173 }
7174
Alexey Bataeved09d242014-05-28 05:53:51 +00007175 if (Vars.empty())
7176 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007177
Alexey Bataev03b340a2014-10-21 03:16:40 +00007178 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7179 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007180}
7181
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007182namespace {
7183class DiagsUninitializedSeveretyRAII {
7184private:
7185 DiagnosticsEngine &Diags;
7186 SourceLocation SavedLoc;
7187 bool IsIgnored;
7188
7189public:
7190 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7191 bool IsIgnored)
7192 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7193 if (!IsIgnored) {
7194 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7195 /*Map*/ diag::Severity::Ignored, Loc);
7196 }
7197 }
7198 ~DiagsUninitializedSeveretyRAII() {
7199 if (!IsIgnored)
7200 Diags.popMappings(SavedLoc);
7201 }
7202};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007203}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007204
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007205OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7206 SourceLocation StartLoc,
7207 SourceLocation LParenLoc,
7208 SourceLocation EndLoc) {
7209 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007210 SmallVector<Expr *, 8> PrivateCopies;
7211 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007212 bool IsImplicitClause =
7213 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7214 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7215
Alexey Bataeved09d242014-05-28 05:53:51 +00007216 for (auto &RefExpr : VarList) {
7217 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007218 auto Res = getPrivateItem(*this, RefExpr);
7219 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007220 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007221 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007222 PrivateCopies.push_back(nullptr);
7223 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007224 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007225 ValueDecl *D = Res.first;
7226 if (!D)
7227 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007228
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007229 SourceLocation ELoc =
7230 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007231 QualType Type = D->getType();
7232 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007233
7234 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7235 // A variable that appears in a private clause must not have an incomplete
7236 // type or a reference type.
7237 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007238 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007239 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007240 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007241
7242 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7243 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007244 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007245 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007246 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007247
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007248 // If an implicit firstprivate variable found it was checked already.
7249 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007250 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007251 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007252 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7253 // A list item that specifies a given variable may not appear in more
7254 // than one clause on the same directive, except that a variable may be
7255 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007256 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007257 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007258 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007259 << getOpenMPClauseName(DVar.CKind)
7260 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007261 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007262 continue;
7263 }
7264
7265 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7266 // in a Construct]
7267 // Variables with the predetermined data-sharing attributes may not be
7268 // listed in data-sharing attributes clauses, except for the cases
7269 // listed below. For these exceptions only, listing a predetermined
7270 // variable in a data-sharing attribute clause is allowed and overrides
7271 // the variable's predetermined data-sharing attributes.
7272 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7273 // in a Construct, C/C++, p.2]
7274 // Variables with const-qualified type having no mutable member may be
7275 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007276 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007277 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7278 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007279 << getOpenMPClauseName(DVar.CKind)
7280 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007281 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007282 continue;
7283 }
7284
Alexey Bataevf29276e2014-06-18 04:14:57 +00007285 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007286 // OpenMP [2.9.3.4, Restrictions, p.2]
7287 // A list item that is private within a parallel region must not appear
7288 // in a firstprivate clause on a worksharing construct if any of the
7289 // worksharing regions arising from the worksharing construct ever bind
7290 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007291 if (isOpenMPWorksharingDirective(CurrDir) &&
7292 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007293 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007294 if (DVar.CKind != OMPC_shared &&
7295 (isOpenMPParallelDirective(DVar.DKind) ||
7296 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007297 Diag(ELoc, diag::err_omp_required_access)
7298 << getOpenMPClauseName(OMPC_firstprivate)
7299 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007300 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007301 continue;
7302 }
7303 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007304 // OpenMP [2.9.3.4, Restrictions, p.3]
7305 // A list item that appears in a reduction clause of a parallel construct
7306 // must not appear in a firstprivate clause on a worksharing or task
7307 // construct if any of the worksharing or task regions arising from the
7308 // worksharing or task construct ever bind to any of the parallel regions
7309 // arising from the parallel construct.
7310 // OpenMP [2.9.3.4, Restrictions, p.4]
7311 // A list item that appears in a reduction clause in worksharing
7312 // construct must not appear in a firstprivate clause in a task construct
7313 // encountered during execution of any of the worksharing regions arising
7314 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007315 if (CurrDir == OMPD_task) {
7316 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007317 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007318 [](OpenMPDirectiveKind K) -> bool {
7319 return isOpenMPParallelDirective(K) ||
7320 isOpenMPWorksharingDirective(K);
7321 },
7322 false);
7323 if (DVar.CKind == OMPC_reduction &&
7324 (isOpenMPParallelDirective(DVar.DKind) ||
7325 isOpenMPWorksharingDirective(DVar.DKind))) {
7326 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7327 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007328 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007329 continue;
7330 }
7331 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007332
7333 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7334 // A list item that is private within a teams region must not appear in a
7335 // firstprivate clause on a distribute construct if any of the distribute
7336 // regions arising from the distribute construct ever bind to any of the
7337 // teams regions arising from the teams construct.
7338 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7339 // A list item that appears in a reduction clause of a teams construct
7340 // must not appear in a firstprivate clause on a distribute construct if
7341 // any of the distribute regions arising from the distribute construct
7342 // ever bind to any of the teams regions arising from the teams construct.
7343 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7344 // A list item may appear in a firstprivate or lastprivate clause but not
7345 // both.
7346 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007347 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007348 [](OpenMPDirectiveKind K) -> bool {
7349 return isOpenMPTeamsDirective(K);
7350 },
7351 false);
7352 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7353 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007354 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007355 continue;
7356 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007357 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007358 [](OpenMPDirectiveKind K) -> bool {
7359 return isOpenMPTeamsDirective(K);
7360 },
7361 false);
7362 if (DVar.CKind == OMPC_reduction &&
7363 isOpenMPTeamsDirective(DVar.DKind)) {
7364 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007365 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007366 continue;
7367 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007368 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007369 if (DVar.CKind == OMPC_lastprivate) {
7370 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007371 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007372 continue;
7373 }
7374 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007375 }
7376
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007377 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007378 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007379 DSAStack->getCurrentDirective() == OMPD_task) {
7380 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7381 << getOpenMPClauseName(OMPC_firstprivate) << Type
7382 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7383 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007384 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007385 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007386 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007387 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007388 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007389 continue;
7390 }
7391
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007392 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007393 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7394 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007395 // Generate helper private variable and initialize it with the value of the
7396 // original variable. The address of the original variable is replaced by
7397 // the address of the new private variable in the CodeGen. This new variable
7398 // is not added to IdResolver, so the code in the OpenMP region uses
7399 // original variable for proper diagnostics and variable capturing.
7400 Expr *VDInitRefExpr = nullptr;
7401 // For arrays generate initializer for single element and replace it by the
7402 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007403 if (Type->isArrayType()) {
7404 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007405 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007406 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007407 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007408 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007409 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007410 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007411 InitializedEntity Entity =
7412 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007413 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7414
7415 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7416 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7417 if (Result.isInvalid())
7418 VDPrivate->setInvalidDecl();
7419 else
7420 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007421 // Remove temp variable declaration.
7422 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007423 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007424 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7425 ".firstprivate.temp");
7426 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7427 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007428 AddInitializerToDecl(VDPrivate,
7429 DefaultLvalueConversion(VDInitRefExpr).get(),
7430 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007431 }
7432 if (VDPrivate->isInvalidDecl()) {
7433 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007434 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007435 diag::note_omp_task_predetermined_firstprivate_here);
7436 }
7437 continue;
7438 }
7439 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007440 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007441 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7442 RefExpr->getExprLoc());
7443 DeclRefExpr *Ref = nullptr;
7444 if (!VD)
7445 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7446 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7447 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007448 PrivateCopies.push_back(VDPrivateRefExpr);
7449 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007450 }
7451
Alexey Bataeved09d242014-05-28 05:53:51 +00007452 if (Vars.empty())
7453 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007454
7455 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007456 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007457}
7458
Alexander Musman1bb328c2014-06-04 13:06:39 +00007459OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7460 SourceLocation StartLoc,
7461 SourceLocation LParenLoc,
7462 SourceLocation EndLoc) {
7463 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007464 SmallVector<Expr *, 8> SrcExprs;
7465 SmallVector<Expr *, 8> DstExprs;
7466 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007467 for (auto &RefExpr : VarList) {
7468 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7469 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7470 // It will be analyzed later.
7471 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007472 SrcExprs.push_back(nullptr);
7473 DstExprs.push_back(nullptr);
7474 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007475 continue;
7476 }
7477
7478 SourceLocation ELoc = RefExpr->getExprLoc();
7479 // OpenMP [2.1, C/C++]
7480 // A list item is a variable name.
7481 // OpenMP [2.14.3.5, Restrictions, p.1]
7482 // A variable that is part of another variable (as an array or structure
7483 // element) cannot appear in a lastprivate clause.
7484 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7485 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007486 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7487 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007488 continue;
7489 }
7490 Decl *D = DE->getDecl();
7491 VarDecl *VD = cast<VarDecl>(D);
7492
7493 QualType Type = VD->getType();
7494 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7495 // It will be analyzed later.
7496 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007497 SrcExprs.push_back(nullptr);
7498 DstExprs.push_back(nullptr);
7499 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007500 continue;
7501 }
7502
7503 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7504 // A variable that appears in a lastprivate clause must not have an
7505 // incomplete type or a reference type.
7506 if (RequireCompleteType(ELoc, Type,
7507 diag::err_omp_lastprivate_incomplete_type)) {
7508 continue;
7509 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007510 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007511
7512 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7513 // in a Construct]
7514 // Variables with the predetermined data-sharing attributes may not be
7515 // listed in data-sharing attributes clauses, except for the cases
7516 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007517 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007518 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7519 DVar.CKind != OMPC_firstprivate &&
7520 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7521 Diag(ELoc, diag::err_omp_wrong_dsa)
7522 << getOpenMPClauseName(DVar.CKind)
7523 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007524 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007525 continue;
7526 }
7527
Alexey Bataevf29276e2014-06-18 04:14:57 +00007528 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7529 // OpenMP [2.14.3.5, Restrictions, p.2]
7530 // A list item that is private within a parallel region, or that appears in
7531 // the reduction clause of a parallel construct, must not appear in a
7532 // lastprivate clause on a worksharing construct if any of the corresponding
7533 // worksharing regions ever binds to any of the corresponding parallel
7534 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007535 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007536 if (isOpenMPWorksharingDirective(CurrDir) &&
7537 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007538 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007539 if (DVar.CKind != OMPC_shared) {
7540 Diag(ELoc, diag::err_omp_required_access)
7541 << getOpenMPClauseName(OMPC_lastprivate)
7542 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007543 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007544 continue;
7545 }
7546 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007547 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007548 // A variable of class type (or array thereof) that appears in a
7549 // lastprivate clause requires an accessible, unambiguous default
7550 // constructor for the class type, unless the list item is also specified
7551 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007552 // A variable of class type (or array thereof) that appears in a
7553 // lastprivate clause requires an accessible, unambiguous copy assignment
7554 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007555 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007556 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007557 Type.getUnqualifiedType(), ".lastprivate.src",
7558 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007559 auto *PseudoSrcExpr = buildDeclRefExpr(
7560 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007561 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007562 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7563 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007564 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007565 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007566 // For arrays generate assignment operation for single element and replace
7567 // it by the original array element in CodeGen.
7568 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7569 PseudoDstExpr, PseudoSrcExpr);
7570 if (AssignmentOp.isInvalid())
7571 continue;
7572 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7573 /*DiscardedValue=*/true);
7574 if (AssignmentOp.isInvalid())
7575 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007576
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007577 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7578 // A list item may appear in a firstprivate or lastprivate clause but not
7579 // both.
7580 if (CurrDir == OMPD_distribute) {
7581 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7582 if (DVar.CKind == OMPC_firstprivate) {
7583 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7584 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7585 continue;
7586 }
7587 }
7588
Alexey Bataev39f915b82015-05-08 10:41:21 +00007589 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007590 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007591 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007592 SrcExprs.push_back(PseudoSrcExpr);
7593 DstExprs.push_back(PseudoDstExpr);
7594 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007595 }
7596
7597 if (Vars.empty())
7598 return nullptr;
7599
7600 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007601 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007602}
7603
Alexey Bataev758e55e2013-09-06 18:03:48 +00007604OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7605 SourceLocation StartLoc,
7606 SourceLocation LParenLoc,
7607 SourceLocation EndLoc) {
7608 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007609 for (auto &RefExpr : VarList) {
7610 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7611 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007612 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007613 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007614 continue;
7615 }
7616
Alexey Bataeved09d242014-05-28 05:53:51 +00007617 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007618 // OpenMP [2.1, C/C++]
7619 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007620 // OpenMP [2.14.3.2, Restrictions, p.1]
7621 // A variable that is part of another variable (as an array or structure
7622 // element) cannot appear in a shared unless it is a static data member
7623 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007624 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007625 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007626 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7627 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007628 continue;
7629 }
7630 Decl *D = DE->getDecl();
7631 VarDecl *VD = cast<VarDecl>(D);
7632
7633 QualType Type = VD->getType();
7634 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7635 // It will be analyzed later.
7636 Vars.push_back(DE);
7637 continue;
7638 }
7639
7640 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7641 // in a Construct]
7642 // Variables with the predetermined data-sharing attributes may not be
7643 // listed in data-sharing attributes clauses, except for the cases
7644 // listed below. For these exceptions only, listing a predetermined
7645 // variable in a data-sharing attribute clause is allowed and overrides
7646 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007647 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007648 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7649 DVar.RefExpr) {
7650 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7651 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007652 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007653 continue;
7654 }
7655
7656 DSAStack->addDSA(VD, DE, OMPC_shared);
7657 Vars.push_back(DE);
7658 }
7659
Alexey Bataeved09d242014-05-28 05:53:51 +00007660 if (Vars.empty())
7661 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007662
7663 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7664}
7665
Alexey Bataevc5e02582014-06-16 07:08:35 +00007666namespace {
7667class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7668 DSAStackTy *Stack;
7669
7670public:
7671 bool VisitDeclRefExpr(DeclRefExpr *E) {
7672 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007673 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007674 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7675 return false;
7676 if (DVar.CKind != OMPC_unknown)
7677 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007678 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007679 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007680 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007681 return true;
7682 return false;
7683 }
7684 return false;
7685 }
7686 bool VisitStmt(Stmt *S) {
7687 for (auto Child : S->children()) {
7688 if (Child && Visit(Child))
7689 return true;
7690 }
7691 return false;
7692 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007693 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007694};
Alexey Bataev23b69422014-06-18 07:08:49 +00007695} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007696
7697OMPClause *Sema::ActOnOpenMPReductionClause(
7698 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7699 SourceLocation ColonLoc, SourceLocation EndLoc,
7700 CXXScopeSpec &ReductionIdScopeSpec,
7701 const DeclarationNameInfo &ReductionId) {
7702 // TODO: Allow scope specification search when 'declare reduction' is
7703 // supported.
7704 assert(ReductionIdScopeSpec.isEmpty() &&
7705 "No support for scoped reduction identifiers yet.");
7706
7707 auto DN = ReductionId.getName();
7708 auto OOK = DN.getCXXOverloadedOperator();
7709 BinaryOperatorKind BOK = BO_Comma;
7710
7711 // OpenMP [2.14.3.6, reduction clause]
7712 // C
7713 // reduction-identifier is either an identifier or one of the following
7714 // operators: +, -, *, &, |, ^, && and ||
7715 // C++
7716 // reduction-identifier is either an id-expression or one of the following
7717 // operators: +, -, *, &, |, ^, && and ||
7718 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7719 switch (OOK) {
7720 case OO_Plus:
7721 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007722 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007723 break;
7724 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007725 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007726 break;
7727 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007728 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007729 break;
7730 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007731 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007732 break;
7733 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007734 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007735 break;
7736 case OO_AmpAmp:
7737 BOK = BO_LAnd;
7738 break;
7739 case OO_PipePipe:
7740 BOK = BO_LOr;
7741 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007742 case OO_New:
7743 case OO_Delete:
7744 case OO_Array_New:
7745 case OO_Array_Delete:
7746 case OO_Slash:
7747 case OO_Percent:
7748 case OO_Tilde:
7749 case OO_Exclaim:
7750 case OO_Equal:
7751 case OO_Less:
7752 case OO_Greater:
7753 case OO_LessEqual:
7754 case OO_GreaterEqual:
7755 case OO_PlusEqual:
7756 case OO_MinusEqual:
7757 case OO_StarEqual:
7758 case OO_SlashEqual:
7759 case OO_PercentEqual:
7760 case OO_CaretEqual:
7761 case OO_AmpEqual:
7762 case OO_PipeEqual:
7763 case OO_LessLess:
7764 case OO_GreaterGreater:
7765 case OO_LessLessEqual:
7766 case OO_GreaterGreaterEqual:
7767 case OO_EqualEqual:
7768 case OO_ExclaimEqual:
7769 case OO_PlusPlus:
7770 case OO_MinusMinus:
7771 case OO_Comma:
7772 case OO_ArrowStar:
7773 case OO_Arrow:
7774 case OO_Call:
7775 case OO_Subscript:
7776 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007777 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007778 case NUM_OVERLOADED_OPERATORS:
7779 llvm_unreachable("Unexpected reduction identifier");
7780 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007781 if (auto II = DN.getAsIdentifierInfo()) {
7782 if (II->isStr("max"))
7783 BOK = BO_GT;
7784 else if (II->isStr("min"))
7785 BOK = BO_LT;
7786 }
7787 break;
7788 }
7789 SourceRange ReductionIdRange;
7790 if (ReductionIdScopeSpec.isValid()) {
7791 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7792 }
7793 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7794 if (BOK == BO_Comma) {
7795 // Not allowed reduction identifier is found.
7796 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7797 << ReductionIdRange;
7798 return nullptr;
7799 }
7800
7801 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007802 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007803 SmallVector<Expr *, 8> LHSs;
7804 SmallVector<Expr *, 8> RHSs;
7805 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007806 for (auto RefExpr : VarList) {
7807 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7808 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7809 // It will be analyzed later.
7810 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007811 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007812 LHSs.push_back(nullptr);
7813 RHSs.push_back(nullptr);
7814 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007815 continue;
7816 }
7817
7818 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7819 RefExpr->isInstantiationDependent() ||
7820 RefExpr->containsUnexpandedParameterPack()) {
7821 // It will be analyzed later.
7822 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007823 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007824 LHSs.push_back(nullptr);
7825 RHSs.push_back(nullptr);
7826 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007827 continue;
7828 }
7829
7830 auto ELoc = RefExpr->getExprLoc();
7831 auto ERange = RefExpr->getSourceRange();
7832 // OpenMP [2.1, C/C++]
7833 // A list item is a variable or array section, subject to the restrictions
7834 // specified in Section 2.4 on page 42 and in each of the sections
7835 // describing clauses and directives for which a list appears.
7836 // OpenMP [2.14.3.3, Restrictions, p.1]
7837 // A variable that is part of another variable (as an array or
7838 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007839 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7840 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7841 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7842 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007843 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7844 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007845 continue;
7846 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007847 QualType Type;
7848 VarDecl *VD = nullptr;
7849 if (DE) {
7850 auto D = DE->getDecl();
7851 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007852 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007853 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007854 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007855 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7856 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7857 Base = TempASE->getBase()->IgnoreParenImpCasts();
7858 DE = dyn_cast<DeclRefExpr>(Base);
7859 if (DE)
7860 VD = dyn_cast<VarDecl>(DE->getDecl());
7861 if (!VD) {
7862 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7863 << 0 << Base->getSourceRange();
7864 continue;
7865 }
7866 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007867 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7868 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7869 Type = ATy->getElementType();
7870 else
7871 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007872 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007873 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7874 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7875 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7876 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7877 Base = TempASE->getBase()->IgnoreParenImpCasts();
7878 DE = dyn_cast<DeclRefExpr>(Base);
7879 if (DE)
7880 VD = dyn_cast<VarDecl>(DE->getDecl());
7881 if (!VD) {
7882 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7883 << 1 << Base->getSourceRange();
7884 continue;
7885 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007886 }
7887
Alexey Bataevc5e02582014-06-16 07:08:35 +00007888 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7889 // A variable that appears in a private clause must not have an incomplete
7890 // type or a reference type.
7891 if (RequireCompleteType(ELoc, Type,
7892 diag::err_omp_reduction_incomplete_type))
7893 continue;
7894 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007895 // A list item that appears in a reduction clause must not be
7896 // const-qualified.
7897 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007898 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007899 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007900 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007901 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7902 VarDecl::DeclarationOnly;
7903 Diag(VD->getLocation(),
7904 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7905 << VD;
7906 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007907 continue;
7908 }
7909 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7910 // If a list-item is a reference type then it must bind to the same object
7911 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007912 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007913 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007914 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007915 DSARefChecker Check(DSAStack);
7916 if (Check.Visit(VDDef->getInit())) {
7917 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7918 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7919 continue;
7920 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007921 }
7922 }
7923 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7924 // The type of a list item that appears in a reduction clause must be valid
7925 // for the reduction-identifier. For a max or min reduction in C, the type
7926 // of the list item must be an allowed arithmetic data type: char, int,
7927 // float, double, or _Bool, possibly modified with long, short, signed, or
7928 // unsigned. For a max or min reduction in C++, the type of the list item
7929 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7930 // double, or bool, possibly modified with long, short, signed, or unsigned.
7931 if ((BOK == BO_GT || BOK == BO_LT) &&
7932 !(Type->isScalarType() ||
7933 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7934 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7935 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007936 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007937 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7938 VarDecl::DeclarationOnly;
7939 Diag(VD->getLocation(),
7940 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7941 << VD;
7942 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007943 continue;
7944 }
7945 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7946 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7947 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007948 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007949 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7950 VarDecl::DeclarationOnly;
7951 Diag(VD->getLocation(),
7952 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7953 << VD;
7954 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007955 continue;
7956 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007957 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7958 // in a Construct]
7959 // Variables with the predetermined data-sharing attributes may not be
7960 // listed in data-sharing attributes clauses, except for the cases
7961 // listed below. For these exceptions only, listing a predetermined
7962 // variable in a data-sharing attribute clause is allowed and overrides
7963 // the variable's predetermined data-sharing attributes.
7964 // OpenMP [2.14.3.6, Restrictions, p.3]
7965 // Any number of reduction clauses can be specified on the directive,
7966 // but a list item can appear only once in the reduction clauses for that
7967 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007968 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007969 DVar = DSAStack->getTopDSA(VD, false);
7970 if (DVar.CKind == OMPC_reduction) {
7971 Diag(ELoc, diag::err_omp_once_referenced)
7972 << getOpenMPClauseName(OMPC_reduction);
7973 if (DVar.RefExpr) {
7974 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007975 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007976 } else if (DVar.CKind != OMPC_unknown) {
7977 Diag(ELoc, diag::err_omp_wrong_dsa)
7978 << getOpenMPClauseName(DVar.CKind)
7979 << getOpenMPClauseName(OMPC_reduction);
7980 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7981 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007982 }
7983
7984 // OpenMP [2.14.3.6, Restrictions, p.1]
7985 // A list item that appears in a reduction clause of a worksharing
7986 // construct must be shared in the parallel regions to which any of the
7987 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007988 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7989 if (isOpenMPWorksharingDirective(CurrDir) &&
7990 !isOpenMPParallelDirective(CurrDir)) {
7991 DVar = DSAStack->getImplicitDSA(VD, true);
7992 if (DVar.CKind != OMPC_shared) {
7993 Diag(ELoc, diag::err_omp_required_access)
7994 << getOpenMPClauseName(OMPC_reduction)
7995 << getOpenMPClauseName(OMPC_shared);
7996 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7997 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007998 }
7999 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008000
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008001 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008002 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
8003 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
8004 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
8005 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
8006 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008007 if (OASE ||
8008 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
8009 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008010 // Create pseudo array type for private copy. The size for this array will
8011 // be generated during codegen.
8012 // For array subscripts or single variables Private Ty is the same as Type
8013 // (type of the variable or single array element).
8014 PrivateTy = Context.getVariableArrayType(
8015 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8016 Context.getSizeType(), VK_RValue),
8017 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00008018 } else if (DE &&
8019 Context.getAsArrayType(VD->getType().getNonReferenceType()))
8020 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008021 // Private copy.
8022 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
8023 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008024 // Add initializer for private variable.
8025 Expr *Init = nullptr;
8026 switch (BOK) {
8027 case BO_Add:
8028 case BO_Xor:
8029 case BO_Or:
8030 case BO_LOr:
8031 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8032 if (Type->isScalarType() || Type->isAnyComplexType()) {
8033 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008034 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008035 break;
8036 case BO_Mul:
8037 case BO_LAnd:
8038 if (Type->isScalarType() || Type->isAnyComplexType()) {
8039 // '*' and '&&' reduction ops - initializer is '1'.
8040 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8041 }
8042 break;
8043 case BO_And: {
8044 // '&' reduction op - initializer is '~0'.
8045 QualType OrigType = Type;
8046 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
8047 Type = ComplexTy->getElementType();
8048 }
8049 if (Type->isRealFloatingType()) {
8050 llvm::APFloat InitValue =
8051 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8052 /*isIEEE=*/true);
8053 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8054 Type, ELoc);
8055 } else if (Type->isScalarType()) {
8056 auto Size = Context.getTypeSize(Type);
8057 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8058 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8059 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8060 }
8061 if (Init && OrigType->isAnyComplexType()) {
8062 // Init = 0xFFFF + 0xFFFFi;
8063 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8064 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8065 }
8066 Type = OrigType;
8067 break;
8068 }
8069 case BO_LT:
8070 case BO_GT: {
8071 // 'min' reduction op - initializer is 'Largest representable number in
8072 // the reduction list item type'.
8073 // 'max' reduction op - initializer is 'Least representable number in
8074 // the reduction list item type'.
8075 if (Type->isIntegerType() || Type->isPointerType()) {
8076 bool IsSigned = Type->hasSignedIntegerRepresentation();
8077 auto Size = Context.getTypeSize(Type);
8078 QualType IntTy =
8079 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8080 llvm::APInt InitValue =
8081 (BOK != BO_LT)
8082 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8083 : llvm::APInt::getMinValue(Size)
8084 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8085 : llvm::APInt::getMaxValue(Size);
8086 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8087 if (Type->isPointerType()) {
8088 // Cast to pointer type.
8089 auto CastExpr = BuildCStyleCastExpr(
8090 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8091 SourceLocation(), Init);
8092 if (CastExpr.isInvalid())
8093 continue;
8094 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008095 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008096 } else if (Type->isRealFloatingType()) {
8097 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8098 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8099 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8100 Type, ELoc);
8101 }
8102 break;
8103 }
8104 case BO_PtrMemD:
8105 case BO_PtrMemI:
8106 case BO_MulAssign:
8107 case BO_Div:
8108 case BO_Rem:
8109 case BO_Sub:
8110 case BO_Shl:
8111 case BO_Shr:
8112 case BO_LE:
8113 case BO_GE:
8114 case BO_EQ:
8115 case BO_NE:
8116 case BO_AndAssign:
8117 case BO_XorAssign:
8118 case BO_OrAssign:
8119 case BO_Assign:
8120 case BO_AddAssign:
8121 case BO_SubAssign:
8122 case BO_DivAssign:
8123 case BO_RemAssign:
8124 case BO_ShlAssign:
8125 case BO_ShrAssign:
8126 case BO_Comma:
8127 llvm_unreachable("Unexpected reduction operation");
8128 }
8129 if (Init) {
8130 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8131 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008132 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008133 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008134 if (!RHSVD->hasInit()) {
8135 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8136 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008137 if (VD) {
8138 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8139 VarDecl::DeclarationOnly;
8140 Diag(VD->getLocation(),
8141 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8142 << VD;
8143 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008144 continue;
8145 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008146 // Store initializer for single element in private copy. Will be used during
8147 // codegen.
8148 PrivateVD->setInit(RHSVD->getInit());
8149 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008150 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8151 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008152 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008153 ExprResult ReductionOp =
8154 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8155 LHSDRE, RHSDRE);
8156 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008157 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008158 ReductionOp =
8159 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8160 BO_Assign, LHSDRE, ReductionOp.get());
8161 } else {
8162 auto *ConditionalOp = new (Context) ConditionalOperator(
8163 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8164 RHSDRE, Type, VK_LValue, OK_Ordinary);
8165 ReductionOp =
8166 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8167 BO_Assign, LHSDRE, ConditionalOp);
8168 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008169 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008170 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008171 if (ReductionOp.isInvalid())
8172 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008173
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008174 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008175 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008176 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008177 LHSs.push_back(LHSDRE);
8178 RHSs.push_back(RHSDRE);
8179 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008180 }
8181
8182 if (Vars.empty())
8183 return nullptr;
8184
8185 return OMPReductionClause::Create(
8186 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008187 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8188 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008189}
8190
Alexey Bataev182227b2015-08-20 10:54:39 +00008191OMPClause *Sema::ActOnOpenMPLinearClause(
8192 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8193 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8194 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008195 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008196 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008197 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008198 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8199 LinKind == OMPC_LINEAR_unknown) {
8200 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8201 LinKind = OMPC_LINEAR_val;
8202 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008203 for (auto &RefExpr : VarList) {
8204 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8205 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008206 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008207 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008208 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008209 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008210 continue;
8211 }
8212
8213 // OpenMP [2.14.3.7, linear clause]
8214 // A list item that appears in a linear clause is subject to the private
8215 // clause semantics described in Section 2.14.3.3 on page 159 except as
8216 // noted. In addition, the value of the new list item on each iteration
8217 // of the associated loop(s) corresponds to the value of the original
8218 // list item before entering the construct plus the logical number of
8219 // the iteration times linear-step.
8220
Alexey Bataeved09d242014-05-28 05:53:51 +00008221 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008222 // OpenMP [2.1, C/C++]
8223 // A list item is a variable name.
8224 // OpenMP [2.14.3.3, Restrictions, p.1]
8225 // A variable that is part of another variable (as an array or
8226 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008227 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008228 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008229 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8230 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008231 continue;
8232 }
8233
8234 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8235
8236 // OpenMP [2.14.3.7, linear clause]
8237 // A list-item cannot appear in more than one linear clause.
8238 // A list-item that appears in a linear clause cannot appear in any
8239 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008240 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008241 if (DVar.RefExpr) {
8242 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8243 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008244 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008245 continue;
8246 }
8247
8248 QualType QType = VD->getType();
8249 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8250 // It will be analyzed later.
8251 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008252 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008253 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008254 continue;
8255 }
8256
8257 // A variable must not have an incomplete type or a reference type.
8258 if (RequireCompleteType(ELoc, QType,
8259 diag::err_omp_linear_incomplete_type)) {
8260 continue;
8261 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008262 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8263 !QType->isReferenceType()) {
8264 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8265 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8266 continue;
8267 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008268 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008269
8270 // A list item must not be const-qualified.
8271 if (QType.isConstant(Context)) {
8272 Diag(ELoc, diag::err_omp_const_variable)
8273 << getOpenMPClauseName(OMPC_linear);
8274 bool IsDecl =
8275 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8276 Diag(VD->getLocation(),
8277 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8278 << VD;
8279 continue;
8280 }
8281
8282 // A list item must be of integral or pointer type.
8283 QType = QType.getUnqualifiedType().getCanonicalType();
8284 const Type *Ty = QType.getTypePtrOrNull();
8285 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8286 !Ty->isPointerType())) {
8287 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8288 bool IsDecl =
8289 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8290 Diag(VD->getLocation(),
8291 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8292 << VD;
8293 continue;
8294 }
8295
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008296 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008297 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8298 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008299 auto *PrivateRef = buildDeclRefExpr(
8300 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008301 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008302 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008303 Expr *InitExpr;
8304 if (LinKind == OMPC_LINEAR_uval)
8305 InitExpr = VD->getInit();
8306 else
8307 InitExpr = DE;
8308 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008309 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008310 auto InitRef = buildDeclRefExpr(
8311 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008312 DSAStack->addDSA(VD, DE, OMPC_linear);
8313 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008314 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008315 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008316 }
8317
8318 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008319 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008320
8321 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008322 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008323 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8324 !Step->isInstantiationDependent() &&
8325 !Step->containsUnexpandedParameterPack()) {
8326 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008327 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008328 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008329 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008330 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008331
Alexander Musman3276a272015-03-21 10:12:56 +00008332 // Build var to save the step value.
8333 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008334 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008335 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008336 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008337 ExprResult CalcStep =
8338 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008339 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008340
Alexander Musman8dba6642014-04-22 13:09:42 +00008341 // Warn about zero linear step (it would be probably better specified as
8342 // making corresponding variables 'const').
8343 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008344 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8345 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008346 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8347 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008348 if (!IsConstant && CalcStep.isUsable()) {
8349 // Calculate the step beforehand instead of doing this on each iteration.
8350 // (This is not used if the number of iterations may be kfold-ed).
8351 CalcStepExpr = CalcStep.get();
8352 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008353 }
8354
Alexey Bataev182227b2015-08-20 10:54:39 +00008355 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8356 ColonLoc, EndLoc, Vars, Privates, Inits,
8357 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008358}
8359
8360static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8361 Expr *NumIterations, Sema &SemaRef,
8362 Scope *S) {
8363 // Walk the vars and build update/final expressions for the CodeGen.
8364 SmallVector<Expr *, 8> Updates;
8365 SmallVector<Expr *, 8> Finals;
8366 Expr *Step = Clause.getStep();
8367 Expr *CalcStep = Clause.getCalcStep();
8368 // OpenMP [2.14.3.7, linear clause]
8369 // If linear-step is not specified it is assumed to be 1.
8370 if (Step == nullptr)
8371 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8372 else if (CalcStep)
8373 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8374 bool HasErrors = false;
8375 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008376 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008377 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008378 for (auto &RefExpr : Clause.varlists()) {
8379 Expr *InitExpr = *CurInit;
8380
8381 // Build privatized reference to the current linear var.
8382 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008383 Expr *CapturedRef;
8384 if (LinKind == OMPC_LINEAR_uval)
8385 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8386 else
8387 CapturedRef =
8388 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8389 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8390 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008391
8392 // Build update: Var = InitExpr + IV * Step
8393 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008394 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008395 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008396 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8397 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008398
8399 // Build final: Var = InitExpr + NumIterations * Step
8400 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008401 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008402 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008403 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8404 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008405 if (!Update.isUsable() || !Final.isUsable()) {
8406 Updates.push_back(nullptr);
8407 Finals.push_back(nullptr);
8408 HasErrors = true;
8409 } else {
8410 Updates.push_back(Update.get());
8411 Finals.push_back(Final.get());
8412 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008413 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008414 }
8415 Clause.setUpdates(Updates);
8416 Clause.setFinals(Finals);
8417 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008418}
8419
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008420OMPClause *Sema::ActOnOpenMPAlignedClause(
8421 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8422 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8423
8424 SmallVector<Expr *, 8> Vars;
8425 for (auto &RefExpr : VarList) {
8426 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8427 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8428 // It will be analyzed later.
8429 Vars.push_back(RefExpr);
8430 continue;
8431 }
8432
8433 SourceLocation ELoc = RefExpr->getExprLoc();
8434 // OpenMP [2.1, C/C++]
8435 // A list item is a variable name.
8436 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8437 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008438 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8439 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008440 continue;
8441 }
8442
8443 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8444
8445 // OpenMP [2.8.1, simd construct, Restrictions]
8446 // The type of list items appearing in the aligned clause must be
8447 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008448 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008449 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008450 const Type *Ty = QType.getTypePtrOrNull();
8451 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8452 !Ty->isPointerType())) {
8453 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8454 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8455 bool IsDecl =
8456 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8457 Diag(VD->getLocation(),
8458 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8459 << VD;
8460 continue;
8461 }
8462
8463 // OpenMP [2.8.1, simd construct, Restrictions]
8464 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008465 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008466 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8467 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8468 << getOpenMPClauseName(OMPC_aligned);
8469 continue;
8470 }
8471
8472 Vars.push_back(DE);
8473 }
8474
8475 // OpenMP [2.8.1, simd construct, Description]
8476 // The parameter of the aligned clause, alignment, must be a constant
8477 // positive integer expression.
8478 // If no optional parameter is specified, implementation-defined default
8479 // alignments for SIMD instructions on the target platforms are assumed.
8480 if (Alignment != nullptr) {
8481 ExprResult AlignResult =
8482 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8483 if (AlignResult.isInvalid())
8484 return nullptr;
8485 Alignment = AlignResult.get();
8486 }
8487 if (Vars.empty())
8488 return nullptr;
8489
8490 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8491 EndLoc, Vars, Alignment);
8492}
8493
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008494OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8495 SourceLocation StartLoc,
8496 SourceLocation LParenLoc,
8497 SourceLocation EndLoc) {
8498 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008499 SmallVector<Expr *, 8> SrcExprs;
8500 SmallVector<Expr *, 8> DstExprs;
8501 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008502 for (auto &RefExpr : VarList) {
8503 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8504 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008505 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008506 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008507 SrcExprs.push_back(nullptr);
8508 DstExprs.push_back(nullptr);
8509 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008510 continue;
8511 }
8512
Alexey Bataeved09d242014-05-28 05:53:51 +00008513 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008514 // OpenMP [2.1, C/C++]
8515 // A list item is a variable name.
8516 // OpenMP [2.14.4.1, Restrictions, p.1]
8517 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008518 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008519 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008520 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8521 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008522 continue;
8523 }
8524
8525 Decl *D = DE->getDecl();
8526 VarDecl *VD = cast<VarDecl>(D);
8527
8528 QualType Type = VD->getType();
8529 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8530 // It will be analyzed later.
8531 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008532 SrcExprs.push_back(nullptr);
8533 DstExprs.push_back(nullptr);
8534 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008535 continue;
8536 }
8537
8538 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8539 // A list item that appears in a copyin clause must be threadprivate.
8540 if (!DSAStack->isThreadPrivate(VD)) {
8541 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008542 << getOpenMPClauseName(OMPC_copyin)
8543 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008544 continue;
8545 }
8546
8547 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8548 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008549 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008550 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008551 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008552 auto *SrcVD =
8553 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8554 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008555 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008556 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8557 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008558 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8559 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008560 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008561 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008562 // For arrays generate assignment operation for single element and replace
8563 // it by the original array element in CodeGen.
8564 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8565 PseudoDstExpr, PseudoSrcExpr);
8566 if (AssignmentOp.isInvalid())
8567 continue;
8568 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8569 /*DiscardedValue=*/true);
8570 if (AssignmentOp.isInvalid())
8571 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008572
8573 DSAStack->addDSA(VD, DE, OMPC_copyin);
8574 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008575 SrcExprs.push_back(PseudoSrcExpr);
8576 DstExprs.push_back(PseudoDstExpr);
8577 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008578 }
8579
Alexey Bataeved09d242014-05-28 05:53:51 +00008580 if (Vars.empty())
8581 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008582
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008583 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8584 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008585}
8586
Alexey Bataevbae9a792014-06-27 10:37:06 +00008587OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8588 SourceLocation StartLoc,
8589 SourceLocation LParenLoc,
8590 SourceLocation EndLoc) {
8591 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008592 SmallVector<Expr *, 8> SrcExprs;
8593 SmallVector<Expr *, 8> DstExprs;
8594 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008595 for (auto &RefExpr : VarList) {
8596 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8597 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8598 // It will be analyzed later.
8599 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008600 SrcExprs.push_back(nullptr);
8601 DstExprs.push_back(nullptr);
8602 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008603 continue;
8604 }
8605
8606 SourceLocation ELoc = RefExpr->getExprLoc();
8607 // OpenMP [2.1, C/C++]
8608 // A list item is a variable name.
8609 // OpenMP [2.14.4.1, Restrictions, p.1]
8610 // A list item that appears in a copyin clause must be threadprivate.
8611 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8612 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008613 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8614 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008615 continue;
8616 }
8617
8618 Decl *D = DE->getDecl();
8619 VarDecl *VD = cast<VarDecl>(D);
8620
8621 QualType Type = VD->getType();
8622 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8623 // It will be analyzed later.
8624 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008625 SrcExprs.push_back(nullptr);
8626 DstExprs.push_back(nullptr);
8627 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008628 continue;
8629 }
8630
8631 // OpenMP [2.14.4.2, Restrictions, p.2]
8632 // A list item that appears in a copyprivate clause may not appear in a
8633 // private or firstprivate clause on the single construct.
8634 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008635 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008636 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8637 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008638 Diag(ELoc, diag::err_omp_wrong_dsa)
8639 << getOpenMPClauseName(DVar.CKind)
8640 << getOpenMPClauseName(OMPC_copyprivate);
8641 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8642 continue;
8643 }
8644
8645 // OpenMP [2.11.4.2, Restrictions, p.1]
8646 // All list items that appear in a copyprivate clause must be either
8647 // threadprivate or private in the enclosing context.
8648 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008649 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008650 if (DVar.CKind == OMPC_shared) {
8651 Diag(ELoc, diag::err_omp_required_access)
8652 << getOpenMPClauseName(OMPC_copyprivate)
8653 << "threadprivate or private in the enclosing context";
8654 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8655 continue;
8656 }
8657 }
8658 }
8659
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008660 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008661 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008662 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008663 << getOpenMPClauseName(OMPC_copyprivate) << Type
8664 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008665 bool IsDecl =
8666 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8667 Diag(VD->getLocation(),
8668 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8669 << VD;
8670 continue;
8671 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008672
Alexey Bataevbae9a792014-06-27 10:37:06 +00008673 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8674 // A variable of class type (or array thereof) that appears in a
8675 // copyin clause requires an accessible, unambiguous copy assignment
8676 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008677 Type = Context.getBaseElementType(Type.getNonReferenceType())
8678 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008679 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008680 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8681 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008682 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008683 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008684 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008685 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8686 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008687 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008688 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008689 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8690 PseudoDstExpr, PseudoSrcExpr);
8691 if (AssignmentOp.isInvalid())
8692 continue;
8693 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8694 /*DiscardedValue=*/true);
8695 if (AssignmentOp.isInvalid())
8696 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008697
8698 // No need to mark vars as copyprivate, they are already threadprivate or
8699 // implicitly private.
8700 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008701 SrcExprs.push_back(PseudoSrcExpr);
8702 DstExprs.push_back(PseudoDstExpr);
8703 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008704 }
8705
8706 if (Vars.empty())
8707 return nullptr;
8708
Alexey Bataeva63048e2015-03-23 06:18:07 +00008709 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8710 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008711}
8712
Alexey Bataev6125da92014-07-21 11:26:11 +00008713OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8714 SourceLocation StartLoc,
8715 SourceLocation LParenLoc,
8716 SourceLocation EndLoc) {
8717 if (VarList.empty())
8718 return nullptr;
8719
8720 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8721}
Alexey Bataevdea47612014-07-23 07:46:59 +00008722
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008723OMPClause *
8724Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8725 SourceLocation DepLoc, SourceLocation ColonLoc,
8726 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8727 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008728 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008729 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008730 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008731 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008732 return nullptr;
8733 }
8734 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008735 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8736 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008737 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008738 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008739 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8740 /*Last=*/OMPC_DEPEND_unknown, Except)
8741 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008742 return nullptr;
8743 }
8744 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008745 llvm::APSInt DepCounter(/*BitWidth=*/32);
8746 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8747 if (DepKind == OMPC_DEPEND_sink) {
8748 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8749 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8750 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008751 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008752 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008753 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8754 DSAStack->getParentOrderedRegionParam()) {
8755 for (auto &RefExpr : VarList) {
8756 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8757 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8758 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8759 // It will be analyzed later.
8760 Vars.push_back(RefExpr);
8761 continue;
8762 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008763
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008764 SourceLocation ELoc = RefExpr->getExprLoc();
8765 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8766 if (DepKind == OMPC_DEPEND_sink) {
8767 if (DepCounter >= TotalDepCount) {
8768 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8769 continue;
8770 }
8771 ++DepCounter;
8772 // OpenMP [2.13.9, Summary]
8773 // depend(dependence-type : vec), where dependence-type is:
8774 // 'sink' and where vec is the iteration vector, which has the form:
8775 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8776 // where n is the value specified by the ordered clause in the loop
8777 // directive, xi denotes the loop iteration variable of the i-th nested
8778 // loop associated with the loop directive, and di is a constant
8779 // non-negative integer.
8780 SimpleExpr = SimpleExpr->IgnoreImplicit();
8781 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8782 if (!DE) {
8783 OverloadedOperatorKind OOK = OO_None;
8784 SourceLocation OOLoc;
8785 Expr *LHS, *RHS;
8786 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8787 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8788 OOLoc = BO->getOperatorLoc();
8789 LHS = BO->getLHS()->IgnoreParenImpCasts();
8790 RHS = BO->getRHS()->IgnoreParenImpCasts();
8791 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8792 OOK = OCE->getOperator();
8793 OOLoc = OCE->getOperatorLoc();
8794 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8795 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8796 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8797 OOK = MCE->getMethodDecl()
8798 ->getNameInfo()
8799 .getName()
8800 .getCXXOverloadedOperator();
8801 OOLoc = MCE->getCallee()->getExprLoc();
8802 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8803 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8804 } else {
8805 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8806 continue;
8807 }
8808 DE = dyn_cast<DeclRefExpr>(LHS);
8809 if (!DE) {
8810 Diag(LHS->getExprLoc(),
8811 diag::err_omp_depend_sink_expected_loop_iteration)
8812 << DSAStack->getParentLoopControlVariable(
8813 DepCounter.getZExtValue());
8814 continue;
8815 }
8816 if (OOK != OO_Plus && OOK != OO_Minus) {
8817 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8818 continue;
8819 }
8820 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8821 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8822 if (Res.isInvalid())
8823 continue;
8824 }
8825 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8826 if (!CurContext->isDependentContext() &&
8827 DSAStack->getParentOrderedRegionParam() &&
8828 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8829 Diag(DE->getExprLoc(),
8830 diag::err_omp_depend_sink_expected_loop_iteration)
8831 << DSAStack->getParentLoopControlVariable(
8832 DepCounter.getZExtValue());
8833 continue;
8834 }
8835 } else {
8836 // OpenMP [2.11.1.1, Restrictions, p.3]
8837 // A variable that is part of another variable (such as a field of a
8838 // structure) but is not an array element or an array section cannot
8839 // appear in a depend clause.
8840 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8841 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8842 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8843 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8844 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008845 (ASE &&
8846 !ASE->getBase()
8847 ->getType()
8848 .getNonReferenceType()
8849 ->isPointerType() &&
8850 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008851 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8852 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008853 continue;
8854 }
8855 }
8856
8857 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8858 }
8859
8860 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8861 TotalDepCount > VarList.size() &&
8862 DSAStack->getParentOrderedRegionParam()) {
8863 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8864 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8865 }
8866 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8867 Vars.empty())
8868 return nullptr;
8869 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008870
8871 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8872 DepLoc, ColonLoc, Vars);
8873}
Michael Wonge710d542015-08-07 16:16:36 +00008874
8875OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8876 SourceLocation LParenLoc,
8877 SourceLocation EndLoc) {
8878 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008879
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008880 // OpenMP [2.9.1, Restrictions]
8881 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008882 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8883 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008884 return nullptr;
8885
Michael Wonge710d542015-08-07 16:16:36 +00008886 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8887}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008888
8889static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8890 DSAStackTy *Stack, CXXRecordDecl *RD) {
8891 if (!RD || RD->isInvalidDecl())
8892 return true;
8893
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008894 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8895 if (auto *CTD = CTSD->getSpecializedTemplate())
8896 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008897 auto QTy = SemaRef.Context.getRecordType(RD);
8898 if (RD->isDynamicClass()) {
8899 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8900 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8901 return false;
8902 }
8903 auto *DC = RD;
8904 bool IsCorrect = true;
8905 for (auto *I : DC->decls()) {
8906 if (I) {
8907 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8908 if (MD->isStatic()) {
8909 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8910 SemaRef.Diag(MD->getLocation(),
8911 diag::note_omp_static_member_in_target);
8912 IsCorrect = false;
8913 }
8914 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8915 if (VD->isStaticDataMember()) {
8916 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8917 SemaRef.Diag(VD->getLocation(),
8918 diag::note_omp_static_member_in_target);
8919 IsCorrect = false;
8920 }
8921 }
8922 }
8923 }
8924
8925 for (auto &I : RD->bases()) {
8926 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8927 I.getType()->getAsCXXRecordDecl()))
8928 IsCorrect = false;
8929 }
8930 return IsCorrect;
8931}
8932
8933static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8934 DSAStackTy *Stack, QualType QTy) {
8935 NamedDecl *ND;
8936 if (QTy->isIncompleteType(&ND)) {
8937 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8938 return false;
8939 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8940 if (!RD->isInvalidDecl() &&
8941 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8942 return false;
8943 }
8944 return true;
8945}
8946
Samuel Antao5de996e2016-01-22 20:21:36 +00008947// Return the expression of the base of the map clause or null if it cannot
8948// be determined and do all the necessary checks to see if the expression is
8949// valid as a standalone map clause expression.
8950static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8951 SourceLocation ELoc = E->getExprLoc();
8952 SourceRange ERange = E->getSourceRange();
8953
8954 // The base of elements of list in a map clause have to be either:
8955 // - a reference to variable or field.
8956 // - a member expression.
8957 // - an array expression.
8958 //
8959 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8960 // reference to 'r'.
8961 //
8962 // If we have:
8963 //
8964 // struct SS {
8965 // Bla S;
8966 // foo() {
8967 // #pragma omp target map (S.Arr[:12]);
8968 // }
8969 // }
8970 //
8971 // We want to retrieve the member expression 'this->S';
8972
8973 Expr *RelevantExpr = nullptr;
8974
8975 // Flags to help capture some memory
8976
8977 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8978 // If a list item is an array section, it must specify contiguous storage.
8979 //
8980 // For this restriction it is sufficient that we make sure only references
8981 // to variables or fields and array expressions, and that no array sections
8982 // exist except in the rightmost expression. E.g. these would be invalid:
8983 //
8984 // r.ArrS[3:5].Arr[6:7]
8985 //
8986 // r.ArrS[3:5].x
8987 //
8988 // but these would be valid:
8989 // r.ArrS[3].Arr[6:7]
8990 //
8991 // r.ArrS[3].x
8992
8993 bool IsRightMostExpression = true;
8994
8995 while (!RelevantExpr) {
8996 auto AllowArraySection = IsRightMostExpression;
8997 IsRightMostExpression = false;
8998
8999 E = E->IgnoreParenImpCasts();
9000
9001 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9002 if (!isa<VarDecl>(CurE->getDecl()))
9003 break;
9004
9005 RelevantExpr = CurE;
9006 continue;
9007 }
9008
9009 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9010 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9011
9012 if (isa<CXXThisExpr>(BaseE))
9013 // We found a base expression: this->Val.
9014 RelevantExpr = CurE;
9015 else
9016 E = BaseE;
9017
9018 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9019 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9020 << CurE->getSourceRange();
9021 break;
9022 }
9023
9024 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9025
9026 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9027 // A bit-field cannot appear in a map clause.
9028 //
9029 if (FD->isBitField()) {
9030 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9031 << CurE->getSourceRange();
9032 break;
9033 }
9034
9035 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9036 // If the type of a list item is a reference to a type T then the type
9037 // will be considered to be T for all purposes of this clause.
9038 QualType CurType = BaseE->getType();
9039 if (CurType->isReferenceType())
9040 CurType = CurType->getPointeeType();
9041
9042 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9043 // A list item cannot be a variable that is a member of a structure with
9044 // a union type.
9045 //
9046 if (auto *RT = CurType->getAs<RecordType>())
9047 if (RT->isUnionType()) {
9048 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9049 << CurE->getSourceRange();
9050 break;
9051 }
9052
9053 continue;
9054 }
9055
9056 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9057 E = CurE->getBase()->IgnoreParenImpCasts();
9058
9059 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9060 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9061 << 0 << CurE->getSourceRange();
9062 break;
9063 }
9064 continue;
9065 }
9066
9067 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9068 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9069 // If a list item is an element of a structure, only the rightmost symbol
9070 // of the variable reference can be an array section.
9071 //
9072 if (!AllowArraySection) {
9073 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9074 << CurE->getSourceRange();
9075 break;
9076 }
9077
9078 E = CurE->getBase()->IgnoreParenImpCasts();
9079
9080 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9081 // If the type of a list item is a reference to a type T then the type
9082 // will be considered to be T for all purposes of this clause.
9083 QualType CurType = E->getType();
9084 if (CurType->isReferenceType())
9085 CurType = CurType->getPointeeType();
9086
9087 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9088 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9089 << 0 << CurE->getSourceRange();
9090 break;
9091 }
9092
9093 continue;
9094 }
9095
9096 // If nothing else worked, this is not a valid map clause expression.
9097 SemaRef.Diag(ELoc,
9098 diag::err_omp_expected_named_var_member_or_array_expression)
9099 << ERange;
9100 break;
9101 }
9102
9103 return RelevantExpr;
9104}
9105
9106// Return true if expression E associated with value VD has conflicts with other
9107// map information.
9108static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9109 Expr *E, bool CurrentRegionOnly) {
9110 assert(VD && E);
9111
9112 // Types used to organize the components of a valid map clause.
9113 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9114 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9115
9116 // Helper to extract the components in the map clause expression E and store
9117 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9118 // it has already passed the single clause checks.
9119 auto ExtractMapExpressionComponents = [](Expr *TE,
9120 MapExpressionComponents &MEC) {
9121 while (true) {
9122 TE = TE->IgnoreParenImpCasts();
9123
9124 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9125 MEC.push_back(
9126 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9127 break;
9128 }
9129
9130 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9131 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9132
9133 MEC.push_back(MapExpressionComponent(
9134 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9135 if (isa<CXXThisExpr>(BaseE))
9136 break;
9137
9138 TE = BaseE;
9139 continue;
9140 }
9141
9142 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9143 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9144 TE = CurE->getBase()->IgnoreParenImpCasts();
9145 continue;
9146 }
9147
9148 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9149 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9150 TE = CurE->getBase()->IgnoreParenImpCasts();
9151 continue;
9152 }
9153
9154 llvm_unreachable(
9155 "Expecting only valid map clause expressions at this point!");
9156 }
9157 };
9158
9159 SourceLocation ELoc = E->getExprLoc();
9160 SourceRange ERange = E->getSourceRange();
9161
9162 // In order to easily check the conflicts we need to match each component of
9163 // the expression under test with the components of the expressions that are
9164 // already in the stack.
9165
9166 MapExpressionComponents CurComponents;
9167 ExtractMapExpressionComponents(E, CurComponents);
9168
9169 assert(!CurComponents.empty() && "Map clause expression with no components!");
9170 assert(CurComponents.back().second == VD &&
9171 "Map clause expression with unexpected base!");
9172
9173 // Variables to help detecting enclosing problems in data environment nests.
9174 bool IsEnclosedByDataEnvironmentExpr = false;
9175 Expr *EnclosingExpr = nullptr;
9176
9177 bool FoundError =
9178 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9179 MapExpressionComponents StackComponents;
9180 ExtractMapExpressionComponents(RE, StackComponents);
9181 assert(!StackComponents.empty() &&
9182 "Map clause expression with no components!");
9183 assert(StackComponents.back().second == VD &&
9184 "Map clause expression with unexpected base!");
9185
9186 // Expressions must start from the same base. Here we detect at which
9187 // point both expressions diverge from each other and see if we can
9188 // detect if the memory referred to both expressions is contiguous and
9189 // do not overlap.
9190 auto CI = CurComponents.rbegin();
9191 auto CE = CurComponents.rend();
9192 auto SI = StackComponents.rbegin();
9193 auto SE = StackComponents.rend();
9194 for (; CI != CE && SI != SE; ++CI, ++SI) {
9195
9196 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9197 // At most one list item can be an array item derived from a given
9198 // variable in map clauses of the same construct.
9199 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9200 isa<OMPArraySectionExpr>(CI->first)) &&
9201 (isa<ArraySubscriptExpr>(SI->first) ||
9202 isa<OMPArraySectionExpr>(SI->first))) {
9203 SemaRef.Diag(CI->first->getExprLoc(),
9204 diag::err_omp_multiple_array_items_in_map_clause)
9205 << CI->first->getSourceRange();
9206 ;
9207 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9208 << SI->first->getSourceRange();
9209 return true;
9210 }
9211
9212 // Do both expressions have the same kind?
9213 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9214 break;
9215
9216 // Are we dealing with different variables/fields?
9217 if (CI->second != SI->second)
9218 break;
9219 }
9220
9221 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9222 // List items of map clauses in the same construct must not share
9223 // original storage.
9224 //
9225 // If the expressions are exactly the same or one is a subset of the
9226 // other, it means they are sharing storage.
9227 if (CI == CE && SI == SE) {
9228 if (CurrentRegionOnly) {
9229 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9230 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9231 << RE->getSourceRange();
9232 return true;
9233 } else {
9234 // If we find the same expression in the enclosing data environment,
9235 // that is legal.
9236 IsEnclosedByDataEnvironmentExpr = true;
9237 return false;
9238 }
9239 }
9240
9241 QualType DerivedType = std::prev(CI)->first->getType();
9242 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9243
9244 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9245 // If the type of a list item is a reference to a type T then the type
9246 // will be considered to be T for all purposes of this clause.
9247 if (DerivedType->isReferenceType())
9248 DerivedType = DerivedType->getPointeeType();
9249
9250 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9251 // A variable for which the type is pointer and an array section
9252 // derived from that variable must not appear as list items of map
9253 // clauses of the same construct.
9254 //
9255 // Also, cover one of the cases in:
9256 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9257 // If any part of the original storage of a list item has corresponding
9258 // storage in the device data environment, all of the original storage
9259 // must have corresponding storage in the device data environment.
9260 //
9261 if (DerivedType->isAnyPointerType()) {
9262 if (CI == CE || SI == SE) {
9263 SemaRef.Diag(
9264 DerivedLoc,
9265 diag::err_omp_pointer_mapped_along_with_derived_section)
9266 << DerivedLoc;
9267 } else {
9268 assert(CI != CE && SI != SE);
9269 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9270 << DerivedLoc;
9271 }
9272 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9273 << RE->getSourceRange();
9274 return true;
9275 }
9276
9277 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9278 // List items of map clauses in the same construct must not share
9279 // original storage.
9280 //
9281 // An expression is a subset of the other.
9282 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9283 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9284 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9285 << RE->getSourceRange();
9286 return true;
9287 }
9288
9289 // The current expression uses the same base as other expression in the
9290 // data environment but does not contain it completelly.
9291 if (!CurrentRegionOnly && SI != SE)
9292 EnclosingExpr = RE;
9293
9294 // The current expression is a subset of the expression in the data
9295 // environment.
9296 IsEnclosedByDataEnvironmentExpr |=
9297 (!CurrentRegionOnly && CI != CE && SI == SE);
9298
9299 return false;
9300 });
9301
9302 if (CurrentRegionOnly)
9303 return FoundError;
9304
9305 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9306 // If any part of the original storage of a list item has corresponding
9307 // storage in the device data environment, all of the original storage must
9308 // have corresponding storage in the device data environment.
9309 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9310 // If a list item is an element of a structure, and a different element of
9311 // the structure has a corresponding list item in the device data environment
9312 // prior to a task encountering the construct associated with the map clause,
9313 // then the list item must also have a correspnding list item in the device
9314 // data environment prior to the task encountering the construct.
9315 //
9316 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9317 SemaRef.Diag(ELoc,
9318 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9319 << ERange;
9320 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9321 << EnclosingExpr->getSourceRange();
9322 return true;
9323 }
9324
9325 return FoundError;
9326}
9327
Samuel Antao23abd722016-01-19 20:40:49 +00009328OMPClause *
9329Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9330 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9331 SourceLocation MapLoc, SourceLocation ColonLoc,
9332 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9333 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009334 SmallVector<Expr *, 4> Vars;
9335
9336 for (auto &RE : VarList) {
9337 assert(RE && "Null expr in omp map");
9338 if (isa<DependentScopeDeclRefExpr>(RE)) {
9339 // It will be analyzed later.
9340 Vars.push_back(RE);
9341 continue;
9342 }
9343 SourceLocation ELoc = RE->getExprLoc();
9344
Kelvin Li0bff7af2015-11-23 05:32:03 +00009345 auto *VE = RE->IgnoreParenLValueCasts();
9346
9347 if (VE->isValueDependent() || VE->isTypeDependent() ||
9348 VE->isInstantiationDependent() ||
9349 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009350 // We can only analyze this information once the missing information is
9351 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009352 Vars.push_back(RE);
9353 continue;
9354 }
9355
9356 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009357
Samuel Antao5de996e2016-01-22 20:21:36 +00009358 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9359 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9360 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009361 continue;
9362 }
9363
Samuel Antao5de996e2016-01-22 20:21:36 +00009364 // Obtain the array or member expression bases if required.
9365 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9366 if (!BE)
9367 continue;
9368
9369 // If the base is a reference to a variable, we rely on that variable for
9370 // the following checks. If it is a 'this' expression we rely on the field.
9371 ValueDecl *D = nullptr;
9372 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9373 D = DRE->getDecl();
9374 } else {
9375 auto *ME = cast<MemberExpr>(BE);
9376 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9377 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009378 }
9379 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009380
Samuel Antao5de996e2016-01-22 20:21:36 +00009381 auto *VD = dyn_cast<VarDecl>(D);
9382 auto *FD = dyn_cast<FieldDecl>(D);
9383
9384 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009385 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009386
9387 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9388 // threadprivate variables cannot appear in a map clause.
9389 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009390 auto DVar = DSAStack->getTopDSA(VD, false);
9391 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9392 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9393 continue;
9394 }
9395
Samuel Antao5de996e2016-01-22 20:21:36 +00009396 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9397 // A list item cannot appear in both a map clause and a data-sharing
9398 // attribute clause on the same construct.
9399 //
9400 // TODO: Implement this check - it cannot currently be tested because of
9401 // missing implementation of the other data sharing clauses in target
9402 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009403
Samuel Antao5de996e2016-01-22 20:21:36 +00009404 // Check conflicts with other map clause expressions. We check the conflicts
9405 // with the current construct separately from the enclosing data
9406 // environment, because the restrictions are different.
9407 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9408 /*CurrentRegionOnly=*/true))
9409 break;
9410 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9411 /*CurrentRegionOnly=*/false))
9412 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009413
Samuel Antao5de996e2016-01-22 20:21:36 +00009414 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9415 // If the type of a list item is a reference to a type T then the type will
9416 // be considered to be T for all purposes of this clause.
9417 QualType Type = D->getType();
9418 if (Type->isReferenceType())
9419 Type = Type->getPointeeType();
9420
9421 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009422 // A list item must have a mappable type.
9423 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9424 DSAStack, Type))
9425 continue;
9426
Samuel Antaodf67fc42016-01-19 19:15:56 +00009427 // target enter data
9428 // OpenMP [2.10.2, Restrictions, p. 99]
9429 // A map-type must be specified in all map clauses and must be either
9430 // to or alloc.
9431 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9432 if (DKind == OMPD_target_enter_data &&
9433 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9434 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009435 << (IsMapTypeImplicit ? 1 : 0)
9436 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009437 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009438 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009439 }
9440
Samuel Antao72590762016-01-19 20:04:50 +00009441 // target exit_data
9442 // OpenMP [2.10.3, Restrictions, p. 102]
9443 // A map-type must be specified in all map clauses and must be either
9444 // from, release, or delete.
9445 DKind = DSAStack->getCurrentDirective();
9446 if (DKind == OMPD_target_exit_data &&
9447 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9448 MapType == OMPC_MAP_delete)) {
9449 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009450 << (IsMapTypeImplicit ? 1 : 0)
9451 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009452 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009453 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009454 }
9455
Kelvin Li0bff7af2015-11-23 05:32:03 +00009456 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009457 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009458 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009459
Samuel Antao5de996e2016-01-22 20:21:36 +00009460 // We need to produce a map clause even if we don't have variables so that
9461 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009462 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009463 MapTypeModifier, MapType, IsMapTypeImplicit,
9464 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009465}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009466
9467OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9468 SourceLocation StartLoc,
9469 SourceLocation LParenLoc,
9470 SourceLocation EndLoc) {
9471 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009472
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009473 // OpenMP [teams Constrcut, Restrictions]
9474 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009475 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9476 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009477 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009478
9479 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9480}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009481
9482OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9483 SourceLocation StartLoc,
9484 SourceLocation LParenLoc,
9485 SourceLocation EndLoc) {
9486 Expr *ValExpr = ThreadLimit;
9487
9488 // OpenMP [teams Constrcut, Restrictions]
9489 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009490 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9491 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009492 return nullptr;
9493
9494 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9495 EndLoc);
9496}
Alexey Bataeva0569352015-12-01 10:17:31 +00009497
9498OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9499 SourceLocation StartLoc,
9500 SourceLocation LParenLoc,
9501 SourceLocation EndLoc) {
9502 Expr *ValExpr = Priority;
9503
9504 // OpenMP [2.9.1, task Constrcut]
9505 // The priority-value is a non-negative numerical scalar expression.
9506 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9507 /*StrictlyPositive=*/false))
9508 return nullptr;
9509
9510 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9511}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009512
9513OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9514 SourceLocation StartLoc,
9515 SourceLocation LParenLoc,
9516 SourceLocation EndLoc) {
9517 Expr *ValExpr = Grainsize;
9518
9519 // OpenMP [2.9.2, taskloop Constrcut]
9520 // The parameter of the grainsize clause must be a positive integer
9521 // expression.
9522 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9523 /*StrictlyPositive=*/true))
9524 return nullptr;
9525
9526 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9527}
Alexey Bataev382967a2015-12-08 12:06:20 +00009528
9529OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9530 SourceLocation StartLoc,
9531 SourceLocation LParenLoc,
9532 SourceLocation EndLoc) {
9533 Expr *ValExpr = NumTasks;
9534
9535 // OpenMP [2.9.2, taskloop Constrcut]
9536 // The parameter of the num_tasks clause must be a positive integer
9537 // expression.
9538 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9539 /*StrictlyPositive=*/true))
9540 return nullptr;
9541
9542 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9543}
9544
Alexey Bataev28c75412015-12-15 08:19:24 +00009545OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9546 SourceLocation LParenLoc,
9547 SourceLocation EndLoc) {
9548 // OpenMP [2.13.2, critical construct, Description]
9549 // ... where hint-expression is an integer constant expression that evaluates
9550 // to a valid lock hint.
9551 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9552 if (HintExpr.isInvalid())
9553 return nullptr;
9554 return new (Context)
9555 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9556}
9557
Carlo Bertollib4adf552016-01-15 18:50:31 +00009558OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9559 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9560 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9561 SourceLocation EndLoc) {
9562 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9563 std::string Values;
9564 Values += "'";
9565 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9566 Values += "'";
9567 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9568 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9569 return nullptr;
9570 }
9571 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009572 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009573 if (ChunkSize) {
9574 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9575 !ChunkSize->isInstantiationDependent() &&
9576 !ChunkSize->containsUnexpandedParameterPack()) {
9577 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9578 ExprResult Val =
9579 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9580 if (Val.isInvalid())
9581 return nullptr;
9582
9583 ValExpr = Val.get();
9584
9585 // OpenMP [2.7.1, Restrictions]
9586 // chunk_size must be a loop invariant integer expression with a positive
9587 // value.
9588 llvm::APSInt Result;
9589 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9590 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9591 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9592 << "dist_schedule" << ChunkSize->getSourceRange();
9593 return nullptr;
9594 }
9595 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev3392d762016-02-16 11:18:12 +00009596 ValExpr = buildCapture(*this, ".chunk.", ValExpr);
9597 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
9598 HelperValStmt =
9599 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
9600 /*NumDecls=*/1),
9601 SourceLocation(), SourceLocation());
9602 ValExpr = DefaultLvalueConversion(ValExpr).get();
Carlo Bertollib4adf552016-01-15 18:50:31 +00009603 }
9604 }
9605 }
9606
9607 return new (Context)
9608 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +00009609 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +00009610}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009611
9612OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9613 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9614 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9615 SourceLocation KindLoc, SourceLocation EndLoc) {
9616 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9617 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9618 Kind != OMPC_DEFAULTMAP_scalar) {
9619 std::string Value;
9620 SourceLocation Loc;
9621 Value += "'";
9622 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9623 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9624 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9625 Loc = MLoc;
9626 } else {
9627 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9628 OMPC_DEFAULTMAP_scalar);
9629 Loc = KindLoc;
9630 }
9631 Value += "'";
9632 Diag(Loc, diag::err_omp_unexpected_clause_value)
9633 << Value << getOpenMPClauseName(OMPC_defaultmap);
9634 return nullptr;
9635 }
9636
9637 return new (Context)
9638 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9639}