blob: 50148b5187c8c26fa61d2655f895acd90ea488fb [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 Bataev74caaf22016-02-20 04:09:36 +0000993 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +0000994 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
995 QualType Type = VD->getType().getNonReferenceType();
996 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000998 // Generate helper private variable and initialize it with the
999 // default value. The address of the original variable is replaced
1000 // by the address of the new private variable in CodeGen. This new
1001 // variable is not added to IdResolver, so the code in the OpenMP
1002 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001003 auto *VDPrivate = buildVarDecl(
1004 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001005 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001006 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1007 if (VDPrivate->isInvalidDecl())
1008 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001009 PrivateCopies.push_back(buildDeclRefExpr(
1010 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001011 } else {
1012 // The variable is also a firstprivate, so initialization sequence
1013 // for private copy is generated already.
1014 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001015 }
1016 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001018 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001019 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001020 }
1021 }
1022 }
1023
Alexey Bataev758e55e2013-09-06 18:03:48 +00001024 DSAStack->pop();
1025 DiscardCleanupsInEvaluationContext();
1026 PopExpressionEvaluationContext();
1027}
1028
Alexander Musman3276a272015-03-21 10:12:56 +00001029static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1030 Expr *NumIterations, Sema &SemaRef,
1031 Scope *S);
1032
Alexey Bataeva769e072013-03-22 06:34:35 +00001033namespace {
1034
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035class VarDeclFilterCCC : public CorrectionCandidateCallback {
1036private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001037 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001038
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001040 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001041 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042 NamedDecl *ND = Candidate.getCorrectionDecl();
1043 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1044 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001045 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1046 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001047 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050};
Alexey Bataeved09d242014-05-28 05:53:51 +00001051} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052
1053ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1054 CXXScopeSpec &ScopeSpec,
1055 const DeclarationNameInfo &Id) {
1056 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1057 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1058
1059 if (Lookup.isAmbiguous())
1060 return ExprError();
1061
1062 VarDecl *VD;
1063 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001064 if (TypoCorrection Corrected = CorrectTypo(
1065 Id, LookupOrdinaryName, CurScope, nullptr,
1066 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001067 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001068 PDiag(Lookup.empty()
1069 ? diag::err_undeclared_var_use_suggest
1070 : diag::err_omp_expected_var_arg_suggest)
1071 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001072 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001073 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001074 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1075 : diag::err_omp_expected_var_arg)
1076 << Id.getName();
1077 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001079 } else {
1080 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001081 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001082 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1083 return ExprError();
1084 }
1085 }
1086 Lookup.suppressDiagnostics();
1087
1088 // OpenMP [2.9.2, Syntax, C/C++]
1089 // Variables must be file-scope, namespace-scope, or static block-scope.
1090 if (!VD->hasGlobalStorage()) {
1091 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1093 bool IsDecl =
1094 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001095 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001096 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1097 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001098 return ExprError();
1099 }
1100
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001101 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1102 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001103 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1104 // A threadprivate directive for file-scope variables must appear outside
1105 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001106 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1107 !getCurLexicalContext()->isTranslationUnit()) {
1108 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001109 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1110 bool IsDecl =
1111 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1112 Diag(VD->getLocation(),
1113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1114 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001115 return ExprError();
1116 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001117 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1118 // A threadprivate directive for static class member variables must appear
1119 // in the class definition, in the same scope in which the member
1120 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001121 if (CanonicalVD->isStaticDataMember() &&
1122 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1123 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001124 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1125 bool IsDecl =
1126 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1127 Diag(VD->getLocation(),
1128 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1129 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001130 return ExprError();
1131 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001132 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1133 // A threadprivate directive for namespace-scope variables must appear
1134 // outside any definition or declaration other than the namespace
1135 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001136 if (CanonicalVD->getDeclContext()->isNamespace() &&
1137 (!getCurLexicalContext()->isFileContext() ||
1138 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1139 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001140 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1141 bool IsDecl =
1142 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1143 Diag(VD->getLocation(),
1144 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1145 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001146 return ExprError();
1147 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1149 // A threadprivate directive for static block-scope variables must appear
1150 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001151 if (CanonicalVD->isStaticLocal() && CurScope &&
1152 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001153 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001154 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1155 bool IsDecl =
1156 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1157 Diag(VD->getLocation(),
1158 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1159 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 return ExprError();
1161 }
1162
1163 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1164 // A threadprivate directive must lexically precede all references to any
1165 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001166 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001168 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 return ExprError();
1170 }
1171
1172 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001173 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1174 SourceLocation(), VD,
1175 /*RefersToEnclosingVariableOrCapture=*/false,
1176 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177}
1178
Alexey Bataeved09d242014-05-28 05:53:51 +00001179Sema::DeclGroupPtrTy
1180Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1181 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001182 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001183 CurContext->addDecl(D);
1184 return DeclGroupPtrTy::make(DeclGroupRef(D));
1185 }
David Blaikie0403cb12016-01-15 23:43:25 +00001186 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001187}
1188
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001189namespace {
1190class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1191 Sema &SemaRef;
1192
1193public:
1194 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1195 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1196 if (VD->hasLocalStorage()) {
1197 SemaRef.Diag(E->getLocStart(),
1198 diag::err_omp_local_var_in_threadprivate_init)
1199 << E->getSourceRange();
1200 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1201 << VD << VD->getSourceRange();
1202 return true;
1203 }
1204 }
1205 return false;
1206 }
1207 bool VisitStmt(const Stmt *S) {
1208 for (auto Child : S->children()) {
1209 if (Child && Visit(Child))
1210 return true;
1211 }
1212 return false;
1213 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001214 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001215};
1216} // namespace
1217
Alexey Bataeved09d242014-05-28 05:53:51 +00001218OMPThreadPrivateDecl *
1219Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001220 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001221 for (auto &RefExpr : VarList) {
1222 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001223 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1224 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001225
Alexey Bataev376b4a42016-02-09 09:41:09 +00001226 // Mark variable as used.
1227 VD->setReferenced();
1228 VD->markUsed(Context);
1229
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001230 QualType QType = VD->getType();
1231 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1232 // It will be analyzed later.
1233 Vars.push_back(DE);
1234 continue;
1235 }
1236
Alexey Bataeva769e072013-03-22 06:34:35 +00001237 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1238 // A threadprivate variable must not have an incomplete type.
1239 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001240 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001241 continue;
1242 }
1243
1244 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1245 // A threadprivate variable must not have a reference type.
1246 if (VD->getType()->isReferenceType()) {
1247 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001248 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1249 bool IsDecl =
1250 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1251 Diag(VD->getLocation(),
1252 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1253 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001254 continue;
1255 }
1256
Samuel Antaof8b50122015-07-13 22:54:53 +00001257 // Check if this is a TLS variable. If TLS is not being supported, produce
1258 // the corresponding diagnostic.
1259 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1260 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1261 getLangOpts().OpenMPUseTLS &&
1262 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001263 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1264 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001265 Diag(ILoc, diag::err_omp_var_thread_local)
1266 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001267 bool IsDecl =
1268 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1269 Diag(VD->getLocation(),
1270 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1271 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001272 continue;
1273 }
1274
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001275 // Check if initial value of threadprivate variable reference variable with
1276 // local storage (it is not supported by runtime).
1277 if (auto Init = VD->getAnyInitializer()) {
1278 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001279 if (Checker.Visit(Init))
1280 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001281 }
1282
Alexey Bataeved09d242014-05-28 05:53:51 +00001283 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001284 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001285 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1286 Context, SourceRange(Loc, Loc)));
1287 if (auto *ML = Context.getASTMutationListener())
1288 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001289 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001290 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001291 if (!Vars.empty()) {
1292 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1293 Vars);
1294 D->setAccess(AS_public);
1295 }
1296 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001297}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001298
Alexey Bataev7ff55242014-06-19 09:13:45 +00001299static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001300 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001301 bool IsLoopIterVar = false) {
1302 if (DVar.RefExpr) {
1303 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1304 << getOpenMPClauseName(DVar.CKind);
1305 return;
1306 }
1307 enum {
1308 PDSA_StaticMemberShared,
1309 PDSA_StaticLocalVarShared,
1310 PDSA_LoopIterVarPrivate,
1311 PDSA_LoopIterVarLinear,
1312 PDSA_LoopIterVarLastprivate,
1313 PDSA_ConstVarShared,
1314 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001315 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001316 PDSA_LocalVarPrivate,
1317 PDSA_Implicit
1318 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001319 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001320 auto ReportLoc = D->getLocation();
1321 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001322 if (IsLoopIterVar) {
1323 if (DVar.CKind == OMPC_private)
1324 Reason = PDSA_LoopIterVarPrivate;
1325 else if (DVar.CKind == OMPC_lastprivate)
1326 Reason = PDSA_LoopIterVarLastprivate;
1327 else
1328 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001329 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1330 Reason = PDSA_TaskVarFirstprivate;
1331 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001332 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001333 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001334 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001335 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001336 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001337 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001338 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001339 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001340 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001341 ReportHint = true;
1342 Reason = PDSA_LocalVarPrivate;
1343 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001344 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001345 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001346 << Reason << ReportHint
1347 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1348 } else if (DVar.ImplicitDSALoc.isValid()) {
1349 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1350 << getOpenMPClauseName(DVar.CKind);
1351 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001352}
1353
Alexey Bataev758e55e2013-09-06 18:03:48 +00001354namespace {
1355class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1356 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001357 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001358 bool ErrorFound;
1359 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001360 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001361 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001362
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363public:
1364 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001365 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001366 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001367 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1368 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001370 auto DVar = Stack->getTopDSA(VD, false);
1371 // Check if the variable has explicit DSA set and stop analysis if it so.
1372 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001373
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001374 auto ELoc = E->getExprLoc();
1375 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001376 // The default(none) clause requires that each variable that is referenced
1377 // in the construct, and does not have a predetermined data-sharing
1378 // attribute, must have its data-sharing attribute explicitly determined
1379 // by being listed in a data-sharing attribute clause.
1380 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001381 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001382 VarsWithInheritedDSA.count(VD) == 0) {
1383 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001384 return;
1385 }
1386
1387 // OpenMP [2.9.3.6, Restrictions, p.2]
1388 // A list item that appears in a reduction clause of the innermost
1389 // enclosing worksharing or parallel construct may not be accessed in an
1390 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001391 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001392 [](OpenMPDirectiveKind K) -> bool {
1393 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001394 isOpenMPWorksharingDirective(K) ||
1395 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001396 },
1397 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001398 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1399 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001400 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1401 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001402 return;
1403 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001404
1405 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001406 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001407 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001408 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001409 }
1410 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001411 void VisitMemberExpr(MemberExpr *E) {
1412 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1413 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1414 auto DVar = Stack->getTopDSA(FD, false);
1415 // Check if the variable has explicit DSA set and stop analysis if it
1416 // so.
1417 if (DVar.RefExpr)
1418 return;
1419
1420 auto ELoc = E->getExprLoc();
1421 auto DKind = Stack->getCurrentDirective();
1422 // OpenMP [2.9.3.6, Restrictions, p.2]
1423 // A list item that appears in a reduction clause of the innermost
1424 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001425 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001426 DVar =
1427 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1428 [](OpenMPDirectiveKind K) -> bool {
1429 return isOpenMPParallelDirective(K) ||
1430 isOpenMPWorksharingDirective(K) ||
1431 isOpenMPTeamsDirective(K);
1432 },
1433 false);
1434 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1435 ErrorFound = true;
1436 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1437 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1438 return;
1439 }
1440
1441 // Define implicit data-sharing attributes for task.
1442 DVar = Stack->getImplicitDSA(FD, false);
1443 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1444 ImplicitFirstprivate.push_back(E);
1445 }
1446 }
1447 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001448 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001449 for (auto *C : S->clauses()) {
1450 // Skip analysis of arguments of implicitly defined firstprivate clause
1451 // for task directives.
1452 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1453 for (auto *CC : C->children()) {
1454 if (CC)
1455 Visit(CC);
1456 }
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458 }
1459 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001460 for (auto *C : S->children()) {
1461 if (C && !isa<OMPExecutableDirective>(C))
1462 Visit(C);
1463 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001464 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001465
1466 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001467 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001468 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001469 return VarsWithInheritedDSA;
1470 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataev7ff55242014-06-19 09:13:45 +00001472 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1473 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001474};
Alexey Bataeved09d242014-05-28 05:53:51 +00001475} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476
Alexey Bataevbae9a792014-06-27 10:37:06 +00001477void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001478 switch (DKind) {
1479 case OMPD_parallel: {
1480 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001481 QualType KmpInt32PtrTy =
1482 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001483 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001484 std::make_pair(".global_tid.", KmpInt32PtrTy),
1485 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1486 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001487 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001488 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1489 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001490 break;
1491 }
1492 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 std::make_pair(StringRef(), QualType()) // __context with shared vars
1495 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001496 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1497 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001498 break;
1499 }
1500 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001501 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001502 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001503 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001504 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1505 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001506 break;
1507 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001508 case OMPD_for_simd: {
1509 Sema::CapturedParamNameType Params[] = {
1510 std::make_pair(StringRef(), QualType()) // __context with shared vars
1511 };
1512 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1513 Params);
1514 break;
1515 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001516 case OMPD_sections: {
1517 Sema::CapturedParamNameType Params[] = {
1518 std::make_pair(StringRef(), QualType()) // __context with shared vars
1519 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001520 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1521 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001522 break;
1523 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001524 case OMPD_section: {
1525 Sema::CapturedParamNameType Params[] = {
1526 std::make_pair(StringRef(), QualType()) // __context with shared vars
1527 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001528 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1529 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001530 break;
1531 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001532 case OMPD_single: {
1533 Sema::CapturedParamNameType Params[] = {
1534 std::make_pair(StringRef(), QualType()) // __context with shared vars
1535 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001536 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1537 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001538 break;
1539 }
Alexander Musman80c22892014-07-17 08:54:58 +00001540 case OMPD_master: {
1541 Sema::CapturedParamNameType Params[] = {
1542 std::make_pair(StringRef(), QualType()) // __context with shared vars
1543 };
1544 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1545 Params);
1546 break;
1547 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001548 case OMPD_critical: {
1549 Sema::CapturedParamNameType Params[] = {
1550 std::make_pair(StringRef(), QualType()) // __context with shared vars
1551 };
1552 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1553 Params);
1554 break;
1555 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001556 case OMPD_parallel_for: {
1557 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001558 QualType KmpInt32PtrTy =
1559 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001560 Sema::CapturedParamNameType Params[] = {
1561 std::make_pair(".global_tid.", KmpInt32PtrTy),
1562 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1563 std::make_pair(StringRef(), QualType()) // __context with shared vars
1564 };
1565 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1566 Params);
1567 break;
1568 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001569 case OMPD_parallel_for_simd: {
1570 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001571 QualType KmpInt32PtrTy =
1572 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001573 Sema::CapturedParamNameType Params[] = {
1574 std::make_pair(".global_tid.", KmpInt32PtrTy),
1575 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1576 std::make_pair(StringRef(), QualType()) // __context with shared vars
1577 };
1578 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1579 Params);
1580 break;
1581 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001582 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001583 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001584 QualType KmpInt32PtrTy =
1585 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001586 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001587 std::make_pair(".global_tid.", KmpInt32PtrTy),
1588 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001589 std::make_pair(StringRef(), QualType()) // __context with shared vars
1590 };
1591 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1592 Params);
1593 break;
1594 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001595 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001596 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001597 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1598 FunctionProtoType::ExtProtoInfo EPI;
1599 EPI.Variadic = true;
1600 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001601 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001602 std::make_pair(".global_tid.", KmpInt32Ty),
1603 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001604 std::make_pair(".privates.",
1605 Context.VoidPtrTy.withConst().withRestrict()),
1606 std::make_pair(
1607 ".copy_fn.",
1608 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001609 std::make_pair(StringRef(), QualType()) // __context with shared vars
1610 };
1611 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1612 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001613 // Mark this captured region as inlined, because we don't use outlined
1614 // function directly.
1615 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1616 AlwaysInlineAttr::CreateImplicit(
1617 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001618 break;
1619 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001620 case OMPD_ordered: {
1621 Sema::CapturedParamNameType Params[] = {
1622 std::make_pair(StringRef(), QualType()) // __context with shared vars
1623 };
1624 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1625 Params);
1626 break;
1627 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001628 case OMPD_atomic: {
1629 Sema::CapturedParamNameType Params[] = {
1630 std::make_pair(StringRef(), QualType()) // __context with shared vars
1631 };
1632 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1633 Params);
1634 break;
1635 }
Michael Wong65f367f2015-07-21 13:44:28 +00001636 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001637 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001638 case OMPD_target_parallel:
1639 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
1643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
1645 break;
1646 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001647 case OMPD_teams: {
1648 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001649 QualType KmpInt32PtrTy =
1650 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001651 Sema::CapturedParamNameType Params[] = {
1652 std::make_pair(".global_tid.", KmpInt32PtrTy),
1653 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1654 std::make_pair(StringRef(), QualType()) // __context with shared vars
1655 };
1656 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1657 Params);
1658 break;
1659 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001660 case OMPD_taskgroup: {
1661 Sema::CapturedParamNameType Params[] = {
1662 std::make_pair(StringRef(), QualType()) // __context with shared vars
1663 };
1664 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1665 Params);
1666 break;
1667 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001668 case OMPD_taskloop: {
1669 Sema::CapturedParamNameType Params[] = {
1670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
1674 break;
1675 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001676 case OMPD_taskloop_simd: {
1677 Sema::CapturedParamNameType Params[] = {
1678 std::make_pair(StringRef(), QualType()) // __context with shared vars
1679 };
1680 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1681 Params);
1682 break;
1683 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001684 case OMPD_distribute: {
1685 Sema::CapturedParamNameType Params[] = {
1686 std::make_pair(StringRef(), QualType()) // __context with shared vars
1687 };
1688 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1689 Params);
1690 break;
1691 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001692 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001693 case OMPD_taskyield:
1694 case OMPD_barrier:
1695 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001696 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001697 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001698 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001699 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001700 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001701 llvm_unreachable("OpenMP Directive is not allowed");
1702 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 llvm_unreachable("Unknown OpenMP directive");
1704 }
1705}
1706
Alexey Bataev3392d762016-02-16 11:18:12 +00001707static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev61205072016-03-02 04:57:40 +00001708 Expr *CaptureExpr, bool WithInit) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001709 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001710 ASTContext &C = S.getASTContext();
1711 Expr *Init = CaptureExpr->IgnoreImpCasts();
1712 QualType Ty = Init->getType();
1713 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1714 if (S.getLangOpts().CPlusPlus)
1715 Ty = C.getLValueReferenceType(Ty);
1716 else {
1717 Ty = C.getPointerType(Ty);
1718 ExprResult Res =
1719 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1720 if (!Res.isUsable())
1721 return nullptr;
1722 Init = Res.get();
1723 }
Alexey Bataev61205072016-03-02 04:57:40 +00001724 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001725 }
1726 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001727 if (!WithInit)
1728 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001729 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001730 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1731 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001732 return CED;
1733}
1734
Alexey Bataev61205072016-03-02 04:57:40 +00001735static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1736 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001737 OMPCapturedExprDecl *CD;
1738 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1739 CD = cast<OMPCapturedExprDecl>(VD);
1740 else
Alexey Bataev61205072016-03-02 04:57:40 +00001741 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit);
Alexey Bataev3392d762016-02-16 11:18:12 +00001742 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1743 SourceLocation());
1744}
1745
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001746static DeclRefExpr *buildCapture(Sema &S, Expr *CaptureExpr) {
Alexey Bataev61205072016-03-02 04:57:40 +00001747 auto *CD =
1748 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1749 CaptureExpr, /*WithInit=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001750 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1751 SourceLocation());
Alexey Bataev4244be22016-02-11 05:35:55 +00001752}
1753
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001754StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1755 ArrayRef<OMPClause *> Clauses) {
1756 if (!S.isUsable()) {
1757 ActOnCapturedRegionError();
1758 return StmtError();
1759 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001760
1761 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001762 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001763 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001764 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001765 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001766 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001767 Clause->getClauseKind() == OMPC_copyprivate ||
1768 (getLangOpts().OpenMPUseTLS &&
1769 getASTContext().getTargetInfo().isTLSSupported() &&
1770 Clause->getClauseKind() == OMPC_copyin)) {
1771 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001772 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001773 for (auto *VarRef : Clause->children()) {
1774 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001775 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001776 }
1777 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001778 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001779 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001780 // Mark all variables in private list clauses as used in inner region.
1781 // Required for proper codegen of combined directives.
1782 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001783 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001784 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1785 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001786 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1787 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001788 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001789 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1790 if (auto *E = C->getPostUpdateExpr())
1791 MarkDeclarationsReferencedInExpr(E);
1792 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001793 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001794 if (Clause->getClauseKind() == OMPC_schedule)
1795 SC = cast<OMPScheduleClause>(Clause);
1796 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001797 OC = cast<OMPOrderedClause>(Clause);
1798 else if (Clause->getClauseKind() == OMPC_linear)
1799 LCs.push_back(cast<OMPLinearClause>(Clause));
1800 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001801 bool ErrorFound = false;
1802 // OpenMP, 2.7.1 Loop Construct, Restrictions
1803 // The nonmonotonic modifier cannot be specified if an ordered clause is
1804 // specified.
1805 if (SC &&
1806 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1807 SC->getSecondScheduleModifier() ==
1808 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1809 OC) {
1810 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1811 ? SC->getFirstScheduleModifierLoc()
1812 : SC->getSecondScheduleModifierLoc(),
1813 diag::err_omp_schedule_nonmonotonic_ordered)
1814 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1815 ErrorFound = true;
1816 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001817 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1818 for (auto *C : LCs) {
1819 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1820 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1821 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001822 ErrorFound = true;
1823 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001824 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1825 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1826 OC->getNumForLoops()) {
1827 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1828 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1829 ErrorFound = true;
1830 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001831 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001832 ActOnCapturedRegionError();
1833 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001834 }
1835 return ActOnCapturedRegionEnd(S.get());
1836}
1837
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001838static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1839 OpenMPDirectiveKind CurrentRegion,
1840 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001841 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001842 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001843 // Allowed nesting of constructs
1844 // +------------------+-----------------+------------------------------------+
1845 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1846 // +------------------+-----------------+------------------------------------+
1847 // | parallel | parallel | * |
1848 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001849 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001850 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001851 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001852 // | parallel | simd | * |
1853 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001854 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001855 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001856 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001857 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001858 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001859 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001860 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001861 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001862 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001863 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001864 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001865 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001866 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001867 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001868 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001869 // | parallel | target parallel | * |
1870 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001871 // | parallel | target enter | * |
1872 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001873 // | parallel | target exit | * |
1874 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001875 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001876 // | parallel | cancellation | |
1877 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001878 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001879 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001880 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001881 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001882 // +------------------+-----------------+------------------------------------+
1883 // | for | parallel | * |
1884 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001885 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001886 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001887 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001888 // | for | simd | * |
1889 // | for | sections | + |
1890 // | for | section | + |
1891 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001892 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001893 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001894 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001895 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001896 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001897 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001898 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001899 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001900 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001901 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001902 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001903 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001904 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001905 // | for | target parallel | * |
1906 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001907 // | for | target enter | * |
1908 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001909 // | for | target exit | * |
1910 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001911 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001912 // | for | cancellation | |
1913 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001914 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001915 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001916 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001917 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001918 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001919 // | master | parallel | * |
1920 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001921 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001922 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001923 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001924 // | master | simd | * |
1925 // | master | sections | + |
1926 // | master | section | + |
1927 // | master | single | + |
1928 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001929 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001930 // | master |parallel sections| * |
1931 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001932 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001933 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001934 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001935 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001936 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001937 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001938 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001939 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001940 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001941 // | master | target parallel | * |
1942 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001943 // | master | target enter | * |
1944 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001945 // | master | target exit | * |
1946 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001947 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001948 // | master | cancellation | |
1949 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001950 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001951 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001952 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001953 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001954 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001955 // | critical | parallel | * |
1956 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001957 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001958 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001959 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001960 // | critical | simd | * |
1961 // | critical | sections | + |
1962 // | critical | section | + |
1963 // | critical | single | + |
1964 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001965 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001966 // | critical |parallel sections| * |
1967 // | critical | task | * |
1968 // | critical | taskyield | * |
1969 // | critical | barrier | + |
1970 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001971 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001972 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001973 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001974 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001975 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001976 // | critical | target parallel | * |
1977 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001978 // | critical | target enter | * |
1979 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001980 // | critical | target exit | * |
1981 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001982 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001983 // | critical | cancellation | |
1984 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001985 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001986 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001987 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001988 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001989 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001990 // | simd | parallel | |
1991 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001992 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001993 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001994 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001995 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001996 // | simd | sections | |
1997 // | simd | section | |
1998 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001999 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002000 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002001 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002002 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002003 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002004 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002005 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002006 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002007 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002008 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002009 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002010 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002011 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002012 // | simd | target parallel | |
2013 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002014 // | simd | target enter | |
2015 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002016 // | simd | target exit | |
2017 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002018 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002019 // | simd | cancellation | |
2020 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002021 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002022 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002023 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002024 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002025 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002026 // | for simd | parallel | |
2027 // | for simd | for | |
2028 // | for simd | for simd | |
2029 // | for simd | master | |
2030 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002031 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002032 // | for simd | sections | |
2033 // | for simd | section | |
2034 // | for simd | single | |
2035 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002036 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002037 // | for simd |parallel sections| |
2038 // | for simd | task | |
2039 // | for simd | taskyield | |
2040 // | for simd | barrier | |
2041 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002042 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002043 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002044 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002045 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002046 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002047 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002048 // | for simd | target parallel | |
2049 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002050 // | for simd | target enter | |
2051 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002052 // | for simd | target exit | |
2053 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002054 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002055 // | for simd | cancellation | |
2056 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002057 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002058 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002059 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002060 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002061 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002062 // | parallel for simd| parallel | |
2063 // | parallel for simd| for | |
2064 // | parallel for simd| for simd | |
2065 // | parallel for simd| master | |
2066 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002067 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002068 // | parallel for simd| sections | |
2069 // | parallel for simd| section | |
2070 // | parallel for simd| single | |
2071 // | parallel for simd| parallel for | |
2072 // | parallel for simd|parallel for simd| |
2073 // | parallel for simd|parallel sections| |
2074 // | parallel for simd| task | |
2075 // | parallel for simd| taskyield | |
2076 // | parallel for simd| barrier | |
2077 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002078 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002079 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002080 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002081 // | parallel for simd| atomic | |
2082 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002083 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002084 // | parallel for simd| target parallel | |
2085 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002086 // | parallel for simd| target enter | |
2087 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002088 // | parallel for simd| target exit | |
2089 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002090 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002091 // | parallel for simd| cancellation | |
2092 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002093 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002094 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002095 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002096 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002097 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002098 // | sections | parallel | * |
2099 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002100 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002101 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002102 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002103 // | sections | simd | * |
2104 // | sections | sections | + |
2105 // | sections | section | * |
2106 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002107 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002108 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002109 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002110 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002111 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002112 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002113 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002114 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002115 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002116 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002117 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002118 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002119 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002120 // | sections | target parallel | * |
2121 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002122 // | sections | target enter | * |
2123 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002124 // | sections | target exit | * |
2125 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002126 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002127 // | sections | cancellation | |
2128 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002129 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002130 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002131 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002132 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002133 // +------------------+-----------------+------------------------------------+
2134 // | section | parallel | * |
2135 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002136 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002137 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002138 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002139 // | section | simd | * |
2140 // | section | sections | + |
2141 // | section | section | + |
2142 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002143 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002144 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002145 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002146 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002147 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002148 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002149 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002150 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002151 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002152 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002153 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002154 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002155 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002156 // | section | target parallel | * |
2157 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002158 // | section | target enter | * |
2159 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002160 // | section | target exit | * |
2161 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002162 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002163 // | section | cancellation | |
2164 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002165 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002166 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002167 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002168 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002169 // +------------------+-----------------+------------------------------------+
2170 // | single | parallel | * |
2171 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002172 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002173 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002174 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002175 // | single | simd | * |
2176 // | single | sections | + |
2177 // | single | section | + |
2178 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002179 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002180 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002181 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002182 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002183 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002184 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002185 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002186 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002187 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002188 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002189 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002190 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002191 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002192 // | single | target parallel | * |
2193 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002194 // | single | target enter | * |
2195 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002196 // | single | target exit | * |
2197 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002198 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002199 // | single | cancellation | |
2200 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002201 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002202 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002203 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002204 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002205 // +------------------+-----------------+------------------------------------+
2206 // | parallel for | parallel | * |
2207 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002208 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002209 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002210 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002211 // | parallel for | simd | * |
2212 // | parallel for | sections | + |
2213 // | parallel for | section | + |
2214 // | parallel for | single | + |
2215 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002216 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002217 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002218 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002219 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002220 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002221 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002222 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002223 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002224 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002225 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002226 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002227 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002228 // | parallel for | target parallel | * |
2229 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002230 // | parallel for | target enter | * |
2231 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002232 // | parallel for | target exit | * |
2233 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002234 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002235 // | parallel for | cancellation | |
2236 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002237 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002238 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002239 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002240 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002241 // +------------------+-----------------+------------------------------------+
2242 // | parallel sections| parallel | * |
2243 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002244 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002245 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002246 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002247 // | parallel sections| simd | * |
2248 // | parallel sections| sections | + |
2249 // | parallel sections| section | * |
2250 // | parallel sections| single | + |
2251 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002252 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002253 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002254 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002255 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002256 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002257 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002258 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002259 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002260 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002261 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002262 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002263 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002264 // | parallel sections| target parallel | * |
2265 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002266 // | parallel sections| target enter | * |
2267 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002268 // | parallel sections| target exit | * |
2269 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002270 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002271 // | parallel sections| cancellation | |
2272 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002273 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002274 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002275 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002276 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002277 // +------------------+-----------------+------------------------------------+
2278 // | task | parallel | * |
2279 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002280 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002281 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002282 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002283 // | task | simd | * |
2284 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002285 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002286 // | task | single | + |
2287 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002288 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002289 // | task |parallel sections| * |
2290 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002291 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002292 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002293 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002294 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002295 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002296 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002297 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002298 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002299 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002300 // | task | target parallel | * |
2301 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002302 // | task | target enter | * |
2303 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002304 // | task | target exit | * |
2305 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002306 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002307 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002308 // | | point | ! |
2309 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002310 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002311 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002312 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002313 // +------------------+-----------------+------------------------------------+
2314 // | ordered | parallel | * |
2315 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002316 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002317 // | ordered | master | * |
2318 // | ordered | critical | * |
2319 // | ordered | simd | * |
2320 // | ordered | sections | + |
2321 // | ordered | section | + |
2322 // | ordered | single | + |
2323 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002324 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002325 // | ordered |parallel sections| * |
2326 // | ordered | task | * |
2327 // | ordered | taskyield | * |
2328 // | ordered | barrier | + |
2329 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002330 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002331 // | ordered | flush | * |
2332 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002333 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002334 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002335 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002336 // | ordered | target parallel | * |
2337 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002338 // | ordered | target enter | * |
2339 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002340 // | ordered | target exit | * |
2341 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002342 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002343 // | ordered | cancellation | |
2344 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002345 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002346 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002347 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002348 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002349 // +------------------+-----------------+------------------------------------+
2350 // | atomic | parallel | |
2351 // | atomic | for | |
2352 // | atomic | for simd | |
2353 // | atomic | master | |
2354 // | atomic | critical | |
2355 // | atomic | simd | |
2356 // | atomic | sections | |
2357 // | atomic | section | |
2358 // | atomic | single | |
2359 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002360 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002361 // | atomic |parallel sections| |
2362 // | atomic | task | |
2363 // | atomic | taskyield | |
2364 // | atomic | barrier | |
2365 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002366 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002367 // | atomic | flush | |
2368 // | atomic | ordered | |
2369 // | atomic | atomic | |
2370 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002371 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002372 // | atomic | target parallel | |
2373 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002374 // | atomic | target enter | |
2375 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002376 // | atomic | target exit | |
2377 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002378 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002379 // | atomic | cancellation | |
2380 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002381 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002382 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002383 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002384 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002385 // +------------------+-----------------+------------------------------------+
2386 // | target | parallel | * |
2387 // | target | for | * |
2388 // | target | for simd | * |
2389 // | target | master | * |
2390 // | target | critical | * |
2391 // | target | simd | * |
2392 // | target | sections | * |
2393 // | target | section | * |
2394 // | target | single | * |
2395 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002396 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002397 // | target |parallel sections| * |
2398 // | target | task | * |
2399 // | target | taskyield | * |
2400 // | target | barrier | * |
2401 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002402 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002403 // | target | flush | * |
2404 // | target | ordered | * |
2405 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002406 // | target | target | |
2407 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002408 // | target | target parallel | |
2409 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002410 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002411 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002412 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002413 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002414 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002415 // | target | cancellation | |
2416 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002417 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002418 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002419 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002420 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002421 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002422 // | target parallel | parallel | * |
2423 // | target parallel | for | * |
2424 // | target parallel | for simd | * |
2425 // | target parallel | master | * |
2426 // | target parallel | critical | * |
2427 // | target parallel | simd | * |
2428 // | target parallel | sections | * |
2429 // | target parallel | section | * |
2430 // | target parallel | single | * |
2431 // | target parallel | parallel for | * |
2432 // | target parallel |parallel for simd| * |
2433 // | target parallel |parallel sections| * |
2434 // | target parallel | task | * |
2435 // | target parallel | taskyield | * |
2436 // | target parallel | barrier | * |
2437 // | target parallel | taskwait | * |
2438 // | target parallel | taskgroup | * |
2439 // | target parallel | flush | * |
2440 // | target parallel | ordered | * |
2441 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002442 // | target parallel | target | |
2443 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002444 // | target parallel | target parallel | |
2445 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002446 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002447 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002448 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002449 // | | data | |
2450 // | target parallel | teams | |
2451 // | target parallel | cancellation | |
2452 // | | point | ! |
2453 // | target parallel | cancel | ! |
2454 // | target parallel | taskloop | * |
2455 // | target parallel | taskloop simd | * |
2456 // | target parallel | distribute | |
2457 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002458 // | target parallel | parallel | * |
2459 // | for | | |
2460 // | target parallel | for | * |
2461 // | for | | |
2462 // | target parallel | for simd | * |
2463 // | for | | |
2464 // | target parallel | master | * |
2465 // | for | | |
2466 // | target parallel | critical | * |
2467 // | for | | |
2468 // | target parallel | simd | * |
2469 // | for | | |
2470 // | target parallel | sections | * |
2471 // | for | | |
2472 // | target parallel | section | * |
2473 // | for | | |
2474 // | target parallel | single | * |
2475 // | for | | |
2476 // | target parallel | parallel for | * |
2477 // | for | | |
2478 // | target parallel |parallel for simd| * |
2479 // | for | | |
2480 // | target parallel |parallel sections| * |
2481 // | for | | |
2482 // | target parallel | task | * |
2483 // | for | | |
2484 // | target parallel | taskyield | * |
2485 // | for | | |
2486 // | target parallel | barrier | * |
2487 // | for | | |
2488 // | target parallel | taskwait | * |
2489 // | for | | |
2490 // | target parallel | taskgroup | * |
2491 // | for | | |
2492 // | target parallel | flush | * |
2493 // | for | | |
2494 // | target parallel | ordered | * |
2495 // | for | | |
2496 // | target parallel | atomic | * |
2497 // | for | | |
2498 // | target parallel | target | |
2499 // | for | | |
2500 // | target parallel | target parallel | |
2501 // | for | | |
2502 // | target parallel | target parallel | |
2503 // | for | for | |
2504 // | target parallel | target enter | |
2505 // | for | data | |
2506 // | target parallel | target exit | |
2507 // | for | data | |
2508 // | target parallel | teams | |
2509 // | for | | |
2510 // | target parallel | cancellation | |
2511 // | for | point | ! |
2512 // | target parallel | cancel | ! |
2513 // | for | | |
2514 // | target parallel | taskloop | * |
2515 // | for | | |
2516 // | target parallel | taskloop simd | * |
2517 // | for | | |
2518 // | target parallel | distribute | |
2519 // | for | | |
2520 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002521 // | teams | parallel | * |
2522 // | teams | for | + |
2523 // | teams | for simd | + |
2524 // | teams | master | + |
2525 // | teams | critical | + |
2526 // | teams | simd | + |
2527 // | teams | sections | + |
2528 // | teams | section | + |
2529 // | teams | single | + |
2530 // | teams | parallel for | * |
2531 // | teams |parallel for simd| * |
2532 // | teams |parallel sections| * |
2533 // | teams | task | + |
2534 // | teams | taskyield | + |
2535 // | teams | barrier | + |
2536 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002537 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002538 // | teams | flush | + |
2539 // | teams | ordered | + |
2540 // | teams | atomic | + |
2541 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002542 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002543 // | teams | target parallel | + |
2544 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002545 // | teams | target enter | + |
2546 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002547 // | teams | target exit | + |
2548 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002549 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002550 // | teams | cancellation | |
2551 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002552 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002553 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002554 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002555 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002556 // +------------------+-----------------+------------------------------------+
2557 // | taskloop | parallel | * |
2558 // | taskloop | for | + |
2559 // | taskloop | for simd | + |
2560 // | taskloop | master | + |
2561 // | taskloop | critical | * |
2562 // | taskloop | simd | * |
2563 // | taskloop | sections | + |
2564 // | taskloop | section | + |
2565 // | taskloop | single | + |
2566 // | taskloop | parallel for | * |
2567 // | taskloop |parallel for simd| * |
2568 // | taskloop |parallel sections| * |
2569 // | taskloop | task | * |
2570 // | taskloop | taskyield | * |
2571 // | taskloop | barrier | + |
2572 // | taskloop | taskwait | * |
2573 // | taskloop | taskgroup | * |
2574 // | taskloop | flush | * |
2575 // | taskloop | ordered | + |
2576 // | taskloop | atomic | * |
2577 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002578 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002579 // | taskloop | target parallel | * |
2580 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002581 // | taskloop | target enter | * |
2582 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002583 // | taskloop | target exit | * |
2584 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002585 // | taskloop | teams | + |
2586 // | taskloop | cancellation | |
2587 // | | point | |
2588 // | taskloop | cancel | |
2589 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002590 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002591 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002592 // | taskloop simd | parallel | |
2593 // | taskloop simd | for | |
2594 // | taskloop simd | for simd | |
2595 // | taskloop simd | master | |
2596 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002597 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002598 // | taskloop simd | sections | |
2599 // | taskloop simd | section | |
2600 // | taskloop simd | single | |
2601 // | taskloop simd | parallel for | |
2602 // | taskloop simd |parallel for simd| |
2603 // | taskloop simd |parallel sections| |
2604 // | taskloop simd | task | |
2605 // | taskloop simd | taskyield | |
2606 // | taskloop simd | barrier | |
2607 // | taskloop simd | taskwait | |
2608 // | taskloop simd | taskgroup | |
2609 // | taskloop simd | flush | |
2610 // | taskloop simd | ordered | + (with simd clause) |
2611 // | taskloop simd | atomic | |
2612 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002613 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002614 // | taskloop simd | target parallel | |
2615 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002616 // | taskloop simd | target enter | |
2617 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002618 // | taskloop simd | target exit | |
2619 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002620 // | taskloop simd | teams | |
2621 // | taskloop simd | cancellation | |
2622 // | | point | |
2623 // | taskloop simd | cancel | |
2624 // | taskloop simd | taskloop | |
2625 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002626 // | taskloop simd | distribute | |
2627 // +------------------+-----------------+------------------------------------+
2628 // | distribute | parallel | * |
2629 // | distribute | for | * |
2630 // | distribute | for simd | * |
2631 // | distribute | master | * |
2632 // | distribute | critical | * |
2633 // | distribute | simd | * |
2634 // | distribute | sections | * |
2635 // | distribute | section | * |
2636 // | distribute | single | * |
2637 // | distribute | parallel for | * |
2638 // | distribute |parallel for simd| * |
2639 // | distribute |parallel sections| * |
2640 // | distribute | task | * |
2641 // | distribute | taskyield | * |
2642 // | distribute | barrier | * |
2643 // | distribute | taskwait | * |
2644 // | distribute | taskgroup | * |
2645 // | distribute | flush | * |
2646 // | distribute | ordered | + |
2647 // | distribute | atomic | * |
2648 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002649 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002650 // | distribute | target parallel | |
2651 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002652 // | distribute | target enter | |
2653 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002654 // | distribute | target exit | |
2655 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002656 // | distribute | teams | |
2657 // | distribute | cancellation | + |
2658 // | | point | |
2659 // | distribute | cancel | + |
2660 // | distribute | taskloop | * |
2661 // | distribute | taskloop simd | * |
2662 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002663 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002664 if (Stack->getCurScope()) {
2665 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002666 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002667 bool NestingProhibited = false;
2668 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002669 enum {
2670 NoRecommend,
2671 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002672 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002673 ShouldBeInTargetRegion,
2674 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002675 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002676 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2677 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002678 // OpenMP [2.16, Nesting of Regions]
2679 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002680 // OpenMP [2.8.1,simd Construct, Restrictions]
2681 // An ordered construct with the simd clause is the only OpenMP construct
2682 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002683 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2684 return true;
2685 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002686 if (ParentRegion == OMPD_atomic) {
2687 // OpenMP [2.16, Nesting of Regions]
2688 // OpenMP constructs may not be nested inside an atomic region.
2689 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2690 return true;
2691 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002692 if (CurrentRegion == OMPD_section) {
2693 // OpenMP [2.7.2, sections Construct, Restrictions]
2694 // Orphaned section directives are prohibited. That is, the section
2695 // directives must appear within the sections construct and must not be
2696 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002697 if (ParentRegion != OMPD_sections &&
2698 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002699 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2700 << (ParentRegion != OMPD_unknown)
2701 << getOpenMPDirectiveName(ParentRegion);
2702 return true;
2703 }
2704 return false;
2705 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002706 // Allow some constructs to be orphaned (they could be used in functions,
2707 // called from OpenMP regions with the required preconditions).
2708 if (ParentRegion == OMPD_unknown)
2709 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002710 if (CurrentRegion == OMPD_cancellation_point ||
2711 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002712 // OpenMP [2.16, Nesting of Regions]
2713 // A cancellation point construct for which construct-type-clause is
2714 // taskgroup must be nested inside a task construct. A cancellation
2715 // point construct for which construct-type-clause is not taskgroup must
2716 // be closely nested inside an OpenMP construct that matches the type
2717 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002718 // A cancel construct for which construct-type-clause is taskgroup must be
2719 // nested inside a task construct. A cancel construct for which
2720 // construct-type-clause is not taskgroup must be closely nested inside an
2721 // OpenMP construct that matches the type specified in
2722 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002723 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002724 !((CancelRegion == OMPD_parallel &&
2725 (ParentRegion == OMPD_parallel ||
2726 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002727 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002728 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2729 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002730 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2731 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002732 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2733 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002734 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002735 // OpenMP [2.16, Nesting of Regions]
2736 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002737 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002738 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002739 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002740 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002741 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2742 // OpenMP [2.16, Nesting of Regions]
2743 // A critical region may not be nested (closely or otherwise) inside a
2744 // critical region with the same name. Note that this restriction is not
2745 // sufficient to prevent deadlock.
2746 SourceLocation PreviousCriticalLoc;
2747 bool DeadLock =
2748 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2749 OpenMPDirectiveKind K,
2750 const DeclarationNameInfo &DNI,
2751 SourceLocation Loc)
2752 ->bool {
2753 if (K == OMPD_critical &&
2754 DNI.getName() == CurrentName.getName()) {
2755 PreviousCriticalLoc = Loc;
2756 return true;
2757 } else
2758 return false;
2759 },
2760 false /* skip top directive */);
2761 if (DeadLock) {
2762 SemaRef.Diag(StartLoc,
2763 diag::err_omp_prohibited_region_critical_same_name)
2764 << CurrentName.getName();
2765 if (PreviousCriticalLoc.isValid())
2766 SemaRef.Diag(PreviousCriticalLoc,
2767 diag::note_omp_previous_critical_region);
2768 return true;
2769 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002770 } else if (CurrentRegion == OMPD_barrier) {
2771 // OpenMP [2.16, Nesting of Regions]
2772 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002773 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002774 NestingProhibited =
2775 isOpenMPWorksharingDirective(ParentRegion) ||
2776 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002777 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002778 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002779 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002780 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002781 // OpenMP [2.16, Nesting of Regions]
2782 // A worksharing region may not be closely nested inside a worksharing,
2783 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002784 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002785 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002786 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002787 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002788 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002789 Recommend = ShouldBeInParallelRegion;
2790 } else if (CurrentRegion == OMPD_ordered) {
2791 // OpenMP [2.16, Nesting of Regions]
2792 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002793 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002794 // An ordered region must be closely nested inside a loop region (or
2795 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002796 // OpenMP [2.8.1,simd Construct, Restrictions]
2797 // An ordered construct with the simd clause is the only OpenMP construct
2798 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002799 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002800 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002801 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002802 !(isOpenMPSimdDirective(ParentRegion) ||
2803 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002804 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002805 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2806 // OpenMP [2.16, Nesting of Regions]
2807 // If specified, a teams construct must be contained within a target
2808 // construct.
2809 NestingProhibited = ParentRegion != OMPD_target;
2810 Recommend = ShouldBeInTargetRegion;
2811 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2812 }
2813 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2814 // OpenMP [2.16, Nesting of Regions]
2815 // distribute, parallel, parallel sections, parallel workshare, and the
2816 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2817 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002818 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2819 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002820 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002821 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002822 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2823 // OpenMP 4.5 [2.17 Nesting of Regions]
2824 // The region associated with the distribute construct must be strictly
2825 // nested inside a teams region
2826 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2827 Recommend = ShouldBeInTeamsRegion;
2828 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002829 if (!NestingProhibited &&
2830 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2831 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2832 // OpenMP 4.5 [2.17 Nesting of Regions]
2833 // If a target, target update, target data, target enter data, or
2834 // target exit data construct is encountered during execution of a
2835 // target region, the behavior is unspecified.
2836 NestingProhibited = Stack->hasDirective(
2837 [&OffendingRegion](OpenMPDirectiveKind K,
2838 const DeclarationNameInfo &DNI,
2839 SourceLocation Loc) -> bool {
2840 if (isOpenMPTargetExecutionDirective(K)) {
2841 OffendingRegion = K;
2842 return true;
2843 } else
2844 return false;
2845 },
2846 false /* don't skip top directive */);
2847 CloseNesting = false;
2848 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002849 if (NestingProhibited) {
2850 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002851 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2852 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002853 return true;
2854 }
2855 }
2856 return false;
2857}
2858
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002859static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2860 ArrayRef<OMPClause *> Clauses,
2861 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2862 bool ErrorFound = false;
2863 unsigned NamedModifiersNumber = 0;
2864 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2865 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002866 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002867 for (const auto *C : Clauses) {
2868 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2869 // At most one if clause without a directive-name-modifier can appear on
2870 // the directive.
2871 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2872 if (FoundNameModifiers[CurNM]) {
2873 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2874 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2875 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2876 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002877 } else if (CurNM != OMPD_unknown) {
2878 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002879 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002880 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002881 FoundNameModifiers[CurNM] = IC;
2882 if (CurNM == OMPD_unknown)
2883 continue;
2884 // Check if the specified name modifier is allowed for the current
2885 // directive.
2886 // At most one if clause with the particular directive-name-modifier can
2887 // appear on the directive.
2888 bool MatchFound = false;
2889 for (auto NM : AllowedNameModifiers) {
2890 if (CurNM == NM) {
2891 MatchFound = true;
2892 break;
2893 }
2894 }
2895 if (!MatchFound) {
2896 S.Diag(IC->getNameModifierLoc(),
2897 diag::err_omp_wrong_if_directive_name_modifier)
2898 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2899 ErrorFound = true;
2900 }
2901 }
2902 }
2903 // If any if clause on the directive includes a directive-name-modifier then
2904 // all if clauses on the directive must include a directive-name-modifier.
2905 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2906 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2907 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2908 diag::err_omp_no_more_if_clause);
2909 } else {
2910 std::string Values;
2911 std::string Sep(", ");
2912 unsigned AllowedCnt = 0;
2913 unsigned TotalAllowedNum =
2914 AllowedNameModifiers.size() - NamedModifiersNumber;
2915 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2916 ++Cnt) {
2917 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2918 if (!FoundNameModifiers[NM]) {
2919 Values += "'";
2920 Values += getOpenMPDirectiveName(NM);
2921 Values += "'";
2922 if (AllowedCnt + 2 == TotalAllowedNum)
2923 Values += " or ";
2924 else if (AllowedCnt + 1 != TotalAllowedNum)
2925 Values += Sep;
2926 ++AllowedCnt;
2927 }
2928 }
2929 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2930 diag::err_omp_unnamed_if_clause)
2931 << (TotalAllowedNum > 1) << Values;
2932 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002933 for (auto Loc : NameModifierLoc) {
2934 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2935 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002936 ErrorFound = true;
2937 }
2938 return ErrorFound;
2939}
2940
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002941StmtResult Sema::ActOnOpenMPExecutableDirective(
2942 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2943 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2944 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002945 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002946 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2947 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002948 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002949
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002950 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002951 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002952 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002953 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002954 if (AStmt) {
2955 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2956
2957 // Check default data sharing attributes for referenced variables.
2958 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2959 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2960 if (DSAChecker.isErrorFound())
2961 return StmtError();
2962 // Generate list of implicitly defined firstprivate variables.
2963 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002964
2965 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2966 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2967 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2968 SourceLocation(), SourceLocation())) {
2969 ClausesWithImplicit.push_back(Implicit);
2970 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2971 DSAChecker.getImplicitFirstprivate().size();
2972 } else
2973 ErrorFound = true;
2974 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002975 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002976
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002977 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002978 switch (Kind) {
2979 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002980 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2981 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002982 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002983 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002984 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002985 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2986 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002987 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002988 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002989 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2990 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002991 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002992 case OMPD_for_simd:
2993 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2994 EndLoc, VarsWithInheritedDSA);
2995 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002996 case OMPD_sections:
2997 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2998 EndLoc);
2999 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003000 case OMPD_section:
3001 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003002 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003003 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3004 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003005 case OMPD_single:
3006 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3007 EndLoc);
3008 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003009 case OMPD_master:
3010 assert(ClausesWithImplicit.empty() &&
3011 "No clauses are allowed for 'omp master' directive");
3012 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3013 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003014 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003015 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3016 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003017 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003018 case OMPD_parallel_for:
3019 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3020 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003021 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003022 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003023 case OMPD_parallel_for_simd:
3024 Res = ActOnOpenMPParallelForSimdDirective(
3025 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003026 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003027 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003028 case OMPD_parallel_sections:
3029 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3030 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003031 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003032 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003033 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003034 Res =
3035 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003036 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003037 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003038 case OMPD_taskyield:
3039 assert(ClausesWithImplicit.empty() &&
3040 "No clauses are allowed for 'omp taskyield' directive");
3041 assert(AStmt == nullptr &&
3042 "No associated statement allowed for 'omp taskyield' directive");
3043 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3044 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003045 case OMPD_barrier:
3046 assert(ClausesWithImplicit.empty() &&
3047 "No clauses are allowed for 'omp barrier' directive");
3048 assert(AStmt == nullptr &&
3049 "No associated statement allowed for 'omp barrier' directive");
3050 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3051 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003052 case OMPD_taskwait:
3053 assert(ClausesWithImplicit.empty() &&
3054 "No clauses are allowed for 'omp taskwait' directive");
3055 assert(AStmt == nullptr &&
3056 "No associated statement allowed for 'omp taskwait' directive");
3057 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3058 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003059 case OMPD_taskgroup:
3060 assert(ClausesWithImplicit.empty() &&
3061 "No clauses are allowed for 'omp taskgroup' directive");
3062 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3063 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003064 case OMPD_flush:
3065 assert(AStmt == nullptr &&
3066 "No associated statement allowed for 'omp flush' directive");
3067 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3068 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003069 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003070 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3071 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003072 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003073 case OMPD_atomic:
3074 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3075 EndLoc);
3076 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003077 case OMPD_teams:
3078 Res =
3079 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3080 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003081 case OMPD_target:
3082 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3083 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003084 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003085 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003086 case OMPD_target_parallel:
3087 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3088 StartLoc, EndLoc);
3089 AllowedNameModifiers.push_back(OMPD_target);
3090 AllowedNameModifiers.push_back(OMPD_parallel);
3091 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003092 case OMPD_target_parallel_for:
3093 Res = ActOnOpenMPTargetParallelForDirective(
3094 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3095 AllowedNameModifiers.push_back(OMPD_target);
3096 AllowedNameModifiers.push_back(OMPD_parallel);
3097 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003098 case OMPD_cancellation_point:
3099 assert(ClausesWithImplicit.empty() &&
3100 "No clauses are allowed for 'omp cancellation point' directive");
3101 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3102 "cancellation point' directive");
3103 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3104 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003105 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003106 assert(AStmt == nullptr &&
3107 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003108 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3109 CancelRegion);
3110 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003111 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003112 case OMPD_target_data:
3113 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3114 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003115 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003116 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003117 case OMPD_target_enter_data:
3118 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3119 EndLoc);
3120 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3121 break;
Samuel Antao72590762016-01-19 20:04:50 +00003122 case OMPD_target_exit_data:
3123 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3124 EndLoc);
3125 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3126 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003127 case OMPD_taskloop:
3128 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3129 EndLoc, VarsWithInheritedDSA);
3130 AllowedNameModifiers.push_back(OMPD_taskloop);
3131 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003132 case OMPD_taskloop_simd:
3133 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3134 EndLoc, VarsWithInheritedDSA);
3135 AllowedNameModifiers.push_back(OMPD_taskloop);
3136 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003137 case OMPD_distribute:
3138 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3139 EndLoc, VarsWithInheritedDSA);
3140 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003141 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003142 llvm_unreachable("OpenMP Directive is not allowed");
3143 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003144 llvm_unreachable("Unknown OpenMP directive");
3145 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003146
Alexey Bataev4acb8592014-07-07 13:01:15 +00003147 for (auto P : VarsWithInheritedDSA) {
3148 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3149 << P.first << P.second->getSourceRange();
3150 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003151 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3152
3153 if (!AllowedNameModifiers.empty())
3154 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3155 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003156
Alexey Bataeved09d242014-05-28 05:53:51 +00003157 if (ErrorFound)
3158 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003159 return Res;
3160}
3161
3162StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3163 Stmt *AStmt,
3164 SourceLocation StartLoc,
3165 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003166 if (!AStmt)
3167 return StmtError();
3168
Alexey Bataev9959db52014-05-06 10:08:46 +00003169 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3170 // 1.2.2 OpenMP Language Terminology
3171 // Structured block - An executable statement with a single entry at the
3172 // top and a single exit at the bottom.
3173 // The point of exit cannot be a branch out of the structured block.
3174 // longjmp() and throw() must not violate the entry/exit criteria.
3175 CS->getCapturedDecl()->setNothrow();
3176
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003177 getCurFunction()->setHasBranchProtectedScope();
3178
Alexey Bataev25e5b442015-09-15 12:52:43 +00003179 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3180 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003181}
3182
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003183namespace {
3184/// \brief Helper class for checking canonical form of the OpenMP loops and
3185/// extracting iteration space of each loop in the loop nest, that will be used
3186/// for IR generation.
3187class OpenMPIterationSpaceChecker {
3188 /// \brief Reference to Sema.
3189 Sema &SemaRef;
3190 /// \brief A location for diagnostics (when there is no some better location).
3191 SourceLocation DefaultLoc;
3192 /// \brief A location for diagnostics (when increment is not compatible).
3193 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003194 /// \brief A source location for referring to loop init later.
3195 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003196 /// \brief A source location for referring to condition later.
3197 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003198 /// \brief A source location for referring to increment later.
3199 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003200 /// \brief Loop variable.
3201 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003202 /// \brief Reference to loop variable.
3203 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003204 /// \brief Lower bound (initializer for the var).
3205 Expr *LB;
3206 /// \brief Upper bound.
3207 Expr *UB;
3208 /// \brief Loop step (increment).
3209 Expr *Step;
3210 /// \brief This flag is true when condition is one of:
3211 /// Var < UB
3212 /// Var <= UB
3213 /// UB > Var
3214 /// UB >= Var
3215 bool TestIsLessOp;
3216 /// \brief This flag is true when condition is strict ( < or > ).
3217 bool TestIsStrictOp;
3218 /// \brief This flag is true when step is subtracted on each iteration.
3219 bool SubtractStep;
3220
3221public:
3222 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3223 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003224 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3225 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003226 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3227 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003228 /// \brief Check init-expr for canonical loop form and save loop counter
3229 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003230 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003231 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3232 /// for less/greater and for strict/non-strict comparison.
3233 bool CheckCond(Expr *S);
3234 /// \brief Check incr-expr for canonical loop form and return true if it
3235 /// does not conform, otherwise save loop step (#Step).
3236 bool CheckInc(Expr *S);
3237 /// \brief Return the loop counter variable.
3238 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003239 /// \brief Return the reference expression to loop counter variable.
3240 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003241 /// \brief Source range of the loop init.
3242 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3243 /// \brief Source range of the loop condition.
3244 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3245 /// \brief Source range of the loop increment.
3246 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3247 /// \brief True if the step should be subtracted.
3248 bool ShouldSubtractStep() const { return SubtractStep; }
3249 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003250 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003251 /// \brief Build the precondition expression for the loops.
3252 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003253 /// \brief Build reference expression to the counter be used for codegen.
3254 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003255 /// \brief Build reference expression to the private counter be used for
3256 /// codegen.
3257 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003258 /// \brief Build initization of the counter be used for codegen.
3259 Expr *BuildCounterInit() const;
3260 /// \brief Build step of the counter be used for codegen.
3261 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003262 /// \brief Return true if any expression is dependent.
3263 bool Dependent() const;
3264
3265private:
3266 /// \brief Check the right-hand side of an assignment in the increment
3267 /// expression.
3268 bool CheckIncRHS(Expr *RHS);
3269 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003270 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003271 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003272 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003273 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003274 /// \brief Helper to set loop increment.
3275 bool SetStep(Expr *NewStep, bool Subtract);
3276};
3277
3278bool OpenMPIterationSpaceChecker::Dependent() const {
3279 if (!Var) {
3280 assert(!LB && !UB && !Step);
3281 return false;
3282 }
3283 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3284 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3285}
3286
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003287template <typename T>
3288static T *getExprAsWritten(T *E) {
3289 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3290 E = ExprTemp->getSubExpr();
3291
3292 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3293 E = MTE->GetTemporaryExpr();
3294
3295 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3296 E = Binder->getSubExpr();
3297
3298 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3299 E = ICE->getSubExprAsWritten();
3300 return E->IgnoreParens();
3301}
3302
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003303bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3304 DeclRefExpr *NewVarRefExpr,
3305 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003306 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003307 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3308 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003309 if (!NewVar || !NewLB)
3310 return true;
3311 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003312 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003313 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3314 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003315 if ((Ctor->isCopyOrMoveConstructor() ||
3316 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3317 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003318 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003319 LB = NewLB;
3320 return false;
3321}
3322
3323bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003324 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003325 // State consistency checking to ensure correct usage.
3326 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3327 !TestIsLessOp && !TestIsStrictOp);
3328 if (!NewUB)
3329 return true;
3330 UB = NewUB;
3331 TestIsLessOp = LessOp;
3332 TestIsStrictOp = StrictOp;
3333 ConditionSrcRange = SR;
3334 ConditionLoc = SL;
3335 return false;
3336}
3337
3338bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3339 // State consistency checking to ensure correct usage.
3340 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3341 if (!NewStep)
3342 return true;
3343 if (!NewStep->isValueDependent()) {
3344 // Check that the step is integer expression.
3345 SourceLocation StepLoc = NewStep->getLocStart();
3346 ExprResult Val =
3347 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3348 if (Val.isInvalid())
3349 return true;
3350 NewStep = Val.get();
3351
3352 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3353 // If test-expr is of form var relational-op b and relational-op is < or
3354 // <= then incr-expr must cause var to increase on each iteration of the
3355 // loop. If test-expr is of form var relational-op b and relational-op is
3356 // > or >= then incr-expr must cause var to decrease on each iteration of
3357 // the loop.
3358 // If test-expr is of form b relational-op var and relational-op is < or
3359 // <= then incr-expr must cause var to decrease on each iteration of the
3360 // loop. If test-expr is of form b relational-op var and relational-op is
3361 // > or >= then incr-expr must cause var to increase on each iteration of
3362 // the loop.
3363 llvm::APSInt Result;
3364 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3365 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3366 bool IsConstNeg =
3367 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003368 bool IsConstPos =
3369 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003370 bool IsConstZero = IsConstant && !Result.getBoolValue();
3371 if (UB && (IsConstZero ||
3372 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003373 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003374 SemaRef.Diag(NewStep->getExprLoc(),
3375 diag::err_omp_loop_incr_not_compatible)
3376 << Var << TestIsLessOp << NewStep->getSourceRange();
3377 SemaRef.Diag(ConditionLoc,
3378 diag::note_omp_loop_cond_requres_compatible_incr)
3379 << TestIsLessOp << ConditionSrcRange;
3380 return true;
3381 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003382 if (TestIsLessOp == Subtract) {
3383 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3384 NewStep).get();
3385 Subtract = !Subtract;
3386 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003387 }
3388
3389 Step = NewStep;
3390 SubtractStep = Subtract;
3391 return false;
3392}
3393
Alexey Bataev9c821032015-04-30 04:23:23 +00003394bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003395 // Check init-expr for canonical loop form and save loop counter
3396 // variable - #Var and its initialization value - #LB.
3397 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3398 // var = lb
3399 // integer-type var = lb
3400 // random-access-iterator-type var = lb
3401 // pointer-type var = lb
3402 //
3403 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003404 if (EmitDiags) {
3405 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3406 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003407 return true;
3408 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003409 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003410 if (Expr *E = dyn_cast<Expr>(S))
3411 S = E->IgnoreParens();
3412 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3413 if (BO->getOpcode() == BO_Assign)
3414 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003415 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003416 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003417 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3418 if (DS->isSingleDecl()) {
3419 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003420 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003421 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003422 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003423 SemaRef.Diag(S->getLocStart(),
3424 diag::ext_omp_loop_not_canonical_init)
3425 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003426 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003427 }
3428 }
3429 }
3430 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3431 if (CE->getOperator() == OO_Equal)
3432 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003433 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3434 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003435
Alexey Bataev9c821032015-04-30 04:23:23 +00003436 if (EmitDiags) {
3437 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3438 << S->getSourceRange();
3439 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003440 return true;
3441}
3442
Alexey Bataev23b69422014-06-18 07:08:49 +00003443/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003444/// variable (which may be the loop variable) if possible.
3445static const VarDecl *GetInitVarDecl(const Expr *E) {
3446 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003447 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003448 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003449 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3450 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003451 if ((Ctor->isCopyOrMoveConstructor() ||
3452 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3453 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003454 E = CE->getArg(0)->IgnoreParenImpCasts();
3455 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3456 if (!DRE)
3457 return nullptr;
3458 return dyn_cast<VarDecl>(DRE->getDecl());
3459}
3460
3461bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3462 // Check test-expr for canonical form, save upper-bound UB, flags for
3463 // less/greater and for strict/non-strict comparison.
3464 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3465 // var relational-op b
3466 // b relational-op var
3467 //
3468 if (!S) {
3469 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3470 return true;
3471 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003472 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003473 SourceLocation CondLoc = S->getLocStart();
3474 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3475 if (BO->isRelationalOp()) {
3476 if (GetInitVarDecl(BO->getLHS()) == Var)
3477 return SetUB(BO->getRHS(),
3478 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3479 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3480 BO->getSourceRange(), BO->getOperatorLoc());
3481 if (GetInitVarDecl(BO->getRHS()) == Var)
3482 return SetUB(BO->getLHS(),
3483 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3484 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3485 BO->getSourceRange(), BO->getOperatorLoc());
3486 }
3487 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3488 if (CE->getNumArgs() == 2) {
3489 auto Op = CE->getOperator();
3490 switch (Op) {
3491 case OO_Greater:
3492 case OO_GreaterEqual:
3493 case OO_Less:
3494 case OO_LessEqual:
3495 if (GetInitVarDecl(CE->getArg(0)) == Var)
3496 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3497 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3498 CE->getOperatorLoc());
3499 if (GetInitVarDecl(CE->getArg(1)) == Var)
3500 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3501 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3502 CE->getOperatorLoc());
3503 break;
3504 default:
3505 break;
3506 }
3507 }
3508 }
3509 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3510 << S->getSourceRange() << Var;
3511 return true;
3512}
3513
3514bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3515 // RHS of canonical loop form increment can be:
3516 // var + incr
3517 // incr + var
3518 // var - incr
3519 //
3520 RHS = RHS->IgnoreParenImpCasts();
3521 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3522 if (BO->isAdditiveOp()) {
3523 bool IsAdd = BO->getOpcode() == BO_Add;
3524 if (GetInitVarDecl(BO->getLHS()) == Var)
3525 return SetStep(BO->getRHS(), !IsAdd);
3526 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3527 return SetStep(BO->getLHS(), false);
3528 }
3529 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3530 bool IsAdd = CE->getOperator() == OO_Plus;
3531 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3532 if (GetInitVarDecl(CE->getArg(0)) == Var)
3533 return SetStep(CE->getArg(1), !IsAdd);
3534 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3535 return SetStep(CE->getArg(0), false);
3536 }
3537 }
3538 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3539 << RHS->getSourceRange() << Var;
3540 return true;
3541}
3542
3543bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3544 // Check incr-expr for canonical loop form and return true if it
3545 // does not conform.
3546 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3547 // ++var
3548 // var++
3549 // --var
3550 // var--
3551 // var += incr
3552 // var -= incr
3553 // var = var + incr
3554 // var = incr + var
3555 // var = var - incr
3556 //
3557 if (!S) {
3558 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3559 return true;
3560 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003561 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003562 S = S->IgnoreParens();
3563 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3564 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3565 return SetStep(
3566 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3567 (UO->isDecrementOp() ? -1 : 1)).get(),
3568 false);
3569 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3570 switch (BO->getOpcode()) {
3571 case BO_AddAssign:
3572 case BO_SubAssign:
3573 if (GetInitVarDecl(BO->getLHS()) == Var)
3574 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3575 break;
3576 case BO_Assign:
3577 if (GetInitVarDecl(BO->getLHS()) == Var)
3578 return CheckIncRHS(BO->getRHS());
3579 break;
3580 default:
3581 break;
3582 }
3583 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3584 switch (CE->getOperator()) {
3585 case OO_PlusPlus:
3586 case OO_MinusMinus:
3587 if (GetInitVarDecl(CE->getArg(0)) == Var)
3588 return SetStep(
3589 SemaRef.ActOnIntegerConstant(
3590 CE->getLocStart(),
3591 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3592 false);
3593 break;
3594 case OO_PlusEqual:
3595 case OO_MinusEqual:
3596 if (GetInitVarDecl(CE->getArg(0)) == Var)
3597 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3598 break;
3599 case OO_Equal:
3600 if (GetInitVarDecl(CE->getArg(0)) == Var)
3601 return CheckIncRHS(CE->getArg(1));
3602 break;
3603 default:
3604 break;
3605 }
3606 }
3607 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3608 << S->getSourceRange() << Var;
3609 return true;
3610}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003611
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003612namespace {
3613// Transform variables declared in GNU statement expressions to new ones to
3614// avoid crash on codegen.
3615class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3616 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3617
3618public:
3619 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3620
3621 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3622 if (auto *VD = cast<VarDecl>(D))
3623 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3624 !isa<ImplicitParamDecl>(D)) {
3625 auto *NewVD = VarDecl::Create(
3626 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3627 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3628 VD->getTypeSourceInfo(), VD->getStorageClass());
3629 NewVD->setTSCSpec(VD->getTSCSpec());
3630 NewVD->setInit(VD->getInit());
3631 NewVD->setInitStyle(VD->getInitStyle());
3632 NewVD->setExceptionVariable(VD->isExceptionVariable());
3633 NewVD->setNRVOVariable(VD->isNRVOVariable());
Alexey Bataev11481f52016-02-17 10:29:05 +00003634 NewVD->setCXXForRangeDecl(VD->isCXXForRangeDecl());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003635 NewVD->setConstexpr(VD->isConstexpr());
3636 NewVD->setInitCapture(VD->isInitCapture());
3637 NewVD->setPreviousDeclInSameBlockScope(
3638 VD->isPreviousDeclInSameBlockScope());
3639 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003640 if (VD->hasAttrs())
3641 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003642 transformedLocalDecl(VD, NewVD);
3643 return NewVD;
3644 }
3645 return BaseTransform::TransformDefinition(Loc, D);
3646 }
3647
3648 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3649 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3650 if (E->getDecl() != NewD) {
3651 NewD->setReferenced();
3652 NewD->markUsed(SemaRef.Context);
3653 return DeclRefExpr::Create(
3654 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3655 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3656 E->getNameInfo(), E->getType(), E->getValueKind());
3657 }
3658 return BaseTransform::TransformDeclRefExpr(E);
3659 }
3660};
3661}
3662
Alexander Musmana5f070a2014-10-01 06:03:56 +00003663/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003664Expr *
3665OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3666 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003667 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003668 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003669 auto VarType = Var->getType().getNonReferenceType();
3670 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003671 SemaRef.getLangOpts().CPlusPlus) {
3672 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003673 auto *UBExpr = TestIsLessOp ? UB : LB;
3674 auto *LBExpr = TestIsLessOp ? LB : UB;
3675 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3676 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3677 if (!Upper || !Lower)
3678 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003679 if (!SemaRef.Context.hasSameType(Upper->getType(), UBExpr->getType())) {
3680 Upper = SemaRef
3681 .PerformImplicitConversion(Upper, UBExpr->getType(),
3682 Sema::AA_Converting,
3683 /*AllowExplicit=*/true)
3684 .get();
3685 }
3686 if (!SemaRef.Context.hasSameType(Lower->getType(), LBExpr->getType())) {
3687 Lower = SemaRef
3688 .PerformImplicitConversion(Lower, LBExpr->getType(),
3689 Sema::AA_Converting,
3690 /*AllowExplicit=*/true)
3691 .get();
3692 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003693 if (!Upper || !Lower)
3694 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003695
3696 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3697
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003698 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003699 // BuildBinOp already emitted error, this one is to point user to upper
3700 // and lower bound, and to tell what is passed to 'operator-'.
3701 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3702 << Upper->getSourceRange() << Lower->getSourceRange();
3703 return nullptr;
3704 }
3705 }
3706
3707 if (!Diff.isUsable())
3708 return nullptr;
3709
3710 // Upper - Lower [- 1]
3711 if (TestIsStrictOp)
3712 Diff = SemaRef.BuildBinOp(
3713 S, DefaultLoc, BO_Sub, Diff.get(),
3714 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3715 if (!Diff.isUsable())
3716 return nullptr;
3717
3718 // Upper - Lower [- 1] + Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003719 auto *StepNoImp = Step->IgnoreImplicit();
3720 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003721 if (NewStep.isInvalid())
3722 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003723 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3724 StepNoImp->getType())) {
3725 NewStep = SemaRef.PerformImplicitConversion(
3726 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3727 /*AllowExplicit=*/true);
3728 if (NewStep.isInvalid())
3729 return nullptr;
3730 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003731 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003732 if (!Diff.isUsable())
3733 return nullptr;
3734
3735 // Parentheses (for dumping/debugging purposes only).
3736 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3737 if (!Diff.isUsable())
3738 return nullptr;
3739
3740 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003741 NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003742 if (NewStep.isInvalid())
3743 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003744 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3745 StepNoImp->getType())) {
3746 NewStep = SemaRef.PerformImplicitConversion(
3747 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3748 /*AllowExplicit=*/true);
3749 if (NewStep.isInvalid())
3750 return nullptr;
3751 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003752 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003753 if (!Diff.isUsable())
3754 return nullptr;
3755
Alexander Musman174b3ca2014-10-06 11:16:29 +00003756 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003757 QualType Type = Diff.get()->getType();
3758 auto &C = SemaRef.Context;
3759 bool UseVarType = VarType->hasIntegerRepresentation() &&
3760 C.getTypeSize(Type) > C.getTypeSize(VarType);
3761 if (!Type->isIntegerType() || UseVarType) {
3762 unsigned NewSize =
3763 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3764 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3765 : Type->hasSignedIntegerRepresentation();
3766 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003767 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3768 Diff = SemaRef.PerformImplicitConversion(
3769 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3770 if (!Diff.isUsable())
3771 return nullptr;
3772 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003773 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003774 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003775 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3776 if (NewSize != C.getTypeSize(Type)) {
3777 if (NewSize < C.getTypeSize(Type)) {
3778 assert(NewSize == 64 && "incorrect loop var size");
3779 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3780 << InitSrcRange << ConditionSrcRange;
3781 }
3782 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003783 NewSize, Type->hasSignedIntegerRepresentation() ||
3784 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003785 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3786 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3787 Sema::AA_Converting, true);
3788 if (!Diff.isUsable())
3789 return nullptr;
3790 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003791 }
3792 }
3793
Alexander Musmana5f070a2014-10-01 06:03:56 +00003794 return Diff.get();
3795}
3796
Alexey Bataev62dbb972015-04-22 11:59:37 +00003797Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3798 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3799 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3800 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003801 TransformToNewDefs Transform(SemaRef);
3802
3803 auto NewLB = Transform.TransformExpr(LB);
3804 auto NewUB = Transform.TransformExpr(UB);
3805 if (NewLB.isInvalid() || NewUB.isInvalid())
3806 return Cond;
Alexey Bataev11481f52016-02-17 10:29:05 +00003807 if (!SemaRef.Context.hasSameType(NewLB.get()->getType(), LB->getType())) {
3808 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3809 Sema::AA_Converting,
3810 /*AllowExplicit=*/true);
3811 }
3812 if (!SemaRef.Context.hasSameType(NewUB.get()->getType(), UB->getType())) {
3813 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3814 Sema::AA_Converting,
3815 /*AllowExplicit=*/true);
3816 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003817 if (NewLB.isInvalid() || NewUB.isInvalid())
3818 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003819 auto CondExpr = SemaRef.BuildBinOp(
3820 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3821 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003822 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003823 if (CondExpr.isUsable()) {
Alexey Bataev11481f52016-02-17 10:29:05 +00003824 if (!SemaRef.Context.hasSameType(CondExpr.get()->getType(),
3825 SemaRef.Context.BoolTy))
3826 CondExpr = SemaRef.PerformImplicitConversion(
3827 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3828 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003829 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003830 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3831 // Otherwise use original loop conditon and evaluate it in runtime.
3832 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3833}
3834
Alexander Musmana5f070a2014-10-01 06:03:56 +00003835/// \brief Build reference expression to the counter be used for codegen.
3836Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003837 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3838 DefaultLoc);
3839}
3840
3841Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3842 if (Var && !Var->isInvalidDecl()) {
3843 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003844 auto *PrivateVar =
3845 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3846 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003847 if (PrivateVar->isInvalidDecl())
3848 return nullptr;
3849 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3850 }
3851 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003852}
3853
3854/// \brief Build initization of the counter be used for codegen.
3855Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3856
3857/// \brief Build step of the counter be used for codegen.
3858Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3859
3860/// \brief Iteration space of a single for loop.
3861struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003862 /// \brief Condition of the loop.
3863 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003864 /// \brief This expression calculates the number of iterations in the loop.
3865 /// It is always possible to calculate it before starting the loop.
3866 Expr *NumIterations;
3867 /// \brief The loop counter variable.
3868 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003869 /// \brief Private loop counter variable.
3870 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003871 /// \brief This is initializer for the initial value of #CounterVar.
3872 Expr *CounterInit;
3873 /// \brief This is step for the #CounterVar used to generate its update:
3874 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3875 Expr *CounterStep;
3876 /// \brief Should step be subtracted?
3877 bool Subtract;
3878 /// \brief Source range of the loop init.
3879 SourceRange InitSrcRange;
3880 /// \brief Source range of the loop condition.
3881 SourceRange CondSrcRange;
3882 /// \brief Source range of the loop increment.
3883 SourceRange IncSrcRange;
3884};
3885
Alexey Bataev23b69422014-06-18 07:08:49 +00003886} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003887
Alexey Bataev9c821032015-04-30 04:23:23 +00003888void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3889 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3890 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003891 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3892 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003893 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3894 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003895 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003896 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003897 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003898 }
3899}
3900
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003901/// \brief Called on a for stmt to check and extract its iteration space
3902/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003903static bool CheckOpenMPIterationSpace(
3904 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3905 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003906 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003907 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003908 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003909 // OpenMP [2.6, Canonical Loop Form]
3910 // for (init-expr; test-expr; incr-expr) structured-block
3911 auto For = dyn_cast_or_null<ForStmt>(S);
3912 if (!For) {
3913 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003914 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3915 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3916 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3917 if (NestedLoopCount > 1) {
3918 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3919 SemaRef.Diag(DSA.getConstructLoc(),
3920 diag::note_omp_collapse_ordered_expr)
3921 << 2 << CollapseLoopCountExpr->getSourceRange()
3922 << OrderedLoopCountExpr->getSourceRange();
3923 else if (CollapseLoopCountExpr)
3924 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3925 diag::note_omp_collapse_ordered_expr)
3926 << 0 << CollapseLoopCountExpr->getSourceRange();
3927 else
3928 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3929 diag::note_omp_collapse_ordered_expr)
3930 << 1 << OrderedLoopCountExpr->getSourceRange();
3931 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003932 return true;
3933 }
3934 assert(For->getBody());
3935
3936 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3937
3938 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003939 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 if (ISC.CheckInit(Init)) {
3941 return true;
3942 }
3943
3944 bool HasErrors = false;
3945
3946 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003947 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003948
3949 // OpenMP [2.6, Canonical Loop Form]
3950 // Var is one of the following:
3951 // A variable of signed or unsigned integer type.
3952 // For C++, a variable of a random access iterator type.
3953 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003954 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003955 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3956 !VarType->isPointerType() &&
3957 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3958 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3959 << SemaRef.getLangOpts().CPlusPlus;
3960 HasErrors = true;
3961 }
3962
Alexey Bataev4acb8592014-07-07 13:01:15 +00003963 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3964 // Construct
3965 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3966 // parallel for construct is (are) private.
3967 // The loop iteration variable in the associated for-loop of a simd construct
3968 // with just one associated for-loop is linear with a constant-linear-step
3969 // that is the increment of the associated for-loop.
3970 // Exclude loop var from the list of variables with implicitly defined data
3971 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003972 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003973
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003974 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3975 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003976 // The loop iteration variable in the associated for-loop of a simd construct
3977 // with just one associated for-loop may be listed in a linear clause with a
3978 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003979 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3980 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003981 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003982 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3983 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3984 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003985 auto PredeterminedCKind =
3986 isOpenMPSimdDirective(DKind)
3987 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3988 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003989 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003990 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003991 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003992 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003993 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003994 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3995 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003997 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3998 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003999 if (DVar.RefExpr == nullptr)
4000 DVar.CKind = PredeterminedCKind;
4001 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004003 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00004004 // Make the loop iteration variable private (for worksharing constructs),
4005 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00004006 // lastprivate (for simd directives with several collapsed or ordered
4007 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00004008 if (DVar.CKind == OMPC_unknown)
4009 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
4010 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00004011 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004012 }
4013
Alexey Bataev7ff55242014-06-19 09:13:45 +00004014 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00004015
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004016 // Check test-expr.
4017 HasErrors |= ISC.CheckCond(For->getCond());
4018
4019 // Check incr-expr.
4020 HasErrors |= ISC.CheckInc(For->getInc());
4021
Alexander Musmana5f070a2014-10-01 06:03:56 +00004022 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004023 return HasErrors;
4024
Alexander Musmana5f070a2014-10-01 06:03:56 +00004025 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004026 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00004027 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004028 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004029 isOpenMPTaskLoopDirective(DKind) ||
4030 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004031 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00004032 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4034 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4035 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4036 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4037 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4038 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4039
Alexey Bataev62dbb972015-04-22 11:59:37 +00004040 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4041 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004042 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004043 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004044 ResultIterSpace.CounterInit == nullptr ||
4045 ResultIterSpace.CounterStep == nullptr);
4046
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004047 return HasErrors;
4048}
4049
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004050/// \brief Build 'VarRef = Start.
4051static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4052 ExprResult VarRef, ExprResult Start) {
4053 TransformToNewDefs Transform(SemaRef);
4054 // Build 'VarRef = Start.
Alexey Bataev11481f52016-02-17 10:29:05 +00004055 auto *StartNoImp = Start.get()->IgnoreImplicit();
4056 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004057 if (NewStart.isInvalid())
4058 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004059 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4060 StartNoImp->getType())) {
4061 NewStart = SemaRef.PerformImplicitConversion(
4062 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4063 /*AllowExplicit=*/true);
4064 if (NewStart.isInvalid())
4065 return ExprError();
4066 }
4067 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4068 VarRef.get()->getType())) {
4069 NewStart = SemaRef.PerformImplicitConversion(
4070 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4071 /*AllowExplicit=*/true);
4072 if (!NewStart.isUsable())
4073 return ExprError();
4074 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004075
4076 auto Init =
4077 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4078 return Init;
4079}
4080
Alexander Musmana5f070a2014-10-01 06:03:56 +00004081/// \brief Build 'VarRef = Start + Iter * Step'.
4082static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4083 SourceLocation Loc, ExprResult VarRef,
4084 ExprResult Start, ExprResult Iter,
4085 ExprResult Step, bool Subtract) {
4086 // Add parentheses (for debugging purposes only).
4087 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4088 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4089 !Step.isUsable())
4090 return ExprError();
4091
Alexey Bataev11481f52016-02-17 10:29:05 +00004092 auto *StepNoImp = Step.get()->IgnoreImplicit();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004093 TransformToNewDefs Transform(SemaRef);
Alexey Bataev11481f52016-02-17 10:29:05 +00004094 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004095 if (NewStep.isInvalid())
4096 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004097 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
4098 StepNoImp->getType())) {
4099 NewStep = SemaRef.PerformImplicitConversion(
4100 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
4101 /*AllowExplicit=*/true);
4102 if (NewStep.isInvalid())
4103 return ExprError();
4104 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004105 ExprResult Update =
4106 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004107 if (!Update.isUsable())
4108 return ExprError();
4109
Alexey Bataevc0214e02016-02-16 12:13:49 +00004110 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4111 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev11481f52016-02-17 10:29:05 +00004112 auto *StartNoImp = Start.get()->IgnoreImplicit();
4113 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004114 if (NewStart.isInvalid())
4115 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004116 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4117 StartNoImp->getType())) {
4118 NewStart = SemaRef.PerformImplicitConversion(
4119 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4120 /*AllowExplicit=*/true);
4121 if (NewStart.isInvalid())
4122 return ExprError();
4123 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004124
Alexey Bataevc0214e02016-02-16 12:13:49 +00004125 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4126 ExprResult SavedUpdate = Update;
4127 ExprResult UpdateVal;
4128 if (VarRef.get()->getType()->isOverloadableType() ||
4129 NewStart.get()->getType()->isOverloadableType() ||
4130 Update.get()->getType()->isOverloadableType()) {
4131 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4132 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4133 Update =
4134 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4135 if (Update.isUsable()) {
4136 UpdateVal =
4137 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4138 VarRef.get(), SavedUpdate.get());
4139 if (UpdateVal.isUsable()) {
4140 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4141 UpdateVal.get());
4142 }
4143 }
4144 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4145 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004146
Alexey Bataevc0214e02016-02-16 12:13:49 +00004147 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4148 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4149 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4150 NewStart.get(), SavedUpdate.get());
4151 if (!Update.isUsable())
4152 return ExprError();
4153
Alexey Bataev11481f52016-02-17 10:29:05 +00004154 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4155 VarRef.get()->getType())) {
4156 Update = SemaRef.PerformImplicitConversion(
4157 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4158 if (!Update.isUsable())
4159 return ExprError();
4160 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004161
4162 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4163 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004164 return Update;
4165}
4166
4167/// \brief Convert integer expression \a E to make it have at least \a Bits
4168/// bits.
4169static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4170 Sema &SemaRef) {
4171 if (E == nullptr)
4172 return ExprError();
4173 auto &C = SemaRef.Context;
4174 QualType OldType = E->getType();
4175 unsigned HasBits = C.getTypeSize(OldType);
4176 if (HasBits >= Bits)
4177 return ExprResult(E);
4178 // OK to convert to signed, because new type has more bits than old.
4179 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4180 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4181 true);
4182}
4183
4184/// \brief Check if the given expression \a E is a constant integer that fits
4185/// into \a Bits bits.
4186static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4187 if (E == nullptr)
4188 return false;
4189 llvm::APSInt Result;
4190 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4191 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4192 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193}
4194
4195/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004196/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4197/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004198static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004199CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4200 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4201 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004202 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004203 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004204 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004205 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004206 // Found 'collapse' clause - calculate collapse number.
4207 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004208 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004209 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004210 }
4211 if (OrderedLoopCountExpr) {
4212 // Found 'ordered' clause - calculate collapse number.
4213 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004214 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4215 if (Result.getLimitedValue() < NestedLoopCount) {
4216 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4217 diag::err_omp_wrong_ordered_loop_count)
4218 << OrderedLoopCountExpr->getSourceRange();
4219 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4220 diag::note_collapse_loop_count)
4221 << CollapseLoopCountExpr->getSourceRange();
4222 }
4223 NestedLoopCount = Result.getLimitedValue();
4224 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004225 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004226 // This is helper routine for loop directives (e.g., 'for', 'simd',
4227 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004228 SmallVector<LoopIterationSpace, 4> IterSpaces;
4229 IterSpaces.resize(NestedLoopCount);
4230 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004231 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004232 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004233 NestedLoopCount, CollapseLoopCountExpr,
4234 OrderedLoopCountExpr, VarsWithImplicitDSA,
4235 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004236 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004237 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004238 // OpenMP [2.8.1, simd construct, Restrictions]
4239 // All loops associated with the construct must be perfectly nested; that
4240 // is, there must be no intervening code nor any OpenMP directive between
4241 // any two loops.
4242 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004243 }
4244
Alexander Musmana5f070a2014-10-01 06:03:56 +00004245 Built.clear(/* size */ NestedLoopCount);
4246
4247 if (SemaRef.CurContext->isDependentContext())
4248 return NestedLoopCount;
4249
4250 // An example of what is generated for the following code:
4251 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004252 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004253 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004254 // for (k = 0; k < NK; ++k)
4255 // for (j = J0; j < NJ; j+=2) {
4256 // <loop body>
4257 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004258 //
4259 // We generate the code below.
4260 // Note: the loop body may be outlined in CodeGen.
4261 // Note: some counters may be C++ classes, operator- is used to find number of
4262 // iterations and operator+= to calculate counter value.
4263 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4264 // or i64 is currently supported).
4265 //
4266 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4267 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4268 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4269 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4270 // // similar updates for vars in clauses (e.g. 'linear')
4271 // <loop body (using local i and j)>
4272 // }
4273 // i = NI; // assign final values of counters
4274 // j = NJ;
4275 //
4276
4277 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4278 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004279 // Precondition tests if there is at least one iteration (all conditions are
4280 // true).
4281 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004282 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004283 ExprResult LastIteration32 = WidenIterationCount(
4284 32 /* Bits */, SemaRef.PerformImplicitConversion(
4285 N0->IgnoreImpCasts(), N0->getType(),
4286 Sema::AA_Converting, /*AllowExplicit=*/true)
4287 .get(),
4288 SemaRef);
4289 ExprResult LastIteration64 = WidenIterationCount(
4290 64 /* Bits */, SemaRef.PerformImplicitConversion(
4291 N0->IgnoreImpCasts(), N0->getType(),
4292 Sema::AA_Converting, /*AllowExplicit=*/true)
4293 .get(),
4294 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004295
4296 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4297 return NestedLoopCount;
4298
4299 auto &C = SemaRef.Context;
4300 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4301
4302 Scope *CurScope = DSA.getCurScope();
4303 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004304 if (PreCond.isUsable()) {
4305 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4306 PreCond.get(), IterSpaces[Cnt].PreCond);
4307 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004308 auto N = IterSpaces[Cnt].NumIterations;
4309 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4310 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004311 LastIteration32 = SemaRef.BuildBinOp(
4312 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4313 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4314 Sema::AA_Converting,
4315 /*AllowExplicit=*/true)
4316 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004317 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004318 LastIteration64 = SemaRef.BuildBinOp(
4319 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4320 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4321 Sema::AA_Converting,
4322 /*AllowExplicit=*/true)
4323 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004324 }
4325
4326 // Choose either the 32-bit or 64-bit version.
4327 ExprResult LastIteration = LastIteration64;
4328 if (LastIteration32.isUsable() &&
4329 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4330 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4331 FitsInto(
4332 32 /* Bits */,
4333 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4334 LastIteration64.get(), SemaRef)))
4335 LastIteration = LastIteration32;
4336
4337 if (!LastIteration.isUsable())
4338 return 0;
4339
4340 // Save the number of iterations.
4341 ExprResult NumIterations = LastIteration;
4342 {
4343 LastIteration = SemaRef.BuildBinOp(
4344 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4345 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4346 if (!LastIteration.isUsable())
4347 return 0;
4348 }
4349
4350 // Calculate the last iteration number beforehand instead of doing this on
4351 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4352 llvm::APSInt Result;
4353 bool IsConstant =
4354 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4355 ExprResult CalcLastIteration;
4356 if (!IsConstant) {
4357 SourceLocation SaveLoc;
4358 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004359 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004361 ExprResult SaveRef = buildDeclRefExpr(
4362 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004363 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4364 SaveRef.get(), LastIteration.get());
4365 LastIteration = SaveRef;
4366
4367 // Prepare SaveRef + 1.
4368 NumIterations = SemaRef.BuildBinOp(
4369 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4370 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4371 if (!NumIterations.isUsable())
4372 return 0;
4373 }
4374
4375 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4376
Alexander Musmanc6388682014-12-15 07:07:06 +00004377 QualType VType = LastIteration.get()->getType();
4378 // Build variables passed into runtime, nesessary for worksharing directives.
4379 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004380 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4381 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004382 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004383 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4384 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004385 SemaRef.AddInitializerToDecl(
4386 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4387 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4388
4389 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004390 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4391 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004392 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4393 /*DirectInit*/ false,
4394 /*TypeMayContainAuto*/ false);
4395
4396 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4397 // This will be used to implement clause 'lastprivate'.
4398 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004399 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4400 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004401 SemaRef.AddInitializerToDecl(
4402 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4403 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4404
4405 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004406 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4407 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004408 SemaRef.AddInitializerToDecl(
4409 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4410 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4411
4412 // Build expression: UB = min(UB, LastIteration)
4413 // It is nesessary for CodeGen of directives with static scheduling.
4414 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4415 UB.get(), LastIteration.get());
4416 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4417 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4418 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4419 CondOp.get());
4420 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4421 }
4422
4423 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004424 ExprResult IV;
4425 ExprResult Init;
4426 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004427 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4428 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004429 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004430 isOpenMPTaskLoopDirective(DKind) ||
4431 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004432 ? LB.get()
4433 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4434 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4435 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004436 }
4437
Alexander Musmanc6388682014-12-15 07:07:06 +00004438 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004439 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004440 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004441 (isOpenMPWorksharingDirective(DKind) ||
4442 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004443 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4444 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4445 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004446
4447 // Loop increment (IV = IV + 1)
4448 SourceLocation IncLoc;
4449 ExprResult Inc =
4450 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4451 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4452 if (!Inc.isUsable())
4453 return 0;
4454 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004455 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4456 if (!Inc.isUsable())
4457 return 0;
4458
4459 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4460 // Used for directives with static scheduling.
4461 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004462 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4463 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004464 // LB + ST
4465 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4466 if (!NextLB.isUsable())
4467 return 0;
4468 // LB = LB + ST
4469 NextLB =
4470 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4471 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4472 if (!NextLB.isUsable())
4473 return 0;
4474 // UB + ST
4475 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4476 if (!NextUB.isUsable())
4477 return 0;
4478 // UB = UB + ST
4479 NextUB =
4480 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4481 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4482 if (!NextUB.isUsable())
4483 return 0;
4484 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004485
4486 // Build updates and final values of the loop counters.
4487 bool HasErrors = false;
4488 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004489 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004490 Built.Updates.resize(NestedLoopCount);
4491 Built.Finals.resize(NestedLoopCount);
4492 {
4493 ExprResult Div;
4494 // Go from inner nested loop to outer.
4495 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4496 LoopIterationSpace &IS = IterSpaces[Cnt];
4497 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4498 // Build: Iter = (IV / Div) % IS.NumIters
4499 // where Div is product of previous iterations' IS.NumIters.
4500 ExprResult Iter;
4501 if (Div.isUsable()) {
4502 Iter =
4503 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4504 } else {
4505 Iter = IV;
4506 assert((Cnt == (int)NestedLoopCount - 1) &&
4507 "unusable div expected on first iteration only");
4508 }
4509
4510 if (Cnt != 0 && Iter.isUsable())
4511 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4512 IS.NumIterations);
4513 if (!Iter.isUsable()) {
4514 HasErrors = true;
4515 break;
4516 }
4517
Alexey Bataev39f915b82015-05-08 10:41:21 +00004518 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4519 auto *CounterVar = buildDeclRefExpr(
4520 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4521 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4522 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004523 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4524 IS.CounterInit);
4525 if (!Init.isUsable()) {
4526 HasErrors = true;
4527 break;
4528 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004529 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004530 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004531 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4532 if (!Update.isUsable()) {
4533 HasErrors = true;
4534 break;
4535 }
4536
4537 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4538 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004539 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004540 IS.NumIterations, IS.CounterStep, IS.Subtract);
4541 if (!Final.isUsable()) {
4542 HasErrors = true;
4543 break;
4544 }
4545
4546 // Build Div for the next iteration: Div <- Div * IS.NumIters
4547 if (Cnt != 0) {
4548 if (Div.isUnset())
4549 Div = IS.NumIterations;
4550 else
4551 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4552 IS.NumIterations);
4553
4554 // Add parentheses (for debugging purposes only).
4555 if (Div.isUsable())
4556 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4557 if (!Div.isUsable()) {
4558 HasErrors = true;
4559 break;
4560 }
4561 }
4562 if (!Update.isUsable() || !Final.isUsable()) {
4563 HasErrors = true;
4564 break;
4565 }
4566 // Save results
4567 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004568 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004569 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004570 Built.Updates[Cnt] = Update.get();
4571 Built.Finals[Cnt] = Final.get();
4572 }
4573 }
4574
4575 if (HasErrors)
4576 return 0;
4577
4578 // Save results
4579 Built.IterationVarRef = IV.get();
4580 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004581 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004582 Built.CalcLastIteration =
4583 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004584 Built.PreCond = PreCond.get();
4585 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004586 Built.Init = Init.get();
4587 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004588 Built.LB = LB.get();
4589 Built.UB = UB.get();
4590 Built.IL = IL.get();
4591 Built.ST = ST.get();
4592 Built.EUB = EUB.get();
4593 Built.NLB = NextLB.get();
4594 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004595
Alexey Bataevabfc0692014-06-25 06:52:00 +00004596 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004597}
4598
Alexey Bataev10e775f2015-07-30 11:36:16 +00004599static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004600 auto CollapseClauses =
4601 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4602 if (CollapseClauses.begin() != CollapseClauses.end())
4603 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004604 return nullptr;
4605}
4606
Alexey Bataev10e775f2015-07-30 11:36:16 +00004607static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004608 auto OrderedClauses =
4609 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4610 if (OrderedClauses.begin() != OrderedClauses.end())
4611 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004612 return nullptr;
4613}
4614
Alexey Bataev66b15b52015-08-21 11:14:16 +00004615static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4616 const Expr *Safelen) {
4617 llvm::APSInt SimdlenRes, SafelenRes;
4618 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4619 Simdlen->isInstantiationDependent() ||
4620 Simdlen->containsUnexpandedParameterPack())
4621 return false;
4622 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4623 Safelen->isInstantiationDependent() ||
4624 Safelen->containsUnexpandedParameterPack())
4625 return false;
4626 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4627 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4628 // 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 if (SimdlenRes > SafelenRes) {
4632 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4633 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4634 return true;
4635 }
4636 return false;
4637}
4638
Alexey Bataev4acb8592014-07-07 13:01:15 +00004639StmtResult Sema::ActOnOpenMPSimdDirective(
4640 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4641 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004642 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004643 if (!AStmt)
4644 return StmtError();
4645
4646 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004647 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004648 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4649 // define the nested loops number.
4650 unsigned NestedLoopCount = CheckOpenMPLoop(
4651 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4652 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004653 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004654 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004655
Alexander Musmana5f070a2014-10-01 06:03:56 +00004656 assert((CurContext->isDependentContext() || B.builtAll()) &&
4657 "omp simd loop exprs were not built");
4658
Alexander Musman3276a272015-03-21 10:12:56 +00004659 if (!CurContext->isDependentContext()) {
4660 // Finalize the clauses that need pre-built expressions for CodeGen.
4661 for (auto C : Clauses) {
4662 if (auto LC = dyn_cast<OMPLinearClause>(C))
4663 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4664 B.NumIterations, *this, CurScope))
4665 return StmtError();
4666 }
4667 }
4668
Alexey Bataev66b15b52015-08-21 11:14:16 +00004669 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4670 // If both simdlen and safelen clauses are specified, the value of the simdlen
4671 // parameter must be less than or equal to the value of the safelen parameter.
4672 OMPSafelenClause *Safelen = nullptr;
4673 OMPSimdlenClause *Simdlen = nullptr;
4674 for (auto *Clause : Clauses) {
4675 if (Clause->getClauseKind() == OMPC_safelen)
4676 Safelen = cast<OMPSafelenClause>(Clause);
4677 else if (Clause->getClauseKind() == OMPC_simdlen)
4678 Simdlen = cast<OMPSimdlenClause>(Clause);
4679 if (Safelen && Simdlen)
4680 break;
4681 }
4682 if (Simdlen && Safelen &&
4683 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4684 Safelen->getSafelen()))
4685 return StmtError();
4686
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004687 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004688 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4689 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004690}
4691
Alexey Bataev4acb8592014-07-07 13:01:15 +00004692StmtResult Sema::ActOnOpenMPForDirective(
4693 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4694 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004695 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004696 if (!AStmt)
4697 return StmtError();
4698
4699 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004700 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004701 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4702 // define the nested loops number.
4703 unsigned NestedLoopCount = CheckOpenMPLoop(
4704 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4705 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004706 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004707 return StmtError();
4708
Alexander Musmana5f070a2014-10-01 06:03:56 +00004709 assert((CurContext->isDependentContext() || B.builtAll()) &&
4710 "omp for loop exprs were not built");
4711
Alexey Bataev54acd402015-08-04 11:18:19 +00004712 if (!CurContext->isDependentContext()) {
4713 // Finalize the clauses that need pre-built expressions for CodeGen.
4714 for (auto C : Clauses) {
4715 if (auto LC = dyn_cast<OMPLinearClause>(C))
4716 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4717 B.NumIterations, *this, CurScope))
4718 return StmtError();
4719 }
4720 }
4721
Alexey Bataevf29276e2014-06-18 04:14:57 +00004722 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004723 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004724 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004725}
4726
Alexander Musmanf82886e2014-09-18 05:12:34 +00004727StmtResult Sema::ActOnOpenMPForSimdDirective(
4728 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4729 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004730 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004731 if (!AStmt)
4732 return StmtError();
4733
4734 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004735 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004736 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4737 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004738 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004739 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4740 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4741 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004742 if (NestedLoopCount == 0)
4743 return StmtError();
4744
Alexander Musmanc6388682014-12-15 07:07:06 +00004745 assert((CurContext->isDependentContext() || B.builtAll()) &&
4746 "omp for simd loop exprs were not built");
4747
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004748 if (!CurContext->isDependentContext()) {
4749 // Finalize the clauses that need pre-built expressions for CodeGen.
4750 for (auto C : Clauses) {
4751 if (auto LC = dyn_cast<OMPLinearClause>(C))
4752 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4753 B.NumIterations, *this, CurScope))
4754 return StmtError();
4755 }
4756 }
4757
Alexey Bataev66b15b52015-08-21 11:14:16 +00004758 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4759 // If both simdlen and safelen clauses are specified, the value of the simdlen
4760 // parameter must be less than or equal to the value of the safelen parameter.
4761 OMPSafelenClause *Safelen = nullptr;
4762 OMPSimdlenClause *Simdlen = nullptr;
4763 for (auto *Clause : Clauses) {
4764 if (Clause->getClauseKind() == OMPC_safelen)
4765 Safelen = cast<OMPSafelenClause>(Clause);
4766 else if (Clause->getClauseKind() == OMPC_simdlen)
4767 Simdlen = cast<OMPSimdlenClause>(Clause);
4768 if (Safelen && Simdlen)
4769 break;
4770 }
4771 if (Simdlen && Safelen &&
4772 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4773 Safelen->getSafelen()))
4774 return StmtError();
4775
Alexander Musmanf82886e2014-09-18 05:12:34 +00004776 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004777 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4778 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004779}
4780
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004781StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4782 Stmt *AStmt,
4783 SourceLocation StartLoc,
4784 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004785 if (!AStmt)
4786 return StmtError();
4787
4788 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004789 auto BaseStmt = AStmt;
4790 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4791 BaseStmt = CS->getCapturedStmt();
4792 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4793 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004794 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004795 return StmtError();
4796 // All associated statements must be '#pragma omp section' except for
4797 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004798 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004799 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4800 if (SectionStmt)
4801 Diag(SectionStmt->getLocStart(),
4802 diag::err_omp_sections_substmt_not_section);
4803 return StmtError();
4804 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004805 cast<OMPSectionDirective>(SectionStmt)
4806 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004807 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004808 } else {
4809 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4810 return StmtError();
4811 }
4812
4813 getCurFunction()->setHasBranchProtectedScope();
4814
Alexey Bataev25e5b442015-09-15 12:52:43 +00004815 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4816 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004817}
4818
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004819StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4820 SourceLocation StartLoc,
4821 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004822 if (!AStmt)
4823 return StmtError();
4824
4825 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004826
4827 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004828 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004829
Alexey Bataev25e5b442015-09-15 12:52:43 +00004830 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4831 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004832}
4833
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004834StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4835 Stmt *AStmt,
4836 SourceLocation StartLoc,
4837 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004838 if (!AStmt)
4839 return StmtError();
4840
4841 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004842
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004843 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004844
Alexey Bataev3255bf32015-01-19 05:20:46 +00004845 // OpenMP [2.7.3, single Construct, Restrictions]
4846 // The copyprivate clause must not be used with the nowait clause.
4847 OMPClause *Nowait = nullptr;
4848 OMPClause *Copyprivate = nullptr;
4849 for (auto *Clause : Clauses) {
4850 if (Clause->getClauseKind() == OMPC_nowait)
4851 Nowait = Clause;
4852 else if (Clause->getClauseKind() == OMPC_copyprivate)
4853 Copyprivate = Clause;
4854 if (Copyprivate && Nowait) {
4855 Diag(Copyprivate->getLocStart(),
4856 diag::err_omp_single_copyprivate_with_nowait);
4857 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4858 return StmtError();
4859 }
4860 }
4861
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004862 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4863}
4864
Alexander Musman80c22892014-07-17 08:54:58 +00004865StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4866 SourceLocation StartLoc,
4867 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004868 if (!AStmt)
4869 return StmtError();
4870
4871 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004872
4873 getCurFunction()->setHasBranchProtectedScope();
4874
4875 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4876}
4877
Alexey Bataev28c75412015-12-15 08:19:24 +00004878StmtResult Sema::ActOnOpenMPCriticalDirective(
4879 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4880 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004881 if (!AStmt)
4882 return StmtError();
4883
4884 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004885
Alexey Bataev28c75412015-12-15 08:19:24 +00004886 bool ErrorFound = false;
4887 llvm::APSInt Hint;
4888 SourceLocation HintLoc;
4889 bool DependentHint = false;
4890 for (auto *C : Clauses) {
4891 if (C->getClauseKind() == OMPC_hint) {
4892 if (!DirName.getName()) {
4893 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4894 ErrorFound = true;
4895 }
4896 Expr *E = cast<OMPHintClause>(C)->getHint();
4897 if (E->isTypeDependent() || E->isValueDependent() ||
4898 E->isInstantiationDependent())
4899 DependentHint = true;
4900 else {
4901 Hint = E->EvaluateKnownConstInt(Context);
4902 HintLoc = C->getLocStart();
4903 }
4904 }
4905 }
4906 if (ErrorFound)
4907 return StmtError();
4908 auto Pair = DSAStack->getCriticalWithHint(DirName);
4909 if (Pair.first && DirName.getName() && !DependentHint) {
4910 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4911 Diag(StartLoc, diag::err_omp_critical_with_hint);
4912 if (HintLoc.isValid()) {
4913 Diag(HintLoc, diag::note_omp_critical_hint_here)
4914 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4915 } else
4916 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4917 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4918 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4919 << 1
4920 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4921 /*Radix=*/10, /*Signed=*/false);
4922 } else
4923 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4924 }
4925 }
4926
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004927 getCurFunction()->setHasBranchProtectedScope();
4928
Alexey Bataev28c75412015-12-15 08:19:24 +00004929 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4930 Clauses, AStmt);
4931 if (!Pair.first && DirName.getName() && !DependentHint)
4932 DSAStack->addCriticalWithHint(Dir, Hint);
4933 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004934}
4935
Alexey Bataev4acb8592014-07-07 13:01:15 +00004936StmtResult Sema::ActOnOpenMPParallelForDirective(
4937 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4938 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004939 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004940 if (!AStmt)
4941 return StmtError();
4942
Alexey Bataev4acb8592014-07-07 13:01:15 +00004943 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4944 // 1.2.2 OpenMP Language Terminology
4945 // Structured block - An executable statement with a single entry at the
4946 // top and a single exit at the bottom.
4947 // The point of exit cannot be a branch out of the structured block.
4948 // longjmp() and throw() must not violate the entry/exit criteria.
4949 CS->getCapturedDecl()->setNothrow();
4950
Alexander Musmanc6388682014-12-15 07:07:06 +00004951 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004952 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4953 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004954 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004955 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4956 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4957 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004958 if (NestedLoopCount == 0)
4959 return StmtError();
4960
Alexander Musmana5f070a2014-10-01 06:03:56 +00004961 assert((CurContext->isDependentContext() || B.builtAll()) &&
4962 "omp parallel for loop exprs were not built");
4963
Alexey Bataev54acd402015-08-04 11:18:19 +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 Bataev4acb8592014-07-07 13:01:15 +00004974 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004975 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004976 NestedLoopCount, Clauses, AStmt, B,
4977 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004978}
4979
Alexander Musmane4e893b2014-09-23 09:33:00 +00004980StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4981 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4982 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004983 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004984 if (!AStmt)
4985 return StmtError();
4986
Alexander Musmane4e893b2014-09-23 09:33:00 +00004987 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4988 // 1.2.2 OpenMP Language Terminology
4989 // Structured block - An executable statement with a single entry at the
4990 // top and a single exit at the bottom.
4991 // The point of exit cannot be a branch out of the structured block.
4992 // longjmp() and throw() must not violate the entry/exit criteria.
4993 CS->getCapturedDecl()->setNothrow();
4994
Alexander Musmanc6388682014-12-15 07:07:06 +00004995 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004996 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4997 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004998 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004999 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5000 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5001 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005002 if (NestedLoopCount == 0)
5003 return StmtError();
5004
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005005 if (!CurContext->isDependentContext()) {
5006 // Finalize the clauses that need pre-built expressions for CodeGen.
5007 for (auto C : Clauses) {
5008 if (auto LC = dyn_cast<OMPLinearClause>(C))
5009 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5010 B.NumIterations, *this, CurScope))
5011 return StmtError();
5012 }
5013 }
5014
Alexey Bataev66b15b52015-08-21 11:14:16 +00005015 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5016 // If both simdlen and safelen clauses are specified, the value of the simdlen
5017 // parameter must be less than or equal to the value of the safelen parameter.
5018 OMPSafelenClause *Safelen = nullptr;
5019 OMPSimdlenClause *Simdlen = nullptr;
5020 for (auto *Clause : Clauses) {
5021 if (Clause->getClauseKind() == OMPC_safelen)
5022 Safelen = cast<OMPSafelenClause>(Clause);
5023 else if (Clause->getClauseKind() == OMPC_simdlen)
5024 Simdlen = cast<OMPSimdlenClause>(Clause);
5025 if (Safelen && Simdlen)
5026 break;
5027 }
5028 if (Simdlen && Safelen &&
5029 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5030 Safelen->getSafelen()))
5031 return StmtError();
5032
Alexander Musmane4e893b2014-09-23 09:33:00 +00005033 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005034 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005035 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005036}
5037
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005038StmtResult
5039Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5040 Stmt *AStmt, SourceLocation StartLoc,
5041 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005042 if (!AStmt)
5043 return StmtError();
5044
5045 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005046 auto BaseStmt = AStmt;
5047 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5048 BaseStmt = CS->getCapturedStmt();
5049 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5050 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005051 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005052 return StmtError();
5053 // All associated statements must be '#pragma omp section' except for
5054 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005055 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005056 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5057 if (SectionStmt)
5058 Diag(SectionStmt->getLocStart(),
5059 diag::err_omp_parallel_sections_substmt_not_section);
5060 return StmtError();
5061 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005062 cast<OMPSectionDirective>(SectionStmt)
5063 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005064 }
5065 } else {
5066 Diag(AStmt->getLocStart(),
5067 diag::err_omp_parallel_sections_not_compound_stmt);
5068 return StmtError();
5069 }
5070
5071 getCurFunction()->setHasBranchProtectedScope();
5072
Alexey Bataev25e5b442015-09-15 12:52:43 +00005073 return OMPParallelSectionsDirective::Create(
5074 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005075}
5076
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005077StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5078 Stmt *AStmt, SourceLocation StartLoc,
5079 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005080 if (!AStmt)
5081 return StmtError();
5082
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005083 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5084 // 1.2.2 OpenMP Language Terminology
5085 // Structured block - An executable statement with a single entry at the
5086 // top and a single exit at the bottom.
5087 // The point of exit cannot be a branch out of the structured block.
5088 // longjmp() and throw() must not violate the entry/exit criteria.
5089 CS->getCapturedDecl()->setNothrow();
5090
5091 getCurFunction()->setHasBranchProtectedScope();
5092
Alexey Bataev25e5b442015-09-15 12:52:43 +00005093 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5094 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005095}
5096
Alexey Bataev68446b72014-07-18 07:47:19 +00005097StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5098 SourceLocation EndLoc) {
5099 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5100}
5101
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005102StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5103 SourceLocation EndLoc) {
5104 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5105}
5106
Alexey Bataev2df347a2014-07-18 10:17:07 +00005107StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5108 SourceLocation EndLoc) {
5109 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5110}
5111
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005112StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5113 SourceLocation StartLoc,
5114 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005115 if (!AStmt)
5116 return StmtError();
5117
5118 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005119
5120 getCurFunction()->setHasBranchProtectedScope();
5121
5122 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5123}
5124
Alexey Bataev6125da92014-07-21 11:26:11 +00005125StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5126 SourceLocation StartLoc,
5127 SourceLocation EndLoc) {
5128 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5129 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5130}
5131
Alexey Bataev346265e2015-09-25 10:37:12 +00005132StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5133 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005134 SourceLocation StartLoc,
5135 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005136 OMPClause *DependFound = nullptr;
5137 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005138 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005139 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005140 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005141 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005142 for (auto *C : Clauses) {
5143 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5144 DependFound = C;
5145 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5146 if (DependSourceClause) {
5147 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5148 << getOpenMPDirectiveName(OMPD_ordered)
5149 << getOpenMPClauseName(OMPC_depend) << 2;
5150 ErrorFound = true;
5151 } else
5152 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005153 if (DependSinkClause) {
5154 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5155 << 0;
5156 ErrorFound = true;
5157 }
5158 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5159 if (DependSourceClause) {
5160 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5161 << 1;
5162 ErrorFound = true;
5163 }
5164 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005165 }
5166 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005167 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005168 else if (C->getClauseKind() == OMPC_simd)
5169 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005170 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005171 if (!ErrorFound && !SC &&
5172 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005173 // OpenMP [2.8.1,simd Construct, Restrictions]
5174 // An ordered construct with the simd clause is the only OpenMP construct
5175 // that can appear in the simd region.
5176 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005177 ErrorFound = true;
5178 } else if (DependFound && (TC || SC)) {
5179 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5180 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5181 ErrorFound = true;
5182 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5183 Diag(DependFound->getLocStart(),
5184 diag::err_omp_ordered_directive_without_param);
5185 ErrorFound = true;
5186 } else if (TC || Clauses.empty()) {
5187 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5188 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5189 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5190 << (TC != nullptr);
5191 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5192 ErrorFound = true;
5193 }
5194 }
5195 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005196 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005197
5198 if (AStmt) {
5199 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5200
5201 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005202 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005203
5204 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005205}
5206
Alexey Bataev1d160b12015-03-13 12:27:31 +00005207namespace {
5208/// \brief Helper class for checking expression in 'omp atomic [update]'
5209/// construct.
5210class OpenMPAtomicUpdateChecker {
5211 /// \brief Error results for atomic update expressions.
5212 enum ExprAnalysisErrorCode {
5213 /// \brief A statement is not an expression statement.
5214 NotAnExpression,
5215 /// \brief Expression is not builtin binary or unary operation.
5216 NotABinaryOrUnaryExpression,
5217 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5218 NotAnUnaryIncDecExpression,
5219 /// \brief An expression is not of scalar type.
5220 NotAScalarType,
5221 /// \brief A binary operation is not an assignment operation.
5222 NotAnAssignmentOp,
5223 /// \brief RHS part of the binary operation is not a binary expression.
5224 NotABinaryExpression,
5225 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5226 /// expression.
5227 NotABinaryOperator,
5228 /// \brief RHS binary operation does not have reference to the updated LHS
5229 /// part.
5230 NotAnUpdateExpression,
5231 /// \brief No errors is found.
5232 NoError
5233 };
5234 /// \brief Reference to Sema.
5235 Sema &SemaRef;
5236 /// \brief A location for note diagnostics (when error is found).
5237 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005238 /// \brief 'x' lvalue part of the source atomic expression.
5239 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005240 /// \brief 'expr' rvalue part of the source atomic expression.
5241 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005242 /// \brief Helper expression of the form
5243 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5244 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5245 Expr *UpdateExpr;
5246 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5247 /// important for non-associative operations.
5248 bool IsXLHSInRHSPart;
5249 BinaryOperatorKind Op;
5250 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005251 /// \brief true if the source expression is a postfix unary operation, false
5252 /// if it is a prefix unary operation.
5253 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005254
5255public:
5256 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005257 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005258 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005259 /// \brief Check specified statement that it is suitable for 'atomic update'
5260 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005261 /// expression. If DiagId and NoteId == 0, then only check is performed
5262 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005263 /// \param DiagId Diagnostic which should be emitted if error is found.
5264 /// \param NoteId Diagnostic note for the main error message.
5265 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005266 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005267 /// \brief Return the 'x' lvalue part of the source atomic expression.
5268 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005269 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5270 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005271 /// \brief Return the update expression used in calculation of the updated
5272 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5273 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5274 Expr *getUpdateExpr() const { return UpdateExpr; }
5275 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5276 /// false otherwise.
5277 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5278
Alexey Bataevb78ca832015-04-01 03:33:17 +00005279 /// \brief true if the source expression is a postfix unary operation, false
5280 /// if it is a prefix unary operation.
5281 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5282
Alexey Bataev1d160b12015-03-13 12:27:31 +00005283private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005284 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5285 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005286};
5287} // namespace
5288
5289bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5290 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5291 ExprAnalysisErrorCode ErrorFound = NoError;
5292 SourceLocation ErrorLoc, NoteLoc;
5293 SourceRange ErrorRange, NoteRange;
5294 // Allowed constructs are:
5295 // x = x binop expr;
5296 // x = expr binop x;
5297 if (AtomicBinOp->getOpcode() == BO_Assign) {
5298 X = AtomicBinOp->getLHS();
5299 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5300 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5301 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5302 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5303 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005304 Op = AtomicInnerBinOp->getOpcode();
5305 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005306 auto *LHS = AtomicInnerBinOp->getLHS();
5307 auto *RHS = AtomicInnerBinOp->getRHS();
5308 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5309 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5310 /*Canonical=*/true);
5311 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5312 /*Canonical=*/true);
5313 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5314 /*Canonical=*/true);
5315 if (XId == LHSId) {
5316 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005317 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005318 } else if (XId == RHSId) {
5319 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005320 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005321 } else {
5322 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5323 ErrorRange = AtomicInnerBinOp->getSourceRange();
5324 NoteLoc = X->getExprLoc();
5325 NoteRange = X->getSourceRange();
5326 ErrorFound = NotAnUpdateExpression;
5327 }
5328 } else {
5329 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5330 ErrorRange = AtomicInnerBinOp->getSourceRange();
5331 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5332 NoteRange = SourceRange(NoteLoc, NoteLoc);
5333 ErrorFound = NotABinaryOperator;
5334 }
5335 } else {
5336 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5337 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5338 ErrorFound = NotABinaryExpression;
5339 }
5340 } else {
5341 ErrorLoc = AtomicBinOp->getExprLoc();
5342 ErrorRange = AtomicBinOp->getSourceRange();
5343 NoteLoc = AtomicBinOp->getOperatorLoc();
5344 NoteRange = SourceRange(NoteLoc, NoteLoc);
5345 ErrorFound = NotAnAssignmentOp;
5346 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005347 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005348 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5349 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5350 return true;
5351 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005352 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005353 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005354}
5355
5356bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5357 unsigned NoteId) {
5358 ExprAnalysisErrorCode ErrorFound = NoError;
5359 SourceLocation ErrorLoc, NoteLoc;
5360 SourceRange ErrorRange, NoteRange;
5361 // Allowed constructs are:
5362 // x++;
5363 // x--;
5364 // ++x;
5365 // --x;
5366 // x binop= expr;
5367 // x = x binop expr;
5368 // x = expr binop x;
5369 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5370 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5371 if (AtomicBody->getType()->isScalarType() ||
5372 AtomicBody->isInstantiationDependent()) {
5373 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5374 AtomicBody->IgnoreParenImpCasts())) {
5375 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005376 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005377 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005378 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005379 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005380 X = AtomicCompAssignOp->getLHS();
5381 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005382 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5383 AtomicBody->IgnoreParenImpCasts())) {
5384 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005385 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5386 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005387 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005388 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5389 // Check for Unary Operation
5390 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005391 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005392 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5393 OpLoc = AtomicUnaryOp->getOperatorLoc();
5394 X = AtomicUnaryOp->getSubExpr();
5395 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5396 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005397 } else {
5398 ErrorFound = NotAnUnaryIncDecExpression;
5399 ErrorLoc = AtomicUnaryOp->getExprLoc();
5400 ErrorRange = AtomicUnaryOp->getSourceRange();
5401 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5402 NoteRange = SourceRange(NoteLoc, NoteLoc);
5403 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005404 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005405 ErrorFound = NotABinaryOrUnaryExpression;
5406 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5407 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5408 }
5409 } else {
5410 ErrorFound = NotAScalarType;
5411 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5412 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5413 }
5414 } else {
5415 ErrorFound = NotAnExpression;
5416 NoteLoc = ErrorLoc = S->getLocStart();
5417 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5418 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005419 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005420 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5421 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5422 return true;
5423 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005424 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005425 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005426 // Build an update expression of form 'OpaqueValueExpr(x) binop
5427 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5428 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5429 auto *OVEX = new (SemaRef.getASTContext())
5430 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5431 auto *OVEExpr = new (SemaRef.getASTContext())
5432 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5433 auto Update =
5434 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5435 IsXLHSInRHSPart ? OVEExpr : OVEX);
5436 if (Update.isInvalid())
5437 return true;
5438 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5439 Sema::AA_Casting);
5440 if (Update.isInvalid())
5441 return true;
5442 UpdateExpr = Update.get();
5443 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005444 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005445}
5446
Alexey Bataev0162e452014-07-22 10:10:35 +00005447StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5448 Stmt *AStmt,
5449 SourceLocation StartLoc,
5450 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005451 if (!AStmt)
5452 return StmtError();
5453
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005454 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005455 // 1.2.2 OpenMP Language Terminology
5456 // Structured block - An executable statement with a single entry at the
5457 // top and a single exit at the bottom.
5458 // The point of exit cannot be a branch out of the structured block.
5459 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005460 OpenMPClauseKind AtomicKind = OMPC_unknown;
5461 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005462 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005463 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005464 C->getClauseKind() == OMPC_update ||
5465 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005466 if (AtomicKind != OMPC_unknown) {
5467 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5468 << SourceRange(C->getLocStart(), C->getLocEnd());
5469 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5470 << getOpenMPClauseName(AtomicKind);
5471 } else {
5472 AtomicKind = C->getClauseKind();
5473 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005474 }
5475 }
5476 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005477
Alexey Bataev459dec02014-07-24 06:46:57 +00005478 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005479 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5480 Body = EWC->getSubExpr();
5481
Alexey Bataev62cec442014-11-18 10:14:22 +00005482 Expr *X = nullptr;
5483 Expr *V = nullptr;
5484 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005485 Expr *UE = nullptr;
5486 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005487 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005488 // OpenMP [2.12.6, atomic Construct]
5489 // In the next expressions:
5490 // * x and v (as applicable) are both l-value expressions with scalar type.
5491 // * During the execution of an atomic region, multiple syntactic
5492 // occurrences of x must designate the same storage location.
5493 // * Neither of v and expr (as applicable) may access the storage location
5494 // designated by x.
5495 // * Neither of x and expr (as applicable) may access the storage location
5496 // designated by v.
5497 // * expr is an expression with scalar type.
5498 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5499 // * binop, binop=, ++, and -- are not overloaded operators.
5500 // * The expression x binop expr must be numerically equivalent to x binop
5501 // (expr). This requirement is satisfied if the operators in expr have
5502 // precedence greater than binop, or by using parentheses around expr or
5503 // subexpressions of expr.
5504 // * The expression expr binop x must be numerically equivalent to (expr)
5505 // binop x. This requirement is satisfied if the operators in expr have
5506 // precedence equal to or greater than binop, or by using parentheses around
5507 // expr or subexpressions of expr.
5508 // * For forms that allow multiple occurrences of x, the number of times
5509 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005510 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005511 enum {
5512 NotAnExpression,
5513 NotAnAssignmentOp,
5514 NotAScalarType,
5515 NotAnLValue,
5516 NoError
5517 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005518 SourceLocation ErrorLoc, NoteLoc;
5519 SourceRange ErrorRange, NoteRange;
5520 // If clause is read:
5521 // v = x;
5522 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5523 auto AtomicBinOp =
5524 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5525 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5526 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5527 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5528 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5529 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5530 if (!X->isLValue() || !V->isLValue()) {
5531 auto NotLValueExpr = X->isLValue() ? V : X;
5532 ErrorFound = NotAnLValue;
5533 ErrorLoc = AtomicBinOp->getExprLoc();
5534 ErrorRange = AtomicBinOp->getSourceRange();
5535 NoteLoc = NotLValueExpr->getExprLoc();
5536 NoteRange = NotLValueExpr->getSourceRange();
5537 }
5538 } else if (!X->isInstantiationDependent() ||
5539 !V->isInstantiationDependent()) {
5540 auto NotScalarExpr =
5541 (X->isInstantiationDependent() || X->getType()->isScalarType())
5542 ? V
5543 : X;
5544 ErrorFound = NotAScalarType;
5545 ErrorLoc = AtomicBinOp->getExprLoc();
5546 ErrorRange = AtomicBinOp->getSourceRange();
5547 NoteLoc = NotScalarExpr->getExprLoc();
5548 NoteRange = NotScalarExpr->getSourceRange();
5549 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005550 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005551 ErrorFound = NotAnAssignmentOp;
5552 ErrorLoc = AtomicBody->getExprLoc();
5553 ErrorRange = AtomicBody->getSourceRange();
5554 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5555 : AtomicBody->getExprLoc();
5556 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5557 : AtomicBody->getSourceRange();
5558 }
5559 } else {
5560 ErrorFound = NotAnExpression;
5561 NoteLoc = ErrorLoc = Body->getLocStart();
5562 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005563 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005564 if (ErrorFound != NoError) {
5565 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5566 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005567 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5568 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005569 return StmtError();
5570 } else if (CurContext->isDependentContext())
5571 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005572 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005573 enum {
5574 NotAnExpression,
5575 NotAnAssignmentOp,
5576 NotAScalarType,
5577 NotAnLValue,
5578 NoError
5579 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005580 SourceLocation ErrorLoc, NoteLoc;
5581 SourceRange ErrorRange, NoteRange;
5582 // If clause is write:
5583 // x = expr;
5584 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5585 auto AtomicBinOp =
5586 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5587 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005588 X = AtomicBinOp->getLHS();
5589 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005590 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5591 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5592 if (!X->isLValue()) {
5593 ErrorFound = NotAnLValue;
5594 ErrorLoc = AtomicBinOp->getExprLoc();
5595 ErrorRange = AtomicBinOp->getSourceRange();
5596 NoteLoc = X->getExprLoc();
5597 NoteRange = X->getSourceRange();
5598 }
5599 } else if (!X->isInstantiationDependent() ||
5600 !E->isInstantiationDependent()) {
5601 auto NotScalarExpr =
5602 (X->isInstantiationDependent() || X->getType()->isScalarType())
5603 ? E
5604 : X;
5605 ErrorFound = NotAScalarType;
5606 ErrorLoc = AtomicBinOp->getExprLoc();
5607 ErrorRange = AtomicBinOp->getSourceRange();
5608 NoteLoc = NotScalarExpr->getExprLoc();
5609 NoteRange = NotScalarExpr->getSourceRange();
5610 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005611 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005612 ErrorFound = NotAnAssignmentOp;
5613 ErrorLoc = AtomicBody->getExprLoc();
5614 ErrorRange = AtomicBody->getSourceRange();
5615 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5616 : AtomicBody->getExprLoc();
5617 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5618 : AtomicBody->getSourceRange();
5619 }
5620 } else {
5621 ErrorFound = NotAnExpression;
5622 NoteLoc = ErrorLoc = Body->getLocStart();
5623 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005624 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005625 if (ErrorFound != NoError) {
5626 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5627 << ErrorRange;
5628 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5629 << NoteRange;
5630 return StmtError();
5631 } else if (CurContext->isDependentContext())
5632 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005633 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005634 // If clause is update:
5635 // x++;
5636 // x--;
5637 // ++x;
5638 // --x;
5639 // x binop= expr;
5640 // x = x binop expr;
5641 // x = expr binop x;
5642 OpenMPAtomicUpdateChecker Checker(*this);
5643 if (Checker.checkStatement(
5644 Body, (AtomicKind == OMPC_update)
5645 ? diag::err_omp_atomic_update_not_expression_statement
5646 : diag::err_omp_atomic_not_expression_statement,
5647 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005648 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005649 if (!CurContext->isDependentContext()) {
5650 E = Checker.getExpr();
5651 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005652 UE = Checker.getUpdateExpr();
5653 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005654 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005655 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005656 enum {
5657 NotAnAssignmentOp,
5658 NotACompoundStatement,
5659 NotTwoSubstatements,
5660 NotASpecificExpression,
5661 NoError
5662 } ErrorFound = NoError;
5663 SourceLocation ErrorLoc, NoteLoc;
5664 SourceRange ErrorRange, NoteRange;
5665 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5666 // If clause is a capture:
5667 // v = x++;
5668 // v = x--;
5669 // v = ++x;
5670 // v = --x;
5671 // v = x binop= expr;
5672 // v = x = x binop expr;
5673 // v = x = expr binop x;
5674 auto *AtomicBinOp =
5675 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5676 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5677 V = AtomicBinOp->getLHS();
5678 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5679 OpenMPAtomicUpdateChecker Checker(*this);
5680 if (Checker.checkStatement(
5681 Body, diag::err_omp_atomic_capture_not_expression_statement,
5682 diag::note_omp_atomic_update))
5683 return StmtError();
5684 E = Checker.getExpr();
5685 X = Checker.getX();
5686 UE = Checker.getUpdateExpr();
5687 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5688 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005689 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005690 ErrorLoc = AtomicBody->getExprLoc();
5691 ErrorRange = AtomicBody->getSourceRange();
5692 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5693 : AtomicBody->getExprLoc();
5694 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5695 : AtomicBody->getSourceRange();
5696 ErrorFound = NotAnAssignmentOp;
5697 }
5698 if (ErrorFound != NoError) {
5699 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5700 << ErrorRange;
5701 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5702 return StmtError();
5703 } else if (CurContext->isDependentContext()) {
5704 UE = V = E = X = nullptr;
5705 }
5706 } else {
5707 // If clause is a capture:
5708 // { v = x; x = expr; }
5709 // { v = x; x++; }
5710 // { v = x; x--; }
5711 // { v = x; ++x; }
5712 // { v = x; --x; }
5713 // { v = x; x binop= expr; }
5714 // { v = x; x = x binop expr; }
5715 // { v = x; x = expr binop x; }
5716 // { x++; v = x; }
5717 // { x--; v = x; }
5718 // { ++x; v = x; }
5719 // { --x; v = x; }
5720 // { x binop= expr; v = x; }
5721 // { x = x binop expr; v = x; }
5722 // { x = expr binop x; v = x; }
5723 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5724 // Check that this is { expr1; expr2; }
5725 if (CS->size() == 2) {
5726 auto *First = CS->body_front();
5727 auto *Second = CS->body_back();
5728 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5729 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5730 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5731 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5732 // Need to find what subexpression is 'v' and what is 'x'.
5733 OpenMPAtomicUpdateChecker Checker(*this);
5734 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5735 BinaryOperator *BinOp = nullptr;
5736 if (IsUpdateExprFound) {
5737 BinOp = dyn_cast<BinaryOperator>(First);
5738 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5739 }
5740 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5741 // { v = x; x++; }
5742 // { v = x; x--; }
5743 // { v = x; ++x; }
5744 // { v = x; --x; }
5745 // { v = x; x binop= expr; }
5746 // { v = x; x = x binop expr; }
5747 // { v = x; x = expr binop x; }
5748 // Check that the first expression has form v = x.
5749 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5750 llvm::FoldingSetNodeID XId, PossibleXId;
5751 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5752 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5753 IsUpdateExprFound = XId == PossibleXId;
5754 if (IsUpdateExprFound) {
5755 V = BinOp->getLHS();
5756 X = Checker.getX();
5757 E = Checker.getExpr();
5758 UE = Checker.getUpdateExpr();
5759 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005760 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005761 }
5762 }
5763 if (!IsUpdateExprFound) {
5764 IsUpdateExprFound = !Checker.checkStatement(First);
5765 BinOp = nullptr;
5766 if (IsUpdateExprFound) {
5767 BinOp = dyn_cast<BinaryOperator>(Second);
5768 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5769 }
5770 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5771 // { x++; v = x; }
5772 // { x--; v = x; }
5773 // { ++x; v = x; }
5774 // { --x; v = x; }
5775 // { x binop= expr; v = x; }
5776 // { x = x binop expr; v = x; }
5777 // { x = expr binop x; v = x; }
5778 // Check that the second expression has form v = x.
5779 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5780 llvm::FoldingSetNodeID XId, PossibleXId;
5781 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5782 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5783 IsUpdateExprFound = XId == PossibleXId;
5784 if (IsUpdateExprFound) {
5785 V = BinOp->getLHS();
5786 X = Checker.getX();
5787 E = Checker.getExpr();
5788 UE = Checker.getUpdateExpr();
5789 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005790 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005791 }
5792 }
5793 }
5794 if (!IsUpdateExprFound) {
5795 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005796 auto *FirstExpr = dyn_cast<Expr>(First);
5797 auto *SecondExpr = dyn_cast<Expr>(Second);
5798 if (!FirstExpr || !SecondExpr ||
5799 !(FirstExpr->isInstantiationDependent() ||
5800 SecondExpr->isInstantiationDependent())) {
5801 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5802 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005803 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005804 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5805 : First->getLocStart();
5806 NoteRange = ErrorRange = FirstBinOp
5807 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005808 : SourceRange(ErrorLoc, ErrorLoc);
5809 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005810 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5811 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5812 ErrorFound = NotAnAssignmentOp;
5813 NoteLoc = ErrorLoc = SecondBinOp
5814 ? SecondBinOp->getOperatorLoc()
5815 : Second->getLocStart();
5816 NoteRange = ErrorRange =
5817 SecondBinOp ? SecondBinOp->getSourceRange()
5818 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005819 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005820 auto *PossibleXRHSInFirst =
5821 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5822 auto *PossibleXLHSInSecond =
5823 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5824 llvm::FoldingSetNodeID X1Id, X2Id;
5825 PossibleXRHSInFirst->Profile(X1Id, Context,
5826 /*Canonical=*/true);
5827 PossibleXLHSInSecond->Profile(X2Id, Context,
5828 /*Canonical=*/true);
5829 IsUpdateExprFound = X1Id == X2Id;
5830 if (IsUpdateExprFound) {
5831 V = FirstBinOp->getLHS();
5832 X = SecondBinOp->getLHS();
5833 E = SecondBinOp->getRHS();
5834 UE = nullptr;
5835 IsXLHSInRHSPart = false;
5836 IsPostfixUpdate = true;
5837 } else {
5838 ErrorFound = NotASpecificExpression;
5839 ErrorLoc = FirstBinOp->getExprLoc();
5840 ErrorRange = FirstBinOp->getSourceRange();
5841 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5842 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5843 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005844 }
5845 }
5846 }
5847 }
5848 } else {
5849 NoteLoc = ErrorLoc = Body->getLocStart();
5850 NoteRange = ErrorRange =
5851 SourceRange(Body->getLocStart(), Body->getLocStart());
5852 ErrorFound = NotTwoSubstatements;
5853 }
5854 } else {
5855 NoteLoc = ErrorLoc = Body->getLocStart();
5856 NoteRange = ErrorRange =
5857 SourceRange(Body->getLocStart(), Body->getLocStart());
5858 ErrorFound = NotACompoundStatement;
5859 }
5860 if (ErrorFound != NoError) {
5861 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5862 << ErrorRange;
5863 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5864 return StmtError();
5865 } else if (CurContext->isDependentContext()) {
5866 UE = V = E = X = nullptr;
5867 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005868 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005869 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005870
5871 getCurFunction()->setHasBranchProtectedScope();
5872
Alexey Bataev62cec442014-11-18 10:14:22 +00005873 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005874 X, V, E, UE, IsXLHSInRHSPart,
5875 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005876}
5877
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005878StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5879 Stmt *AStmt,
5880 SourceLocation StartLoc,
5881 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005882 if (!AStmt)
5883 return StmtError();
5884
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005885 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5886 // 1.2.2 OpenMP Language Terminology
5887 // Structured block - An executable statement with a single entry at the
5888 // top and a single exit at the bottom.
5889 // The point of exit cannot be a branch out of the structured block.
5890 // longjmp() and throw() must not violate the entry/exit criteria.
5891 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005892
Alexey Bataev13314bf2014-10-09 04:18:56 +00005893 // OpenMP [2.16, Nesting of Regions]
5894 // If specified, a teams construct must be contained within a target
5895 // construct. That target construct must contain no statements or directives
5896 // outside of the teams construct.
5897 if (DSAStack->hasInnerTeamsRegion()) {
5898 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5899 bool OMPTeamsFound = true;
5900 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5901 auto I = CS->body_begin();
5902 while (I != CS->body_end()) {
5903 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5904 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5905 OMPTeamsFound = false;
5906 break;
5907 }
5908 ++I;
5909 }
5910 assert(I != CS->body_end() && "Not found statement");
5911 S = *I;
5912 }
5913 if (!OMPTeamsFound) {
5914 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5915 Diag(DSAStack->getInnerTeamsRegionLoc(),
5916 diag::note_omp_nested_teams_construct_here);
5917 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5918 << isa<OMPExecutableDirective>(S);
5919 return StmtError();
5920 }
5921 }
5922
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005923 getCurFunction()->setHasBranchProtectedScope();
5924
5925 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5926}
5927
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005928StmtResult
5929Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5930 Stmt *AStmt, SourceLocation StartLoc,
5931 SourceLocation EndLoc) {
5932 if (!AStmt)
5933 return StmtError();
5934
5935 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5936 // 1.2.2 OpenMP Language Terminology
5937 // Structured block - An executable statement with a single entry at the
5938 // top and a single exit at the bottom.
5939 // The point of exit cannot be a branch out of the structured block.
5940 // longjmp() and throw() must not violate the entry/exit criteria.
5941 CS->getCapturedDecl()->setNothrow();
5942
5943 getCurFunction()->setHasBranchProtectedScope();
5944
5945 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5946 AStmt);
5947}
5948
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005949StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5950 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5951 SourceLocation EndLoc,
5952 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5953 if (!AStmt)
5954 return StmtError();
5955
5956 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5957 // 1.2.2 OpenMP Language Terminology
5958 // Structured block - An executable statement with a single entry at the
5959 // top and a single exit at the bottom.
5960 // The point of exit cannot be a branch out of the structured block.
5961 // longjmp() and throw() must not violate the entry/exit criteria.
5962 CS->getCapturedDecl()->setNothrow();
5963
5964 OMPLoopDirective::HelperExprs B;
5965 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5966 // define the nested loops number.
5967 unsigned NestedLoopCount =
5968 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5969 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5970 VarsWithImplicitDSA, B);
5971 if (NestedLoopCount == 0)
5972 return StmtError();
5973
5974 assert((CurContext->isDependentContext() || B.builtAll()) &&
5975 "omp target parallel for loop exprs were not built");
5976
5977 if (!CurContext->isDependentContext()) {
5978 // Finalize the clauses that need pre-built expressions for CodeGen.
5979 for (auto C : Clauses) {
5980 if (auto LC = dyn_cast<OMPLinearClause>(C))
5981 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5982 B.NumIterations, *this, CurScope))
5983 return StmtError();
5984 }
5985 }
5986
5987 getCurFunction()->setHasBranchProtectedScope();
5988 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5989 NestedLoopCount, Clauses, AStmt,
5990 B, DSAStack->isCancelRegion());
5991}
5992
Samuel Antaodf67fc42016-01-19 19:15:56 +00005993/// \brief Check for existence of a map clause in the list of clauses.
5994static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5995 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5996 I != E; ++I) {
5997 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5998 return true;
5999 }
6000 }
6001
6002 return false;
6003}
6004
Michael Wong65f367f2015-07-21 13:44:28 +00006005StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6006 Stmt *AStmt,
6007 SourceLocation StartLoc,
6008 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006009 if (!AStmt)
6010 return StmtError();
6011
6012 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6013
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006014 // OpenMP [2.10.1, Restrictions, p. 97]
6015 // At least one map clause must appear on the directive.
6016 if (!HasMapClause(Clauses)) {
6017 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6018 getOpenMPDirectiveName(OMPD_target_data);
6019 return StmtError();
6020 }
6021
Michael Wong65f367f2015-07-21 13:44:28 +00006022 getCurFunction()->setHasBranchProtectedScope();
6023
6024 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6025 AStmt);
6026}
6027
Samuel Antaodf67fc42016-01-19 19:15:56 +00006028StmtResult
6029Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6030 SourceLocation StartLoc,
6031 SourceLocation EndLoc) {
6032 // OpenMP [2.10.2, Restrictions, p. 99]
6033 // At least one map clause must appear on the directive.
6034 if (!HasMapClause(Clauses)) {
6035 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6036 << getOpenMPDirectiveName(OMPD_target_enter_data);
6037 return StmtError();
6038 }
6039
6040 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6041 Clauses);
6042}
6043
Samuel Antao72590762016-01-19 20:04:50 +00006044StmtResult
6045Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6046 SourceLocation StartLoc,
6047 SourceLocation EndLoc) {
6048 // OpenMP [2.10.3, Restrictions, p. 102]
6049 // At least one map clause must appear on the directive.
6050 if (!HasMapClause(Clauses)) {
6051 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6052 << getOpenMPDirectiveName(OMPD_target_exit_data);
6053 return StmtError();
6054 }
6055
6056 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6057}
6058
Alexey Bataev13314bf2014-10-09 04:18:56 +00006059StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6060 Stmt *AStmt, SourceLocation StartLoc,
6061 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006062 if (!AStmt)
6063 return StmtError();
6064
Alexey Bataev13314bf2014-10-09 04:18:56 +00006065 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6066 // 1.2.2 OpenMP Language Terminology
6067 // Structured block - An executable statement with a single entry at the
6068 // top and a single exit at the bottom.
6069 // The point of exit cannot be a branch out of the structured block.
6070 // longjmp() and throw() must not violate the entry/exit criteria.
6071 CS->getCapturedDecl()->setNothrow();
6072
6073 getCurFunction()->setHasBranchProtectedScope();
6074
6075 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6076}
6077
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006078StmtResult
6079Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6080 SourceLocation EndLoc,
6081 OpenMPDirectiveKind CancelRegion) {
6082 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6083 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6084 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6085 << getOpenMPDirectiveName(CancelRegion);
6086 return StmtError();
6087 }
6088 if (DSAStack->isParentNowaitRegion()) {
6089 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6090 return StmtError();
6091 }
6092 if (DSAStack->isParentOrderedRegion()) {
6093 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6094 return StmtError();
6095 }
6096 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6097 CancelRegion);
6098}
6099
Alexey Bataev87933c72015-09-18 08:07:34 +00006100StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6101 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006102 SourceLocation EndLoc,
6103 OpenMPDirectiveKind CancelRegion) {
6104 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6105 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6106 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6107 << getOpenMPDirectiveName(CancelRegion);
6108 return StmtError();
6109 }
6110 if (DSAStack->isParentNowaitRegion()) {
6111 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6112 return StmtError();
6113 }
6114 if (DSAStack->isParentOrderedRegion()) {
6115 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6116 return StmtError();
6117 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006118 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006119 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6120 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006121}
6122
Alexey Bataev382967a2015-12-08 12:06:20 +00006123static bool checkGrainsizeNumTasksClauses(Sema &S,
6124 ArrayRef<OMPClause *> Clauses) {
6125 OMPClause *PrevClause = nullptr;
6126 bool ErrorFound = false;
6127 for (auto *C : Clauses) {
6128 if (C->getClauseKind() == OMPC_grainsize ||
6129 C->getClauseKind() == OMPC_num_tasks) {
6130 if (!PrevClause)
6131 PrevClause = C;
6132 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6133 S.Diag(C->getLocStart(),
6134 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6135 << getOpenMPClauseName(C->getClauseKind())
6136 << getOpenMPClauseName(PrevClause->getClauseKind());
6137 S.Diag(PrevClause->getLocStart(),
6138 diag::note_omp_previous_grainsize_num_tasks)
6139 << getOpenMPClauseName(PrevClause->getClauseKind());
6140 ErrorFound = true;
6141 }
6142 }
6143 }
6144 return ErrorFound;
6145}
6146
Alexey Bataev49f6e782015-12-01 04:18:41 +00006147StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6148 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6149 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006150 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006151 if (!AStmt)
6152 return StmtError();
6153
6154 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6155 OMPLoopDirective::HelperExprs B;
6156 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6157 // define the nested loops number.
6158 unsigned NestedLoopCount =
6159 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006160 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006161 VarsWithImplicitDSA, B);
6162 if (NestedLoopCount == 0)
6163 return StmtError();
6164
6165 assert((CurContext->isDependentContext() || B.builtAll()) &&
6166 "omp for loop exprs were not built");
6167
Alexey Bataev382967a2015-12-08 12:06:20 +00006168 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6169 // The grainsize clause and num_tasks clause are mutually exclusive and may
6170 // not appear on the same taskloop directive.
6171 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6172 return StmtError();
6173
Alexey Bataev49f6e782015-12-01 04:18:41 +00006174 getCurFunction()->setHasBranchProtectedScope();
6175 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6176 NestedLoopCount, Clauses, AStmt, B);
6177}
6178
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006179StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6180 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6181 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006182 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006183 if (!AStmt)
6184 return StmtError();
6185
6186 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6187 OMPLoopDirective::HelperExprs B;
6188 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6189 // define the nested loops number.
6190 unsigned NestedLoopCount =
6191 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6192 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6193 VarsWithImplicitDSA, B);
6194 if (NestedLoopCount == 0)
6195 return StmtError();
6196
6197 assert((CurContext->isDependentContext() || B.builtAll()) &&
6198 "omp for loop exprs were not built");
6199
Alexey Bataev382967a2015-12-08 12:06:20 +00006200 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6201 // The grainsize clause and num_tasks clause are mutually exclusive and may
6202 // not appear on the same taskloop directive.
6203 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6204 return StmtError();
6205
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006206 getCurFunction()->setHasBranchProtectedScope();
6207 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6208 NestedLoopCount, Clauses, AStmt, B);
6209}
6210
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006211StmtResult Sema::ActOnOpenMPDistributeDirective(
6212 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6213 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006214 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006215 if (!AStmt)
6216 return StmtError();
6217
6218 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6219 OMPLoopDirective::HelperExprs B;
6220 // In presence of clause 'collapse' with number of loops, it will
6221 // define the nested loops number.
6222 unsigned NestedLoopCount =
6223 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6224 nullptr /*ordered not a clause on distribute*/, AStmt,
6225 *this, *DSAStack, VarsWithImplicitDSA, B);
6226 if (NestedLoopCount == 0)
6227 return StmtError();
6228
6229 assert((CurContext->isDependentContext() || B.builtAll()) &&
6230 "omp for loop exprs were not built");
6231
6232 getCurFunction()->setHasBranchProtectedScope();
6233 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6234 NestedLoopCount, Clauses, AStmt, B);
6235}
6236
Alexey Bataeved09d242014-05-28 05:53:51 +00006237OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006238 SourceLocation StartLoc,
6239 SourceLocation LParenLoc,
6240 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006241 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006242 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006243 case OMPC_final:
6244 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6245 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006246 case OMPC_num_threads:
6247 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6248 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006249 case OMPC_safelen:
6250 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6251 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006252 case OMPC_simdlen:
6253 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6254 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006255 case OMPC_collapse:
6256 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6257 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006258 case OMPC_ordered:
6259 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6260 break;
Michael Wonge710d542015-08-07 16:16:36 +00006261 case OMPC_device:
6262 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6263 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006264 case OMPC_num_teams:
6265 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6266 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006267 case OMPC_thread_limit:
6268 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6269 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006270 case OMPC_priority:
6271 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6272 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006273 case OMPC_grainsize:
6274 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6275 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006276 case OMPC_num_tasks:
6277 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6278 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006279 case OMPC_hint:
6280 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6281 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006282 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006283 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006284 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006285 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006286 case OMPC_private:
6287 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006288 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006289 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006290 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006291 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006292 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006293 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006294 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006295 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006296 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006297 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006298 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006299 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006300 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006301 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006302 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006303 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006304 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006305 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006306 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006307 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006308 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006309 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006310 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006311 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006312 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006313 llvm_unreachable("Clause is not allowed.");
6314 }
6315 return Res;
6316}
6317
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006318OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6319 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006320 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006321 SourceLocation NameModifierLoc,
6322 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006323 SourceLocation EndLoc) {
6324 Expr *ValExpr = Condition;
6325 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6326 !Condition->isInstantiationDependent() &&
6327 !Condition->containsUnexpandedParameterPack()) {
6328 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006329 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006330 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006331 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006332
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006333 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006334 }
6335
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006336 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6337 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006338}
6339
Alexey Bataev3778b602014-07-17 07:32:53 +00006340OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6341 SourceLocation StartLoc,
6342 SourceLocation LParenLoc,
6343 SourceLocation EndLoc) {
6344 Expr *ValExpr = Condition;
6345 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6346 !Condition->isInstantiationDependent() &&
6347 !Condition->containsUnexpandedParameterPack()) {
6348 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6349 Condition->getExprLoc(), Condition);
6350 if (Val.isInvalid())
6351 return nullptr;
6352
6353 ValExpr = Val.get();
6354 }
6355
6356 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6357}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006358ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6359 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006360 if (!Op)
6361 return ExprError();
6362
6363 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6364 public:
6365 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006366 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006367 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6368 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006369 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6370 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006371 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6372 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006373 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6374 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006375 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6376 QualType T,
6377 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006378 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6379 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006380 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6381 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006382 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006383 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006384 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006385 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6386 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006387 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6388 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006389 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6390 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006391 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006392 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006393 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006394 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6395 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006396 llvm_unreachable("conversion functions are permitted");
6397 }
6398 } ConvertDiagnoser;
6399 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6400}
6401
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006402static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006403 OpenMPClauseKind CKind,
6404 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006405 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6406 !ValExpr->isInstantiationDependent()) {
6407 SourceLocation Loc = ValExpr->getExprLoc();
6408 ExprResult Value =
6409 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6410 if (Value.isInvalid())
6411 return false;
6412
6413 ValExpr = Value.get();
6414 // The expression must evaluate to a non-negative integer value.
6415 llvm::APSInt Result;
6416 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006417 Result.isSigned() &&
6418 !((!StrictlyPositive && Result.isNonNegative()) ||
6419 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006420 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006421 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6422 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006423 return false;
6424 }
6425 }
6426 return true;
6427}
6428
Alexey Bataev568a8332014-03-06 06:15:19 +00006429OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6430 SourceLocation StartLoc,
6431 SourceLocation LParenLoc,
6432 SourceLocation EndLoc) {
6433 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006434
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006435 // OpenMP [2.5, Restrictions]
6436 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006437 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6438 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006439 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006440
Alexey Bataeved09d242014-05-28 05:53:51 +00006441 return new (Context)
6442 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006443}
6444
Alexey Bataev62c87d22014-03-21 04:51:18 +00006445ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006446 OpenMPClauseKind CKind,
6447 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006448 if (!E)
6449 return ExprError();
6450 if (E->isValueDependent() || E->isTypeDependent() ||
6451 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006452 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006453 llvm::APSInt Result;
6454 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6455 if (ICE.isInvalid())
6456 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006457 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6458 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006459 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006460 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6461 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006462 return ExprError();
6463 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006464 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6465 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6466 << E->getSourceRange();
6467 return ExprError();
6468 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006469 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6470 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006471 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006472 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006473 return ICE;
6474}
6475
6476OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6477 SourceLocation LParenLoc,
6478 SourceLocation EndLoc) {
6479 // OpenMP [2.8.1, simd construct, Description]
6480 // The parameter of the safelen clause must be a constant
6481 // positive integer expression.
6482 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6483 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006484 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006485 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006486 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006487}
6488
Alexey Bataev66b15b52015-08-21 11:14:16 +00006489OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6490 SourceLocation LParenLoc,
6491 SourceLocation EndLoc) {
6492 // OpenMP [2.8.1, simd construct, Description]
6493 // The parameter of the simdlen clause must be a constant
6494 // positive integer expression.
6495 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6496 if (Simdlen.isInvalid())
6497 return nullptr;
6498 return new (Context)
6499 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6500}
6501
Alexander Musman64d33f12014-06-04 07:53:32 +00006502OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6503 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006504 SourceLocation LParenLoc,
6505 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006506 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006507 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006508 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006509 // The parameter of the collapse clause must be a constant
6510 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006511 ExprResult NumForLoopsResult =
6512 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6513 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006514 return nullptr;
6515 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006516 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006517}
6518
Alexey Bataev10e775f2015-07-30 11:36:16 +00006519OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6520 SourceLocation EndLoc,
6521 SourceLocation LParenLoc,
6522 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006523 // OpenMP [2.7.1, loop construct, Description]
6524 // OpenMP [2.8.1, simd construct, Description]
6525 // OpenMP [2.9.6, distribute construct, Description]
6526 // The parameter of the ordered clause must be a constant
6527 // positive integer expression if any.
6528 if (NumForLoops && LParenLoc.isValid()) {
6529 ExprResult NumForLoopsResult =
6530 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6531 if (NumForLoopsResult.isInvalid())
6532 return nullptr;
6533 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006534 } else
6535 NumForLoops = nullptr;
6536 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006537 return new (Context)
6538 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6539}
6540
Alexey Bataeved09d242014-05-28 05:53:51 +00006541OMPClause *Sema::ActOnOpenMPSimpleClause(
6542 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6543 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006544 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006545 switch (Kind) {
6546 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006547 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006548 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6549 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006550 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006551 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006552 Res = ActOnOpenMPProcBindClause(
6553 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6554 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006555 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006556 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006557 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006558 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006559 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006560 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006561 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006562 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006563 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006564 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006565 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006566 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006567 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006568 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006569 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006570 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006571 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006572 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006573 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006574 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006575 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006576 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006577 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006578 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006579 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006580 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006581 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006582 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006583 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006584 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006585 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006586 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006587 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006588 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006589 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006590 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006591 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006592 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006593 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006594 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006595 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006596 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006597 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006598 llvm_unreachable("Clause is not allowed.");
6599 }
6600 return Res;
6601}
6602
Alexey Bataev6402bca2015-12-28 07:25:51 +00006603static std::string
6604getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6605 ArrayRef<unsigned> Exclude = llvm::None) {
6606 std::string Values;
6607 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6608 unsigned Skipped = Exclude.size();
6609 auto S = Exclude.begin(), E = Exclude.end();
6610 for (unsigned i = First; i < Last; ++i) {
6611 if (std::find(S, E, i) != E) {
6612 --Skipped;
6613 continue;
6614 }
6615 Values += "'";
6616 Values += getOpenMPSimpleClauseTypeName(K, i);
6617 Values += "'";
6618 if (i == Bound - Skipped)
6619 Values += " or ";
6620 else if (i != Bound + 1 - Skipped)
6621 Values += ", ";
6622 }
6623 return Values;
6624}
6625
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006626OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6627 SourceLocation KindKwLoc,
6628 SourceLocation StartLoc,
6629 SourceLocation LParenLoc,
6630 SourceLocation EndLoc) {
6631 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006632 static_assert(OMPC_DEFAULT_unknown > 0,
6633 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006634 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006635 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6636 /*Last=*/OMPC_DEFAULT_unknown)
6637 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006638 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006639 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006640 switch (Kind) {
6641 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006642 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006643 break;
6644 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006645 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006646 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006647 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006648 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006649 break;
6650 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006651 return new (Context)
6652 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006653}
6654
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006655OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6656 SourceLocation KindKwLoc,
6657 SourceLocation StartLoc,
6658 SourceLocation LParenLoc,
6659 SourceLocation EndLoc) {
6660 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006661 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006662 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6663 /*Last=*/OMPC_PROC_BIND_unknown)
6664 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006665 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006666 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006667 return new (Context)
6668 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006669}
6670
Alexey Bataev56dafe82014-06-20 07:16:17 +00006671OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006672 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006673 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006674 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006675 SourceLocation EndLoc) {
6676 OMPClause *Res = nullptr;
6677 switch (Kind) {
6678 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006679 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6680 assert(Argument.size() == NumberOfElements &&
6681 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006682 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006683 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6684 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6685 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6686 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6687 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006688 break;
6689 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006690 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6691 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6692 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6693 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006694 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006695 case OMPC_dist_schedule:
6696 Res = ActOnOpenMPDistScheduleClause(
6697 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6698 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6699 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006700 case OMPC_defaultmap:
6701 enum { Modifier, DefaultmapKind };
6702 Res = ActOnOpenMPDefaultmapClause(
6703 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6704 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6705 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6706 ArgumentLoc[DefaultmapKind], EndLoc);
6707 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006708 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006709 case OMPC_num_threads:
6710 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006711 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006712 case OMPC_collapse:
6713 case OMPC_default:
6714 case OMPC_proc_bind:
6715 case OMPC_private:
6716 case OMPC_firstprivate:
6717 case OMPC_lastprivate:
6718 case OMPC_shared:
6719 case OMPC_reduction:
6720 case OMPC_linear:
6721 case OMPC_aligned:
6722 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006723 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006724 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006725 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006726 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006727 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006728 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006729 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006730 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006731 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006732 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006733 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006734 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006735 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006736 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006737 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006738 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006739 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006740 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006741 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006742 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006743 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006744 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006745 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006746 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006747 case OMPC_unknown:
6748 llvm_unreachable("Clause is not allowed.");
6749 }
6750 return Res;
6751}
6752
Alexey Bataev6402bca2015-12-28 07:25:51 +00006753static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6754 OpenMPScheduleClauseModifier M2,
6755 SourceLocation M1Loc, SourceLocation M2Loc) {
6756 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6757 SmallVector<unsigned, 2> Excluded;
6758 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6759 Excluded.push_back(M2);
6760 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6761 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6762 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6763 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6764 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6765 << getListOfPossibleValues(OMPC_schedule,
6766 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6767 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6768 Excluded)
6769 << getOpenMPClauseName(OMPC_schedule);
6770 return true;
6771 }
6772 return false;
6773}
6774
Alexey Bataev56dafe82014-06-20 07:16:17 +00006775OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006776 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006777 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006778 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6779 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6780 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6781 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6782 return nullptr;
6783 // OpenMP, 2.7.1, Loop Construct, Restrictions
6784 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6785 // but not both.
6786 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6787 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6788 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6789 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6790 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6791 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6792 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6793 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6794 return nullptr;
6795 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006796 if (Kind == OMPC_SCHEDULE_unknown) {
6797 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006798 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6799 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6800 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6801 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6802 Exclude);
6803 } else {
6804 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6805 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006806 }
6807 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6808 << Values << getOpenMPClauseName(OMPC_schedule);
6809 return nullptr;
6810 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006811 // OpenMP, 2.7.1, Loop Construct, Restrictions
6812 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6813 // schedule(guided).
6814 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6815 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6816 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6817 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6818 diag::err_omp_schedule_nonmonotonic_static);
6819 return nullptr;
6820 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006821 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006822 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006823 if (ChunkSize) {
6824 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6825 !ChunkSize->isInstantiationDependent() &&
6826 !ChunkSize->containsUnexpandedParameterPack()) {
6827 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6828 ExprResult Val =
6829 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6830 if (Val.isInvalid())
6831 return nullptr;
6832
6833 ValExpr = Val.get();
6834
6835 // OpenMP [2.7.1, Restrictions]
6836 // chunk_size must be a loop invariant integer expression with a positive
6837 // value.
6838 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006839 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6840 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6841 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006842 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006843 return nullptr;
6844 }
6845 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00006846 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00006847 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
6848 HelperValStmt =
6849 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
6850 /*NumDecls=*/1),
6851 SourceLocation(), SourceLocation());
6852 ValExpr = DefaultLvalueConversion(ValExpr).get();
Alexey Bataev56dafe82014-06-20 07:16:17 +00006853 }
6854 }
6855 }
6856
Alexey Bataev6402bca2015-12-28 07:25:51 +00006857 return new (Context)
6858 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006859 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006860}
6861
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006862OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6863 SourceLocation StartLoc,
6864 SourceLocation EndLoc) {
6865 OMPClause *Res = nullptr;
6866 switch (Kind) {
6867 case OMPC_ordered:
6868 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6869 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006870 case OMPC_nowait:
6871 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6872 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006873 case OMPC_untied:
6874 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6875 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006876 case OMPC_mergeable:
6877 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6878 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006879 case OMPC_read:
6880 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6881 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006882 case OMPC_write:
6883 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6884 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006885 case OMPC_update:
6886 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6887 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006888 case OMPC_capture:
6889 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6890 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006891 case OMPC_seq_cst:
6892 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6893 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006894 case OMPC_threads:
6895 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6896 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006897 case OMPC_simd:
6898 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6899 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006900 case OMPC_nogroup:
6901 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6902 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006903 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006904 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006905 case OMPC_num_threads:
6906 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006907 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006908 case OMPC_collapse:
6909 case OMPC_schedule:
6910 case OMPC_private:
6911 case OMPC_firstprivate:
6912 case OMPC_lastprivate:
6913 case OMPC_shared:
6914 case OMPC_reduction:
6915 case OMPC_linear:
6916 case OMPC_aligned:
6917 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006918 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006919 case OMPC_default:
6920 case OMPC_proc_bind:
6921 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006922 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006923 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006924 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006925 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006926 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006927 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006928 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006929 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006930 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006931 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006932 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006933 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006934 case OMPC_unknown:
6935 llvm_unreachable("Clause is not allowed.");
6936 }
6937 return Res;
6938}
6939
Alexey Bataev236070f2014-06-20 11:19:47 +00006940OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6941 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006942 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006943 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6944}
6945
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006946OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6947 SourceLocation EndLoc) {
6948 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6949}
6950
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006951OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6952 SourceLocation EndLoc) {
6953 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6954}
6955
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006956OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6957 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006958 return new (Context) OMPReadClause(StartLoc, EndLoc);
6959}
6960
Alexey Bataevdea47612014-07-23 07:46:59 +00006961OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6962 SourceLocation EndLoc) {
6963 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6964}
6965
Alexey Bataev67a4f222014-07-23 10:25:33 +00006966OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6967 SourceLocation EndLoc) {
6968 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6969}
6970
Alexey Bataev459dec02014-07-24 06:46:57 +00006971OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6972 SourceLocation EndLoc) {
6973 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6974}
6975
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006976OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6977 SourceLocation EndLoc) {
6978 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6979}
6980
Alexey Bataev346265e2015-09-25 10:37:12 +00006981OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6982 SourceLocation EndLoc) {
6983 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6984}
6985
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006986OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6987 SourceLocation EndLoc) {
6988 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6989}
6990
Alexey Bataevb825de12015-12-07 10:51:44 +00006991OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6992 SourceLocation EndLoc) {
6993 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6994}
6995
Alexey Bataevc5e02582014-06-16 07:08:35 +00006996OMPClause *Sema::ActOnOpenMPVarListClause(
6997 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6998 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6999 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007000 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007001 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7002 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7003 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007004 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007005 switch (Kind) {
7006 case OMPC_private:
7007 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7008 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007009 case OMPC_firstprivate:
7010 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7011 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007012 case OMPC_lastprivate:
7013 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7014 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007015 case OMPC_shared:
7016 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7017 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007018 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007019 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7020 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007021 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007022 case OMPC_linear:
7023 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007024 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007025 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007026 case OMPC_aligned:
7027 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7028 ColonLoc, EndLoc);
7029 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007030 case OMPC_copyin:
7031 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7032 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007033 case OMPC_copyprivate:
7034 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7035 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007036 case OMPC_flush:
7037 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7038 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007039 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007040 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7041 StartLoc, LParenLoc, EndLoc);
7042 break;
7043 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007044 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7045 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7046 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007047 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007048 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007049 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007050 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007051 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007052 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007053 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007054 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007055 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007056 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007057 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007058 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007059 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007060 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007061 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007062 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007063 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007064 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007065 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007066 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007067 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007068 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007069 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007070 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007071 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007072 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007073 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007074 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007075 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007076 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007077 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007078 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007079 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007080 llvm_unreachable("Clause is not allowed.");
7081 }
7082 return Res;
7083}
7084
Alexey Bataev90c228f2016-02-08 09:29:13 +00007085ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007086 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007087 ExprResult Res = BuildDeclRefExpr(
7088 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7089 if (!Res.isUsable())
7090 return ExprError();
7091 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7092 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7093 if (!Res.isUsable())
7094 return ExprError();
7095 }
7096 if (VK != VK_LValue && Res.get()->isGLValue()) {
7097 Res = DefaultLvalueConversion(Res.get());
7098 if (!Res.isUsable())
7099 return ExprError();
7100 }
7101 return Res;
7102}
7103
Alexey Bataev60da77e2016-02-29 05:54:20 +00007104static std::pair<ValueDecl *, bool>
7105getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7106 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007107 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7108 RefExpr->containsUnexpandedParameterPack())
7109 return std::make_pair(nullptr, true);
7110
Alexey Bataevd985eda2016-02-10 11:29:16 +00007111 // OpenMP [3.1, C/C++]
7112 // A list item is a variable name.
7113 // OpenMP [2.9.3.3, Restrictions, p.1]
7114 // A variable that is part of another variable (as an array or
7115 // structure element) cannot appear in a private clause.
7116 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007117 enum {
7118 NoArrayExpr = -1,
7119 ArraySubscript = 0,
7120 OMPArraySection = 1
7121 } IsArrayExpr = NoArrayExpr;
7122 if (AllowArraySection) {
7123 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7124 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7125 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7126 Base = TempASE->getBase()->IgnoreParenImpCasts();
7127 RefExpr = Base;
7128 IsArrayExpr = ArraySubscript;
7129 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7130 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7131 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7132 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7133 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7134 Base = TempASE->getBase()->IgnoreParenImpCasts();
7135 RefExpr = Base;
7136 IsArrayExpr = OMPArraySection;
7137 }
7138 }
7139 ELoc = RefExpr->getExprLoc();
7140 ERange = RefExpr->getSourceRange();
7141 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007142 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7143 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7144 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7145 (S.getCurrentThisType().isNull() || !ME ||
7146 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7147 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007148 if (IsArrayExpr != NoArrayExpr)
7149 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7150 << ERange;
7151 else {
7152 S.Diag(ELoc,
7153 AllowArraySection
7154 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7155 : diag::err_omp_expected_var_name_member_expr)
7156 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7157 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007158 return std::make_pair(nullptr, false);
7159 }
7160 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7161}
7162
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007163OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7164 SourceLocation StartLoc,
7165 SourceLocation LParenLoc,
7166 SourceLocation EndLoc) {
7167 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007168 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007169 for (auto &RefExpr : VarList) {
7170 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007171 SourceLocation ELoc;
7172 SourceRange ERange;
7173 Expr *SimpleRefExpr = RefExpr;
7174 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007175 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007176 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007177 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007178 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007179 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007180 ValueDecl *D = Res.first;
7181 if (!D)
7182 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007183
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007184 QualType Type = D->getType();
7185 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007186
7187 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7188 // A variable that appears in a private clause must not have an incomplete
7189 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007190 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007191 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007192 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007193
Alexey Bataev758e55e2013-09-06 18:03:48 +00007194 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7195 // in a Construct]
7196 // Variables with the predetermined data-sharing attributes may not be
7197 // listed in data-sharing attributes clauses, except for the cases
7198 // listed below. For these exceptions only, listing a predetermined
7199 // variable in a data-sharing attribute clause is allowed and overrides
7200 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007201 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007202 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007203 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7204 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007205 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007206 continue;
7207 }
7208
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007209 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007210 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007211 DSAStack->getCurrentDirective() == OMPD_task) {
7212 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7213 << getOpenMPClauseName(OMPC_private) << Type
7214 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7215 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007216 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007217 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007218 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007219 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007220 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007221 continue;
7222 }
7223
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007224 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7225 // A variable of class type (or array thereof) that appears in a private
7226 // clause requires an accessible, unambiguous default constructor for the
7227 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007228 // Generate helper private variable and initialize it with the default
7229 // value. The address of the original variable is replaced by the address of
7230 // the new private variable in CodeGen. This new variable is not added to
7231 // IdResolver, so the code in the OpenMP region uses original variable for
7232 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007233 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007234 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7235 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007236 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007237 if (VDPrivate->isInvalidDecl())
7238 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007239 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007240 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007241
Alexey Bataev90c228f2016-02-08 09:29:13 +00007242 DeclRefExpr *Ref = nullptr;
7243 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007244 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007245 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7246 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007247 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007248 }
7249
Alexey Bataeved09d242014-05-28 05:53:51 +00007250 if (Vars.empty())
7251 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007252
Alexey Bataev03b340a2014-10-21 03:16:40 +00007253 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7254 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007255}
7256
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007257namespace {
7258class DiagsUninitializedSeveretyRAII {
7259private:
7260 DiagnosticsEngine &Diags;
7261 SourceLocation SavedLoc;
7262 bool IsIgnored;
7263
7264public:
7265 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7266 bool IsIgnored)
7267 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7268 if (!IsIgnored) {
7269 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7270 /*Map*/ diag::Severity::Ignored, Loc);
7271 }
7272 }
7273 ~DiagsUninitializedSeveretyRAII() {
7274 if (!IsIgnored)
7275 Diags.popMappings(SavedLoc);
7276 }
7277};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007278}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007279
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007280OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7281 SourceLocation StartLoc,
7282 SourceLocation LParenLoc,
7283 SourceLocation EndLoc) {
7284 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007285 SmallVector<Expr *, 8> PrivateCopies;
7286 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007287 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007288 bool IsImplicitClause =
7289 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7290 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7291
Alexey Bataeved09d242014-05-28 05:53:51 +00007292 for (auto &RefExpr : VarList) {
7293 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007294 SourceLocation ELoc;
7295 SourceRange ERange;
7296 Expr *SimpleRefExpr = RefExpr;
7297 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007298 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007299 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007300 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007301 PrivateCopies.push_back(nullptr);
7302 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007303 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007304 ValueDecl *D = Res.first;
7305 if (!D)
7306 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007307
Alexey Bataev60da77e2016-02-29 05:54:20 +00007308 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007309 QualType Type = D->getType();
7310 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007311
7312 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7313 // A variable that appears in a private clause must not have an incomplete
7314 // type or a reference type.
7315 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007316 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007317 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007318 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007319
7320 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7321 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007322 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007323 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007324 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007325
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007326 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007327 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007328 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007329 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007330 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007331 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007332 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7333 // A list item that specifies a given variable may not appear in more
7334 // than one clause on the same directive, except that a variable may be
7335 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007336 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007337 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007338 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007339 << getOpenMPClauseName(DVar.CKind)
7340 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007341 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007342 continue;
7343 }
7344
7345 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7346 // in a Construct]
7347 // Variables with the predetermined data-sharing attributes may not be
7348 // listed in data-sharing attributes clauses, except for the cases
7349 // listed below. For these exceptions only, listing a predetermined
7350 // variable in a data-sharing attribute clause is allowed and overrides
7351 // the variable's predetermined data-sharing attributes.
7352 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7353 // in a Construct, C/C++, p.2]
7354 // Variables with const-qualified type having no mutable member may be
7355 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007356 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007357 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7358 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007359 << getOpenMPClauseName(DVar.CKind)
7360 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007361 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007362 continue;
7363 }
7364
Alexey Bataevf29276e2014-06-18 04:14:57 +00007365 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007366 // OpenMP [2.9.3.4, Restrictions, p.2]
7367 // A list item that is private within a parallel region must not appear
7368 // in a firstprivate clause on a worksharing construct if any of the
7369 // worksharing regions arising from the worksharing construct ever bind
7370 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007371 if (isOpenMPWorksharingDirective(CurrDir) &&
7372 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007373 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007374 if (DVar.CKind != OMPC_shared &&
7375 (isOpenMPParallelDirective(DVar.DKind) ||
7376 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007377 Diag(ELoc, diag::err_omp_required_access)
7378 << getOpenMPClauseName(OMPC_firstprivate)
7379 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007380 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007381 continue;
7382 }
7383 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007384 // OpenMP [2.9.3.4, Restrictions, p.3]
7385 // A list item that appears in a reduction clause of a parallel construct
7386 // must not appear in a firstprivate clause on a worksharing or task
7387 // construct if any of the worksharing or task regions arising from the
7388 // worksharing or task construct ever bind to any of the parallel regions
7389 // arising from the parallel construct.
7390 // OpenMP [2.9.3.4, Restrictions, p.4]
7391 // A list item that appears in a reduction clause in worksharing
7392 // construct must not appear in a firstprivate clause in a task construct
7393 // encountered during execution of any of the worksharing regions arising
7394 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007395 if (CurrDir == OMPD_task) {
7396 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007397 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007398 [](OpenMPDirectiveKind K) -> bool {
7399 return isOpenMPParallelDirective(K) ||
7400 isOpenMPWorksharingDirective(K);
7401 },
7402 false);
7403 if (DVar.CKind == OMPC_reduction &&
7404 (isOpenMPParallelDirective(DVar.DKind) ||
7405 isOpenMPWorksharingDirective(DVar.DKind))) {
7406 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7407 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007408 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007409 continue;
7410 }
7411 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007412
7413 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7414 // A list item that is private within a teams region must not appear in a
7415 // firstprivate clause on a distribute construct if any of the distribute
7416 // regions arising from the distribute construct ever bind to any of the
7417 // teams regions arising from the teams construct.
7418 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7419 // A list item that appears in a reduction clause of a teams construct
7420 // must not appear in a firstprivate clause on a distribute construct if
7421 // any of the distribute regions arising from the distribute construct
7422 // ever bind to any of the teams regions arising from the teams construct.
7423 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7424 // A list item may appear in a firstprivate or lastprivate clause but not
7425 // both.
7426 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007427 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007428 [](OpenMPDirectiveKind K) -> bool {
7429 return isOpenMPTeamsDirective(K);
7430 },
7431 false);
7432 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7433 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007434 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007435 continue;
7436 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007437 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007438 [](OpenMPDirectiveKind K) -> bool {
7439 return isOpenMPTeamsDirective(K);
7440 },
7441 false);
7442 if (DVar.CKind == OMPC_reduction &&
7443 isOpenMPTeamsDirective(DVar.DKind)) {
7444 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007445 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007446 continue;
7447 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007448 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007449 if (DVar.CKind == OMPC_lastprivate) {
7450 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007451 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007452 continue;
7453 }
7454 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007455 }
7456
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007457 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007458 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007459 DSAStack->getCurrentDirective() == OMPD_task) {
7460 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7461 << getOpenMPClauseName(OMPC_firstprivate) << Type
7462 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7463 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007464 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007465 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007466 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007467 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007468 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007469 continue;
7470 }
7471
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007472 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007473 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7474 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007475 // Generate helper private variable and initialize it with the value of the
7476 // original variable. The address of the original variable is replaced by
7477 // the address of the new private variable in the CodeGen. This new variable
7478 // is not added to IdResolver, so the code in the OpenMP region uses
7479 // original variable for proper diagnostics and variable capturing.
7480 Expr *VDInitRefExpr = nullptr;
7481 // For arrays generate initializer for single element and replace it by the
7482 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007483 if (Type->isArrayType()) {
7484 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007485 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007486 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007487 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007488 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007489 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007490 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007491 InitializedEntity Entity =
7492 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007493 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7494
7495 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7496 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7497 if (Result.isInvalid())
7498 VDPrivate->setInvalidDecl();
7499 else
7500 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007501 // Remove temp variable declaration.
7502 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007503 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007504 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7505 ".firstprivate.temp");
7506 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7507 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007508 AddInitializerToDecl(VDPrivate,
7509 DefaultLvalueConversion(VDInitRefExpr).get(),
7510 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007511 }
7512 if (VDPrivate->isInvalidDecl()) {
7513 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007514 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007515 diag::note_omp_task_predetermined_firstprivate_here);
7516 }
7517 continue;
7518 }
7519 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007520 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007521 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7522 RefExpr->getExprLoc());
7523 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007524 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007525 if (TopDVar.CKind == OMPC_lastprivate)
7526 Ref = TopDVar.PrivateCopy;
7527 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007528 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007529 if (!IsOpenMPCapturedDecl(D))
7530 ExprCaptures.push_back(Ref->getDecl());
7531 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007532 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007533 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7534 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007535 PrivateCopies.push_back(VDPrivateRefExpr);
7536 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007537 }
7538
Alexey Bataeved09d242014-05-28 05:53:51 +00007539 if (Vars.empty())
7540 return nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007541 Stmt *PreInit = nullptr;
7542 if (!ExprCaptures.empty()) {
7543 PreInit = new (Context)
7544 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7545 ExprCaptures.size()),
7546 SourceLocation(), SourceLocation());
7547 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007548
7549 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev417089f2016-02-17 13:19:37 +00007550 Vars, PrivateCopies, Inits, PreInit);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007551}
7552
Alexander Musman1bb328c2014-06-04 13:06:39 +00007553OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7554 SourceLocation StartLoc,
7555 SourceLocation LParenLoc,
7556 SourceLocation EndLoc) {
7557 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007558 SmallVector<Expr *, 8> SrcExprs;
7559 SmallVector<Expr *, 8> DstExprs;
7560 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007561 SmallVector<Decl *, 4> ExprCaptures;
7562 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007563 for (auto &RefExpr : VarList) {
7564 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007565 SourceLocation ELoc;
7566 SourceRange ERange;
7567 Expr *SimpleRefExpr = RefExpr;
7568 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007569 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007570 // It will be analyzed later.
7571 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007572 SrcExprs.push_back(nullptr);
7573 DstExprs.push_back(nullptr);
7574 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007575 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007576 ValueDecl *D = Res.first;
7577 if (!D)
7578 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007579
Alexey Bataev74caaf22016-02-20 04:09:36 +00007580 QualType Type = D->getType();
7581 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007582
7583 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7584 // A variable that appears in a lastprivate clause must not have an
7585 // incomplete type or a reference type.
7586 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007587 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007588 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007589 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007590
7591 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7592 // in a Construct]
7593 // Variables with the predetermined data-sharing attributes may not be
7594 // listed in data-sharing attributes clauses, except for the cases
7595 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007596 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007597 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7598 DVar.CKind != OMPC_firstprivate &&
7599 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7600 Diag(ELoc, diag::err_omp_wrong_dsa)
7601 << getOpenMPClauseName(DVar.CKind)
7602 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007603 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007604 continue;
7605 }
7606
Alexey Bataevf29276e2014-06-18 04:14:57 +00007607 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7608 // OpenMP [2.14.3.5, Restrictions, p.2]
7609 // A list item that is private within a parallel region, or that appears in
7610 // the reduction clause of a parallel construct, must not appear in a
7611 // lastprivate clause on a worksharing construct if any of the corresponding
7612 // worksharing regions ever binds to any of the corresponding parallel
7613 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007614 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007615 if (isOpenMPWorksharingDirective(CurrDir) &&
7616 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007617 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007618 if (DVar.CKind != OMPC_shared) {
7619 Diag(ELoc, diag::err_omp_required_access)
7620 << getOpenMPClauseName(OMPC_lastprivate)
7621 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007622 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007623 continue;
7624 }
7625 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007626
7627 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7628 // A list item may appear in a firstprivate or lastprivate clause but not
7629 // both.
7630 if (CurrDir == OMPD_distribute) {
7631 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7632 if (DVar.CKind == OMPC_firstprivate) {
7633 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7634 ReportOriginalDSA(*this, DSAStack, D, DVar);
7635 continue;
7636 }
7637 }
7638
Alexander Musman1bb328c2014-06-04 13:06:39 +00007639 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007640 // A variable of class type (or array thereof) that appears in a
7641 // lastprivate clause requires an accessible, unambiguous default
7642 // constructor for the class type, unless the list item is also specified
7643 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007644 // A variable of class type (or array thereof) that appears in a
7645 // lastprivate clause requires an accessible, unambiguous copy assignment
7646 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007647 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007648 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007649 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007650 D->hasAttrs() ? &D->getAttrs() : nullptr);
7651 auto *PseudoSrcExpr =
7652 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007653 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007654 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007655 D->hasAttrs() ? &D->getAttrs() : nullptr);
7656 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007657 // For arrays generate assignment operation for single element and replace
7658 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007659 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007660 PseudoDstExpr, PseudoSrcExpr);
7661 if (AssignmentOp.isInvalid())
7662 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007663 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007664 /*DiscardedValue=*/true);
7665 if (AssignmentOp.isInvalid())
7666 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007667
Alexey Bataev74caaf22016-02-20 04:09:36 +00007668 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007669 if (!VD) {
7670 if (TopDVar.CKind == OMPC_firstprivate)
7671 Ref = TopDVar.PrivateCopy;
7672 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007673 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007674 if (!IsOpenMPCapturedDecl(D))
7675 ExprCaptures.push_back(Ref->getDecl());
7676 }
7677 if (TopDVar.CKind == OMPC_firstprivate ||
7678 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007679 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007680 ExprResult RefRes = DefaultLvalueConversion(Ref);
7681 if (!RefRes.isUsable())
7682 continue;
7683 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007684 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7685 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007686 if (!PostUpdateRes.isUsable())
7687 continue;
7688 ExprPostUpdates.push_back(PostUpdateRes.get());
7689 }
7690 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007691 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007692 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7693 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007694 SrcExprs.push_back(PseudoSrcExpr);
7695 DstExprs.push_back(PseudoDstExpr);
7696 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007697 }
7698
7699 if (Vars.empty())
7700 return nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007701 Stmt *PreInit = nullptr;
7702 if (!ExprCaptures.empty()) {
7703 PreInit = new (Context)
7704 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7705 ExprCaptures.size()),
7706 SourceLocation(), SourceLocation());
7707 }
7708 Expr *PostUpdate = nullptr;
7709 if (!ExprPostUpdates.empty()) {
7710 for (auto *E : ExprPostUpdates) {
7711 ExprResult PostUpdateRes =
7712 PostUpdate
7713 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
7714 : E;
7715 PostUpdate = PostUpdateRes.get();
7716 }
7717 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007718
7719 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00007720 Vars, SrcExprs, DstExprs, AssignmentOps,
7721 PreInit, PostUpdate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007722}
7723
Alexey Bataev758e55e2013-09-06 18:03:48 +00007724OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7725 SourceLocation StartLoc,
7726 SourceLocation LParenLoc,
7727 SourceLocation EndLoc) {
7728 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007729 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007730 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007731 SourceLocation ELoc;
7732 SourceRange ERange;
7733 Expr *SimpleRefExpr = RefExpr;
7734 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007735 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007736 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007737 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007738 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007739 ValueDecl *D = Res.first;
7740 if (!D)
7741 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007742
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007743 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007744 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7745 // in a Construct]
7746 // Variables with the predetermined data-sharing attributes may not be
7747 // listed in data-sharing attributes clauses, except for the cases
7748 // listed below. For these exceptions only, listing a predetermined
7749 // variable in a data-sharing attribute clause is allowed and overrides
7750 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007751 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007752 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7753 DVar.RefExpr) {
7754 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7755 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007756 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007757 continue;
7758 }
7759
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007760 DeclRefExpr *Ref = nullptr;
7761 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007762 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007763 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
7764 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007765 }
7766
Alexey Bataeved09d242014-05-28 05:53:51 +00007767 if (Vars.empty())
7768 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007769
7770 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7771}
7772
Alexey Bataevc5e02582014-06-16 07:08:35 +00007773namespace {
7774class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7775 DSAStackTy *Stack;
7776
7777public:
7778 bool VisitDeclRefExpr(DeclRefExpr *E) {
7779 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007780 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007781 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7782 return false;
7783 if (DVar.CKind != OMPC_unknown)
7784 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007785 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007786 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007787 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007788 return true;
7789 return false;
7790 }
7791 return false;
7792 }
7793 bool VisitStmt(Stmt *S) {
7794 for (auto Child : S->children()) {
7795 if (Child && Visit(Child))
7796 return true;
7797 }
7798 return false;
7799 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007800 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007801};
Alexey Bataev23b69422014-06-18 07:08:49 +00007802} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007803
Alexey Bataev60da77e2016-02-29 05:54:20 +00007804namespace {
7805// Transform MemberExpression for specified FieldDecl of current class to
7806// DeclRefExpr to specified OMPCapturedExprDecl.
7807class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
7808 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
7809 ValueDecl *Field;
7810 DeclRefExpr *CapturedExpr;
7811
7812public:
7813 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
7814 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
7815
7816 ExprResult TransformMemberExpr(MemberExpr *E) {
7817 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
7818 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00007819 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00007820 return CapturedExpr;
7821 }
7822 return BaseTransform::TransformMemberExpr(E);
7823 }
7824 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
7825};
7826} // namespace
7827
Alexey Bataevc5e02582014-06-16 07:08:35 +00007828OMPClause *Sema::ActOnOpenMPReductionClause(
7829 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7830 SourceLocation ColonLoc, SourceLocation EndLoc,
7831 CXXScopeSpec &ReductionIdScopeSpec,
7832 const DeclarationNameInfo &ReductionId) {
7833 // TODO: Allow scope specification search when 'declare reduction' is
7834 // supported.
7835 assert(ReductionIdScopeSpec.isEmpty() &&
7836 "No support for scoped reduction identifiers yet.");
7837
7838 auto DN = ReductionId.getName();
7839 auto OOK = DN.getCXXOverloadedOperator();
7840 BinaryOperatorKind BOK = BO_Comma;
7841
7842 // OpenMP [2.14.3.6, reduction clause]
7843 // C
7844 // reduction-identifier is either an identifier or one of the following
7845 // operators: +, -, *, &, |, ^, && and ||
7846 // C++
7847 // reduction-identifier is either an id-expression or one of the following
7848 // operators: +, -, *, &, |, ^, && and ||
7849 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7850 switch (OOK) {
7851 case OO_Plus:
7852 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007853 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007854 break;
7855 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007856 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007857 break;
7858 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007859 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007860 break;
7861 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007862 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007863 break;
7864 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007865 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007866 break;
7867 case OO_AmpAmp:
7868 BOK = BO_LAnd;
7869 break;
7870 case OO_PipePipe:
7871 BOK = BO_LOr;
7872 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007873 case OO_New:
7874 case OO_Delete:
7875 case OO_Array_New:
7876 case OO_Array_Delete:
7877 case OO_Slash:
7878 case OO_Percent:
7879 case OO_Tilde:
7880 case OO_Exclaim:
7881 case OO_Equal:
7882 case OO_Less:
7883 case OO_Greater:
7884 case OO_LessEqual:
7885 case OO_GreaterEqual:
7886 case OO_PlusEqual:
7887 case OO_MinusEqual:
7888 case OO_StarEqual:
7889 case OO_SlashEqual:
7890 case OO_PercentEqual:
7891 case OO_CaretEqual:
7892 case OO_AmpEqual:
7893 case OO_PipeEqual:
7894 case OO_LessLess:
7895 case OO_GreaterGreater:
7896 case OO_LessLessEqual:
7897 case OO_GreaterGreaterEqual:
7898 case OO_EqualEqual:
7899 case OO_ExclaimEqual:
7900 case OO_PlusPlus:
7901 case OO_MinusMinus:
7902 case OO_Comma:
7903 case OO_ArrowStar:
7904 case OO_Arrow:
7905 case OO_Call:
7906 case OO_Subscript:
7907 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007908 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007909 case NUM_OVERLOADED_OPERATORS:
7910 llvm_unreachable("Unexpected reduction identifier");
7911 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007912 if (auto II = DN.getAsIdentifierInfo()) {
7913 if (II->isStr("max"))
7914 BOK = BO_GT;
7915 else if (II->isStr("min"))
7916 BOK = BO_LT;
7917 }
7918 break;
7919 }
7920 SourceRange ReductionIdRange;
7921 if (ReductionIdScopeSpec.isValid()) {
7922 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7923 }
7924 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7925 if (BOK == BO_Comma) {
7926 // Not allowed reduction identifier is found.
7927 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7928 << ReductionIdRange;
7929 return nullptr;
7930 }
7931
7932 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007933 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007934 SmallVector<Expr *, 8> LHSs;
7935 SmallVector<Expr *, 8> RHSs;
7936 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00007937 SmallVector<Decl *, 4> ExprCaptures;
7938 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007939 for (auto RefExpr : VarList) {
7940 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00007941 // OpenMP [2.1, C/C++]
7942 // A list item is a variable or array section, subject to the restrictions
7943 // specified in Section 2.4 on page 42 and in each of the sections
7944 // describing clauses and directives for which a list appears.
7945 // OpenMP [2.14.3.3, Restrictions, p.1]
7946 // A variable that is part of another variable (as an array or
7947 // structure element) cannot appear in a private clause.
Alexey Bataev60da77e2016-02-29 05:54:20 +00007948 SourceLocation ELoc;
7949 SourceRange ERange;
7950 Expr *SimpleRefExpr = RefExpr;
7951 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
7952 /*AllowArraySection=*/true);
7953 if (Res.second) {
7954 // It will be analyzed later.
7955 Vars.push_back(RefExpr);
7956 Privates.push_back(nullptr);
7957 LHSs.push_back(nullptr);
7958 RHSs.push_back(nullptr);
7959 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007960 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00007961 ValueDecl *D = Res.first;
7962 if (!D)
7963 continue;
7964
Alexey Bataeva1764212015-09-30 09:22:36 +00007965 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00007966 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
7967 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
7968 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00007969 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007970 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007971 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7972 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7973 Type = ATy->getElementType();
7974 else
7975 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007976 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007977 } else
7978 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
7979 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00007980
Alexey Bataevc5e02582014-06-16 07:08:35 +00007981 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7982 // A variable that appears in a private clause must not have an incomplete
7983 // type or a reference type.
7984 if (RequireCompleteType(ELoc, Type,
7985 diag::err_omp_reduction_incomplete_type))
7986 continue;
7987 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007988 // A list item that appears in a reduction clause must not be
7989 // const-qualified.
7990 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007991 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007992 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007993 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007994 bool IsDecl = !VD ||
7995 VD->isThisDeclarationADefinition(Context) ==
7996 VarDecl::DeclarationOnly;
7997 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00007998 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00007999 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008000 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008001 continue;
8002 }
8003 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8004 // If a list-item is a reference type then it must bind to the same object
8005 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008006 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008007 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008008 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008009 DSARefChecker Check(DSAStack);
8010 if (Check.Visit(VDDef->getInit())) {
8011 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8012 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8013 continue;
8014 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008015 }
8016 }
8017 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8018 // The type of a list item that appears in a reduction clause must be valid
8019 // for the reduction-identifier. For a max or min reduction in C, the type
8020 // of the list item must be an allowed arithmetic data type: char, int,
8021 // float, double, or _Bool, possibly modified with long, short, signed, or
8022 // unsigned. For a max or min reduction in C++, the type of the list item
8023 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8024 // double, or bool, possibly modified with long, short, signed, or unsigned.
8025 if ((BOK == BO_GT || BOK == BO_LT) &&
8026 !(Type->isScalarType() ||
8027 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8028 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8029 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008030 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008031 bool IsDecl = !VD ||
8032 VD->isThisDeclarationADefinition(Context) ==
8033 VarDecl::DeclarationOnly;
8034 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008035 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008036 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008037 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008038 continue;
8039 }
8040 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8041 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8042 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008043 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008044 bool IsDecl = !VD ||
8045 VD->isThisDeclarationADefinition(Context) ==
8046 VarDecl::DeclarationOnly;
8047 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008048 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008049 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008050 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008051 continue;
8052 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008053 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8054 // in a Construct]
8055 // Variables with the predetermined data-sharing attributes may not be
8056 // listed in data-sharing attributes clauses, except for the cases
8057 // listed below. For these exceptions only, listing a predetermined
8058 // variable in a data-sharing attribute clause is allowed and overrides
8059 // the variable's predetermined data-sharing attributes.
8060 // OpenMP [2.14.3.6, Restrictions, p.3]
8061 // Any number of reduction clauses can be specified on the directive,
8062 // but a list item can appear only once in the reduction clauses for that
8063 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008064 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008065 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008066 if (DVar.CKind == OMPC_reduction) {
8067 Diag(ELoc, diag::err_omp_once_referenced)
8068 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008069 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008070 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008071 } else if (DVar.CKind != OMPC_unknown) {
8072 Diag(ELoc, diag::err_omp_wrong_dsa)
8073 << getOpenMPClauseName(DVar.CKind)
8074 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008075 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008076 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008077 }
8078
8079 // OpenMP [2.14.3.6, Restrictions, p.1]
8080 // A list item that appears in a reduction clause of a worksharing
8081 // construct must be shared in the parallel regions to which any of the
8082 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008083 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8084 if (isOpenMPWorksharingDirective(CurrDir) &&
8085 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008086 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008087 if (DVar.CKind != OMPC_shared) {
8088 Diag(ELoc, diag::err_omp_required_access)
8089 << getOpenMPClauseName(OMPC_reduction)
8090 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008091 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008092 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008093 }
8094 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008095
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008096 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008097 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008098 D->hasAttrs() ? &D->getAttrs() : nullptr);
8099 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8100 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008101 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008102 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008103 (!ASE &&
8104 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008105 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008106 // Create pseudo array type for private copy. The size for this array will
8107 // be generated during codegen.
8108 // For array subscripts or single variables Private Ty is the same as Type
8109 // (type of the variable or single array element).
8110 PrivateTy = Context.getVariableArrayType(
8111 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8112 Context.getSizeType(), VK_RValue),
8113 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008114 } else if (!ASE && !OASE &&
8115 Context.getAsArrayType(D->getType().getNonReferenceType()))
8116 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008117 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008118 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8119 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008120 // Add initializer for private variable.
8121 Expr *Init = nullptr;
8122 switch (BOK) {
8123 case BO_Add:
8124 case BO_Xor:
8125 case BO_Or:
8126 case BO_LOr:
8127 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008128 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008129 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008130 break;
8131 case BO_Mul:
8132 case BO_LAnd:
8133 if (Type->isScalarType() || Type->isAnyComplexType()) {
8134 // '*' and '&&' reduction ops - initializer is '1'.
8135 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8136 }
8137 break;
8138 case BO_And: {
8139 // '&' reduction op - initializer is '~0'.
8140 QualType OrigType = Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008141 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008142 Type = ComplexTy->getElementType();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008143 if (Type->isRealFloatingType()) {
8144 llvm::APFloat InitValue =
8145 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8146 /*isIEEE=*/true);
8147 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8148 Type, ELoc);
8149 } else if (Type->isScalarType()) {
8150 auto Size = Context.getTypeSize(Type);
8151 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8152 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8153 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8154 }
8155 if (Init && OrigType->isAnyComplexType()) {
8156 // Init = 0xFFFF + 0xFFFFi;
8157 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8158 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8159 }
8160 Type = OrigType;
8161 break;
8162 }
8163 case BO_LT:
8164 case BO_GT: {
8165 // 'min' reduction op - initializer is 'Largest representable number in
8166 // the reduction list item type'.
8167 // 'max' reduction op - initializer is 'Least representable number in
8168 // the reduction list item type'.
8169 if (Type->isIntegerType() || Type->isPointerType()) {
8170 bool IsSigned = Type->hasSignedIntegerRepresentation();
8171 auto Size = Context.getTypeSize(Type);
8172 QualType IntTy =
8173 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8174 llvm::APInt InitValue =
8175 (BOK != BO_LT)
8176 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8177 : llvm::APInt::getMinValue(Size)
8178 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8179 : llvm::APInt::getMaxValue(Size);
8180 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8181 if (Type->isPointerType()) {
8182 // Cast to pointer type.
8183 auto CastExpr = BuildCStyleCastExpr(
8184 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8185 SourceLocation(), Init);
8186 if (CastExpr.isInvalid())
8187 continue;
8188 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008189 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008190 } else if (Type->isRealFloatingType()) {
8191 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8192 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8193 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8194 Type, ELoc);
8195 }
8196 break;
8197 }
8198 case BO_PtrMemD:
8199 case BO_PtrMemI:
8200 case BO_MulAssign:
8201 case BO_Div:
8202 case BO_Rem:
8203 case BO_Sub:
8204 case BO_Shl:
8205 case BO_Shr:
8206 case BO_LE:
8207 case BO_GE:
8208 case BO_EQ:
8209 case BO_NE:
8210 case BO_AndAssign:
8211 case BO_XorAssign:
8212 case BO_OrAssign:
8213 case BO_Assign:
8214 case BO_AddAssign:
8215 case BO_SubAssign:
8216 case BO_DivAssign:
8217 case BO_RemAssign:
8218 case BO_ShlAssign:
8219 case BO_ShrAssign:
8220 case BO_Comma:
8221 llvm_unreachable("Unexpected reduction operation");
8222 }
8223 if (Init) {
8224 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8225 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008226 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008227 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008228 if (!RHSVD->hasInit()) {
8229 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8230 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008231 bool IsDecl =
8232 !VD ||
8233 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8234 Diag(D->getLocation(),
8235 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8236 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008237 continue;
8238 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008239 // Store initializer for single element in private copy. Will be used during
8240 // codegen.
8241 PrivateVD->setInit(RHSVD->getInit());
8242 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008243 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8244 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008245 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008246 ExprResult ReductionOp =
8247 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8248 LHSDRE, RHSDRE);
8249 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008250 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008251 ReductionOp =
8252 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8253 BO_Assign, LHSDRE, ReductionOp.get());
8254 } else {
8255 auto *ConditionalOp = new (Context) ConditionalOperator(
8256 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8257 RHSDRE, Type, VK_LValue, OK_Ordinary);
8258 ReductionOp =
8259 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8260 BO_Assign, LHSDRE, ConditionalOp);
8261 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008262 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008263 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008264 if (ReductionOp.isInvalid())
8265 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008266
Alexey Bataev60da77e2016-02-29 05:54:20 +00008267 DeclRefExpr *Ref = nullptr;
8268 Expr *VarsExpr = RefExpr->IgnoreParens();
8269 if (!VD) {
8270 if (ASE || OASE) {
8271 TransformExprToCaptures RebuildToCapture(*this, D);
8272 VarsExpr =
8273 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8274 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008275 } else {
8276 VarsExpr = Ref =
8277 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8278 if (!IsOpenMPCapturedDecl(D)) {
8279 ExprCaptures.push_back(Ref->getDecl());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008280 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataev61205072016-03-02 04:57:40 +00008281 ExprResult RefRes = DefaultLvalueConversion(Ref);
8282 if (!RefRes.isUsable())
8283 continue;
8284 ExprResult PostUpdateRes =
8285 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8286 SimpleRefExpr, RefRes.get());
8287 if (!PostUpdateRes.isUsable())
8288 continue;
8289 ExprPostUpdates.push_back(PostUpdateRes.get());
8290 }
8291 }
8292 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008293 }
8294 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8295 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008296 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008297 LHSs.push_back(LHSDRE);
8298 RHSs.push_back(RHSDRE);
8299 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008300 }
8301
8302 if (Vars.empty())
8303 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008304 Stmt *PreInit = nullptr;
8305 if (!ExprCaptures.empty()) {
8306 PreInit = new (Context)
8307 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
8308 ExprCaptures.size()),
8309 SourceLocation(), SourceLocation());
8310 }
8311 Expr *PostUpdate = nullptr;
8312 if (!ExprPostUpdates.empty()) {
8313 for (auto *E : ExprPostUpdates) {
8314 ExprResult PostUpdateRes =
8315 PostUpdate
8316 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
8317 : E;
8318 PostUpdate = PostUpdateRes.get();
8319 }
8320 }
8321
Alexey Bataevc5e02582014-06-16 07:08:35 +00008322
8323 return OMPReductionClause::Create(
8324 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008325 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev61205072016-03-02 04:57:40 +00008326 LHSs, RHSs, ReductionOps, PreInit, PostUpdate);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008327}
8328
Alexey Bataev182227b2015-08-20 10:54:39 +00008329OMPClause *Sema::ActOnOpenMPLinearClause(
8330 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8331 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8332 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008333 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008334 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008335 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008336 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8337 LinKind == OMPC_LINEAR_unknown) {
8338 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8339 LinKind = OMPC_LINEAR_val;
8340 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008341 for (auto &RefExpr : VarList) {
8342 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008343 SourceLocation ELoc;
8344 SourceRange ERange;
8345 Expr *SimpleRefExpr = RefExpr;
8346 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8347 /*AllowArraySection=*/false);
8348 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008349 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008350 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008351 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008352 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008353 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008354 ValueDecl *D = Res.first;
8355 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008356 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008357
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008358 QualType Type = D->getType();
8359 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008360
8361 // OpenMP [2.14.3.7, linear clause]
8362 // A list-item cannot appear in more than one linear clause.
8363 // A list-item that appears in a linear clause cannot appear in any
8364 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008365 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008366 if (DVar.RefExpr) {
8367 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8368 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008369 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008370 continue;
8371 }
8372
8373 // A variable must not have an incomplete type or a reference type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008374 if (RequireCompleteType(ELoc, Type,
8375 diag::err_omp_linear_incomplete_type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008376 continue;
Alexey Bataev1185e192015-08-20 12:15:57 +00008377 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008378 !Type->isReferenceType()) {
Alexey Bataev1185e192015-08-20 12:15:57 +00008379 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008380 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
Alexey Bataev1185e192015-08-20 12:15:57 +00008381 continue;
8382 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008383 Type = Type.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008384
8385 // A list item must not be const-qualified.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008386 if (Type.isConstant(Context)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008387 Diag(ELoc, diag::err_omp_const_variable)
8388 << getOpenMPClauseName(OMPC_linear);
8389 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008390 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008391 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008392 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008393 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008394 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008395 continue;
8396 }
8397
8398 // A list item must be of integral or pointer type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008399 Type = Type.getUnqualifiedType().getCanonicalType();
8400 const auto *Ty = Type.getTypePtrOrNull();
Alexander Musman8dba6642014-04-22 13:09:42 +00008401 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8402 !Ty->isPointerType())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008403 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
Alexander Musman8dba6642014-04-22 13:09:42 +00008404 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008405 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008406 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008407 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008408 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008409 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008410 continue;
8411 }
8412
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008413 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008414 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8415 D->hasAttrs() ? &D->getAttrs() : nullptr);
8416 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008417 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008418 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008419 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008420 DeclRefExpr *Ref = nullptr;
8421 if (!VD)
8422 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008423 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008424 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008425 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008426 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008427 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008428 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8429 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8430
8431 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8432 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008433 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008434 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008435 }
8436
8437 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008438 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008439
8440 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008441 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008442 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8443 !Step->isInstantiationDependent() &&
8444 !Step->containsUnexpandedParameterPack()) {
8445 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008446 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008447 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008448 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008449 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008450
Alexander Musman3276a272015-03-21 10:12:56 +00008451 // Build var to save the step value.
8452 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008453 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008454 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008455 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008456 ExprResult CalcStep =
8457 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008458 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008459
Alexander Musman8dba6642014-04-22 13:09:42 +00008460 // Warn about zero linear step (it would be probably better specified as
8461 // making corresponding variables 'const').
8462 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008463 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8464 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008465 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8466 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008467 if (!IsConstant && CalcStep.isUsable()) {
8468 // Calculate the step beforehand instead of doing this on each iteration.
8469 // (This is not used if the number of iterations may be kfold-ed).
8470 CalcStepExpr = CalcStep.get();
8471 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008472 }
8473
Alexey Bataev182227b2015-08-20 10:54:39 +00008474 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8475 ColonLoc, EndLoc, Vars, Privates, Inits,
8476 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008477}
8478
8479static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8480 Expr *NumIterations, Sema &SemaRef,
8481 Scope *S) {
8482 // Walk the vars and build update/final expressions for the CodeGen.
8483 SmallVector<Expr *, 8> Updates;
8484 SmallVector<Expr *, 8> Finals;
8485 Expr *Step = Clause.getStep();
8486 Expr *CalcStep = Clause.getCalcStep();
8487 // OpenMP [2.14.3.7, linear clause]
8488 // If linear-step is not specified it is assumed to be 1.
8489 if (Step == nullptr)
8490 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8491 else if (CalcStep)
8492 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8493 bool HasErrors = false;
8494 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008495 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008496 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008497 for (auto &RefExpr : Clause.varlists()) {
8498 Expr *InitExpr = *CurInit;
8499
8500 // Build privatized reference to the current linear var.
8501 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008502 Expr *CapturedRef;
8503 if (LinKind == OMPC_LINEAR_uval)
8504 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8505 else
8506 CapturedRef =
8507 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8508 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8509 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008510
8511 // Build update: Var = InitExpr + IV * Step
8512 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008513 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008514 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008515 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8516 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008517
8518 // Build final: Var = InitExpr + NumIterations * Step
8519 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008520 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008521 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008522 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8523 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008524 if (!Update.isUsable() || !Final.isUsable()) {
8525 Updates.push_back(nullptr);
8526 Finals.push_back(nullptr);
8527 HasErrors = true;
8528 } else {
8529 Updates.push_back(Update.get());
8530 Finals.push_back(Final.get());
8531 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008532 ++CurInit;
8533 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008534 }
8535 Clause.setUpdates(Updates);
8536 Clause.setFinals(Finals);
8537 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008538}
8539
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008540OMPClause *Sema::ActOnOpenMPAlignedClause(
8541 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8542 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8543
8544 SmallVector<Expr *, 8> Vars;
8545 for (auto &RefExpr : VarList) {
8546 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8547 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8548 // It will be analyzed later.
8549 Vars.push_back(RefExpr);
8550 continue;
8551 }
8552
8553 SourceLocation ELoc = RefExpr->getExprLoc();
8554 // OpenMP [2.1, C/C++]
8555 // A list item is a variable name.
8556 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8557 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008558 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8559 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008560 continue;
8561 }
8562
8563 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8564
8565 // OpenMP [2.8.1, simd construct, Restrictions]
8566 // The type of list items appearing in the aligned clause must be
8567 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008568 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008569 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008570 const Type *Ty = QType.getTypePtrOrNull();
8571 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8572 !Ty->isPointerType())) {
8573 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8574 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8575 bool IsDecl =
8576 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8577 Diag(VD->getLocation(),
8578 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8579 << VD;
8580 continue;
8581 }
8582
8583 // OpenMP [2.8.1, simd construct, Restrictions]
8584 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008585 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008586 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8587 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8588 << getOpenMPClauseName(OMPC_aligned);
8589 continue;
8590 }
8591
8592 Vars.push_back(DE);
8593 }
8594
8595 // OpenMP [2.8.1, simd construct, Description]
8596 // The parameter of the aligned clause, alignment, must be a constant
8597 // positive integer expression.
8598 // If no optional parameter is specified, implementation-defined default
8599 // alignments for SIMD instructions on the target platforms are assumed.
8600 if (Alignment != nullptr) {
8601 ExprResult AlignResult =
8602 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8603 if (AlignResult.isInvalid())
8604 return nullptr;
8605 Alignment = AlignResult.get();
8606 }
8607 if (Vars.empty())
8608 return nullptr;
8609
8610 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8611 EndLoc, Vars, Alignment);
8612}
8613
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008614OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8615 SourceLocation StartLoc,
8616 SourceLocation LParenLoc,
8617 SourceLocation EndLoc) {
8618 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008619 SmallVector<Expr *, 8> SrcExprs;
8620 SmallVector<Expr *, 8> DstExprs;
8621 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008622 for (auto &RefExpr : VarList) {
8623 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8624 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008625 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008626 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008627 SrcExprs.push_back(nullptr);
8628 DstExprs.push_back(nullptr);
8629 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008630 continue;
8631 }
8632
Alexey Bataeved09d242014-05-28 05:53:51 +00008633 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008634 // OpenMP [2.1, C/C++]
8635 // A list item is a variable name.
8636 // OpenMP [2.14.4.1, Restrictions, p.1]
8637 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008638 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008639 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008640 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8641 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008642 continue;
8643 }
8644
8645 Decl *D = DE->getDecl();
8646 VarDecl *VD = cast<VarDecl>(D);
8647
8648 QualType Type = VD->getType();
8649 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8650 // It will be analyzed later.
8651 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008652 SrcExprs.push_back(nullptr);
8653 DstExprs.push_back(nullptr);
8654 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008655 continue;
8656 }
8657
8658 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8659 // A list item that appears in a copyin clause must be threadprivate.
8660 if (!DSAStack->isThreadPrivate(VD)) {
8661 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008662 << getOpenMPClauseName(OMPC_copyin)
8663 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008664 continue;
8665 }
8666
8667 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8668 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008669 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008670 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008671 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008672 auto *SrcVD =
8673 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8674 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008675 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008676 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8677 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008678 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8679 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008680 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008681 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008682 // For arrays generate assignment operation for single element and replace
8683 // it by the original array element in CodeGen.
8684 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8685 PseudoDstExpr, PseudoSrcExpr);
8686 if (AssignmentOp.isInvalid())
8687 continue;
8688 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8689 /*DiscardedValue=*/true);
8690 if (AssignmentOp.isInvalid())
8691 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008692
8693 DSAStack->addDSA(VD, DE, OMPC_copyin);
8694 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008695 SrcExprs.push_back(PseudoSrcExpr);
8696 DstExprs.push_back(PseudoDstExpr);
8697 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008698 }
8699
Alexey Bataeved09d242014-05-28 05:53:51 +00008700 if (Vars.empty())
8701 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008702
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008703 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8704 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008705}
8706
Alexey Bataevbae9a792014-06-27 10:37:06 +00008707OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8708 SourceLocation StartLoc,
8709 SourceLocation LParenLoc,
8710 SourceLocation EndLoc) {
8711 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008712 SmallVector<Expr *, 8> SrcExprs;
8713 SmallVector<Expr *, 8> DstExprs;
8714 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008715 for (auto &RefExpr : VarList) {
8716 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8717 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8718 // It will be analyzed later.
8719 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008720 SrcExprs.push_back(nullptr);
8721 DstExprs.push_back(nullptr);
8722 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008723 continue;
8724 }
8725
8726 SourceLocation ELoc = RefExpr->getExprLoc();
8727 // OpenMP [2.1, C/C++]
8728 // A list item is a variable name.
8729 // OpenMP [2.14.4.1, Restrictions, p.1]
8730 // A list item that appears in a copyin clause must be threadprivate.
8731 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8732 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008733 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8734 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008735 continue;
8736 }
8737
8738 Decl *D = DE->getDecl();
8739 VarDecl *VD = cast<VarDecl>(D);
8740
8741 QualType Type = VD->getType();
8742 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8743 // It will be analyzed later.
8744 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008745 SrcExprs.push_back(nullptr);
8746 DstExprs.push_back(nullptr);
8747 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008748 continue;
8749 }
8750
8751 // OpenMP [2.14.4.2, Restrictions, p.2]
8752 // A list item that appears in a copyprivate clause may not appear in a
8753 // private or firstprivate clause on the single construct.
8754 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008755 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008756 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8757 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008758 Diag(ELoc, diag::err_omp_wrong_dsa)
8759 << getOpenMPClauseName(DVar.CKind)
8760 << getOpenMPClauseName(OMPC_copyprivate);
8761 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8762 continue;
8763 }
8764
8765 // OpenMP [2.11.4.2, Restrictions, p.1]
8766 // All list items that appear in a copyprivate clause must be either
8767 // threadprivate or private in the enclosing context.
8768 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008769 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008770 if (DVar.CKind == OMPC_shared) {
8771 Diag(ELoc, diag::err_omp_required_access)
8772 << getOpenMPClauseName(OMPC_copyprivate)
8773 << "threadprivate or private in the enclosing context";
8774 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8775 continue;
8776 }
8777 }
8778 }
8779
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008780 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008781 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008782 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008783 << getOpenMPClauseName(OMPC_copyprivate) << Type
8784 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008785 bool IsDecl =
8786 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8787 Diag(VD->getLocation(),
8788 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8789 << VD;
8790 continue;
8791 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008792
Alexey Bataevbae9a792014-06-27 10:37:06 +00008793 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8794 // A variable of class type (or array thereof) that appears in a
8795 // copyin clause requires an accessible, unambiguous copy assignment
8796 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008797 Type = Context.getBaseElementType(Type.getNonReferenceType())
8798 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008799 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008800 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8801 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008802 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008803 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008804 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008805 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8806 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008807 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008808 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008809 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8810 PseudoDstExpr, PseudoSrcExpr);
8811 if (AssignmentOp.isInvalid())
8812 continue;
8813 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8814 /*DiscardedValue=*/true);
8815 if (AssignmentOp.isInvalid())
8816 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008817
8818 // No need to mark vars as copyprivate, they are already threadprivate or
8819 // implicitly private.
8820 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008821 SrcExprs.push_back(PseudoSrcExpr);
8822 DstExprs.push_back(PseudoDstExpr);
8823 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008824 }
8825
8826 if (Vars.empty())
8827 return nullptr;
8828
Alexey Bataeva63048e2015-03-23 06:18:07 +00008829 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8830 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008831}
8832
Alexey Bataev6125da92014-07-21 11:26:11 +00008833OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8834 SourceLocation StartLoc,
8835 SourceLocation LParenLoc,
8836 SourceLocation EndLoc) {
8837 if (VarList.empty())
8838 return nullptr;
8839
8840 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8841}
Alexey Bataevdea47612014-07-23 07:46:59 +00008842
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008843OMPClause *
8844Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8845 SourceLocation DepLoc, SourceLocation ColonLoc,
8846 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8847 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008848 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008849 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008850 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008851 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008852 return nullptr;
8853 }
8854 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008855 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8856 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008857 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008858 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008859 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8860 /*Last=*/OMPC_DEPEND_unknown, Except)
8861 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008862 return nullptr;
8863 }
8864 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008865 llvm::APSInt DepCounter(/*BitWidth=*/32);
8866 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8867 if (DepKind == OMPC_DEPEND_sink) {
8868 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8869 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8870 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008871 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008872 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008873 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8874 DSAStack->getParentOrderedRegionParam()) {
8875 for (auto &RefExpr : VarList) {
8876 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8877 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8878 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8879 // It will be analyzed later.
8880 Vars.push_back(RefExpr);
8881 continue;
8882 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008883
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008884 SourceLocation ELoc = RefExpr->getExprLoc();
8885 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8886 if (DepKind == OMPC_DEPEND_sink) {
8887 if (DepCounter >= TotalDepCount) {
8888 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8889 continue;
8890 }
8891 ++DepCounter;
8892 // OpenMP [2.13.9, Summary]
8893 // depend(dependence-type : vec), where dependence-type is:
8894 // 'sink' and where vec is the iteration vector, which has the form:
8895 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8896 // where n is the value specified by the ordered clause in the loop
8897 // directive, xi denotes the loop iteration variable of the i-th nested
8898 // loop associated with the loop directive, and di is a constant
8899 // non-negative integer.
8900 SimpleExpr = SimpleExpr->IgnoreImplicit();
8901 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8902 if (!DE) {
8903 OverloadedOperatorKind OOK = OO_None;
8904 SourceLocation OOLoc;
8905 Expr *LHS, *RHS;
8906 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8907 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8908 OOLoc = BO->getOperatorLoc();
8909 LHS = BO->getLHS()->IgnoreParenImpCasts();
8910 RHS = BO->getRHS()->IgnoreParenImpCasts();
8911 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8912 OOK = OCE->getOperator();
8913 OOLoc = OCE->getOperatorLoc();
8914 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8915 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8916 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8917 OOK = MCE->getMethodDecl()
8918 ->getNameInfo()
8919 .getName()
8920 .getCXXOverloadedOperator();
8921 OOLoc = MCE->getCallee()->getExprLoc();
8922 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8923 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8924 } else {
8925 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8926 continue;
8927 }
8928 DE = dyn_cast<DeclRefExpr>(LHS);
8929 if (!DE) {
8930 Diag(LHS->getExprLoc(),
8931 diag::err_omp_depend_sink_expected_loop_iteration)
8932 << DSAStack->getParentLoopControlVariable(
8933 DepCounter.getZExtValue());
8934 continue;
8935 }
8936 if (OOK != OO_Plus && OOK != OO_Minus) {
8937 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8938 continue;
8939 }
8940 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8941 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8942 if (Res.isInvalid())
8943 continue;
8944 }
8945 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8946 if (!CurContext->isDependentContext() &&
8947 DSAStack->getParentOrderedRegionParam() &&
8948 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8949 Diag(DE->getExprLoc(),
8950 diag::err_omp_depend_sink_expected_loop_iteration)
8951 << DSAStack->getParentLoopControlVariable(
8952 DepCounter.getZExtValue());
8953 continue;
8954 }
8955 } else {
8956 // OpenMP [2.11.1.1, Restrictions, p.3]
8957 // A variable that is part of another variable (such as a field of a
8958 // structure) but is not an array element or an array section cannot
8959 // appear in a depend clause.
8960 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8961 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8962 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8963 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8964 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008965 (ASE &&
8966 !ASE->getBase()
8967 ->getType()
8968 .getNonReferenceType()
8969 ->isPointerType() &&
8970 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008971 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8972 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008973 continue;
8974 }
8975 }
8976
8977 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8978 }
8979
8980 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8981 TotalDepCount > VarList.size() &&
8982 DSAStack->getParentOrderedRegionParam()) {
8983 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8984 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8985 }
8986 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8987 Vars.empty())
8988 return nullptr;
8989 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008990
8991 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8992 DepLoc, ColonLoc, Vars);
8993}
Michael Wonge710d542015-08-07 16:16:36 +00008994
8995OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8996 SourceLocation LParenLoc,
8997 SourceLocation EndLoc) {
8998 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008999
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009000 // OpenMP [2.9.1, Restrictions]
9001 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009002 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9003 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009004 return nullptr;
9005
Michael Wonge710d542015-08-07 16:16:36 +00009006 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9007}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009008
9009static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9010 DSAStackTy *Stack, CXXRecordDecl *RD) {
9011 if (!RD || RD->isInvalidDecl())
9012 return true;
9013
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009014 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9015 if (auto *CTD = CTSD->getSpecializedTemplate())
9016 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009017 auto QTy = SemaRef.Context.getRecordType(RD);
9018 if (RD->isDynamicClass()) {
9019 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9020 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9021 return false;
9022 }
9023 auto *DC = RD;
9024 bool IsCorrect = true;
9025 for (auto *I : DC->decls()) {
9026 if (I) {
9027 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9028 if (MD->isStatic()) {
9029 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9030 SemaRef.Diag(MD->getLocation(),
9031 diag::note_omp_static_member_in_target);
9032 IsCorrect = false;
9033 }
9034 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9035 if (VD->isStaticDataMember()) {
9036 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9037 SemaRef.Diag(VD->getLocation(),
9038 diag::note_omp_static_member_in_target);
9039 IsCorrect = false;
9040 }
9041 }
9042 }
9043 }
9044
9045 for (auto &I : RD->bases()) {
9046 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9047 I.getType()->getAsCXXRecordDecl()))
9048 IsCorrect = false;
9049 }
9050 return IsCorrect;
9051}
9052
9053static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9054 DSAStackTy *Stack, QualType QTy) {
9055 NamedDecl *ND;
9056 if (QTy->isIncompleteType(&ND)) {
9057 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9058 return false;
9059 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9060 if (!RD->isInvalidDecl() &&
9061 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9062 return false;
9063 }
9064 return true;
9065}
9066
Samuel Antao5de996e2016-01-22 20:21:36 +00009067// Return the expression of the base of the map clause or null if it cannot
9068// be determined and do all the necessary checks to see if the expression is
9069// valid as a standalone map clause expression.
9070static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9071 SourceLocation ELoc = E->getExprLoc();
9072 SourceRange ERange = E->getSourceRange();
9073
9074 // The base of elements of list in a map clause have to be either:
9075 // - a reference to variable or field.
9076 // - a member expression.
9077 // - an array expression.
9078 //
9079 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9080 // reference to 'r'.
9081 //
9082 // If we have:
9083 //
9084 // struct SS {
9085 // Bla S;
9086 // foo() {
9087 // #pragma omp target map (S.Arr[:12]);
9088 // }
9089 // }
9090 //
9091 // We want to retrieve the member expression 'this->S';
9092
9093 Expr *RelevantExpr = nullptr;
9094
9095 // Flags to help capture some memory
9096
9097 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9098 // If a list item is an array section, it must specify contiguous storage.
9099 //
9100 // For this restriction it is sufficient that we make sure only references
9101 // to variables or fields and array expressions, and that no array sections
9102 // exist except in the rightmost expression. E.g. these would be invalid:
9103 //
9104 // r.ArrS[3:5].Arr[6:7]
9105 //
9106 // r.ArrS[3:5].x
9107 //
9108 // but these would be valid:
9109 // r.ArrS[3].Arr[6:7]
9110 //
9111 // r.ArrS[3].x
9112
9113 bool IsRightMostExpression = true;
9114
9115 while (!RelevantExpr) {
9116 auto AllowArraySection = IsRightMostExpression;
9117 IsRightMostExpression = false;
9118
9119 E = E->IgnoreParenImpCasts();
9120
9121 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9122 if (!isa<VarDecl>(CurE->getDecl()))
9123 break;
9124
9125 RelevantExpr = CurE;
9126 continue;
9127 }
9128
9129 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9130 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9131
9132 if (isa<CXXThisExpr>(BaseE))
9133 // We found a base expression: this->Val.
9134 RelevantExpr = CurE;
9135 else
9136 E = BaseE;
9137
9138 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9139 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9140 << CurE->getSourceRange();
9141 break;
9142 }
9143
9144 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9145
9146 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9147 // A bit-field cannot appear in a map clause.
9148 //
9149 if (FD->isBitField()) {
9150 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9151 << CurE->getSourceRange();
9152 break;
9153 }
9154
9155 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9156 // If the type of a list item is a reference to a type T then the type
9157 // will be considered to be T for all purposes of this clause.
9158 QualType CurType = BaseE->getType();
9159 if (CurType->isReferenceType())
9160 CurType = CurType->getPointeeType();
9161
9162 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9163 // A list item cannot be a variable that is a member of a structure with
9164 // a union type.
9165 //
9166 if (auto *RT = CurType->getAs<RecordType>())
9167 if (RT->isUnionType()) {
9168 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9169 << CurE->getSourceRange();
9170 break;
9171 }
9172
9173 continue;
9174 }
9175
9176 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9177 E = CurE->getBase()->IgnoreParenImpCasts();
9178
9179 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9180 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9181 << 0 << CurE->getSourceRange();
9182 break;
9183 }
9184 continue;
9185 }
9186
9187 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9188 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9189 // If a list item is an element of a structure, only the rightmost symbol
9190 // of the variable reference can be an array section.
9191 //
9192 if (!AllowArraySection) {
9193 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9194 << CurE->getSourceRange();
9195 break;
9196 }
9197
9198 E = CurE->getBase()->IgnoreParenImpCasts();
9199
9200 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9201 // If the type of a list item is a reference to a type T then the type
9202 // will be considered to be T for all purposes of this clause.
9203 QualType CurType = E->getType();
9204 if (CurType->isReferenceType())
9205 CurType = CurType->getPointeeType();
9206
9207 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9208 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9209 << 0 << CurE->getSourceRange();
9210 break;
9211 }
9212
9213 continue;
9214 }
9215
9216 // If nothing else worked, this is not a valid map clause expression.
9217 SemaRef.Diag(ELoc,
9218 diag::err_omp_expected_named_var_member_or_array_expression)
9219 << ERange;
9220 break;
9221 }
9222
9223 return RelevantExpr;
9224}
9225
9226// Return true if expression E associated with value VD has conflicts with other
9227// map information.
9228static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9229 Expr *E, bool CurrentRegionOnly) {
9230 assert(VD && E);
9231
9232 // Types used to organize the components of a valid map clause.
9233 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9234 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9235
9236 // Helper to extract the components in the map clause expression E and store
9237 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9238 // it has already passed the single clause checks.
9239 auto ExtractMapExpressionComponents = [](Expr *TE,
9240 MapExpressionComponents &MEC) {
9241 while (true) {
9242 TE = TE->IgnoreParenImpCasts();
9243
9244 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9245 MEC.push_back(
9246 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9247 break;
9248 }
9249
9250 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9251 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9252
9253 MEC.push_back(MapExpressionComponent(
9254 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9255 if (isa<CXXThisExpr>(BaseE))
9256 break;
9257
9258 TE = BaseE;
9259 continue;
9260 }
9261
9262 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9263 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9264 TE = CurE->getBase()->IgnoreParenImpCasts();
9265 continue;
9266 }
9267
9268 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9269 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9270 TE = CurE->getBase()->IgnoreParenImpCasts();
9271 continue;
9272 }
9273
9274 llvm_unreachable(
9275 "Expecting only valid map clause expressions at this point!");
9276 }
9277 };
9278
9279 SourceLocation ELoc = E->getExprLoc();
9280 SourceRange ERange = E->getSourceRange();
9281
9282 // In order to easily check the conflicts we need to match each component of
9283 // the expression under test with the components of the expressions that are
9284 // already in the stack.
9285
9286 MapExpressionComponents CurComponents;
9287 ExtractMapExpressionComponents(E, CurComponents);
9288
9289 assert(!CurComponents.empty() && "Map clause expression with no components!");
9290 assert(CurComponents.back().second == VD &&
9291 "Map clause expression with unexpected base!");
9292
9293 // Variables to help detecting enclosing problems in data environment nests.
9294 bool IsEnclosedByDataEnvironmentExpr = false;
9295 Expr *EnclosingExpr = nullptr;
9296
9297 bool FoundError =
9298 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9299 MapExpressionComponents StackComponents;
9300 ExtractMapExpressionComponents(RE, StackComponents);
9301 assert(!StackComponents.empty() &&
9302 "Map clause expression with no components!");
9303 assert(StackComponents.back().second == VD &&
9304 "Map clause expression with unexpected base!");
9305
9306 // Expressions must start from the same base. Here we detect at which
9307 // point both expressions diverge from each other and see if we can
9308 // detect if the memory referred to both expressions is contiguous and
9309 // do not overlap.
9310 auto CI = CurComponents.rbegin();
9311 auto CE = CurComponents.rend();
9312 auto SI = StackComponents.rbegin();
9313 auto SE = StackComponents.rend();
9314 for (; CI != CE && SI != SE; ++CI, ++SI) {
9315
9316 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9317 // At most one list item can be an array item derived from a given
9318 // variable in map clauses of the same construct.
9319 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9320 isa<OMPArraySectionExpr>(CI->first)) &&
9321 (isa<ArraySubscriptExpr>(SI->first) ||
9322 isa<OMPArraySectionExpr>(SI->first))) {
9323 SemaRef.Diag(CI->first->getExprLoc(),
9324 diag::err_omp_multiple_array_items_in_map_clause)
9325 << CI->first->getSourceRange();
9326 ;
9327 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9328 << SI->first->getSourceRange();
9329 return true;
9330 }
9331
9332 // Do both expressions have the same kind?
9333 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9334 break;
9335
9336 // Are we dealing with different variables/fields?
9337 if (CI->second != SI->second)
9338 break;
9339 }
9340
9341 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9342 // List items of map clauses in the same construct must not share
9343 // original storage.
9344 //
9345 // If the expressions are exactly the same or one is a subset of the
9346 // other, it means they are sharing storage.
9347 if (CI == CE && SI == SE) {
9348 if (CurrentRegionOnly) {
9349 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9350 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9351 << RE->getSourceRange();
9352 return true;
9353 } else {
9354 // If we find the same expression in the enclosing data environment,
9355 // that is legal.
9356 IsEnclosedByDataEnvironmentExpr = true;
9357 return false;
9358 }
9359 }
9360
9361 QualType DerivedType = std::prev(CI)->first->getType();
9362 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9363
9364 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9365 // If the type of a list item is a reference to a type T then the type
9366 // will be considered to be T for all purposes of this clause.
9367 if (DerivedType->isReferenceType())
9368 DerivedType = DerivedType->getPointeeType();
9369
9370 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9371 // A variable for which the type is pointer and an array section
9372 // derived from that variable must not appear as list items of map
9373 // clauses of the same construct.
9374 //
9375 // Also, cover one of the cases in:
9376 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9377 // If any part of the original storage of a list item has corresponding
9378 // storage in the device data environment, all of the original storage
9379 // must have corresponding storage in the device data environment.
9380 //
9381 if (DerivedType->isAnyPointerType()) {
9382 if (CI == CE || SI == SE) {
9383 SemaRef.Diag(
9384 DerivedLoc,
9385 diag::err_omp_pointer_mapped_along_with_derived_section)
9386 << DerivedLoc;
9387 } else {
9388 assert(CI != CE && SI != SE);
9389 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9390 << DerivedLoc;
9391 }
9392 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9393 << RE->getSourceRange();
9394 return true;
9395 }
9396
9397 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9398 // List items of map clauses in the same construct must not share
9399 // original storage.
9400 //
9401 // An expression is a subset of the other.
9402 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9403 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9404 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9405 << RE->getSourceRange();
9406 return true;
9407 }
9408
9409 // The current expression uses the same base as other expression in the
9410 // data environment but does not contain it completelly.
9411 if (!CurrentRegionOnly && SI != SE)
9412 EnclosingExpr = RE;
9413
9414 // The current expression is a subset of the expression in the data
9415 // environment.
9416 IsEnclosedByDataEnvironmentExpr |=
9417 (!CurrentRegionOnly && CI != CE && SI == SE);
9418
9419 return false;
9420 });
9421
9422 if (CurrentRegionOnly)
9423 return FoundError;
9424
9425 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9426 // If any part of the original storage of a list item has corresponding
9427 // storage in the device data environment, all of the original storage must
9428 // have corresponding storage in the device data environment.
9429 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9430 // If a list item is an element of a structure, and a different element of
9431 // the structure has a corresponding list item in the device data environment
9432 // prior to a task encountering the construct associated with the map clause,
9433 // then the list item must also have a correspnding list item in the device
9434 // data environment prior to the task encountering the construct.
9435 //
9436 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9437 SemaRef.Diag(ELoc,
9438 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9439 << ERange;
9440 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9441 << EnclosingExpr->getSourceRange();
9442 return true;
9443 }
9444
9445 return FoundError;
9446}
9447
Samuel Antao23abd722016-01-19 20:40:49 +00009448OMPClause *
9449Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9450 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9451 SourceLocation MapLoc, SourceLocation ColonLoc,
9452 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9453 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009454 SmallVector<Expr *, 4> Vars;
9455
9456 for (auto &RE : VarList) {
9457 assert(RE && "Null expr in omp map");
9458 if (isa<DependentScopeDeclRefExpr>(RE)) {
9459 // It will be analyzed later.
9460 Vars.push_back(RE);
9461 continue;
9462 }
9463 SourceLocation ELoc = RE->getExprLoc();
9464
Kelvin Li0bff7af2015-11-23 05:32:03 +00009465 auto *VE = RE->IgnoreParenLValueCasts();
9466
9467 if (VE->isValueDependent() || VE->isTypeDependent() ||
9468 VE->isInstantiationDependent() ||
9469 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009470 // We can only analyze this information once the missing information is
9471 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009472 Vars.push_back(RE);
9473 continue;
9474 }
9475
9476 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009477
Samuel Antao5de996e2016-01-22 20:21:36 +00009478 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9479 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9480 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009481 continue;
9482 }
9483
Samuel Antao5de996e2016-01-22 20:21:36 +00009484 // Obtain the array or member expression bases if required.
9485 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9486 if (!BE)
9487 continue;
9488
9489 // If the base is a reference to a variable, we rely on that variable for
9490 // the following checks. If it is a 'this' expression we rely on the field.
9491 ValueDecl *D = nullptr;
9492 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9493 D = DRE->getDecl();
9494 } else {
9495 auto *ME = cast<MemberExpr>(BE);
9496 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9497 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009498 }
9499 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009500
Samuel Antao5de996e2016-01-22 20:21:36 +00009501 auto *VD = dyn_cast<VarDecl>(D);
9502 auto *FD = dyn_cast<FieldDecl>(D);
9503
9504 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009505 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009506
9507 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9508 // threadprivate variables cannot appear in a map clause.
9509 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009510 auto DVar = DSAStack->getTopDSA(VD, false);
9511 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9512 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9513 continue;
9514 }
9515
Samuel Antao5de996e2016-01-22 20:21:36 +00009516 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9517 // A list item cannot appear in both a map clause and a data-sharing
9518 // attribute clause on the same construct.
9519 //
9520 // TODO: Implement this check - it cannot currently be tested because of
9521 // missing implementation of the other data sharing clauses in target
9522 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009523
Samuel Antao5de996e2016-01-22 20:21:36 +00009524 // Check conflicts with other map clause expressions. We check the conflicts
9525 // with the current construct separately from the enclosing data
9526 // environment, because the restrictions are different.
9527 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9528 /*CurrentRegionOnly=*/true))
9529 break;
9530 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9531 /*CurrentRegionOnly=*/false))
9532 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009533
Samuel Antao5de996e2016-01-22 20:21:36 +00009534 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9535 // If the type of a list item is a reference to a type T then the type will
9536 // be considered to be T for all purposes of this clause.
9537 QualType Type = D->getType();
9538 if (Type->isReferenceType())
9539 Type = Type->getPointeeType();
9540
9541 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009542 // A list item must have a mappable type.
9543 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9544 DSAStack, Type))
9545 continue;
9546
Samuel Antaodf67fc42016-01-19 19:15:56 +00009547 // target enter data
9548 // OpenMP [2.10.2, Restrictions, p. 99]
9549 // A map-type must be specified in all map clauses and must be either
9550 // to or alloc.
9551 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9552 if (DKind == OMPD_target_enter_data &&
9553 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9554 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009555 << (IsMapTypeImplicit ? 1 : 0)
9556 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009557 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009558 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009559 }
9560
Samuel Antao72590762016-01-19 20:04:50 +00009561 // target exit_data
9562 // OpenMP [2.10.3, Restrictions, p. 102]
9563 // A map-type must be specified in all map clauses and must be either
9564 // from, release, or delete.
9565 DKind = DSAStack->getCurrentDirective();
9566 if (DKind == OMPD_target_exit_data &&
9567 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9568 MapType == OMPC_MAP_delete)) {
9569 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009570 << (IsMapTypeImplicit ? 1 : 0)
9571 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009572 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009573 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009574 }
9575
Kelvin Li0bff7af2015-11-23 05:32:03 +00009576 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009577 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009578 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009579
Samuel Antao5de996e2016-01-22 20:21:36 +00009580 // We need to produce a map clause even if we don't have variables so that
9581 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009582 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009583 MapTypeModifier, MapType, IsMapTypeImplicit,
9584 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009585}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009586
9587OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9588 SourceLocation StartLoc,
9589 SourceLocation LParenLoc,
9590 SourceLocation EndLoc) {
9591 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009592
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009593 // OpenMP [teams Constrcut, Restrictions]
9594 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009595 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9596 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009597 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009598
9599 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9600}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009601
9602OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9603 SourceLocation StartLoc,
9604 SourceLocation LParenLoc,
9605 SourceLocation EndLoc) {
9606 Expr *ValExpr = ThreadLimit;
9607
9608 // OpenMP [teams Constrcut, Restrictions]
9609 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009610 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9611 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009612 return nullptr;
9613
9614 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9615 EndLoc);
9616}
Alexey Bataeva0569352015-12-01 10:17:31 +00009617
9618OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9619 SourceLocation StartLoc,
9620 SourceLocation LParenLoc,
9621 SourceLocation EndLoc) {
9622 Expr *ValExpr = Priority;
9623
9624 // OpenMP [2.9.1, task Constrcut]
9625 // The priority-value is a non-negative numerical scalar expression.
9626 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9627 /*StrictlyPositive=*/false))
9628 return nullptr;
9629
9630 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9631}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009632
9633OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9634 SourceLocation StartLoc,
9635 SourceLocation LParenLoc,
9636 SourceLocation EndLoc) {
9637 Expr *ValExpr = Grainsize;
9638
9639 // OpenMP [2.9.2, taskloop Constrcut]
9640 // The parameter of the grainsize clause must be a positive integer
9641 // expression.
9642 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9643 /*StrictlyPositive=*/true))
9644 return nullptr;
9645
9646 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9647}
Alexey Bataev382967a2015-12-08 12:06:20 +00009648
9649OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9650 SourceLocation StartLoc,
9651 SourceLocation LParenLoc,
9652 SourceLocation EndLoc) {
9653 Expr *ValExpr = NumTasks;
9654
9655 // OpenMP [2.9.2, taskloop Constrcut]
9656 // The parameter of the num_tasks clause must be a positive integer
9657 // expression.
9658 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9659 /*StrictlyPositive=*/true))
9660 return nullptr;
9661
9662 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9663}
9664
Alexey Bataev28c75412015-12-15 08:19:24 +00009665OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9666 SourceLocation LParenLoc,
9667 SourceLocation EndLoc) {
9668 // OpenMP [2.13.2, critical construct, Description]
9669 // ... where hint-expression is an integer constant expression that evaluates
9670 // to a valid lock hint.
9671 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9672 if (HintExpr.isInvalid())
9673 return nullptr;
9674 return new (Context)
9675 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9676}
9677
Carlo Bertollib4adf552016-01-15 18:50:31 +00009678OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9679 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9680 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9681 SourceLocation EndLoc) {
9682 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9683 std::string Values;
9684 Values += "'";
9685 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9686 Values += "'";
9687 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9688 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9689 return nullptr;
9690 }
9691 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009692 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009693 if (ChunkSize) {
9694 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9695 !ChunkSize->isInstantiationDependent() &&
9696 !ChunkSize->containsUnexpandedParameterPack()) {
9697 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9698 ExprResult Val =
9699 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9700 if (Val.isInvalid())
9701 return nullptr;
9702
9703 ValExpr = Val.get();
9704
9705 // OpenMP [2.7.1, Restrictions]
9706 // chunk_size must be a loop invariant integer expression with a positive
9707 // value.
9708 llvm::APSInt Result;
9709 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9710 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9711 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9712 << "dist_schedule" << ChunkSize->getSourceRange();
9713 return nullptr;
9714 }
9715 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009716 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00009717 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
9718 HelperValStmt =
9719 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
9720 /*NumDecls=*/1),
9721 SourceLocation(), SourceLocation());
9722 ValExpr = DefaultLvalueConversion(ValExpr).get();
Carlo Bertollib4adf552016-01-15 18:50:31 +00009723 }
9724 }
9725 }
9726
9727 return new (Context)
9728 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +00009729 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +00009730}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009731
9732OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9733 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9734 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9735 SourceLocation KindLoc, SourceLocation EndLoc) {
9736 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9737 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9738 Kind != OMPC_DEFAULTMAP_scalar) {
9739 std::string Value;
9740 SourceLocation Loc;
9741 Value += "'";
9742 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9743 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9744 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9745 Loc = MLoc;
9746 } else {
9747 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9748 OMPC_DEFAULTMAP_scalar);
9749 Loc = KindLoc;
9750 }
9751 Value += "'";
9752 Diag(Loc, diag::err_omp_unexpected_clause_value)
9753 << Value << getOpenMPClauseName(OMPC_defaultmap);
9754 return nullptr;
9755 }
9756
9757 return new (Context)
9758 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9759}