blob: 31943fa676f95365e1230e7b3db0da9e525ce580 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000073 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000074 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000075 SourceLocation ImplicitDSALoc;
76 DSAVarData()
77 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000078 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000079 };
Alexey Bataeved09d242014-05-28 05:53:51 +000080
Alexey Bataev758e55e2013-09-06 18:03:48 +000081private:
Samuel Antao5de996e2016-01-22 20:21:36 +000082 typedef SmallVector<Expr *, 4> MapInfo;
83
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 struct DSAInfo {
85 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000086 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000087 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
90 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000091 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000092 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000093 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
94 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095
96 struct SharingMapTy {
97 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000098 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000099 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000100 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 OpenMPDirectiveKind Directive;
104 DeclarationNameInfo DirectiveName;
105 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000106 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000107 /// \brief first argument (Expr *) contains optional argument of the
108 /// 'ordered' clause, the second one is true if the regions has 'ordered'
109 /// clause, false otherwise.
110 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000111 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000112 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000113 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000114 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000115 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000116 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000117 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000118 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000119 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000122 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000124 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 };
127
Axel Naumann323862e2016-02-03 10:45:22 +0000128 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129
130 /// \brief Stack of used declaration and their data-sharing attributes.
131 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000132 /// \brief true, if check for DSA must be from parent directive, false, if
133 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000135 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000136 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000137 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138
139 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
140
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000141 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000142
143 /// \brief Checks if the variable is a local for OpenMP region.
144 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000145
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000147 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000148 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
149 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000150
Alexey Bataevaac108a2015-06-23 04:51:00 +0000151 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
152 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000154 bool isForceVarCapturing() const { return ForceCapturing; }
155 void setForceVarCapturing(bool V) { ForceCapturing = V; }
156
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000158 Scope *CurScope, SourceLocation Loc) {
159 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
160 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161 }
162
163 void pop() {
164 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
165 Stack.pop_back();
166 }
167
Alexey Bataev28c75412015-12-15 08:19:24 +0000168 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
169 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
170 }
171 const std::pair<OMPCriticalDirective *, llvm::APSInt>
172 getCriticalWithHint(const DeclarationNameInfo &Name) const {
173 auto I = Criticals.find(Name.getAsString());
174 if (I != Criticals.end())
175 return I->second;
176 return std::make_pair(nullptr, llvm::APSInt());
177 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000178 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000179 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000181 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000182
Alexey Bataev9c821032015-04-30 04:23:23 +0000183 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000185 /// \brief Check if the specified variable is a loop control variable for
186 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000187 /// \return The index of the loop control variable in the list of associated
188 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000189 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \brief Check if the specified variable is a loop control variable for
191 /// parent region.
192 /// \return The index of the loop control variable in the list of associated
193 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000194 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000195 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
196 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000197 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000198
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000200 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
201 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data sharing attributes from top of the stack for the
204 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000205 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 /// \brief Checks if the specified variables has data-sharing attributes which
209 /// match specified \a CPred predicate in any directive which matches \a DPred
210 /// predicate.
211 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000212 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000213 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000214 /// \brief Checks if the specified variables has data-sharing attributes which
215 /// match specified \a CPred predicate in any innermost directive which
216 /// matches \a DPred predicate.
217 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000218 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
219 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000220 /// \brief Checks if the specified variables has explicit data-sharing
221 /// attributes which match specified \a CPred predicate at the specified
222 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000223 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000224 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
225 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000226
227 /// \brief Returns true if the directive at level \Level matches in the
228 /// specified \a DPred predicate.
229 bool hasExplicitDirective(
230 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
231 unsigned Level);
232
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000233 /// \brief Finds a directive which matches specified \a DPred predicate.
234 template <class NamedDirectivesPredicate>
235 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 /// \brief Returns currently analyzed directive.
238 OpenMPDirectiveKind getCurrentDirective() const {
239 return Stack.back().Directive;
240 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000241 /// \brief Returns parent directive.
242 OpenMPDirectiveKind getParentDirective() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].Directive;
245 return OMPD_unknown;
246 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000247 /// \brief Return the directive associated with the provided scope.
248 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249
250 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000251 void setDefaultDSANone(SourceLocation Loc) {
252 Stack.back().DefaultAttr = DSA_none;
253 Stack.back().DefaultAttrLoc = Loc;
254 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000256 void setDefaultDSAShared(SourceLocation Loc) {
257 Stack.back().DefaultAttr = DSA_shared;
258 Stack.back().DefaultAttrLoc = Loc;
259 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260
261 DefaultDataSharingAttributes getDefaultDSA() const {
262 return Stack.back().DefaultAttr;
263 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000264 SourceLocation getDefaultDSALocation() const {
265 return Stack.back().DefaultAttrLoc;
266 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000270 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 }
273
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000274 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000275 void setOrderedRegion(bool IsOrdered, Expr *Param) {
276 Stack.back().OrderedRegion.setInt(IsOrdered);
277 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000278 }
279 /// \brief Returns true, if parent region is ordered (has associated
280 /// 'ordered' clause), false - otherwise.
281 bool isParentOrderedRegion() const {
282 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000284 return false;
285 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 /// \brief Returns optional parameter for the ordered region.
287 Expr *getParentOrderedRegionParam() const {
288 if (Stack.size() > 2)
289 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
290 return nullptr;
291 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000292 /// \brief Marks current region as nowait (it has a 'nowait' clause).
293 void setNowaitRegion(bool IsNowait = true) {
294 Stack.back().NowaitRegion = IsNowait;
295 }
296 /// \brief Returns true, if parent region is nowait (has associated
297 /// 'nowait' clause), false - otherwise.
298 bool isParentNowaitRegion() const {
299 if (Stack.size() > 2)
300 return Stack[Stack.size() - 2].NowaitRegion;
301 return false;
302 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000303 /// \brief Marks parent region as cancel region.
304 void setParentCancelRegion(bool Cancel = true) {
305 if (Stack.size() > 2)
306 Stack[Stack.size() - 2].CancelRegion =
307 Stack[Stack.size() - 2].CancelRegion || Cancel;
308 }
309 /// \brief Return true if current region has inner cancel construct.
310 bool isCancelRegion() const {
311 return Stack.back().CancelRegion;
312 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000313
Alexey Bataev9c821032015-04-30 04:23:23 +0000314 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000315 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318
Alexey Bataev13314bf2014-10-09 04:18:56 +0000319 /// \brief Marks current target region as one with closely nested teams
320 /// region.
321 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
322 if (Stack.size() > 2)
323 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
324 }
325 /// \brief Returns true, if current region has closely nested teams region.
326 bool hasInnerTeamsRegion() const {
327 return getInnerTeamsRegionLoc().isValid();
328 }
329 /// \brief Returns location of the nested teams region (if any).
330 SourceLocation getInnerTeamsRegionLoc() const {
331 if (Stack.size() > 1)
332 return Stack.back().InnerTeamsRegionLoc;
333 return SourceLocation();
334 }
335
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000336 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000338 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000339
Samuel Antao5de996e2016-01-22 20:21:36 +0000340 // Do the check specified in MapInfoCheck and return true if any issue is
341 // found.
342 template <class MapInfoCheck>
343 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
344 MapInfoCheck Check) {
345 auto SI = Stack.rbegin();
346 auto SE = Stack.rend();
347
348 if (SI == SE)
349 return false;
350
351 if (CurrentRegionOnly) {
352 SE = std::next(SI);
353 } else {
354 ++SI;
355 }
356
357 for (; SI != SE; ++SI) {
358 auto MI = SI->MappedDecls.find(VD);
359 if (MI != SI->MappedDecls.end()) {
360 for (Expr *E : MI->second) {
361 if (Check(E))
362 return true;
363 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000364 }
365 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000371 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000372 }
373 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000375bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
376 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000377 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000378 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000379}
Alexey Bataeved09d242014-05-28 05:53:51 +0000380} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000382static ValueDecl *getCanonicalDecl(ValueDecl *D) {
383 auto *VD = dyn_cast<VarDecl>(D);
384 auto *FD = dyn_cast<FieldDecl>(D);
385 if (VD != nullptr) {
386 VD = VD->getCanonicalDecl();
387 D = VD;
388 } else {
389 assert(FD);
390 FD = FD->getCanonicalDecl();
391 D = FD;
392 }
393 return D;
394}
395
Alexey Bataev758e55e2013-09-06 18:03:48 +0000396DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000397 ValueDecl *D) {
398 D = getCanonicalDecl(D);
399 auto *VD = dyn_cast<VarDecl>(D);
400 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000402 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a region but not in construct]
405 // File-scope or namespace-scope variables referenced in called routines
406 // in the region are shared unless they appear in a threadprivate
407 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000409 DVar.CKind = OMPC_shared;
410
411 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
412 // in a region but not in construct]
413 // Variables with static storage duration that are declared in called
414 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000415 if (VD && VD->hasGlobalStorage())
416 DVar.CKind = OMPC_shared;
417
418 // Non-static data members are shared by default.
419 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 return DVar;
423 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000424
Alexey Bataev758e55e2013-09-06 18:03:48 +0000425 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427 // in a Construct, C/C++, predetermined, p.1]
428 // Variables with automatic storage duration that are declared in a scope
429 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000430 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
431 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000432 DVar.CKind = OMPC_private;
433 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000434 }
435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
438 if (Iter->SharingMap.count(D)) {
439 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000440 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000442 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 return DVar;
444 }
445
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, C/C++, implicitly determined, p.1]
448 // In a parallel or task construct, the data-sharing attributes of these
449 // variables are determined by the default clause, if present.
450 switch (Iter->DefaultAttr) {
451 case DSA_shared:
452 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 case DSA_none:
456 return DVar;
457 case DSA_unspecified:
458 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
459 // in a Construct, implicitly determined, p.2]
460 // In a parallel construct, if no default clause is present, these
461 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000462 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000463 if (isOpenMPParallelDirective(DVar.DKind) ||
464 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 DVar.CKind = OMPC_shared;
466 return DVar;
467 }
468
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.4]
471 // In a task construct, if no default clause is present, a variable that in
472 // the enclosing context is determined to be shared by all implicit tasks
473 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 if (DVar.DKind == OMPD_task) {
475 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000476 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000478 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
479 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 // in a Construct, implicitly determined, p.6]
481 // In a task construct, if no default clause is present, a variable
482 // whose data-sharing attribute is not determined by the rules above is
483 // firstprivate.
484 DVarTemp = getDSA(I, D);
485 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000486 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 return DVar;
490 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 }
494 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000496 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 return DVar;
498 }
499 }
500 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
501 // in a Construct, implicitly determined, p.3]
502 // For constructs other than task, if no default clause is present, these
503 // variables inherit their data-sharing attributes from the enclosing
504 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000505 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000506}
507
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000508Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000509 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000511 auto It = Stack.back().AlignedMap.find(D);
512 if (It == Stack.back().AlignedMap.end()) {
513 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
514 Stack.back().AlignedMap[D] = NewDE;
515 return nullptr;
516 } else {
517 assert(It->second && "Unexpected nullptr expr in the aligned map");
518 return It->second;
519 }
520 return nullptr;
521}
522
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000523void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000524 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000526 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000527}
528
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000529unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000530 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000532 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
533}
534
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000535unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000536 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
539 ? Stack[Stack.size() - 2].LCVMap[D]
540 : 0;
541}
542
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000543ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000544 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
545 if (Stack[Stack.size() - 2].LCVMap.size() < I)
546 return nullptr;
547 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
548 if (Pair.second == I)
549 return Pair.first;
550 }
551 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000552}
553
Alexey Bataev90c228f2016-02-08 09:29:13 +0000554void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
555 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000556 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000557 if (A == OMPC_threadprivate) {
558 Stack[0].SharingMap[D].Attributes = A;
559 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000560 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000561 } else {
562 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
563 Stack.back().SharingMap[D].Attributes = A;
564 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000565 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
566 if (PrivateCopy)
567 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568 }
569}
570
Alexey Bataeved09d242014-05-28 05:53:51 +0000571bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000572 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000574 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000575 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000577 ++I;
578 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000579 if (I == E)
580 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000581 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000582 Scope *CurScope = getCurScope();
583 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000584 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000585 }
586 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000588 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589}
590
Alexey Bataev39f915b82015-05-08 10:41:21 +0000591/// \brief Build a variable declaration for OpenMP loop iteration variable.
592static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000593 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000594 DeclContext *DC = SemaRef.CurContext;
595 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
596 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
597 VarDecl *Decl =
598 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000599 if (Attrs) {
600 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
601 I != E; ++I)
602 Decl->addAttr(*I);
603 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000604 Decl->setImplicit();
605 return Decl;
606}
607
608static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
609 SourceLocation Loc,
610 bool RefersToCapture = false) {
611 D->setReferenced();
612 D->markUsed(S.Context);
613 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
614 SourceLocation(), D, RefersToCapture, Loc, Ty,
615 VK_LValue);
616}
617
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000618DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
619 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620 DSAVarData DVar;
621
622 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
623 // in a Construct, C/C++, predetermined, p.1]
624 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000625 auto *VD = dyn_cast<VarDecl>(D);
626 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
627 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000628 SemaRef.getLangOpts().OpenMPUseTLS &&
629 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000630 (VD && VD->getStorageClass() == SC_Register &&
631 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
632 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000633 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000634 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000635 }
636 if (Stack[0].SharingMap.count(D)) {
637 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
638 DVar.CKind = OMPC_threadprivate;
639 return DVar;
640 }
641
642 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000643 // in a Construct, C/C++, predetermined, p.4]
644 // Static data members are shared.
645 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
646 // in a Construct, C/C++, predetermined, p.7]
647 // Variables with static storage duration that are declared in a scope
648 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000649 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000650 DSAVarData DVarTemp =
651 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
652 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000653 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000655 DVar.CKind = OMPC_shared;
656 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657 }
658
659 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000660 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
661 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
663 // in a Construct, C/C++, predetermined, p.6]
664 // Variables with const qualified type having no mutable member are
665 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000666 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000667 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000668 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
669 if (auto *CTD = CTSD->getSpecializedTemplate())
670 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000672 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
673 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674 // Variables with const-qualified type having no mutable member may be
675 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000676 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
677 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000678 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
679 return DVar;
680
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 DVar.CKind = OMPC_shared;
682 return DVar;
683 }
684
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685 // Explicitly specified attributes and local variables with predetermined
686 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000687 auto StartI = std::next(Stack.rbegin());
688 auto EndI = std::prev(Stack.rend());
689 if (FromParent && StartI != EndI) {
690 StartI = std::next(StartI);
691 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000692 auto I = std::prev(StartI);
693 if (I->SharingMap.count(D)) {
694 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000695 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000696 DVar.CKind = I->SharingMap[D].Attributes;
697 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000698 }
699
700 return DVar;
701}
702
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000703DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
704 bool FromParent) {
705 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 auto StartI = Stack.rbegin();
707 auto EndI = std::prev(Stack.rend());
708 if (FromParent && StartI != EndI) {
709 StartI = std::next(StartI);
710 }
711 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000712}
713
Alexey Bataevf29276e2014-06-18 04:14:57 +0000714template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000715DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000716 DirectivesPredicate DPred,
717 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000718 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
724 for (auto I = StartI, EE = EndI; I != EE; ++I) {
725 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000727 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000728 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 return DVar;
730 }
731 return DSAVarData();
732}
733
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000736DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000737 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000738 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000739 auto StartI = std::next(Stack.rbegin());
740 auto EndI = std::prev(Stack.rend());
741 if (FromParent && StartI != EndI) {
742 StartI = std::next(StartI);
743 }
744 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000745 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000746 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000747 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000748 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000749 return DVar;
750 return DSAVarData();
751 }
752 return DSAVarData();
753}
754
Alexey Bataevaac108a2015-06-23 04:51:00 +0000755bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000756 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000757 unsigned Level) {
758 if (CPred(ClauseKindMode))
759 return true;
760 if (isClauseParsingMode())
761 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000762 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000763 auto StartI = Stack.rbegin();
764 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000765 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000766 return false;
767 std::advance(StartI, Level);
768 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
769 CPred(StartI->SharingMap[D].Attributes);
770}
771
Samuel Antao4be30e92015-10-02 17:14:03 +0000772bool DSAStackTy::hasExplicitDirective(
773 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
774 unsigned Level) {
775 if (isClauseParsingMode())
776 ++Level;
777 auto StartI = Stack.rbegin();
778 auto EndI = std::prev(Stack.rend());
779 if (std::distance(StartI, EndI) <= (int)Level)
780 return false;
781 std::advance(StartI, Level);
782 return DPred(StartI->Directive);
783}
784
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785template <class NamedDirectivesPredicate>
786bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
787 auto StartI = std::next(Stack.rbegin());
788 auto EndI = std::prev(Stack.rend());
789 if (FromParent && StartI != EndI) {
790 StartI = std::next(StartI);
791 }
792 for (auto I = StartI, EE = EndI; I != EE; ++I) {
793 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
794 return true;
795 }
796 return false;
797}
798
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000799OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
800 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
801 if (I->CurScope == S)
802 return I->Directive;
803 return OMPD_unknown;
804}
805
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806void Sema::InitDataSharingAttributesStack() {
807 VarDataSharingAttributesStack = new DSAStackTy(*this);
808}
809
810#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
811
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000812bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000813 const CapturedRegionScopeInfo *RSI) {
814 assert(LangOpts.OpenMP && "OpenMP is not allowed");
815
816 auto &Ctx = getASTContext();
817 bool IsByRef = true;
818
819 // Find the directive that is associated with the provided scope.
820 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000821 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000822
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000823 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824 // This table summarizes how a given variable should be passed to the device
825 // given its type and the clauses where it appears. This table is based on
826 // the description in OpenMP 4.5 [2.10.4, target Construct] and
827 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
828 //
829 // =========================================================================
830 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
831 // | |(tofrom:scalar)| | pvt | | | |
832 // =========================================================================
833 // | scl | | | | - | | bycopy|
834 // | scl | | - | x | - | - | bycopy|
835 // | scl | | x | - | - | - | null |
836 // | scl | x | | | - | | byref |
837 // | scl | x | - | x | - | - | bycopy|
838 // | scl | x | x | - | - | - | null |
839 // | scl | | - | - | - | x | byref |
840 // | scl | x | - | - | - | x | byref |
841 //
842 // | agg | n.a. | | | - | | byref |
843 // | agg | n.a. | - | x | - | - | byref |
844 // | agg | n.a. | x | - | - | - | null |
845 // | agg | n.a. | - | - | - | x | byref |
846 // | agg | n.a. | - | - | - | x[] | byref |
847 //
848 // | ptr | n.a. | | | - | | bycopy|
849 // | ptr | n.a. | - | x | - | - | bycopy|
850 // | ptr | n.a. | x | - | - | - | null |
851 // | ptr | n.a. | - | - | - | x | byref |
852 // | ptr | n.a. | - | - | - | x[] | bycopy|
853 // | ptr | n.a. | - | - | x | | bycopy|
854 // | ptr | n.a. | - | - | x | x | bycopy|
855 // | ptr | n.a. | - | - | x | x[] | bycopy|
856 // =========================================================================
857 // Legend:
858 // scl - scalar
859 // ptr - pointer
860 // agg - aggregate
861 // x - applies
862 // - - invalid in this combination
863 // [] - mapped with an array section
864 // byref - should be mapped by reference
865 // byval - should be mapped by value
866 // null - initialize a local variable to null on the device
867 //
868 // Observations:
869 // - All scalar declarations that show up in a map clause have to be passed
870 // by reference, because they may have been mapped in the enclosing data
871 // environment.
872 // - If the scalar value does not fit the size of uintptr, it has to be
873 // passed by reference, regardless the result in the table above.
874 // - For pointers mapped by value that have either an implicit map or an
875 // array section, the runtime library may pass the NULL value to the
876 // device instead of the value passed to it by the compiler.
877
878 // FIXME: Right now, only implicit maps are implemented. Properly mapping
879 // values requires having the map, private, and firstprivate clauses SEMA
880 // and parsing in place, which we don't yet.
881
882 if (Ty->isReferenceType())
883 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
884 IsByRef = !Ty->isScalarType();
885 }
886
887 // When passing data by value, we need to make sure it fits the uintptr size
888 // and alignment, because the runtime library only deals with uintptr types.
889 // If it does not fit the uintptr size, we need to pass the data by reference
890 // instead.
891 if (!IsByRef &&
892 (Ctx.getTypeSizeInChars(Ty) >
893 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000894 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000895 IsByRef = true;
896
897 return IsByRef;
898}
899
Alexey Bataev90c228f2016-02-08 09:29:13 +0000900VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000901 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000902 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000903
904 // If we are attempting to capture a global variable in a directive with
905 // 'target' we return true so that this global is also mapped to the device.
906 //
907 // FIXME: If the declaration is enclosed in a 'declare target' directive,
908 // then it should not be captured. Therefore, an extra check has to be
909 // inserted here once support for 'declare target' is added.
910 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000911 auto *VD = dyn_cast<VarDecl>(D);
912 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000913 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000914 !DSAStack->isClauseParsingMode())
915 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000916 if (DSAStack->getCurScope() &&
917 DSAStack->hasDirective(
918 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
919 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000920 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000921 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000922 false))
923 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000924 }
925
Alexey Bataev48977c32015-08-04 08:10:48 +0000926 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
927 (!DSAStack->isClauseParsingMode() ||
928 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000929 if (DSAStack->isLoopControlVariable(D) ||
930 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000931 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000932 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000933 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000935 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000936 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000937 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000938 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000939 if (DVarPrivate.CKind != OMPC_unknown)
940 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000941 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000942 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000943}
944
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000946 assert(LangOpts.OpenMP && "OpenMP is not allowed");
947 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000948 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000949}
950
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000951bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000952 assert(LangOpts.OpenMP && "OpenMP is not allowed");
953 // Return true if the current level is no longer enclosed in a target region.
954
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955 auto *VD = dyn_cast<VarDecl>(D);
956 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000957 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
958 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000959}
960
Alexey Bataeved09d242014-05-28 05:53:51 +0000961void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962
963void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
964 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000965 Scope *CurScope, SourceLocation Loc) {
966 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967 PushExpressionEvaluationContext(PotentiallyEvaluated);
968}
969
Alexey Bataevaac108a2015-06-23 04:51:00 +0000970void Sema::StartOpenMPClause(OpenMPClauseKind K) {
971 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000972}
973
Alexey Bataevaac108a2015-06-23 04:51:00 +0000974void Sema::EndOpenMPClause() {
975 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000976}
977
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000979 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
980 // A variable of class type (or array thereof) that appears in a lastprivate
981 // clause requires an accessible, unambiguous default constructor for the
982 // class type, unless the list item is also specified in a firstprivate
983 // clause.
984 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000985 for (auto *C : D->clauses()) {
986 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
987 SmallVector<Expr *, 8> PrivateCopies;
988 for (auto *DE : Clause->varlists()) {
989 if (DE->isValueDependent() || DE->isTypeDependent()) {
990 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000991 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000992 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000993 VarDecl *VD = nullptr;
994 FieldDecl *FD = nullptr;
995 ValueDecl *D;
Alexey Bataev74caaf22016-02-20 04:09:36 +0000996 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
997 if (auto *OCE = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) {
998 FD = cast<FieldDecl>(
999 cast<MemberExpr>(OCE->getInit()->IgnoreImpCasts())
1000 ->getMemberDecl());
1001 D = FD;
1002 } else {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001003 VD = cast<VarDecl>(DRE->getDecl());
1004 D = VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001005 }
1006 QualType Type = D->getType().getNonReferenceType();
1007 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001008 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001009 // Generate helper private variable and initialize it with the
1010 // default value. The address of the original variable is replaced
1011 // by the address of the new private variable in CodeGen. This new
1012 // variable is not added to IdResolver, so the code in the OpenMP
1013 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001014 auto *VDPrivate = buildVarDecl(
1015 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001016 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1018 if (VDPrivate->isInvalidDecl())
1019 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001020 PrivateCopies.push_back(buildDeclRefExpr(
1021 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001022 } else {
1023 // The variable is also a firstprivate, so initialization sequence
1024 // for private copy is generated already.
1025 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001026 }
1027 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001028 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001030 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001031 }
1032 }
1033 }
1034
Alexey Bataev758e55e2013-09-06 18:03:48 +00001035 DSAStack->pop();
1036 DiscardCleanupsInEvaluationContext();
1037 PopExpressionEvaluationContext();
1038}
1039
Alexander Musman3276a272015-03-21 10:12:56 +00001040static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1041 Expr *NumIterations, Sema &SemaRef,
1042 Scope *S);
1043
Alexey Bataeva769e072013-03-22 06:34:35 +00001044namespace {
1045
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046class VarDeclFilterCCC : public CorrectionCandidateCallback {
1047private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001048 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001049
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001051 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001052 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053 NamedDecl *ND = Candidate.getCorrectionDecl();
1054 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1055 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001056 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1057 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001058 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001060 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001061};
Alexey Bataeved09d242014-05-28 05:53:51 +00001062} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001063
1064ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1065 CXXScopeSpec &ScopeSpec,
1066 const DeclarationNameInfo &Id) {
1067 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1068 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1069
1070 if (Lookup.isAmbiguous())
1071 return ExprError();
1072
1073 VarDecl *VD;
1074 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001075 if (TypoCorrection Corrected = CorrectTypo(
1076 Id, LookupOrdinaryName, CurScope, nullptr,
1077 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001078 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001079 PDiag(Lookup.empty()
1080 ? diag::err_undeclared_var_use_suggest
1081 : diag::err_omp_expected_var_arg_suggest)
1082 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001083 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001085 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1086 : diag::err_omp_expected_var_arg)
1087 << Id.getName();
1088 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001090 } else {
1091 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001093 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1094 return ExprError();
1095 }
1096 }
1097 Lookup.suppressDiagnostics();
1098
1099 // OpenMP [2.9.2, Syntax, C/C++]
1100 // Variables must be file-scope, namespace-scope, or static block-scope.
1101 if (!VD->hasGlobalStorage()) {
1102 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1104 bool IsDecl =
1105 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001106 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001107 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1108 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001109 return ExprError();
1110 }
1111
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001112 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1113 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1115 // A threadprivate directive for file-scope variables must appear outside
1116 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001117 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1118 !getCurLexicalContext()->isTranslationUnit()) {
1119 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001120 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1121 bool IsDecl =
1122 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1123 Diag(VD->getLocation(),
1124 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1125 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001126 return ExprError();
1127 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001128 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1129 // A threadprivate directive for static class member variables must appear
1130 // in the class definition, in the same scope in which the member
1131 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001132 if (CanonicalVD->isStaticDataMember() &&
1133 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1134 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001135 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1136 bool IsDecl =
1137 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1138 Diag(VD->getLocation(),
1139 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1140 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001141 return ExprError();
1142 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001143 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1144 // A threadprivate directive for namespace-scope variables must appear
1145 // outside any definition or declaration other than the namespace
1146 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001147 if (CanonicalVD->getDeclContext()->isNamespace() &&
1148 (!getCurLexicalContext()->isFileContext() ||
1149 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1150 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001151 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1152 bool IsDecl =
1153 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1154 Diag(VD->getLocation(),
1155 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1156 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001157 return ExprError();
1158 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1160 // A threadprivate directive for static block-scope variables must appear
1161 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001162 if (CanonicalVD->isStaticLocal() && CurScope &&
1163 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001164 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001165 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1166 bool IsDecl =
1167 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1168 Diag(VD->getLocation(),
1169 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1170 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 return ExprError();
1172 }
1173
1174 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1175 // A threadprivate directive must lexically precede all references to any
1176 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001177 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 return ExprError();
1181 }
1182
1183 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001184 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1185 SourceLocation(), VD,
1186 /*RefersToEnclosingVariableOrCapture=*/false,
1187 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188}
1189
Alexey Bataeved09d242014-05-28 05:53:51 +00001190Sema::DeclGroupPtrTy
1191Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1192 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001194 CurContext->addDecl(D);
1195 return DeclGroupPtrTy::make(DeclGroupRef(D));
1196 }
David Blaikie0403cb12016-01-15 23:43:25 +00001197 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001198}
1199
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001200namespace {
1201class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1202 Sema &SemaRef;
1203
1204public:
1205 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1206 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1207 if (VD->hasLocalStorage()) {
1208 SemaRef.Diag(E->getLocStart(),
1209 diag::err_omp_local_var_in_threadprivate_init)
1210 << E->getSourceRange();
1211 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1212 << VD << VD->getSourceRange();
1213 return true;
1214 }
1215 }
1216 return false;
1217 }
1218 bool VisitStmt(const Stmt *S) {
1219 for (auto Child : S->children()) {
1220 if (Child && Visit(Child))
1221 return true;
1222 }
1223 return false;
1224 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001225 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001226};
1227} // namespace
1228
Alexey Bataeved09d242014-05-28 05:53:51 +00001229OMPThreadPrivateDecl *
1230Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001231 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001232 for (auto &RefExpr : VarList) {
1233 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001234 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1235 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001236
Alexey Bataev376b4a42016-02-09 09:41:09 +00001237 // Mark variable as used.
1238 VD->setReferenced();
1239 VD->markUsed(Context);
1240
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001241 QualType QType = VD->getType();
1242 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1243 // It will be analyzed later.
1244 Vars.push_back(DE);
1245 continue;
1246 }
1247
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1249 // A threadprivate variable must not have an incomplete type.
1250 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001252 continue;
1253 }
1254
1255 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1256 // A threadprivate variable must not have a reference type.
1257 if (VD->getType()->isReferenceType()) {
1258 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001259 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1260 bool IsDecl =
1261 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1262 Diag(VD->getLocation(),
1263 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1264 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001265 continue;
1266 }
1267
Samuel Antaof8b50122015-07-13 22:54:53 +00001268 // Check if this is a TLS variable. If TLS is not being supported, produce
1269 // the corresponding diagnostic.
1270 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1271 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1272 getLangOpts().OpenMPUseTLS &&
1273 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001274 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1275 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001276 Diag(ILoc, diag::err_omp_var_thread_local)
1277 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001278 bool IsDecl =
1279 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1280 Diag(VD->getLocation(),
1281 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1282 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001283 continue;
1284 }
1285
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001286 // Check if initial value of threadprivate variable reference variable with
1287 // local storage (it is not supported by runtime).
1288 if (auto Init = VD->getAnyInitializer()) {
1289 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001290 if (Checker.Visit(Init))
1291 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001292 }
1293
Alexey Bataeved09d242014-05-28 05:53:51 +00001294 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001295 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001296 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1297 Context, SourceRange(Loc, Loc)));
1298 if (auto *ML = Context.getASTMutationListener())
1299 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001300 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001301 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001302 if (!Vars.empty()) {
1303 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1304 Vars);
1305 D->setAccess(AS_public);
1306 }
1307 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001308}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001309
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001311 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001312 bool IsLoopIterVar = false) {
1313 if (DVar.RefExpr) {
1314 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1315 << getOpenMPClauseName(DVar.CKind);
1316 return;
1317 }
1318 enum {
1319 PDSA_StaticMemberShared,
1320 PDSA_StaticLocalVarShared,
1321 PDSA_LoopIterVarPrivate,
1322 PDSA_LoopIterVarLinear,
1323 PDSA_LoopIterVarLastprivate,
1324 PDSA_ConstVarShared,
1325 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001326 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001327 PDSA_LocalVarPrivate,
1328 PDSA_Implicit
1329 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001330 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001331 auto ReportLoc = D->getLocation();
1332 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001333 if (IsLoopIterVar) {
1334 if (DVar.CKind == OMPC_private)
1335 Reason = PDSA_LoopIterVarPrivate;
1336 else if (DVar.CKind == OMPC_lastprivate)
1337 Reason = PDSA_LoopIterVarLastprivate;
1338 else
1339 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001340 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1341 Reason = PDSA_TaskVarFirstprivate;
1342 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001343 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001344 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001345 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001346 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001347 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001348 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001349 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001350 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001351 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001352 ReportHint = true;
1353 Reason = PDSA_LocalVarPrivate;
1354 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001355 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001356 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001357 << Reason << ReportHint
1358 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1359 } else if (DVar.ImplicitDSALoc.isValid()) {
1360 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1361 << getOpenMPClauseName(DVar.CKind);
1362 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001363}
1364
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365namespace {
1366class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1367 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001368 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369 bool ErrorFound;
1370 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001371 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001372 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001373
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374public:
1375 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001378 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1379 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001380
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001381 auto DVar = Stack->getTopDSA(VD, false);
1382 // Check if the variable has explicit DSA set and stop analysis if it so.
1383 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001384
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001385 auto ELoc = E->getExprLoc();
1386 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001387 // The default(none) clause requires that each variable that is referenced
1388 // in the construct, and does not have a predetermined data-sharing
1389 // attribute, must have its data-sharing attribute explicitly determined
1390 // by being listed in a data-sharing attribute clause.
1391 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001392 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001393 VarsWithInheritedDSA.count(VD) == 0) {
1394 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001395 return;
1396 }
1397
1398 // OpenMP [2.9.3.6, Restrictions, p.2]
1399 // A list item that appears in a reduction clause of the innermost
1400 // enclosing worksharing or parallel construct may not be accessed in an
1401 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001402 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001403 [](OpenMPDirectiveKind K) -> bool {
1404 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001405 isOpenMPWorksharingDirective(K) ||
1406 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 },
1408 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001409 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1410 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1412 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001413 return;
1414 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001415
1416 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001417 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001418 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001419 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001420 }
1421 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001422 void VisitMemberExpr(MemberExpr *E) {
1423 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1424 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1425 auto DVar = Stack->getTopDSA(FD, false);
1426 // Check if the variable has explicit DSA set and stop analysis if it
1427 // so.
1428 if (DVar.RefExpr)
1429 return;
1430
1431 auto ELoc = E->getExprLoc();
1432 auto DKind = Stack->getCurrentDirective();
1433 // OpenMP [2.9.3.6, Restrictions, p.2]
1434 // A list item that appears in a reduction clause of the innermost
1435 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001436 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 DVar =
1438 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1439 [](OpenMPDirectiveKind K) -> bool {
1440 return isOpenMPParallelDirective(K) ||
1441 isOpenMPWorksharingDirective(K) ||
1442 isOpenMPTeamsDirective(K);
1443 },
1444 false);
1445 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1446 ErrorFound = true;
1447 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1448 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1449 return;
1450 }
1451
1452 // Define implicit data-sharing attributes for task.
1453 DVar = Stack->getImplicitDSA(FD, false);
1454 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1455 ImplicitFirstprivate.push_back(E);
1456 }
1457 }
1458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001460 for (auto *C : S->clauses()) {
1461 // Skip analysis of arguments of implicitly defined firstprivate clause
1462 // for task directives.
1463 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1464 for (auto *CC : C->children()) {
1465 if (CC)
1466 Visit(CC);
1467 }
1468 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469 }
1470 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001471 for (auto *C : S->children()) {
1472 if (C && !isa<OMPExecutableDirective>(C))
1473 Visit(C);
1474 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476
1477 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001478 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001479 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001480 return VarsWithInheritedDSA;
1481 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482
Alexey Bataev7ff55242014-06-19 09:13:45 +00001483 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1484 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001485};
Alexey Bataeved09d242014-05-28 05:53:51 +00001486} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001487
Alexey Bataevbae9a792014-06-27 10:37:06 +00001488void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001489 switch (DKind) {
1490 case OMPD_parallel: {
1491 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001492 QualType KmpInt32PtrTy =
1493 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001494 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001495 std::make_pair(".global_tid.", KmpInt32PtrTy),
1496 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1497 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1500 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 break;
1502 }
1503 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001504 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001509 break;
1510 }
1511 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001512 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001513 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001514 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1516 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001517 break;
1518 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001519 case OMPD_for_simd: {
1520 Sema::CapturedParamNameType Params[] = {
1521 std::make_pair(StringRef(), QualType()) // __context with shared vars
1522 };
1523 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1524 Params);
1525 break;
1526 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001527 case OMPD_sections: {
1528 Sema::CapturedParamNameType Params[] = {
1529 std::make_pair(StringRef(), QualType()) // __context with shared vars
1530 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001531 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1532 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001533 break;
1534 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001535 case OMPD_section: {
1536 Sema::CapturedParamNameType Params[] = {
1537 std::make_pair(StringRef(), QualType()) // __context with shared vars
1538 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001539 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1540 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001541 break;
1542 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001543 case OMPD_single: {
1544 Sema::CapturedParamNameType Params[] = {
1545 std::make_pair(StringRef(), QualType()) // __context with shared vars
1546 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001547 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1548 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001549 break;
1550 }
Alexander Musman80c22892014-07-17 08:54:58 +00001551 case OMPD_master: {
1552 Sema::CapturedParamNameType Params[] = {
1553 std::make_pair(StringRef(), QualType()) // __context with shared vars
1554 };
1555 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1556 Params);
1557 break;
1558 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001559 case OMPD_critical: {
1560 Sema::CapturedParamNameType Params[] = {
1561 std::make_pair(StringRef(), QualType()) // __context with shared vars
1562 };
1563 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1564 Params);
1565 break;
1566 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001567 case OMPD_parallel_for: {
1568 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001569 QualType KmpInt32PtrTy =
1570 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001571 Sema::CapturedParamNameType Params[] = {
1572 std::make_pair(".global_tid.", KmpInt32PtrTy),
1573 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1574 std::make_pair(StringRef(), QualType()) // __context with shared vars
1575 };
1576 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1577 Params);
1578 break;
1579 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001580 case OMPD_parallel_for_simd: {
1581 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001582 QualType KmpInt32PtrTy =
1583 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001584 Sema::CapturedParamNameType Params[] = {
1585 std::make_pair(".global_tid.", KmpInt32PtrTy),
1586 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1587 std::make_pair(StringRef(), QualType()) // __context with shared vars
1588 };
1589 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1590 Params);
1591 break;
1592 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001593 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001594 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001595 QualType KmpInt32PtrTy =
1596 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001597 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001598 std::make_pair(".global_tid.", KmpInt32PtrTy),
1599 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001600 std::make_pair(StringRef(), QualType()) // __context with shared vars
1601 };
1602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1603 Params);
1604 break;
1605 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001606 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001607 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001608 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1609 FunctionProtoType::ExtProtoInfo EPI;
1610 EPI.Variadic = true;
1611 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001612 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001613 std::make_pair(".global_tid.", KmpInt32Ty),
1614 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001615 std::make_pair(".privates.",
1616 Context.VoidPtrTy.withConst().withRestrict()),
1617 std::make_pair(
1618 ".copy_fn.",
1619 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 std::make_pair(StringRef(), QualType()) // __context with shared vars
1621 };
1622 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1623 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001624 // Mark this captured region as inlined, because we don't use outlined
1625 // function directly.
1626 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1627 AlwaysInlineAttr::CreateImplicit(
1628 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001629 break;
1630 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001631 case OMPD_ordered: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
1635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
1637 break;
1638 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001639 case OMPD_atomic: {
1640 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 }
Michael Wong65f367f2015-07-21 13:44:28 +00001647 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001648 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001649 case OMPD_target_parallel:
1650 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001651 Sema::CapturedParamNameType Params[] = {
1652 std::make_pair(StringRef(), QualType()) // __context with shared vars
1653 };
1654 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1655 Params);
1656 break;
1657 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001658 case OMPD_teams: {
1659 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001660 QualType KmpInt32PtrTy =
1661 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001662 Sema::CapturedParamNameType Params[] = {
1663 std::make_pair(".global_tid.", KmpInt32PtrTy),
1664 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1665 std::make_pair(StringRef(), QualType()) // __context with shared vars
1666 };
1667 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1668 Params);
1669 break;
1670 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001671 case OMPD_taskgroup: {
1672 Sema::CapturedParamNameType Params[] = {
1673 std::make_pair(StringRef(), QualType()) // __context with shared vars
1674 };
1675 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1676 Params);
1677 break;
1678 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001679 case OMPD_taskloop: {
1680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(StringRef(), QualType()) // __context with shared vars
1682 };
1683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1684 Params);
1685 break;
1686 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001687 case OMPD_taskloop_simd: {
1688 Sema::CapturedParamNameType Params[] = {
1689 std::make_pair(StringRef(), QualType()) // __context with shared vars
1690 };
1691 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1692 Params);
1693 break;
1694 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001695 case OMPD_distribute: {
1696 Sema::CapturedParamNameType Params[] = {
1697 std::make_pair(StringRef(), QualType()) // __context with shared vars
1698 };
1699 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1700 Params);
1701 break;
1702 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_taskyield:
1705 case OMPD_barrier:
1706 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001707 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001708 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001709 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001710 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001711 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 llvm_unreachable("OpenMP Directive is not allowed");
1713 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("Unknown OpenMP directive");
1715 }
1716}
1717
Alexey Bataev3392d762016-02-16 11:18:12 +00001718static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1719 Expr *CaptureExpr) {
Alexey Bataev4244be22016-02-11 05:35:55 +00001720 ASTContext &C = S.getASTContext();
1721 Expr *Init = CaptureExpr->IgnoreImpCasts();
1722 QualType Ty = Init->getType();
1723 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1724 if (S.getLangOpts().CPlusPlus)
1725 Ty = C.getLValueReferenceType(Ty);
1726 else {
1727 Ty = C.getPointerType(Ty);
1728 ExprResult Res =
1729 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1730 if (!Res.isUsable())
1731 return nullptr;
1732 Init = Res.get();
1733 }
1734 }
1735 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1736 S.CurContext->addHiddenDecl(CED);
1737 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1738 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001739 return CED;
1740}
1741
1742static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
1743 Expr *CaptureExpr) {
1744 auto *CD = buildCaptureDecl(S, Id, CaptureExpr);
1745 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1746 SourceLocation());
1747}
1748
1749static DeclRefExpr *buildCapture(Sema &S, StringRef Name, Expr *CaptureExpr) {
1750 auto *CD =
1751 buildCaptureDecl(S, &S.getASTContext().Idents.get(Name), CaptureExpr);
1752 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1753 SourceLocation());
Alexey Bataev4244be22016-02-11 05:35:55 +00001754}
1755
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001756StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1757 ArrayRef<OMPClause *> Clauses) {
1758 if (!S.isUsable()) {
1759 ActOnCapturedRegionError();
1760 return StmtError();
1761 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001762
1763 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001764 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001765 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001766 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001767 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001768 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001769 Clause->getClauseKind() == OMPC_copyprivate ||
1770 (getLangOpts().OpenMPUseTLS &&
1771 getASTContext().getTargetInfo().isTLSSupported() &&
1772 Clause->getClauseKind() == OMPC_copyin)) {
1773 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001774 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001775 for (auto *VarRef : Clause->children()) {
1776 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001777 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001778 }
1779 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001780 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001781 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001782 // Mark all variables in private list clauses as used in inner region.
1783 // Required for proper codegen of combined directives.
1784 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001785 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1786 if (auto *S = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1787 for (auto *D : S->decls())
1788 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1789 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001790 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001791 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001792 if (Clause->getClauseKind() == OMPC_schedule)
1793 SC = cast<OMPScheduleClause>(Clause);
1794 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001795 OC = cast<OMPOrderedClause>(Clause);
1796 else if (Clause->getClauseKind() == OMPC_linear)
1797 LCs.push_back(cast<OMPLinearClause>(Clause));
1798 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001799 bool ErrorFound = false;
1800 // OpenMP, 2.7.1 Loop Construct, Restrictions
1801 // The nonmonotonic modifier cannot be specified if an ordered clause is
1802 // specified.
1803 if (SC &&
1804 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1805 SC->getSecondScheduleModifier() ==
1806 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1807 OC) {
1808 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1809 ? SC->getFirstScheduleModifierLoc()
1810 : SC->getSecondScheduleModifierLoc(),
1811 diag::err_omp_schedule_nonmonotonic_ordered)
1812 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1813 ErrorFound = true;
1814 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001815 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1816 for (auto *C : LCs) {
1817 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1818 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1819 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001820 ErrorFound = true;
1821 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001822 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1823 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1824 OC->getNumForLoops()) {
1825 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1826 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1827 ErrorFound = true;
1828 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001829 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001830 ActOnCapturedRegionError();
1831 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001832 }
1833 return ActOnCapturedRegionEnd(S.get());
1834}
1835
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001836static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1837 OpenMPDirectiveKind CurrentRegion,
1838 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001839 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001840 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001841 // Allowed nesting of constructs
1842 // +------------------+-----------------+------------------------------------+
1843 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1844 // +------------------+-----------------+------------------------------------+
1845 // | parallel | parallel | * |
1846 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001847 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001848 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001849 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001850 // | parallel | simd | * |
1851 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001852 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001853 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001854 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001855 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001856 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001857 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001858 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001859 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001860 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001861 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001862 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001863 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001864 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001865 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001866 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001867 // | parallel | target parallel | * |
1868 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001869 // | parallel | target enter | * |
1870 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001871 // | parallel | target exit | * |
1872 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001873 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001874 // | parallel | cancellation | |
1875 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001876 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001877 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001878 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001879 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001880 // +------------------+-----------------+------------------------------------+
1881 // | for | parallel | * |
1882 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001883 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001884 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001885 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001886 // | for | simd | * |
1887 // | for | sections | + |
1888 // | for | section | + |
1889 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001890 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001891 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001892 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001893 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001894 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001895 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001896 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001897 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001898 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001899 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001900 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001901 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001902 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001903 // | for | target parallel | * |
1904 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001905 // | for | target enter | * |
1906 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001907 // | for | target exit | * |
1908 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001909 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001910 // | for | cancellation | |
1911 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001912 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001913 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001914 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001915 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001916 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001917 // | master | parallel | * |
1918 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001919 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001920 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001921 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001922 // | master | simd | * |
1923 // | master | sections | + |
1924 // | master | section | + |
1925 // | master | single | + |
1926 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001927 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001928 // | master |parallel sections| * |
1929 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001930 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001931 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001932 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001933 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001934 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001935 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001936 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001937 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001938 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001939 // | master | target parallel | * |
1940 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001941 // | master | target enter | * |
1942 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001943 // | master | target exit | * |
1944 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001945 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001946 // | master | cancellation | |
1947 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001948 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001949 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001950 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001951 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001952 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001953 // | critical | parallel | * |
1954 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001955 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001956 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001957 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001958 // | critical | simd | * |
1959 // | critical | sections | + |
1960 // | critical | section | + |
1961 // | critical | single | + |
1962 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001963 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001964 // | critical |parallel sections| * |
1965 // | critical | task | * |
1966 // | critical | taskyield | * |
1967 // | critical | barrier | + |
1968 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001969 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001970 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001971 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001972 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001973 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001974 // | critical | target parallel | * |
1975 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001976 // | critical | target enter | * |
1977 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001978 // | critical | target exit | * |
1979 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001980 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001981 // | critical | cancellation | |
1982 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001983 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001984 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001985 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001986 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001987 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001988 // | simd | parallel | |
1989 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001990 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001991 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001992 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001993 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001994 // | simd | sections | |
1995 // | simd | section | |
1996 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001997 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001998 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001999 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002000 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002001 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002002 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002003 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002004 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002005 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002006 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002007 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002008 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002009 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002010 // | simd | target parallel | |
2011 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002012 // | simd | target enter | |
2013 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002014 // | simd | target exit | |
2015 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002016 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002017 // | simd | cancellation | |
2018 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002019 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002020 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002021 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002022 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002023 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002024 // | for simd | parallel | |
2025 // | for simd | for | |
2026 // | for simd | for simd | |
2027 // | for simd | master | |
2028 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002029 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002030 // | for simd | sections | |
2031 // | for simd | section | |
2032 // | for simd | single | |
2033 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002034 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002035 // | for simd |parallel sections| |
2036 // | for simd | task | |
2037 // | for simd | taskyield | |
2038 // | for simd | barrier | |
2039 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002040 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002041 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002042 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002043 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002044 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002045 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002046 // | for simd | target parallel | |
2047 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002048 // | for simd | target enter | |
2049 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002050 // | for simd | target exit | |
2051 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002052 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002053 // | for simd | cancellation | |
2054 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002055 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002056 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002057 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002058 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002059 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002060 // | parallel for simd| parallel | |
2061 // | parallel for simd| for | |
2062 // | parallel for simd| for simd | |
2063 // | parallel for simd| master | |
2064 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002065 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002066 // | parallel for simd| sections | |
2067 // | parallel for simd| section | |
2068 // | parallel for simd| single | |
2069 // | parallel for simd| parallel for | |
2070 // | parallel for simd|parallel for simd| |
2071 // | parallel for simd|parallel sections| |
2072 // | parallel for simd| task | |
2073 // | parallel for simd| taskyield | |
2074 // | parallel for simd| barrier | |
2075 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002076 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002077 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002078 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002079 // | parallel for simd| atomic | |
2080 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002081 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002082 // | parallel for simd| target parallel | |
2083 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002084 // | parallel for simd| target enter | |
2085 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002086 // | parallel for simd| target exit | |
2087 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002088 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002089 // | parallel for simd| cancellation | |
2090 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002091 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002092 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002093 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002094 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002095 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002096 // | sections | parallel | * |
2097 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002098 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002099 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002100 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002101 // | sections | simd | * |
2102 // | sections | sections | + |
2103 // | sections | section | * |
2104 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002105 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002106 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002107 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002108 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002109 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002110 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002111 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002112 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002113 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002114 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002115 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002116 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002117 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002118 // | sections | target parallel | * |
2119 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002120 // | sections | target enter | * |
2121 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002122 // | sections | target exit | * |
2123 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002124 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002125 // | sections | cancellation | |
2126 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002127 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002128 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002129 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002130 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002131 // +------------------+-----------------+------------------------------------+
2132 // | section | parallel | * |
2133 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002134 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002135 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002136 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002137 // | section | simd | * |
2138 // | section | sections | + |
2139 // | section | section | + |
2140 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002141 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002142 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002143 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002144 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002145 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002146 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002147 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002148 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002149 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002150 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002151 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002152 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002153 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002154 // | section | target parallel | * |
2155 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002156 // | section | target enter | * |
2157 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002158 // | section | target exit | * |
2159 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002160 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002161 // | section | cancellation | |
2162 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002163 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002164 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002165 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002166 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002167 // +------------------+-----------------+------------------------------------+
2168 // | single | parallel | * |
2169 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002170 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002171 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002172 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002173 // | single | simd | * |
2174 // | single | sections | + |
2175 // | single | section | + |
2176 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002177 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002178 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002179 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002180 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002181 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002182 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002183 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002184 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002185 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002186 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002187 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002188 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002189 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002190 // | single | target parallel | * |
2191 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002192 // | single | target enter | * |
2193 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002194 // | single | target exit | * |
2195 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002196 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002197 // | single | cancellation | |
2198 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002199 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002200 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002201 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002202 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002203 // +------------------+-----------------+------------------------------------+
2204 // | parallel for | parallel | * |
2205 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002206 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002207 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002208 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002209 // | parallel for | simd | * |
2210 // | parallel for | sections | + |
2211 // | parallel for | section | + |
2212 // | parallel for | single | + |
2213 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002214 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002215 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002216 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002217 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002218 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002219 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002220 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002221 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002222 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002223 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002224 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002225 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002226 // | parallel for | target parallel | * |
2227 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002228 // | parallel for | target enter | * |
2229 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002230 // | parallel for | target exit | * |
2231 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002232 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002233 // | parallel for | cancellation | |
2234 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002235 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002236 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002237 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002238 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002239 // +------------------+-----------------+------------------------------------+
2240 // | parallel sections| parallel | * |
2241 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002242 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002243 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002244 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002245 // | parallel sections| simd | * |
2246 // | parallel sections| sections | + |
2247 // | parallel sections| section | * |
2248 // | parallel sections| single | + |
2249 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002250 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002251 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002252 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002253 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002254 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002255 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002256 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002257 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002258 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002259 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002260 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002261 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002262 // | parallel sections| target parallel | * |
2263 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002264 // | parallel sections| target enter | * |
2265 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002266 // | parallel sections| target exit | * |
2267 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002268 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002269 // | parallel sections| cancellation | |
2270 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002271 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002272 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002273 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002274 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002275 // +------------------+-----------------+------------------------------------+
2276 // | task | parallel | * |
2277 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002278 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002279 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002280 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002281 // | task | simd | * |
2282 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002283 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002284 // | task | single | + |
2285 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002286 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002287 // | task |parallel sections| * |
2288 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002289 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002290 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002291 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002292 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002293 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002294 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002295 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002296 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002297 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002298 // | task | target parallel | * |
2299 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002300 // | task | target enter | * |
2301 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002302 // | task | target exit | * |
2303 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002304 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002305 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002306 // | | point | ! |
2307 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002308 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002309 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002310 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002311 // +------------------+-----------------+------------------------------------+
2312 // | ordered | parallel | * |
2313 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002314 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002315 // | ordered | master | * |
2316 // | ordered | critical | * |
2317 // | ordered | simd | * |
2318 // | ordered | sections | + |
2319 // | ordered | section | + |
2320 // | ordered | single | + |
2321 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002322 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002323 // | ordered |parallel sections| * |
2324 // | ordered | task | * |
2325 // | ordered | taskyield | * |
2326 // | ordered | barrier | + |
2327 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002328 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002329 // | ordered | flush | * |
2330 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002331 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002332 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002333 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002334 // | ordered | target parallel | * |
2335 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002336 // | ordered | target enter | * |
2337 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002338 // | ordered | target exit | * |
2339 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002340 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002341 // | ordered | cancellation | |
2342 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002343 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002344 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002345 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002346 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002347 // +------------------+-----------------+------------------------------------+
2348 // | atomic | parallel | |
2349 // | atomic | for | |
2350 // | atomic | for simd | |
2351 // | atomic | master | |
2352 // | atomic | critical | |
2353 // | atomic | simd | |
2354 // | atomic | sections | |
2355 // | atomic | section | |
2356 // | atomic | single | |
2357 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002358 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002359 // | atomic |parallel sections| |
2360 // | atomic | task | |
2361 // | atomic | taskyield | |
2362 // | atomic | barrier | |
2363 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002364 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002365 // | atomic | flush | |
2366 // | atomic | ordered | |
2367 // | atomic | atomic | |
2368 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002369 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002370 // | atomic | target parallel | |
2371 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002372 // | atomic | target enter | |
2373 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002374 // | atomic | target exit | |
2375 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002376 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002377 // | atomic | cancellation | |
2378 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002379 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002380 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002381 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002382 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002383 // +------------------+-----------------+------------------------------------+
2384 // | target | parallel | * |
2385 // | target | for | * |
2386 // | target | for simd | * |
2387 // | target | master | * |
2388 // | target | critical | * |
2389 // | target | simd | * |
2390 // | target | sections | * |
2391 // | target | section | * |
2392 // | target | single | * |
2393 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002394 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002395 // | target |parallel sections| * |
2396 // | target | task | * |
2397 // | target | taskyield | * |
2398 // | target | barrier | * |
2399 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002400 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002401 // | target | flush | * |
2402 // | target | ordered | * |
2403 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002404 // | target | target | |
2405 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002406 // | target | target parallel | |
2407 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002408 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002409 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002410 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002411 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002412 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002413 // | target | cancellation | |
2414 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002415 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002416 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002417 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002418 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002419 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002420 // | target parallel | parallel | * |
2421 // | target parallel | for | * |
2422 // | target parallel | for simd | * |
2423 // | target parallel | master | * |
2424 // | target parallel | critical | * |
2425 // | target parallel | simd | * |
2426 // | target parallel | sections | * |
2427 // | target parallel | section | * |
2428 // | target parallel | single | * |
2429 // | target parallel | parallel for | * |
2430 // | target parallel |parallel for simd| * |
2431 // | target parallel |parallel sections| * |
2432 // | target parallel | task | * |
2433 // | target parallel | taskyield | * |
2434 // | target parallel | barrier | * |
2435 // | target parallel | taskwait | * |
2436 // | target parallel | taskgroup | * |
2437 // | target parallel | flush | * |
2438 // | target parallel | ordered | * |
2439 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002440 // | target parallel | target | |
2441 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002442 // | target parallel | target parallel | |
2443 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002444 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002445 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002446 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002447 // | | data | |
2448 // | target parallel | teams | |
2449 // | target parallel | cancellation | |
2450 // | | point | ! |
2451 // | target parallel | cancel | ! |
2452 // | target parallel | taskloop | * |
2453 // | target parallel | taskloop simd | * |
2454 // | target parallel | distribute | |
2455 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002456 // | target parallel | parallel | * |
2457 // | for | | |
2458 // | target parallel | for | * |
2459 // | for | | |
2460 // | target parallel | for simd | * |
2461 // | for | | |
2462 // | target parallel | master | * |
2463 // | for | | |
2464 // | target parallel | critical | * |
2465 // | for | | |
2466 // | target parallel | simd | * |
2467 // | for | | |
2468 // | target parallel | sections | * |
2469 // | for | | |
2470 // | target parallel | section | * |
2471 // | for | | |
2472 // | target parallel | single | * |
2473 // | for | | |
2474 // | target parallel | parallel for | * |
2475 // | for | | |
2476 // | target parallel |parallel for simd| * |
2477 // | for | | |
2478 // | target parallel |parallel sections| * |
2479 // | for | | |
2480 // | target parallel | task | * |
2481 // | for | | |
2482 // | target parallel | taskyield | * |
2483 // | for | | |
2484 // | target parallel | barrier | * |
2485 // | for | | |
2486 // | target parallel | taskwait | * |
2487 // | for | | |
2488 // | target parallel | taskgroup | * |
2489 // | for | | |
2490 // | target parallel | flush | * |
2491 // | for | | |
2492 // | target parallel | ordered | * |
2493 // | for | | |
2494 // | target parallel | atomic | * |
2495 // | for | | |
2496 // | target parallel | target | |
2497 // | for | | |
2498 // | target parallel | target parallel | |
2499 // | for | | |
2500 // | target parallel | target parallel | |
2501 // | for | for | |
2502 // | target parallel | target enter | |
2503 // | for | data | |
2504 // | target parallel | target exit | |
2505 // | for | data | |
2506 // | target parallel | teams | |
2507 // | for | | |
2508 // | target parallel | cancellation | |
2509 // | for | point | ! |
2510 // | target parallel | cancel | ! |
2511 // | for | | |
2512 // | target parallel | taskloop | * |
2513 // | for | | |
2514 // | target parallel | taskloop simd | * |
2515 // | for | | |
2516 // | target parallel | distribute | |
2517 // | for | | |
2518 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002519 // | teams | parallel | * |
2520 // | teams | for | + |
2521 // | teams | for simd | + |
2522 // | teams | master | + |
2523 // | teams | critical | + |
2524 // | teams | simd | + |
2525 // | teams | sections | + |
2526 // | teams | section | + |
2527 // | teams | single | + |
2528 // | teams | parallel for | * |
2529 // | teams |parallel for simd| * |
2530 // | teams |parallel sections| * |
2531 // | teams | task | + |
2532 // | teams | taskyield | + |
2533 // | teams | barrier | + |
2534 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002535 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002536 // | teams | flush | + |
2537 // | teams | ordered | + |
2538 // | teams | atomic | + |
2539 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002540 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002541 // | teams | target parallel | + |
2542 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002543 // | teams | target enter | + |
2544 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002545 // | teams | target exit | + |
2546 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002547 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002548 // | teams | cancellation | |
2549 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002550 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002551 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002552 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002553 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002554 // +------------------+-----------------+------------------------------------+
2555 // | taskloop | parallel | * |
2556 // | taskloop | for | + |
2557 // | taskloop | for simd | + |
2558 // | taskloop | master | + |
2559 // | taskloop | critical | * |
2560 // | taskloop | simd | * |
2561 // | taskloop | sections | + |
2562 // | taskloop | section | + |
2563 // | taskloop | single | + |
2564 // | taskloop | parallel for | * |
2565 // | taskloop |parallel for simd| * |
2566 // | taskloop |parallel sections| * |
2567 // | taskloop | task | * |
2568 // | taskloop | taskyield | * |
2569 // | taskloop | barrier | + |
2570 // | taskloop | taskwait | * |
2571 // | taskloop | taskgroup | * |
2572 // | taskloop | flush | * |
2573 // | taskloop | ordered | + |
2574 // | taskloop | atomic | * |
2575 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002576 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002577 // | taskloop | target parallel | * |
2578 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002579 // | taskloop | target enter | * |
2580 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002581 // | taskloop | target exit | * |
2582 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002583 // | taskloop | teams | + |
2584 // | taskloop | cancellation | |
2585 // | | point | |
2586 // | taskloop | cancel | |
2587 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002588 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002589 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002590 // | taskloop simd | parallel | |
2591 // | taskloop simd | for | |
2592 // | taskloop simd | for simd | |
2593 // | taskloop simd | master | |
2594 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002595 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002596 // | taskloop simd | sections | |
2597 // | taskloop simd | section | |
2598 // | taskloop simd | single | |
2599 // | taskloop simd | parallel for | |
2600 // | taskloop simd |parallel for simd| |
2601 // | taskloop simd |parallel sections| |
2602 // | taskloop simd | task | |
2603 // | taskloop simd | taskyield | |
2604 // | taskloop simd | barrier | |
2605 // | taskloop simd | taskwait | |
2606 // | taskloop simd | taskgroup | |
2607 // | taskloop simd | flush | |
2608 // | taskloop simd | ordered | + (with simd clause) |
2609 // | taskloop simd | atomic | |
2610 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002611 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002612 // | taskloop simd | target parallel | |
2613 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002614 // | taskloop simd | target enter | |
2615 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002616 // | taskloop simd | target exit | |
2617 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002618 // | taskloop simd | teams | |
2619 // | taskloop simd | cancellation | |
2620 // | | point | |
2621 // | taskloop simd | cancel | |
2622 // | taskloop simd | taskloop | |
2623 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002624 // | taskloop simd | distribute | |
2625 // +------------------+-----------------+------------------------------------+
2626 // | distribute | parallel | * |
2627 // | distribute | for | * |
2628 // | distribute | for simd | * |
2629 // | distribute | master | * |
2630 // | distribute | critical | * |
2631 // | distribute | simd | * |
2632 // | distribute | sections | * |
2633 // | distribute | section | * |
2634 // | distribute | single | * |
2635 // | distribute | parallel for | * |
2636 // | distribute |parallel for simd| * |
2637 // | distribute |parallel sections| * |
2638 // | distribute | task | * |
2639 // | distribute | taskyield | * |
2640 // | distribute | barrier | * |
2641 // | distribute | taskwait | * |
2642 // | distribute | taskgroup | * |
2643 // | distribute | flush | * |
2644 // | distribute | ordered | + |
2645 // | distribute | atomic | * |
2646 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002647 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002648 // | distribute | target parallel | |
2649 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002650 // | distribute | target enter | |
2651 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002652 // | distribute | target exit | |
2653 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002654 // | distribute | teams | |
2655 // | distribute | cancellation | + |
2656 // | | point | |
2657 // | distribute | cancel | + |
2658 // | distribute | taskloop | * |
2659 // | distribute | taskloop simd | * |
2660 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002661 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002662 if (Stack->getCurScope()) {
2663 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002664 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002665 bool NestingProhibited = false;
2666 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002667 enum {
2668 NoRecommend,
2669 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002670 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002671 ShouldBeInTargetRegion,
2672 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002673 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002674 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2675 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002676 // OpenMP [2.16, Nesting of Regions]
2677 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002678 // OpenMP [2.8.1,simd Construct, Restrictions]
2679 // An ordered construct with the simd clause is the only OpenMP construct
2680 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002681 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2682 return true;
2683 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002684 if (ParentRegion == OMPD_atomic) {
2685 // OpenMP [2.16, Nesting of Regions]
2686 // OpenMP constructs may not be nested inside an atomic region.
2687 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2688 return true;
2689 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002690 if (CurrentRegion == OMPD_section) {
2691 // OpenMP [2.7.2, sections Construct, Restrictions]
2692 // Orphaned section directives are prohibited. That is, the section
2693 // directives must appear within the sections construct and must not be
2694 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002695 if (ParentRegion != OMPD_sections &&
2696 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002697 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2698 << (ParentRegion != OMPD_unknown)
2699 << getOpenMPDirectiveName(ParentRegion);
2700 return true;
2701 }
2702 return false;
2703 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002704 // Allow some constructs to be orphaned (they could be used in functions,
2705 // called from OpenMP regions with the required preconditions).
2706 if (ParentRegion == OMPD_unknown)
2707 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002708 if (CurrentRegion == OMPD_cancellation_point ||
2709 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002710 // OpenMP [2.16, Nesting of Regions]
2711 // A cancellation point construct for which construct-type-clause is
2712 // taskgroup must be nested inside a task construct. A cancellation
2713 // point construct for which construct-type-clause is not taskgroup must
2714 // be closely nested inside an OpenMP construct that matches the type
2715 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002716 // A cancel construct for which construct-type-clause is taskgroup must be
2717 // nested inside a task construct. A cancel construct for which
2718 // construct-type-clause is not taskgroup must be closely nested inside an
2719 // OpenMP construct that matches the type specified in
2720 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002721 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002722 !((CancelRegion == OMPD_parallel &&
2723 (ParentRegion == OMPD_parallel ||
2724 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002725 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002726 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2727 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002728 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2729 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002730 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2731 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002732 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002733 // OpenMP [2.16, Nesting of Regions]
2734 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002735 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002736 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002737 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002738 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002739 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2740 // OpenMP [2.16, Nesting of Regions]
2741 // A critical region may not be nested (closely or otherwise) inside a
2742 // critical region with the same name. Note that this restriction is not
2743 // sufficient to prevent deadlock.
2744 SourceLocation PreviousCriticalLoc;
2745 bool DeadLock =
2746 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2747 OpenMPDirectiveKind K,
2748 const DeclarationNameInfo &DNI,
2749 SourceLocation Loc)
2750 ->bool {
2751 if (K == OMPD_critical &&
2752 DNI.getName() == CurrentName.getName()) {
2753 PreviousCriticalLoc = Loc;
2754 return true;
2755 } else
2756 return false;
2757 },
2758 false /* skip top directive */);
2759 if (DeadLock) {
2760 SemaRef.Diag(StartLoc,
2761 diag::err_omp_prohibited_region_critical_same_name)
2762 << CurrentName.getName();
2763 if (PreviousCriticalLoc.isValid())
2764 SemaRef.Diag(PreviousCriticalLoc,
2765 diag::note_omp_previous_critical_region);
2766 return true;
2767 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002768 } else if (CurrentRegion == OMPD_barrier) {
2769 // OpenMP [2.16, Nesting of Regions]
2770 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002771 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002772 NestingProhibited =
2773 isOpenMPWorksharingDirective(ParentRegion) ||
2774 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002775 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002776 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002777 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002778 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002779 // OpenMP [2.16, Nesting of Regions]
2780 // A worksharing region may not be closely nested inside a worksharing,
2781 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002782 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002783 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002784 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002785 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002786 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002787 Recommend = ShouldBeInParallelRegion;
2788 } else if (CurrentRegion == OMPD_ordered) {
2789 // OpenMP [2.16, Nesting of Regions]
2790 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002791 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002792 // An ordered region must be closely nested inside a loop region (or
2793 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002794 // OpenMP [2.8.1,simd Construct, Restrictions]
2795 // An ordered construct with the simd clause is the only OpenMP construct
2796 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002797 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002798 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002799 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002800 !(isOpenMPSimdDirective(ParentRegion) ||
2801 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002802 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002803 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2804 // OpenMP [2.16, Nesting of Regions]
2805 // If specified, a teams construct must be contained within a target
2806 // construct.
2807 NestingProhibited = ParentRegion != OMPD_target;
2808 Recommend = ShouldBeInTargetRegion;
2809 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2810 }
2811 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2812 // OpenMP [2.16, Nesting of Regions]
2813 // distribute, parallel, parallel sections, parallel workshare, and the
2814 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2815 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002816 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2817 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002818 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002819 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002820 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2821 // OpenMP 4.5 [2.17 Nesting of Regions]
2822 // The region associated with the distribute construct must be strictly
2823 // nested inside a teams region
2824 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2825 Recommend = ShouldBeInTeamsRegion;
2826 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002827 if (!NestingProhibited &&
2828 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2829 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2830 // OpenMP 4.5 [2.17 Nesting of Regions]
2831 // If a target, target update, target data, target enter data, or
2832 // target exit data construct is encountered during execution of a
2833 // target region, the behavior is unspecified.
2834 NestingProhibited = Stack->hasDirective(
2835 [&OffendingRegion](OpenMPDirectiveKind K,
2836 const DeclarationNameInfo &DNI,
2837 SourceLocation Loc) -> bool {
2838 if (isOpenMPTargetExecutionDirective(K)) {
2839 OffendingRegion = K;
2840 return true;
2841 } else
2842 return false;
2843 },
2844 false /* don't skip top directive */);
2845 CloseNesting = false;
2846 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002847 if (NestingProhibited) {
2848 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002849 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2850 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002851 return true;
2852 }
2853 }
2854 return false;
2855}
2856
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002857static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2858 ArrayRef<OMPClause *> Clauses,
2859 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2860 bool ErrorFound = false;
2861 unsigned NamedModifiersNumber = 0;
2862 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2863 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002864 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002865 for (const auto *C : Clauses) {
2866 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2867 // At most one if clause without a directive-name-modifier can appear on
2868 // the directive.
2869 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2870 if (FoundNameModifiers[CurNM]) {
2871 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2872 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2873 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2874 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002875 } else if (CurNM != OMPD_unknown) {
2876 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002877 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002878 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002879 FoundNameModifiers[CurNM] = IC;
2880 if (CurNM == OMPD_unknown)
2881 continue;
2882 // Check if the specified name modifier is allowed for the current
2883 // directive.
2884 // At most one if clause with the particular directive-name-modifier can
2885 // appear on the directive.
2886 bool MatchFound = false;
2887 for (auto NM : AllowedNameModifiers) {
2888 if (CurNM == NM) {
2889 MatchFound = true;
2890 break;
2891 }
2892 }
2893 if (!MatchFound) {
2894 S.Diag(IC->getNameModifierLoc(),
2895 diag::err_omp_wrong_if_directive_name_modifier)
2896 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2897 ErrorFound = true;
2898 }
2899 }
2900 }
2901 // If any if clause on the directive includes a directive-name-modifier then
2902 // all if clauses on the directive must include a directive-name-modifier.
2903 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2904 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2905 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2906 diag::err_omp_no_more_if_clause);
2907 } else {
2908 std::string Values;
2909 std::string Sep(", ");
2910 unsigned AllowedCnt = 0;
2911 unsigned TotalAllowedNum =
2912 AllowedNameModifiers.size() - NamedModifiersNumber;
2913 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2914 ++Cnt) {
2915 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2916 if (!FoundNameModifiers[NM]) {
2917 Values += "'";
2918 Values += getOpenMPDirectiveName(NM);
2919 Values += "'";
2920 if (AllowedCnt + 2 == TotalAllowedNum)
2921 Values += " or ";
2922 else if (AllowedCnt + 1 != TotalAllowedNum)
2923 Values += Sep;
2924 ++AllowedCnt;
2925 }
2926 }
2927 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2928 diag::err_omp_unnamed_if_clause)
2929 << (TotalAllowedNum > 1) << Values;
2930 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002931 for (auto Loc : NameModifierLoc) {
2932 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2933 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002934 ErrorFound = true;
2935 }
2936 return ErrorFound;
2937}
2938
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002939StmtResult Sema::ActOnOpenMPExecutableDirective(
2940 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2941 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2942 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002943 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002944 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2945 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002946 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002947
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002948 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002949 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002950 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002951 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002952 if (AStmt) {
2953 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2954
2955 // Check default data sharing attributes for referenced variables.
2956 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2957 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2958 if (DSAChecker.isErrorFound())
2959 return StmtError();
2960 // Generate list of implicitly defined firstprivate variables.
2961 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002962
2963 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2964 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2965 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2966 SourceLocation(), SourceLocation())) {
2967 ClausesWithImplicit.push_back(Implicit);
2968 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2969 DSAChecker.getImplicitFirstprivate().size();
2970 } else
2971 ErrorFound = true;
2972 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002973 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002974
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002975 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002976 switch (Kind) {
2977 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002978 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2979 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002980 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002981 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002982 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002983 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2984 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002985 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002986 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002987 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2988 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002989 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002990 case OMPD_for_simd:
2991 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2992 EndLoc, VarsWithInheritedDSA);
2993 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002994 case OMPD_sections:
2995 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2996 EndLoc);
2997 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002998 case OMPD_section:
2999 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003000 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003001 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3002 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003003 case OMPD_single:
3004 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3005 EndLoc);
3006 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003007 case OMPD_master:
3008 assert(ClausesWithImplicit.empty() &&
3009 "No clauses are allowed for 'omp master' directive");
3010 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3011 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003012 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003013 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3014 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003015 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003016 case OMPD_parallel_for:
3017 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3018 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003019 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003020 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003021 case OMPD_parallel_for_simd:
3022 Res = ActOnOpenMPParallelForSimdDirective(
3023 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003024 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003025 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003026 case OMPD_parallel_sections:
3027 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3028 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003029 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003030 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003031 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003032 Res =
3033 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003034 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003035 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003036 case OMPD_taskyield:
3037 assert(ClausesWithImplicit.empty() &&
3038 "No clauses are allowed for 'omp taskyield' directive");
3039 assert(AStmt == nullptr &&
3040 "No associated statement allowed for 'omp taskyield' directive");
3041 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3042 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003043 case OMPD_barrier:
3044 assert(ClausesWithImplicit.empty() &&
3045 "No clauses are allowed for 'omp barrier' directive");
3046 assert(AStmt == nullptr &&
3047 "No associated statement allowed for 'omp barrier' directive");
3048 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3049 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003050 case OMPD_taskwait:
3051 assert(ClausesWithImplicit.empty() &&
3052 "No clauses are allowed for 'omp taskwait' directive");
3053 assert(AStmt == nullptr &&
3054 "No associated statement allowed for 'omp taskwait' directive");
3055 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3056 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003057 case OMPD_taskgroup:
3058 assert(ClausesWithImplicit.empty() &&
3059 "No clauses are allowed for 'omp taskgroup' directive");
3060 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3061 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003062 case OMPD_flush:
3063 assert(AStmt == nullptr &&
3064 "No associated statement allowed for 'omp flush' directive");
3065 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3066 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003067 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003068 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3069 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003070 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003071 case OMPD_atomic:
3072 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3073 EndLoc);
3074 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003075 case OMPD_teams:
3076 Res =
3077 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3078 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003079 case OMPD_target:
3080 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3081 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003082 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003083 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003084 case OMPD_target_parallel:
3085 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3086 StartLoc, EndLoc);
3087 AllowedNameModifiers.push_back(OMPD_target);
3088 AllowedNameModifiers.push_back(OMPD_parallel);
3089 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003090 case OMPD_target_parallel_for:
3091 Res = ActOnOpenMPTargetParallelForDirective(
3092 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3093 AllowedNameModifiers.push_back(OMPD_target);
3094 AllowedNameModifiers.push_back(OMPD_parallel);
3095 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003096 case OMPD_cancellation_point:
3097 assert(ClausesWithImplicit.empty() &&
3098 "No clauses are allowed for 'omp cancellation point' directive");
3099 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3100 "cancellation point' directive");
3101 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3102 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003103 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003104 assert(AStmt == nullptr &&
3105 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003106 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3107 CancelRegion);
3108 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003109 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003110 case OMPD_target_data:
3111 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3112 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003113 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003114 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003115 case OMPD_target_enter_data:
3116 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3117 EndLoc);
3118 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3119 break;
Samuel Antao72590762016-01-19 20:04:50 +00003120 case OMPD_target_exit_data:
3121 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3122 EndLoc);
3123 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3124 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003125 case OMPD_taskloop:
3126 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3127 EndLoc, VarsWithInheritedDSA);
3128 AllowedNameModifiers.push_back(OMPD_taskloop);
3129 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003130 case OMPD_taskloop_simd:
3131 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3132 EndLoc, VarsWithInheritedDSA);
3133 AllowedNameModifiers.push_back(OMPD_taskloop);
3134 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003135 case OMPD_distribute:
3136 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3137 EndLoc, VarsWithInheritedDSA);
3138 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003139 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003140 llvm_unreachable("OpenMP Directive is not allowed");
3141 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003142 llvm_unreachable("Unknown OpenMP directive");
3143 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003144
Alexey Bataev4acb8592014-07-07 13:01:15 +00003145 for (auto P : VarsWithInheritedDSA) {
3146 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3147 << P.first << P.second->getSourceRange();
3148 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003149 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3150
3151 if (!AllowedNameModifiers.empty())
3152 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3153 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003154
Alexey Bataeved09d242014-05-28 05:53:51 +00003155 if (ErrorFound)
3156 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003157 return Res;
3158}
3159
3160StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3161 Stmt *AStmt,
3162 SourceLocation StartLoc,
3163 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003164 if (!AStmt)
3165 return StmtError();
3166
Alexey Bataev9959db52014-05-06 10:08:46 +00003167 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3168 // 1.2.2 OpenMP Language Terminology
3169 // Structured block - An executable statement with a single entry at the
3170 // top and a single exit at the bottom.
3171 // The point of exit cannot be a branch out of the structured block.
3172 // longjmp() and throw() must not violate the entry/exit criteria.
3173 CS->getCapturedDecl()->setNothrow();
3174
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003175 getCurFunction()->setHasBranchProtectedScope();
3176
Alexey Bataev25e5b442015-09-15 12:52:43 +00003177 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3178 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003179}
3180
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003181namespace {
3182/// \brief Helper class for checking canonical form of the OpenMP loops and
3183/// extracting iteration space of each loop in the loop nest, that will be used
3184/// for IR generation.
3185class OpenMPIterationSpaceChecker {
3186 /// \brief Reference to Sema.
3187 Sema &SemaRef;
3188 /// \brief A location for diagnostics (when there is no some better location).
3189 SourceLocation DefaultLoc;
3190 /// \brief A location for diagnostics (when increment is not compatible).
3191 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003192 /// \brief A source location for referring to loop init later.
3193 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003194 /// \brief A source location for referring to condition later.
3195 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003196 /// \brief A source location for referring to increment later.
3197 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003198 /// \brief Loop variable.
3199 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003200 /// \brief Reference to loop variable.
3201 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003202 /// \brief Lower bound (initializer for the var).
3203 Expr *LB;
3204 /// \brief Upper bound.
3205 Expr *UB;
3206 /// \brief Loop step (increment).
3207 Expr *Step;
3208 /// \brief This flag is true when condition is one of:
3209 /// Var < UB
3210 /// Var <= UB
3211 /// UB > Var
3212 /// UB >= Var
3213 bool TestIsLessOp;
3214 /// \brief This flag is true when condition is strict ( < or > ).
3215 bool TestIsStrictOp;
3216 /// \brief This flag is true when step is subtracted on each iteration.
3217 bool SubtractStep;
3218
3219public:
3220 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3221 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003222 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3223 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003224 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3225 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003226 /// \brief Check init-expr for canonical loop form and save loop counter
3227 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003228 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003229 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3230 /// for less/greater and for strict/non-strict comparison.
3231 bool CheckCond(Expr *S);
3232 /// \brief Check incr-expr for canonical loop form and return true if it
3233 /// does not conform, otherwise save loop step (#Step).
3234 bool CheckInc(Expr *S);
3235 /// \brief Return the loop counter variable.
3236 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003237 /// \brief Return the reference expression to loop counter variable.
3238 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003239 /// \brief Source range of the loop init.
3240 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3241 /// \brief Source range of the loop condition.
3242 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3243 /// \brief Source range of the loop increment.
3244 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3245 /// \brief True if the step should be subtracted.
3246 bool ShouldSubtractStep() const { return SubtractStep; }
3247 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003248 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003249 /// \brief Build the precondition expression for the loops.
3250 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003251 /// \brief Build reference expression to the counter be used for codegen.
3252 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003253 /// \brief Build reference expression to the private counter be used for
3254 /// codegen.
3255 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003256 /// \brief Build initization of the counter be used for codegen.
3257 Expr *BuildCounterInit() const;
3258 /// \brief Build step of the counter be used for codegen.
3259 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003260 /// \brief Return true if any expression is dependent.
3261 bool Dependent() const;
3262
3263private:
3264 /// \brief Check the right-hand side of an assignment in the increment
3265 /// expression.
3266 bool CheckIncRHS(Expr *RHS);
3267 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003268 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003269 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003270 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003271 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003272 /// \brief Helper to set loop increment.
3273 bool SetStep(Expr *NewStep, bool Subtract);
3274};
3275
3276bool OpenMPIterationSpaceChecker::Dependent() const {
3277 if (!Var) {
3278 assert(!LB && !UB && !Step);
3279 return false;
3280 }
3281 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3282 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3283}
3284
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003285template <typename T>
3286static T *getExprAsWritten(T *E) {
3287 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3288 E = ExprTemp->getSubExpr();
3289
3290 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3291 E = MTE->GetTemporaryExpr();
3292
3293 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3294 E = Binder->getSubExpr();
3295
3296 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3297 E = ICE->getSubExprAsWritten();
3298 return E->IgnoreParens();
3299}
3300
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003301bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3302 DeclRefExpr *NewVarRefExpr,
3303 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003304 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003305 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3306 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003307 if (!NewVar || !NewLB)
3308 return true;
3309 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003310 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003311 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3312 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003313 if ((Ctor->isCopyOrMoveConstructor() ||
3314 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3315 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003316 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003317 LB = NewLB;
3318 return false;
3319}
3320
3321bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003322 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003323 // State consistency checking to ensure correct usage.
3324 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3325 !TestIsLessOp && !TestIsStrictOp);
3326 if (!NewUB)
3327 return true;
3328 UB = NewUB;
3329 TestIsLessOp = LessOp;
3330 TestIsStrictOp = StrictOp;
3331 ConditionSrcRange = SR;
3332 ConditionLoc = SL;
3333 return false;
3334}
3335
3336bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3337 // State consistency checking to ensure correct usage.
3338 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3339 if (!NewStep)
3340 return true;
3341 if (!NewStep->isValueDependent()) {
3342 // Check that the step is integer expression.
3343 SourceLocation StepLoc = NewStep->getLocStart();
3344 ExprResult Val =
3345 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3346 if (Val.isInvalid())
3347 return true;
3348 NewStep = Val.get();
3349
3350 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3351 // If test-expr is of form var relational-op b and relational-op is < or
3352 // <= then incr-expr must cause var to increase on each iteration of the
3353 // loop. If test-expr is of form var relational-op b and relational-op is
3354 // > or >= then incr-expr must cause var to decrease on each iteration of
3355 // the loop.
3356 // If test-expr is of form b relational-op var and relational-op is < or
3357 // <= then incr-expr must cause var to decrease on each iteration of the
3358 // loop. If test-expr is of form b relational-op var and relational-op is
3359 // > or >= then incr-expr must cause var to increase on each iteration of
3360 // the loop.
3361 llvm::APSInt Result;
3362 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3363 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3364 bool IsConstNeg =
3365 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003366 bool IsConstPos =
3367 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003368 bool IsConstZero = IsConstant && !Result.getBoolValue();
3369 if (UB && (IsConstZero ||
3370 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003371 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003372 SemaRef.Diag(NewStep->getExprLoc(),
3373 diag::err_omp_loop_incr_not_compatible)
3374 << Var << TestIsLessOp << NewStep->getSourceRange();
3375 SemaRef.Diag(ConditionLoc,
3376 diag::note_omp_loop_cond_requres_compatible_incr)
3377 << TestIsLessOp << ConditionSrcRange;
3378 return true;
3379 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003380 if (TestIsLessOp == Subtract) {
3381 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3382 NewStep).get();
3383 Subtract = !Subtract;
3384 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003385 }
3386
3387 Step = NewStep;
3388 SubtractStep = Subtract;
3389 return false;
3390}
3391
Alexey Bataev9c821032015-04-30 04:23:23 +00003392bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003393 // Check init-expr for canonical loop form and save loop counter
3394 // variable - #Var and its initialization value - #LB.
3395 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3396 // var = lb
3397 // integer-type var = lb
3398 // random-access-iterator-type var = lb
3399 // pointer-type var = lb
3400 //
3401 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003402 if (EmitDiags) {
3403 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3404 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003405 return true;
3406 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003407 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003408 if (Expr *E = dyn_cast<Expr>(S))
3409 S = E->IgnoreParens();
3410 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3411 if (BO->getOpcode() == BO_Assign)
3412 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003413 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003414 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003415 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3416 if (DS->isSingleDecl()) {
3417 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003418 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003419 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003420 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003421 SemaRef.Diag(S->getLocStart(),
3422 diag::ext_omp_loop_not_canonical_init)
3423 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003424 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003425 }
3426 }
3427 }
3428 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3429 if (CE->getOperator() == OO_Equal)
3430 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003431 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3432 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003433
Alexey Bataev9c821032015-04-30 04:23:23 +00003434 if (EmitDiags) {
3435 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3436 << S->getSourceRange();
3437 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003438 return true;
3439}
3440
Alexey Bataev23b69422014-06-18 07:08:49 +00003441/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003442/// variable (which may be the loop variable) if possible.
3443static const VarDecl *GetInitVarDecl(const Expr *E) {
3444 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003445 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003446 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003447 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3448 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003449 if ((Ctor->isCopyOrMoveConstructor() ||
3450 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3451 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003452 E = CE->getArg(0)->IgnoreParenImpCasts();
3453 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3454 if (!DRE)
3455 return nullptr;
3456 return dyn_cast<VarDecl>(DRE->getDecl());
3457}
3458
3459bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3460 // Check test-expr for canonical form, save upper-bound UB, flags for
3461 // less/greater and for strict/non-strict comparison.
3462 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3463 // var relational-op b
3464 // b relational-op var
3465 //
3466 if (!S) {
3467 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3468 return true;
3469 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003470 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003471 SourceLocation CondLoc = S->getLocStart();
3472 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3473 if (BO->isRelationalOp()) {
3474 if (GetInitVarDecl(BO->getLHS()) == Var)
3475 return SetUB(BO->getRHS(),
3476 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3477 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3478 BO->getSourceRange(), BO->getOperatorLoc());
3479 if (GetInitVarDecl(BO->getRHS()) == Var)
3480 return SetUB(BO->getLHS(),
3481 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3482 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3483 BO->getSourceRange(), BO->getOperatorLoc());
3484 }
3485 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3486 if (CE->getNumArgs() == 2) {
3487 auto Op = CE->getOperator();
3488 switch (Op) {
3489 case OO_Greater:
3490 case OO_GreaterEqual:
3491 case OO_Less:
3492 case OO_LessEqual:
3493 if (GetInitVarDecl(CE->getArg(0)) == Var)
3494 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3495 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3496 CE->getOperatorLoc());
3497 if (GetInitVarDecl(CE->getArg(1)) == Var)
3498 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3499 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3500 CE->getOperatorLoc());
3501 break;
3502 default:
3503 break;
3504 }
3505 }
3506 }
3507 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3508 << S->getSourceRange() << Var;
3509 return true;
3510}
3511
3512bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3513 // RHS of canonical loop form increment can be:
3514 // var + incr
3515 // incr + var
3516 // var - incr
3517 //
3518 RHS = RHS->IgnoreParenImpCasts();
3519 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3520 if (BO->isAdditiveOp()) {
3521 bool IsAdd = BO->getOpcode() == BO_Add;
3522 if (GetInitVarDecl(BO->getLHS()) == Var)
3523 return SetStep(BO->getRHS(), !IsAdd);
3524 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3525 return SetStep(BO->getLHS(), false);
3526 }
3527 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3528 bool IsAdd = CE->getOperator() == OO_Plus;
3529 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3530 if (GetInitVarDecl(CE->getArg(0)) == Var)
3531 return SetStep(CE->getArg(1), !IsAdd);
3532 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3533 return SetStep(CE->getArg(0), false);
3534 }
3535 }
3536 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3537 << RHS->getSourceRange() << Var;
3538 return true;
3539}
3540
3541bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3542 // Check incr-expr for canonical loop form and return true if it
3543 // does not conform.
3544 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3545 // ++var
3546 // var++
3547 // --var
3548 // var--
3549 // var += incr
3550 // var -= incr
3551 // var = var + incr
3552 // var = incr + var
3553 // var = var - incr
3554 //
3555 if (!S) {
3556 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3557 return true;
3558 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003559 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003560 S = S->IgnoreParens();
3561 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3562 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3563 return SetStep(
3564 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3565 (UO->isDecrementOp() ? -1 : 1)).get(),
3566 false);
3567 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3568 switch (BO->getOpcode()) {
3569 case BO_AddAssign:
3570 case BO_SubAssign:
3571 if (GetInitVarDecl(BO->getLHS()) == Var)
3572 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3573 break;
3574 case BO_Assign:
3575 if (GetInitVarDecl(BO->getLHS()) == Var)
3576 return CheckIncRHS(BO->getRHS());
3577 break;
3578 default:
3579 break;
3580 }
3581 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3582 switch (CE->getOperator()) {
3583 case OO_PlusPlus:
3584 case OO_MinusMinus:
3585 if (GetInitVarDecl(CE->getArg(0)) == Var)
3586 return SetStep(
3587 SemaRef.ActOnIntegerConstant(
3588 CE->getLocStart(),
3589 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3590 false);
3591 break;
3592 case OO_PlusEqual:
3593 case OO_MinusEqual:
3594 if (GetInitVarDecl(CE->getArg(0)) == Var)
3595 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3596 break;
3597 case OO_Equal:
3598 if (GetInitVarDecl(CE->getArg(0)) == Var)
3599 return CheckIncRHS(CE->getArg(1));
3600 break;
3601 default:
3602 break;
3603 }
3604 }
3605 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3606 << S->getSourceRange() << Var;
3607 return true;
3608}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003609
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003610namespace {
3611// Transform variables declared in GNU statement expressions to new ones to
3612// avoid crash on codegen.
3613class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3614 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3615
3616public:
3617 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3618
3619 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3620 if (auto *VD = cast<VarDecl>(D))
3621 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3622 !isa<ImplicitParamDecl>(D)) {
3623 auto *NewVD = VarDecl::Create(
3624 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3625 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3626 VD->getTypeSourceInfo(), VD->getStorageClass());
3627 NewVD->setTSCSpec(VD->getTSCSpec());
3628 NewVD->setInit(VD->getInit());
3629 NewVD->setInitStyle(VD->getInitStyle());
3630 NewVD->setExceptionVariable(VD->isExceptionVariable());
3631 NewVD->setNRVOVariable(VD->isNRVOVariable());
Alexey Bataev11481f52016-02-17 10:29:05 +00003632 NewVD->setCXXForRangeDecl(VD->isCXXForRangeDecl());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003633 NewVD->setConstexpr(VD->isConstexpr());
3634 NewVD->setInitCapture(VD->isInitCapture());
3635 NewVD->setPreviousDeclInSameBlockScope(
3636 VD->isPreviousDeclInSameBlockScope());
3637 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003638 if (VD->hasAttrs())
3639 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003640 transformedLocalDecl(VD, NewVD);
3641 return NewVD;
3642 }
3643 return BaseTransform::TransformDefinition(Loc, D);
3644 }
3645
3646 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3647 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3648 if (E->getDecl() != NewD) {
3649 NewD->setReferenced();
3650 NewD->markUsed(SemaRef.Context);
3651 return DeclRefExpr::Create(
3652 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3653 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3654 E->getNameInfo(), E->getType(), E->getValueKind());
3655 }
3656 return BaseTransform::TransformDeclRefExpr(E);
3657 }
3658};
3659}
3660
Alexander Musmana5f070a2014-10-01 06:03:56 +00003661/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003662Expr *
3663OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3664 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003665 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003666 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003667 auto VarType = Var->getType().getNonReferenceType();
3668 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003669 SemaRef.getLangOpts().CPlusPlus) {
3670 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003671 auto *UBExpr = TestIsLessOp ? UB : LB;
3672 auto *LBExpr = TestIsLessOp ? LB : UB;
3673 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3674 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3675 if (!Upper || !Lower)
3676 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003677 if (!SemaRef.Context.hasSameType(Upper->getType(), UBExpr->getType())) {
3678 Upper = SemaRef
3679 .PerformImplicitConversion(Upper, UBExpr->getType(),
3680 Sema::AA_Converting,
3681 /*AllowExplicit=*/true)
3682 .get();
3683 }
3684 if (!SemaRef.Context.hasSameType(Lower->getType(), LBExpr->getType())) {
3685 Lower = SemaRef
3686 .PerformImplicitConversion(Lower, LBExpr->getType(),
3687 Sema::AA_Converting,
3688 /*AllowExplicit=*/true)
3689 .get();
3690 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003691 if (!Upper || !Lower)
3692 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003693
3694 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3695
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003696 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003697 // BuildBinOp already emitted error, this one is to point user to upper
3698 // and lower bound, and to tell what is passed to 'operator-'.
3699 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3700 << Upper->getSourceRange() << Lower->getSourceRange();
3701 return nullptr;
3702 }
3703 }
3704
3705 if (!Diff.isUsable())
3706 return nullptr;
3707
3708 // Upper - Lower [- 1]
3709 if (TestIsStrictOp)
3710 Diff = SemaRef.BuildBinOp(
3711 S, DefaultLoc, BO_Sub, Diff.get(),
3712 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3713 if (!Diff.isUsable())
3714 return nullptr;
3715
3716 // Upper - Lower [- 1] + Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003717 auto *StepNoImp = Step->IgnoreImplicit();
3718 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003719 if (NewStep.isInvalid())
3720 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003721 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3722 StepNoImp->getType())) {
3723 NewStep = SemaRef.PerformImplicitConversion(
3724 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3725 /*AllowExplicit=*/true);
3726 if (NewStep.isInvalid())
3727 return nullptr;
3728 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003729 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003730 if (!Diff.isUsable())
3731 return nullptr;
3732
3733 // Parentheses (for dumping/debugging purposes only).
3734 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3735 if (!Diff.isUsable())
3736 return nullptr;
3737
3738 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003739 NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003740 if (NewStep.isInvalid())
3741 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003742 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3743 StepNoImp->getType())) {
3744 NewStep = SemaRef.PerformImplicitConversion(
3745 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3746 /*AllowExplicit=*/true);
3747 if (NewStep.isInvalid())
3748 return nullptr;
3749 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003750 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003751 if (!Diff.isUsable())
3752 return nullptr;
3753
Alexander Musman174b3ca2014-10-06 11:16:29 +00003754 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003755 QualType Type = Diff.get()->getType();
3756 auto &C = SemaRef.Context;
3757 bool UseVarType = VarType->hasIntegerRepresentation() &&
3758 C.getTypeSize(Type) > C.getTypeSize(VarType);
3759 if (!Type->isIntegerType() || UseVarType) {
3760 unsigned NewSize =
3761 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3762 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3763 : Type->hasSignedIntegerRepresentation();
3764 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003765 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3766 Diff = SemaRef.PerformImplicitConversion(
3767 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3768 if (!Diff.isUsable())
3769 return nullptr;
3770 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003771 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003772 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003773 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3774 if (NewSize != C.getTypeSize(Type)) {
3775 if (NewSize < C.getTypeSize(Type)) {
3776 assert(NewSize == 64 && "incorrect loop var size");
3777 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3778 << InitSrcRange << ConditionSrcRange;
3779 }
3780 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003781 NewSize, Type->hasSignedIntegerRepresentation() ||
3782 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003783 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3784 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3785 Sema::AA_Converting, true);
3786 if (!Diff.isUsable())
3787 return nullptr;
3788 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003789 }
3790 }
3791
Alexander Musmana5f070a2014-10-01 06:03:56 +00003792 return Diff.get();
3793}
3794
Alexey Bataev62dbb972015-04-22 11:59:37 +00003795Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3796 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3797 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3798 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003799 TransformToNewDefs Transform(SemaRef);
3800
3801 auto NewLB = Transform.TransformExpr(LB);
3802 auto NewUB = Transform.TransformExpr(UB);
3803 if (NewLB.isInvalid() || NewUB.isInvalid())
3804 return Cond;
Alexey Bataev11481f52016-02-17 10:29:05 +00003805 if (!SemaRef.Context.hasSameType(NewLB.get()->getType(), LB->getType())) {
3806 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3807 Sema::AA_Converting,
3808 /*AllowExplicit=*/true);
3809 }
3810 if (!SemaRef.Context.hasSameType(NewUB.get()->getType(), UB->getType())) {
3811 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3812 Sema::AA_Converting,
3813 /*AllowExplicit=*/true);
3814 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003815 if (NewLB.isInvalid() || NewUB.isInvalid())
3816 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003817 auto CondExpr = SemaRef.BuildBinOp(
3818 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3819 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003820 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003821 if (CondExpr.isUsable()) {
Alexey Bataev11481f52016-02-17 10:29:05 +00003822 if (!SemaRef.Context.hasSameType(CondExpr.get()->getType(),
3823 SemaRef.Context.BoolTy))
3824 CondExpr = SemaRef.PerformImplicitConversion(
3825 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3826 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003827 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003828 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3829 // Otherwise use original loop conditon and evaluate it in runtime.
3830 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3831}
3832
Alexander Musmana5f070a2014-10-01 06:03:56 +00003833/// \brief Build reference expression to the counter be used for codegen.
3834Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003835 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3836 DefaultLoc);
3837}
3838
3839Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3840 if (Var && !Var->isInvalidDecl()) {
3841 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003842 auto *PrivateVar =
3843 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3844 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003845 if (PrivateVar->isInvalidDecl())
3846 return nullptr;
3847 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3848 }
3849 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003850}
3851
3852/// \brief Build initization of the counter be used for codegen.
3853Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3854
3855/// \brief Build step of the counter be used for codegen.
3856Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3857
3858/// \brief Iteration space of a single for loop.
3859struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003860 /// \brief Condition of the loop.
3861 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003862 /// \brief This expression calculates the number of iterations in the loop.
3863 /// It is always possible to calculate it before starting the loop.
3864 Expr *NumIterations;
3865 /// \brief The loop counter variable.
3866 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003867 /// \brief Private loop counter variable.
3868 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003869 /// \brief This is initializer for the initial value of #CounterVar.
3870 Expr *CounterInit;
3871 /// \brief This is step for the #CounterVar used to generate its update:
3872 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3873 Expr *CounterStep;
3874 /// \brief Should step be subtracted?
3875 bool Subtract;
3876 /// \brief Source range of the loop init.
3877 SourceRange InitSrcRange;
3878 /// \brief Source range of the loop condition.
3879 SourceRange CondSrcRange;
3880 /// \brief Source range of the loop increment.
3881 SourceRange IncSrcRange;
3882};
3883
Alexey Bataev23b69422014-06-18 07:08:49 +00003884} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003885
Alexey Bataev9c821032015-04-30 04:23:23 +00003886void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3887 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3888 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003889 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3890 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003891 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3892 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003893 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003894 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003895 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003896 }
3897}
3898
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003899/// \brief Called on a for stmt to check and extract its iteration space
3900/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003901static bool CheckOpenMPIterationSpace(
3902 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3903 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003904 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003905 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003906 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003907 // OpenMP [2.6, Canonical Loop Form]
3908 // for (init-expr; test-expr; incr-expr) structured-block
3909 auto For = dyn_cast_or_null<ForStmt>(S);
3910 if (!For) {
3911 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003912 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3913 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3914 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3915 if (NestedLoopCount > 1) {
3916 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3917 SemaRef.Diag(DSA.getConstructLoc(),
3918 diag::note_omp_collapse_ordered_expr)
3919 << 2 << CollapseLoopCountExpr->getSourceRange()
3920 << OrderedLoopCountExpr->getSourceRange();
3921 else if (CollapseLoopCountExpr)
3922 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3923 diag::note_omp_collapse_ordered_expr)
3924 << 0 << CollapseLoopCountExpr->getSourceRange();
3925 else
3926 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3927 diag::note_omp_collapse_ordered_expr)
3928 << 1 << OrderedLoopCountExpr->getSourceRange();
3929 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 return true;
3931 }
3932 assert(For->getBody());
3933
3934 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3935
3936 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003937 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 if (ISC.CheckInit(Init)) {
3939 return true;
3940 }
3941
3942 bool HasErrors = false;
3943
3944 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003945 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946
3947 // OpenMP [2.6, Canonical Loop Form]
3948 // Var is one of the following:
3949 // A variable of signed or unsigned integer type.
3950 // For C++, a variable of a random access iterator type.
3951 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003952 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003953 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3954 !VarType->isPointerType() &&
3955 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3956 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3957 << SemaRef.getLangOpts().CPlusPlus;
3958 HasErrors = true;
3959 }
3960
Alexey Bataev4acb8592014-07-07 13:01:15 +00003961 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3962 // Construct
3963 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3964 // parallel for construct is (are) private.
3965 // The loop iteration variable in the associated for-loop of a simd construct
3966 // with just one associated for-loop is linear with a constant-linear-step
3967 // that is the increment of the associated for-loop.
3968 // Exclude loop var from the list of variables with implicitly defined data
3969 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003970 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003971
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003972 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3973 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003974 // The loop iteration variable in the associated for-loop of a simd construct
3975 // with just one associated for-loop may be listed in a linear clause with a
3976 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003977 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3978 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003979 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003980 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3981 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3982 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003983 auto PredeterminedCKind =
3984 isOpenMPSimdDirective(DKind)
3985 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3986 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003987 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003988 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003989 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003990 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003991 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003992 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3993 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003995 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3996 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003997 if (DVar.RefExpr == nullptr)
3998 DVar.CKind = PredeterminedCKind;
3999 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004001 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00004002 // Make the loop iteration variable private (for worksharing constructs),
4003 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00004004 // lastprivate (for simd directives with several collapsed or ordered
4005 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00004006 if (DVar.CKind == OMPC_unknown)
4007 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
4008 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00004009 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004010 }
4011
Alexey Bataev7ff55242014-06-19 09:13:45 +00004012 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00004013
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004014 // Check test-expr.
4015 HasErrors |= ISC.CheckCond(For->getCond());
4016
4017 // Check incr-expr.
4018 HasErrors |= ISC.CheckInc(For->getInc());
4019
Alexander Musmana5f070a2014-10-01 06:03:56 +00004020 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004021 return HasErrors;
4022
Alexander Musmana5f070a2014-10-01 06:03:56 +00004023 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004024 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00004025 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004026 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004027 isOpenMPTaskLoopDirective(DKind) ||
4028 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004029 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00004030 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004031 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4032 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4033 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4034 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4035 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4036 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4037
Alexey Bataev62dbb972015-04-22 11:59:37 +00004038 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4039 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004040 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004041 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004042 ResultIterSpace.CounterInit == nullptr ||
4043 ResultIterSpace.CounterStep == nullptr);
4044
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004045 return HasErrors;
4046}
4047
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004048/// \brief Build 'VarRef = Start.
4049static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4050 ExprResult VarRef, ExprResult Start) {
4051 TransformToNewDefs Transform(SemaRef);
4052 // Build 'VarRef = Start.
Alexey Bataev11481f52016-02-17 10:29:05 +00004053 auto *StartNoImp = Start.get()->IgnoreImplicit();
4054 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004055 if (NewStart.isInvalid())
4056 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004057 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4058 StartNoImp->getType())) {
4059 NewStart = SemaRef.PerformImplicitConversion(
4060 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4061 /*AllowExplicit=*/true);
4062 if (NewStart.isInvalid())
4063 return ExprError();
4064 }
4065 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4066 VarRef.get()->getType())) {
4067 NewStart = SemaRef.PerformImplicitConversion(
4068 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4069 /*AllowExplicit=*/true);
4070 if (!NewStart.isUsable())
4071 return ExprError();
4072 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004073
4074 auto Init =
4075 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4076 return Init;
4077}
4078
Alexander Musmana5f070a2014-10-01 06:03:56 +00004079/// \brief Build 'VarRef = Start + Iter * Step'.
4080static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4081 SourceLocation Loc, ExprResult VarRef,
4082 ExprResult Start, ExprResult Iter,
4083 ExprResult Step, bool Subtract) {
4084 // Add parentheses (for debugging purposes only).
4085 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4086 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4087 !Step.isUsable())
4088 return ExprError();
4089
Alexey Bataev11481f52016-02-17 10:29:05 +00004090 auto *StepNoImp = Step.get()->IgnoreImplicit();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004091 TransformToNewDefs Transform(SemaRef);
Alexey Bataev11481f52016-02-17 10:29:05 +00004092 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004093 if (NewStep.isInvalid())
4094 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004095 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
4096 StepNoImp->getType())) {
4097 NewStep = SemaRef.PerformImplicitConversion(
4098 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
4099 /*AllowExplicit=*/true);
4100 if (NewStep.isInvalid())
4101 return ExprError();
4102 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004103 ExprResult Update =
4104 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004105 if (!Update.isUsable())
4106 return ExprError();
4107
Alexey Bataevc0214e02016-02-16 12:13:49 +00004108 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4109 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev11481f52016-02-17 10:29:05 +00004110 auto *StartNoImp = Start.get()->IgnoreImplicit();
4111 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004112 if (NewStart.isInvalid())
4113 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004114 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4115 StartNoImp->getType())) {
4116 NewStart = SemaRef.PerformImplicitConversion(
4117 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4118 /*AllowExplicit=*/true);
4119 if (NewStart.isInvalid())
4120 return ExprError();
4121 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004122
Alexey Bataevc0214e02016-02-16 12:13:49 +00004123 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4124 ExprResult SavedUpdate = Update;
4125 ExprResult UpdateVal;
4126 if (VarRef.get()->getType()->isOverloadableType() ||
4127 NewStart.get()->getType()->isOverloadableType() ||
4128 Update.get()->getType()->isOverloadableType()) {
4129 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4130 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4131 Update =
4132 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4133 if (Update.isUsable()) {
4134 UpdateVal =
4135 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4136 VarRef.get(), SavedUpdate.get());
4137 if (UpdateVal.isUsable()) {
4138 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4139 UpdateVal.get());
4140 }
4141 }
4142 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4143 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004144
Alexey Bataevc0214e02016-02-16 12:13:49 +00004145 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4146 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4147 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4148 NewStart.get(), SavedUpdate.get());
4149 if (!Update.isUsable())
4150 return ExprError();
4151
Alexey Bataev11481f52016-02-17 10:29:05 +00004152 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4153 VarRef.get()->getType())) {
4154 Update = SemaRef.PerformImplicitConversion(
4155 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4156 if (!Update.isUsable())
4157 return ExprError();
4158 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004159
4160 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4161 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004162 return Update;
4163}
4164
4165/// \brief Convert integer expression \a E to make it have at least \a Bits
4166/// bits.
4167static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4168 Sema &SemaRef) {
4169 if (E == nullptr)
4170 return ExprError();
4171 auto &C = SemaRef.Context;
4172 QualType OldType = E->getType();
4173 unsigned HasBits = C.getTypeSize(OldType);
4174 if (HasBits >= Bits)
4175 return ExprResult(E);
4176 // OK to convert to signed, because new type has more bits than old.
4177 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4178 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4179 true);
4180}
4181
4182/// \brief Check if the given expression \a E is a constant integer that fits
4183/// into \a Bits bits.
4184static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4185 if (E == nullptr)
4186 return false;
4187 llvm::APSInt Result;
4188 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4189 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4190 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004191}
4192
4193/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004194/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4195/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004196static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004197CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4198 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4199 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004200 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004201 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004202 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004203 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004204 // Found 'collapse' clause - calculate collapse number.
4205 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004206 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004207 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004208 }
4209 if (OrderedLoopCountExpr) {
4210 // Found 'ordered' clause - calculate collapse number.
4211 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004212 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4213 if (Result.getLimitedValue() < NestedLoopCount) {
4214 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4215 diag::err_omp_wrong_ordered_loop_count)
4216 << OrderedLoopCountExpr->getSourceRange();
4217 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4218 diag::note_collapse_loop_count)
4219 << CollapseLoopCountExpr->getSourceRange();
4220 }
4221 NestedLoopCount = Result.getLimitedValue();
4222 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004223 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004224 // This is helper routine for loop directives (e.g., 'for', 'simd',
4225 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004226 SmallVector<LoopIterationSpace, 4> IterSpaces;
4227 IterSpaces.resize(NestedLoopCount);
4228 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004229 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004230 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004231 NestedLoopCount, CollapseLoopCountExpr,
4232 OrderedLoopCountExpr, VarsWithImplicitDSA,
4233 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004234 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004235 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004236 // OpenMP [2.8.1, simd construct, Restrictions]
4237 // All loops associated with the construct must be perfectly nested; that
4238 // is, there must be no intervening code nor any OpenMP directive between
4239 // any two loops.
4240 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241 }
4242
Alexander Musmana5f070a2014-10-01 06:03:56 +00004243 Built.clear(/* size */ NestedLoopCount);
4244
4245 if (SemaRef.CurContext->isDependentContext())
4246 return NestedLoopCount;
4247
4248 // An example of what is generated for the following code:
4249 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004250 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004251 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004252 // for (k = 0; k < NK; ++k)
4253 // for (j = J0; j < NJ; j+=2) {
4254 // <loop body>
4255 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004256 //
4257 // We generate the code below.
4258 // Note: the loop body may be outlined in CodeGen.
4259 // Note: some counters may be C++ classes, operator- is used to find number of
4260 // iterations and operator+= to calculate counter value.
4261 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4262 // or i64 is currently supported).
4263 //
4264 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4265 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4266 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4267 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4268 // // similar updates for vars in clauses (e.g. 'linear')
4269 // <loop body (using local i and j)>
4270 // }
4271 // i = NI; // assign final values of counters
4272 // j = NJ;
4273 //
4274
4275 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4276 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004277 // Precondition tests if there is at least one iteration (all conditions are
4278 // true).
4279 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004280 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004281 ExprResult LastIteration32 = WidenIterationCount(
4282 32 /* Bits */, SemaRef.PerformImplicitConversion(
4283 N0->IgnoreImpCasts(), N0->getType(),
4284 Sema::AA_Converting, /*AllowExplicit=*/true)
4285 .get(),
4286 SemaRef);
4287 ExprResult LastIteration64 = WidenIterationCount(
4288 64 /* Bits */, SemaRef.PerformImplicitConversion(
4289 N0->IgnoreImpCasts(), N0->getType(),
4290 Sema::AA_Converting, /*AllowExplicit=*/true)
4291 .get(),
4292 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004293
4294 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4295 return NestedLoopCount;
4296
4297 auto &C = SemaRef.Context;
4298 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4299
4300 Scope *CurScope = DSA.getCurScope();
4301 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004302 if (PreCond.isUsable()) {
4303 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4304 PreCond.get(), IterSpaces[Cnt].PreCond);
4305 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004306 auto N = IterSpaces[Cnt].NumIterations;
4307 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4308 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004309 LastIteration32 = SemaRef.BuildBinOp(
4310 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4311 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4312 Sema::AA_Converting,
4313 /*AllowExplicit=*/true)
4314 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004315 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004316 LastIteration64 = SemaRef.BuildBinOp(
4317 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4318 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4319 Sema::AA_Converting,
4320 /*AllowExplicit=*/true)
4321 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004322 }
4323
4324 // Choose either the 32-bit or 64-bit version.
4325 ExprResult LastIteration = LastIteration64;
4326 if (LastIteration32.isUsable() &&
4327 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4328 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4329 FitsInto(
4330 32 /* Bits */,
4331 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4332 LastIteration64.get(), SemaRef)))
4333 LastIteration = LastIteration32;
4334
4335 if (!LastIteration.isUsable())
4336 return 0;
4337
4338 // Save the number of iterations.
4339 ExprResult NumIterations = LastIteration;
4340 {
4341 LastIteration = SemaRef.BuildBinOp(
4342 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4343 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4344 if (!LastIteration.isUsable())
4345 return 0;
4346 }
4347
4348 // Calculate the last iteration number beforehand instead of doing this on
4349 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4350 llvm::APSInt Result;
4351 bool IsConstant =
4352 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4353 ExprResult CalcLastIteration;
4354 if (!IsConstant) {
4355 SourceLocation SaveLoc;
4356 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004357 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004358 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004359 ExprResult SaveRef = buildDeclRefExpr(
4360 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004361 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4362 SaveRef.get(), LastIteration.get());
4363 LastIteration = SaveRef;
4364
4365 // Prepare SaveRef + 1.
4366 NumIterations = SemaRef.BuildBinOp(
4367 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4368 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4369 if (!NumIterations.isUsable())
4370 return 0;
4371 }
4372
4373 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4374
Alexander Musmanc6388682014-12-15 07:07:06 +00004375 QualType VType = LastIteration.get()->getType();
4376 // Build variables passed into runtime, nesessary for worksharing directives.
4377 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004378 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4379 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004380 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004381 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4382 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004383 SemaRef.AddInitializerToDecl(
4384 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4385 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4386
4387 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004388 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4389 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004390 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4391 /*DirectInit*/ false,
4392 /*TypeMayContainAuto*/ false);
4393
4394 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4395 // This will be used to implement clause 'lastprivate'.
4396 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004397 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4398 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004399 SemaRef.AddInitializerToDecl(
4400 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4401 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4402
4403 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004404 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4405 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004406 SemaRef.AddInitializerToDecl(
4407 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4408 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4409
4410 // Build expression: UB = min(UB, LastIteration)
4411 // It is nesessary for CodeGen of directives with static scheduling.
4412 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4413 UB.get(), LastIteration.get());
4414 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4415 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4416 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4417 CondOp.get());
4418 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4419 }
4420
4421 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004422 ExprResult IV;
4423 ExprResult Init;
4424 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004425 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4426 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004427 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004428 isOpenMPTaskLoopDirective(DKind) ||
4429 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004430 ? LB.get()
4431 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4432 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4433 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004434 }
4435
Alexander Musmanc6388682014-12-15 07:07:06 +00004436 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004437 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004438 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004439 (isOpenMPWorksharingDirective(DKind) ||
4440 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004441 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4442 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4443 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004444
4445 // Loop increment (IV = IV + 1)
4446 SourceLocation IncLoc;
4447 ExprResult Inc =
4448 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4449 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4450 if (!Inc.isUsable())
4451 return 0;
4452 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004453 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4454 if (!Inc.isUsable())
4455 return 0;
4456
4457 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4458 // Used for directives with static scheduling.
4459 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004460 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4461 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004462 // LB + ST
4463 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4464 if (!NextLB.isUsable())
4465 return 0;
4466 // LB = LB + ST
4467 NextLB =
4468 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4469 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4470 if (!NextLB.isUsable())
4471 return 0;
4472 // UB + ST
4473 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4474 if (!NextUB.isUsable())
4475 return 0;
4476 // UB = UB + ST
4477 NextUB =
4478 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4479 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4480 if (!NextUB.isUsable())
4481 return 0;
4482 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004483
4484 // Build updates and final values of the loop counters.
4485 bool HasErrors = false;
4486 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004487 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004488 Built.Updates.resize(NestedLoopCount);
4489 Built.Finals.resize(NestedLoopCount);
4490 {
4491 ExprResult Div;
4492 // Go from inner nested loop to outer.
4493 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4494 LoopIterationSpace &IS = IterSpaces[Cnt];
4495 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4496 // Build: Iter = (IV / Div) % IS.NumIters
4497 // where Div is product of previous iterations' IS.NumIters.
4498 ExprResult Iter;
4499 if (Div.isUsable()) {
4500 Iter =
4501 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4502 } else {
4503 Iter = IV;
4504 assert((Cnt == (int)NestedLoopCount - 1) &&
4505 "unusable div expected on first iteration only");
4506 }
4507
4508 if (Cnt != 0 && Iter.isUsable())
4509 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4510 IS.NumIterations);
4511 if (!Iter.isUsable()) {
4512 HasErrors = true;
4513 break;
4514 }
4515
Alexey Bataev39f915b82015-05-08 10:41:21 +00004516 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4517 auto *CounterVar = buildDeclRefExpr(
4518 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4519 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4520 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004521 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4522 IS.CounterInit);
4523 if (!Init.isUsable()) {
4524 HasErrors = true;
4525 break;
4526 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004527 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004528 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004529 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4530 if (!Update.isUsable()) {
4531 HasErrors = true;
4532 break;
4533 }
4534
4535 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4536 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004537 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004538 IS.NumIterations, IS.CounterStep, IS.Subtract);
4539 if (!Final.isUsable()) {
4540 HasErrors = true;
4541 break;
4542 }
4543
4544 // Build Div for the next iteration: Div <- Div * IS.NumIters
4545 if (Cnt != 0) {
4546 if (Div.isUnset())
4547 Div = IS.NumIterations;
4548 else
4549 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4550 IS.NumIterations);
4551
4552 // Add parentheses (for debugging purposes only).
4553 if (Div.isUsable())
4554 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4555 if (!Div.isUsable()) {
4556 HasErrors = true;
4557 break;
4558 }
4559 }
4560 if (!Update.isUsable() || !Final.isUsable()) {
4561 HasErrors = true;
4562 break;
4563 }
4564 // Save results
4565 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004566 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004567 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004568 Built.Updates[Cnt] = Update.get();
4569 Built.Finals[Cnt] = Final.get();
4570 }
4571 }
4572
4573 if (HasErrors)
4574 return 0;
4575
4576 // Save results
4577 Built.IterationVarRef = IV.get();
4578 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004579 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004580 Built.CalcLastIteration =
4581 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004582 Built.PreCond = PreCond.get();
4583 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004584 Built.Init = Init.get();
4585 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004586 Built.LB = LB.get();
4587 Built.UB = UB.get();
4588 Built.IL = IL.get();
4589 Built.ST = ST.get();
4590 Built.EUB = EUB.get();
4591 Built.NLB = NextLB.get();
4592 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004593
Alexey Bataevabfc0692014-06-25 06:52:00 +00004594 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004595}
4596
Alexey Bataev10e775f2015-07-30 11:36:16 +00004597static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004598 auto CollapseClauses =
4599 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4600 if (CollapseClauses.begin() != CollapseClauses.end())
4601 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004602 return nullptr;
4603}
4604
Alexey Bataev10e775f2015-07-30 11:36:16 +00004605static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004606 auto OrderedClauses =
4607 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4608 if (OrderedClauses.begin() != OrderedClauses.end())
4609 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004610 return nullptr;
4611}
4612
Alexey Bataev66b15b52015-08-21 11:14:16 +00004613static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4614 const Expr *Safelen) {
4615 llvm::APSInt SimdlenRes, SafelenRes;
4616 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4617 Simdlen->isInstantiationDependent() ||
4618 Simdlen->containsUnexpandedParameterPack())
4619 return false;
4620 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4621 Safelen->isInstantiationDependent() ||
4622 Safelen->containsUnexpandedParameterPack())
4623 return false;
4624 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4625 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4626 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4627 // If both simdlen and safelen clauses are specified, the value of the simdlen
4628 // parameter must be less than or equal to the value of the safelen parameter.
4629 if (SimdlenRes > SafelenRes) {
4630 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4631 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4632 return true;
4633 }
4634 return false;
4635}
4636
Alexey Bataev4acb8592014-07-07 13:01:15 +00004637StmtResult Sema::ActOnOpenMPSimdDirective(
4638 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4639 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004640 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004641 if (!AStmt)
4642 return StmtError();
4643
4644 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004645 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004646 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4647 // define the nested loops number.
4648 unsigned NestedLoopCount = CheckOpenMPLoop(
4649 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4650 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004651 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004652 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004653
Alexander Musmana5f070a2014-10-01 06:03:56 +00004654 assert((CurContext->isDependentContext() || B.builtAll()) &&
4655 "omp simd loop exprs were not built");
4656
Alexander Musman3276a272015-03-21 10:12:56 +00004657 if (!CurContext->isDependentContext()) {
4658 // Finalize the clauses that need pre-built expressions for CodeGen.
4659 for (auto C : Clauses) {
4660 if (auto LC = dyn_cast<OMPLinearClause>(C))
4661 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4662 B.NumIterations, *this, CurScope))
4663 return StmtError();
4664 }
4665 }
4666
Alexey Bataev66b15b52015-08-21 11:14:16 +00004667 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4668 // If both simdlen and safelen clauses are specified, the value of the simdlen
4669 // parameter must be less than or equal to the value of the safelen parameter.
4670 OMPSafelenClause *Safelen = nullptr;
4671 OMPSimdlenClause *Simdlen = nullptr;
4672 for (auto *Clause : Clauses) {
4673 if (Clause->getClauseKind() == OMPC_safelen)
4674 Safelen = cast<OMPSafelenClause>(Clause);
4675 else if (Clause->getClauseKind() == OMPC_simdlen)
4676 Simdlen = cast<OMPSimdlenClause>(Clause);
4677 if (Safelen && Simdlen)
4678 break;
4679 }
4680 if (Simdlen && Safelen &&
4681 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4682 Safelen->getSafelen()))
4683 return StmtError();
4684
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004685 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004686 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4687 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004688}
4689
Alexey Bataev4acb8592014-07-07 13:01:15 +00004690StmtResult Sema::ActOnOpenMPForDirective(
4691 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4692 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004693 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004694 if (!AStmt)
4695 return StmtError();
4696
4697 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004698 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004699 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4700 // define the nested loops number.
4701 unsigned NestedLoopCount = CheckOpenMPLoop(
4702 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4703 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004704 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004705 return StmtError();
4706
Alexander Musmana5f070a2014-10-01 06:03:56 +00004707 assert((CurContext->isDependentContext() || B.builtAll()) &&
4708 "omp for loop exprs were not built");
4709
Alexey Bataev54acd402015-08-04 11:18:19 +00004710 if (!CurContext->isDependentContext()) {
4711 // Finalize the clauses that need pre-built expressions for CodeGen.
4712 for (auto C : Clauses) {
4713 if (auto LC = dyn_cast<OMPLinearClause>(C))
4714 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4715 B.NumIterations, *this, CurScope))
4716 return StmtError();
4717 }
4718 }
4719
Alexey Bataevf29276e2014-06-18 04:14:57 +00004720 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004721 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004722 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004723}
4724
Alexander Musmanf82886e2014-09-18 05:12:34 +00004725StmtResult Sema::ActOnOpenMPForSimdDirective(
4726 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4727 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004728 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004729 if (!AStmt)
4730 return StmtError();
4731
4732 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004733 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004734 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4735 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004736 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004737 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4738 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4739 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004740 if (NestedLoopCount == 0)
4741 return StmtError();
4742
Alexander Musmanc6388682014-12-15 07:07:06 +00004743 assert((CurContext->isDependentContext() || B.builtAll()) &&
4744 "omp for simd loop exprs were not built");
4745
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004746 if (!CurContext->isDependentContext()) {
4747 // Finalize the clauses that need pre-built expressions for CodeGen.
4748 for (auto C : Clauses) {
4749 if (auto LC = dyn_cast<OMPLinearClause>(C))
4750 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4751 B.NumIterations, *this, CurScope))
4752 return StmtError();
4753 }
4754 }
4755
Alexey Bataev66b15b52015-08-21 11:14:16 +00004756 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4757 // If both simdlen and safelen clauses are specified, the value of the simdlen
4758 // parameter must be less than or equal to the value of the safelen parameter.
4759 OMPSafelenClause *Safelen = nullptr;
4760 OMPSimdlenClause *Simdlen = nullptr;
4761 for (auto *Clause : Clauses) {
4762 if (Clause->getClauseKind() == OMPC_safelen)
4763 Safelen = cast<OMPSafelenClause>(Clause);
4764 else if (Clause->getClauseKind() == OMPC_simdlen)
4765 Simdlen = cast<OMPSimdlenClause>(Clause);
4766 if (Safelen && Simdlen)
4767 break;
4768 }
4769 if (Simdlen && Safelen &&
4770 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4771 Safelen->getSafelen()))
4772 return StmtError();
4773
Alexander Musmanf82886e2014-09-18 05:12:34 +00004774 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004775 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4776 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004777}
4778
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004779StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4780 Stmt *AStmt,
4781 SourceLocation StartLoc,
4782 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004783 if (!AStmt)
4784 return StmtError();
4785
4786 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004787 auto BaseStmt = AStmt;
4788 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4789 BaseStmt = CS->getCapturedStmt();
4790 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4791 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004792 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004793 return StmtError();
4794 // All associated statements must be '#pragma omp section' except for
4795 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004796 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004797 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4798 if (SectionStmt)
4799 Diag(SectionStmt->getLocStart(),
4800 diag::err_omp_sections_substmt_not_section);
4801 return StmtError();
4802 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004803 cast<OMPSectionDirective>(SectionStmt)
4804 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004805 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004806 } else {
4807 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4808 return StmtError();
4809 }
4810
4811 getCurFunction()->setHasBranchProtectedScope();
4812
Alexey Bataev25e5b442015-09-15 12:52:43 +00004813 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4814 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004815}
4816
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004817StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4818 SourceLocation StartLoc,
4819 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004820 if (!AStmt)
4821 return StmtError();
4822
4823 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004824
4825 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004826 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004827
Alexey Bataev25e5b442015-09-15 12:52:43 +00004828 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4829 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004830}
4831
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004832StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4833 Stmt *AStmt,
4834 SourceLocation StartLoc,
4835 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004836 if (!AStmt)
4837 return StmtError();
4838
4839 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004840
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004841 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004842
Alexey Bataev3255bf32015-01-19 05:20:46 +00004843 // OpenMP [2.7.3, single Construct, Restrictions]
4844 // The copyprivate clause must not be used with the nowait clause.
4845 OMPClause *Nowait = nullptr;
4846 OMPClause *Copyprivate = nullptr;
4847 for (auto *Clause : Clauses) {
4848 if (Clause->getClauseKind() == OMPC_nowait)
4849 Nowait = Clause;
4850 else if (Clause->getClauseKind() == OMPC_copyprivate)
4851 Copyprivate = Clause;
4852 if (Copyprivate && Nowait) {
4853 Diag(Copyprivate->getLocStart(),
4854 diag::err_omp_single_copyprivate_with_nowait);
4855 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4856 return StmtError();
4857 }
4858 }
4859
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004860 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4861}
4862
Alexander Musman80c22892014-07-17 08:54:58 +00004863StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4864 SourceLocation StartLoc,
4865 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004866 if (!AStmt)
4867 return StmtError();
4868
4869 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004870
4871 getCurFunction()->setHasBranchProtectedScope();
4872
4873 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4874}
4875
Alexey Bataev28c75412015-12-15 08:19:24 +00004876StmtResult Sema::ActOnOpenMPCriticalDirective(
4877 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4878 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004879 if (!AStmt)
4880 return StmtError();
4881
4882 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004883
Alexey Bataev28c75412015-12-15 08:19:24 +00004884 bool ErrorFound = false;
4885 llvm::APSInt Hint;
4886 SourceLocation HintLoc;
4887 bool DependentHint = false;
4888 for (auto *C : Clauses) {
4889 if (C->getClauseKind() == OMPC_hint) {
4890 if (!DirName.getName()) {
4891 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4892 ErrorFound = true;
4893 }
4894 Expr *E = cast<OMPHintClause>(C)->getHint();
4895 if (E->isTypeDependent() || E->isValueDependent() ||
4896 E->isInstantiationDependent())
4897 DependentHint = true;
4898 else {
4899 Hint = E->EvaluateKnownConstInt(Context);
4900 HintLoc = C->getLocStart();
4901 }
4902 }
4903 }
4904 if (ErrorFound)
4905 return StmtError();
4906 auto Pair = DSAStack->getCriticalWithHint(DirName);
4907 if (Pair.first && DirName.getName() && !DependentHint) {
4908 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4909 Diag(StartLoc, diag::err_omp_critical_with_hint);
4910 if (HintLoc.isValid()) {
4911 Diag(HintLoc, diag::note_omp_critical_hint_here)
4912 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4913 } else
4914 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4915 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4916 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4917 << 1
4918 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4919 /*Radix=*/10, /*Signed=*/false);
4920 } else
4921 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4922 }
4923 }
4924
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004925 getCurFunction()->setHasBranchProtectedScope();
4926
Alexey Bataev28c75412015-12-15 08:19:24 +00004927 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4928 Clauses, AStmt);
4929 if (!Pair.first && DirName.getName() && !DependentHint)
4930 DSAStack->addCriticalWithHint(Dir, Hint);
4931 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004932}
4933
Alexey Bataev4acb8592014-07-07 13:01:15 +00004934StmtResult Sema::ActOnOpenMPParallelForDirective(
4935 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4936 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004937 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004938 if (!AStmt)
4939 return StmtError();
4940
Alexey Bataev4acb8592014-07-07 13:01:15 +00004941 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4942 // 1.2.2 OpenMP Language Terminology
4943 // Structured block - An executable statement with a single entry at the
4944 // top and a single exit at the bottom.
4945 // The point of exit cannot be a branch out of the structured block.
4946 // longjmp() and throw() must not violate the entry/exit criteria.
4947 CS->getCapturedDecl()->setNothrow();
4948
Alexander Musmanc6388682014-12-15 07:07:06 +00004949 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004950 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4951 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004952 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004953 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4954 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4955 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004956 if (NestedLoopCount == 0)
4957 return StmtError();
4958
Alexander Musmana5f070a2014-10-01 06:03:56 +00004959 assert((CurContext->isDependentContext() || B.builtAll()) &&
4960 "omp parallel for loop exprs were not built");
4961
Alexey Bataev54acd402015-08-04 11:18:19 +00004962 if (!CurContext->isDependentContext()) {
4963 // Finalize the clauses that need pre-built expressions for CodeGen.
4964 for (auto C : Clauses) {
4965 if (auto LC = dyn_cast<OMPLinearClause>(C))
4966 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4967 B.NumIterations, *this, CurScope))
4968 return StmtError();
4969 }
4970 }
4971
Alexey Bataev4acb8592014-07-07 13:01:15 +00004972 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004973 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004974 NestedLoopCount, Clauses, AStmt, B,
4975 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004976}
4977
Alexander Musmane4e893b2014-09-23 09:33:00 +00004978StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4979 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4980 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004981 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004982 if (!AStmt)
4983 return StmtError();
4984
Alexander Musmane4e893b2014-09-23 09:33:00 +00004985 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4986 // 1.2.2 OpenMP Language Terminology
4987 // Structured block - An executable statement with a single entry at the
4988 // top and a single exit at the bottom.
4989 // The point of exit cannot be a branch out of the structured block.
4990 // longjmp() and throw() must not violate the entry/exit criteria.
4991 CS->getCapturedDecl()->setNothrow();
4992
Alexander Musmanc6388682014-12-15 07:07:06 +00004993 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004994 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4995 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004996 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004997 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4998 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4999 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005000 if (NestedLoopCount == 0)
5001 return StmtError();
5002
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005003 if (!CurContext->isDependentContext()) {
5004 // Finalize the clauses that need pre-built expressions for CodeGen.
5005 for (auto C : Clauses) {
5006 if (auto LC = dyn_cast<OMPLinearClause>(C))
5007 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5008 B.NumIterations, *this, CurScope))
5009 return StmtError();
5010 }
5011 }
5012
Alexey Bataev66b15b52015-08-21 11:14:16 +00005013 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5014 // If both simdlen and safelen clauses are specified, the value of the simdlen
5015 // parameter must be less than or equal to the value of the safelen parameter.
5016 OMPSafelenClause *Safelen = nullptr;
5017 OMPSimdlenClause *Simdlen = nullptr;
5018 for (auto *Clause : Clauses) {
5019 if (Clause->getClauseKind() == OMPC_safelen)
5020 Safelen = cast<OMPSafelenClause>(Clause);
5021 else if (Clause->getClauseKind() == OMPC_simdlen)
5022 Simdlen = cast<OMPSimdlenClause>(Clause);
5023 if (Safelen && Simdlen)
5024 break;
5025 }
5026 if (Simdlen && Safelen &&
5027 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5028 Safelen->getSafelen()))
5029 return StmtError();
5030
Alexander Musmane4e893b2014-09-23 09:33:00 +00005031 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005032 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005033 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005034}
5035
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005036StmtResult
5037Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5038 Stmt *AStmt, SourceLocation StartLoc,
5039 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005040 if (!AStmt)
5041 return StmtError();
5042
5043 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005044 auto BaseStmt = AStmt;
5045 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5046 BaseStmt = CS->getCapturedStmt();
5047 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5048 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005049 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005050 return StmtError();
5051 // All associated statements must be '#pragma omp section' except for
5052 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005053 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005054 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5055 if (SectionStmt)
5056 Diag(SectionStmt->getLocStart(),
5057 diag::err_omp_parallel_sections_substmt_not_section);
5058 return StmtError();
5059 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005060 cast<OMPSectionDirective>(SectionStmt)
5061 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005062 }
5063 } else {
5064 Diag(AStmt->getLocStart(),
5065 diag::err_omp_parallel_sections_not_compound_stmt);
5066 return StmtError();
5067 }
5068
5069 getCurFunction()->setHasBranchProtectedScope();
5070
Alexey Bataev25e5b442015-09-15 12:52:43 +00005071 return OMPParallelSectionsDirective::Create(
5072 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005073}
5074
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005075StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5076 Stmt *AStmt, SourceLocation StartLoc,
5077 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005078 if (!AStmt)
5079 return StmtError();
5080
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005081 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5082 // 1.2.2 OpenMP Language Terminology
5083 // Structured block - An executable statement with a single entry at the
5084 // top and a single exit at the bottom.
5085 // The point of exit cannot be a branch out of the structured block.
5086 // longjmp() and throw() must not violate the entry/exit criteria.
5087 CS->getCapturedDecl()->setNothrow();
5088
5089 getCurFunction()->setHasBranchProtectedScope();
5090
Alexey Bataev25e5b442015-09-15 12:52:43 +00005091 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5092 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005093}
5094
Alexey Bataev68446b72014-07-18 07:47:19 +00005095StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5096 SourceLocation EndLoc) {
5097 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5098}
5099
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005100StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5101 SourceLocation EndLoc) {
5102 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5103}
5104
Alexey Bataev2df347a2014-07-18 10:17:07 +00005105StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5106 SourceLocation EndLoc) {
5107 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5108}
5109
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005110StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5111 SourceLocation StartLoc,
5112 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005113 if (!AStmt)
5114 return StmtError();
5115
5116 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005117
5118 getCurFunction()->setHasBranchProtectedScope();
5119
5120 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5121}
5122
Alexey Bataev6125da92014-07-21 11:26:11 +00005123StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5124 SourceLocation StartLoc,
5125 SourceLocation EndLoc) {
5126 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5127 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5128}
5129
Alexey Bataev346265e2015-09-25 10:37:12 +00005130StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5131 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005132 SourceLocation StartLoc,
5133 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005134 OMPClause *DependFound = nullptr;
5135 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005136 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005137 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005138 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005139 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005140 for (auto *C : Clauses) {
5141 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5142 DependFound = C;
5143 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5144 if (DependSourceClause) {
5145 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5146 << getOpenMPDirectiveName(OMPD_ordered)
5147 << getOpenMPClauseName(OMPC_depend) << 2;
5148 ErrorFound = true;
5149 } else
5150 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005151 if (DependSinkClause) {
5152 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5153 << 0;
5154 ErrorFound = true;
5155 }
5156 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5157 if (DependSourceClause) {
5158 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5159 << 1;
5160 ErrorFound = true;
5161 }
5162 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005163 }
5164 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005165 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005166 else if (C->getClauseKind() == OMPC_simd)
5167 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005168 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005169 if (!ErrorFound && !SC &&
5170 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005171 // OpenMP [2.8.1,simd Construct, Restrictions]
5172 // An ordered construct with the simd clause is the only OpenMP construct
5173 // that can appear in the simd region.
5174 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005175 ErrorFound = true;
5176 } else if (DependFound && (TC || SC)) {
5177 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5178 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5179 ErrorFound = true;
5180 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5181 Diag(DependFound->getLocStart(),
5182 diag::err_omp_ordered_directive_without_param);
5183 ErrorFound = true;
5184 } else if (TC || Clauses.empty()) {
5185 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5186 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5187 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5188 << (TC != nullptr);
5189 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5190 ErrorFound = true;
5191 }
5192 }
5193 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005194 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005195
5196 if (AStmt) {
5197 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5198
5199 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005200 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005201
5202 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005203}
5204
Alexey Bataev1d160b12015-03-13 12:27:31 +00005205namespace {
5206/// \brief Helper class for checking expression in 'omp atomic [update]'
5207/// construct.
5208class OpenMPAtomicUpdateChecker {
5209 /// \brief Error results for atomic update expressions.
5210 enum ExprAnalysisErrorCode {
5211 /// \brief A statement is not an expression statement.
5212 NotAnExpression,
5213 /// \brief Expression is not builtin binary or unary operation.
5214 NotABinaryOrUnaryExpression,
5215 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5216 NotAnUnaryIncDecExpression,
5217 /// \brief An expression is not of scalar type.
5218 NotAScalarType,
5219 /// \brief A binary operation is not an assignment operation.
5220 NotAnAssignmentOp,
5221 /// \brief RHS part of the binary operation is not a binary expression.
5222 NotABinaryExpression,
5223 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5224 /// expression.
5225 NotABinaryOperator,
5226 /// \brief RHS binary operation does not have reference to the updated LHS
5227 /// part.
5228 NotAnUpdateExpression,
5229 /// \brief No errors is found.
5230 NoError
5231 };
5232 /// \brief Reference to Sema.
5233 Sema &SemaRef;
5234 /// \brief A location for note diagnostics (when error is found).
5235 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005236 /// \brief 'x' lvalue part of the source atomic expression.
5237 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005238 /// \brief 'expr' rvalue part of the source atomic expression.
5239 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005240 /// \brief Helper expression of the form
5241 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5242 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5243 Expr *UpdateExpr;
5244 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5245 /// important for non-associative operations.
5246 bool IsXLHSInRHSPart;
5247 BinaryOperatorKind Op;
5248 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005249 /// \brief true if the source expression is a postfix unary operation, false
5250 /// if it is a prefix unary operation.
5251 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005252
5253public:
5254 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005255 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005256 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005257 /// \brief Check specified statement that it is suitable for 'atomic update'
5258 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005259 /// expression. If DiagId and NoteId == 0, then only check is performed
5260 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005261 /// \param DiagId Diagnostic which should be emitted if error is found.
5262 /// \param NoteId Diagnostic note for the main error message.
5263 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005264 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005265 /// \brief Return the 'x' lvalue part of the source atomic expression.
5266 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005267 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5268 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005269 /// \brief Return the update expression used in calculation of the updated
5270 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5271 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5272 Expr *getUpdateExpr() const { return UpdateExpr; }
5273 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5274 /// false otherwise.
5275 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5276
Alexey Bataevb78ca832015-04-01 03:33:17 +00005277 /// \brief true if the source expression is a postfix unary operation, false
5278 /// if it is a prefix unary operation.
5279 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5280
Alexey Bataev1d160b12015-03-13 12:27:31 +00005281private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005282 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5283 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005284};
5285} // namespace
5286
5287bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5288 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5289 ExprAnalysisErrorCode ErrorFound = NoError;
5290 SourceLocation ErrorLoc, NoteLoc;
5291 SourceRange ErrorRange, NoteRange;
5292 // Allowed constructs are:
5293 // x = x binop expr;
5294 // x = expr binop x;
5295 if (AtomicBinOp->getOpcode() == BO_Assign) {
5296 X = AtomicBinOp->getLHS();
5297 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5298 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5299 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5300 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5301 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005302 Op = AtomicInnerBinOp->getOpcode();
5303 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005304 auto *LHS = AtomicInnerBinOp->getLHS();
5305 auto *RHS = AtomicInnerBinOp->getRHS();
5306 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5307 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5308 /*Canonical=*/true);
5309 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5310 /*Canonical=*/true);
5311 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5312 /*Canonical=*/true);
5313 if (XId == LHSId) {
5314 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005315 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005316 } else if (XId == RHSId) {
5317 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005318 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005319 } else {
5320 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5321 ErrorRange = AtomicInnerBinOp->getSourceRange();
5322 NoteLoc = X->getExprLoc();
5323 NoteRange = X->getSourceRange();
5324 ErrorFound = NotAnUpdateExpression;
5325 }
5326 } else {
5327 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5328 ErrorRange = AtomicInnerBinOp->getSourceRange();
5329 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5330 NoteRange = SourceRange(NoteLoc, NoteLoc);
5331 ErrorFound = NotABinaryOperator;
5332 }
5333 } else {
5334 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5335 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5336 ErrorFound = NotABinaryExpression;
5337 }
5338 } else {
5339 ErrorLoc = AtomicBinOp->getExprLoc();
5340 ErrorRange = AtomicBinOp->getSourceRange();
5341 NoteLoc = AtomicBinOp->getOperatorLoc();
5342 NoteRange = SourceRange(NoteLoc, NoteLoc);
5343 ErrorFound = NotAnAssignmentOp;
5344 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005345 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005346 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5347 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5348 return true;
5349 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005350 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005351 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005352}
5353
5354bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5355 unsigned NoteId) {
5356 ExprAnalysisErrorCode ErrorFound = NoError;
5357 SourceLocation ErrorLoc, NoteLoc;
5358 SourceRange ErrorRange, NoteRange;
5359 // Allowed constructs are:
5360 // x++;
5361 // x--;
5362 // ++x;
5363 // --x;
5364 // x binop= expr;
5365 // x = x binop expr;
5366 // x = expr binop x;
5367 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5368 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5369 if (AtomicBody->getType()->isScalarType() ||
5370 AtomicBody->isInstantiationDependent()) {
5371 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5372 AtomicBody->IgnoreParenImpCasts())) {
5373 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005374 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005375 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005376 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005377 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005378 X = AtomicCompAssignOp->getLHS();
5379 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005380 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5381 AtomicBody->IgnoreParenImpCasts())) {
5382 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005383 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5384 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005385 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005386 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5387 // Check for Unary Operation
5388 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005389 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005390 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5391 OpLoc = AtomicUnaryOp->getOperatorLoc();
5392 X = AtomicUnaryOp->getSubExpr();
5393 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5394 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005395 } else {
5396 ErrorFound = NotAnUnaryIncDecExpression;
5397 ErrorLoc = AtomicUnaryOp->getExprLoc();
5398 ErrorRange = AtomicUnaryOp->getSourceRange();
5399 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5400 NoteRange = SourceRange(NoteLoc, NoteLoc);
5401 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005402 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005403 ErrorFound = NotABinaryOrUnaryExpression;
5404 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5405 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5406 }
5407 } else {
5408 ErrorFound = NotAScalarType;
5409 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5410 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5411 }
5412 } else {
5413 ErrorFound = NotAnExpression;
5414 NoteLoc = ErrorLoc = S->getLocStart();
5415 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5416 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005417 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005418 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5419 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5420 return true;
5421 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005422 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005423 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005424 // Build an update expression of form 'OpaqueValueExpr(x) binop
5425 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5426 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5427 auto *OVEX = new (SemaRef.getASTContext())
5428 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5429 auto *OVEExpr = new (SemaRef.getASTContext())
5430 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5431 auto Update =
5432 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5433 IsXLHSInRHSPart ? OVEExpr : OVEX);
5434 if (Update.isInvalid())
5435 return true;
5436 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5437 Sema::AA_Casting);
5438 if (Update.isInvalid())
5439 return true;
5440 UpdateExpr = Update.get();
5441 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005442 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005443}
5444
Alexey Bataev0162e452014-07-22 10:10:35 +00005445StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5446 Stmt *AStmt,
5447 SourceLocation StartLoc,
5448 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005449 if (!AStmt)
5450 return StmtError();
5451
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005452 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005453 // 1.2.2 OpenMP Language Terminology
5454 // Structured block - An executable statement with a single entry at the
5455 // top and a single exit at the bottom.
5456 // The point of exit cannot be a branch out of the structured block.
5457 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005458 OpenMPClauseKind AtomicKind = OMPC_unknown;
5459 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005460 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005461 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005462 C->getClauseKind() == OMPC_update ||
5463 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005464 if (AtomicKind != OMPC_unknown) {
5465 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5466 << SourceRange(C->getLocStart(), C->getLocEnd());
5467 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5468 << getOpenMPClauseName(AtomicKind);
5469 } else {
5470 AtomicKind = C->getClauseKind();
5471 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005472 }
5473 }
5474 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005475
Alexey Bataev459dec02014-07-24 06:46:57 +00005476 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005477 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5478 Body = EWC->getSubExpr();
5479
Alexey Bataev62cec442014-11-18 10:14:22 +00005480 Expr *X = nullptr;
5481 Expr *V = nullptr;
5482 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005483 Expr *UE = nullptr;
5484 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005485 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005486 // OpenMP [2.12.6, atomic Construct]
5487 // In the next expressions:
5488 // * x and v (as applicable) are both l-value expressions with scalar type.
5489 // * During the execution of an atomic region, multiple syntactic
5490 // occurrences of x must designate the same storage location.
5491 // * Neither of v and expr (as applicable) may access the storage location
5492 // designated by x.
5493 // * Neither of x and expr (as applicable) may access the storage location
5494 // designated by v.
5495 // * expr is an expression with scalar type.
5496 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5497 // * binop, binop=, ++, and -- are not overloaded operators.
5498 // * The expression x binop expr must be numerically equivalent to x binop
5499 // (expr). This requirement is satisfied if the operators in expr have
5500 // precedence greater than binop, or by using parentheses around expr or
5501 // subexpressions of expr.
5502 // * The expression expr binop x must be numerically equivalent to (expr)
5503 // binop x. This requirement is satisfied if the operators in expr have
5504 // precedence equal to or greater than binop, or by using parentheses around
5505 // expr or subexpressions of expr.
5506 // * For forms that allow multiple occurrences of x, the number of times
5507 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005508 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005509 enum {
5510 NotAnExpression,
5511 NotAnAssignmentOp,
5512 NotAScalarType,
5513 NotAnLValue,
5514 NoError
5515 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005516 SourceLocation ErrorLoc, NoteLoc;
5517 SourceRange ErrorRange, NoteRange;
5518 // If clause is read:
5519 // v = x;
5520 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5521 auto AtomicBinOp =
5522 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5523 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5524 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5525 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5526 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5527 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5528 if (!X->isLValue() || !V->isLValue()) {
5529 auto NotLValueExpr = X->isLValue() ? V : X;
5530 ErrorFound = NotAnLValue;
5531 ErrorLoc = AtomicBinOp->getExprLoc();
5532 ErrorRange = AtomicBinOp->getSourceRange();
5533 NoteLoc = NotLValueExpr->getExprLoc();
5534 NoteRange = NotLValueExpr->getSourceRange();
5535 }
5536 } else if (!X->isInstantiationDependent() ||
5537 !V->isInstantiationDependent()) {
5538 auto NotScalarExpr =
5539 (X->isInstantiationDependent() || X->getType()->isScalarType())
5540 ? V
5541 : X;
5542 ErrorFound = NotAScalarType;
5543 ErrorLoc = AtomicBinOp->getExprLoc();
5544 ErrorRange = AtomicBinOp->getSourceRange();
5545 NoteLoc = NotScalarExpr->getExprLoc();
5546 NoteRange = NotScalarExpr->getSourceRange();
5547 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005548 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005549 ErrorFound = NotAnAssignmentOp;
5550 ErrorLoc = AtomicBody->getExprLoc();
5551 ErrorRange = AtomicBody->getSourceRange();
5552 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5553 : AtomicBody->getExprLoc();
5554 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5555 : AtomicBody->getSourceRange();
5556 }
5557 } else {
5558 ErrorFound = NotAnExpression;
5559 NoteLoc = ErrorLoc = Body->getLocStart();
5560 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005561 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005562 if (ErrorFound != NoError) {
5563 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5564 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005565 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5566 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005567 return StmtError();
5568 } else if (CurContext->isDependentContext())
5569 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005570 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005571 enum {
5572 NotAnExpression,
5573 NotAnAssignmentOp,
5574 NotAScalarType,
5575 NotAnLValue,
5576 NoError
5577 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005578 SourceLocation ErrorLoc, NoteLoc;
5579 SourceRange ErrorRange, NoteRange;
5580 // If clause is write:
5581 // x = expr;
5582 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5583 auto AtomicBinOp =
5584 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5585 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005586 X = AtomicBinOp->getLHS();
5587 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005588 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5589 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5590 if (!X->isLValue()) {
5591 ErrorFound = NotAnLValue;
5592 ErrorLoc = AtomicBinOp->getExprLoc();
5593 ErrorRange = AtomicBinOp->getSourceRange();
5594 NoteLoc = X->getExprLoc();
5595 NoteRange = X->getSourceRange();
5596 }
5597 } else if (!X->isInstantiationDependent() ||
5598 !E->isInstantiationDependent()) {
5599 auto NotScalarExpr =
5600 (X->isInstantiationDependent() || X->getType()->isScalarType())
5601 ? E
5602 : X;
5603 ErrorFound = NotAScalarType;
5604 ErrorLoc = AtomicBinOp->getExprLoc();
5605 ErrorRange = AtomicBinOp->getSourceRange();
5606 NoteLoc = NotScalarExpr->getExprLoc();
5607 NoteRange = NotScalarExpr->getSourceRange();
5608 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005609 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005610 ErrorFound = NotAnAssignmentOp;
5611 ErrorLoc = AtomicBody->getExprLoc();
5612 ErrorRange = AtomicBody->getSourceRange();
5613 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5614 : AtomicBody->getExprLoc();
5615 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5616 : AtomicBody->getSourceRange();
5617 }
5618 } else {
5619 ErrorFound = NotAnExpression;
5620 NoteLoc = ErrorLoc = Body->getLocStart();
5621 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005622 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005623 if (ErrorFound != NoError) {
5624 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5625 << ErrorRange;
5626 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5627 << NoteRange;
5628 return StmtError();
5629 } else if (CurContext->isDependentContext())
5630 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005631 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005632 // If clause is update:
5633 // x++;
5634 // x--;
5635 // ++x;
5636 // --x;
5637 // x binop= expr;
5638 // x = x binop expr;
5639 // x = expr binop x;
5640 OpenMPAtomicUpdateChecker Checker(*this);
5641 if (Checker.checkStatement(
5642 Body, (AtomicKind == OMPC_update)
5643 ? diag::err_omp_atomic_update_not_expression_statement
5644 : diag::err_omp_atomic_not_expression_statement,
5645 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005646 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005647 if (!CurContext->isDependentContext()) {
5648 E = Checker.getExpr();
5649 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005650 UE = Checker.getUpdateExpr();
5651 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005652 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005653 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005654 enum {
5655 NotAnAssignmentOp,
5656 NotACompoundStatement,
5657 NotTwoSubstatements,
5658 NotASpecificExpression,
5659 NoError
5660 } ErrorFound = NoError;
5661 SourceLocation ErrorLoc, NoteLoc;
5662 SourceRange ErrorRange, NoteRange;
5663 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5664 // If clause is a capture:
5665 // v = x++;
5666 // v = x--;
5667 // v = ++x;
5668 // v = --x;
5669 // v = x binop= expr;
5670 // v = x = x binop expr;
5671 // v = x = expr binop x;
5672 auto *AtomicBinOp =
5673 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5674 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5675 V = AtomicBinOp->getLHS();
5676 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5677 OpenMPAtomicUpdateChecker Checker(*this);
5678 if (Checker.checkStatement(
5679 Body, diag::err_omp_atomic_capture_not_expression_statement,
5680 diag::note_omp_atomic_update))
5681 return StmtError();
5682 E = Checker.getExpr();
5683 X = Checker.getX();
5684 UE = Checker.getUpdateExpr();
5685 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5686 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005687 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005688 ErrorLoc = AtomicBody->getExprLoc();
5689 ErrorRange = AtomicBody->getSourceRange();
5690 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5691 : AtomicBody->getExprLoc();
5692 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5693 : AtomicBody->getSourceRange();
5694 ErrorFound = NotAnAssignmentOp;
5695 }
5696 if (ErrorFound != NoError) {
5697 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5698 << ErrorRange;
5699 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5700 return StmtError();
5701 } else if (CurContext->isDependentContext()) {
5702 UE = V = E = X = nullptr;
5703 }
5704 } else {
5705 // If clause is a capture:
5706 // { v = x; x = expr; }
5707 // { v = x; x++; }
5708 // { v = x; x--; }
5709 // { v = x; ++x; }
5710 // { v = x; --x; }
5711 // { v = x; x binop= expr; }
5712 // { v = x; x = x binop expr; }
5713 // { v = x; x = expr binop x; }
5714 // { x++; v = x; }
5715 // { x--; v = x; }
5716 // { ++x; v = x; }
5717 // { --x; v = x; }
5718 // { x binop= expr; v = x; }
5719 // { x = x binop expr; v = x; }
5720 // { x = expr binop x; v = x; }
5721 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5722 // Check that this is { expr1; expr2; }
5723 if (CS->size() == 2) {
5724 auto *First = CS->body_front();
5725 auto *Second = CS->body_back();
5726 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5727 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5728 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5729 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5730 // Need to find what subexpression is 'v' and what is 'x'.
5731 OpenMPAtomicUpdateChecker Checker(*this);
5732 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5733 BinaryOperator *BinOp = nullptr;
5734 if (IsUpdateExprFound) {
5735 BinOp = dyn_cast<BinaryOperator>(First);
5736 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5737 }
5738 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5739 // { v = x; x++; }
5740 // { v = x; x--; }
5741 // { v = x; ++x; }
5742 // { v = x; --x; }
5743 // { v = x; x binop= expr; }
5744 // { v = x; x = x binop expr; }
5745 // { v = x; x = expr binop x; }
5746 // Check that the first expression has form v = x.
5747 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5748 llvm::FoldingSetNodeID XId, PossibleXId;
5749 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5750 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5751 IsUpdateExprFound = XId == PossibleXId;
5752 if (IsUpdateExprFound) {
5753 V = BinOp->getLHS();
5754 X = Checker.getX();
5755 E = Checker.getExpr();
5756 UE = Checker.getUpdateExpr();
5757 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005758 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005759 }
5760 }
5761 if (!IsUpdateExprFound) {
5762 IsUpdateExprFound = !Checker.checkStatement(First);
5763 BinOp = nullptr;
5764 if (IsUpdateExprFound) {
5765 BinOp = dyn_cast<BinaryOperator>(Second);
5766 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5767 }
5768 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5769 // { x++; v = x; }
5770 // { x--; v = x; }
5771 // { ++x; v = x; }
5772 // { --x; v = x; }
5773 // { x binop= expr; v = x; }
5774 // { x = x binop expr; v = x; }
5775 // { x = expr binop x; v = x; }
5776 // Check that the second expression has form v = x.
5777 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5778 llvm::FoldingSetNodeID XId, PossibleXId;
5779 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5780 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5781 IsUpdateExprFound = XId == PossibleXId;
5782 if (IsUpdateExprFound) {
5783 V = BinOp->getLHS();
5784 X = Checker.getX();
5785 E = Checker.getExpr();
5786 UE = Checker.getUpdateExpr();
5787 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005788 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005789 }
5790 }
5791 }
5792 if (!IsUpdateExprFound) {
5793 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005794 auto *FirstExpr = dyn_cast<Expr>(First);
5795 auto *SecondExpr = dyn_cast<Expr>(Second);
5796 if (!FirstExpr || !SecondExpr ||
5797 !(FirstExpr->isInstantiationDependent() ||
5798 SecondExpr->isInstantiationDependent())) {
5799 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5800 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005801 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005802 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5803 : First->getLocStart();
5804 NoteRange = ErrorRange = FirstBinOp
5805 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005806 : SourceRange(ErrorLoc, ErrorLoc);
5807 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005808 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5809 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5810 ErrorFound = NotAnAssignmentOp;
5811 NoteLoc = ErrorLoc = SecondBinOp
5812 ? SecondBinOp->getOperatorLoc()
5813 : Second->getLocStart();
5814 NoteRange = ErrorRange =
5815 SecondBinOp ? SecondBinOp->getSourceRange()
5816 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005817 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005818 auto *PossibleXRHSInFirst =
5819 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5820 auto *PossibleXLHSInSecond =
5821 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5822 llvm::FoldingSetNodeID X1Id, X2Id;
5823 PossibleXRHSInFirst->Profile(X1Id, Context,
5824 /*Canonical=*/true);
5825 PossibleXLHSInSecond->Profile(X2Id, Context,
5826 /*Canonical=*/true);
5827 IsUpdateExprFound = X1Id == X2Id;
5828 if (IsUpdateExprFound) {
5829 V = FirstBinOp->getLHS();
5830 X = SecondBinOp->getLHS();
5831 E = SecondBinOp->getRHS();
5832 UE = nullptr;
5833 IsXLHSInRHSPart = false;
5834 IsPostfixUpdate = true;
5835 } else {
5836 ErrorFound = NotASpecificExpression;
5837 ErrorLoc = FirstBinOp->getExprLoc();
5838 ErrorRange = FirstBinOp->getSourceRange();
5839 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5840 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5841 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005842 }
5843 }
5844 }
5845 }
5846 } else {
5847 NoteLoc = ErrorLoc = Body->getLocStart();
5848 NoteRange = ErrorRange =
5849 SourceRange(Body->getLocStart(), Body->getLocStart());
5850 ErrorFound = NotTwoSubstatements;
5851 }
5852 } else {
5853 NoteLoc = ErrorLoc = Body->getLocStart();
5854 NoteRange = ErrorRange =
5855 SourceRange(Body->getLocStart(), Body->getLocStart());
5856 ErrorFound = NotACompoundStatement;
5857 }
5858 if (ErrorFound != NoError) {
5859 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5860 << ErrorRange;
5861 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5862 return StmtError();
5863 } else if (CurContext->isDependentContext()) {
5864 UE = V = E = X = nullptr;
5865 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005866 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005867 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005868
5869 getCurFunction()->setHasBranchProtectedScope();
5870
Alexey Bataev62cec442014-11-18 10:14:22 +00005871 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005872 X, V, E, UE, IsXLHSInRHSPart,
5873 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005874}
5875
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005876StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5877 Stmt *AStmt,
5878 SourceLocation StartLoc,
5879 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005880 if (!AStmt)
5881 return StmtError();
5882
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005883 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5884 // 1.2.2 OpenMP Language Terminology
5885 // Structured block - An executable statement with a single entry at the
5886 // top and a single exit at the bottom.
5887 // The point of exit cannot be a branch out of the structured block.
5888 // longjmp() and throw() must not violate the entry/exit criteria.
5889 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005890
Alexey Bataev13314bf2014-10-09 04:18:56 +00005891 // OpenMP [2.16, Nesting of Regions]
5892 // If specified, a teams construct must be contained within a target
5893 // construct. That target construct must contain no statements or directives
5894 // outside of the teams construct.
5895 if (DSAStack->hasInnerTeamsRegion()) {
5896 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5897 bool OMPTeamsFound = true;
5898 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5899 auto I = CS->body_begin();
5900 while (I != CS->body_end()) {
5901 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5902 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5903 OMPTeamsFound = false;
5904 break;
5905 }
5906 ++I;
5907 }
5908 assert(I != CS->body_end() && "Not found statement");
5909 S = *I;
5910 }
5911 if (!OMPTeamsFound) {
5912 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5913 Diag(DSAStack->getInnerTeamsRegionLoc(),
5914 diag::note_omp_nested_teams_construct_here);
5915 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5916 << isa<OMPExecutableDirective>(S);
5917 return StmtError();
5918 }
5919 }
5920
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005921 getCurFunction()->setHasBranchProtectedScope();
5922
5923 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5924}
5925
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005926StmtResult
5927Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5928 Stmt *AStmt, SourceLocation StartLoc,
5929 SourceLocation EndLoc) {
5930 if (!AStmt)
5931 return StmtError();
5932
5933 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5934 // 1.2.2 OpenMP Language Terminology
5935 // Structured block - An executable statement with a single entry at the
5936 // top and a single exit at the bottom.
5937 // The point of exit cannot be a branch out of the structured block.
5938 // longjmp() and throw() must not violate the entry/exit criteria.
5939 CS->getCapturedDecl()->setNothrow();
5940
5941 getCurFunction()->setHasBranchProtectedScope();
5942
5943 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5944 AStmt);
5945}
5946
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005947StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5949 SourceLocation EndLoc,
5950 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5951 if (!AStmt)
5952 return StmtError();
5953
5954 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5955 // 1.2.2 OpenMP Language Terminology
5956 // Structured block - An executable statement with a single entry at the
5957 // top and a single exit at the bottom.
5958 // The point of exit cannot be a branch out of the structured block.
5959 // longjmp() and throw() must not violate the entry/exit criteria.
5960 CS->getCapturedDecl()->setNothrow();
5961
5962 OMPLoopDirective::HelperExprs B;
5963 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5964 // define the nested loops number.
5965 unsigned NestedLoopCount =
5966 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5967 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5968 VarsWithImplicitDSA, B);
5969 if (NestedLoopCount == 0)
5970 return StmtError();
5971
5972 assert((CurContext->isDependentContext() || B.builtAll()) &&
5973 "omp target parallel for loop exprs were not built");
5974
5975 if (!CurContext->isDependentContext()) {
5976 // Finalize the clauses that need pre-built expressions for CodeGen.
5977 for (auto C : Clauses) {
5978 if (auto LC = dyn_cast<OMPLinearClause>(C))
5979 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5980 B.NumIterations, *this, CurScope))
5981 return StmtError();
5982 }
5983 }
5984
5985 getCurFunction()->setHasBranchProtectedScope();
5986 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5987 NestedLoopCount, Clauses, AStmt,
5988 B, DSAStack->isCancelRegion());
5989}
5990
Samuel Antaodf67fc42016-01-19 19:15:56 +00005991/// \brief Check for existence of a map clause in the list of clauses.
5992static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5993 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5994 I != E; ++I) {
5995 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5996 return true;
5997 }
5998 }
5999
6000 return false;
6001}
6002
Michael Wong65f367f2015-07-21 13:44:28 +00006003StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6004 Stmt *AStmt,
6005 SourceLocation StartLoc,
6006 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006007 if (!AStmt)
6008 return StmtError();
6009
6010 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6011
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006012 // OpenMP [2.10.1, Restrictions, p. 97]
6013 // At least one map clause must appear on the directive.
6014 if (!HasMapClause(Clauses)) {
6015 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6016 getOpenMPDirectiveName(OMPD_target_data);
6017 return StmtError();
6018 }
6019
Michael Wong65f367f2015-07-21 13:44:28 +00006020 getCurFunction()->setHasBranchProtectedScope();
6021
6022 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6023 AStmt);
6024}
6025
Samuel Antaodf67fc42016-01-19 19:15:56 +00006026StmtResult
6027Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6028 SourceLocation StartLoc,
6029 SourceLocation EndLoc) {
6030 // OpenMP [2.10.2, Restrictions, p. 99]
6031 // At least one map clause must appear on the directive.
6032 if (!HasMapClause(Clauses)) {
6033 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6034 << getOpenMPDirectiveName(OMPD_target_enter_data);
6035 return StmtError();
6036 }
6037
6038 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6039 Clauses);
6040}
6041
Samuel Antao72590762016-01-19 20:04:50 +00006042StmtResult
6043Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6044 SourceLocation StartLoc,
6045 SourceLocation EndLoc) {
6046 // OpenMP [2.10.3, Restrictions, p. 102]
6047 // At least one map clause must appear on the directive.
6048 if (!HasMapClause(Clauses)) {
6049 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6050 << getOpenMPDirectiveName(OMPD_target_exit_data);
6051 return StmtError();
6052 }
6053
6054 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6055}
6056
Alexey Bataev13314bf2014-10-09 04:18:56 +00006057StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6058 Stmt *AStmt, SourceLocation StartLoc,
6059 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006060 if (!AStmt)
6061 return StmtError();
6062
Alexey Bataev13314bf2014-10-09 04:18:56 +00006063 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6064 // 1.2.2 OpenMP Language Terminology
6065 // Structured block - An executable statement with a single entry at the
6066 // top and a single exit at the bottom.
6067 // The point of exit cannot be a branch out of the structured block.
6068 // longjmp() and throw() must not violate the entry/exit criteria.
6069 CS->getCapturedDecl()->setNothrow();
6070
6071 getCurFunction()->setHasBranchProtectedScope();
6072
6073 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6074}
6075
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006076StmtResult
6077Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6078 SourceLocation EndLoc,
6079 OpenMPDirectiveKind CancelRegion) {
6080 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6081 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6082 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6083 << getOpenMPDirectiveName(CancelRegion);
6084 return StmtError();
6085 }
6086 if (DSAStack->isParentNowaitRegion()) {
6087 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6088 return StmtError();
6089 }
6090 if (DSAStack->isParentOrderedRegion()) {
6091 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6092 return StmtError();
6093 }
6094 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6095 CancelRegion);
6096}
6097
Alexey Bataev87933c72015-09-18 08:07:34 +00006098StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6099 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006100 SourceLocation EndLoc,
6101 OpenMPDirectiveKind CancelRegion) {
6102 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6103 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6104 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6105 << getOpenMPDirectiveName(CancelRegion);
6106 return StmtError();
6107 }
6108 if (DSAStack->isParentNowaitRegion()) {
6109 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6110 return StmtError();
6111 }
6112 if (DSAStack->isParentOrderedRegion()) {
6113 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6114 return StmtError();
6115 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006116 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006117 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6118 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006119}
6120
Alexey Bataev382967a2015-12-08 12:06:20 +00006121static bool checkGrainsizeNumTasksClauses(Sema &S,
6122 ArrayRef<OMPClause *> Clauses) {
6123 OMPClause *PrevClause = nullptr;
6124 bool ErrorFound = false;
6125 for (auto *C : Clauses) {
6126 if (C->getClauseKind() == OMPC_grainsize ||
6127 C->getClauseKind() == OMPC_num_tasks) {
6128 if (!PrevClause)
6129 PrevClause = C;
6130 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6131 S.Diag(C->getLocStart(),
6132 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6133 << getOpenMPClauseName(C->getClauseKind())
6134 << getOpenMPClauseName(PrevClause->getClauseKind());
6135 S.Diag(PrevClause->getLocStart(),
6136 diag::note_omp_previous_grainsize_num_tasks)
6137 << getOpenMPClauseName(PrevClause->getClauseKind());
6138 ErrorFound = true;
6139 }
6140 }
6141 }
6142 return ErrorFound;
6143}
6144
Alexey Bataev49f6e782015-12-01 04:18:41 +00006145StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6146 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6147 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006148 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006149 if (!AStmt)
6150 return StmtError();
6151
6152 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6153 OMPLoopDirective::HelperExprs B;
6154 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6155 // define the nested loops number.
6156 unsigned NestedLoopCount =
6157 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006158 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006159 VarsWithImplicitDSA, B);
6160 if (NestedLoopCount == 0)
6161 return StmtError();
6162
6163 assert((CurContext->isDependentContext() || B.builtAll()) &&
6164 "omp for loop exprs were not built");
6165
Alexey Bataev382967a2015-12-08 12:06:20 +00006166 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6167 // The grainsize clause and num_tasks clause are mutually exclusive and may
6168 // not appear on the same taskloop directive.
6169 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6170 return StmtError();
6171
Alexey Bataev49f6e782015-12-01 04:18:41 +00006172 getCurFunction()->setHasBranchProtectedScope();
6173 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6174 NestedLoopCount, Clauses, AStmt, B);
6175}
6176
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006177StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6178 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6179 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006180 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006181 if (!AStmt)
6182 return StmtError();
6183
6184 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6185 OMPLoopDirective::HelperExprs B;
6186 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6187 // define the nested loops number.
6188 unsigned NestedLoopCount =
6189 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6190 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6191 VarsWithImplicitDSA, B);
6192 if (NestedLoopCount == 0)
6193 return StmtError();
6194
6195 assert((CurContext->isDependentContext() || B.builtAll()) &&
6196 "omp for loop exprs were not built");
6197
Alexey Bataev382967a2015-12-08 12:06:20 +00006198 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6199 // The grainsize clause and num_tasks clause are mutually exclusive and may
6200 // not appear on the same taskloop directive.
6201 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6202 return StmtError();
6203
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006204 getCurFunction()->setHasBranchProtectedScope();
6205 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6206 NestedLoopCount, Clauses, AStmt, B);
6207}
6208
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006209StmtResult Sema::ActOnOpenMPDistributeDirective(
6210 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6211 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006212 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006213 if (!AStmt)
6214 return StmtError();
6215
6216 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6217 OMPLoopDirective::HelperExprs B;
6218 // In presence of clause 'collapse' with number of loops, it will
6219 // define the nested loops number.
6220 unsigned NestedLoopCount =
6221 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6222 nullptr /*ordered not a clause on distribute*/, AStmt,
6223 *this, *DSAStack, VarsWithImplicitDSA, B);
6224 if (NestedLoopCount == 0)
6225 return StmtError();
6226
6227 assert((CurContext->isDependentContext() || B.builtAll()) &&
6228 "omp for loop exprs were not built");
6229
6230 getCurFunction()->setHasBranchProtectedScope();
6231 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6232 NestedLoopCount, Clauses, AStmt, B);
6233}
6234
Alexey Bataeved09d242014-05-28 05:53:51 +00006235OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006236 SourceLocation StartLoc,
6237 SourceLocation LParenLoc,
6238 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006239 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006240 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006241 case OMPC_final:
6242 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6243 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006244 case OMPC_num_threads:
6245 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6246 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006247 case OMPC_safelen:
6248 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6249 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006250 case OMPC_simdlen:
6251 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6252 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006253 case OMPC_collapse:
6254 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6255 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006256 case OMPC_ordered:
6257 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6258 break;
Michael Wonge710d542015-08-07 16:16:36 +00006259 case OMPC_device:
6260 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6261 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006262 case OMPC_num_teams:
6263 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6264 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006265 case OMPC_thread_limit:
6266 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6267 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006268 case OMPC_priority:
6269 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6270 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006271 case OMPC_grainsize:
6272 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6273 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006274 case OMPC_num_tasks:
6275 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6276 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006277 case OMPC_hint:
6278 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6279 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006280 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006281 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006282 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006283 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006284 case OMPC_private:
6285 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006286 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006287 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006288 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006289 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006290 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006291 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006292 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006293 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006294 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006295 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006296 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006297 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006298 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006299 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006300 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006301 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006302 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006303 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006304 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006305 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006306 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006307 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006308 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006309 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006310 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006311 llvm_unreachable("Clause is not allowed.");
6312 }
6313 return Res;
6314}
6315
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006316OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6317 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006318 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006319 SourceLocation NameModifierLoc,
6320 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006321 SourceLocation EndLoc) {
6322 Expr *ValExpr = Condition;
6323 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6324 !Condition->isInstantiationDependent() &&
6325 !Condition->containsUnexpandedParameterPack()) {
6326 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006327 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006328 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006329 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006330
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006331 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006332 }
6333
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006334 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6335 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006336}
6337
Alexey Bataev3778b602014-07-17 07:32:53 +00006338OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6339 SourceLocation StartLoc,
6340 SourceLocation LParenLoc,
6341 SourceLocation EndLoc) {
6342 Expr *ValExpr = Condition;
6343 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6344 !Condition->isInstantiationDependent() &&
6345 !Condition->containsUnexpandedParameterPack()) {
6346 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6347 Condition->getExprLoc(), Condition);
6348 if (Val.isInvalid())
6349 return nullptr;
6350
6351 ValExpr = Val.get();
6352 }
6353
6354 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6355}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006356ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6357 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006358 if (!Op)
6359 return ExprError();
6360
6361 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6362 public:
6363 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006364 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006365 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6366 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006367 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6368 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006369 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6370 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006371 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6372 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006373 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6374 QualType T,
6375 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006376 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6377 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006378 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6379 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006380 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006381 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006382 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006383 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6384 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006385 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6386 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006387 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6388 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006389 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006390 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006391 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006392 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6393 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006394 llvm_unreachable("conversion functions are permitted");
6395 }
6396 } ConvertDiagnoser;
6397 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6398}
6399
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006400static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006401 OpenMPClauseKind CKind,
6402 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006403 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6404 !ValExpr->isInstantiationDependent()) {
6405 SourceLocation Loc = ValExpr->getExprLoc();
6406 ExprResult Value =
6407 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6408 if (Value.isInvalid())
6409 return false;
6410
6411 ValExpr = Value.get();
6412 // The expression must evaluate to a non-negative integer value.
6413 llvm::APSInt Result;
6414 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006415 Result.isSigned() &&
6416 !((!StrictlyPositive && Result.isNonNegative()) ||
6417 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006418 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006419 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6420 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006421 return false;
6422 }
6423 }
6424 return true;
6425}
6426
Alexey Bataev568a8332014-03-06 06:15:19 +00006427OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6428 SourceLocation StartLoc,
6429 SourceLocation LParenLoc,
6430 SourceLocation EndLoc) {
6431 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006432
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006433 // OpenMP [2.5, Restrictions]
6434 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006435 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6436 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006437 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006438
Alexey Bataeved09d242014-05-28 05:53:51 +00006439 return new (Context)
6440 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006441}
6442
Alexey Bataev62c87d22014-03-21 04:51:18 +00006443ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006444 OpenMPClauseKind CKind,
6445 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006446 if (!E)
6447 return ExprError();
6448 if (E->isValueDependent() || E->isTypeDependent() ||
6449 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006450 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006451 llvm::APSInt Result;
6452 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6453 if (ICE.isInvalid())
6454 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006455 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6456 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006457 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006458 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6459 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006460 return ExprError();
6461 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006462 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6463 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6464 << E->getSourceRange();
6465 return ExprError();
6466 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006467 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6468 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006469 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006470 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006471 return ICE;
6472}
6473
6474OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6475 SourceLocation LParenLoc,
6476 SourceLocation EndLoc) {
6477 // OpenMP [2.8.1, simd construct, Description]
6478 // The parameter of the safelen clause must be a constant
6479 // positive integer expression.
6480 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6481 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006482 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006483 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006484 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006485}
6486
Alexey Bataev66b15b52015-08-21 11:14:16 +00006487OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6488 SourceLocation LParenLoc,
6489 SourceLocation EndLoc) {
6490 // OpenMP [2.8.1, simd construct, Description]
6491 // The parameter of the simdlen clause must be a constant
6492 // positive integer expression.
6493 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6494 if (Simdlen.isInvalid())
6495 return nullptr;
6496 return new (Context)
6497 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6498}
6499
Alexander Musman64d33f12014-06-04 07:53:32 +00006500OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6501 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006502 SourceLocation LParenLoc,
6503 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006504 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006505 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006506 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006507 // The parameter of the collapse clause must be a constant
6508 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006509 ExprResult NumForLoopsResult =
6510 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6511 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006512 return nullptr;
6513 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006514 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006515}
6516
Alexey Bataev10e775f2015-07-30 11:36:16 +00006517OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6518 SourceLocation EndLoc,
6519 SourceLocation LParenLoc,
6520 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006521 // OpenMP [2.7.1, loop construct, Description]
6522 // OpenMP [2.8.1, simd construct, Description]
6523 // OpenMP [2.9.6, distribute construct, Description]
6524 // The parameter of the ordered clause must be a constant
6525 // positive integer expression if any.
6526 if (NumForLoops && LParenLoc.isValid()) {
6527 ExprResult NumForLoopsResult =
6528 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6529 if (NumForLoopsResult.isInvalid())
6530 return nullptr;
6531 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006532 } else
6533 NumForLoops = nullptr;
6534 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006535 return new (Context)
6536 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6537}
6538
Alexey Bataeved09d242014-05-28 05:53:51 +00006539OMPClause *Sema::ActOnOpenMPSimpleClause(
6540 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6541 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006542 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006543 switch (Kind) {
6544 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006545 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006546 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6547 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006548 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006549 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006550 Res = ActOnOpenMPProcBindClause(
6551 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6552 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006553 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006554 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006555 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006556 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006557 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006558 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006559 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006560 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006561 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006562 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006563 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006564 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006565 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006566 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006567 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006568 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006569 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006570 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006571 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006572 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006573 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006574 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006575 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006576 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006577 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006578 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006579 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006580 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006581 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006582 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006583 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006584 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006585 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006586 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006587 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006588 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006589 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006590 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006591 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006592 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006593 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006594 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006595 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006596 llvm_unreachable("Clause is not allowed.");
6597 }
6598 return Res;
6599}
6600
Alexey Bataev6402bca2015-12-28 07:25:51 +00006601static std::string
6602getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6603 ArrayRef<unsigned> Exclude = llvm::None) {
6604 std::string Values;
6605 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6606 unsigned Skipped = Exclude.size();
6607 auto S = Exclude.begin(), E = Exclude.end();
6608 for (unsigned i = First; i < Last; ++i) {
6609 if (std::find(S, E, i) != E) {
6610 --Skipped;
6611 continue;
6612 }
6613 Values += "'";
6614 Values += getOpenMPSimpleClauseTypeName(K, i);
6615 Values += "'";
6616 if (i == Bound - Skipped)
6617 Values += " or ";
6618 else if (i != Bound + 1 - Skipped)
6619 Values += ", ";
6620 }
6621 return Values;
6622}
6623
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006624OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6625 SourceLocation KindKwLoc,
6626 SourceLocation StartLoc,
6627 SourceLocation LParenLoc,
6628 SourceLocation EndLoc) {
6629 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006630 static_assert(OMPC_DEFAULT_unknown > 0,
6631 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006632 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006633 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6634 /*Last=*/OMPC_DEFAULT_unknown)
6635 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006636 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006637 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006638 switch (Kind) {
6639 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006640 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006641 break;
6642 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006643 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006644 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006645 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006646 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006647 break;
6648 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006649 return new (Context)
6650 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006651}
6652
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006653OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6654 SourceLocation KindKwLoc,
6655 SourceLocation StartLoc,
6656 SourceLocation LParenLoc,
6657 SourceLocation EndLoc) {
6658 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006659 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006660 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6661 /*Last=*/OMPC_PROC_BIND_unknown)
6662 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006663 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006664 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006665 return new (Context)
6666 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006667}
6668
Alexey Bataev56dafe82014-06-20 07:16:17 +00006669OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006670 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006671 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006672 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006673 SourceLocation EndLoc) {
6674 OMPClause *Res = nullptr;
6675 switch (Kind) {
6676 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006677 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6678 assert(Argument.size() == NumberOfElements &&
6679 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006680 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006681 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6682 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6683 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6684 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6685 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006686 break;
6687 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006688 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6689 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6690 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6691 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006692 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006693 case OMPC_dist_schedule:
6694 Res = ActOnOpenMPDistScheduleClause(
6695 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6696 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6697 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006698 case OMPC_defaultmap:
6699 enum { Modifier, DefaultmapKind };
6700 Res = ActOnOpenMPDefaultmapClause(
6701 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6702 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6703 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6704 ArgumentLoc[DefaultmapKind], EndLoc);
6705 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006706 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006707 case OMPC_num_threads:
6708 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006709 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006710 case OMPC_collapse:
6711 case OMPC_default:
6712 case OMPC_proc_bind:
6713 case OMPC_private:
6714 case OMPC_firstprivate:
6715 case OMPC_lastprivate:
6716 case OMPC_shared:
6717 case OMPC_reduction:
6718 case OMPC_linear:
6719 case OMPC_aligned:
6720 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006721 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006722 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006723 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006724 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006725 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006726 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006727 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006728 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006729 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006730 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006731 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006732 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006733 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006734 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006735 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006736 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006737 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006738 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006739 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006740 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006741 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006742 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006743 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006744 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006745 case OMPC_unknown:
6746 llvm_unreachable("Clause is not allowed.");
6747 }
6748 return Res;
6749}
6750
Alexey Bataev6402bca2015-12-28 07:25:51 +00006751static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6752 OpenMPScheduleClauseModifier M2,
6753 SourceLocation M1Loc, SourceLocation M2Loc) {
6754 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6755 SmallVector<unsigned, 2> Excluded;
6756 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6757 Excluded.push_back(M2);
6758 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6759 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6760 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6761 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6762 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6763 << getListOfPossibleValues(OMPC_schedule,
6764 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6765 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6766 Excluded)
6767 << getOpenMPClauseName(OMPC_schedule);
6768 return true;
6769 }
6770 return false;
6771}
6772
Alexey Bataev56dafe82014-06-20 07:16:17 +00006773OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006774 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006775 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006776 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6777 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6778 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6779 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6780 return nullptr;
6781 // OpenMP, 2.7.1, Loop Construct, Restrictions
6782 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6783 // but not both.
6784 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6785 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6786 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6787 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6788 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6789 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6790 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6791 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6792 return nullptr;
6793 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006794 if (Kind == OMPC_SCHEDULE_unknown) {
6795 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006796 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6797 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6798 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6799 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6800 Exclude);
6801 } else {
6802 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6803 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006804 }
6805 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6806 << Values << getOpenMPClauseName(OMPC_schedule);
6807 return nullptr;
6808 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006809 // OpenMP, 2.7.1, Loop Construct, Restrictions
6810 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6811 // schedule(guided).
6812 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6813 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6814 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6815 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6816 diag::err_omp_schedule_nonmonotonic_static);
6817 return nullptr;
6818 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006819 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006820 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006821 if (ChunkSize) {
6822 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6823 !ChunkSize->isInstantiationDependent() &&
6824 !ChunkSize->containsUnexpandedParameterPack()) {
6825 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6826 ExprResult Val =
6827 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6828 if (Val.isInvalid())
6829 return nullptr;
6830
6831 ValExpr = Val.get();
6832
6833 // OpenMP [2.7.1, Restrictions]
6834 // chunk_size must be a loop invariant integer expression with a positive
6835 // value.
6836 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006837 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6838 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6839 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006840 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006841 return nullptr;
6842 }
6843 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev3392d762016-02-16 11:18:12 +00006844 ValExpr = buildCapture(*this, ".chunk.", ValExpr);
6845 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
6846 HelperValStmt =
6847 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
6848 /*NumDecls=*/1),
6849 SourceLocation(), SourceLocation());
6850 ValExpr = DefaultLvalueConversion(ValExpr).get();
Alexey Bataev56dafe82014-06-20 07:16:17 +00006851 }
6852 }
6853 }
6854
Alexey Bataev6402bca2015-12-28 07:25:51 +00006855 return new (Context)
6856 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006857 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006858}
6859
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006860OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6861 SourceLocation StartLoc,
6862 SourceLocation EndLoc) {
6863 OMPClause *Res = nullptr;
6864 switch (Kind) {
6865 case OMPC_ordered:
6866 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6867 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006868 case OMPC_nowait:
6869 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6870 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006871 case OMPC_untied:
6872 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6873 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006874 case OMPC_mergeable:
6875 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6876 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006877 case OMPC_read:
6878 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6879 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006880 case OMPC_write:
6881 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6882 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006883 case OMPC_update:
6884 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6885 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006886 case OMPC_capture:
6887 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6888 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006889 case OMPC_seq_cst:
6890 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6891 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006892 case OMPC_threads:
6893 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6894 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006895 case OMPC_simd:
6896 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6897 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006898 case OMPC_nogroup:
6899 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6900 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006901 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006902 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006903 case OMPC_num_threads:
6904 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006905 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006906 case OMPC_collapse:
6907 case OMPC_schedule:
6908 case OMPC_private:
6909 case OMPC_firstprivate:
6910 case OMPC_lastprivate:
6911 case OMPC_shared:
6912 case OMPC_reduction:
6913 case OMPC_linear:
6914 case OMPC_aligned:
6915 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006916 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006917 case OMPC_default:
6918 case OMPC_proc_bind:
6919 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006920 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006921 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006922 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006923 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006924 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006925 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006926 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006927 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006928 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006929 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006930 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006931 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006932 case OMPC_unknown:
6933 llvm_unreachable("Clause is not allowed.");
6934 }
6935 return Res;
6936}
6937
Alexey Bataev236070f2014-06-20 11:19:47 +00006938OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6939 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006940 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006941 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6942}
6943
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006944OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6945 SourceLocation EndLoc) {
6946 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6947}
6948
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006949OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6950 SourceLocation EndLoc) {
6951 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6952}
6953
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006954OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6955 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006956 return new (Context) OMPReadClause(StartLoc, EndLoc);
6957}
6958
Alexey Bataevdea47612014-07-23 07:46:59 +00006959OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6960 SourceLocation EndLoc) {
6961 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6962}
6963
Alexey Bataev67a4f222014-07-23 10:25:33 +00006964OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6965 SourceLocation EndLoc) {
6966 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6967}
6968
Alexey Bataev459dec02014-07-24 06:46:57 +00006969OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6970 SourceLocation EndLoc) {
6971 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6972}
6973
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006974OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6975 SourceLocation EndLoc) {
6976 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6977}
6978
Alexey Bataev346265e2015-09-25 10:37:12 +00006979OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6980 SourceLocation EndLoc) {
6981 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6982}
6983
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006984OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6985 SourceLocation EndLoc) {
6986 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6987}
6988
Alexey Bataevb825de12015-12-07 10:51:44 +00006989OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6990 SourceLocation EndLoc) {
6991 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6992}
6993
Alexey Bataevc5e02582014-06-16 07:08:35 +00006994OMPClause *Sema::ActOnOpenMPVarListClause(
6995 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6996 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6997 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006998 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006999 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7000 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7001 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007002 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007003 switch (Kind) {
7004 case OMPC_private:
7005 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7006 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007007 case OMPC_firstprivate:
7008 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7009 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007010 case OMPC_lastprivate:
7011 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7012 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007013 case OMPC_shared:
7014 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7015 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007016 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007017 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7018 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007019 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007020 case OMPC_linear:
7021 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007022 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007023 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007024 case OMPC_aligned:
7025 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7026 ColonLoc, EndLoc);
7027 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007028 case OMPC_copyin:
7029 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7030 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007031 case OMPC_copyprivate:
7032 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7033 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007034 case OMPC_flush:
7035 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7036 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007037 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007038 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7039 StartLoc, LParenLoc, EndLoc);
7040 break;
7041 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007042 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7043 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7044 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007045 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007046 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007047 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007048 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007049 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007050 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007051 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007052 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007053 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007054 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007055 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007056 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007057 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007058 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007059 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007060 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007061 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007062 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007063 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007064 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007065 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007066 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007067 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007068 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007069 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007070 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007071 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007072 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007073 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007074 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007075 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007076 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007077 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007078 llvm_unreachable("Clause is not allowed.");
7079 }
7080 return Res;
7081}
7082
Alexey Bataev90c228f2016-02-08 09:29:13 +00007083ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7084 ExprObjectKind OK) {
7085 SourceLocation Loc = Capture->getInit()->getExprLoc();
7086 ExprResult Res = BuildDeclRefExpr(
7087 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7088 if (!Res.isUsable())
7089 return ExprError();
7090 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7091 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7092 if (!Res.isUsable())
7093 return ExprError();
7094 }
7095 if (VK != VK_LValue && Res.get()->isGLValue()) {
7096 Res = DefaultLvalueConversion(Res.get());
7097 if (!Res.isUsable())
7098 return ExprError();
7099 }
7100 return Res;
7101}
7102
Alexey Bataevd985eda2016-02-10 11:29:16 +00007103static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *RefExpr) {
7104 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7105 RefExpr->containsUnexpandedParameterPack())
7106 return std::make_pair(nullptr, true);
7107
7108 SourceLocation ELoc = RefExpr->getExprLoc();
7109 SourceRange SR = RefExpr->getSourceRange();
7110 // OpenMP [3.1, C/C++]
7111 // A list item is a variable name.
7112 // OpenMP [2.9.3.3, Restrictions, p.1]
7113 // A variable that is part of another variable (as an array or
7114 // structure element) cannot appear in a private clause.
7115 RefExpr = RefExpr->IgnoreParens();
7116 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7117 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7118 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7119 (S.getCurrentThisType().isNull() || !ME ||
7120 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7121 !isa<FieldDecl>(ME->getMemberDecl()))) {
7122 S.Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7123 << (S.getCurrentThisType().isNull() ? 0 : 1) << SR;
7124 return std::make_pair(nullptr, false);
7125 }
7126 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7127}
7128
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007129OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7130 SourceLocation StartLoc,
7131 SourceLocation LParenLoc,
7132 SourceLocation EndLoc) {
7133 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007134 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007135 for (auto &RefExpr : VarList) {
7136 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007137 auto Res = getPrivateItem(*this, RefExpr);
7138 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007139 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007140 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007141 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007142 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007143 ValueDecl *D = Res.first;
7144 if (!D)
7145 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007146
Alexey Bataeved09d242014-05-28 05:53:51 +00007147 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007148 QualType Type = D->getType();
7149 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007150
7151 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7152 // A variable that appears in a private clause must not have an incomplete
7153 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007154 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007155 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007156 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007157
Alexey Bataev758e55e2013-09-06 18:03:48 +00007158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7159 // in a Construct]
7160 // Variables with the predetermined data-sharing attributes may not be
7161 // listed in data-sharing attributes clauses, except for the cases
7162 // listed below. For these exceptions only, listing a predetermined
7163 // variable in a data-sharing attribute clause is allowed and overrides
7164 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007165 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007166 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007167 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7168 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007169 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007170 continue;
7171 }
7172
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007173 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007174 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007175 DSAStack->getCurrentDirective() == OMPD_task) {
7176 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7177 << getOpenMPClauseName(OMPC_private) << Type
7178 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7179 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007180 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007181 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007182 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007183 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007184 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007185 continue;
7186 }
7187
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007188 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7189 // A variable of class type (or array thereof) that appears in a private
7190 // clause requires an accessible, unambiguous default constructor for the
7191 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007192 // Generate helper private variable and initialize it with the default
7193 // value. The address of the original variable is replaced by the address of
7194 // the new private variable in CodeGen. This new variable is not added to
7195 // IdResolver, so the code in the OpenMP region uses original variable for
7196 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007197 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007198 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7199 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007200 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007201 if (VDPrivate->isInvalidDecl())
7202 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007203 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007204 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007205
Alexey Bataev90c228f2016-02-08 09:29:13 +00007206 DeclRefExpr *Ref = nullptr;
7207 if (!VD)
7208 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7209 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7210 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007211 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007212 }
7213
Alexey Bataeved09d242014-05-28 05:53:51 +00007214 if (Vars.empty())
7215 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007216
Alexey Bataev03b340a2014-10-21 03:16:40 +00007217 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7218 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007219}
7220
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007221namespace {
7222class DiagsUninitializedSeveretyRAII {
7223private:
7224 DiagnosticsEngine &Diags;
7225 SourceLocation SavedLoc;
7226 bool IsIgnored;
7227
7228public:
7229 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7230 bool IsIgnored)
7231 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7232 if (!IsIgnored) {
7233 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7234 /*Map*/ diag::Severity::Ignored, Loc);
7235 }
7236 }
7237 ~DiagsUninitializedSeveretyRAII() {
7238 if (!IsIgnored)
7239 Diags.popMappings(SavedLoc);
7240 }
7241};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007242}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007243
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007244OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7245 SourceLocation StartLoc,
7246 SourceLocation LParenLoc,
7247 SourceLocation EndLoc) {
7248 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007249 SmallVector<Expr *, 8> PrivateCopies;
7250 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007251 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007252 bool IsImplicitClause =
7253 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7254 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7255
Alexey Bataeved09d242014-05-28 05:53:51 +00007256 for (auto &RefExpr : VarList) {
7257 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007258 auto Res = getPrivateItem(*this, RefExpr);
7259 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007260 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007261 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007262 PrivateCopies.push_back(nullptr);
7263 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007264 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007265 ValueDecl *D = Res.first;
7266 if (!D)
7267 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007268
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007269 SourceLocation ELoc =
7270 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007271 QualType Type = D->getType();
7272 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007273
7274 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7275 // A variable that appears in a private clause must not have an incomplete
7276 // type or a reference type.
7277 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007278 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007279 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007280 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007281
7282 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7283 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007284 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007285 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007286 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007287
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007288 // If an implicit firstprivate variable found it was checked already.
7289 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007290 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007291 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007292 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7293 // A list item that specifies a given variable may not appear in more
7294 // than one clause on the same directive, except that a variable may be
7295 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007296 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007297 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007298 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007299 << getOpenMPClauseName(DVar.CKind)
7300 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007301 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007302 continue;
7303 }
7304
7305 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7306 // in a Construct]
7307 // Variables with the predetermined data-sharing attributes may not be
7308 // listed in data-sharing attributes clauses, except for the cases
7309 // listed below. For these exceptions only, listing a predetermined
7310 // variable in a data-sharing attribute clause is allowed and overrides
7311 // the variable's predetermined data-sharing attributes.
7312 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7313 // in a Construct, C/C++, p.2]
7314 // Variables with const-qualified type having no mutable member may be
7315 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007316 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007317 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7318 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007319 << getOpenMPClauseName(DVar.CKind)
7320 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007321 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007322 continue;
7323 }
7324
Alexey Bataevf29276e2014-06-18 04:14:57 +00007325 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007326 // OpenMP [2.9.3.4, Restrictions, p.2]
7327 // A list item that is private within a parallel region must not appear
7328 // in a firstprivate clause on a worksharing construct if any of the
7329 // worksharing regions arising from the worksharing construct ever bind
7330 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007331 if (isOpenMPWorksharingDirective(CurrDir) &&
7332 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007333 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007334 if (DVar.CKind != OMPC_shared &&
7335 (isOpenMPParallelDirective(DVar.DKind) ||
7336 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007337 Diag(ELoc, diag::err_omp_required_access)
7338 << getOpenMPClauseName(OMPC_firstprivate)
7339 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007340 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007341 continue;
7342 }
7343 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007344 // OpenMP [2.9.3.4, Restrictions, p.3]
7345 // A list item that appears in a reduction clause of a parallel construct
7346 // must not appear in a firstprivate clause on a worksharing or task
7347 // construct if any of the worksharing or task regions arising from the
7348 // worksharing or task construct ever bind to any of the parallel regions
7349 // arising from the parallel construct.
7350 // OpenMP [2.9.3.4, Restrictions, p.4]
7351 // A list item that appears in a reduction clause in worksharing
7352 // construct must not appear in a firstprivate clause in a task construct
7353 // encountered during execution of any of the worksharing regions arising
7354 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007355 if (CurrDir == OMPD_task) {
7356 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007357 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007358 [](OpenMPDirectiveKind K) -> bool {
7359 return isOpenMPParallelDirective(K) ||
7360 isOpenMPWorksharingDirective(K);
7361 },
7362 false);
7363 if (DVar.CKind == OMPC_reduction &&
7364 (isOpenMPParallelDirective(DVar.DKind) ||
7365 isOpenMPWorksharingDirective(DVar.DKind))) {
7366 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7367 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007368 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007369 continue;
7370 }
7371 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007372
7373 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7374 // A list item that is private within a teams region must not appear in a
7375 // firstprivate clause on a distribute construct if any of the distribute
7376 // regions arising from the distribute construct ever bind to any of the
7377 // teams regions arising from the teams construct.
7378 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7379 // A list item that appears in a reduction clause of a teams construct
7380 // must not appear in a firstprivate clause on a distribute construct if
7381 // any of the distribute regions arising from the distribute construct
7382 // ever bind to any of the teams regions arising from the teams construct.
7383 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7384 // A list item may appear in a firstprivate or lastprivate clause but not
7385 // both.
7386 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007387 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007388 [](OpenMPDirectiveKind K) -> bool {
7389 return isOpenMPTeamsDirective(K);
7390 },
7391 false);
7392 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7393 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007394 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007395 continue;
7396 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007397 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007398 [](OpenMPDirectiveKind K) -> bool {
7399 return isOpenMPTeamsDirective(K);
7400 },
7401 false);
7402 if (DVar.CKind == OMPC_reduction &&
7403 isOpenMPTeamsDirective(DVar.DKind)) {
7404 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007405 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007406 continue;
7407 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007408 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007409 if (DVar.CKind == OMPC_lastprivate) {
7410 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007411 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007412 continue;
7413 }
7414 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007415 }
7416
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007417 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007418 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007419 DSAStack->getCurrentDirective() == OMPD_task) {
7420 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7421 << getOpenMPClauseName(OMPC_firstprivate) << Type
7422 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7423 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007424 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007425 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007426 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007427 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007428 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007429 continue;
7430 }
7431
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007432 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007433 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7434 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007435 // Generate helper private variable and initialize it with the value of the
7436 // original variable. The address of the original variable is replaced by
7437 // the address of the new private variable in the CodeGen. This new variable
7438 // is not added to IdResolver, so the code in the OpenMP region uses
7439 // original variable for proper diagnostics and variable capturing.
7440 Expr *VDInitRefExpr = nullptr;
7441 // For arrays generate initializer for single element and replace it by the
7442 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007443 if (Type->isArrayType()) {
7444 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007445 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007446 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007447 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007448 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007449 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007450 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007451 InitializedEntity Entity =
7452 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007453 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7454
7455 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7456 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7457 if (Result.isInvalid())
7458 VDPrivate->setInvalidDecl();
7459 else
7460 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007461 // Remove temp variable declaration.
7462 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007463 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007464 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7465 ".firstprivate.temp");
7466 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7467 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007468 AddInitializerToDecl(VDPrivate,
7469 DefaultLvalueConversion(VDInitRefExpr).get(),
7470 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007471 }
7472 if (VDPrivate->isInvalidDecl()) {
7473 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007474 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007475 diag::note_omp_task_predetermined_firstprivate_here);
7476 }
7477 continue;
7478 }
7479 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007480 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007481 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7482 RefExpr->getExprLoc());
7483 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007484 if (!VD) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007485 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
Alexey Bataev417089f2016-02-17 13:19:37 +00007486 ExprCaptures.push_back(Ref->getDecl());
7487 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007488 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7489 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007490 PrivateCopies.push_back(VDPrivateRefExpr);
7491 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007492 }
7493
Alexey Bataeved09d242014-05-28 05:53:51 +00007494 if (Vars.empty())
7495 return nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007496 Stmt *PreInit = nullptr;
7497 if (!ExprCaptures.empty()) {
7498 PreInit = new (Context)
7499 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7500 ExprCaptures.size()),
7501 SourceLocation(), SourceLocation());
7502 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007503
7504 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev417089f2016-02-17 13:19:37 +00007505 Vars, PrivateCopies, Inits, PreInit);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007506}
7507
Alexander Musman1bb328c2014-06-04 13:06:39 +00007508OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7509 SourceLocation StartLoc,
7510 SourceLocation LParenLoc,
7511 SourceLocation EndLoc) {
7512 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007513 SmallVector<Expr *, 8> SrcExprs;
7514 SmallVector<Expr *, 8> DstExprs;
7515 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007516 for (auto &RefExpr : VarList) {
7517 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev74caaf22016-02-20 04:09:36 +00007518 auto Res = getPrivateItem(*this, RefExpr);
7519 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007520 // It will be analyzed later.
7521 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007522 SrcExprs.push_back(nullptr);
7523 DstExprs.push_back(nullptr);
7524 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007525 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007526 ValueDecl *D = Res.first;
7527 if (!D)
7528 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007529
7530 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev74caaf22016-02-20 04:09:36 +00007531 QualType Type = D->getType();
7532 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007533
7534 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7535 // A variable that appears in a lastprivate clause must not have an
7536 // incomplete type or a reference type.
7537 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007538 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007539 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007540 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007541
7542 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7543 // in a Construct]
7544 // Variables with the predetermined data-sharing attributes may not be
7545 // listed in data-sharing attributes clauses, except for the cases
7546 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007547 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007548 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7549 DVar.CKind != OMPC_firstprivate &&
7550 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7551 Diag(ELoc, diag::err_omp_wrong_dsa)
7552 << getOpenMPClauseName(DVar.CKind)
7553 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007554 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007555 continue;
7556 }
7557
Alexey Bataevf29276e2014-06-18 04:14:57 +00007558 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7559 // OpenMP [2.14.3.5, Restrictions, p.2]
7560 // A list item that is private within a parallel region, or that appears in
7561 // the reduction clause of a parallel construct, must not appear in a
7562 // lastprivate clause on a worksharing construct if any of the corresponding
7563 // worksharing regions ever binds to any of the corresponding parallel
7564 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007565 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007566 if (isOpenMPWorksharingDirective(CurrDir) &&
7567 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007568 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007569 if (DVar.CKind != OMPC_shared) {
7570 Diag(ELoc, diag::err_omp_required_access)
7571 << getOpenMPClauseName(OMPC_lastprivate)
7572 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007573 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007574 continue;
7575 }
7576 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007577
7578 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7579 // A list item may appear in a firstprivate or lastprivate clause but not
7580 // both.
7581 if (CurrDir == OMPD_distribute) {
7582 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7583 if (DVar.CKind == OMPC_firstprivate) {
7584 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7585 ReportOriginalDSA(*this, DSAStack, D, DVar);
7586 continue;
7587 }
7588 }
7589
Alexander Musman1bb328c2014-06-04 13:06:39 +00007590 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007591 // A variable of class type (or array thereof) that appears in a
7592 // lastprivate clause requires an accessible, unambiguous default
7593 // constructor for the class type, unless the list item is also specified
7594 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007595 // A variable of class type (or array thereof) that appears in a
7596 // lastprivate clause requires an accessible, unambiguous copy assignment
7597 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007598 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev74caaf22016-02-20 04:09:36 +00007599 auto *SrcVD = buildVarDecl(*this, RefExpr->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007600 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007601 D->hasAttrs() ? &D->getAttrs() : nullptr);
7602 auto *PseudoSrcExpr =
7603 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007604 auto *DstVD =
Alexey Bataev74caaf22016-02-20 04:09:36 +00007605 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".lastprivate.dst",
7606 D->hasAttrs() ? &D->getAttrs() : nullptr);
7607 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007608 // For arrays generate assignment operation for single element and replace
7609 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007610 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007611 PseudoDstExpr, PseudoSrcExpr);
7612 if (AssignmentOp.isInvalid())
7613 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007614 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007615 /*DiscardedValue=*/true);
7616 if (AssignmentOp.isInvalid())
7617 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007618
Alexey Bataev74caaf22016-02-20 04:09:36 +00007619 DeclRefExpr *Ref = nullptr;
7620 if (!VD)
7621 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007622 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007623 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7624 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007625 SrcExprs.push_back(PseudoSrcExpr);
7626 DstExprs.push_back(PseudoDstExpr);
7627 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007628 }
7629
7630 if (Vars.empty())
7631 return nullptr;
7632
7633 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007634 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007635}
7636
Alexey Bataev758e55e2013-09-06 18:03:48 +00007637OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7638 SourceLocation StartLoc,
7639 SourceLocation LParenLoc,
7640 SourceLocation EndLoc) {
7641 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007642 for (auto &RefExpr : VarList) {
7643 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7644 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007645 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007646 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007647 continue;
7648 }
7649
Alexey Bataeved09d242014-05-28 05:53:51 +00007650 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007651 // OpenMP [2.1, C/C++]
7652 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007653 // OpenMP [2.14.3.2, Restrictions, p.1]
7654 // A variable that is part of another variable (as an array or structure
7655 // element) cannot appear in a shared unless it is a static data member
7656 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007657 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007658 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007659 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7660 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007661 continue;
7662 }
7663 Decl *D = DE->getDecl();
7664 VarDecl *VD = cast<VarDecl>(D);
7665
7666 QualType Type = VD->getType();
7667 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7668 // It will be analyzed later.
7669 Vars.push_back(DE);
7670 continue;
7671 }
7672
7673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7674 // in a Construct]
7675 // Variables with the predetermined data-sharing attributes may not be
7676 // listed in data-sharing attributes clauses, except for the cases
7677 // listed below. For these exceptions only, listing a predetermined
7678 // variable in a data-sharing attribute clause is allowed and overrides
7679 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007680 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007681 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7682 DVar.RefExpr) {
7683 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7684 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007685 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007686 continue;
7687 }
7688
7689 DSAStack->addDSA(VD, DE, OMPC_shared);
7690 Vars.push_back(DE);
7691 }
7692
Alexey Bataeved09d242014-05-28 05:53:51 +00007693 if (Vars.empty())
7694 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007695
7696 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7697}
7698
Alexey Bataevc5e02582014-06-16 07:08:35 +00007699namespace {
7700class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7701 DSAStackTy *Stack;
7702
7703public:
7704 bool VisitDeclRefExpr(DeclRefExpr *E) {
7705 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007706 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007707 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7708 return false;
7709 if (DVar.CKind != OMPC_unknown)
7710 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007711 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007712 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007713 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007714 return true;
7715 return false;
7716 }
7717 return false;
7718 }
7719 bool VisitStmt(Stmt *S) {
7720 for (auto Child : S->children()) {
7721 if (Child && Visit(Child))
7722 return true;
7723 }
7724 return false;
7725 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007726 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007727};
Alexey Bataev23b69422014-06-18 07:08:49 +00007728} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007729
7730OMPClause *Sema::ActOnOpenMPReductionClause(
7731 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7732 SourceLocation ColonLoc, SourceLocation EndLoc,
7733 CXXScopeSpec &ReductionIdScopeSpec,
7734 const DeclarationNameInfo &ReductionId) {
7735 // TODO: Allow scope specification search when 'declare reduction' is
7736 // supported.
7737 assert(ReductionIdScopeSpec.isEmpty() &&
7738 "No support for scoped reduction identifiers yet.");
7739
7740 auto DN = ReductionId.getName();
7741 auto OOK = DN.getCXXOverloadedOperator();
7742 BinaryOperatorKind BOK = BO_Comma;
7743
7744 // OpenMP [2.14.3.6, reduction clause]
7745 // C
7746 // reduction-identifier is either an identifier or one of the following
7747 // operators: +, -, *, &, |, ^, && and ||
7748 // C++
7749 // reduction-identifier is either an id-expression or one of the following
7750 // operators: +, -, *, &, |, ^, && and ||
7751 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7752 switch (OOK) {
7753 case OO_Plus:
7754 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007755 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007756 break;
7757 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007758 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007759 break;
7760 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007761 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007762 break;
7763 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007764 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007765 break;
7766 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007767 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007768 break;
7769 case OO_AmpAmp:
7770 BOK = BO_LAnd;
7771 break;
7772 case OO_PipePipe:
7773 BOK = BO_LOr;
7774 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007775 case OO_New:
7776 case OO_Delete:
7777 case OO_Array_New:
7778 case OO_Array_Delete:
7779 case OO_Slash:
7780 case OO_Percent:
7781 case OO_Tilde:
7782 case OO_Exclaim:
7783 case OO_Equal:
7784 case OO_Less:
7785 case OO_Greater:
7786 case OO_LessEqual:
7787 case OO_GreaterEqual:
7788 case OO_PlusEqual:
7789 case OO_MinusEqual:
7790 case OO_StarEqual:
7791 case OO_SlashEqual:
7792 case OO_PercentEqual:
7793 case OO_CaretEqual:
7794 case OO_AmpEqual:
7795 case OO_PipeEqual:
7796 case OO_LessLess:
7797 case OO_GreaterGreater:
7798 case OO_LessLessEqual:
7799 case OO_GreaterGreaterEqual:
7800 case OO_EqualEqual:
7801 case OO_ExclaimEqual:
7802 case OO_PlusPlus:
7803 case OO_MinusMinus:
7804 case OO_Comma:
7805 case OO_ArrowStar:
7806 case OO_Arrow:
7807 case OO_Call:
7808 case OO_Subscript:
7809 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007810 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007811 case NUM_OVERLOADED_OPERATORS:
7812 llvm_unreachable("Unexpected reduction identifier");
7813 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007814 if (auto II = DN.getAsIdentifierInfo()) {
7815 if (II->isStr("max"))
7816 BOK = BO_GT;
7817 else if (II->isStr("min"))
7818 BOK = BO_LT;
7819 }
7820 break;
7821 }
7822 SourceRange ReductionIdRange;
7823 if (ReductionIdScopeSpec.isValid()) {
7824 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7825 }
7826 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7827 if (BOK == BO_Comma) {
7828 // Not allowed reduction identifier is found.
7829 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7830 << ReductionIdRange;
7831 return nullptr;
7832 }
7833
7834 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007835 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007836 SmallVector<Expr *, 8> LHSs;
7837 SmallVector<Expr *, 8> RHSs;
7838 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007839 for (auto RefExpr : VarList) {
7840 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7841 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7842 // It will be analyzed later.
7843 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007844 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007845 LHSs.push_back(nullptr);
7846 RHSs.push_back(nullptr);
7847 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007848 continue;
7849 }
7850
7851 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7852 RefExpr->isInstantiationDependent() ||
7853 RefExpr->containsUnexpandedParameterPack()) {
7854 // It will be analyzed later.
7855 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007856 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007857 LHSs.push_back(nullptr);
7858 RHSs.push_back(nullptr);
7859 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007860 continue;
7861 }
7862
7863 auto ELoc = RefExpr->getExprLoc();
7864 auto ERange = RefExpr->getSourceRange();
7865 // OpenMP [2.1, C/C++]
7866 // A list item is a variable or array section, subject to the restrictions
7867 // specified in Section 2.4 on page 42 and in each of the sections
7868 // describing clauses and directives for which a list appears.
7869 // OpenMP [2.14.3.3, Restrictions, p.1]
7870 // A variable that is part of another variable (as an array or
7871 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007872 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7873 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7874 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7875 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007876 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7877 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007878 continue;
7879 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007880 QualType Type;
7881 VarDecl *VD = nullptr;
7882 if (DE) {
7883 auto D = DE->getDecl();
7884 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007885 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007886 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007887 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007888 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7889 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7890 Base = TempASE->getBase()->IgnoreParenImpCasts();
7891 DE = dyn_cast<DeclRefExpr>(Base);
7892 if (DE)
7893 VD = dyn_cast<VarDecl>(DE->getDecl());
7894 if (!VD) {
7895 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7896 << 0 << Base->getSourceRange();
7897 continue;
7898 }
7899 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007900 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7901 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7902 Type = ATy->getElementType();
7903 else
7904 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007905 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007906 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7907 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7908 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7909 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7910 Base = TempASE->getBase()->IgnoreParenImpCasts();
7911 DE = dyn_cast<DeclRefExpr>(Base);
7912 if (DE)
7913 VD = dyn_cast<VarDecl>(DE->getDecl());
7914 if (!VD) {
7915 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7916 << 1 << Base->getSourceRange();
7917 continue;
7918 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007919 }
7920
Alexey Bataevc5e02582014-06-16 07:08:35 +00007921 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7922 // A variable that appears in a private clause must not have an incomplete
7923 // type or a reference type.
7924 if (RequireCompleteType(ELoc, Type,
7925 diag::err_omp_reduction_incomplete_type))
7926 continue;
7927 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007928 // A list item that appears in a reduction clause must not be
7929 // const-qualified.
7930 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007931 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007932 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007933 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007934 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7935 VarDecl::DeclarationOnly;
7936 Diag(VD->getLocation(),
7937 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7938 << VD;
7939 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007940 continue;
7941 }
7942 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7943 // If a list-item is a reference type then it must bind to the same object
7944 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007945 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007946 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007947 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007948 DSARefChecker Check(DSAStack);
7949 if (Check.Visit(VDDef->getInit())) {
7950 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7951 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7952 continue;
7953 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007954 }
7955 }
7956 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7957 // The type of a list item that appears in a reduction clause must be valid
7958 // for the reduction-identifier. For a max or min reduction in C, the type
7959 // of the list item must be an allowed arithmetic data type: char, int,
7960 // float, double, or _Bool, possibly modified with long, short, signed, or
7961 // unsigned. For a max or min reduction in C++, the type of the list item
7962 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7963 // double, or bool, possibly modified with long, short, signed, or unsigned.
7964 if ((BOK == BO_GT || BOK == BO_LT) &&
7965 !(Type->isScalarType() ||
7966 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7967 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7968 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007969 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007970 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7971 VarDecl::DeclarationOnly;
7972 Diag(VD->getLocation(),
7973 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7974 << VD;
7975 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007976 continue;
7977 }
7978 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7979 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7980 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007981 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007982 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7983 VarDecl::DeclarationOnly;
7984 Diag(VD->getLocation(),
7985 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7986 << VD;
7987 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007988 continue;
7989 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007990 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7991 // in a Construct]
7992 // Variables with the predetermined data-sharing attributes may not be
7993 // listed in data-sharing attributes clauses, except for the cases
7994 // listed below. For these exceptions only, listing a predetermined
7995 // variable in a data-sharing attribute clause is allowed and overrides
7996 // the variable's predetermined data-sharing attributes.
7997 // OpenMP [2.14.3.6, Restrictions, p.3]
7998 // Any number of reduction clauses can be specified on the directive,
7999 // but a list item can appear only once in the reduction clauses for that
8000 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008001 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008002 DVar = DSAStack->getTopDSA(VD, false);
8003 if (DVar.CKind == OMPC_reduction) {
8004 Diag(ELoc, diag::err_omp_once_referenced)
8005 << getOpenMPClauseName(OMPC_reduction);
8006 if (DVar.RefExpr) {
8007 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008008 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008009 } else if (DVar.CKind != OMPC_unknown) {
8010 Diag(ELoc, diag::err_omp_wrong_dsa)
8011 << getOpenMPClauseName(DVar.CKind)
8012 << getOpenMPClauseName(OMPC_reduction);
8013 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8014 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008015 }
8016
8017 // OpenMP [2.14.3.6, Restrictions, p.1]
8018 // A list item that appears in a reduction clause of a worksharing
8019 // construct must be shared in the parallel regions to which any of the
8020 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008021 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8022 if (isOpenMPWorksharingDirective(CurrDir) &&
8023 !isOpenMPParallelDirective(CurrDir)) {
8024 DVar = DSAStack->getImplicitDSA(VD, true);
8025 if (DVar.CKind != OMPC_shared) {
8026 Diag(ELoc, diag::err_omp_required_access)
8027 << getOpenMPClauseName(OMPC_reduction)
8028 << getOpenMPClauseName(OMPC_shared);
8029 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8030 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008031 }
8032 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008033
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008034 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008035 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
8036 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
8037 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
8038 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
8039 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008040 if (OASE ||
8041 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
8042 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008043 // Create pseudo array type for private copy. The size for this array will
8044 // be generated during codegen.
8045 // For array subscripts or single variables Private Ty is the same as Type
8046 // (type of the variable or single array element).
8047 PrivateTy = Context.getVariableArrayType(
8048 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8049 Context.getSizeType(), VK_RValue),
8050 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00008051 } else if (DE &&
8052 Context.getAsArrayType(VD->getType().getNonReferenceType()))
8053 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008054 // Private copy.
8055 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
8056 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008057 // Add initializer for private variable.
8058 Expr *Init = nullptr;
8059 switch (BOK) {
8060 case BO_Add:
8061 case BO_Xor:
8062 case BO_Or:
8063 case BO_LOr:
8064 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8065 if (Type->isScalarType() || Type->isAnyComplexType()) {
8066 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008067 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008068 break;
8069 case BO_Mul:
8070 case BO_LAnd:
8071 if (Type->isScalarType() || Type->isAnyComplexType()) {
8072 // '*' and '&&' reduction ops - initializer is '1'.
8073 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8074 }
8075 break;
8076 case BO_And: {
8077 // '&' reduction op - initializer is '~0'.
8078 QualType OrigType = Type;
8079 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
8080 Type = ComplexTy->getElementType();
8081 }
8082 if (Type->isRealFloatingType()) {
8083 llvm::APFloat InitValue =
8084 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8085 /*isIEEE=*/true);
8086 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8087 Type, ELoc);
8088 } else if (Type->isScalarType()) {
8089 auto Size = Context.getTypeSize(Type);
8090 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8091 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8092 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8093 }
8094 if (Init && OrigType->isAnyComplexType()) {
8095 // Init = 0xFFFF + 0xFFFFi;
8096 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8097 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8098 }
8099 Type = OrigType;
8100 break;
8101 }
8102 case BO_LT:
8103 case BO_GT: {
8104 // 'min' reduction op - initializer is 'Largest representable number in
8105 // the reduction list item type'.
8106 // 'max' reduction op - initializer is 'Least representable number in
8107 // the reduction list item type'.
8108 if (Type->isIntegerType() || Type->isPointerType()) {
8109 bool IsSigned = Type->hasSignedIntegerRepresentation();
8110 auto Size = Context.getTypeSize(Type);
8111 QualType IntTy =
8112 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8113 llvm::APInt InitValue =
8114 (BOK != BO_LT)
8115 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8116 : llvm::APInt::getMinValue(Size)
8117 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8118 : llvm::APInt::getMaxValue(Size);
8119 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8120 if (Type->isPointerType()) {
8121 // Cast to pointer type.
8122 auto CastExpr = BuildCStyleCastExpr(
8123 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8124 SourceLocation(), Init);
8125 if (CastExpr.isInvalid())
8126 continue;
8127 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008128 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008129 } else if (Type->isRealFloatingType()) {
8130 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8131 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8132 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8133 Type, ELoc);
8134 }
8135 break;
8136 }
8137 case BO_PtrMemD:
8138 case BO_PtrMemI:
8139 case BO_MulAssign:
8140 case BO_Div:
8141 case BO_Rem:
8142 case BO_Sub:
8143 case BO_Shl:
8144 case BO_Shr:
8145 case BO_LE:
8146 case BO_GE:
8147 case BO_EQ:
8148 case BO_NE:
8149 case BO_AndAssign:
8150 case BO_XorAssign:
8151 case BO_OrAssign:
8152 case BO_Assign:
8153 case BO_AddAssign:
8154 case BO_SubAssign:
8155 case BO_DivAssign:
8156 case BO_RemAssign:
8157 case BO_ShlAssign:
8158 case BO_ShrAssign:
8159 case BO_Comma:
8160 llvm_unreachable("Unexpected reduction operation");
8161 }
8162 if (Init) {
8163 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8164 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008165 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008166 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008167 if (!RHSVD->hasInit()) {
8168 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8169 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008170 if (VD) {
8171 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8172 VarDecl::DeclarationOnly;
8173 Diag(VD->getLocation(),
8174 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8175 << VD;
8176 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008177 continue;
8178 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008179 // Store initializer for single element in private copy. Will be used during
8180 // codegen.
8181 PrivateVD->setInit(RHSVD->getInit());
8182 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008183 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8184 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008185 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008186 ExprResult ReductionOp =
8187 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8188 LHSDRE, RHSDRE);
8189 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008190 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008191 ReductionOp =
8192 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8193 BO_Assign, LHSDRE, ReductionOp.get());
8194 } else {
8195 auto *ConditionalOp = new (Context) ConditionalOperator(
8196 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8197 RHSDRE, Type, VK_LValue, OK_Ordinary);
8198 ReductionOp =
8199 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8200 BO_Assign, LHSDRE, ConditionalOp);
8201 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008202 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008203 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008204 if (ReductionOp.isInvalid())
8205 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008206
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008207 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008208 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008209 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008210 LHSs.push_back(LHSDRE);
8211 RHSs.push_back(RHSDRE);
8212 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008213 }
8214
8215 if (Vars.empty())
8216 return nullptr;
8217
8218 return OMPReductionClause::Create(
8219 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008220 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8221 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008222}
8223
Alexey Bataev182227b2015-08-20 10:54:39 +00008224OMPClause *Sema::ActOnOpenMPLinearClause(
8225 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8226 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8227 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008228 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008229 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008230 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008231 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8232 LinKind == OMPC_LINEAR_unknown) {
8233 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8234 LinKind = OMPC_LINEAR_val;
8235 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008236 for (auto &RefExpr : VarList) {
8237 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8238 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008239 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008240 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008241 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008242 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008243 continue;
8244 }
8245
8246 // OpenMP [2.14.3.7, linear clause]
8247 // A list item that appears in a linear clause is subject to the private
8248 // clause semantics described in Section 2.14.3.3 on page 159 except as
8249 // noted. In addition, the value of the new list item on each iteration
8250 // of the associated loop(s) corresponds to the value of the original
8251 // list item before entering the construct plus the logical number of
8252 // the iteration times linear-step.
8253
Alexey Bataeved09d242014-05-28 05:53:51 +00008254 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008255 // OpenMP [2.1, C/C++]
8256 // A list item is a variable name.
8257 // OpenMP [2.14.3.3, Restrictions, p.1]
8258 // A variable that is part of another variable (as an array or
8259 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008260 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008261 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008262 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8263 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008264 continue;
8265 }
8266
8267 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8268
8269 // OpenMP [2.14.3.7, linear clause]
8270 // A list-item cannot appear in more than one linear clause.
8271 // A list-item that appears in a linear clause cannot appear in any
8272 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008273 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008274 if (DVar.RefExpr) {
8275 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8276 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008277 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008278 continue;
8279 }
8280
8281 QualType QType = VD->getType();
8282 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8283 // It will be analyzed later.
8284 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008285 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008286 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008287 continue;
8288 }
8289
8290 // A variable must not have an incomplete type or a reference type.
8291 if (RequireCompleteType(ELoc, QType,
8292 diag::err_omp_linear_incomplete_type)) {
8293 continue;
8294 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008295 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8296 !QType->isReferenceType()) {
8297 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8298 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8299 continue;
8300 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008301 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008302
8303 // A list item must not be const-qualified.
8304 if (QType.isConstant(Context)) {
8305 Diag(ELoc, diag::err_omp_const_variable)
8306 << getOpenMPClauseName(OMPC_linear);
8307 bool IsDecl =
8308 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8309 Diag(VD->getLocation(),
8310 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8311 << VD;
8312 continue;
8313 }
8314
8315 // A list item must be of integral or pointer type.
8316 QType = QType.getUnqualifiedType().getCanonicalType();
8317 const Type *Ty = QType.getTypePtrOrNull();
8318 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8319 !Ty->isPointerType())) {
8320 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8321 bool IsDecl =
8322 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8323 Diag(VD->getLocation(),
8324 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8325 << VD;
8326 continue;
8327 }
8328
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008329 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008330 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8331 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008332 auto *PrivateRef = buildDeclRefExpr(
8333 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008334 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008335 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008336 Expr *InitExpr;
8337 if (LinKind == OMPC_LINEAR_uval)
8338 InitExpr = VD->getInit();
8339 else
8340 InitExpr = DE;
8341 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008342 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008343 auto InitRef = buildDeclRefExpr(
8344 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008345 DSAStack->addDSA(VD, DE, OMPC_linear);
8346 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008347 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008348 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008349 }
8350
8351 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008352 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008353
8354 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008355 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008356 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8357 !Step->isInstantiationDependent() &&
8358 !Step->containsUnexpandedParameterPack()) {
8359 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008360 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008361 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008362 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008363 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008364
Alexander Musman3276a272015-03-21 10:12:56 +00008365 // Build var to save the step value.
8366 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008367 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008368 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008369 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008370 ExprResult CalcStep =
8371 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008372 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008373
Alexander Musman8dba6642014-04-22 13:09:42 +00008374 // Warn about zero linear step (it would be probably better specified as
8375 // making corresponding variables 'const').
8376 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008377 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8378 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008379 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8380 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008381 if (!IsConstant && CalcStep.isUsable()) {
8382 // Calculate the step beforehand instead of doing this on each iteration.
8383 // (This is not used if the number of iterations may be kfold-ed).
8384 CalcStepExpr = CalcStep.get();
8385 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008386 }
8387
Alexey Bataev182227b2015-08-20 10:54:39 +00008388 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8389 ColonLoc, EndLoc, Vars, Privates, Inits,
8390 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008391}
8392
8393static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8394 Expr *NumIterations, Sema &SemaRef,
8395 Scope *S) {
8396 // Walk the vars and build update/final expressions for the CodeGen.
8397 SmallVector<Expr *, 8> Updates;
8398 SmallVector<Expr *, 8> Finals;
8399 Expr *Step = Clause.getStep();
8400 Expr *CalcStep = Clause.getCalcStep();
8401 // OpenMP [2.14.3.7, linear clause]
8402 // If linear-step is not specified it is assumed to be 1.
8403 if (Step == nullptr)
8404 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8405 else if (CalcStep)
8406 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8407 bool HasErrors = false;
8408 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008409 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008410 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008411 for (auto &RefExpr : Clause.varlists()) {
8412 Expr *InitExpr = *CurInit;
8413
8414 // Build privatized reference to the current linear var.
8415 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008416 Expr *CapturedRef;
8417 if (LinKind == OMPC_LINEAR_uval)
8418 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8419 else
8420 CapturedRef =
8421 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8422 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8423 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008424
8425 // Build update: Var = InitExpr + IV * Step
8426 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008427 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008428 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008429 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8430 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008431
8432 // Build final: Var = InitExpr + NumIterations * Step
8433 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008434 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008435 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008436 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8437 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008438 if (!Update.isUsable() || !Final.isUsable()) {
8439 Updates.push_back(nullptr);
8440 Finals.push_back(nullptr);
8441 HasErrors = true;
8442 } else {
8443 Updates.push_back(Update.get());
8444 Finals.push_back(Final.get());
8445 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008446 ++CurInit;
8447 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008448 }
8449 Clause.setUpdates(Updates);
8450 Clause.setFinals(Finals);
8451 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008452}
8453
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008454OMPClause *Sema::ActOnOpenMPAlignedClause(
8455 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8456 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8457
8458 SmallVector<Expr *, 8> Vars;
8459 for (auto &RefExpr : VarList) {
8460 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8461 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8462 // It will be analyzed later.
8463 Vars.push_back(RefExpr);
8464 continue;
8465 }
8466
8467 SourceLocation ELoc = RefExpr->getExprLoc();
8468 // OpenMP [2.1, C/C++]
8469 // A list item is a variable name.
8470 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8471 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008472 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8473 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008474 continue;
8475 }
8476
8477 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8478
8479 // OpenMP [2.8.1, simd construct, Restrictions]
8480 // The type of list items appearing in the aligned clause must be
8481 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008482 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008483 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008484 const Type *Ty = QType.getTypePtrOrNull();
8485 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8486 !Ty->isPointerType())) {
8487 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8488 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8489 bool IsDecl =
8490 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8491 Diag(VD->getLocation(),
8492 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8493 << VD;
8494 continue;
8495 }
8496
8497 // OpenMP [2.8.1, simd construct, Restrictions]
8498 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008499 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008500 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8501 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8502 << getOpenMPClauseName(OMPC_aligned);
8503 continue;
8504 }
8505
8506 Vars.push_back(DE);
8507 }
8508
8509 // OpenMP [2.8.1, simd construct, Description]
8510 // The parameter of the aligned clause, alignment, must be a constant
8511 // positive integer expression.
8512 // If no optional parameter is specified, implementation-defined default
8513 // alignments for SIMD instructions on the target platforms are assumed.
8514 if (Alignment != nullptr) {
8515 ExprResult AlignResult =
8516 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8517 if (AlignResult.isInvalid())
8518 return nullptr;
8519 Alignment = AlignResult.get();
8520 }
8521 if (Vars.empty())
8522 return nullptr;
8523
8524 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8525 EndLoc, Vars, Alignment);
8526}
8527
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008528OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8529 SourceLocation StartLoc,
8530 SourceLocation LParenLoc,
8531 SourceLocation EndLoc) {
8532 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008533 SmallVector<Expr *, 8> SrcExprs;
8534 SmallVector<Expr *, 8> DstExprs;
8535 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008536 for (auto &RefExpr : VarList) {
8537 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8538 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008539 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008540 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008541 SrcExprs.push_back(nullptr);
8542 DstExprs.push_back(nullptr);
8543 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008544 continue;
8545 }
8546
Alexey Bataeved09d242014-05-28 05:53:51 +00008547 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008548 // OpenMP [2.1, C/C++]
8549 // A list item is a variable name.
8550 // OpenMP [2.14.4.1, Restrictions, p.1]
8551 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008552 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008553 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008554 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8555 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008556 continue;
8557 }
8558
8559 Decl *D = DE->getDecl();
8560 VarDecl *VD = cast<VarDecl>(D);
8561
8562 QualType Type = VD->getType();
8563 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8564 // It will be analyzed later.
8565 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008566 SrcExprs.push_back(nullptr);
8567 DstExprs.push_back(nullptr);
8568 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008569 continue;
8570 }
8571
8572 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8573 // A list item that appears in a copyin clause must be threadprivate.
8574 if (!DSAStack->isThreadPrivate(VD)) {
8575 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008576 << getOpenMPClauseName(OMPC_copyin)
8577 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008578 continue;
8579 }
8580
8581 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8582 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008583 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008584 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008585 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008586 auto *SrcVD =
8587 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8588 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008589 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008590 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8591 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008592 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8593 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008594 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008595 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008596 // For arrays generate assignment operation for single element and replace
8597 // it by the original array element in CodeGen.
8598 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8599 PseudoDstExpr, PseudoSrcExpr);
8600 if (AssignmentOp.isInvalid())
8601 continue;
8602 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8603 /*DiscardedValue=*/true);
8604 if (AssignmentOp.isInvalid())
8605 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008606
8607 DSAStack->addDSA(VD, DE, OMPC_copyin);
8608 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008609 SrcExprs.push_back(PseudoSrcExpr);
8610 DstExprs.push_back(PseudoDstExpr);
8611 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008612 }
8613
Alexey Bataeved09d242014-05-28 05:53:51 +00008614 if (Vars.empty())
8615 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008616
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008617 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8618 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008619}
8620
Alexey Bataevbae9a792014-06-27 10:37:06 +00008621OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8622 SourceLocation StartLoc,
8623 SourceLocation LParenLoc,
8624 SourceLocation EndLoc) {
8625 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008626 SmallVector<Expr *, 8> SrcExprs;
8627 SmallVector<Expr *, 8> DstExprs;
8628 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008629 for (auto &RefExpr : VarList) {
8630 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8631 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8632 // It will be analyzed later.
8633 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008634 SrcExprs.push_back(nullptr);
8635 DstExprs.push_back(nullptr);
8636 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008637 continue;
8638 }
8639
8640 SourceLocation ELoc = RefExpr->getExprLoc();
8641 // OpenMP [2.1, C/C++]
8642 // A list item is a variable name.
8643 // OpenMP [2.14.4.1, Restrictions, p.1]
8644 // A list item that appears in a copyin clause must be threadprivate.
8645 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8646 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008647 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8648 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008649 continue;
8650 }
8651
8652 Decl *D = DE->getDecl();
8653 VarDecl *VD = cast<VarDecl>(D);
8654
8655 QualType Type = VD->getType();
8656 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8657 // It will be analyzed later.
8658 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008659 SrcExprs.push_back(nullptr);
8660 DstExprs.push_back(nullptr);
8661 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008662 continue;
8663 }
8664
8665 // OpenMP [2.14.4.2, Restrictions, p.2]
8666 // A list item that appears in a copyprivate clause may not appear in a
8667 // private or firstprivate clause on the single construct.
8668 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008669 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008670 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8671 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008672 Diag(ELoc, diag::err_omp_wrong_dsa)
8673 << getOpenMPClauseName(DVar.CKind)
8674 << getOpenMPClauseName(OMPC_copyprivate);
8675 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8676 continue;
8677 }
8678
8679 // OpenMP [2.11.4.2, Restrictions, p.1]
8680 // All list items that appear in a copyprivate clause must be either
8681 // threadprivate or private in the enclosing context.
8682 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008683 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008684 if (DVar.CKind == OMPC_shared) {
8685 Diag(ELoc, diag::err_omp_required_access)
8686 << getOpenMPClauseName(OMPC_copyprivate)
8687 << "threadprivate or private in the enclosing context";
8688 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8689 continue;
8690 }
8691 }
8692 }
8693
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008694 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008695 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008696 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008697 << getOpenMPClauseName(OMPC_copyprivate) << Type
8698 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008699 bool IsDecl =
8700 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8701 Diag(VD->getLocation(),
8702 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8703 << VD;
8704 continue;
8705 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008706
Alexey Bataevbae9a792014-06-27 10:37:06 +00008707 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8708 // A variable of class type (or array thereof) that appears in a
8709 // copyin clause requires an accessible, unambiguous copy assignment
8710 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008711 Type = Context.getBaseElementType(Type.getNonReferenceType())
8712 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008713 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008714 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8715 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008716 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008717 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008718 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008719 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8720 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008721 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008722 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008723 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8724 PseudoDstExpr, PseudoSrcExpr);
8725 if (AssignmentOp.isInvalid())
8726 continue;
8727 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8728 /*DiscardedValue=*/true);
8729 if (AssignmentOp.isInvalid())
8730 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008731
8732 // No need to mark vars as copyprivate, they are already threadprivate or
8733 // implicitly private.
8734 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008735 SrcExprs.push_back(PseudoSrcExpr);
8736 DstExprs.push_back(PseudoDstExpr);
8737 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008738 }
8739
8740 if (Vars.empty())
8741 return nullptr;
8742
Alexey Bataeva63048e2015-03-23 06:18:07 +00008743 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8744 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008745}
8746
Alexey Bataev6125da92014-07-21 11:26:11 +00008747OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8748 SourceLocation StartLoc,
8749 SourceLocation LParenLoc,
8750 SourceLocation EndLoc) {
8751 if (VarList.empty())
8752 return nullptr;
8753
8754 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8755}
Alexey Bataevdea47612014-07-23 07:46:59 +00008756
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008757OMPClause *
8758Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8759 SourceLocation DepLoc, SourceLocation ColonLoc,
8760 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8761 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008762 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008763 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008764 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008765 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008766 return nullptr;
8767 }
8768 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008769 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8770 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008771 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008772 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008773 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8774 /*Last=*/OMPC_DEPEND_unknown, Except)
8775 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008776 return nullptr;
8777 }
8778 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008779 llvm::APSInt DepCounter(/*BitWidth=*/32);
8780 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8781 if (DepKind == OMPC_DEPEND_sink) {
8782 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8783 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8784 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008785 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008786 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008787 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8788 DSAStack->getParentOrderedRegionParam()) {
8789 for (auto &RefExpr : VarList) {
8790 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8791 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8792 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8793 // It will be analyzed later.
8794 Vars.push_back(RefExpr);
8795 continue;
8796 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008797
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008798 SourceLocation ELoc = RefExpr->getExprLoc();
8799 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8800 if (DepKind == OMPC_DEPEND_sink) {
8801 if (DepCounter >= TotalDepCount) {
8802 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8803 continue;
8804 }
8805 ++DepCounter;
8806 // OpenMP [2.13.9, Summary]
8807 // depend(dependence-type : vec), where dependence-type is:
8808 // 'sink' and where vec is the iteration vector, which has the form:
8809 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8810 // where n is the value specified by the ordered clause in the loop
8811 // directive, xi denotes the loop iteration variable of the i-th nested
8812 // loop associated with the loop directive, and di is a constant
8813 // non-negative integer.
8814 SimpleExpr = SimpleExpr->IgnoreImplicit();
8815 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8816 if (!DE) {
8817 OverloadedOperatorKind OOK = OO_None;
8818 SourceLocation OOLoc;
8819 Expr *LHS, *RHS;
8820 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8821 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8822 OOLoc = BO->getOperatorLoc();
8823 LHS = BO->getLHS()->IgnoreParenImpCasts();
8824 RHS = BO->getRHS()->IgnoreParenImpCasts();
8825 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8826 OOK = OCE->getOperator();
8827 OOLoc = OCE->getOperatorLoc();
8828 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8829 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8830 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8831 OOK = MCE->getMethodDecl()
8832 ->getNameInfo()
8833 .getName()
8834 .getCXXOverloadedOperator();
8835 OOLoc = MCE->getCallee()->getExprLoc();
8836 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8837 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8838 } else {
8839 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8840 continue;
8841 }
8842 DE = dyn_cast<DeclRefExpr>(LHS);
8843 if (!DE) {
8844 Diag(LHS->getExprLoc(),
8845 diag::err_omp_depend_sink_expected_loop_iteration)
8846 << DSAStack->getParentLoopControlVariable(
8847 DepCounter.getZExtValue());
8848 continue;
8849 }
8850 if (OOK != OO_Plus && OOK != OO_Minus) {
8851 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8852 continue;
8853 }
8854 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8855 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8856 if (Res.isInvalid())
8857 continue;
8858 }
8859 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8860 if (!CurContext->isDependentContext() &&
8861 DSAStack->getParentOrderedRegionParam() &&
8862 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8863 Diag(DE->getExprLoc(),
8864 diag::err_omp_depend_sink_expected_loop_iteration)
8865 << DSAStack->getParentLoopControlVariable(
8866 DepCounter.getZExtValue());
8867 continue;
8868 }
8869 } else {
8870 // OpenMP [2.11.1.1, Restrictions, p.3]
8871 // A variable that is part of another variable (such as a field of a
8872 // structure) but is not an array element or an array section cannot
8873 // appear in a depend clause.
8874 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8875 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8876 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8877 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8878 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008879 (ASE &&
8880 !ASE->getBase()
8881 ->getType()
8882 .getNonReferenceType()
8883 ->isPointerType() &&
8884 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008885 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8886 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008887 continue;
8888 }
8889 }
8890
8891 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8892 }
8893
8894 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8895 TotalDepCount > VarList.size() &&
8896 DSAStack->getParentOrderedRegionParam()) {
8897 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8898 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8899 }
8900 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8901 Vars.empty())
8902 return nullptr;
8903 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008904
8905 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8906 DepLoc, ColonLoc, Vars);
8907}
Michael Wonge710d542015-08-07 16:16:36 +00008908
8909OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8910 SourceLocation LParenLoc,
8911 SourceLocation EndLoc) {
8912 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008913
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008914 // OpenMP [2.9.1, Restrictions]
8915 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008916 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8917 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008918 return nullptr;
8919
Michael Wonge710d542015-08-07 16:16:36 +00008920 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8921}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008922
8923static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8924 DSAStackTy *Stack, CXXRecordDecl *RD) {
8925 if (!RD || RD->isInvalidDecl())
8926 return true;
8927
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008928 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8929 if (auto *CTD = CTSD->getSpecializedTemplate())
8930 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008931 auto QTy = SemaRef.Context.getRecordType(RD);
8932 if (RD->isDynamicClass()) {
8933 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8934 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8935 return false;
8936 }
8937 auto *DC = RD;
8938 bool IsCorrect = true;
8939 for (auto *I : DC->decls()) {
8940 if (I) {
8941 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8942 if (MD->isStatic()) {
8943 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8944 SemaRef.Diag(MD->getLocation(),
8945 diag::note_omp_static_member_in_target);
8946 IsCorrect = false;
8947 }
8948 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8949 if (VD->isStaticDataMember()) {
8950 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8951 SemaRef.Diag(VD->getLocation(),
8952 diag::note_omp_static_member_in_target);
8953 IsCorrect = false;
8954 }
8955 }
8956 }
8957 }
8958
8959 for (auto &I : RD->bases()) {
8960 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8961 I.getType()->getAsCXXRecordDecl()))
8962 IsCorrect = false;
8963 }
8964 return IsCorrect;
8965}
8966
8967static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8968 DSAStackTy *Stack, QualType QTy) {
8969 NamedDecl *ND;
8970 if (QTy->isIncompleteType(&ND)) {
8971 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8972 return false;
8973 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8974 if (!RD->isInvalidDecl() &&
8975 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8976 return false;
8977 }
8978 return true;
8979}
8980
Samuel Antao5de996e2016-01-22 20:21:36 +00008981// Return the expression of the base of the map clause or null if it cannot
8982// be determined and do all the necessary checks to see if the expression is
8983// valid as a standalone map clause expression.
8984static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8985 SourceLocation ELoc = E->getExprLoc();
8986 SourceRange ERange = E->getSourceRange();
8987
8988 // The base of elements of list in a map clause have to be either:
8989 // - a reference to variable or field.
8990 // - a member expression.
8991 // - an array expression.
8992 //
8993 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8994 // reference to 'r'.
8995 //
8996 // If we have:
8997 //
8998 // struct SS {
8999 // Bla S;
9000 // foo() {
9001 // #pragma omp target map (S.Arr[:12]);
9002 // }
9003 // }
9004 //
9005 // We want to retrieve the member expression 'this->S';
9006
9007 Expr *RelevantExpr = nullptr;
9008
9009 // Flags to help capture some memory
9010
9011 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9012 // If a list item is an array section, it must specify contiguous storage.
9013 //
9014 // For this restriction it is sufficient that we make sure only references
9015 // to variables or fields and array expressions, and that no array sections
9016 // exist except in the rightmost expression. E.g. these would be invalid:
9017 //
9018 // r.ArrS[3:5].Arr[6:7]
9019 //
9020 // r.ArrS[3:5].x
9021 //
9022 // but these would be valid:
9023 // r.ArrS[3].Arr[6:7]
9024 //
9025 // r.ArrS[3].x
9026
9027 bool IsRightMostExpression = true;
9028
9029 while (!RelevantExpr) {
9030 auto AllowArraySection = IsRightMostExpression;
9031 IsRightMostExpression = false;
9032
9033 E = E->IgnoreParenImpCasts();
9034
9035 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9036 if (!isa<VarDecl>(CurE->getDecl()))
9037 break;
9038
9039 RelevantExpr = CurE;
9040 continue;
9041 }
9042
9043 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9044 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9045
9046 if (isa<CXXThisExpr>(BaseE))
9047 // We found a base expression: this->Val.
9048 RelevantExpr = CurE;
9049 else
9050 E = BaseE;
9051
9052 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9053 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9054 << CurE->getSourceRange();
9055 break;
9056 }
9057
9058 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9059
9060 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9061 // A bit-field cannot appear in a map clause.
9062 //
9063 if (FD->isBitField()) {
9064 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9065 << CurE->getSourceRange();
9066 break;
9067 }
9068
9069 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9070 // If the type of a list item is a reference to a type T then the type
9071 // will be considered to be T for all purposes of this clause.
9072 QualType CurType = BaseE->getType();
9073 if (CurType->isReferenceType())
9074 CurType = CurType->getPointeeType();
9075
9076 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9077 // A list item cannot be a variable that is a member of a structure with
9078 // a union type.
9079 //
9080 if (auto *RT = CurType->getAs<RecordType>())
9081 if (RT->isUnionType()) {
9082 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9083 << CurE->getSourceRange();
9084 break;
9085 }
9086
9087 continue;
9088 }
9089
9090 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9091 E = CurE->getBase()->IgnoreParenImpCasts();
9092
9093 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9094 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9095 << 0 << CurE->getSourceRange();
9096 break;
9097 }
9098 continue;
9099 }
9100
9101 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9102 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9103 // If a list item is an element of a structure, only the rightmost symbol
9104 // of the variable reference can be an array section.
9105 //
9106 if (!AllowArraySection) {
9107 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9108 << CurE->getSourceRange();
9109 break;
9110 }
9111
9112 E = CurE->getBase()->IgnoreParenImpCasts();
9113
9114 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9115 // If the type of a list item is a reference to a type T then the type
9116 // will be considered to be T for all purposes of this clause.
9117 QualType CurType = E->getType();
9118 if (CurType->isReferenceType())
9119 CurType = CurType->getPointeeType();
9120
9121 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9122 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9123 << 0 << CurE->getSourceRange();
9124 break;
9125 }
9126
9127 continue;
9128 }
9129
9130 // If nothing else worked, this is not a valid map clause expression.
9131 SemaRef.Diag(ELoc,
9132 diag::err_omp_expected_named_var_member_or_array_expression)
9133 << ERange;
9134 break;
9135 }
9136
9137 return RelevantExpr;
9138}
9139
9140// Return true if expression E associated with value VD has conflicts with other
9141// map information.
9142static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9143 Expr *E, bool CurrentRegionOnly) {
9144 assert(VD && E);
9145
9146 // Types used to organize the components of a valid map clause.
9147 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9148 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9149
9150 // Helper to extract the components in the map clause expression E and store
9151 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9152 // it has already passed the single clause checks.
9153 auto ExtractMapExpressionComponents = [](Expr *TE,
9154 MapExpressionComponents &MEC) {
9155 while (true) {
9156 TE = TE->IgnoreParenImpCasts();
9157
9158 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9159 MEC.push_back(
9160 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9161 break;
9162 }
9163
9164 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9165 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9166
9167 MEC.push_back(MapExpressionComponent(
9168 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9169 if (isa<CXXThisExpr>(BaseE))
9170 break;
9171
9172 TE = BaseE;
9173 continue;
9174 }
9175
9176 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9177 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9178 TE = CurE->getBase()->IgnoreParenImpCasts();
9179 continue;
9180 }
9181
9182 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9183 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9184 TE = CurE->getBase()->IgnoreParenImpCasts();
9185 continue;
9186 }
9187
9188 llvm_unreachable(
9189 "Expecting only valid map clause expressions at this point!");
9190 }
9191 };
9192
9193 SourceLocation ELoc = E->getExprLoc();
9194 SourceRange ERange = E->getSourceRange();
9195
9196 // In order to easily check the conflicts we need to match each component of
9197 // the expression under test with the components of the expressions that are
9198 // already in the stack.
9199
9200 MapExpressionComponents CurComponents;
9201 ExtractMapExpressionComponents(E, CurComponents);
9202
9203 assert(!CurComponents.empty() && "Map clause expression with no components!");
9204 assert(CurComponents.back().second == VD &&
9205 "Map clause expression with unexpected base!");
9206
9207 // Variables to help detecting enclosing problems in data environment nests.
9208 bool IsEnclosedByDataEnvironmentExpr = false;
9209 Expr *EnclosingExpr = nullptr;
9210
9211 bool FoundError =
9212 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9213 MapExpressionComponents StackComponents;
9214 ExtractMapExpressionComponents(RE, StackComponents);
9215 assert(!StackComponents.empty() &&
9216 "Map clause expression with no components!");
9217 assert(StackComponents.back().second == VD &&
9218 "Map clause expression with unexpected base!");
9219
9220 // Expressions must start from the same base. Here we detect at which
9221 // point both expressions diverge from each other and see if we can
9222 // detect if the memory referred to both expressions is contiguous and
9223 // do not overlap.
9224 auto CI = CurComponents.rbegin();
9225 auto CE = CurComponents.rend();
9226 auto SI = StackComponents.rbegin();
9227 auto SE = StackComponents.rend();
9228 for (; CI != CE && SI != SE; ++CI, ++SI) {
9229
9230 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9231 // At most one list item can be an array item derived from a given
9232 // variable in map clauses of the same construct.
9233 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9234 isa<OMPArraySectionExpr>(CI->first)) &&
9235 (isa<ArraySubscriptExpr>(SI->first) ||
9236 isa<OMPArraySectionExpr>(SI->first))) {
9237 SemaRef.Diag(CI->first->getExprLoc(),
9238 diag::err_omp_multiple_array_items_in_map_clause)
9239 << CI->first->getSourceRange();
9240 ;
9241 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9242 << SI->first->getSourceRange();
9243 return true;
9244 }
9245
9246 // Do both expressions have the same kind?
9247 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9248 break;
9249
9250 // Are we dealing with different variables/fields?
9251 if (CI->second != SI->second)
9252 break;
9253 }
9254
9255 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9256 // List items of map clauses in the same construct must not share
9257 // original storage.
9258 //
9259 // If the expressions are exactly the same or one is a subset of the
9260 // other, it means they are sharing storage.
9261 if (CI == CE && SI == SE) {
9262 if (CurrentRegionOnly) {
9263 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9264 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9265 << RE->getSourceRange();
9266 return true;
9267 } else {
9268 // If we find the same expression in the enclosing data environment,
9269 // that is legal.
9270 IsEnclosedByDataEnvironmentExpr = true;
9271 return false;
9272 }
9273 }
9274
9275 QualType DerivedType = std::prev(CI)->first->getType();
9276 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9277
9278 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9279 // If the type of a list item is a reference to a type T then the type
9280 // will be considered to be T for all purposes of this clause.
9281 if (DerivedType->isReferenceType())
9282 DerivedType = DerivedType->getPointeeType();
9283
9284 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9285 // A variable for which the type is pointer and an array section
9286 // derived from that variable must not appear as list items of map
9287 // clauses of the same construct.
9288 //
9289 // Also, cover one of the cases in:
9290 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9291 // If any part of the original storage of a list item has corresponding
9292 // storage in the device data environment, all of the original storage
9293 // must have corresponding storage in the device data environment.
9294 //
9295 if (DerivedType->isAnyPointerType()) {
9296 if (CI == CE || SI == SE) {
9297 SemaRef.Diag(
9298 DerivedLoc,
9299 diag::err_omp_pointer_mapped_along_with_derived_section)
9300 << DerivedLoc;
9301 } else {
9302 assert(CI != CE && SI != SE);
9303 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9304 << DerivedLoc;
9305 }
9306 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9307 << RE->getSourceRange();
9308 return true;
9309 }
9310
9311 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9312 // List items of map clauses in the same construct must not share
9313 // original storage.
9314 //
9315 // An expression is a subset of the other.
9316 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9317 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9318 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9319 << RE->getSourceRange();
9320 return true;
9321 }
9322
9323 // The current expression uses the same base as other expression in the
9324 // data environment but does not contain it completelly.
9325 if (!CurrentRegionOnly && SI != SE)
9326 EnclosingExpr = RE;
9327
9328 // The current expression is a subset of the expression in the data
9329 // environment.
9330 IsEnclosedByDataEnvironmentExpr |=
9331 (!CurrentRegionOnly && CI != CE && SI == SE);
9332
9333 return false;
9334 });
9335
9336 if (CurrentRegionOnly)
9337 return FoundError;
9338
9339 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9340 // If any part of the original storage of a list item has corresponding
9341 // storage in the device data environment, all of the original storage must
9342 // have corresponding storage in the device data environment.
9343 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9344 // If a list item is an element of a structure, and a different element of
9345 // the structure has a corresponding list item in the device data environment
9346 // prior to a task encountering the construct associated with the map clause,
9347 // then the list item must also have a correspnding list item in the device
9348 // data environment prior to the task encountering the construct.
9349 //
9350 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9351 SemaRef.Diag(ELoc,
9352 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9353 << ERange;
9354 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9355 << EnclosingExpr->getSourceRange();
9356 return true;
9357 }
9358
9359 return FoundError;
9360}
9361
Samuel Antao23abd722016-01-19 20:40:49 +00009362OMPClause *
9363Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9364 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9365 SourceLocation MapLoc, SourceLocation ColonLoc,
9366 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9367 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009368 SmallVector<Expr *, 4> Vars;
9369
9370 for (auto &RE : VarList) {
9371 assert(RE && "Null expr in omp map");
9372 if (isa<DependentScopeDeclRefExpr>(RE)) {
9373 // It will be analyzed later.
9374 Vars.push_back(RE);
9375 continue;
9376 }
9377 SourceLocation ELoc = RE->getExprLoc();
9378
Kelvin Li0bff7af2015-11-23 05:32:03 +00009379 auto *VE = RE->IgnoreParenLValueCasts();
9380
9381 if (VE->isValueDependent() || VE->isTypeDependent() ||
9382 VE->isInstantiationDependent() ||
9383 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009384 // We can only analyze this information once the missing information is
9385 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009386 Vars.push_back(RE);
9387 continue;
9388 }
9389
9390 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009391
Samuel Antao5de996e2016-01-22 20:21:36 +00009392 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9393 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9394 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009395 continue;
9396 }
9397
Samuel Antao5de996e2016-01-22 20:21:36 +00009398 // Obtain the array or member expression bases if required.
9399 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9400 if (!BE)
9401 continue;
9402
9403 // If the base is a reference to a variable, we rely on that variable for
9404 // the following checks. If it is a 'this' expression we rely on the field.
9405 ValueDecl *D = nullptr;
9406 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9407 D = DRE->getDecl();
9408 } else {
9409 auto *ME = cast<MemberExpr>(BE);
9410 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9411 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009412 }
9413 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009414
Samuel Antao5de996e2016-01-22 20:21:36 +00009415 auto *VD = dyn_cast<VarDecl>(D);
9416 auto *FD = dyn_cast<FieldDecl>(D);
9417
9418 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009419 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009420
9421 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9422 // threadprivate variables cannot appear in a map clause.
9423 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009424 auto DVar = DSAStack->getTopDSA(VD, false);
9425 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9426 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9427 continue;
9428 }
9429
Samuel Antao5de996e2016-01-22 20:21:36 +00009430 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9431 // A list item cannot appear in both a map clause and a data-sharing
9432 // attribute clause on the same construct.
9433 //
9434 // TODO: Implement this check - it cannot currently be tested because of
9435 // missing implementation of the other data sharing clauses in target
9436 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009437
Samuel Antao5de996e2016-01-22 20:21:36 +00009438 // Check conflicts with other map clause expressions. We check the conflicts
9439 // with the current construct separately from the enclosing data
9440 // environment, because the restrictions are different.
9441 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9442 /*CurrentRegionOnly=*/true))
9443 break;
9444 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9445 /*CurrentRegionOnly=*/false))
9446 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009447
Samuel Antao5de996e2016-01-22 20:21:36 +00009448 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9449 // If the type of a list item is a reference to a type T then the type will
9450 // be considered to be T for all purposes of this clause.
9451 QualType Type = D->getType();
9452 if (Type->isReferenceType())
9453 Type = Type->getPointeeType();
9454
9455 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009456 // A list item must have a mappable type.
9457 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9458 DSAStack, Type))
9459 continue;
9460
Samuel Antaodf67fc42016-01-19 19:15:56 +00009461 // target enter data
9462 // OpenMP [2.10.2, Restrictions, p. 99]
9463 // A map-type must be specified in all map clauses and must be either
9464 // to or alloc.
9465 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9466 if (DKind == OMPD_target_enter_data &&
9467 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9468 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009469 << (IsMapTypeImplicit ? 1 : 0)
9470 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009471 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009472 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009473 }
9474
Samuel Antao72590762016-01-19 20:04:50 +00009475 // target exit_data
9476 // OpenMP [2.10.3, Restrictions, p. 102]
9477 // A map-type must be specified in all map clauses and must be either
9478 // from, release, or delete.
9479 DKind = DSAStack->getCurrentDirective();
9480 if (DKind == OMPD_target_exit_data &&
9481 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9482 MapType == OMPC_MAP_delete)) {
9483 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009484 << (IsMapTypeImplicit ? 1 : 0)
9485 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009486 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009487 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009488 }
9489
Kelvin Li0bff7af2015-11-23 05:32:03 +00009490 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009491 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009492 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009493
Samuel Antao5de996e2016-01-22 20:21:36 +00009494 // We need to produce a map clause even if we don't have variables so that
9495 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009496 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009497 MapTypeModifier, MapType, IsMapTypeImplicit,
9498 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009499}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009500
9501OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9502 SourceLocation StartLoc,
9503 SourceLocation LParenLoc,
9504 SourceLocation EndLoc) {
9505 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009506
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009507 // OpenMP [teams Constrcut, Restrictions]
9508 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009509 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9510 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009511 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009512
9513 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9514}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009515
9516OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9517 SourceLocation StartLoc,
9518 SourceLocation LParenLoc,
9519 SourceLocation EndLoc) {
9520 Expr *ValExpr = ThreadLimit;
9521
9522 // OpenMP [teams Constrcut, Restrictions]
9523 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009524 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9525 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009526 return nullptr;
9527
9528 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9529 EndLoc);
9530}
Alexey Bataeva0569352015-12-01 10:17:31 +00009531
9532OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9533 SourceLocation StartLoc,
9534 SourceLocation LParenLoc,
9535 SourceLocation EndLoc) {
9536 Expr *ValExpr = Priority;
9537
9538 // OpenMP [2.9.1, task Constrcut]
9539 // The priority-value is a non-negative numerical scalar expression.
9540 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9541 /*StrictlyPositive=*/false))
9542 return nullptr;
9543
9544 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9545}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009546
9547OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9548 SourceLocation StartLoc,
9549 SourceLocation LParenLoc,
9550 SourceLocation EndLoc) {
9551 Expr *ValExpr = Grainsize;
9552
9553 // OpenMP [2.9.2, taskloop Constrcut]
9554 // The parameter of the grainsize clause must be a positive integer
9555 // expression.
9556 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9557 /*StrictlyPositive=*/true))
9558 return nullptr;
9559
9560 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9561}
Alexey Bataev382967a2015-12-08 12:06:20 +00009562
9563OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9564 SourceLocation StartLoc,
9565 SourceLocation LParenLoc,
9566 SourceLocation EndLoc) {
9567 Expr *ValExpr = NumTasks;
9568
9569 // OpenMP [2.9.2, taskloop Constrcut]
9570 // The parameter of the num_tasks clause must be a positive integer
9571 // expression.
9572 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9573 /*StrictlyPositive=*/true))
9574 return nullptr;
9575
9576 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9577}
9578
Alexey Bataev28c75412015-12-15 08:19:24 +00009579OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9580 SourceLocation LParenLoc,
9581 SourceLocation EndLoc) {
9582 // OpenMP [2.13.2, critical construct, Description]
9583 // ... where hint-expression is an integer constant expression that evaluates
9584 // to a valid lock hint.
9585 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9586 if (HintExpr.isInvalid())
9587 return nullptr;
9588 return new (Context)
9589 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9590}
9591
Carlo Bertollib4adf552016-01-15 18:50:31 +00009592OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9593 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9594 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9595 SourceLocation EndLoc) {
9596 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9597 std::string Values;
9598 Values += "'";
9599 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9600 Values += "'";
9601 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9602 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9603 return nullptr;
9604 }
9605 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009606 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009607 if (ChunkSize) {
9608 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9609 !ChunkSize->isInstantiationDependent() &&
9610 !ChunkSize->containsUnexpandedParameterPack()) {
9611 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9612 ExprResult Val =
9613 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9614 if (Val.isInvalid())
9615 return nullptr;
9616
9617 ValExpr = Val.get();
9618
9619 // OpenMP [2.7.1, Restrictions]
9620 // chunk_size must be a loop invariant integer expression with a positive
9621 // value.
9622 llvm::APSInt Result;
9623 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9624 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9625 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9626 << "dist_schedule" << ChunkSize->getSourceRange();
9627 return nullptr;
9628 }
9629 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev3392d762016-02-16 11:18:12 +00009630 ValExpr = buildCapture(*this, ".chunk.", ValExpr);
9631 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
9632 HelperValStmt =
9633 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
9634 /*NumDecls=*/1),
9635 SourceLocation(), SourceLocation());
9636 ValExpr = DefaultLvalueConversion(ValExpr).get();
Carlo Bertollib4adf552016-01-15 18:50:31 +00009637 }
9638 }
9639 }
9640
9641 return new (Context)
9642 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +00009643 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +00009644}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009645
9646OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9647 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9648 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9649 SourceLocation KindLoc, SourceLocation EndLoc) {
9650 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9651 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9652 Kind != OMPC_DEFAULTMAP_scalar) {
9653 std::string Value;
9654 SourceLocation Loc;
9655 Value += "'";
9656 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9657 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9658 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9659 Loc = MLoc;
9660 } else {
9661 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9662 OMPC_DEFAULTMAP_scalar);
9663 Loc = KindLoc;
9664 }
9665 Value += "'";
9666 Diag(Loc, diag::err_omp_unexpected_clause_value)
9667 << Value << getOpenMPClauseName(OMPC_defaultmap);
9668 return nullptr;
9669 }
9670
9671 return new (Context)
9672 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9673}