blob: d4c1f98c99897cb2da547954a6cd1bdeccdc8b42 [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 Bataev7ff55242014-06-19 09:13:45 +0000672 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 // Variables with const-qualified type having no mutable member may be
674 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000675 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
676 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000677 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
678 return DVar;
679
Alexey Bataev758e55e2013-09-06 18:03:48 +0000680 DVar.CKind = OMPC_shared;
681 return DVar;
682 }
683
Alexey Bataev758e55e2013-09-06 18:03:48 +0000684 // Explicitly specified attributes and local variables with predetermined
685 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000686 auto StartI = std::next(Stack.rbegin());
687 auto EndI = std::prev(Stack.rend());
688 if (FromParent && StartI != EndI) {
689 StartI = std::next(StartI);
690 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000691 auto I = std::prev(StartI);
692 if (I->SharingMap.count(D)) {
693 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000694 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000695 DVar.CKind = I->SharingMap[D].Attributes;
696 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000697 }
698
699 return DVar;
700}
701
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000702DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
703 bool FromParent) {
704 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000705 auto StartI = Stack.rbegin();
706 auto EndI = std::prev(Stack.rend());
707 if (FromParent && StartI != EndI) {
708 StartI = std::next(StartI);
709 }
710 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000711}
712
Alexey Bataevf29276e2014-06-18 04:14:57 +0000713template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000714DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000715 DirectivesPredicate DPred,
716 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000717 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000718 auto StartI = std::next(Stack.rbegin());
719 auto EndI = std::prev(Stack.rend());
720 if (FromParent && StartI != EndI) {
721 StartI = std::next(StartI);
722 }
723 for (auto I = StartI, EE = EndI; I != EE; ++I) {
724 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000725 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000726 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000727 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000728 return DVar;
729 }
730 return DSAVarData();
731}
732
Alexey Bataevf29276e2014-06-18 04:14:57 +0000733template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000734DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000736 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000737 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 auto StartI = std::next(Stack.rbegin());
739 auto EndI = std::prev(Stack.rend());
740 if (FromParent && StartI != EndI) {
741 StartI = std::next(StartI);
742 }
743 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000744 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000745 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000746 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000748 return DVar;
749 return DSAVarData();
750 }
751 return DSAVarData();
752}
753
Alexey Bataevaac108a2015-06-23 04:51:00 +0000754bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000755 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000756 unsigned Level) {
757 if (CPred(ClauseKindMode))
758 return true;
759 if (isClauseParsingMode())
760 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000761 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000762 auto StartI = Stack.rbegin();
763 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000764 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000765 return false;
766 std::advance(StartI, Level);
767 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
768 CPred(StartI->SharingMap[D].Attributes);
769}
770
Samuel Antao4be30e92015-10-02 17:14:03 +0000771bool DSAStackTy::hasExplicitDirective(
772 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
773 unsigned Level) {
774 if (isClauseParsingMode())
775 ++Level;
776 auto StartI = Stack.rbegin();
777 auto EndI = std::prev(Stack.rend());
778 if (std::distance(StartI, EndI) <= (int)Level)
779 return false;
780 std::advance(StartI, Level);
781 return DPred(StartI->Directive);
782}
783
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000784template <class NamedDirectivesPredicate>
785bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
786 auto StartI = std::next(Stack.rbegin());
787 auto EndI = std::prev(Stack.rend());
788 if (FromParent && StartI != EndI) {
789 StartI = std::next(StartI);
790 }
791 for (auto I = StartI, EE = EndI; I != EE; ++I) {
792 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
793 return true;
794 }
795 return false;
796}
797
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000798OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
799 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
800 if (I->CurScope == S)
801 return I->Directive;
802 return OMPD_unknown;
803}
804
Alexey Bataev758e55e2013-09-06 18:03:48 +0000805void Sema::InitDataSharingAttributesStack() {
806 VarDataSharingAttributesStack = new DSAStackTy(*this);
807}
808
809#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
810
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000811bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000812 const CapturedRegionScopeInfo *RSI) {
813 assert(LangOpts.OpenMP && "OpenMP is not allowed");
814
815 auto &Ctx = getASTContext();
816 bool IsByRef = true;
817
818 // Find the directive that is associated with the provided scope.
819 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000820 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000821
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000822 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000823 // This table summarizes how a given variable should be passed to the device
824 // given its type and the clauses where it appears. This table is based on
825 // the description in OpenMP 4.5 [2.10.4, target Construct] and
826 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
827 //
828 // =========================================================================
829 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
830 // | |(tofrom:scalar)| | pvt | | | |
831 // =========================================================================
832 // | scl | | | | - | | bycopy|
833 // | scl | | - | x | - | - | bycopy|
834 // | scl | | x | - | - | - | null |
835 // | scl | x | | | - | | byref |
836 // | scl | x | - | x | - | - | bycopy|
837 // | scl | x | x | - | - | - | null |
838 // | scl | | - | - | - | x | byref |
839 // | scl | x | - | - | - | x | byref |
840 //
841 // | agg | n.a. | | | - | | byref |
842 // | agg | n.a. | - | x | - | - | byref |
843 // | agg | n.a. | x | - | - | - | null |
844 // | agg | n.a. | - | - | - | x | byref |
845 // | agg | n.a. | - | - | - | x[] | byref |
846 //
847 // | ptr | n.a. | | | - | | bycopy|
848 // | ptr | n.a. | - | x | - | - | bycopy|
849 // | ptr | n.a. | x | - | - | - | null |
850 // | ptr | n.a. | - | - | - | x | byref |
851 // | ptr | n.a. | - | - | - | x[] | bycopy|
852 // | ptr | n.a. | - | - | x | | bycopy|
853 // | ptr | n.a. | - | - | x | x | bycopy|
854 // | ptr | n.a. | - | - | x | x[] | bycopy|
855 // =========================================================================
856 // Legend:
857 // scl - scalar
858 // ptr - pointer
859 // agg - aggregate
860 // x - applies
861 // - - invalid in this combination
862 // [] - mapped with an array section
863 // byref - should be mapped by reference
864 // byval - should be mapped by value
865 // null - initialize a local variable to null on the device
866 //
867 // Observations:
868 // - All scalar declarations that show up in a map clause have to be passed
869 // by reference, because they may have been mapped in the enclosing data
870 // environment.
871 // - If the scalar value does not fit the size of uintptr, it has to be
872 // passed by reference, regardless the result in the table above.
873 // - For pointers mapped by value that have either an implicit map or an
874 // array section, the runtime library may pass the NULL value to the
875 // device instead of the value passed to it by the compiler.
876
877 // FIXME: Right now, only implicit maps are implemented. Properly mapping
878 // values requires having the map, private, and firstprivate clauses SEMA
879 // and parsing in place, which we don't yet.
880
881 if (Ty->isReferenceType())
882 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
883 IsByRef = !Ty->isScalarType();
884 }
885
886 // When passing data by value, we need to make sure it fits the uintptr size
887 // and alignment, because the runtime library only deals with uintptr types.
888 // If it does not fit the uintptr size, we need to pass the data by reference
889 // instead.
890 if (!IsByRef &&
891 (Ctx.getTypeSizeInChars(Ty) >
892 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000893 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000894 IsByRef = true;
895
896 return IsByRef;
897}
898
Alexey Bataev90c228f2016-02-08 09:29:13 +0000899VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000900 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000901 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000902
903 // If we are attempting to capture a global variable in a directive with
904 // 'target' we return true so that this global is also mapped to the device.
905 //
906 // FIXME: If the declaration is enclosed in a 'declare target' directive,
907 // then it should not be captured. Therefore, an extra check has to be
908 // inserted here once support for 'declare target' is added.
909 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000910 auto *VD = dyn_cast<VarDecl>(D);
911 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000912 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000913 !DSAStack->isClauseParsingMode())
914 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000915 if (DSAStack->getCurScope() &&
916 DSAStack->hasDirective(
917 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
918 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000919 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000920 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000921 false))
922 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000923 }
924
Alexey Bataev48977c32015-08-04 08:10:48 +0000925 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
926 (!DSAStack->isClauseParsingMode() ||
927 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000928 if (DSAStack->isLoopControlVariable(D) ||
929 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000930 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000931 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000932 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000933 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000934 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000935 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000936 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000937 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000938 if (DVarPrivate.CKind != OMPC_unknown)
939 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000940 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000941 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000942}
943
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000944bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000945 assert(LangOpts.OpenMP && "OpenMP is not allowed");
946 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000947 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000948}
949
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000950bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000951 assert(LangOpts.OpenMP && "OpenMP is not allowed");
952 // Return true if the current level is no longer enclosed in a target region.
953
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000954 auto *VD = dyn_cast<VarDecl>(D);
955 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000956 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
957 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000958}
959
Alexey Bataeved09d242014-05-28 05:53:51 +0000960void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000961
962void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
963 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000964 Scope *CurScope, SourceLocation Loc) {
965 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000966 PushExpressionEvaluationContext(PotentiallyEvaluated);
967}
968
Alexey Bataevaac108a2015-06-23 04:51:00 +0000969void Sema::StartOpenMPClause(OpenMPClauseKind K) {
970 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000971}
972
Alexey Bataevaac108a2015-06-23 04:51:00 +0000973void Sema::EndOpenMPClause() {
974 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000975}
976
Alexey Bataev758e55e2013-09-06 18:03:48 +0000977void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000978 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
979 // A variable of class type (or array thereof) that appears in a lastprivate
980 // clause requires an accessible, unambiguous default constructor for the
981 // class type, unless the list item is also specified in a firstprivate
982 // clause.
983 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000984 for (auto *C : D->clauses()) {
985 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
986 SmallVector<Expr *, 8> PrivateCopies;
987 for (auto *DE : Clause->varlists()) {
988 if (DE->isValueDependent() || DE->isTypeDependent()) {
989 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000990 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000991 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000992 DE = DE->IgnoreParens();
993 VarDecl *VD = nullptr;
994 FieldDecl *FD = nullptr;
995 ValueDecl *D;
996 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
997 VD = cast<VarDecl>(DRE->getDecl());
998 D = VD;
999 } else {
1000 assert(isa<MemberExpr>(DE));
1001 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
1002 D = FD;
1003 }
1004 QualType Type = D->getType().getNonReferenceType();
1005 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001006 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001007 // Generate helper private variable and initialize it with the
1008 // default value. The address of the original variable is replaced
1009 // by the address of the new private variable in CodeGen. This new
1010 // variable is not added to IdResolver, so the code in the OpenMP
1011 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001012 auto *VDPrivate = buildVarDecl(
1013 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001014 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001015 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1016 if (VDPrivate->isInvalidDecl())
1017 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001018 PrivateCopies.push_back(buildDeclRefExpr(
1019 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001020 } else {
1021 // The variable is also a firstprivate, so initialization sequence
1022 // for private copy is generated already.
1023 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001024 }
1025 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001026 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001027 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001028 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001029 }
1030 }
1031 }
1032
Alexey Bataev758e55e2013-09-06 18:03:48 +00001033 DSAStack->pop();
1034 DiscardCleanupsInEvaluationContext();
1035 PopExpressionEvaluationContext();
1036}
1037
Alexander Musman3276a272015-03-21 10:12:56 +00001038static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1039 Expr *NumIterations, Sema &SemaRef,
1040 Scope *S);
1041
Alexey Bataeva769e072013-03-22 06:34:35 +00001042namespace {
1043
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001044class VarDeclFilterCCC : public CorrectionCandidateCallback {
1045private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001046 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001047
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001049 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001050 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001051 NamedDecl *ND = Candidate.getCorrectionDecl();
1052 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1053 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001054 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1055 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001056 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001058 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059};
Alexey Bataeved09d242014-05-28 05:53:51 +00001060} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001061
1062ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1063 CXXScopeSpec &ScopeSpec,
1064 const DeclarationNameInfo &Id) {
1065 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1066 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1067
1068 if (Lookup.isAmbiguous())
1069 return ExprError();
1070
1071 VarDecl *VD;
1072 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001073 if (TypoCorrection Corrected = CorrectTypo(
1074 Id, LookupOrdinaryName, CurScope, nullptr,
1075 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001076 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001077 PDiag(Lookup.empty()
1078 ? diag::err_undeclared_var_use_suggest
1079 : diag::err_omp_expected_var_arg_suggest)
1080 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001081 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001082 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001083 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1084 : diag::err_omp_expected_var_arg)
1085 << Id.getName();
1086 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001087 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001088 } else {
1089 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001090 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001091 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1092 return ExprError();
1093 }
1094 }
1095 Lookup.suppressDiagnostics();
1096
1097 // OpenMP [2.9.2, Syntax, C/C++]
1098 // Variables must be file-scope, namespace-scope, or static block-scope.
1099 if (!VD->hasGlobalStorage()) {
1100 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001101 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1102 bool IsDecl =
1103 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001104 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001105 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1106 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001107 return ExprError();
1108 }
1109
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001110 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1111 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001112 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1113 // A threadprivate directive for file-scope variables must appear outside
1114 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001115 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1116 !getCurLexicalContext()->isTranslationUnit()) {
1117 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001118 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1119 bool IsDecl =
1120 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1121 Diag(VD->getLocation(),
1122 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1123 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001124 return ExprError();
1125 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001126 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1127 // A threadprivate directive for static class member variables must appear
1128 // in the class definition, in the same scope in which the member
1129 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001130 if (CanonicalVD->isStaticDataMember() &&
1131 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1132 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001133 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1134 bool IsDecl =
1135 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1136 Diag(VD->getLocation(),
1137 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1138 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001139 return ExprError();
1140 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001141 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1142 // A threadprivate directive for namespace-scope variables must appear
1143 // outside any definition or declaration other than the namespace
1144 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001145 if (CanonicalVD->getDeclContext()->isNamespace() &&
1146 (!getCurLexicalContext()->isFileContext() ||
1147 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1148 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001149 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1150 bool IsDecl =
1151 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1152 Diag(VD->getLocation(),
1153 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1154 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001155 return ExprError();
1156 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001157 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1158 // A threadprivate directive for static block-scope variables must appear
1159 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001160 if (CanonicalVD->isStaticLocal() && CurScope &&
1161 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001162 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001163 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1164 bool IsDecl =
1165 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1166 Diag(VD->getLocation(),
1167 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1168 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 return ExprError();
1170 }
1171
1172 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1173 // A threadprivate directive must lexically precede all references to any
1174 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001175 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001177 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 return ExprError();
1179 }
1180
1181 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001182 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001183 return DE;
1184}
1185
Alexey Bataeved09d242014-05-28 05:53:51 +00001186Sema::DeclGroupPtrTy
1187Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1188 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001189 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001190 CurContext->addDecl(D);
1191 return DeclGroupPtrTy::make(DeclGroupRef(D));
1192 }
David Blaikie0403cb12016-01-15 23:43:25 +00001193 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001194}
1195
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001196namespace {
1197class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1198 Sema &SemaRef;
1199
1200public:
1201 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1202 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1203 if (VD->hasLocalStorage()) {
1204 SemaRef.Diag(E->getLocStart(),
1205 diag::err_omp_local_var_in_threadprivate_init)
1206 << E->getSourceRange();
1207 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1208 << VD << VD->getSourceRange();
1209 return true;
1210 }
1211 }
1212 return false;
1213 }
1214 bool VisitStmt(const Stmt *S) {
1215 for (auto Child : S->children()) {
1216 if (Child && Visit(Child))
1217 return true;
1218 }
1219 return false;
1220 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001221 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001222};
1223} // namespace
1224
Alexey Bataeved09d242014-05-28 05:53:51 +00001225OMPThreadPrivateDecl *
1226Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001227 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001228 for (auto &RefExpr : VarList) {
1229 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1231 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001232
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001233 QualType QType = VD->getType();
1234 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1235 // It will be analyzed later.
1236 Vars.push_back(DE);
1237 continue;
1238 }
1239
Alexey Bataeva769e072013-03-22 06:34:35 +00001240 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1241 // A threadprivate variable must not have an incomplete type.
1242 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001243 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001244 continue;
1245 }
1246
1247 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1248 // A threadprivate variable must not have a reference type.
1249 if (VD->getType()->isReferenceType()) {
1250 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001251 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1252 bool IsDecl =
1253 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1254 Diag(VD->getLocation(),
1255 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1256 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001257 continue;
1258 }
1259
Samuel Antaof8b50122015-07-13 22:54:53 +00001260 // Check if this is a TLS variable. If TLS is not being supported, produce
1261 // the corresponding diagnostic.
1262 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1263 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1264 getLangOpts().OpenMPUseTLS &&
1265 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001266 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1267 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001268 Diag(ILoc, diag::err_omp_var_thread_local)
1269 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001270 bool IsDecl =
1271 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1272 Diag(VD->getLocation(),
1273 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1274 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001275 continue;
1276 }
1277
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001278 // Check if initial value of threadprivate variable reference variable with
1279 // local storage (it is not supported by runtime).
1280 if (auto Init = VD->getAnyInitializer()) {
1281 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001282 if (Checker.Visit(Init))
1283 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001284 }
1285
Alexey Bataeved09d242014-05-28 05:53:51 +00001286 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001287 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001288 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1289 Context, SourceRange(Loc, Loc)));
1290 if (auto *ML = Context.getASTMutationListener())
1291 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001292 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001293 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001294 if (!Vars.empty()) {
1295 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1296 Vars);
1297 D->setAccess(AS_public);
1298 }
1299 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001300}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001301
Alexey Bataev7ff55242014-06-19 09:13:45 +00001302static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001303 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001304 bool IsLoopIterVar = false) {
1305 if (DVar.RefExpr) {
1306 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1307 << getOpenMPClauseName(DVar.CKind);
1308 return;
1309 }
1310 enum {
1311 PDSA_StaticMemberShared,
1312 PDSA_StaticLocalVarShared,
1313 PDSA_LoopIterVarPrivate,
1314 PDSA_LoopIterVarLinear,
1315 PDSA_LoopIterVarLastprivate,
1316 PDSA_ConstVarShared,
1317 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001319 PDSA_LocalVarPrivate,
1320 PDSA_Implicit
1321 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001322 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001323 auto ReportLoc = D->getLocation();
1324 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001325 if (IsLoopIterVar) {
1326 if (DVar.CKind == OMPC_private)
1327 Reason = PDSA_LoopIterVarPrivate;
1328 else if (DVar.CKind == OMPC_lastprivate)
1329 Reason = PDSA_LoopIterVarLastprivate;
1330 else
1331 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001332 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1333 Reason = PDSA_TaskVarFirstprivate;
1334 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001335 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001336 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001337 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001338 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001339 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001340 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001341 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001342 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001343 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001344 ReportHint = true;
1345 Reason = PDSA_LocalVarPrivate;
1346 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001347 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001348 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001349 << Reason << ReportHint
1350 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1351 } else if (DVar.ImplicitDSALoc.isValid()) {
1352 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1353 << getOpenMPClauseName(DVar.CKind);
1354 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001355}
1356
Alexey Bataev758e55e2013-09-06 18:03:48 +00001357namespace {
1358class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1359 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001360 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001361 bool ErrorFound;
1362 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001363 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001364 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001365
Alexey Bataev758e55e2013-09-06 18:03:48 +00001366public:
1367 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001368 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001370 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1371 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001372
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001373 auto DVar = Stack->getTopDSA(VD, false);
1374 // Check if the variable has explicit DSA set and stop analysis if it so.
1375 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001376
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001377 auto ELoc = E->getExprLoc();
1378 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001379 // The default(none) clause requires that each variable that is referenced
1380 // in the construct, and does not have a predetermined data-sharing
1381 // attribute, must have its data-sharing attribute explicitly determined
1382 // by being listed in a data-sharing attribute clause.
1383 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001384 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001385 VarsWithInheritedDSA.count(VD) == 0) {
1386 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001387 return;
1388 }
1389
1390 // OpenMP [2.9.3.6, Restrictions, p.2]
1391 // A list item that appears in a reduction clause of the innermost
1392 // enclosing worksharing or parallel construct may not be accessed in an
1393 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001394 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001395 [](OpenMPDirectiveKind K) -> bool {
1396 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001397 isOpenMPWorksharingDirective(K) ||
1398 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001399 },
1400 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001401 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1402 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001403 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1404 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001405 return;
1406 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001407
1408 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001409 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001410 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001411 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001412 }
1413 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001414 void VisitMemberExpr(MemberExpr *E) {
1415 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1416 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1417 auto DVar = Stack->getTopDSA(FD, false);
1418 // Check if the variable has explicit DSA set and stop analysis if it
1419 // so.
1420 if (DVar.RefExpr)
1421 return;
1422
1423 auto ELoc = E->getExprLoc();
1424 auto DKind = Stack->getCurrentDirective();
1425 // OpenMP [2.9.3.6, Restrictions, p.2]
1426 // A list item that appears in a reduction clause of the innermost
1427 // enclosing worksharing or parallel construct may not be accessed in
1428 // an
1429 // explicit task.
1430 DVar =
1431 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1432 [](OpenMPDirectiveKind K) -> bool {
1433 return isOpenMPParallelDirective(K) ||
1434 isOpenMPWorksharingDirective(K) ||
1435 isOpenMPTeamsDirective(K);
1436 },
1437 false);
1438 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1439 ErrorFound = true;
1440 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1441 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1442 return;
1443 }
1444
1445 // Define implicit data-sharing attributes for task.
1446 DVar = Stack->getImplicitDSA(FD, false);
1447 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1448 ImplicitFirstprivate.push_back(E);
1449 }
1450 }
1451 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001452 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001453 for (auto *C : S->clauses()) {
1454 // Skip analysis of arguments of implicitly defined firstprivate clause
1455 // for task directives.
1456 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1457 for (auto *CC : C->children()) {
1458 if (CC)
1459 Visit(CC);
1460 }
1461 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462 }
1463 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001464 for (auto *C : S->children()) {
1465 if (C && !isa<OMPExecutableDirective>(C))
1466 Visit(C);
1467 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001468 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469
1470 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001471 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001472 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001473 return VarsWithInheritedDSA;
1474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
Alexey Bataev7ff55242014-06-19 09:13:45 +00001476 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1477 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478};
Alexey Bataeved09d242014-05-28 05:53:51 +00001479} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001480
Alexey Bataevbae9a792014-06-27 10:37:06 +00001481void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001482 switch (DKind) {
1483 case OMPD_parallel: {
1484 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001485 QualType KmpInt32PtrTy =
1486 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001487 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001488 std::make_pair(".global_tid.", KmpInt32PtrTy),
1489 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1490 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001491 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001492 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1493 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001494 break;
1495 }
1496 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001497 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001498 std::make_pair(StringRef(), QualType()) // __context with shared vars
1499 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001500 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1501 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001502 break;
1503 }
1504 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001505 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001506 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001507 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001508 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1509 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001510 break;
1511 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001512 case OMPD_for_simd: {
1513 Sema::CapturedParamNameType Params[] = {
1514 std::make_pair(StringRef(), QualType()) // __context with shared vars
1515 };
1516 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1517 Params);
1518 break;
1519 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001520 case OMPD_sections: {
1521 Sema::CapturedParamNameType Params[] = {
1522 std::make_pair(StringRef(), QualType()) // __context with shared vars
1523 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001524 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1525 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001526 break;
1527 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001528 case OMPD_section: {
1529 Sema::CapturedParamNameType Params[] = {
1530 std::make_pair(StringRef(), QualType()) // __context with shared vars
1531 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001532 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1533 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001534 break;
1535 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001536 case OMPD_single: {
1537 Sema::CapturedParamNameType Params[] = {
1538 std::make_pair(StringRef(), QualType()) // __context with shared vars
1539 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001540 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1541 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542 break;
1543 }
Alexander Musman80c22892014-07-17 08:54:58 +00001544 case OMPD_master: {
1545 Sema::CapturedParamNameType Params[] = {
1546 std::make_pair(StringRef(), QualType()) // __context with shared vars
1547 };
1548 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1549 Params);
1550 break;
1551 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001552 case OMPD_critical: {
1553 Sema::CapturedParamNameType Params[] = {
1554 std::make_pair(StringRef(), QualType()) // __context with shared vars
1555 };
1556 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1557 Params);
1558 break;
1559 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001560 case OMPD_parallel_for: {
1561 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001562 QualType KmpInt32PtrTy =
1563 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001564 Sema::CapturedParamNameType Params[] = {
1565 std::make_pair(".global_tid.", KmpInt32PtrTy),
1566 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1567 std::make_pair(StringRef(), QualType()) // __context with shared vars
1568 };
1569 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1570 Params);
1571 break;
1572 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001573 case OMPD_parallel_for_simd: {
1574 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001575 QualType KmpInt32PtrTy =
1576 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001577 Sema::CapturedParamNameType Params[] = {
1578 std::make_pair(".global_tid.", KmpInt32PtrTy),
1579 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1580 std::make_pair(StringRef(), QualType()) // __context with shared vars
1581 };
1582 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1583 Params);
1584 break;
1585 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001586 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001587 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001588 QualType KmpInt32PtrTy =
1589 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001590 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001591 std::make_pair(".global_tid.", KmpInt32PtrTy),
1592 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001593 std::make_pair(StringRef(), QualType()) // __context with shared vars
1594 };
1595 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1596 Params);
1597 break;
1598 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001599 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001600 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001601 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1602 FunctionProtoType::ExtProtoInfo EPI;
1603 EPI.Variadic = true;
1604 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001605 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001606 std::make_pair(".global_tid.", KmpInt32Ty),
1607 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001608 std::make_pair(".privates.",
1609 Context.VoidPtrTy.withConst().withRestrict()),
1610 std::make_pair(
1611 ".copy_fn.",
1612 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001613 std::make_pair(StringRef(), QualType()) // __context with shared vars
1614 };
1615 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1616 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001617 // Mark this captured region as inlined, because we don't use outlined
1618 // function directly.
1619 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1620 AlwaysInlineAttr::CreateImplicit(
1621 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001622 break;
1623 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001624 case OMPD_ordered: {
1625 Sema::CapturedParamNameType Params[] = {
1626 std::make_pair(StringRef(), QualType()) // __context with shared vars
1627 };
1628 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1629 Params);
1630 break;
1631 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001632 case OMPD_atomic: {
1633 Sema::CapturedParamNameType Params[] = {
1634 std::make_pair(StringRef(), QualType()) // __context with shared vars
1635 };
1636 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1637 Params);
1638 break;
1639 }
Michael Wong65f367f2015-07-21 13:44:28 +00001640 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001641 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001642 case OMPD_target_parallel:
1643 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001644 Sema::CapturedParamNameType Params[] = {
1645 std::make_pair(StringRef(), QualType()) // __context with shared vars
1646 };
1647 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1648 Params);
1649 break;
1650 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001651 case OMPD_teams: {
1652 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001653 QualType KmpInt32PtrTy =
1654 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001655 Sema::CapturedParamNameType Params[] = {
1656 std::make_pair(".global_tid.", KmpInt32PtrTy),
1657 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1658 std::make_pair(StringRef(), QualType()) // __context with shared vars
1659 };
1660 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1661 Params);
1662 break;
1663 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001664 case OMPD_taskgroup: {
1665 Sema::CapturedParamNameType Params[] = {
1666 std::make_pair(StringRef(), QualType()) // __context with shared vars
1667 };
1668 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1669 Params);
1670 break;
1671 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001672 case OMPD_taskloop: {
1673 Sema::CapturedParamNameType Params[] = {
1674 std::make_pair(StringRef(), QualType()) // __context with shared vars
1675 };
1676 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1677 Params);
1678 break;
1679 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001680 case OMPD_taskloop_simd: {
1681 Sema::CapturedParamNameType Params[] = {
1682 std::make_pair(StringRef(), QualType()) // __context with shared vars
1683 };
1684 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1685 Params);
1686 break;
1687 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001688 case OMPD_distribute: {
1689 Sema::CapturedParamNameType Params[] = {
1690 std::make_pair(StringRef(), QualType()) // __context with shared vars
1691 };
1692 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1693 Params);
1694 break;
1695 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001696 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001697 case OMPD_taskyield:
1698 case OMPD_barrier:
1699 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001700 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001701 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001702 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001703 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001704 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001705 llvm_unreachable("OpenMP Directive is not allowed");
1706 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001707 llvm_unreachable("Unknown OpenMP directive");
1708 }
1709}
1710
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001711StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1712 ArrayRef<OMPClause *> Clauses) {
1713 if (!S.isUsable()) {
1714 ActOnCapturedRegionError();
1715 return StmtError();
1716 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001717
1718 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001719 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001720 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001721 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001722 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001723 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001724 Clause->getClauseKind() == OMPC_copyprivate ||
1725 (getLangOpts().OpenMPUseTLS &&
1726 getASTContext().getTargetInfo().isTLSSupported() &&
1727 Clause->getClauseKind() == OMPC_copyin)) {
1728 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001729 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001730 for (auto *VarRef : Clause->children()) {
1731 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001732 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001733 }
1734 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001735 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001736 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1737 Clause->getClauseKind() == OMPC_schedule) {
1738 // Mark all variables in private list clauses as used in inner region.
1739 // Required for proper codegen of combined directives.
1740 // TODO: add processing for other clauses.
1741 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001742 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1743 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001744 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001745 if (Clause->getClauseKind() == OMPC_schedule)
1746 SC = cast<OMPScheduleClause>(Clause);
1747 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001748 OC = cast<OMPOrderedClause>(Clause);
1749 else if (Clause->getClauseKind() == OMPC_linear)
1750 LCs.push_back(cast<OMPLinearClause>(Clause));
1751 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001752 bool ErrorFound = false;
1753 // OpenMP, 2.7.1 Loop Construct, Restrictions
1754 // The nonmonotonic modifier cannot be specified if an ordered clause is
1755 // specified.
1756 if (SC &&
1757 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1758 SC->getSecondScheduleModifier() ==
1759 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1760 OC) {
1761 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1762 ? SC->getFirstScheduleModifierLoc()
1763 : SC->getSecondScheduleModifierLoc(),
1764 diag::err_omp_schedule_nonmonotonic_ordered)
1765 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1766 ErrorFound = true;
1767 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001768 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1769 for (auto *C : LCs) {
1770 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1771 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1772 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001773 ErrorFound = true;
1774 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001775 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1776 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1777 OC->getNumForLoops()) {
1778 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1779 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1780 ErrorFound = true;
1781 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001782 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001783 ActOnCapturedRegionError();
1784 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001785 }
1786 return ActOnCapturedRegionEnd(S.get());
1787}
1788
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001789static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1790 OpenMPDirectiveKind CurrentRegion,
1791 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001792 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001793 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001794 // Allowed nesting of constructs
1795 // +------------------+-----------------+------------------------------------+
1796 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1797 // +------------------+-----------------+------------------------------------+
1798 // | parallel | parallel | * |
1799 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001800 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001801 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001802 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001803 // | parallel | simd | * |
1804 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001805 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001806 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001807 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001808 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001809 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001810 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001811 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001812 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001813 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001814 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001815 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001816 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001817 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001818 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001819 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001820 // | parallel | target parallel | * |
1821 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001822 // | parallel | target enter | * |
1823 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001824 // | parallel | target exit | * |
1825 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001826 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001827 // | parallel | cancellation | |
1828 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001829 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001830 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001831 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001832 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001833 // +------------------+-----------------+------------------------------------+
1834 // | for | parallel | * |
1835 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001836 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001837 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001838 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001839 // | for | simd | * |
1840 // | for | sections | + |
1841 // | for | section | + |
1842 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001843 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001844 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001845 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001846 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001847 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001848 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001849 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001850 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001851 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001852 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001853 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001854 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001855 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001856 // | for | target parallel | * |
1857 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001858 // | for | target enter | * |
1859 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001860 // | for | target exit | * |
1861 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001862 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001863 // | for | cancellation | |
1864 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001865 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001866 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001867 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001868 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001869 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001870 // | master | parallel | * |
1871 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001872 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001873 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001874 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001875 // | master | simd | * |
1876 // | master | sections | + |
1877 // | master | section | + |
1878 // | master | single | + |
1879 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001880 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001881 // | master |parallel sections| * |
1882 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001883 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001884 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001885 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001886 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001887 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001888 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001889 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001890 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001891 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001892 // | master | target parallel | * |
1893 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001894 // | master | target enter | * |
1895 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001896 // | master | target exit | * |
1897 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001898 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001899 // | master | cancellation | |
1900 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001901 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001902 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001903 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001904 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001905 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001906 // | critical | parallel | * |
1907 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001908 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001909 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001910 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001911 // | critical | simd | * |
1912 // | critical | sections | + |
1913 // | critical | section | + |
1914 // | critical | single | + |
1915 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001916 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001917 // | critical |parallel sections| * |
1918 // | critical | task | * |
1919 // | critical | taskyield | * |
1920 // | critical | barrier | + |
1921 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001922 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001923 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001924 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001925 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001926 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001927 // | critical | target parallel | * |
1928 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001929 // | critical | target enter | * |
1930 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001931 // | critical | target exit | * |
1932 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001933 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001934 // | critical | cancellation | |
1935 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001936 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001937 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001938 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001939 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001940 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001941 // | simd | parallel | |
1942 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001943 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001944 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001945 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001946 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001947 // | simd | sections | |
1948 // | simd | section | |
1949 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001950 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001951 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001952 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001953 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001954 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001955 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001956 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001957 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001958 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001959 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001960 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001961 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001962 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001963 // | simd | target parallel | |
1964 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001965 // | simd | target enter | |
1966 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001967 // | simd | target exit | |
1968 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001969 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001970 // | simd | cancellation | |
1971 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001972 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001973 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001974 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001975 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001976 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001977 // | for simd | parallel | |
1978 // | for simd | for | |
1979 // | for simd | for simd | |
1980 // | for simd | master | |
1981 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001982 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001983 // | for simd | sections | |
1984 // | for simd | section | |
1985 // | for simd | single | |
1986 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001987 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001988 // | for simd |parallel sections| |
1989 // | for simd | task | |
1990 // | for simd | taskyield | |
1991 // | for simd | barrier | |
1992 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001993 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001994 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001995 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001996 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001997 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001998 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001999 // | for simd | target parallel | |
2000 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002001 // | for simd | target enter | |
2002 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002003 // | for simd | target exit | |
2004 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002005 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002006 // | for simd | cancellation | |
2007 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002008 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002009 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002010 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002011 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002012 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002013 // | parallel for simd| parallel | |
2014 // | parallel for simd| for | |
2015 // | parallel for simd| for simd | |
2016 // | parallel for simd| master | |
2017 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002018 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002019 // | parallel for simd| sections | |
2020 // | parallel for simd| section | |
2021 // | parallel for simd| single | |
2022 // | parallel for simd| parallel for | |
2023 // | parallel for simd|parallel for simd| |
2024 // | parallel for simd|parallel sections| |
2025 // | parallel for simd| task | |
2026 // | parallel for simd| taskyield | |
2027 // | parallel for simd| barrier | |
2028 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002029 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002030 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002031 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002032 // | parallel for simd| atomic | |
2033 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002034 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002035 // | parallel for simd| target parallel | |
2036 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002037 // | parallel for simd| target enter | |
2038 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002039 // | parallel for simd| target exit | |
2040 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002041 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002042 // | parallel for simd| cancellation | |
2043 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002044 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002045 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002046 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002047 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002048 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002049 // | sections | parallel | * |
2050 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002051 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002052 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002053 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002054 // | sections | simd | * |
2055 // | sections | sections | + |
2056 // | sections | section | * |
2057 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002058 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002059 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002060 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002061 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002062 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002063 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002064 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002065 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002066 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002067 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002068 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002069 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002070 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002071 // | sections | target parallel | * |
2072 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002073 // | sections | target enter | * |
2074 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002075 // | sections | target exit | * |
2076 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002077 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002078 // | sections | cancellation | |
2079 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002080 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002081 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002082 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002083 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002084 // +------------------+-----------------+------------------------------------+
2085 // | section | parallel | * |
2086 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002087 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002088 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002089 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002090 // | section | simd | * |
2091 // | section | sections | + |
2092 // | section | section | + |
2093 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002094 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002095 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002096 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002097 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002098 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002099 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002100 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002101 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002102 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002103 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002104 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002105 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002106 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002107 // | section | target parallel | * |
2108 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002109 // | section | target enter | * |
2110 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002111 // | section | target exit | * |
2112 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002113 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002114 // | section | cancellation | |
2115 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002116 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002117 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002118 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002119 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002120 // +------------------+-----------------+------------------------------------+
2121 // | single | parallel | * |
2122 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002123 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002124 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002125 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002126 // | single | simd | * |
2127 // | single | sections | + |
2128 // | single | section | + |
2129 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002130 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002131 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002132 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002133 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002134 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002135 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002136 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002137 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002138 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002139 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002140 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002141 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002142 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002143 // | single | target parallel | * |
2144 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002145 // | single | target enter | * |
2146 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002147 // | single | target exit | * |
2148 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002149 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002150 // | single | cancellation | |
2151 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002152 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002153 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002154 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002155 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002156 // +------------------+-----------------+------------------------------------+
2157 // | parallel for | parallel | * |
2158 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002159 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002160 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002161 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002162 // | parallel for | simd | * |
2163 // | parallel for | sections | + |
2164 // | parallel for | section | + |
2165 // | parallel for | single | + |
2166 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002167 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002168 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002169 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002170 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002171 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002172 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002173 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002174 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002175 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002176 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002177 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002178 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002179 // | parallel for | target parallel | * |
2180 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002181 // | parallel for | target enter | * |
2182 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002183 // | parallel for | target exit | * |
2184 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002185 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002186 // | parallel for | cancellation | |
2187 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002188 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002189 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002190 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002191 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002192 // +------------------+-----------------+------------------------------------+
2193 // | parallel sections| parallel | * |
2194 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002195 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002196 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002197 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002198 // | parallel sections| simd | * |
2199 // | parallel sections| sections | + |
2200 // | parallel sections| section | * |
2201 // | parallel sections| single | + |
2202 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002203 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002204 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002205 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002206 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002207 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002208 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002209 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002210 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002211 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002212 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002213 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002214 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002215 // | parallel sections| target parallel | * |
2216 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002217 // | parallel sections| target enter | * |
2218 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002219 // | parallel sections| target exit | * |
2220 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002221 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002222 // | parallel sections| cancellation | |
2223 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002224 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002225 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002226 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002227 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002228 // +------------------+-----------------+------------------------------------+
2229 // | task | parallel | * |
2230 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002231 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002232 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002233 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002234 // | task | simd | * |
2235 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002236 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002237 // | task | single | + |
2238 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002239 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002240 // | task |parallel sections| * |
2241 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002242 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002243 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002244 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002245 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002246 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002247 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002248 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002249 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002250 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002251 // | task | target parallel | * |
2252 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002253 // | task | target enter | * |
2254 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002255 // | task | target exit | * |
2256 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002257 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002258 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002259 // | | point | ! |
2260 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002261 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002262 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002263 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002264 // +------------------+-----------------+------------------------------------+
2265 // | ordered | parallel | * |
2266 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002267 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002268 // | ordered | master | * |
2269 // | ordered | critical | * |
2270 // | ordered | simd | * |
2271 // | ordered | sections | + |
2272 // | ordered | section | + |
2273 // | ordered | single | + |
2274 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002275 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002276 // | ordered |parallel sections| * |
2277 // | ordered | task | * |
2278 // | ordered | taskyield | * |
2279 // | ordered | barrier | + |
2280 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002281 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002282 // | ordered | flush | * |
2283 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002284 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002285 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002286 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002287 // | ordered | target parallel | * |
2288 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002289 // | ordered | target enter | * |
2290 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002291 // | ordered | target exit | * |
2292 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002293 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002294 // | ordered | cancellation | |
2295 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002296 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002297 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002298 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002299 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002300 // +------------------+-----------------+------------------------------------+
2301 // | atomic | parallel | |
2302 // | atomic | for | |
2303 // | atomic | for simd | |
2304 // | atomic | master | |
2305 // | atomic | critical | |
2306 // | atomic | simd | |
2307 // | atomic | sections | |
2308 // | atomic | section | |
2309 // | atomic | single | |
2310 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002311 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002312 // | atomic |parallel sections| |
2313 // | atomic | task | |
2314 // | atomic | taskyield | |
2315 // | atomic | barrier | |
2316 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002317 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002318 // | atomic | flush | |
2319 // | atomic | ordered | |
2320 // | atomic | atomic | |
2321 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002322 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002323 // | atomic | target parallel | |
2324 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002325 // | atomic | target enter | |
2326 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002327 // | atomic | target exit | |
2328 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002329 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002330 // | atomic | cancellation | |
2331 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002332 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002333 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002334 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002335 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002336 // +------------------+-----------------+------------------------------------+
2337 // | target | parallel | * |
2338 // | target | for | * |
2339 // | target | for simd | * |
2340 // | target | master | * |
2341 // | target | critical | * |
2342 // | target | simd | * |
2343 // | target | sections | * |
2344 // | target | section | * |
2345 // | target | single | * |
2346 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002347 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002348 // | target |parallel sections| * |
2349 // | target | task | * |
2350 // | target | taskyield | * |
2351 // | target | barrier | * |
2352 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002353 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002354 // | target | flush | * |
2355 // | target | ordered | * |
2356 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002357 // | target | target | |
2358 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002359 // | target | target parallel | |
2360 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002361 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002362 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002363 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002364 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002365 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002366 // | target | cancellation | |
2367 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002368 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002369 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002370 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002371 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002372 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002373 // | target parallel | parallel | * |
2374 // | target parallel | for | * |
2375 // | target parallel | for simd | * |
2376 // | target parallel | master | * |
2377 // | target parallel | critical | * |
2378 // | target parallel | simd | * |
2379 // | target parallel | sections | * |
2380 // | target parallel | section | * |
2381 // | target parallel | single | * |
2382 // | target parallel | parallel for | * |
2383 // | target parallel |parallel for simd| * |
2384 // | target parallel |parallel sections| * |
2385 // | target parallel | task | * |
2386 // | target parallel | taskyield | * |
2387 // | target parallel | barrier | * |
2388 // | target parallel | taskwait | * |
2389 // | target parallel | taskgroup | * |
2390 // | target parallel | flush | * |
2391 // | target parallel | ordered | * |
2392 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002393 // | target parallel | target | |
2394 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002395 // | target parallel | target parallel | |
2396 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002397 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002398 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002399 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002400 // | | data | |
2401 // | target parallel | teams | |
2402 // | target parallel | cancellation | |
2403 // | | point | ! |
2404 // | target parallel | cancel | ! |
2405 // | target parallel | taskloop | * |
2406 // | target parallel | taskloop simd | * |
2407 // | target parallel | distribute | |
2408 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002409 // | target parallel | parallel | * |
2410 // | for | | |
2411 // | target parallel | for | * |
2412 // | for | | |
2413 // | target parallel | for simd | * |
2414 // | for | | |
2415 // | target parallel | master | * |
2416 // | for | | |
2417 // | target parallel | critical | * |
2418 // | for | | |
2419 // | target parallel | simd | * |
2420 // | for | | |
2421 // | target parallel | sections | * |
2422 // | for | | |
2423 // | target parallel | section | * |
2424 // | for | | |
2425 // | target parallel | single | * |
2426 // | for | | |
2427 // | target parallel | parallel for | * |
2428 // | for | | |
2429 // | target parallel |parallel for simd| * |
2430 // | for | | |
2431 // | target parallel |parallel sections| * |
2432 // | for | | |
2433 // | target parallel | task | * |
2434 // | for | | |
2435 // | target parallel | taskyield | * |
2436 // | for | | |
2437 // | target parallel | barrier | * |
2438 // | for | | |
2439 // | target parallel | taskwait | * |
2440 // | for | | |
2441 // | target parallel | taskgroup | * |
2442 // | for | | |
2443 // | target parallel | flush | * |
2444 // | for | | |
2445 // | target parallel | ordered | * |
2446 // | for | | |
2447 // | target parallel | atomic | * |
2448 // | for | | |
2449 // | target parallel | target | |
2450 // | for | | |
2451 // | target parallel | target parallel | |
2452 // | for | | |
2453 // | target parallel | target parallel | |
2454 // | for | for | |
2455 // | target parallel | target enter | |
2456 // | for | data | |
2457 // | target parallel | target exit | |
2458 // | for | data | |
2459 // | target parallel | teams | |
2460 // | for | | |
2461 // | target parallel | cancellation | |
2462 // | for | point | ! |
2463 // | target parallel | cancel | ! |
2464 // | for | | |
2465 // | target parallel | taskloop | * |
2466 // | for | | |
2467 // | target parallel | taskloop simd | * |
2468 // | for | | |
2469 // | target parallel | distribute | |
2470 // | for | | |
2471 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002472 // | teams | parallel | * |
2473 // | teams | for | + |
2474 // | teams | for simd | + |
2475 // | teams | master | + |
2476 // | teams | critical | + |
2477 // | teams | simd | + |
2478 // | teams | sections | + |
2479 // | teams | section | + |
2480 // | teams | single | + |
2481 // | teams | parallel for | * |
2482 // | teams |parallel for simd| * |
2483 // | teams |parallel sections| * |
2484 // | teams | task | + |
2485 // | teams | taskyield | + |
2486 // | teams | barrier | + |
2487 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002488 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002489 // | teams | flush | + |
2490 // | teams | ordered | + |
2491 // | teams | atomic | + |
2492 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002493 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002494 // | teams | target parallel | + |
2495 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002496 // | teams | target enter | + |
2497 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002498 // | teams | target exit | + |
2499 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002500 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002501 // | teams | cancellation | |
2502 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002503 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002504 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002505 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002506 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002507 // +------------------+-----------------+------------------------------------+
2508 // | taskloop | parallel | * |
2509 // | taskloop | for | + |
2510 // | taskloop | for simd | + |
2511 // | taskloop | master | + |
2512 // | taskloop | critical | * |
2513 // | taskloop | simd | * |
2514 // | taskloop | sections | + |
2515 // | taskloop | section | + |
2516 // | taskloop | single | + |
2517 // | taskloop | parallel for | * |
2518 // | taskloop |parallel for simd| * |
2519 // | taskloop |parallel sections| * |
2520 // | taskloop | task | * |
2521 // | taskloop | taskyield | * |
2522 // | taskloop | barrier | + |
2523 // | taskloop | taskwait | * |
2524 // | taskloop | taskgroup | * |
2525 // | taskloop | flush | * |
2526 // | taskloop | ordered | + |
2527 // | taskloop | atomic | * |
2528 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002529 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002530 // | taskloop | target parallel | * |
2531 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002532 // | taskloop | target enter | * |
2533 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002534 // | taskloop | target exit | * |
2535 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002536 // | taskloop | teams | + |
2537 // | taskloop | cancellation | |
2538 // | | point | |
2539 // | taskloop | cancel | |
2540 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002541 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002542 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002543 // | taskloop simd | parallel | |
2544 // | taskloop simd | for | |
2545 // | taskloop simd | for simd | |
2546 // | taskloop simd | master | |
2547 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002548 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002549 // | taskloop simd | sections | |
2550 // | taskloop simd | section | |
2551 // | taskloop simd | single | |
2552 // | taskloop simd | parallel for | |
2553 // | taskloop simd |parallel for simd| |
2554 // | taskloop simd |parallel sections| |
2555 // | taskloop simd | task | |
2556 // | taskloop simd | taskyield | |
2557 // | taskloop simd | barrier | |
2558 // | taskloop simd | taskwait | |
2559 // | taskloop simd | taskgroup | |
2560 // | taskloop simd | flush | |
2561 // | taskloop simd | ordered | + (with simd clause) |
2562 // | taskloop simd | atomic | |
2563 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002564 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002565 // | taskloop simd | target parallel | |
2566 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002567 // | taskloop simd | target enter | |
2568 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002569 // | taskloop simd | target exit | |
2570 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002571 // | taskloop simd | teams | |
2572 // | taskloop simd | cancellation | |
2573 // | | point | |
2574 // | taskloop simd | cancel | |
2575 // | taskloop simd | taskloop | |
2576 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002577 // | taskloop simd | distribute | |
2578 // +------------------+-----------------+------------------------------------+
2579 // | distribute | parallel | * |
2580 // | distribute | for | * |
2581 // | distribute | for simd | * |
2582 // | distribute | master | * |
2583 // | distribute | critical | * |
2584 // | distribute | simd | * |
2585 // | distribute | sections | * |
2586 // | distribute | section | * |
2587 // | distribute | single | * |
2588 // | distribute | parallel for | * |
2589 // | distribute |parallel for simd| * |
2590 // | distribute |parallel sections| * |
2591 // | distribute | task | * |
2592 // | distribute | taskyield | * |
2593 // | distribute | barrier | * |
2594 // | distribute | taskwait | * |
2595 // | distribute | taskgroup | * |
2596 // | distribute | flush | * |
2597 // | distribute | ordered | + |
2598 // | distribute | atomic | * |
2599 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002600 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002601 // | distribute | target parallel | |
2602 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002603 // | distribute | target enter | |
2604 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002605 // | distribute | target exit | |
2606 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002607 // | distribute | teams | |
2608 // | distribute | cancellation | + |
2609 // | | point | |
2610 // | distribute | cancel | + |
2611 // | distribute | taskloop | * |
2612 // | distribute | taskloop simd | * |
2613 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002614 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002615 if (Stack->getCurScope()) {
2616 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002617 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002618 bool NestingProhibited = false;
2619 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002620 enum {
2621 NoRecommend,
2622 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002623 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002624 ShouldBeInTargetRegion,
2625 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002626 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002627 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2628 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002629 // OpenMP [2.16, Nesting of Regions]
2630 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002631 // OpenMP [2.8.1,simd Construct, Restrictions]
2632 // An ordered construct with the simd clause is the only OpenMP construct
2633 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002634 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2635 return true;
2636 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002637 if (ParentRegion == OMPD_atomic) {
2638 // OpenMP [2.16, Nesting of Regions]
2639 // OpenMP constructs may not be nested inside an atomic region.
2640 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2641 return true;
2642 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002643 if (CurrentRegion == OMPD_section) {
2644 // OpenMP [2.7.2, sections Construct, Restrictions]
2645 // Orphaned section directives are prohibited. That is, the section
2646 // directives must appear within the sections construct and must not be
2647 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002648 if (ParentRegion != OMPD_sections &&
2649 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002650 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2651 << (ParentRegion != OMPD_unknown)
2652 << getOpenMPDirectiveName(ParentRegion);
2653 return true;
2654 }
2655 return false;
2656 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002657 // Allow some constructs to be orphaned (they could be used in functions,
2658 // called from OpenMP regions with the required preconditions).
2659 if (ParentRegion == OMPD_unknown)
2660 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002661 if (CurrentRegion == OMPD_cancellation_point ||
2662 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002663 // OpenMP [2.16, Nesting of Regions]
2664 // A cancellation point construct for which construct-type-clause is
2665 // taskgroup must be nested inside a task construct. A cancellation
2666 // point construct for which construct-type-clause is not taskgroup must
2667 // be closely nested inside an OpenMP construct that matches the type
2668 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002669 // A cancel construct for which construct-type-clause is taskgroup must be
2670 // nested inside a task construct. A cancel construct for which
2671 // construct-type-clause is not taskgroup must be closely nested inside an
2672 // OpenMP construct that matches the type specified in
2673 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002674 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002675 !((CancelRegion == OMPD_parallel &&
2676 (ParentRegion == OMPD_parallel ||
2677 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002678 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002679 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2680 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002681 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2682 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002683 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2684 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002685 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002686 // OpenMP [2.16, Nesting of Regions]
2687 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002688 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002689 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002690 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002691 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002692 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2693 // OpenMP [2.16, Nesting of Regions]
2694 // A critical region may not be nested (closely or otherwise) inside a
2695 // critical region with the same name. Note that this restriction is not
2696 // sufficient to prevent deadlock.
2697 SourceLocation PreviousCriticalLoc;
2698 bool DeadLock =
2699 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2700 OpenMPDirectiveKind K,
2701 const DeclarationNameInfo &DNI,
2702 SourceLocation Loc)
2703 ->bool {
2704 if (K == OMPD_critical &&
2705 DNI.getName() == CurrentName.getName()) {
2706 PreviousCriticalLoc = Loc;
2707 return true;
2708 } else
2709 return false;
2710 },
2711 false /* skip top directive */);
2712 if (DeadLock) {
2713 SemaRef.Diag(StartLoc,
2714 diag::err_omp_prohibited_region_critical_same_name)
2715 << CurrentName.getName();
2716 if (PreviousCriticalLoc.isValid())
2717 SemaRef.Diag(PreviousCriticalLoc,
2718 diag::note_omp_previous_critical_region);
2719 return true;
2720 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002721 } else if (CurrentRegion == OMPD_barrier) {
2722 // OpenMP [2.16, Nesting of Regions]
2723 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002724 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002725 NestingProhibited =
2726 isOpenMPWorksharingDirective(ParentRegion) ||
2727 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002728 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002729 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002730 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002731 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002732 // OpenMP [2.16, Nesting of Regions]
2733 // A worksharing region may not be closely nested inside a worksharing,
2734 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002735 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002736 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002737 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002738 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002739 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002740 Recommend = ShouldBeInParallelRegion;
2741 } else if (CurrentRegion == OMPD_ordered) {
2742 // OpenMP [2.16, Nesting of Regions]
2743 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002744 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002745 // An ordered region must be closely nested inside a loop region (or
2746 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002747 // OpenMP [2.8.1,simd Construct, Restrictions]
2748 // An ordered construct with the simd clause is the only OpenMP construct
2749 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002750 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002751 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002752 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002753 !(isOpenMPSimdDirective(ParentRegion) ||
2754 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002755 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002756 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2757 // OpenMP [2.16, Nesting of Regions]
2758 // If specified, a teams construct must be contained within a target
2759 // construct.
2760 NestingProhibited = ParentRegion != OMPD_target;
2761 Recommend = ShouldBeInTargetRegion;
2762 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2763 }
2764 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2765 // OpenMP [2.16, Nesting of Regions]
2766 // distribute, parallel, parallel sections, parallel workshare, and the
2767 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2768 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002769 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2770 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002771 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002772 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002773 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2774 // OpenMP 4.5 [2.17 Nesting of Regions]
2775 // The region associated with the distribute construct must be strictly
2776 // nested inside a teams region
2777 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2778 Recommend = ShouldBeInTeamsRegion;
2779 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002780 if (!NestingProhibited &&
2781 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2782 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2783 // OpenMP 4.5 [2.17 Nesting of Regions]
2784 // If a target, target update, target data, target enter data, or
2785 // target exit data construct is encountered during execution of a
2786 // target region, the behavior is unspecified.
2787 NestingProhibited = Stack->hasDirective(
2788 [&OffendingRegion](OpenMPDirectiveKind K,
2789 const DeclarationNameInfo &DNI,
2790 SourceLocation Loc) -> bool {
2791 if (isOpenMPTargetExecutionDirective(K)) {
2792 OffendingRegion = K;
2793 return true;
2794 } else
2795 return false;
2796 },
2797 false /* don't skip top directive */);
2798 CloseNesting = false;
2799 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002800 if (NestingProhibited) {
2801 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002802 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2803 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002804 return true;
2805 }
2806 }
2807 return false;
2808}
2809
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002810static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2811 ArrayRef<OMPClause *> Clauses,
2812 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2813 bool ErrorFound = false;
2814 unsigned NamedModifiersNumber = 0;
2815 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2816 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002817 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002818 for (const auto *C : Clauses) {
2819 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2820 // At most one if clause without a directive-name-modifier can appear on
2821 // the directive.
2822 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2823 if (FoundNameModifiers[CurNM]) {
2824 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2825 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2826 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2827 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002828 } else if (CurNM != OMPD_unknown) {
2829 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002830 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002831 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002832 FoundNameModifiers[CurNM] = IC;
2833 if (CurNM == OMPD_unknown)
2834 continue;
2835 // Check if the specified name modifier is allowed for the current
2836 // directive.
2837 // At most one if clause with the particular directive-name-modifier can
2838 // appear on the directive.
2839 bool MatchFound = false;
2840 for (auto NM : AllowedNameModifiers) {
2841 if (CurNM == NM) {
2842 MatchFound = true;
2843 break;
2844 }
2845 }
2846 if (!MatchFound) {
2847 S.Diag(IC->getNameModifierLoc(),
2848 diag::err_omp_wrong_if_directive_name_modifier)
2849 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2850 ErrorFound = true;
2851 }
2852 }
2853 }
2854 // If any if clause on the directive includes a directive-name-modifier then
2855 // all if clauses on the directive must include a directive-name-modifier.
2856 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2857 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2858 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2859 diag::err_omp_no_more_if_clause);
2860 } else {
2861 std::string Values;
2862 std::string Sep(", ");
2863 unsigned AllowedCnt = 0;
2864 unsigned TotalAllowedNum =
2865 AllowedNameModifiers.size() - NamedModifiersNumber;
2866 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2867 ++Cnt) {
2868 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2869 if (!FoundNameModifiers[NM]) {
2870 Values += "'";
2871 Values += getOpenMPDirectiveName(NM);
2872 Values += "'";
2873 if (AllowedCnt + 2 == TotalAllowedNum)
2874 Values += " or ";
2875 else if (AllowedCnt + 1 != TotalAllowedNum)
2876 Values += Sep;
2877 ++AllowedCnt;
2878 }
2879 }
2880 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2881 diag::err_omp_unnamed_if_clause)
2882 << (TotalAllowedNum > 1) << Values;
2883 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002884 for (auto Loc : NameModifierLoc) {
2885 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2886 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002887 ErrorFound = true;
2888 }
2889 return ErrorFound;
2890}
2891
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002892StmtResult Sema::ActOnOpenMPExecutableDirective(
2893 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2894 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2895 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002896 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002897 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2898 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002899 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002900
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002901 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002902 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002903 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002904 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002905 if (AStmt) {
2906 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2907
2908 // Check default data sharing attributes for referenced variables.
2909 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2910 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2911 if (DSAChecker.isErrorFound())
2912 return StmtError();
2913 // Generate list of implicitly defined firstprivate variables.
2914 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002915
2916 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2917 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2918 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2919 SourceLocation(), SourceLocation())) {
2920 ClausesWithImplicit.push_back(Implicit);
2921 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2922 DSAChecker.getImplicitFirstprivate().size();
2923 } else
2924 ErrorFound = true;
2925 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002926 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002927
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002928 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002929 switch (Kind) {
2930 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002931 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2932 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002933 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002934 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002935 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002936 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2937 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002938 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002939 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002940 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2941 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002942 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002943 case OMPD_for_simd:
2944 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2945 EndLoc, VarsWithInheritedDSA);
2946 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002947 case OMPD_sections:
2948 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2949 EndLoc);
2950 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002951 case OMPD_section:
2952 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002953 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002954 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2955 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002956 case OMPD_single:
2957 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2958 EndLoc);
2959 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002960 case OMPD_master:
2961 assert(ClausesWithImplicit.empty() &&
2962 "No clauses are allowed for 'omp master' directive");
2963 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2964 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002965 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002966 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2967 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002968 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002969 case OMPD_parallel_for:
2970 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2971 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002972 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002973 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002974 case OMPD_parallel_for_simd:
2975 Res = ActOnOpenMPParallelForSimdDirective(
2976 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002977 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002978 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002979 case OMPD_parallel_sections:
2980 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2981 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002982 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002983 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002984 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002985 Res =
2986 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002987 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002988 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002989 case OMPD_taskyield:
2990 assert(ClausesWithImplicit.empty() &&
2991 "No clauses are allowed for 'omp taskyield' directive");
2992 assert(AStmt == nullptr &&
2993 "No associated statement allowed for 'omp taskyield' directive");
2994 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2995 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002996 case OMPD_barrier:
2997 assert(ClausesWithImplicit.empty() &&
2998 "No clauses are allowed for 'omp barrier' directive");
2999 assert(AStmt == nullptr &&
3000 "No associated statement allowed for 'omp barrier' directive");
3001 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3002 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003003 case OMPD_taskwait:
3004 assert(ClausesWithImplicit.empty() &&
3005 "No clauses are allowed for 'omp taskwait' directive");
3006 assert(AStmt == nullptr &&
3007 "No associated statement allowed for 'omp taskwait' directive");
3008 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3009 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003010 case OMPD_taskgroup:
3011 assert(ClausesWithImplicit.empty() &&
3012 "No clauses are allowed for 'omp taskgroup' directive");
3013 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3014 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003015 case OMPD_flush:
3016 assert(AStmt == nullptr &&
3017 "No associated statement allowed for 'omp flush' directive");
3018 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3019 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003020 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003021 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3022 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003023 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003024 case OMPD_atomic:
3025 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3026 EndLoc);
3027 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003028 case OMPD_teams:
3029 Res =
3030 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3031 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003032 case OMPD_target:
3033 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3034 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003035 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003036 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003037 case OMPD_target_parallel:
3038 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3039 StartLoc, EndLoc);
3040 AllowedNameModifiers.push_back(OMPD_target);
3041 AllowedNameModifiers.push_back(OMPD_parallel);
3042 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003043 case OMPD_target_parallel_for:
3044 Res = ActOnOpenMPTargetParallelForDirective(
3045 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3046 AllowedNameModifiers.push_back(OMPD_target);
3047 AllowedNameModifiers.push_back(OMPD_parallel);
3048 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003049 case OMPD_cancellation_point:
3050 assert(ClausesWithImplicit.empty() &&
3051 "No clauses are allowed for 'omp cancellation point' directive");
3052 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3053 "cancellation point' directive");
3054 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3055 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003056 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003057 assert(AStmt == nullptr &&
3058 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003059 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3060 CancelRegion);
3061 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003062 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003063 case OMPD_target_data:
3064 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3065 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003066 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003067 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003068 case OMPD_target_enter_data:
3069 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3070 EndLoc);
3071 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3072 break;
Samuel Antao72590762016-01-19 20:04:50 +00003073 case OMPD_target_exit_data:
3074 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3075 EndLoc);
3076 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3077 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003078 case OMPD_taskloop:
3079 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3080 EndLoc, VarsWithInheritedDSA);
3081 AllowedNameModifiers.push_back(OMPD_taskloop);
3082 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003083 case OMPD_taskloop_simd:
3084 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3085 EndLoc, VarsWithInheritedDSA);
3086 AllowedNameModifiers.push_back(OMPD_taskloop);
3087 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003088 case OMPD_distribute:
3089 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3090 EndLoc, VarsWithInheritedDSA);
3091 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003092 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003093 llvm_unreachable("OpenMP Directive is not allowed");
3094 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003095 llvm_unreachable("Unknown OpenMP directive");
3096 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003097
Alexey Bataev4acb8592014-07-07 13:01:15 +00003098 for (auto P : VarsWithInheritedDSA) {
3099 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3100 << P.first << P.second->getSourceRange();
3101 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003102 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3103
3104 if (!AllowedNameModifiers.empty())
3105 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3106 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003107
Alexey Bataeved09d242014-05-28 05:53:51 +00003108 if (ErrorFound)
3109 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003110 return Res;
3111}
3112
3113StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3114 Stmt *AStmt,
3115 SourceLocation StartLoc,
3116 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003117 if (!AStmt)
3118 return StmtError();
3119
Alexey Bataev9959db52014-05-06 10:08:46 +00003120 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3121 // 1.2.2 OpenMP Language Terminology
3122 // Structured block - An executable statement with a single entry at the
3123 // top and a single exit at the bottom.
3124 // The point of exit cannot be a branch out of the structured block.
3125 // longjmp() and throw() must not violate the entry/exit criteria.
3126 CS->getCapturedDecl()->setNothrow();
3127
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003128 getCurFunction()->setHasBranchProtectedScope();
3129
Alexey Bataev25e5b442015-09-15 12:52:43 +00003130 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3131 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003132}
3133
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003134namespace {
3135/// \brief Helper class for checking canonical form of the OpenMP loops and
3136/// extracting iteration space of each loop in the loop nest, that will be used
3137/// for IR generation.
3138class OpenMPIterationSpaceChecker {
3139 /// \brief Reference to Sema.
3140 Sema &SemaRef;
3141 /// \brief A location for diagnostics (when there is no some better location).
3142 SourceLocation DefaultLoc;
3143 /// \brief A location for diagnostics (when increment is not compatible).
3144 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003145 /// \brief A source location for referring to loop init later.
3146 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003147 /// \brief A source location for referring to condition later.
3148 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003149 /// \brief A source location for referring to increment later.
3150 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003151 /// \brief Loop variable.
3152 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003153 /// \brief Reference to loop variable.
3154 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003155 /// \brief Lower bound (initializer for the var).
3156 Expr *LB;
3157 /// \brief Upper bound.
3158 Expr *UB;
3159 /// \brief Loop step (increment).
3160 Expr *Step;
3161 /// \brief This flag is true when condition is one of:
3162 /// Var < UB
3163 /// Var <= UB
3164 /// UB > Var
3165 /// UB >= Var
3166 bool TestIsLessOp;
3167 /// \brief This flag is true when condition is strict ( < or > ).
3168 bool TestIsStrictOp;
3169 /// \brief This flag is true when step is subtracted on each iteration.
3170 bool SubtractStep;
3171
3172public:
3173 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3174 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003175 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3176 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003177 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3178 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003179 /// \brief Check init-expr for canonical loop form and save loop counter
3180 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003181 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003182 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3183 /// for less/greater and for strict/non-strict comparison.
3184 bool CheckCond(Expr *S);
3185 /// \brief Check incr-expr for canonical loop form and return true if it
3186 /// does not conform, otherwise save loop step (#Step).
3187 bool CheckInc(Expr *S);
3188 /// \brief Return the loop counter variable.
3189 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003190 /// \brief Return the reference expression to loop counter variable.
3191 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003192 /// \brief Source range of the loop init.
3193 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3194 /// \brief Source range of the loop condition.
3195 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3196 /// \brief Source range of the loop increment.
3197 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3198 /// \brief True if the step should be subtracted.
3199 bool ShouldSubtractStep() const { return SubtractStep; }
3200 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003201 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003202 /// \brief Build the precondition expression for the loops.
3203 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003204 /// \brief Build reference expression to the counter be used for codegen.
3205 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003206 /// \brief Build reference expression to the private counter be used for
3207 /// codegen.
3208 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003209 /// \brief Build initization of the counter be used for codegen.
3210 Expr *BuildCounterInit() const;
3211 /// \brief Build step of the counter be used for codegen.
3212 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003213 /// \brief Return true if any expression is dependent.
3214 bool Dependent() const;
3215
3216private:
3217 /// \brief Check the right-hand side of an assignment in the increment
3218 /// expression.
3219 bool CheckIncRHS(Expr *RHS);
3220 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003221 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003222 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003223 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003224 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003225 /// \brief Helper to set loop increment.
3226 bool SetStep(Expr *NewStep, bool Subtract);
3227};
3228
3229bool OpenMPIterationSpaceChecker::Dependent() const {
3230 if (!Var) {
3231 assert(!LB && !UB && !Step);
3232 return false;
3233 }
3234 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3235 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3236}
3237
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003238template <typename T>
3239static T *getExprAsWritten(T *E) {
3240 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3241 E = ExprTemp->getSubExpr();
3242
3243 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3244 E = MTE->GetTemporaryExpr();
3245
3246 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3247 E = Binder->getSubExpr();
3248
3249 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3250 E = ICE->getSubExprAsWritten();
3251 return E->IgnoreParens();
3252}
3253
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003254bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3255 DeclRefExpr *NewVarRefExpr,
3256 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003257 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003258 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3259 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003260 if (!NewVar || !NewLB)
3261 return true;
3262 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003263 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003264 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3265 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003266 if ((Ctor->isCopyOrMoveConstructor() ||
3267 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3268 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003269 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003270 LB = NewLB;
3271 return false;
3272}
3273
3274bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003275 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003276 // State consistency checking to ensure correct usage.
3277 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3278 !TestIsLessOp && !TestIsStrictOp);
3279 if (!NewUB)
3280 return true;
3281 UB = NewUB;
3282 TestIsLessOp = LessOp;
3283 TestIsStrictOp = StrictOp;
3284 ConditionSrcRange = SR;
3285 ConditionLoc = SL;
3286 return false;
3287}
3288
3289bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3290 // State consistency checking to ensure correct usage.
3291 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3292 if (!NewStep)
3293 return true;
3294 if (!NewStep->isValueDependent()) {
3295 // Check that the step is integer expression.
3296 SourceLocation StepLoc = NewStep->getLocStart();
3297 ExprResult Val =
3298 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3299 if (Val.isInvalid())
3300 return true;
3301 NewStep = Val.get();
3302
3303 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3304 // If test-expr is of form var relational-op b and relational-op is < or
3305 // <= then incr-expr must cause var to increase on each iteration of the
3306 // loop. If test-expr is of form var relational-op b and relational-op is
3307 // > or >= then incr-expr must cause var to decrease on each iteration of
3308 // the loop.
3309 // If test-expr is of form b relational-op var and relational-op is < or
3310 // <= then incr-expr must cause var to decrease on each iteration of the
3311 // loop. If test-expr is of form b relational-op var and relational-op is
3312 // > or >= then incr-expr must cause var to increase on each iteration of
3313 // the loop.
3314 llvm::APSInt Result;
3315 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3316 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3317 bool IsConstNeg =
3318 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003319 bool IsConstPos =
3320 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003321 bool IsConstZero = IsConstant && !Result.getBoolValue();
3322 if (UB && (IsConstZero ||
3323 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003324 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003325 SemaRef.Diag(NewStep->getExprLoc(),
3326 diag::err_omp_loop_incr_not_compatible)
3327 << Var << TestIsLessOp << NewStep->getSourceRange();
3328 SemaRef.Diag(ConditionLoc,
3329 diag::note_omp_loop_cond_requres_compatible_incr)
3330 << TestIsLessOp << ConditionSrcRange;
3331 return true;
3332 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003333 if (TestIsLessOp == Subtract) {
3334 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3335 NewStep).get();
3336 Subtract = !Subtract;
3337 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003338 }
3339
3340 Step = NewStep;
3341 SubtractStep = Subtract;
3342 return false;
3343}
3344
Alexey Bataev9c821032015-04-30 04:23:23 +00003345bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003346 // Check init-expr for canonical loop form and save loop counter
3347 // variable - #Var and its initialization value - #LB.
3348 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3349 // var = lb
3350 // integer-type var = lb
3351 // random-access-iterator-type var = lb
3352 // pointer-type var = lb
3353 //
3354 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003355 if (EmitDiags) {
3356 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3357 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003358 return true;
3359 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003360 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003361 if (Expr *E = dyn_cast<Expr>(S))
3362 S = E->IgnoreParens();
3363 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3364 if (BO->getOpcode() == BO_Assign)
3365 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003366 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003367 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003368 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3369 if (DS->isSingleDecl()) {
3370 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003371 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003372 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003373 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003374 SemaRef.Diag(S->getLocStart(),
3375 diag::ext_omp_loop_not_canonical_init)
3376 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003377 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003378 }
3379 }
3380 }
3381 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3382 if (CE->getOperator() == OO_Equal)
3383 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003384 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3385 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003386
Alexey Bataev9c821032015-04-30 04:23:23 +00003387 if (EmitDiags) {
3388 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3389 << S->getSourceRange();
3390 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003391 return true;
3392}
3393
Alexey Bataev23b69422014-06-18 07:08:49 +00003394/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003395/// variable (which may be the loop variable) if possible.
3396static const VarDecl *GetInitVarDecl(const Expr *E) {
3397 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003398 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003399 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003400 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3401 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003402 if ((Ctor->isCopyOrMoveConstructor() ||
3403 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3404 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003405 E = CE->getArg(0)->IgnoreParenImpCasts();
3406 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3407 if (!DRE)
3408 return nullptr;
3409 return dyn_cast<VarDecl>(DRE->getDecl());
3410}
3411
3412bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3413 // Check test-expr for canonical form, save upper-bound UB, flags for
3414 // less/greater and for strict/non-strict comparison.
3415 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3416 // var relational-op b
3417 // b relational-op var
3418 //
3419 if (!S) {
3420 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3421 return true;
3422 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003423 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003424 SourceLocation CondLoc = S->getLocStart();
3425 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3426 if (BO->isRelationalOp()) {
3427 if (GetInitVarDecl(BO->getLHS()) == Var)
3428 return SetUB(BO->getRHS(),
3429 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3430 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3431 BO->getSourceRange(), BO->getOperatorLoc());
3432 if (GetInitVarDecl(BO->getRHS()) == Var)
3433 return SetUB(BO->getLHS(),
3434 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3435 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3436 BO->getSourceRange(), BO->getOperatorLoc());
3437 }
3438 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3439 if (CE->getNumArgs() == 2) {
3440 auto Op = CE->getOperator();
3441 switch (Op) {
3442 case OO_Greater:
3443 case OO_GreaterEqual:
3444 case OO_Less:
3445 case OO_LessEqual:
3446 if (GetInitVarDecl(CE->getArg(0)) == Var)
3447 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3448 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3449 CE->getOperatorLoc());
3450 if (GetInitVarDecl(CE->getArg(1)) == Var)
3451 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3452 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3453 CE->getOperatorLoc());
3454 break;
3455 default:
3456 break;
3457 }
3458 }
3459 }
3460 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3461 << S->getSourceRange() << Var;
3462 return true;
3463}
3464
3465bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3466 // RHS of canonical loop form increment can be:
3467 // var + incr
3468 // incr + var
3469 // var - incr
3470 //
3471 RHS = RHS->IgnoreParenImpCasts();
3472 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3473 if (BO->isAdditiveOp()) {
3474 bool IsAdd = BO->getOpcode() == BO_Add;
3475 if (GetInitVarDecl(BO->getLHS()) == Var)
3476 return SetStep(BO->getRHS(), !IsAdd);
3477 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3478 return SetStep(BO->getLHS(), false);
3479 }
3480 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3481 bool IsAdd = CE->getOperator() == OO_Plus;
3482 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3483 if (GetInitVarDecl(CE->getArg(0)) == Var)
3484 return SetStep(CE->getArg(1), !IsAdd);
3485 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3486 return SetStep(CE->getArg(0), false);
3487 }
3488 }
3489 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3490 << RHS->getSourceRange() << Var;
3491 return true;
3492}
3493
3494bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3495 // Check incr-expr for canonical loop form and return true if it
3496 // does not conform.
3497 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3498 // ++var
3499 // var++
3500 // --var
3501 // var--
3502 // var += incr
3503 // var -= incr
3504 // var = var + incr
3505 // var = incr + var
3506 // var = var - incr
3507 //
3508 if (!S) {
3509 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3510 return true;
3511 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003512 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003513 S = S->IgnoreParens();
3514 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3515 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3516 return SetStep(
3517 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3518 (UO->isDecrementOp() ? -1 : 1)).get(),
3519 false);
3520 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3521 switch (BO->getOpcode()) {
3522 case BO_AddAssign:
3523 case BO_SubAssign:
3524 if (GetInitVarDecl(BO->getLHS()) == Var)
3525 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3526 break;
3527 case BO_Assign:
3528 if (GetInitVarDecl(BO->getLHS()) == Var)
3529 return CheckIncRHS(BO->getRHS());
3530 break;
3531 default:
3532 break;
3533 }
3534 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3535 switch (CE->getOperator()) {
3536 case OO_PlusPlus:
3537 case OO_MinusMinus:
3538 if (GetInitVarDecl(CE->getArg(0)) == Var)
3539 return SetStep(
3540 SemaRef.ActOnIntegerConstant(
3541 CE->getLocStart(),
3542 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3543 false);
3544 break;
3545 case OO_PlusEqual:
3546 case OO_MinusEqual:
3547 if (GetInitVarDecl(CE->getArg(0)) == Var)
3548 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3549 break;
3550 case OO_Equal:
3551 if (GetInitVarDecl(CE->getArg(0)) == Var)
3552 return CheckIncRHS(CE->getArg(1));
3553 break;
3554 default:
3555 break;
3556 }
3557 }
3558 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3559 << S->getSourceRange() << Var;
3560 return true;
3561}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003562
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003563namespace {
3564// Transform variables declared in GNU statement expressions to new ones to
3565// avoid crash on codegen.
3566class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3567 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3568
3569public:
3570 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3571
3572 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3573 if (auto *VD = cast<VarDecl>(D))
3574 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3575 !isa<ImplicitParamDecl>(D)) {
3576 auto *NewVD = VarDecl::Create(
3577 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3578 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3579 VD->getTypeSourceInfo(), VD->getStorageClass());
3580 NewVD->setTSCSpec(VD->getTSCSpec());
3581 NewVD->setInit(VD->getInit());
3582 NewVD->setInitStyle(VD->getInitStyle());
3583 NewVD->setExceptionVariable(VD->isExceptionVariable());
3584 NewVD->setNRVOVariable(VD->isNRVOVariable());
3585 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3586 NewVD->setConstexpr(VD->isConstexpr());
3587 NewVD->setInitCapture(VD->isInitCapture());
3588 NewVD->setPreviousDeclInSameBlockScope(
3589 VD->isPreviousDeclInSameBlockScope());
3590 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003591 if (VD->hasAttrs())
3592 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003593 transformedLocalDecl(VD, NewVD);
3594 return NewVD;
3595 }
3596 return BaseTransform::TransformDefinition(Loc, D);
3597 }
3598
3599 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3600 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3601 if (E->getDecl() != NewD) {
3602 NewD->setReferenced();
3603 NewD->markUsed(SemaRef.Context);
3604 return DeclRefExpr::Create(
3605 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3606 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3607 E->getNameInfo(), E->getType(), E->getValueKind());
3608 }
3609 return BaseTransform::TransformDeclRefExpr(E);
3610 }
3611};
3612}
3613
Alexander Musmana5f070a2014-10-01 06:03:56 +00003614/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003615Expr *
3616OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3617 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003618 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003619 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003620 auto VarType = Var->getType().getNonReferenceType();
3621 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003622 SemaRef.getLangOpts().CPlusPlus) {
3623 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003624 auto *UBExpr = TestIsLessOp ? UB : LB;
3625 auto *LBExpr = TestIsLessOp ? LB : UB;
3626 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3627 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3628 if (!Upper || !Lower)
3629 return nullptr;
3630 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3631 Sema::AA_Converting,
3632 /*AllowExplicit=*/true)
3633 .get();
3634 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3635 Sema::AA_Converting,
3636 /*AllowExplicit=*/true)
3637 .get();
3638 if (!Upper || !Lower)
3639 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003640
3641 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3642
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003643 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003644 // BuildBinOp already emitted error, this one is to point user to upper
3645 // and lower bound, and to tell what is passed to 'operator-'.
3646 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3647 << Upper->getSourceRange() << Lower->getSourceRange();
3648 return nullptr;
3649 }
3650 }
3651
3652 if (!Diff.isUsable())
3653 return nullptr;
3654
3655 // Upper - Lower [- 1]
3656 if (TestIsStrictOp)
3657 Diff = SemaRef.BuildBinOp(
3658 S, DefaultLoc, BO_Sub, Diff.get(),
3659 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3660 if (!Diff.isUsable())
3661 return nullptr;
3662
3663 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003664 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3665 if (NewStep.isInvalid())
3666 return nullptr;
3667 NewStep = SemaRef.PerformImplicitConversion(
3668 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3669 /*AllowExplicit=*/true);
3670 if (NewStep.isInvalid())
3671 return nullptr;
3672 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003673 if (!Diff.isUsable())
3674 return nullptr;
3675
3676 // Parentheses (for dumping/debugging purposes only).
3677 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3678 if (!Diff.isUsable())
3679 return nullptr;
3680
3681 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003682 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3683 if (NewStep.isInvalid())
3684 return nullptr;
3685 NewStep = SemaRef.PerformImplicitConversion(
3686 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3687 /*AllowExplicit=*/true);
3688 if (NewStep.isInvalid())
3689 return nullptr;
3690 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003691 if (!Diff.isUsable())
3692 return nullptr;
3693
Alexander Musman174b3ca2014-10-06 11:16:29 +00003694 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003695 QualType Type = Diff.get()->getType();
3696 auto &C = SemaRef.Context;
3697 bool UseVarType = VarType->hasIntegerRepresentation() &&
3698 C.getTypeSize(Type) > C.getTypeSize(VarType);
3699 if (!Type->isIntegerType() || UseVarType) {
3700 unsigned NewSize =
3701 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3702 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3703 : Type->hasSignedIntegerRepresentation();
3704 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3705 Diff = SemaRef.PerformImplicitConversion(
3706 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3707 if (!Diff.isUsable())
3708 return nullptr;
3709 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003710 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003711 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3712 if (NewSize != C.getTypeSize(Type)) {
3713 if (NewSize < C.getTypeSize(Type)) {
3714 assert(NewSize == 64 && "incorrect loop var size");
3715 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3716 << InitSrcRange << ConditionSrcRange;
3717 }
3718 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003719 NewSize, Type->hasSignedIntegerRepresentation() ||
3720 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003721 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3722 Sema::AA_Converting, true);
3723 if (!Diff.isUsable())
3724 return nullptr;
3725 }
3726 }
3727
Alexander Musmana5f070a2014-10-01 06:03:56 +00003728 return Diff.get();
3729}
3730
Alexey Bataev62dbb972015-04-22 11:59:37 +00003731Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3732 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3733 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3734 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003735 TransformToNewDefs Transform(SemaRef);
3736
3737 auto NewLB = Transform.TransformExpr(LB);
3738 auto NewUB = Transform.TransformExpr(UB);
3739 if (NewLB.isInvalid() || NewUB.isInvalid())
3740 return Cond;
3741 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3742 Sema::AA_Converting,
3743 /*AllowExplicit=*/true);
3744 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3745 Sema::AA_Converting,
3746 /*AllowExplicit=*/true);
3747 if (NewLB.isInvalid() || NewUB.isInvalid())
3748 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003749 auto CondExpr = SemaRef.BuildBinOp(
3750 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3751 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003752 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003753 if (CondExpr.isUsable()) {
3754 CondExpr = SemaRef.PerformImplicitConversion(
3755 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3756 /*AllowExplicit=*/true);
3757 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003758 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3759 // Otherwise use original loop conditon and evaluate it in runtime.
3760 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3761}
3762
Alexander Musmana5f070a2014-10-01 06:03:56 +00003763/// \brief Build reference expression to the counter be used for codegen.
3764Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003765 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3766 DefaultLoc);
3767}
3768
3769Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3770 if (Var && !Var->isInvalidDecl()) {
3771 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003772 auto *PrivateVar =
3773 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3774 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003775 if (PrivateVar->isInvalidDecl())
3776 return nullptr;
3777 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3778 }
3779 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003780}
3781
3782/// \brief Build initization of the counter be used for codegen.
3783Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3784
3785/// \brief Build step of the counter be used for codegen.
3786Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3787
3788/// \brief Iteration space of a single for loop.
3789struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003790 /// \brief Condition of the loop.
3791 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003792 /// \brief This expression calculates the number of iterations in the loop.
3793 /// It is always possible to calculate it before starting the loop.
3794 Expr *NumIterations;
3795 /// \brief The loop counter variable.
3796 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003797 /// \brief Private loop counter variable.
3798 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003799 /// \brief This is initializer for the initial value of #CounterVar.
3800 Expr *CounterInit;
3801 /// \brief This is step for the #CounterVar used to generate its update:
3802 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3803 Expr *CounterStep;
3804 /// \brief Should step be subtracted?
3805 bool Subtract;
3806 /// \brief Source range of the loop init.
3807 SourceRange InitSrcRange;
3808 /// \brief Source range of the loop condition.
3809 SourceRange CondSrcRange;
3810 /// \brief Source range of the loop increment.
3811 SourceRange IncSrcRange;
3812};
3813
Alexey Bataev23b69422014-06-18 07:08:49 +00003814} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003815
Alexey Bataev9c821032015-04-30 04:23:23 +00003816void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3817 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3818 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003819 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3820 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003821 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3822 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003823 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003824 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003825 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003826 }
3827}
3828
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003829/// \brief Called on a for stmt to check and extract its iteration space
3830/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003831static bool CheckOpenMPIterationSpace(
3832 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3833 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003834 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003835 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003836 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003837 // OpenMP [2.6, Canonical Loop Form]
3838 // for (init-expr; test-expr; incr-expr) structured-block
3839 auto For = dyn_cast_or_null<ForStmt>(S);
3840 if (!For) {
3841 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003842 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3843 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3844 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3845 if (NestedLoopCount > 1) {
3846 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3847 SemaRef.Diag(DSA.getConstructLoc(),
3848 diag::note_omp_collapse_ordered_expr)
3849 << 2 << CollapseLoopCountExpr->getSourceRange()
3850 << OrderedLoopCountExpr->getSourceRange();
3851 else if (CollapseLoopCountExpr)
3852 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3853 diag::note_omp_collapse_ordered_expr)
3854 << 0 << CollapseLoopCountExpr->getSourceRange();
3855 else
3856 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3857 diag::note_omp_collapse_ordered_expr)
3858 << 1 << OrderedLoopCountExpr->getSourceRange();
3859 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003860 return true;
3861 }
3862 assert(For->getBody());
3863
3864 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3865
3866 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003867 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003868 if (ISC.CheckInit(Init)) {
3869 return true;
3870 }
3871
3872 bool HasErrors = false;
3873
3874 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003875 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876
3877 // OpenMP [2.6, Canonical Loop Form]
3878 // Var is one of the following:
3879 // A variable of signed or unsigned integer type.
3880 // For C++, a variable of a random access iterator type.
3881 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003882 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3884 !VarType->isPointerType() &&
3885 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3886 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3887 << SemaRef.getLangOpts().CPlusPlus;
3888 HasErrors = true;
3889 }
3890
Alexey Bataev4acb8592014-07-07 13:01:15 +00003891 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3892 // Construct
3893 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3894 // parallel for construct is (are) private.
3895 // The loop iteration variable in the associated for-loop of a simd construct
3896 // with just one associated for-loop is linear with a constant-linear-step
3897 // that is the increment of the associated for-loop.
3898 // Exclude loop var from the list of variables with implicitly defined data
3899 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003900 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003901
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003902 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3903 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003904 // The loop iteration variable in the associated for-loop of a simd construct
3905 // with just one associated for-loop may be listed in a linear clause with a
3906 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003907 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3908 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003909 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003910 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3911 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3912 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003913 auto PredeterminedCKind =
3914 isOpenMPSimdDirective(DKind)
3915 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3916 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003917 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003918 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003919 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003920 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003921 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003922 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3923 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003924 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003925 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3926 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003927 if (DVar.RefExpr == nullptr)
3928 DVar.CKind = PredeterminedCKind;
3929 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003931 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003932 // Make the loop iteration variable private (for worksharing constructs),
3933 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003934 // lastprivate (for simd directives with several collapsed or ordered
3935 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003936 if (DVar.CKind == OMPC_unknown)
3937 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3938 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003939 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 }
3941
Alexey Bataev7ff55242014-06-19 09:13:45 +00003942 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003943
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003944 // Check test-expr.
3945 HasErrors |= ISC.CheckCond(For->getCond());
3946
3947 // Check incr-expr.
3948 HasErrors |= ISC.CheckInc(For->getInc());
3949
Alexander Musmana5f070a2014-10-01 06:03:56 +00003950 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003951 return HasErrors;
3952
Alexander Musmana5f070a2014-10-01 06:03:56 +00003953 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003954 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003955 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003956 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003957 isOpenMPTaskLoopDirective(DKind) ||
3958 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003960 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003961 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3962 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3963 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3964 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3965 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3966 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3967
Alexey Bataev62dbb972015-04-22 11:59:37 +00003968 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3969 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003970 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003971 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003972 ResultIterSpace.CounterInit == nullptr ||
3973 ResultIterSpace.CounterStep == nullptr);
3974
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975 return HasErrors;
3976}
3977
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003978/// \brief Build 'VarRef = Start.
3979static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3980 ExprResult VarRef, ExprResult Start) {
3981 TransformToNewDefs Transform(SemaRef);
3982 // Build 'VarRef = Start.
3983 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3984 if (NewStart.isInvalid())
3985 return ExprError();
3986 NewStart = SemaRef.PerformImplicitConversion(
3987 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3988 Sema::AA_Converting,
3989 /*AllowExplicit=*/true);
3990 if (NewStart.isInvalid())
3991 return ExprError();
3992 NewStart = SemaRef.PerformImplicitConversion(
3993 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3994 /*AllowExplicit=*/true);
3995 if (!NewStart.isUsable())
3996 return ExprError();
3997
3998 auto Init =
3999 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4000 return Init;
4001}
4002
Alexander Musmana5f070a2014-10-01 06:03:56 +00004003/// \brief Build 'VarRef = Start + Iter * Step'.
4004static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4005 SourceLocation Loc, ExprResult VarRef,
4006 ExprResult Start, ExprResult Iter,
4007 ExprResult Step, bool Subtract) {
4008 // Add parentheses (for debugging purposes only).
4009 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4010 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4011 !Step.isUsable())
4012 return ExprError();
4013
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004014 TransformToNewDefs Transform(SemaRef);
4015 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
4016 if (NewStep.isInvalid())
4017 return ExprError();
4018 NewStep = SemaRef.PerformImplicitConversion(
4019 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
4020 Sema::AA_Converting,
4021 /*AllowExplicit=*/true);
4022 if (NewStep.isInvalid())
4023 return ExprError();
4024 ExprResult Update =
4025 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004026 if (!Update.isUsable())
4027 return ExprError();
4028
4029 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004030 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4031 if (NewStart.isInvalid())
4032 return ExprError();
4033 NewStart = SemaRef.PerformImplicitConversion(
4034 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4035 Sema::AA_Converting,
4036 /*AllowExplicit=*/true);
4037 if (NewStart.isInvalid())
4038 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004039 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004040 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004041 if (!Update.isUsable())
4042 return ExprError();
4043
4044 Update = SemaRef.PerformImplicitConversion(
4045 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4046 if (!Update.isUsable())
4047 return ExprError();
4048
4049 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4050 return Update;
4051}
4052
4053/// \brief Convert integer expression \a E to make it have at least \a Bits
4054/// bits.
4055static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4056 Sema &SemaRef) {
4057 if (E == nullptr)
4058 return ExprError();
4059 auto &C = SemaRef.Context;
4060 QualType OldType = E->getType();
4061 unsigned HasBits = C.getTypeSize(OldType);
4062 if (HasBits >= Bits)
4063 return ExprResult(E);
4064 // OK to convert to signed, because new type has more bits than old.
4065 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4066 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4067 true);
4068}
4069
4070/// \brief Check if the given expression \a E is a constant integer that fits
4071/// into \a Bits bits.
4072static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4073 if (E == nullptr)
4074 return false;
4075 llvm::APSInt Result;
4076 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4077 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4078 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004079}
4080
4081/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004082/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4083/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004084static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004085CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4086 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4087 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004088 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004089 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004090 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004091 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004092 // Found 'collapse' clause - calculate collapse number.
4093 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004094 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004095 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004096 }
4097 if (OrderedLoopCountExpr) {
4098 // Found 'ordered' clause - calculate collapse number.
4099 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004100 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4101 if (Result.getLimitedValue() < NestedLoopCount) {
4102 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4103 diag::err_omp_wrong_ordered_loop_count)
4104 << OrderedLoopCountExpr->getSourceRange();
4105 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4106 diag::note_collapse_loop_count)
4107 << CollapseLoopCountExpr->getSourceRange();
4108 }
4109 NestedLoopCount = Result.getLimitedValue();
4110 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004111 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004112 // This is helper routine for loop directives (e.g., 'for', 'simd',
4113 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004114 SmallVector<LoopIterationSpace, 4> IterSpaces;
4115 IterSpaces.resize(NestedLoopCount);
4116 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004117 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004118 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004119 NestedLoopCount, CollapseLoopCountExpr,
4120 OrderedLoopCountExpr, VarsWithImplicitDSA,
4121 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004122 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004123 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004124 // OpenMP [2.8.1, simd construct, Restrictions]
4125 // All loops associated with the construct must be perfectly nested; that
4126 // is, there must be no intervening code nor any OpenMP directive between
4127 // any two loops.
4128 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 }
4130
Alexander Musmana5f070a2014-10-01 06:03:56 +00004131 Built.clear(/* size */ NestedLoopCount);
4132
4133 if (SemaRef.CurContext->isDependentContext())
4134 return NestedLoopCount;
4135
4136 // An example of what is generated for the following code:
4137 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004138 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004139 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004140 // for (k = 0; k < NK; ++k)
4141 // for (j = J0; j < NJ; j+=2) {
4142 // <loop body>
4143 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004144 //
4145 // We generate the code below.
4146 // Note: the loop body may be outlined in CodeGen.
4147 // Note: some counters may be C++ classes, operator- is used to find number of
4148 // iterations and operator+= to calculate counter value.
4149 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4150 // or i64 is currently supported).
4151 //
4152 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4153 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4154 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4155 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4156 // // similar updates for vars in clauses (e.g. 'linear')
4157 // <loop body (using local i and j)>
4158 // }
4159 // i = NI; // assign final values of counters
4160 // j = NJ;
4161 //
4162
4163 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4164 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004165 // Precondition tests if there is at least one iteration (all conditions are
4166 // true).
4167 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004168 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004169 ExprResult LastIteration32 = WidenIterationCount(
4170 32 /* Bits */, SemaRef.PerformImplicitConversion(
4171 N0->IgnoreImpCasts(), N0->getType(),
4172 Sema::AA_Converting, /*AllowExplicit=*/true)
4173 .get(),
4174 SemaRef);
4175 ExprResult LastIteration64 = WidenIterationCount(
4176 64 /* Bits */, SemaRef.PerformImplicitConversion(
4177 N0->IgnoreImpCasts(), N0->getType(),
4178 Sema::AA_Converting, /*AllowExplicit=*/true)
4179 .get(),
4180 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004181
4182 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4183 return NestedLoopCount;
4184
4185 auto &C = SemaRef.Context;
4186 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4187
4188 Scope *CurScope = DSA.getCurScope();
4189 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004190 if (PreCond.isUsable()) {
4191 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4192 PreCond.get(), IterSpaces[Cnt].PreCond);
4193 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004194 auto N = IterSpaces[Cnt].NumIterations;
4195 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4196 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004197 LastIteration32 = SemaRef.BuildBinOp(
4198 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4199 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4200 Sema::AA_Converting,
4201 /*AllowExplicit=*/true)
4202 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004203 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004204 LastIteration64 = SemaRef.BuildBinOp(
4205 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4206 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4207 Sema::AA_Converting,
4208 /*AllowExplicit=*/true)
4209 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004210 }
4211
4212 // Choose either the 32-bit or 64-bit version.
4213 ExprResult LastIteration = LastIteration64;
4214 if (LastIteration32.isUsable() &&
4215 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4216 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4217 FitsInto(
4218 32 /* Bits */,
4219 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4220 LastIteration64.get(), SemaRef)))
4221 LastIteration = LastIteration32;
4222
4223 if (!LastIteration.isUsable())
4224 return 0;
4225
4226 // Save the number of iterations.
4227 ExprResult NumIterations = LastIteration;
4228 {
4229 LastIteration = SemaRef.BuildBinOp(
4230 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4231 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4232 if (!LastIteration.isUsable())
4233 return 0;
4234 }
4235
4236 // Calculate the last iteration number beforehand instead of doing this on
4237 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4238 llvm::APSInt Result;
4239 bool IsConstant =
4240 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4241 ExprResult CalcLastIteration;
4242 if (!IsConstant) {
4243 SourceLocation SaveLoc;
4244 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004245 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004246 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004247 ExprResult SaveRef = buildDeclRefExpr(
4248 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004249 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4250 SaveRef.get(), LastIteration.get());
4251 LastIteration = SaveRef;
4252
4253 // Prepare SaveRef + 1.
4254 NumIterations = SemaRef.BuildBinOp(
4255 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4256 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4257 if (!NumIterations.isUsable())
4258 return 0;
4259 }
4260
4261 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4262
Alexander Musmanc6388682014-12-15 07:07:06 +00004263 QualType VType = LastIteration.get()->getType();
4264 // Build variables passed into runtime, nesessary for worksharing directives.
4265 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004266 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4267 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004268 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004269 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4270 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004271 SemaRef.AddInitializerToDecl(
4272 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4273 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4274
4275 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004276 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4277 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004278 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4279 /*DirectInit*/ false,
4280 /*TypeMayContainAuto*/ false);
4281
4282 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4283 // This will be used to implement clause 'lastprivate'.
4284 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004285 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4286 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004287 SemaRef.AddInitializerToDecl(
4288 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4289 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4290
4291 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004292 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4293 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004294 SemaRef.AddInitializerToDecl(
4295 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4296 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4297
4298 // Build expression: UB = min(UB, LastIteration)
4299 // It is nesessary for CodeGen of directives with static scheduling.
4300 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4301 UB.get(), LastIteration.get());
4302 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4303 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4304 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4305 CondOp.get());
4306 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4307 }
4308
4309 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004310 ExprResult IV;
4311 ExprResult Init;
4312 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004313 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4314 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004315 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004316 isOpenMPTaskLoopDirective(DKind) ||
4317 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004318 ? LB.get()
4319 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4320 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4321 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004322 }
4323
Alexander Musmanc6388682014-12-15 07:07:06 +00004324 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004325 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004326 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004327 (isOpenMPWorksharingDirective(DKind) ||
4328 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004329 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4330 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4331 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004332
4333 // Loop increment (IV = IV + 1)
4334 SourceLocation IncLoc;
4335 ExprResult Inc =
4336 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4337 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4338 if (!Inc.isUsable())
4339 return 0;
4340 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004341 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4342 if (!Inc.isUsable())
4343 return 0;
4344
4345 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4346 // Used for directives with static scheduling.
4347 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004348 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4349 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004350 // LB + ST
4351 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4352 if (!NextLB.isUsable())
4353 return 0;
4354 // LB = LB + ST
4355 NextLB =
4356 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4357 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4358 if (!NextLB.isUsable())
4359 return 0;
4360 // UB + ST
4361 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4362 if (!NextUB.isUsable())
4363 return 0;
4364 // UB = UB + ST
4365 NextUB =
4366 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4367 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4368 if (!NextUB.isUsable())
4369 return 0;
4370 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004371
4372 // Build updates and final values of the loop counters.
4373 bool HasErrors = false;
4374 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004375 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004376 Built.Updates.resize(NestedLoopCount);
4377 Built.Finals.resize(NestedLoopCount);
4378 {
4379 ExprResult Div;
4380 // Go from inner nested loop to outer.
4381 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4382 LoopIterationSpace &IS = IterSpaces[Cnt];
4383 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4384 // Build: Iter = (IV / Div) % IS.NumIters
4385 // where Div is product of previous iterations' IS.NumIters.
4386 ExprResult Iter;
4387 if (Div.isUsable()) {
4388 Iter =
4389 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4390 } else {
4391 Iter = IV;
4392 assert((Cnt == (int)NestedLoopCount - 1) &&
4393 "unusable div expected on first iteration only");
4394 }
4395
4396 if (Cnt != 0 && Iter.isUsable())
4397 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4398 IS.NumIterations);
4399 if (!Iter.isUsable()) {
4400 HasErrors = true;
4401 break;
4402 }
4403
Alexey Bataev39f915b82015-05-08 10:41:21 +00004404 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4405 auto *CounterVar = buildDeclRefExpr(
4406 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4407 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4408 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004409 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4410 IS.CounterInit);
4411 if (!Init.isUsable()) {
4412 HasErrors = true;
4413 break;
4414 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004415 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004416 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004417 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4418 if (!Update.isUsable()) {
4419 HasErrors = true;
4420 break;
4421 }
4422
4423 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4424 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004425 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004426 IS.NumIterations, IS.CounterStep, IS.Subtract);
4427 if (!Final.isUsable()) {
4428 HasErrors = true;
4429 break;
4430 }
4431
4432 // Build Div for the next iteration: Div <- Div * IS.NumIters
4433 if (Cnt != 0) {
4434 if (Div.isUnset())
4435 Div = IS.NumIterations;
4436 else
4437 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4438 IS.NumIterations);
4439
4440 // Add parentheses (for debugging purposes only).
4441 if (Div.isUsable())
4442 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4443 if (!Div.isUsable()) {
4444 HasErrors = true;
4445 break;
4446 }
4447 }
4448 if (!Update.isUsable() || !Final.isUsable()) {
4449 HasErrors = true;
4450 break;
4451 }
4452 // Save results
4453 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004454 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004455 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004456 Built.Updates[Cnt] = Update.get();
4457 Built.Finals[Cnt] = Final.get();
4458 }
4459 }
4460
4461 if (HasErrors)
4462 return 0;
4463
4464 // Save results
4465 Built.IterationVarRef = IV.get();
4466 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004467 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004468 Built.CalcLastIteration =
4469 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004470 Built.PreCond = PreCond.get();
4471 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472 Built.Init = Init.get();
4473 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004474 Built.LB = LB.get();
4475 Built.UB = UB.get();
4476 Built.IL = IL.get();
4477 Built.ST = ST.get();
4478 Built.EUB = EUB.get();
4479 Built.NLB = NextLB.get();
4480 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004481
Alexey Bataevabfc0692014-06-25 06:52:00 +00004482 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004483}
4484
Alexey Bataev10e775f2015-07-30 11:36:16 +00004485static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004486 auto CollapseClauses =
4487 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4488 if (CollapseClauses.begin() != CollapseClauses.end())
4489 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004490 return nullptr;
4491}
4492
Alexey Bataev10e775f2015-07-30 11:36:16 +00004493static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004494 auto OrderedClauses =
4495 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4496 if (OrderedClauses.begin() != OrderedClauses.end())
4497 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004498 return nullptr;
4499}
4500
Alexey Bataev66b15b52015-08-21 11:14:16 +00004501static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4502 const Expr *Safelen) {
4503 llvm::APSInt SimdlenRes, SafelenRes;
4504 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4505 Simdlen->isInstantiationDependent() ||
4506 Simdlen->containsUnexpandedParameterPack())
4507 return false;
4508 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4509 Safelen->isInstantiationDependent() ||
4510 Safelen->containsUnexpandedParameterPack())
4511 return false;
4512 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4513 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4514 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4515 // If both simdlen and safelen clauses are specified, the value of the simdlen
4516 // parameter must be less than or equal to the value of the safelen parameter.
4517 if (SimdlenRes > SafelenRes) {
4518 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4519 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4520 return true;
4521 }
4522 return false;
4523}
4524
Alexey Bataev4acb8592014-07-07 13:01:15 +00004525StmtResult Sema::ActOnOpenMPSimdDirective(
4526 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4527 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004528 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004529 if (!AStmt)
4530 return StmtError();
4531
4532 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004533 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004534 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4535 // define the nested loops number.
4536 unsigned NestedLoopCount = CheckOpenMPLoop(
4537 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4538 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004539 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004540 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004541
Alexander Musmana5f070a2014-10-01 06:03:56 +00004542 assert((CurContext->isDependentContext() || B.builtAll()) &&
4543 "omp simd loop exprs were not built");
4544
Alexander Musman3276a272015-03-21 10:12:56 +00004545 if (!CurContext->isDependentContext()) {
4546 // Finalize the clauses that need pre-built expressions for CodeGen.
4547 for (auto C : Clauses) {
4548 if (auto LC = dyn_cast<OMPLinearClause>(C))
4549 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4550 B.NumIterations, *this, CurScope))
4551 return StmtError();
4552 }
4553 }
4554
Alexey Bataev66b15b52015-08-21 11:14:16 +00004555 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4556 // If both simdlen and safelen clauses are specified, the value of the simdlen
4557 // parameter must be less than or equal to the value of the safelen parameter.
4558 OMPSafelenClause *Safelen = nullptr;
4559 OMPSimdlenClause *Simdlen = nullptr;
4560 for (auto *Clause : Clauses) {
4561 if (Clause->getClauseKind() == OMPC_safelen)
4562 Safelen = cast<OMPSafelenClause>(Clause);
4563 else if (Clause->getClauseKind() == OMPC_simdlen)
4564 Simdlen = cast<OMPSimdlenClause>(Clause);
4565 if (Safelen && Simdlen)
4566 break;
4567 }
4568 if (Simdlen && Safelen &&
4569 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4570 Safelen->getSafelen()))
4571 return StmtError();
4572
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004573 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004574 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4575 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004576}
4577
Alexey Bataev4acb8592014-07-07 13:01:15 +00004578StmtResult Sema::ActOnOpenMPForDirective(
4579 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4580 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004581 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004582 if (!AStmt)
4583 return StmtError();
4584
4585 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004586 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004587 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4588 // define the nested loops number.
4589 unsigned NestedLoopCount = CheckOpenMPLoop(
4590 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4591 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004592 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004593 return StmtError();
4594
Alexander Musmana5f070a2014-10-01 06:03:56 +00004595 assert((CurContext->isDependentContext() || B.builtAll()) &&
4596 "omp for loop exprs were not built");
4597
Alexey Bataev54acd402015-08-04 11:18:19 +00004598 if (!CurContext->isDependentContext()) {
4599 // Finalize the clauses that need pre-built expressions for CodeGen.
4600 for (auto C : Clauses) {
4601 if (auto LC = dyn_cast<OMPLinearClause>(C))
4602 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4603 B.NumIterations, *this, CurScope))
4604 return StmtError();
4605 }
4606 }
4607
Alexey Bataevf29276e2014-06-18 04:14:57 +00004608 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004609 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004610 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004611}
4612
Alexander Musmanf82886e2014-09-18 05:12:34 +00004613StmtResult Sema::ActOnOpenMPForSimdDirective(
4614 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4615 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004616 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004617 if (!AStmt)
4618 return StmtError();
4619
4620 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004621 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004622 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4623 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004624 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004625 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4626 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4627 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004628 if (NestedLoopCount == 0)
4629 return StmtError();
4630
Alexander Musmanc6388682014-12-15 07:07:06 +00004631 assert((CurContext->isDependentContext() || B.builtAll()) &&
4632 "omp for simd loop exprs were not built");
4633
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004634 if (!CurContext->isDependentContext()) {
4635 // Finalize the clauses that need pre-built expressions for CodeGen.
4636 for (auto C : Clauses) {
4637 if (auto LC = dyn_cast<OMPLinearClause>(C))
4638 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4639 B.NumIterations, *this, CurScope))
4640 return StmtError();
4641 }
4642 }
4643
Alexey Bataev66b15b52015-08-21 11:14:16 +00004644 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4645 // If both simdlen and safelen clauses are specified, the value of the simdlen
4646 // parameter must be less than or equal to the value of the safelen parameter.
4647 OMPSafelenClause *Safelen = nullptr;
4648 OMPSimdlenClause *Simdlen = nullptr;
4649 for (auto *Clause : Clauses) {
4650 if (Clause->getClauseKind() == OMPC_safelen)
4651 Safelen = cast<OMPSafelenClause>(Clause);
4652 else if (Clause->getClauseKind() == OMPC_simdlen)
4653 Simdlen = cast<OMPSimdlenClause>(Clause);
4654 if (Safelen && Simdlen)
4655 break;
4656 }
4657 if (Simdlen && Safelen &&
4658 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4659 Safelen->getSafelen()))
4660 return StmtError();
4661
Alexander Musmanf82886e2014-09-18 05:12:34 +00004662 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004663 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4664 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004665}
4666
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004667StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4668 Stmt *AStmt,
4669 SourceLocation StartLoc,
4670 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004671 if (!AStmt)
4672 return StmtError();
4673
4674 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004675 auto BaseStmt = AStmt;
4676 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4677 BaseStmt = CS->getCapturedStmt();
4678 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4679 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004680 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004681 return StmtError();
4682 // All associated statements must be '#pragma omp section' except for
4683 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004684 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004685 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4686 if (SectionStmt)
4687 Diag(SectionStmt->getLocStart(),
4688 diag::err_omp_sections_substmt_not_section);
4689 return StmtError();
4690 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004691 cast<OMPSectionDirective>(SectionStmt)
4692 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004693 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004694 } else {
4695 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4696 return StmtError();
4697 }
4698
4699 getCurFunction()->setHasBranchProtectedScope();
4700
Alexey Bataev25e5b442015-09-15 12:52:43 +00004701 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4702 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004703}
4704
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004705StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4706 SourceLocation StartLoc,
4707 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004708 if (!AStmt)
4709 return StmtError();
4710
4711 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004712
4713 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004714 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004715
Alexey Bataev25e5b442015-09-15 12:52:43 +00004716 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4717 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004718}
4719
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004720StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4721 Stmt *AStmt,
4722 SourceLocation StartLoc,
4723 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004724 if (!AStmt)
4725 return StmtError();
4726
4727 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004728
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004729 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004730
Alexey Bataev3255bf32015-01-19 05:20:46 +00004731 // OpenMP [2.7.3, single Construct, Restrictions]
4732 // The copyprivate clause must not be used with the nowait clause.
4733 OMPClause *Nowait = nullptr;
4734 OMPClause *Copyprivate = nullptr;
4735 for (auto *Clause : Clauses) {
4736 if (Clause->getClauseKind() == OMPC_nowait)
4737 Nowait = Clause;
4738 else if (Clause->getClauseKind() == OMPC_copyprivate)
4739 Copyprivate = Clause;
4740 if (Copyprivate && Nowait) {
4741 Diag(Copyprivate->getLocStart(),
4742 diag::err_omp_single_copyprivate_with_nowait);
4743 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4744 return StmtError();
4745 }
4746 }
4747
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004748 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4749}
4750
Alexander Musman80c22892014-07-17 08:54:58 +00004751StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4752 SourceLocation StartLoc,
4753 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004754 if (!AStmt)
4755 return StmtError();
4756
4757 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004758
4759 getCurFunction()->setHasBranchProtectedScope();
4760
4761 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4762}
4763
Alexey Bataev28c75412015-12-15 08:19:24 +00004764StmtResult Sema::ActOnOpenMPCriticalDirective(
4765 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4766 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004767 if (!AStmt)
4768 return StmtError();
4769
4770 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004771
Alexey Bataev28c75412015-12-15 08:19:24 +00004772 bool ErrorFound = false;
4773 llvm::APSInt Hint;
4774 SourceLocation HintLoc;
4775 bool DependentHint = false;
4776 for (auto *C : Clauses) {
4777 if (C->getClauseKind() == OMPC_hint) {
4778 if (!DirName.getName()) {
4779 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4780 ErrorFound = true;
4781 }
4782 Expr *E = cast<OMPHintClause>(C)->getHint();
4783 if (E->isTypeDependent() || E->isValueDependent() ||
4784 E->isInstantiationDependent())
4785 DependentHint = true;
4786 else {
4787 Hint = E->EvaluateKnownConstInt(Context);
4788 HintLoc = C->getLocStart();
4789 }
4790 }
4791 }
4792 if (ErrorFound)
4793 return StmtError();
4794 auto Pair = DSAStack->getCriticalWithHint(DirName);
4795 if (Pair.first && DirName.getName() && !DependentHint) {
4796 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4797 Diag(StartLoc, diag::err_omp_critical_with_hint);
4798 if (HintLoc.isValid()) {
4799 Diag(HintLoc, diag::note_omp_critical_hint_here)
4800 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4801 } else
4802 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4803 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4804 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4805 << 1
4806 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4807 /*Radix=*/10, /*Signed=*/false);
4808 } else
4809 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4810 }
4811 }
4812
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004813 getCurFunction()->setHasBranchProtectedScope();
4814
Alexey Bataev28c75412015-12-15 08:19:24 +00004815 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4816 Clauses, AStmt);
4817 if (!Pair.first && DirName.getName() && !DependentHint)
4818 DSAStack->addCriticalWithHint(Dir, Hint);
4819 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004820}
4821
Alexey Bataev4acb8592014-07-07 13:01:15 +00004822StmtResult Sema::ActOnOpenMPParallelForDirective(
4823 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4824 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004825 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004826 if (!AStmt)
4827 return StmtError();
4828
Alexey Bataev4acb8592014-07-07 13:01:15 +00004829 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4830 // 1.2.2 OpenMP Language Terminology
4831 // Structured block - An executable statement with a single entry at the
4832 // top and a single exit at the bottom.
4833 // The point of exit cannot be a branch out of the structured block.
4834 // longjmp() and throw() must not violate the entry/exit criteria.
4835 CS->getCapturedDecl()->setNothrow();
4836
Alexander Musmanc6388682014-12-15 07:07:06 +00004837 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004838 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4839 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004840 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004841 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4842 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4843 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004844 if (NestedLoopCount == 0)
4845 return StmtError();
4846
Alexander Musmana5f070a2014-10-01 06:03:56 +00004847 assert((CurContext->isDependentContext() || B.builtAll()) &&
4848 "omp parallel for loop exprs were not built");
4849
Alexey Bataev54acd402015-08-04 11:18:19 +00004850 if (!CurContext->isDependentContext()) {
4851 // Finalize the clauses that need pre-built expressions for CodeGen.
4852 for (auto C : Clauses) {
4853 if (auto LC = dyn_cast<OMPLinearClause>(C))
4854 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4855 B.NumIterations, *this, CurScope))
4856 return StmtError();
4857 }
4858 }
4859
Alexey Bataev4acb8592014-07-07 13:01:15 +00004860 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004861 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004862 NestedLoopCount, Clauses, AStmt, B,
4863 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004864}
4865
Alexander Musmane4e893b2014-09-23 09:33:00 +00004866StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4867 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4868 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004869 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004870 if (!AStmt)
4871 return StmtError();
4872
Alexander Musmane4e893b2014-09-23 09:33:00 +00004873 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4874 // 1.2.2 OpenMP Language Terminology
4875 // Structured block - An executable statement with a single entry at the
4876 // top and a single exit at the bottom.
4877 // The point of exit cannot be a branch out of the structured block.
4878 // longjmp() and throw() must not violate the entry/exit criteria.
4879 CS->getCapturedDecl()->setNothrow();
4880
Alexander Musmanc6388682014-12-15 07:07:06 +00004881 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004882 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4883 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004884 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004885 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4886 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4887 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004888 if (NestedLoopCount == 0)
4889 return StmtError();
4890
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004891 if (!CurContext->isDependentContext()) {
4892 // Finalize the clauses that need pre-built expressions for CodeGen.
4893 for (auto C : Clauses) {
4894 if (auto LC = dyn_cast<OMPLinearClause>(C))
4895 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4896 B.NumIterations, *this, CurScope))
4897 return StmtError();
4898 }
4899 }
4900
Alexey Bataev66b15b52015-08-21 11:14:16 +00004901 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4902 // If both simdlen and safelen clauses are specified, the value of the simdlen
4903 // parameter must be less than or equal to the value of the safelen parameter.
4904 OMPSafelenClause *Safelen = nullptr;
4905 OMPSimdlenClause *Simdlen = nullptr;
4906 for (auto *Clause : Clauses) {
4907 if (Clause->getClauseKind() == OMPC_safelen)
4908 Safelen = cast<OMPSafelenClause>(Clause);
4909 else if (Clause->getClauseKind() == OMPC_simdlen)
4910 Simdlen = cast<OMPSimdlenClause>(Clause);
4911 if (Safelen && Simdlen)
4912 break;
4913 }
4914 if (Simdlen && Safelen &&
4915 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4916 Safelen->getSafelen()))
4917 return StmtError();
4918
Alexander Musmane4e893b2014-09-23 09:33:00 +00004919 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004920 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004921 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004922}
4923
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004924StmtResult
4925Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4926 Stmt *AStmt, SourceLocation StartLoc,
4927 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004928 if (!AStmt)
4929 return StmtError();
4930
4931 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004932 auto BaseStmt = AStmt;
4933 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4934 BaseStmt = CS->getCapturedStmt();
4935 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4936 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004937 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004938 return StmtError();
4939 // All associated statements must be '#pragma omp section' except for
4940 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004941 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004942 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4943 if (SectionStmt)
4944 Diag(SectionStmt->getLocStart(),
4945 diag::err_omp_parallel_sections_substmt_not_section);
4946 return StmtError();
4947 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004948 cast<OMPSectionDirective>(SectionStmt)
4949 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004950 }
4951 } else {
4952 Diag(AStmt->getLocStart(),
4953 diag::err_omp_parallel_sections_not_compound_stmt);
4954 return StmtError();
4955 }
4956
4957 getCurFunction()->setHasBranchProtectedScope();
4958
Alexey Bataev25e5b442015-09-15 12:52:43 +00004959 return OMPParallelSectionsDirective::Create(
4960 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004961}
4962
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004963StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4964 Stmt *AStmt, SourceLocation StartLoc,
4965 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004966 if (!AStmt)
4967 return StmtError();
4968
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004969 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4970 // 1.2.2 OpenMP Language Terminology
4971 // Structured block - An executable statement with a single entry at the
4972 // top and a single exit at the bottom.
4973 // The point of exit cannot be a branch out of the structured block.
4974 // longjmp() and throw() must not violate the entry/exit criteria.
4975 CS->getCapturedDecl()->setNothrow();
4976
4977 getCurFunction()->setHasBranchProtectedScope();
4978
Alexey Bataev25e5b442015-09-15 12:52:43 +00004979 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4980 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004981}
4982
Alexey Bataev68446b72014-07-18 07:47:19 +00004983StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4984 SourceLocation EndLoc) {
4985 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4986}
4987
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004988StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4989 SourceLocation EndLoc) {
4990 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4991}
4992
Alexey Bataev2df347a2014-07-18 10:17:07 +00004993StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4994 SourceLocation EndLoc) {
4995 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4996}
4997
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004998StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4999 SourceLocation StartLoc,
5000 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005001 if (!AStmt)
5002 return StmtError();
5003
5004 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005005
5006 getCurFunction()->setHasBranchProtectedScope();
5007
5008 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5009}
5010
Alexey Bataev6125da92014-07-21 11:26:11 +00005011StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5012 SourceLocation StartLoc,
5013 SourceLocation EndLoc) {
5014 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5015 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5016}
5017
Alexey Bataev346265e2015-09-25 10:37:12 +00005018StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5019 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005020 SourceLocation StartLoc,
5021 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005022 OMPClause *DependFound = nullptr;
5023 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005024 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005025 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005026 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005027 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005028 for (auto *C : Clauses) {
5029 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5030 DependFound = C;
5031 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5032 if (DependSourceClause) {
5033 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5034 << getOpenMPDirectiveName(OMPD_ordered)
5035 << getOpenMPClauseName(OMPC_depend) << 2;
5036 ErrorFound = true;
5037 } else
5038 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005039 if (DependSinkClause) {
5040 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5041 << 0;
5042 ErrorFound = true;
5043 }
5044 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5045 if (DependSourceClause) {
5046 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5047 << 1;
5048 ErrorFound = true;
5049 }
5050 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005051 }
5052 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005053 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005054 else if (C->getClauseKind() == OMPC_simd)
5055 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005056 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005057 if (!ErrorFound && !SC &&
5058 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005059 // OpenMP [2.8.1,simd Construct, Restrictions]
5060 // An ordered construct with the simd clause is the only OpenMP construct
5061 // that can appear in the simd region.
5062 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005063 ErrorFound = true;
5064 } else if (DependFound && (TC || SC)) {
5065 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5066 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5067 ErrorFound = true;
5068 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5069 Diag(DependFound->getLocStart(),
5070 diag::err_omp_ordered_directive_without_param);
5071 ErrorFound = true;
5072 } else if (TC || Clauses.empty()) {
5073 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5074 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5075 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5076 << (TC != nullptr);
5077 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5078 ErrorFound = true;
5079 }
5080 }
5081 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005082 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005083
5084 if (AStmt) {
5085 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5086
5087 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005088 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005089
5090 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005091}
5092
Alexey Bataev1d160b12015-03-13 12:27:31 +00005093namespace {
5094/// \brief Helper class for checking expression in 'omp atomic [update]'
5095/// construct.
5096class OpenMPAtomicUpdateChecker {
5097 /// \brief Error results for atomic update expressions.
5098 enum ExprAnalysisErrorCode {
5099 /// \brief A statement is not an expression statement.
5100 NotAnExpression,
5101 /// \brief Expression is not builtin binary or unary operation.
5102 NotABinaryOrUnaryExpression,
5103 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5104 NotAnUnaryIncDecExpression,
5105 /// \brief An expression is not of scalar type.
5106 NotAScalarType,
5107 /// \brief A binary operation is not an assignment operation.
5108 NotAnAssignmentOp,
5109 /// \brief RHS part of the binary operation is not a binary expression.
5110 NotABinaryExpression,
5111 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5112 /// expression.
5113 NotABinaryOperator,
5114 /// \brief RHS binary operation does not have reference to the updated LHS
5115 /// part.
5116 NotAnUpdateExpression,
5117 /// \brief No errors is found.
5118 NoError
5119 };
5120 /// \brief Reference to Sema.
5121 Sema &SemaRef;
5122 /// \brief A location for note diagnostics (when error is found).
5123 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005124 /// \brief 'x' lvalue part of the source atomic expression.
5125 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005126 /// \brief 'expr' rvalue part of the source atomic expression.
5127 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005128 /// \brief Helper expression of the form
5129 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5130 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5131 Expr *UpdateExpr;
5132 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5133 /// important for non-associative operations.
5134 bool IsXLHSInRHSPart;
5135 BinaryOperatorKind Op;
5136 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005137 /// \brief true if the source expression is a postfix unary operation, false
5138 /// if it is a prefix unary operation.
5139 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005140
5141public:
5142 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005143 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005144 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005145 /// \brief Check specified statement that it is suitable for 'atomic update'
5146 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005147 /// expression. If DiagId and NoteId == 0, then only check is performed
5148 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005149 /// \param DiagId Diagnostic which should be emitted if error is found.
5150 /// \param NoteId Diagnostic note for the main error message.
5151 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005152 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005153 /// \brief Return the 'x' lvalue part of the source atomic expression.
5154 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005155 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5156 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005157 /// \brief Return the update expression used in calculation of the updated
5158 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5159 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5160 Expr *getUpdateExpr() const { return UpdateExpr; }
5161 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5162 /// false otherwise.
5163 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5164
Alexey Bataevb78ca832015-04-01 03:33:17 +00005165 /// \brief true if the source expression is a postfix unary operation, false
5166 /// if it is a prefix unary operation.
5167 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5168
Alexey Bataev1d160b12015-03-13 12:27:31 +00005169private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005170 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5171 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005172};
5173} // namespace
5174
5175bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5176 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5177 ExprAnalysisErrorCode ErrorFound = NoError;
5178 SourceLocation ErrorLoc, NoteLoc;
5179 SourceRange ErrorRange, NoteRange;
5180 // Allowed constructs are:
5181 // x = x binop expr;
5182 // x = expr binop x;
5183 if (AtomicBinOp->getOpcode() == BO_Assign) {
5184 X = AtomicBinOp->getLHS();
5185 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5186 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5187 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5188 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5189 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005190 Op = AtomicInnerBinOp->getOpcode();
5191 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005192 auto *LHS = AtomicInnerBinOp->getLHS();
5193 auto *RHS = AtomicInnerBinOp->getRHS();
5194 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5195 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5196 /*Canonical=*/true);
5197 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5198 /*Canonical=*/true);
5199 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5200 /*Canonical=*/true);
5201 if (XId == LHSId) {
5202 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005203 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005204 } else if (XId == RHSId) {
5205 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005206 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005207 } else {
5208 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5209 ErrorRange = AtomicInnerBinOp->getSourceRange();
5210 NoteLoc = X->getExprLoc();
5211 NoteRange = X->getSourceRange();
5212 ErrorFound = NotAnUpdateExpression;
5213 }
5214 } else {
5215 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5216 ErrorRange = AtomicInnerBinOp->getSourceRange();
5217 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5218 NoteRange = SourceRange(NoteLoc, NoteLoc);
5219 ErrorFound = NotABinaryOperator;
5220 }
5221 } else {
5222 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5223 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5224 ErrorFound = NotABinaryExpression;
5225 }
5226 } else {
5227 ErrorLoc = AtomicBinOp->getExprLoc();
5228 ErrorRange = AtomicBinOp->getSourceRange();
5229 NoteLoc = AtomicBinOp->getOperatorLoc();
5230 NoteRange = SourceRange(NoteLoc, NoteLoc);
5231 ErrorFound = NotAnAssignmentOp;
5232 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005233 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005234 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5235 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5236 return true;
5237 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005238 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005239 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005240}
5241
5242bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5243 unsigned NoteId) {
5244 ExprAnalysisErrorCode ErrorFound = NoError;
5245 SourceLocation ErrorLoc, NoteLoc;
5246 SourceRange ErrorRange, NoteRange;
5247 // Allowed constructs are:
5248 // x++;
5249 // x--;
5250 // ++x;
5251 // --x;
5252 // x binop= expr;
5253 // x = x binop expr;
5254 // x = expr binop x;
5255 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5256 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5257 if (AtomicBody->getType()->isScalarType() ||
5258 AtomicBody->isInstantiationDependent()) {
5259 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5260 AtomicBody->IgnoreParenImpCasts())) {
5261 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005262 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005263 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005264 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005265 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005266 X = AtomicCompAssignOp->getLHS();
5267 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005268 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5269 AtomicBody->IgnoreParenImpCasts())) {
5270 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005271 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5272 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005273 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005274 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5275 // Check for Unary Operation
5276 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005277 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005278 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5279 OpLoc = AtomicUnaryOp->getOperatorLoc();
5280 X = AtomicUnaryOp->getSubExpr();
5281 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5282 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005283 } else {
5284 ErrorFound = NotAnUnaryIncDecExpression;
5285 ErrorLoc = AtomicUnaryOp->getExprLoc();
5286 ErrorRange = AtomicUnaryOp->getSourceRange();
5287 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5288 NoteRange = SourceRange(NoteLoc, NoteLoc);
5289 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005290 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005291 ErrorFound = NotABinaryOrUnaryExpression;
5292 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5293 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5294 }
5295 } else {
5296 ErrorFound = NotAScalarType;
5297 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5298 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5299 }
5300 } else {
5301 ErrorFound = NotAnExpression;
5302 NoteLoc = ErrorLoc = S->getLocStart();
5303 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5304 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005305 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005306 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5307 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5308 return true;
5309 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005310 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005311 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005312 // Build an update expression of form 'OpaqueValueExpr(x) binop
5313 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5314 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5315 auto *OVEX = new (SemaRef.getASTContext())
5316 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5317 auto *OVEExpr = new (SemaRef.getASTContext())
5318 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5319 auto Update =
5320 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5321 IsXLHSInRHSPart ? OVEExpr : OVEX);
5322 if (Update.isInvalid())
5323 return true;
5324 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5325 Sema::AA_Casting);
5326 if (Update.isInvalid())
5327 return true;
5328 UpdateExpr = Update.get();
5329 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005330 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005331}
5332
Alexey Bataev0162e452014-07-22 10:10:35 +00005333StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5334 Stmt *AStmt,
5335 SourceLocation StartLoc,
5336 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005337 if (!AStmt)
5338 return StmtError();
5339
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005340 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005341 // 1.2.2 OpenMP Language Terminology
5342 // Structured block - An executable statement with a single entry at the
5343 // top and a single exit at the bottom.
5344 // The point of exit cannot be a branch out of the structured block.
5345 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005346 OpenMPClauseKind AtomicKind = OMPC_unknown;
5347 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005348 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005349 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005350 C->getClauseKind() == OMPC_update ||
5351 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005352 if (AtomicKind != OMPC_unknown) {
5353 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5354 << SourceRange(C->getLocStart(), C->getLocEnd());
5355 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5356 << getOpenMPClauseName(AtomicKind);
5357 } else {
5358 AtomicKind = C->getClauseKind();
5359 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005360 }
5361 }
5362 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005363
Alexey Bataev459dec02014-07-24 06:46:57 +00005364 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005365 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5366 Body = EWC->getSubExpr();
5367
Alexey Bataev62cec442014-11-18 10:14:22 +00005368 Expr *X = nullptr;
5369 Expr *V = nullptr;
5370 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005371 Expr *UE = nullptr;
5372 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005373 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005374 // OpenMP [2.12.6, atomic Construct]
5375 // In the next expressions:
5376 // * x and v (as applicable) are both l-value expressions with scalar type.
5377 // * During the execution of an atomic region, multiple syntactic
5378 // occurrences of x must designate the same storage location.
5379 // * Neither of v and expr (as applicable) may access the storage location
5380 // designated by x.
5381 // * Neither of x and expr (as applicable) may access the storage location
5382 // designated by v.
5383 // * expr is an expression with scalar type.
5384 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5385 // * binop, binop=, ++, and -- are not overloaded operators.
5386 // * The expression x binop expr must be numerically equivalent to x binop
5387 // (expr). This requirement is satisfied if the operators in expr have
5388 // precedence greater than binop, or by using parentheses around expr or
5389 // subexpressions of expr.
5390 // * The expression expr binop x must be numerically equivalent to (expr)
5391 // binop x. This requirement is satisfied if the operators in expr have
5392 // precedence equal to or greater than binop, or by using parentheses around
5393 // expr or subexpressions of expr.
5394 // * For forms that allow multiple occurrences of x, the number of times
5395 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005396 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005397 enum {
5398 NotAnExpression,
5399 NotAnAssignmentOp,
5400 NotAScalarType,
5401 NotAnLValue,
5402 NoError
5403 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005404 SourceLocation ErrorLoc, NoteLoc;
5405 SourceRange ErrorRange, NoteRange;
5406 // If clause is read:
5407 // v = x;
5408 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5409 auto AtomicBinOp =
5410 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5411 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5412 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5413 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5414 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5415 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5416 if (!X->isLValue() || !V->isLValue()) {
5417 auto NotLValueExpr = X->isLValue() ? V : X;
5418 ErrorFound = NotAnLValue;
5419 ErrorLoc = AtomicBinOp->getExprLoc();
5420 ErrorRange = AtomicBinOp->getSourceRange();
5421 NoteLoc = NotLValueExpr->getExprLoc();
5422 NoteRange = NotLValueExpr->getSourceRange();
5423 }
5424 } else if (!X->isInstantiationDependent() ||
5425 !V->isInstantiationDependent()) {
5426 auto NotScalarExpr =
5427 (X->isInstantiationDependent() || X->getType()->isScalarType())
5428 ? V
5429 : X;
5430 ErrorFound = NotAScalarType;
5431 ErrorLoc = AtomicBinOp->getExprLoc();
5432 ErrorRange = AtomicBinOp->getSourceRange();
5433 NoteLoc = NotScalarExpr->getExprLoc();
5434 NoteRange = NotScalarExpr->getSourceRange();
5435 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005436 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005437 ErrorFound = NotAnAssignmentOp;
5438 ErrorLoc = AtomicBody->getExprLoc();
5439 ErrorRange = AtomicBody->getSourceRange();
5440 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5441 : AtomicBody->getExprLoc();
5442 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5443 : AtomicBody->getSourceRange();
5444 }
5445 } else {
5446 ErrorFound = NotAnExpression;
5447 NoteLoc = ErrorLoc = Body->getLocStart();
5448 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005449 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005450 if (ErrorFound != NoError) {
5451 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5452 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005453 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5454 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005455 return StmtError();
5456 } else if (CurContext->isDependentContext())
5457 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005458 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005459 enum {
5460 NotAnExpression,
5461 NotAnAssignmentOp,
5462 NotAScalarType,
5463 NotAnLValue,
5464 NoError
5465 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005466 SourceLocation ErrorLoc, NoteLoc;
5467 SourceRange ErrorRange, NoteRange;
5468 // If clause is write:
5469 // x = expr;
5470 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5471 auto AtomicBinOp =
5472 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5473 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005474 X = AtomicBinOp->getLHS();
5475 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005476 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5477 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5478 if (!X->isLValue()) {
5479 ErrorFound = NotAnLValue;
5480 ErrorLoc = AtomicBinOp->getExprLoc();
5481 ErrorRange = AtomicBinOp->getSourceRange();
5482 NoteLoc = X->getExprLoc();
5483 NoteRange = X->getSourceRange();
5484 }
5485 } else if (!X->isInstantiationDependent() ||
5486 !E->isInstantiationDependent()) {
5487 auto NotScalarExpr =
5488 (X->isInstantiationDependent() || X->getType()->isScalarType())
5489 ? E
5490 : X;
5491 ErrorFound = NotAScalarType;
5492 ErrorLoc = AtomicBinOp->getExprLoc();
5493 ErrorRange = AtomicBinOp->getSourceRange();
5494 NoteLoc = NotScalarExpr->getExprLoc();
5495 NoteRange = NotScalarExpr->getSourceRange();
5496 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005497 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005498 ErrorFound = NotAnAssignmentOp;
5499 ErrorLoc = AtomicBody->getExprLoc();
5500 ErrorRange = AtomicBody->getSourceRange();
5501 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5502 : AtomicBody->getExprLoc();
5503 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5504 : AtomicBody->getSourceRange();
5505 }
5506 } else {
5507 ErrorFound = NotAnExpression;
5508 NoteLoc = ErrorLoc = Body->getLocStart();
5509 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005510 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005511 if (ErrorFound != NoError) {
5512 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5513 << ErrorRange;
5514 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5515 << NoteRange;
5516 return StmtError();
5517 } else if (CurContext->isDependentContext())
5518 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005519 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005520 // If clause is update:
5521 // x++;
5522 // x--;
5523 // ++x;
5524 // --x;
5525 // x binop= expr;
5526 // x = x binop expr;
5527 // x = expr binop x;
5528 OpenMPAtomicUpdateChecker Checker(*this);
5529 if (Checker.checkStatement(
5530 Body, (AtomicKind == OMPC_update)
5531 ? diag::err_omp_atomic_update_not_expression_statement
5532 : diag::err_omp_atomic_not_expression_statement,
5533 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005534 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005535 if (!CurContext->isDependentContext()) {
5536 E = Checker.getExpr();
5537 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005538 UE = Checker.getUpdateExpr();
5539 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005540 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005541 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005542 enum {
5543 NotAnAssignmentOp,
5544 NotACompoundStatement,
5545 NotTwoSubstatements,
5546 NotASpecificExpression,
5547 NoError
5548 } ErrorFound = NoError;
5549 SourceLocation ErrorLoc, NoteLoc;
5550 SourceRange ErrorRange, NoteRange;
5551 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5552 // If clause is a capture:
5553 // v = x++;
5554 // v = x--;
5555 // v = ++x;
5556 // v = --x;
5557 // v = x binop= expr;
5558 // v = x = x binop expr;
5559 // v = x = expr binop x;
5560 auto *AtomicBinOp =
5561 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5562 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5563 V = AtomicBinOp->getLHS();
5564 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5565 OpenMPAtomicUpdateChecker Checker(*this);
5566 if (Checker.checkStatement(
5567 Body, diag::err_omp_atomic_capture_not_expression_statement,
5568 diag::note_omp_atomic_update))
5569 return StmtError();
5570 E = Checker.getExpr();
5571 X = Checker.getX();
5572 UE = Checker.getUpdateExpr();
5573 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5574 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005575 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005576 ErrorLoc = AtomicBody->getExprLoc();
5577 ErrorRange = AtomicBody->getSourceRange();
5578 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5579 : AtomicBody->getExprLoc();
5580 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5581 : AtomicBody->getSourceRange();
5582 ErrorFound = NotAnAssignmentOp;
5583 }
5584 if (ErrorFound != NoError) {
5585 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5586 << ErrorRange;
5587 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5588 return StmtError();
5589 } else if (CurContext->isDependentContext()) {
5590 UE = V = E = X = nullptr;
5591 }
5592 } else {
5593 // If clause is a capture:
5594 // { v = x; x = expr; }
5595 // { v = x; x++; }
5596 // { v = x; x--; }
5597 // { v = x; ++x; }
5598 // { v = x; --x; }
5599 // { v = x; x binop= expr; }
5600 // { v = x; x = x binop expr; }
5601 // { v = x; x = expr binop x; }
5602 // { x++; v = x; }
5603 // { x--; v = x; }
5604 // { ++x; v = x; }
5605 // { --x; v = x; }
5606 // { x binop= expr; v = x; }
5607 // { x = x binop expr; v = x; }
5608 // { x = expr binop x; v = x; }
5609 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5610 // Check that this is { expr1; expr2; }
5611 if (CS->size() == 2) {
5612 auto *First = CS->body_front();
5613 auto *Second = CS->body_back();
5614 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5615 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5616 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5617 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5618 // Need to find what subexpression is 'v' and what is 'x'.
5619 OpenMPAtomicUpdateChecker Checker(*this);
5620 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5621 BinaryOperator *BinOp = nullptr;
5622 if (IsUpdateExprFound) {
5623 BinOp = dyn_cast<BinaryOperator>(First);
5624 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5625 }
5626 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5627 // { v = x; x++; }
5628 // { v = x; x--; }
5629 // { v = x; ++x; }
5630 // { v = x; --x; }
5631 // { v = x; x binop= expr; }
5632 // { v = x; x = x binop expr; }
5633 // { v = x; x = expr binop x; }
5634 // Check that the first expression has form v = x.
5635 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5636 llvm::FoldingSetNodeID XId, PossibleXId;
5637 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5638 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5639 IsUpdateExprFound = XId == PossibleXId;
5640 if (IsUpdateExprFound) {
5641 V = BinOp->getLHS();
5642 X = Checker.getX();
5643 E = Checker.getExpr();
5644 UE = Checker.getUpdateExpr();
5645 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005646 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005647 }
5648 }
5649 if (!IsUpdateExprFound) {
5650 IsUpdateExprFound = !Checker.checkStatement(First);
5651 BinOp = nullptr;
5652 if (IsUpdateExprFound) {
5653 BinOp = dyn_cast<BinaryOperator>(Second);
5654 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5655 }
5656 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5657 // { x++; v = x; }
5658 // { x--; v = x; }
5659 // { ++x; v = x; }
5660 // { --x; v = x; }
5661 // { x binop= expr; v = x; }
5662 // { x = x binop expr; v = x; }
5663 // { x = expr binop x; v = x; }
5664 // Check that the second expression has form v = x.
5665 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5666 llvm::FoldingSetNodeID XId, PossibleXId;
5667 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5668 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5669 IsUpdateExprFound = XId == PossibleXId;
5670 if (IsUpdateExprFound) {
5671 V = BinOp->getLHS();
5672 X = Checker.getX();
5673 E = Checker.getExpr();
5674 UE = Checker.getUpdateExpr();
5675 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005676 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005677 }
5678 }
5679 }
5680 if (!IsUpdateExprFound) {
5681 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005682 auto *FirstExpr = dyn_cast<Expr>(First);
5683 auto *SecondExpr = dyn_cast<Expr>(Second);
5684 if (!FirstExpr || !SecondExpr ||
5685 !(FirstExpr->isInstantiationDependent() ||
5686 SecondExpr->isInstantiationDependent())) {
5687 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5688 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005689 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005690 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5691 : First->getLocStart();
5692 NoteRange = ErrorRange = FirstBinOp
5693 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005694 : SourceRange(ErrorLoc, ErrorLoc);
5695 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005696 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5697 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5698 ErrorFound = NotAnAssignmentOp;
5699 NoteLoc = ErrorLoc = SecondBinOp
5700 ? SecondBinOp->getOperatorLoc()
5701 : Second->getLocStart();
5702 NoteRange = ErrorRange =
5703 SecondBinOp ? SecondBinOp->getSourceRange()
5704 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005705 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005706 auto *PossibleXRHSInFirst =
5707 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5708 auto *PossibleXLHSInSecond =
5709 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5710 llvm::FoldingSetNodeID X1Id, X2Id;
5711 PossibleXRHSInFirst->Profile(X1Id, Context,
5712 /*Canonical=*/true);
5713 PossibleXLHSInSecond->Profile(X2Id, Context,
5714 /*Canonical=*/true);
5715 IsUpdateExprFound = X1Id == X2Id;
5716 if (IsUpdateExprFound) {
5717 V = FirstBinOp->getLHS();
5718 X = SecondBinOp->getLHS();
5719 E = SecondBinOp->getRHS();
5720 UE = nullptr;
5721 IsXLHSInRHSPart = false;
5722 IsPostfixUpdate = true;
5723 } else {
5724 ErrorFound = NotASpecificExpression;
5725 ErrorLoc = FirstBinOp->getExprLoc();
5726 ErrorRange = FirstBinOp->getSourceRange();
5727 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5728 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5729 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005730 }
5731 }
5732 }
5733 }
5734 } else {
5735 NoteLoc = ErrorLoc = Body->getLocStart();
5736 NoteRange = ErrorRange =
5737 SourceRange(Body->getLocStart(), Body->getLocStart());
5738 ErrorFound = NotTwoSubstatements;
5739 }
5740 } else {
5741 NoteLoc = ErrorLoc = Body->getLocStart();
5742 NoteRange = ErrorRange =
5743 SourceRange(Body->getLocStart(), Body->getLocStart());
5744 ErrorFound = NotACompoundStatement;
5745 }
5746 if (ErrorFound != NoError) {
5747 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5748 << ErrorRange;
5749 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5750 return StmtError();
5751 } else if (CurContext->isDependentContext()) {
5752 UE = V = E = X = nullptr;
5753 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005754 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005755 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005756
5757 getCurFunction()->setHasBranchProtectedScope();
5758
Alexey Bataev62cec442014-11-18 10:14:22 +00005759 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005760 X, V, E, UE, IsXLHSInRHSPart,
5761 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005762}
5763
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005764StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5765 Stmt *AStmt,
5766 SourceLocation StartLoc,
5767 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005768 if (!AStmt)
5769 return StmtError();
5770
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005771 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5772 // 1.2.2 OpenMP Language Terminology
5773 // Structured block - An executable statement with a single entry at the
5774 // top and a single exit at the bottom.
5775 // The point of exit cannot be a branch out of the structured block.
5776 // longjmp() and throw() must not violate the entry/exit criteria.
5777 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005778
Alexey Bataev13314bf2014-10-09 04:18:56 +00005779 // OpenMP [2.16, Nesting of Regions]
5780 // If specified, a teams construct must be contained within a target
5781 // construct. That target construct must contain no statements or directives
5782 // outside of the teams construct.
5783 if (DSAStack->hasInnerTeamsRegion()) {
5784 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5785 bool OMPTeamsFound = true;
5786 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5787 auto I = CS->body_begin();
5788 while (I != CS->body_end()) {
5789 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5790 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5791 OMPTeamsFound = false;
5792 break;
5793 }
5794 ++I;
5795 }
5796 assert(I != CS->body_end() && "Not found statement");
5797 S = *I;
5798 }
5799 if (!OMPTeamsFound) {
5800 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5801 Diag(DSAStack->getInnerTeamsRegionLoc(),
5802 diag::note_omp_nested_teams_construct_here);
5803 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5804 << isa<OMPExecutableDirective>(S);
5805 return StmtError();
5806 }
5807 }
5808
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005809 getCurFunction()->setHasBranchProtectedScope();
5810
5811 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5812}
5813
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005814StmtResult
5815Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5816 Stmt *AStmt, SourceLocation StartLoc,
5817 SourceLocation EndLoc) {
5818 if (!AStmt)
5819 return StmtError();
5820
5821 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5822 // 1.2.2 OpenMP Language Terminology
5823 // Structured block - An executable statement with a single entry at the
5824 // top and a single exit at the bottom.
5825 // The point of exit cannot be a branch out of the structured block.
5826 // longjmp() and throw() must not violate the entry/exit criteria.
5827 CS->getCapturedDecl()->setNothrow();
5828
5829 getCurFunction()->setHasBranchProtectedScope();
5830
5831 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5832 AStmt);
5833}
5834
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005835StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5836 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5837 SourceLocation EndLoc,
5838 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5839 if (!AStmt)
5840 return StmtError();
5841
5842 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5843 // 1.2.2 OpenMP Language Terminology
5844 // Structured block - An executable statement with a single entry at the
5845 // top and a single exit at the bottom.
5846 // The point of exit cannot be a branch out of the structured block.
5847 // longjmp() and throw() must not violate the entry/exit criteria.
5848 CS->getCapturedDecl()->setNothrow();
5849
5850 OMPLoopDirective::HelperExprs B;
5851 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5852 // define the nested loops number.
5853 unsigned NestedLoopCount =
5854 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5855 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5856 VarsWithImplicitDSA, B);
5857 if (NestedLoopCount == 0)
5858 return StmtError();
5859
5860 assert((CurContext->isDependentContext() || B.builtAll()) &&
5861 "omp target parallel for loop exprs were not built");
5862
5863 if (!CurContext->isDependentContext()) {
5864 // Finalize the clauses that need pre-built expressions for CodeGen.
5865 for (auto C : Clauses) {
5866 if (auto LC = dyn_cast<OMPLinearClause>(C))
5867 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5868 B.NumIterations, *this, CurScope))
5869 return StmtError();
5870 }
5871 }
5872
5873 getCurFunction()->setHasBranchProtectedScope();
5874 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5875 NestedLoopCount, Clauses, AStmt,
5876 B, DSAStack->isCancelRegion());
5877}
5878
Samuel Antaodf67fc42016-01-19 19:15:56 +00005879/// \brief Check for existence of a map clause in the list of clauses.
5880static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5881 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5882 I != E; ++I) {
5883 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5884 return true;
5885 }
5886 }
5887
5888 return false;
5889}
5890
Michael Wong65f367f2015-07-21 13:44:28 +00005891StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5892 Stmt *AStmt,
5893 SourceLocation StartLoc,
5894 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005895 if (!AStmt)
5896 return StmtError();
5897
5898 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5899
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005900 // OpenMP [2.10.1, Restrictions, p. 97]
5901 // At least one map clause must appear on the directive.
5902 if (!HasMapClause(Clauses)) {
5903 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5904 getOpenMPDirectiveName(OMPD_target_data);
5905 return StmtError();
5906 }
5907
Michael Wong65f367f2015-07-21 13:44:28 +00005908 getCurFunction()->setHasBranchProtectedScope();
5909
5910 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5911 AStmt);
5912}
5913
Samuel Antaodf67fc42016-01-19 19:15:56 +00005914StmtResult
5915Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5916 SourceLocation StartLoc,
5917 SourceLocation EndLoc) {
5918 // OpenMP [2.10.2, Restrictions, p. 99]
5919 // At least one map clause must appear on the directive.
5920 if (!HasMapClause(Clauses)) {
5921 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5922 << getOpenMPDirectiveName(OMPD_target_enter_data);
5923 return StmtError();
5924 }
5925
5926 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5927 Clauses);
5928}
5929
Samuel Antao72590762016-01-19 20:04:50 +00005930StmtResult
5931Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5932 SourceLocation StartLoc,
5933 SourceLocation EndLoc) {
5934 // OpenMP [2.10.3, Restrictions, p. 102]
5935 // At least one map clause must appear on the directive.
5936 if (!HasMapClause(Clauses)) {
5937 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5938 << getOpenMPDirectiveName(OMPD_target_exit_data);
5939 return StmtError();
5940 }
5941
5942 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5943}
5944
Alexey Bataev13314bf2014-10-09 04:18:56 +00005945StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5946 Stmt *AStmt, SourceLocation StartLoc,
5947 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005948 if (!AStmt)
5949 return StmtError();
5950
Alexey Bataev13314bf2014-10-09 04:18:56 +00005951 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5952 // 1.2.2 OpenMP Language Terminology
5953 // Structured block - An executable statement with a single entry at the
5954 // top and a single exit at the bottom.
5955 // The point of exit cannot be a branch out of the structured block.
5956 // longjmp() and throw() must not violate the entry/exit criteria.
5957 CS->getCapturedDecl()->setNothrow();
5958
5959 getCurFunction()->setHasBranchProtectedScope();
5960
5961 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5962}
5963
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005964StmtResult
5965Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5966 SourceLocation EndLoc,
5967 OpenMPDirectiveKind CancelRegion) {
5968 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5969 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5970 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5971 << getOpenMPDirectiveName(CancelRegion);
5972 return StmtError();
5973 }
5974 if (DSAStack->isParentNowaitRegion()) {
5975 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5976 return StmtError();
5977 }
5978 if (DSAStack->isParentOrderedRegion()) {
5979 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5980 return StmtError();
5981 }
5982 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5983 CancelRegion);
5984}
5985
Alexey Bataev87933c72015-09-18 08:07:34 +00005986StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5987 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005988 SourceLocation EndLoc,
5989 OpenMPDirectiveKind CancelRegion) {
5990 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5991 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5992 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5993 << getOpenMPDirectiveName(CancelRegion);
5994 return StmtError();
5995 }
5996 if (DSAStack->isParentNowaitRegion()) {
5997 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5998 return StmtError();
5999 }
6000 if (DSAStack->isParentOrderedRegion()) {
6001 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6002 return StmtError();
6003 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006004 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006005 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6006 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006007}
6008
Alexey Bataev382967a2015-12-08 12:06:20 +00006009static bool checkGrainsizeNumTasksClauses(Sema &S,
6010 ArrayRef<OMPClause *> Clauses) {
6011 OMPClause *PrevClause = nullptr;
6012 bool ErrorFound = false;
6013 for (auto *C : Clauses) {
6014 if (C->getClauseKind() == OMPC_grainsize ||
6015 C->getClauseKind() == OMPC_num_tasks) {
6016 if (!PrevClause)
6017 PrevClause = C;
6018 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6019 S.Diag(C->getLocStart(),
6020 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6021 << getOpenMPClauseName(C->getClauseKind())
6022 << getOpenMPClauseName(PrevClause->getClauseKind());
6023 S.Diag(PrevClause->getLocStart(),
6024 diag::note_omp_previous_grainsize_num_tasks)
6025 << getOpenMPClauseName(PrevClause->getClauseKind());
6026 ErrorFound = true;
6027 }
6028 }
6029 }
6030 return ErrorFound;
6031}
6032
Alexey Bataev49f6e782015-12-01 04:18:41 +00006033StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6034 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6035 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006036 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006037 if (!AStmt)
6038 return StmtError();
6039
6040 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6041 OMPLoopDirective::HelperExprs B;
6042 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6043 // define the nested loops number.
6044 unsigned NestedLoopCount =
6045 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006046 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006047 VarsWithImplicitDSA, B);
6048 if (NestedLoopCount == 0)
6049 return StmtError();
6050
6051 assert((CurContext->isDependentContext() || B.builtAll()) &&
6052 "omp for loop exprs were not built");
6053
Alexey Bataev382967a2015-12-08 12:06:20 +00006054 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6055 // The grainsize clause and num_tasks clause are mutually exclusive and may
6056 // not appear on the same taskloop directive.
6057 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6058 return StmtError();
6059
Alexey Bataev49f6e782015-12-01 04:18:41 +00006060 getCurFunction()->setHasBranchProtectedScope();
6061 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6062 NestedLoopCount, Clauses, AStmt, B);
6063}
6064
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006065StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6066 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6067 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006068 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006069 if (!AStmt)
6070 return StmtError();
6071
6072 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6073 OMPLoopDirective::HelperExprs B;
6074 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6075 // define the nested loops number.
6076 unsigned NestedLoopCount =
6077 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6078 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6079 VarsWithImplicitDSA, B);
6080 if (NestedLoopCount == 0)
6081 return StmtError();
6082
6083 assert((CurContext->isDependentContext() || B.builtAll()) &&
6084 "omp for loop exprs were not built");
6085
Alexey Bataev382967a2015-12-08 12:06:20 +00006086 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6087 // The grainsize clause and num_tasks clause are mutually exclusive and may
6088 // not appear on the same taskloop directive.
6089 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6090 return StmtError();
6091
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006092 getCurFunction()->setHasBranchProtectedScope();
6093 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6094 NestedLoopCount, Clauses, AStmt, B);
6095}
6096
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006097StmtResult Sema::ActOnOpenMPDistributeDirective(
6098 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6099 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006100 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006101 if (!AStmt)
6102 return StmtError();
6103
6104 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6105 OMPLoopDirective::HelperExprs B;
6106 // In presence of clause 'collapse' with number of loops, it will
6107 // define the nested loops number.
6108 unsigned NestedLoopCount =
6109 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6110 nullptr /*ordered not a clause on distribute*/, AStmt,
6111 *this, *DSAStack, VarsWithImplicitDSA, B);
6112 if (NestedLoopCount == 0)
6113 return StmtError();
6114
6115 assert((CurContext->isDependentContext() || B.builtAll()) &&
6116 "omp for loop exprs were not built");
6117
6118 getCurFunction()->setHasBranchProtectedScope();
6119 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6120 NestedLoopCount, Clauses, AStmt, B);
6121}
6122
Alexey Bataeved09d242014-05-28 05:53:51 +00006123OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006124 SourceLocation StartLoc,
6125 SourceLocation LParenLoc,
6126 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006127 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006128 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006129 case OMPC_final:
6130 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6131 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006132 case OMPC_num_threads:
6133 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6134 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006135 case OMPC_safelen:
6136 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6137 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006138 case OMPC_simdlen:
6139 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6140 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006141 case OMPC_collapse:
6142 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6143 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006144 case OMPC_ordered:
6145 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6146 break;
Michael Wonge710d542015-08-07 16:16:36 +00006147 case OMPC_device:
6148 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6149 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006150 case OMPC_num_teams:
6151 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6152 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006153 case OMPC_thread_limit:
6154 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6155 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006156 case OMPC_priority:
6157 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6158 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006159 case OMPC_grainsize:
6160 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6161 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006162 case OMPC_num_tasks:
6163 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6164 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006165 case OMPC_hint:
6166 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6167 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006168 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006169 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006170 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006171 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006172 case OMPC_private:
6173 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006174 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006175 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006176 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006177 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006178 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006179 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006180 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006181 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006182 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006183 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006184 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006185 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006186 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006187 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006188 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006189 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006190 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006191 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006192 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006193 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006194 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006195 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006196 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006197 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006198 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006199 llvm_unreachable("Clause is not allowed.");
6200 }
6201 return Res;
6202}
6203
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006204OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6205 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006206 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006207 SourceLocation NameModifierLoc,
6208 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006209 SourceLocation EndLoc) {
6210 Expr *ValExpr = Condition;
6211 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6212 !Condition->isInstantiationDependent() &&
6213 !Condition->containsUnexpandedParameterPack()) {
6214 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006215 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006216 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006217 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006218
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006219 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006220 }
6221
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006222 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6223 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006224}
6225
Alexey Bataev3778b602014-07-17 07:32:53 +00006226OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6227 SourceLocation StartLoc,
6228 SourceLocation LParenLoc,
6229 SourceLocation EndLoc) {
6230 Expr *ValExpr = Condition;
6231 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6232 !Condition->isInstantiationDependent() &&
6233 !Condition->containsUnexpandedParameterPack()) {
6234 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6235 Condition->getExprLoc(), Condition);
6236 if (Val.isInvalid())
6237 return nullptr;
6238
6239 ValExpr = Val.get();
6240 }
6241
6242 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6243}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006244ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6245 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006246 if (!Op)
6247 return ExprError();
6248
6249 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6250 public:
6251 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006252 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006253 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6254 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006255 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6256 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006257 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6258 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006259 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6260 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006261 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6262 QualType T,
6263 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006264 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6265 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006266 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6267 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006268 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006269 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006270 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006271 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6272 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006273 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6274 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006275 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6276 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006277 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006278 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006279 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006280 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6281 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006282 llvm_unreachable("conversion functions are permitted");
6283 }
6284 } ConvertDiagnoser;
6285 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6286}
6287
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006288static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006289 OpenMPClauseKind CKind,
6290 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006291 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6292 !ValExpr->isInstantiationDependent()) {
6293 SourceLocation Loc = ValExpr->getExprLoc();
6294 ExprResult Value =
6295 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6296 if (Value.isInvalid())
6297 return false;
6298
6299 ValExpr = Value.get();
6300 // The expression must evaluate to a non-negative integer value.
6301 llvm::APSInt Result;
6302 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006303 Result.isSigned() &&
6304 !((!StrictlyPositive && Result.isNonNegative()) ||
6305 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006306 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006307 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6308 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006309 return false;
6310 }
6311 }
6312 return true;
6313}
6314
Alexey Bataev568a8332014-03-06 06:15:19 +00006315OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6316 SourceLocation StartLoc,
6317 SourceLocation LParenLoc,
6318 SourceLocation EndLoc) {
6319 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006320
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006321 // OpenMP [2.5, Restrictions]
6322 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006323 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6324 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006325 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006326
Alexey Bataeved09d242014-05-28 05:53:51 +00006327 return new (Context)
6328 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006329}
6330
Alexey Bataev62c87d22014-03-21 04:51:18 +00006331ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006332 OpenMPClauseKind CKind,
6333 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006334 if (!E)
6335 return ExprError();
6336 if (E->isValueDependent() || E->isTypeDependent() ||
6337 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006338 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006339 llvm::APSInt Result;
6340 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6341 if (ICE.isInvalid())
6342 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006343 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6344 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006345 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006346 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6347 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006348 return ExprError();
6349 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006350 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6351 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6352 << E->getSourceRange();
6353 return ExprError();
6354 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006355 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6356 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006357 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006358 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006359 return ICE;
6360}
6361
6362OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6363 SourceLocation LParenLoc,
6364 SourceLocation EndLoc) {
6365 // OpenMP [2.8.1, simd construct, Description]
6366 // The parameter of the safelen clause must be a constant
6367 // positive integer expression.
6368 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6369 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006370 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006371 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006372 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006373}
6374
Alexey Bataev66b15b52015-08-21 11:14:16 +00006375OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6376 SourceLocation LParenLoc,
6377 SourceLocation EndLoc) {
6378 // OpenMP [2.8.1, simd construct, Description]
6379 // The parameter of the simdlen clause must be a constant
6380 // positive integer expression.
6381 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6382 if (Simdlen.isInvalid())
6383 return nullptr;
6384 return new (Context)
6385 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6386}
6387
Alexander Musman64d33f12014-06-04 07:53:32 +00006388OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6389 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006390 SourceLocation LParenLoc,
6391 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006392 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006393 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006394 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006395 // The parameter of the collapse clause must be a constant
6396 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006397 ExprResult NumForLoopsResult =
6398 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6399 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006400 return nullptr;
6401 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006402 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006403}
6404
Alexey Bataev10e775f2015-07-30 11:36:16 +00006405OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6406 SourceLocation EndLoc,
6407 SourceLocation LParenLoc,
6408 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006409 // OpenMP [2.7.1, loop construct, Description]
6410 // OpenMP [2.8.1, simd construct, Description]
6411 // OpenMP [2.9.6, distribute construct, Description]
6412 // The parameter of the ordered clause must be a constant
6413 // positive integer expression if any.
6414 if (NumForLoops && LParenLoc.isValid()) {
6415 ExprResult NumForLoopsResult =
6416 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6417 if (NumForLoopsResult.isInvalid())
6418 return nullptr;
6419 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006420 } else
6421 NumForLoops = nullptr;
6422 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006423 return new (Context)
6424 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6425}
6426
Alexey Bataeved09d242014-05-28 05:53:51 +00006427OMPClause *Sema::ActOnOpenMPSimpleClause(
6428 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6429 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006430 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006431 switch (Kind) {
6432 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006433 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006434 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6435 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006436 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006437 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006438 Res = ActOnOpenMPProcBindClause(
6439 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6440 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006441 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006442 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006443 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006444 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006445 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006446 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006447 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006448 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006449 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006450 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006451 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006452 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006453 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006454 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006455 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006456 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006457 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006458 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006459 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006460 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006461 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006462 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006463 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006464 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006465 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006466 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006467 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006468 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006469 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006470 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006471 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006472 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006473 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006474 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006475 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006476 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006477 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006478 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006479 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006480 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006481 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006482 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006483 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006484 llvm_unreachable("Clause is not allowed.");
6485 }
6486 return Res;
6487}
6488
Alexey Bataev6402bca2015-12-28 07:25:51 +00006489static std::string
6490getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6491 ArrayRef<unsigned> Exclude = llvm::None) {
6492 std::string Values;
6493 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6494 unsigned Skipped = Exclude.size();
6495 auto S = Exclude.begin(), E = Exclude.end();
6496 for (unsigned i = First; i < Last; ++i) {
6497 if (std::find(S, E, i) != E) {
6498 --Skipped;
6499 continue;
6500 }
6501 Values += "'";
6502 Values += getOpenMPSimpleClauseTypeName(K, i);
6503 Values += "'";
6504 if (i == Bound - Skipped)
6505 Values += " or ";
6506 else if (i != Bound + 1 - Skipped)
6507 Values += ", ";
6508 }
6509 return Values;
6510}
6511
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006512OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6513 SourceLocation KindKwLoc,
6514 SourceLocation StartLoc,
6515 SourceLocation LParenLoc,
6516 SourceLocation EndLoc) {
6517 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006518 static_assert(OMPC_DEFAULT_unknown > 0,
6519 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006520 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006521 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6522 /*Last=*/OMPC_DEFAULT_unknown)
6523 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006524 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006525 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006526 switch (Kind) {
6527 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006528 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006529 break;
6530 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006531 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006532 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006533 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006534 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006535 break;
6536 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006537 return new (Context)
6538 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006539}
6540
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006541OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6542 SourceLocation KindKwLoc,
6543 SourceLocation StartLoc,
6544 SourceLocation LParenLoc,
6545 SourceLocation EndLoc) {
6546 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006547 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006548 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6549 /*Last=*/OMPC_PROC_BIND_unknown)
6550 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006551 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006552 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006553 return new (Context)
6554 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006555}
6556
Alexey Bataev56dafe82014-06-20 07:16:17 +00006557OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006558 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006559 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006560 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006561 SourceLocation EndLoc) {
6562 OMPClause *Res = nullptr;
6563 switch (Kind) {
6564 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006565 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6566 assert(Argument.size() == NumberOfElements &&
6567 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006568 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006569 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6570 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6571 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6572 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6573 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006574 break;
6575 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006576 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6577 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6578 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6579 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006580 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006581 case OMPC_dist_schedule:
6582 Res = ActOnOpenMPDistScheduleClause(
6583 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6584 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6585 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006586 case OMPC_defaultmap:
6587 enum { Modifier, DefaultmapKind };
6588 Res = ActOnOpenMPDefaultmapClause(
6589 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6590 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6591 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6592 ArgumentLoc[DefaultmapKind], EndLoc);
6593 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006594 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006595 case OMPC_num_threads:
6596 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006597 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006598 case OMPC_collapse:
6599 case OMPC_default:
6600 case OMPC_proc_bind:
6601 case OMPC_private:
6602 case OMPC_firstprivate:
6603 case OMPC_lastprivate:
6604 case OMPC_shared:
6605 case OMPC_reduction:
6606 case OMPC_linear:
6607 case OMPC_aligned:
6608 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006609 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006610 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006611 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006612 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006613 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006614 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006615 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006616 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006617 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006618 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006619 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006620 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006621 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006622 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006623 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006624 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006625 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006626 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006627 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006628 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006629 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006630 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006631 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006632 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006633 case OMPC_unknown:
6634 llvm_unreachable("Clause is not allowed.");
6635 }
6636 return Res;
6637}
6638
Alexey Bataev6402bca2015-12-28 07:25:51 +00006639static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6640 OpenMPScheduleClauseModifier M2,
6641 SourceLocation M1Loc, SourceLocation M2Loc) {
6642 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6643 SmallVector<unsigned, 2> Excluded;
6644 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6645 Excluded.push_back(M2);
6646 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6647 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6648 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6649 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6650 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6651 << getListOfPossibleValues(OMPC_schedule,
6652 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6653 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6654 Excluded)
6655 << getOpenMPClauseName(OMPC_schedule);
6656 return true;
6657 }
6658 return false;
6659}
6660
Alexey Bataev56dafe82014-06-20 07:16:17 +00006661OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006662 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006663 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006664 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6665 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6666 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6667 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6668 return nullptr;
6669 // OpenMP, 2.7.1, Loop Construct, Restrictions
6670 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6671 // but not both.
6672 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6673 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6674 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6675 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6676 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6677 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6678 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6679 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6680 return nullptr;
6681 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006682 if (Kind == OMPC_SCHEDULE_unknown) {
6683 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006684 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6685 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6686 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6687 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6688 Exclude);
6689 } else {
6690 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6691 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006692 }
6693 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6694 << Values << getOpenMPClauseName(OMPC_schedule);
6695 return nullptr;
6696 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006697 // OpenMP, 2.7.1, Loop Construct, Restrictions
6698 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6699 // schedule(guided).
6700 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6701 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6702 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6703 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6704 diag::err_omp_schedule_nonmonotonic_static);
6705 return nullptr;
6706 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006707 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006708 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006709 if (ChunkSize) {
6710 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6711 !ChunkSize->isInstantiationDependent() &&
6712 !ChunkSize->containsUnexpandedParameterPack()) {
6713 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6714 ExprResult Val =
6715 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6716 if (Val.isInvalid())
6717 return nullptr;
6718
6719 ValExpr = Val.get();
6720
6721 // OpenMP [2.7.1, Restrictions]
6722 // chunk_size must be a loop invariant integer expression with a positive
6723 // value.
6724 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006725 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6726 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6727 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006728 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006729 return nullptr;
6730 }
6731 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6732 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6733 ChunkSize->getType(), ".chunk.");
6734 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6735 ChunkSize->getExprLoc(),
6736 /*RefersToCapture=*/true);
6737 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006738 }
6739 }
6740 }
6741
Alexey Bataev6402bca2015-12-28 07:25:51 +00006742 return new (Context)
6743 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6744 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006745}
6746
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006747OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6748 SourceLocation StartLoc,
6749 SourceLocation EndLoc) {
6750 OMPClause *Res = nullptr;
6751 switch (Kind) {
6752 case OMPC_ordered:
6753 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6754 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006755 case OMPC_nowait:
6756 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6757 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006758 case OMPC_untied:
6759 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6760 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006761 case OMPC_mergeable:
6762 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6763 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006764 case OMPC_read:
6765 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6766 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006767 case OMPC_write:
6768 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6769 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006770 case OMPC_update:
6771 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6772 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006773 case OMPC_capture:
6774 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6775 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006776 case OMPC_seq_cst:
6777 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6778 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006779 case OMPC_threads:
6780 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6781 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006782 case OMPC_simd:
6783 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6784 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006785 case OMPC_nogroup:
6786 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6787 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006788 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006789 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006790 case OMPC_num_threads:
6791 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006792 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006793 case OMPC_collapse:
6794 case OMPC_schedule:
6795 case OMPC_private:
6796 case OMPC_firstprivate:
6797 case OMPC_lastprivate:
6798 case OMPC_shared:
6799 case OMPC_reduction:
6800 case OMPC_linear:
6801 case OMPC_aligned:
6802 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006803 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006804 case OMPC_default:
6805 case OMPC_proc_bind:
6806 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006807 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006808 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006809 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006810 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006811 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006812 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006813 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006814 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006815 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006816 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006817 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006818 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006819 case OMPC_unknown:
6820 llvm_unreachable("Clause is not allowed.");
6821 }
6822 return Res;
6823}
6824
Alexey Bataev236070f2014-06-20 11:19:47 +00006825OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6826 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006827 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006828 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6829}
6830
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006831OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6832 SourceLocation EndLoc) {
6833 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6834}
6835
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006836OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6837 SourceLocation EndLoc) {
6838 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6839}
6840
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006841OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6842 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006843 return new (Context) OMPReadClause(StartLoc, EndLoc);
6844}
6845
Alexey Bataevdea47612014-07-23 07:46:59 +00006846OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6847 SourceLocation EndLoc) {
6848 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6849}
6850
Alexey Bataev67a4f222014-07-23 10:25:33 +00006851OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6852 SourceLocation EndLoc) {
6853 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6854}
6855
Alexey Bataev459dec02014-07-24 06:46:57 +00006856OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6857 SourceLocation EndLoc) {
6858 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6859}
6860
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006861OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6862 SourceLocation EndLoc) {
6863 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6864}
6865
Alexey Bataev346265e2015-09-25 10:37:12 +00006866OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6867 SourceLocation EndLoc) {
6868 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6869}
6870
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006871OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6872 SourceLocation EndLoc) {
6873 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6874}
6875
Alexey Bataevb825de12015-12-07 10:51:44 +00006876OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6877 SourceLocation EndLoc) {
6878 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6879}
6880
Alexey Bataevc5e02582014-06-16 07:08:35 +00006881OMPClause *Sema::ActOnOpenMPVarListClause(
6882 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6883 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6884 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006885 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006886 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6887 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6888 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006889 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006890 switch (Kind) {
6891 case OMPC_private:
6892 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6893 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006894 case OMPC_firstprivate:
6895 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6896 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006897 case OMPC_lastprivate:
6898 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6899 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006900 case OMPC_shared:
6901 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6902 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006903 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006904 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6905 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006906 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006907 case OMPC_linear:
6908 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006909 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006910 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006911 case OMPC_aligned:
6912 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6913 ColonLoc, EndLoc);
6914 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006915 case OMPC_copyin:
6916 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6917 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006918 case OMPC_copyprivate:
6919 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6920 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006921 case OMPC_flush:
6922 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6923 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006924 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006925 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6926 StartLoc, LParenLoc, EndLoc);
6927 break;
6928 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006929 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6930 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6931 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006932 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006933 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006934 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006935 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006936 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006937 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006938 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006939 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006940 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006941 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006942 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006943 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006944 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006945 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006946 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006947 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006948 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006949 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006950 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006951 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006952 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006953 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006954 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006955 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006956 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006957 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006958 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006959 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006960 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006961 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006962 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006963 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006964 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006965 llvm_unreachable("Clause is not allowed.");
6966 }
6967 return Res;
6968}
6969
Alexey Bataev90c228f2016-02-08 09:29:13 +00006970static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
6971 Expr *CaptureExpr) {
6972 ASTContext &C = S.getASTContext();
6973 Expr *Init = CaptureExpr->IgnoreImpCasts();
6974 QualType Ty = Init->getType();
6975 if (CaptureExpr->getObjectKind() == OK_Ordinary) {
6976 if (S.getLangOpts().CPlusPlus)
6977 Ty = C.getLValueReferenceType(Ty);
6978 else {
6979 Ty = C.getPointerType(Ty);
6980 ExprResult Res =
6981 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
6982 if (!Res.isUsable())
6983 return nullptr;
6984 Init = Res.get();
6985 }
6986 }
6987 auto *CFD = OMPCapturedFieldDecl::Create(C, S.CurContext, Id, Ty);
6988 S.CurContext->addHiddenDecl(CFD);
6989 S.AddInitializerToDecl(CFD, Init, /*DirectInit=*/false,
6990 /*TypeMayContainAuto=*/true);
6991 return buildDeclRefExpr(S, CFD, Ty.getNonReferenceType(), SourceLocation());
6992}
6993
6994ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
6995 ExprObjectKind OK) {
6996 SourceLocation Loc = Capture->getInit()->getExprLoc();
6997 ExprResult Res = BuildDeclRefExpr(
6998 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
6999 if (!Res.isUsable())
7000 return ExprError();
7001 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7002 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7003 if (!Res.isUsable())
7004 return ExprError();
7005 }
7006 if (VK != VK_LValue && Res.get()->isGLValue()) {
7007 Res = DefaultLvalueConversion(Res.get());
7008 if (!Res.isUsable())
7009 return ExprError();
7010 }
7011 return Res;
7012}
7013
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007014OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7015 SourceLocation StartLoc,
7016 SourceLocation LParenLoc,
7017 SourceLocation EndLoc) {
7018 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007019 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007020 for (auto &RefExpr : VarList) {
7021 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007022 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7023 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007024 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007025 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007026 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007027 continue;
7028 }
7029
Alexey Bataeved09d242014-05-28 05:53:51 +00007030 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00007031 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007032 // A list item is a variable name.
7033 // OpenMP [2.9.3.3, Restrictions, p.1]
7034 // A variable that is part of another variable (as an array or
7035 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007036 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
7037 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
7038 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7039 (getCurrentThisType().isNull() || !ME ||
7040 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7041 !isa<FieldDecl>(ME->getMemberDecl()))) {
7042 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7043 << (getCurrentThisType().isNull() ? 0 : 1)
7044 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007045 continue;
7046 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007047 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
7048 QualType Type = D->getType();
7049 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007050
7051 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7052 // A variable that appears in a private clause must not have an incomplete
7053 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007054 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007055 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007056 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007057
Alexey Bataev758e55e2013-09-06 18:03:48 +00007058 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7059 // in a Construct]
7060 // Variables with the predetermined data-sharing attributes may not be
7061 // listed in data-sharing attributes clauses, except for the cases
7062 // listed below. For these exceptions only, listing a predetermined
7063 // variable in a data-sharing attribute clause is allowed and overrides
7064 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007065 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007066 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007067 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7068 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007069 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007070 continue;
7071 }
7072
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007073 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007074 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007075 DSAStack->getCurrentDirective() == OMPD_task) {
7076 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7077 << getOpenMPClauseName(OMPC_private) << Type
7078 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7079 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007080 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007081 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007082 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007083 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007084 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007085 continue;
7086 }
7087
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007088 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7089 // A variable of class type (or array thereof) that appears in a private
7090 // clause requires an accessible, unambiguous default constructor for the
7091 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007092 // Generate helper private variable and initialize it with the default
7093 // value. The address of the original variable is replaced by the address of
7094 // the new private variable in CodeGen. This new variable is not added to
7095 // IdResolver, so the code in the OpenMP region uses original variable for
7096 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007097 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007098 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7099 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007100 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007101 if (VDPrivate->isInvalidDecl())
7102 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007103 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007104 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007105
Alexey Bataev90c228f2016-02-08 09:29:13 +00007106 DeclRefExpr *Ref = nullptr;
7107 if (!VD)
7108 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7109 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7110 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007111 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007112 }
7113
Alexey Bataeved09d242014-05-28 05:53:51 +00007114 if (Vars.empty())
7115 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007116
Alexey Bataev03b340a2014-10-21 03:16:40 +00007117 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7118 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007119}
7120
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007121namespace {
7122class DiagsUninitializedSeveretyRAII {
7123private:
7124 DiagnosticsEngine &Diags;
7125 SourceLocation SavedLoc;
7126 bool IsIgnored;
7127
7128public:
7129 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7130 bool IsIgnored)
7131 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7132 if (!IsIgnored) {
7133 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7134 /*Map*/ diag::Severity::Ignored, Loc);
7135 }
7136 }
7137 ~DiagsUninitializedSeveretyRAII() {
7138 if (!IsIgnored)
7139 Diags.popMappings(SavedLoc);
7140 }
7141};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007142}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007143
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007144OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7145 SourceLocation StartLoc,
7146 SourceLocation LParenLoc,
7147 SourceLocation EndLoc) {
7148 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007149 SmallVector<Expr *, 8> PrivateCopies;
7150 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007151 bool IsImplicitClause =
7152 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7153 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7154
Alexey Bataeved09d242014-05-28 05:53:51 +00007155 for (auto &RefExpr : VarList) {
7156 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
7157 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007158 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007159 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007160 PrivateCopies.push_back(nullptr);
7161 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007162 continue;
7163 }
7164
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007165 SourceLocation ELoc =
7166 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007167 // OpenMP [2.1, C/C++]
7168 // A list item is a variable name.
7169 // OpenMP [2.9.3.3, Restrictions, p.1]
7170 // A variable that is part of another variable (as an array or
7171 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007172 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007173 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007174 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7175 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007176 continue;
7177 }
7178 Decl *D = DE->getDecl();
7179 VarDecl *VD = cast<VarDecl>(D);
7180
7181 QualType Type = VD->getType();
7182 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7183 // It will be analyzed later.
7184 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007185 PrivateCopies.push_back(nullptr);
7186 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007187 continue;
7188 }
7189
7190 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7191 // A variable that appears in a private clause must not have an incomplete
7192 // type or a reference type.
7193 if (RequireCompleteType(ELoc, Type,
7194 diag::err_omp_firstprivate_incomplete_type)) {
7195 continue;
7196 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007197 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007198
7199 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7200 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007201 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007202 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007203 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007204
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007205 // If an implicit firstprivate variable found it was checked already.
7206 if (!IsImplicitClause) {
7207 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007208 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007209 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7210 // A list item that specifies a given variable may not appear in more
7211 // than one clause on the same directive, except that a variable may be
7212 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007213 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007214 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007215 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007216 << getOpenMPClauseName(DVar.CKind)
7217 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007218 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007219 continue;
7220 }
7221
7222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7223 // in a Construct]
7224 // Variables with the predetermined data-sharing attributes may not be
7225 // listed in data-sharing attributes clauses, except for the cases
7226 // listed below. For these exceptions only, listing a predetermined
7227 // variable in a data-sharing attribute clause is allowed and overrides
7228 // the variable's predetermined data-sharing attributes.
7229 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7230 // in a Construct, C/C++, p.2]
7231 // Variables with const-qualified type having no mutable member may be
7232 // listed in a firstprivate clause, even if they are static data members.
7233 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7234 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7235 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007236 << getOpenMPClauseName(DVar.CKind)
7237 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007238 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007239 continue;
7240 }
7241
Alexey Bataevf29276e2014-06-18 04:14:57 +00007242 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007243 // OpenMP [2.9.3.4, Restrictions, p.2]
7244 // A list item that is private within a parallel region must not appear
7245 // in a firstprivate clause on a worksharing construct if any of the
7246 // worksharing regions arising from the worksharing construct ever bind
7247 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007248 if (isOpenMPWorksharingDirective(CurrDir) &&
7249 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007250 DVar = DSAStack->getImplicitDSA(VD, true);
7251 if (DVar.CKind != OMPC_shared &&
7252 (isOpenMPParallelDirective(DVar.DKind) ||
7253 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007254 Diag(ELoc, diag::err_omp_required_access)
7255 << getOpenMPClauseName(OMPC_firstprivate)
7256 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007257 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007258 continue;
7259 }
7260 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007261 // OpenMP [2.9.3.4, Restrictions, p.3]
7262 // A list item that appears in a reduction clause of a parallel construct
7263 // must not appear in a firstprivate clause on a worksharing or task
7264 // construct if any of the worksharing or task regions arising from the
7265 // worksharing or task construct ever bind to any of the parallel regions
7266 // arising from the parallel construct.
7267 // OpenMP [2.9.3.4, Restrictions, p.4]
7268 // A list item that appears in a reduction clause in worksharing
7269 // construct must not appear in a firstprivate clause in a task construct
7270 // encountered during execution of any of the worksharing regions arising
7271 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007272 if (CurrDir == OMPD_task) {
7273 DVar =
7274 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7275 [](OpenMPDirectiveKind K) -> bool {
7276 return isOpenMPParallelDirective(K) ||
7277 isOpenMPWorksharingDirective(K);
7278 },
7279 false);
7280 if (DVar.CKind == OMPC_reduction &&
7281 (isOpenMPParallelDirective(DVar.DKind) ||
7282 isOpenMPWorksharingDirective(DVar.DKind))) {
7283 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7284 << getOpenMPDirectiveName(DVar.DKind);
7285 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7286 continue;
7287 }
7288 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007289
7290 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7291 // A list item that is private within a teams region must not appear in a
7292 // firstprivate clause on a distribute construct if any of the distribute
7293 // regions arising from the distribute construct ever bind to any of the
7294 // teams regions arising from the teams construct.
7295 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7296 // A list item that appears in a reduction clause of a teams construct
7297 // must not appear in a firstprivate clause on a distribute construct if
7298 // any of the distribute regions arising from the distribute construct
7299 // ever bind to any of the teams regions arising from the teams construct.
7300 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7301 // A list item may appear in a firstprivate or lastprivate clause but not
7302 // both.
7303 if (CurrDir == OMPD_distribute) {
7304 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7305 [](OpenMPDirectiveKind K) -> bool {
7306 return isOpenMPTeamsDirective(K);
7307 },
7308 false);
7309 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7310 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7311 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7312 continue;
7313 }
7314 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7315 [](OpenMPDirectiveKind K) -> bool {
7316 return isOpenMPTeamsDirective(K);
7317 },
7318 false);
7319 if (DVar.CKind == OMPC_reduction &&
7320 isOpenMPTeamsDirective(DVar.DKind)) {
7321 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7322 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7323 continue;
7324 }
7325 DVar = DSAStack->getTopDSA(VD, false);
7326 if (DVar.CKind == OMPC_lastprivate) {
7327 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7328 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7329 continue;
7330 }
7331 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007332 }
7333
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007334 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007335 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007336 DSAStack->getCurrentDirective() == OMPD_task) {
7337 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7338 << getOpenMPClauseName(OMPC_firstprivate) << Type
7339 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7340 bool IsDecl =
7341 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7342 Diag(VD->getLocation(),
7343 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7344 << VD;
7345 continue;
7346 }
7347
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007348 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007349 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7350 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007351 // Generate helper private variable and initialize it with the value of the
7352 // original variable. The address of the original variable is replaced by
7353 // the address of the new private variable in the CodeGen. This new variable
7354 // is not added to IdResolver, so the code in the OpenMP region uses
7355 // original variable for proper diagnostics and variable capturing.
7356 Expr *VDInitRefExpr = nullptr;
7357 // For arrays generate initializer for single element and replace it by the
7358 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007359 if (Type->isArrayType()) {
7360 auto VDInit =
7361 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7362 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007363 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007364 ElemType = ElemType.getUnqualifiedType();
7365 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7366 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007367 InitializedEntity Entity =
7368 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007369 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7370
7371 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7372 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7373 if (Result.isInvalid())
7374 VDPrivate->setInvalidDecl();
7375 else
7376 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007377 // Remove temp variable declaration.
7378 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007379 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007380 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007381 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007382 VDInitRefExpr =
7383 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007384 AddInitializerToDecl(VDPrivate,
7385 DefaultLvalueConversion(VDInitRefExpr).get(),
7386 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007387 }
7388 if (VDPrivate->isInvalidDecl()) {
7389 if (IsImplicitClause) {
7390 Diag(DE->getExprLoc(),
7391 diag::note_omp_task_predetermined_firstprivate_here);
7392 }
7393 continue;
7394 }
7395 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007396 auto VDPrivateRefExpr = buildDeclRefExpr(
7397 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007398 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7399 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007400 PrivateCopies.push_back(VDPrivateRefExpr);
7401 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007402 }
7403
Alexey Bataeved09d242014-05-28 05:53:51 +00007404 if (Vars.empty())
7405 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007406
7407 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007408 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007409}
7410
Alexander Musman1bb328c2014-06-04 13:06:39 +00007411OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7412 SourceLocation StartLoc,
7413 SourceLocation LParenLoc,
7414 SourceLocation EndLoc) {
7415 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007416 SmallVector<Expr *, 8> SrcExprs;
7417 SmallVector<Expr *, 8> DstExprs;
7418 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007419 for (auto &RefExpr : VarList) {
7420 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7421 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7422 // It will be analyzed later.
7423 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007424 SrcExprs.push_back(nullptr);
7425 DstExprs.push_back(nullptr);
7426 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007427 continue;
7428 }
7429
7430 SourceLocation ELoc = RefExpr->getExprLoc();
7431 // OpenMP [2.1, C/C++]
7432 // A list item is a variable name.
7433 // OpenMP [2.14.3.5, Restrictions, p.1]
7434 // A variable that is part of another variable (as an array or structure
7435 // element) cannot appear in a lastprivate clause.
7436 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7437 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007438 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7439 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007440 continue;
7441 }
7442 Decl *D = DE->getDecl();
7443 VarDecl *VD = cast<VarDecl>(D);
7444
7445 QualType Type = VD->getType();
7446 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7447 // It will be analyzed later.
7448 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007449 SrcExprs.push_back(nullptr);
7450 DstExprs.push_back(nullptr);
7451 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007452 continue;
7453 }
7454
7455 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7456 // A variable that appears in a lastprivate clause must not have an
7457 // incomplete type or a reference type.
7458 if (RequireCompleteType(ELoc, Type,
7459 diag::err_omp_lastprivate_incomplete_type)) {
7460 continue;
7461 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007462 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007463
7464 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7465 // in a Construct]
7466 // Variables with the predetermined data-sharing attributes may not be
7467 // listed in data-sharing attributes clauses, except for the cases
7468 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007469 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007470 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7471 DVar.CKind != OMPC_firstprivate &&
7472 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7473 Diag(ELoc, diag::err_omp_wrong_dsa)
7474 << getOpenMPClauseName(DVar.CKind)
7475 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007476 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007477 continue;
7478 }
7479
Alexey Bataevf29276e2014-06-18 04:14:57 +00007480 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7481 // OpenMP [2.14.3.5, Restrictions, p.2]
7482 // A list item that is private within a parallel region, or that appears in
7483 // the reduction clause of a parallel construct, must not appear in a
7484 // lastprivate clause on a worksharing construct if any of the corresponding
7485 // worksharing regions ever binds to any of the corresponding parallel
7486 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007487 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007488 if (isOpenMPWorksharingDirective(CurrDir) &&
7489 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007490 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007491 if (DVar.CKind != OMPC_shared) {
7492 Diag(ELoc, diag::err_omp_required_access)
7493 << getOpenMPClauseName(OMPC_lastprivate)
7494 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007495 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007496 continue;
7497 }
7498 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007499 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007500 // A variable of class type (or array thereof) that appears in a
7501 // lastprivate clause requires an accessible, unambiguous default
7502 // constructor for the class type, unless the list item is also specified
7503 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007504 // A variable of class type (or array thereof) that appears in a
7505 // lastprivate clause requires an accessible, unambiguous copy assignment
7506 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007507 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007508 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007509 Type.getUnqualifiedType(), ".lastprivate.src",
7510 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007511 auto *PseudoSrcExpr = buildDeclRefExpr(
7512 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007513 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007514 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7515 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007516 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007517 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007518 // For arrays generate assignment operation for single element and replace
7519 // it by the original array element in CodeGen.
7520 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7521 PseudoDstExpr, PseudoSrcExpr);
7522 if (AssignmentOp.isInvalid())
7523 continue;
7524 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7525 /*DiscardedValue=*/true);
7526 if (AssignmentOp.isInvalid())
7527 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007528
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007529 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7530 // A list item may appear in a firstprivate or lastprivate clause but not
7531 // both.
7532 if (CurrDir == OMPD_distribute) {
7533 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7534 if (DVar.CKind == OMPC_firstprivate) {
7535 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7536 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7537 continue;
7538 }
7539 }
7540
Alexey Bataev39f915b82015-05-08 10:41:21 +00007541 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007542 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007543 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007544 SrcExprs.push_back(PseudoSrcExpr);
7545 DstExprs.push_back(PseudoDstExpr);
7546 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007547 }
7548
7549 if (Vars.empty())
7550 return nullptr;
7551
7552 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007553 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007554}
7555
Alexey Bataev758e55e2013-09-06 18:03:48 +00007556OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7557 SourceLocation StartLoc,
7558 SourceLocation LParenLoc,
7559 SourceLocation EndLoc) {
7560 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007561 for (auto &RefExpr : VarList) {
7562 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7563 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007564 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007565 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007566 continue;
7567 }
7568
Alexey Bataeved09d242014-05-28 05:53:51 +00007569 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007570 // OpenMP [2.1, C/C++]
7571 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007572 // OpenMP [2.14.3.2, Restrictions, p.1]
7573 // A variable that is part of another variable (as an array or structure
7574 // element) cannot appear in a shared unless it is a static data member
7575 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007576 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007577 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007578 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7579 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007580 continue;
7581 }
7582 Decl *D = DE->getDecl();
7583 VarDecl *VD = cast<VarDecl>(D);
7584
7585 QualType Type = VD->getType();
7586 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7587 // It will be analyzed later.
7588 Vars.push_back(DE);
7589 continue;
7590 }
7591
7592 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7593 // in a Construct]
7594 // Variables with the predetermined data-sharing attributes may not be
7595 // listed in data-sharing attributes clauses, except for the cases
7596 // listed below. For these exceptions only, listing a predetermined
7597 // variable in a data-sharing attribute clause is allowed and overrides
7598 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007599 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007600 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7601 DVar.RefExpr) {
7602 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7603 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007604 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007605 continue;
7606 }
7607
7608 DSAStack->addDSA(VD, DE, OMPC_shared);
7609 Vars.push_back(DE);
7610 }
7611
Alexey Bataeved09d242014-05-28 05:53:51 +00007612 if (Vars.empty())
7613 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007614
7615 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7616}
7617
Alexey Bataevc5e02582014-06-16 07:08:35 +00007618namespace {
7619class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7620 DSAStackTy *Stack;
7621
7622public:
7623 bool VisitDeclRefExpr(DeclRefExpr *E) {
7624 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007625 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007626 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7627 return false;
7628 if (DVar.CKind != OMPC_unknown)
7629 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007630 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007631 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007632 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007633 return true;
7634 return false;
7635 }
7636 return false;
7637 }
7638 bool VisitStmt(Stmt *S) {
7639 for (auto Child : S->children()) {
7640 if (Child && Visit(Child))
7641 return true;
7642 }
7643 return false;
7644 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007645 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007646};
Alexey Bataev23b69422014-06-18 07:08:49 +00007647} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007648
7649OMPClause *Sema::ActOnOpenMPReductionClause(
7650 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7651 SourceLocation ColonLoc, SourceLocation EndLoc,
7652 CXXScopeSpec &ReductionIdScopeSpec,
7653 const DeclarationNameInfo &ReductionId) {
7654 // TODO: Allow scope specification search when 'declare reduction' is
7655 // supported.
7656 assert(ReductionIdScopeSpec.isEmpty() &&
7657 "No support for scoped reduction identifiers yet.");
7658
7659 auto DN = ReductionId.getName();
7660 auto OOK = DN.getCXXOverloadedOperator();
7661 BinaryOperatorKind BOK = BO_Comma;
7662
7663 // OpenMP [2.14.3.6, reduction clause]
7664 // C
7665 // reduction-identifier is either an identifier or one of the following
7666 // operators: +, -, *, &, |, ^, && and ||
7667 // C++
7668 // reduction-identifier is either an id-expression or one of the following
7669 // operators: +, -, *, &, |, ^, && and ||
7670 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7671 switch (OOK) {
7672 case OO_Plus:
7673 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007674 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007675 break;
7676 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007677 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007678 break;
7679 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007680 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007681 break;
7682 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007683 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007684 break;
7685 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007686 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007687 break;
7688 case OO_AmpAmp:
7689 BOK = BO_LAnd;
7690 break;
7691 case OO_PipePipe:
7692 BOK = BO_LOr;
7693 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007694 case OO_New:
7695 case OO_Delete:
7696 case OO_Array_New:
7697 case OO_Array_Delete:
7698 case OO_Slash:
7699 case OO_Percent:
7700 case OO_Tilde:
7701 case OO_Exclaim:
7702 case OO_Equal:
7703 case OO_Less:
7704 case OO_Greater:
7705 case OO_LessEqual:
7706 case OO_GreaterEqual:
7707 case OO_PlusEqual:
7708 case OO_MinusEqual:
7709 case OO_StarEqual:
7710 case OO_SlashEqual:
7711 case OO_PercentEqual:
7712 case OO_CaretEqual:
7713 case OO_AmpEqual:
7714 case OO_PipeEqual:
7715 case OO_LessLess:
7716 case OO_GreaterGreater:
7717 case OO_LessLessEqual:
7718 case OO_GreaterGreaterEqual:
7719 case OO_EqualEqual:
7720 case OO_ExclaimEqual:
7721 case OO_PlusPlus:
7722 case OO_MinusMinus:
7723 case OO_Comma:
7724 case OO_ArrowStar:
7725 case OO_Arrow:
7726 case OO_Call:
7727 case OO_Subscript:
7728 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007729 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007730 case NUM_OVERLOADED_OPERATORS:
7731 llvm_unreachable("Unexpected reduction identifier");
7732 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007733 if (auto II = DN.getAsIdentifierInfo()) {
7734 if (II->isStr("max"))
7735 BOK = BO_GT;
7736 else if (II->isStr("min"))
7737 BOK = BO_LT;
7738 }
7739 break;
7740 }
7741 SourceRange ReductionIdRange;
7742 if (ReductionIdScopeSpec.isValid()) {
7743 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7744 }
7745 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7746 if (BOK == BO_Comma) {
7747 // Not allowed reduction identifier is found.
7748 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7749 << ReductionIdRange;
7750 return nullptr;
7751 }
7752
7753 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007754 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007755 SmallVector<Expr *, 8> LHSs;
7756 SmallVector<Expr *, 8> RHSs;
7757 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007758 for (auto RefExpr : VarList) {
7759 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7760 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7761 // It will be analyzed later.
7762 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007763 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007764 LHSs.push_back(nullptr);
7765 RHSs.push_back(nullptr);
7766 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007767 continue;
7768 }
7769
7770 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7771 RefExpr->isInstantiationDependent() ||
7772 RefExpr->containsUnexpandedParameterPack()) {
7773 // It will be analyzed later.
7774 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007775 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007776 LHSs.push_back(nullptr);
7777 RHSs.push_back(nullptr);
7778 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007779 continue;
7780 }
7781
7782 auto ELoc = RefExpr->getExprLoc();
7783 auto ERange = RefExpr->getSourceRange();
7784 // OpenMP [2.1, C/C++]
7785 // A list item is a variable or array section, subject to the restrictions
7786 // specified in Section 2.4 on page 42 and in each of the sections
7787 // describing clauses and directives for which a list appears.
7788 // OpenMP [2.14.3.3, Restrictions, p.1]
7789 // A variable that is part of another variable (as an array or
7790 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007791 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7792 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7793 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7794 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007795 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7796 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007797 continue;
7798 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007799 QualType Type;
7800 VarDecl *VD = nullptr;
7801 if (DE) {
7802 auto D = DE->getDecl();
7803 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007804 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007805 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007806 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007807 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7808 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7809 Base = TempASE->getBase()->IgnoreParenImpCasts();
7810 DE = dyn_cast<DeclRefExpr>(Base);
7811 if (DE)
7812 VD = dyn_cast<VarDecl>(DE->getDecl());
7813 if (!VD) {
7814 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7815 << 0 << Base->getSourceRange();
7816 continue;
7817 }
7818 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007819 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7820 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7821 Type = ATy->getElementType();
7822 else
7823 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007824 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007825 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7826 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7827 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7828 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7829 Base = TempASE->getBase()->IgnoreParenImpCasts();
7830 DE = dyn_cast<DeclRefExpr>(Base);
7831 if (DE)
7832 VD = dyn_cast<VarDecl>(DE->getDecl());
7833 if (!VD) {
7834 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7835 << 1 << Base->getSourceRange();
7836 continue;
7837 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007838 }
7839
Alexey Bataevc5e02582014-06-16 07:08:35 +00007840 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7841 // A variable that appears in a private clause must not have an incomplete
7842 // type or a reference type.
7843 if (RequireCompleteType(ELoc, Type,
7844 diag::err_omp_reduction_incomplete_type))
7845 continue;
7846 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007847 // A list item that appears in a reduction clause must not be
7848 // const-qualified.
7849 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007850 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007851 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007852 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007853 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7854 VarDecl::DeclarationOnly;
7855 Diag(VD->getLocation(),
7856 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7857 << VD;
7858 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007859 continue;
7860 }
7861 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7862 // If a list-item is a reference type then it must bind to the same object
7863 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007864 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007865 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007866 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007867 DSARefChecker Check(DSAStack);
7868 if (Check.Visit(VDDef->getInit())) {
7869 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7870 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7871 continue;
7872 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007873 }
7874 }
7875 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7876 // The type of a list item that appears in a reduction clause must be valid
7877 // for the reduction-identifier. For a max or min reduction in C, the type
7878 // of the list item must be an allowed arithmetic data type: char, int,
7879 // float, double, or _Bool, possibly modified with long, short, signed, or
7880 // unsigned. For a max or min reduction in C++, the type of the list item
7881 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7882 // double, or bool, possibly modified with long, short, signed, or unsigned.
7883 if ((BOK == BO_GT || BOK == BO_LT) &&
7884 !(Type->isScalarType() ||
7885 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7886 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7887 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007888 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007889 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7890 VarDecl::DeclarationOnly;
7891 Diag(VD->getLocation(),
7892 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7893 << VD;
7894 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007895 continue;
7896 }
7897 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7898 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7899 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007900 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007901 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7902 VarDecl::DeclarationOnly;
7903 Diag(VD->getLocation(),
7904 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7905 << VD;
7906 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007907 continue;
7908 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007909 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7910 // in a Construct]
7911 // Variables with the predetermined data-sharing attributes may not be
7912 // listed in data-sharing attributes clauses, except for the cases
7913 // listed below. For these exceptions only, listing a predetermined
7914 // variable in a data-sharing attribute clause is allowed and overrides
7915 // the variable's predetermined data-sharing attributes.
7916 // OpenMP [2.14.3.6, Restrictions, p.3]
7917 // Any number of reduction clauses can be specified on the directive,
7918 // but a list item can appear only once in the reduction clauses for that
7919 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007920 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007921 DVar = DSAStack->getTopDSA(VD, false);
7922 if (DVar.CKind == OMPC_reduction) {
7923 Diag(ELoc, diag::err_omp_once_referenced)
7924 << getOpenMPClauseName(OMPC_reduction);
7925 if (DVar.RefExpr) {
7926 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007927 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007928 } else if (DVar.CKind != OMPC_unknown) {
7929 Diag(ELoc, diag::err_omp_wrong_dsa)
7930 << getOpenMPClauseName(DVar.CKind)
7931 << getOpenMPClauseName(OMPC_reduction);
7932 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7933 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007934 }
7935
7936 // OpenMP [2.14.3.6, Restrictions, p.1]
7937 // A list item that appears in a reduction clause of a worksharing
7938 // construct must be shared in the parallel regions to which any of the
7939 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007940 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7941 if (isOpenMPWorksharingDirective(CurrDir) &&
7942 !isOpenMPParallelDirective(CurrDir)) {
7943 DVar = DSAStack->getImplicitDSA(VD, true);
7944 if (DVar.CKind != OMPC_shared) {
7945 Diag(ELoc, diag::err_omp_required_access)
7946 << getOpenMPClauseName(OMPC_reduction)
7947 << getOpenMPClauseName(OMPC_shared);
7948 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7949 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007950 }
7951 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007952
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007953 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007954 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7955 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7956 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7957 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7958 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007959 if (OASE ||
7960 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7961 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007962 // Create pseudo array type for private copy. The size for this array will
7963 // be generated during codegen.
7964 // For array subscripts or single variables Private Ty is the same as Type
7965 // (type of the variable or single array element).
7966 PrivateTy = Context.getVariableArrayType(
7967 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7968 Context.getSizeType(), VK_RValue),
7969 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007970 } else if (DE &&
7971 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7972 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007973 // Private copy.
7974 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7975 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007976 // Add initializer for private variable.
7977 Expr *Init = nullptr;
7978 switch (BOK) {
7979 case BO_Add:
7980 case BO_Xor:
7981 case BO_Or:
7982 case BO_LOr:
7983 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7984 if (Type->isScalarType() || Type->isAnyComplexType()) {
7985 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007986 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007987 break;
7988 case BO_Mul:
7989 case BO_LAnd:
7990 if (Type->isScalarType() || Type->isAnyComplexType()) {
7991 // '*' and '&&' reduction ops - initializer is '1'.
7992 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7993 }
7994 break;
7995 case BO_And: {
7996 // '&' reduction op - initializer is '~0'.
7997 QualType OrigType = Type;
7998 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7999 Type = ComplexTy->getElementType();
8000 }
8001 if (Type->isRealFloatingType()) {
8002 llvm::APFloat InitValue =
8003 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8004 /*isIEEE=*/true);
8005 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8006 Type, ELoc);
8007 } else if (Type->isScalarType()) {
8008 auto Size = Context.getTypeSize(Type);
8009 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8010 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8011 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8012 }
8013 if (Init && OrigType->isAnyComplexType()) {
8014 // Init = 0xFFFF + 0xFFFFi;
8015 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8016 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8017 }
8018 Type = OrigType;
8019 break;
8020 }
8021 case BO_LT:
8022 case BO_GT: {
8023 // 'min' reduction op - initializer is 'Largest representable number in
8024 // the reduction list item type'.
8025 // 'max' reduction op - initializer is 'Least representable number in
8026 // the reduction list item type'.
8027 if (Type->isIntegerType() || Type->isPointerType()) {
8028 bool IsSigned = Type->hasSignedIntegerRepresentation();
8029 auto Size = Context.getTypeSize(Type);
8030 QualType IntTy =
8031 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8032 llvm::APInt InitValue =
8033 (BOK != BO_LT)
8034 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8035 : llvm::APInt::getMinValue(Size)
8036 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8037 : llvm::APInt::getMaxValue(Size);
8038 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8039 if (Type->isPointerType()) {
8040 // Cast to pointer type.
8041 auto CastExpr = BuildCStyleCastExpr(
8042 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8043 SourceLocation(), Init);
8044 if (CastExpr.isInvalid())
8045 continue;
8046 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008047 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008048 } else if (Type->isRealFloatingType()) {
8049 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8050 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8051 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8052 Type, ELoc);
8053 }
8054 break;
8055 }
8056 case BO_PtrMemD:
8057 case BO_PtrMemI:
8058 case BO_MulAssign:
8059 case BO_Div:
8060 case BO_Rem:
8061 case BO_Sub:
8062 case BO_Shl:
8063 case BO_Shr:
8064 case BO_LE:
8065 case BO_GE:
8066 case BO_EQ:
8067 case BO_NE:
8068 case BO_AndAssign:
8069 case BO_XorAssign:
8070 case BO_OrAssign:
8071 case BO_Assign:
8072 case BO_AddAssign:
8073 case BO_SubAssign:
8074 case BO_DivAssign:
8075 case BO_RemAssign:
8076 case BO_ShlAssign:
8077 case BO_ShrAssign:
8078 case BO_Comma:
8079 llvm_unreachable("Unexpected reduction operation");
8080 }
8081 if (Init) {
8082 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8083 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008084 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008085 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008086 if (!RHSVD->hasInit()) {
8087 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8088 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008089 if (VD) {
8090 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8091 VarDecl::DeclarationOnly;
8092 Diag(VD->getLocation(),
8093 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8094 << VD;
8095 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008096 continue;
8097 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008098 // Store initializer for single element in private copy. Will be used during
8099 // codegen.
8100 PrivateVD->setInit(RHSVD->getInit());
8101 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008102 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8103 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008104 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008105 ExprResult ReductionOp =
8106 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8107 LHSDRE, RHSDRE);
8108 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008109 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008110 ReductionOp =
8111 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8112 BO_Assign, LHSDRE, ReductionOp.get());
8113 } else {
8114 auto *ConditionalOp = new (Context) ConditionalOperator(
8115 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8116 RHSDRE, Type, VK_LValue, OK_Ordinary);
8117 ReductionOp =
8118 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8119 BO_Assign, LHSDRE, ConditionalOp);
8120 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008121 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008122 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008123 if (ReductionOp.isInvalid())
8124 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008125
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008126 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008127 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008128 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008129 LHSs.push_back(LHSDRE);
8130 RHSs.push_back(RHSDRE);
8131 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008132 }
8133
8134 if (Vars.empty())
8135 return nullptr;
8136
8137 return OMPReductionClause::Create(
8138 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008139 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8140 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008141}
8142
Alexey Bataev182227b2015-08-20 10:54:39 +00008143OMPClause *Sema::ActOnOpenMPLinearClause(
8144 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8145 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8146 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008147 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008148 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008149 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008150 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8151 LinKind == OMPC_LINEAR_unknown) {
8152 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8153 LinKind = OMPC_LINEAR_val;
8154 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008155 for (auto &RefExpr : VarList) {
8156 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8157 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008158 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008159 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008160 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008161 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008162 continue;
8163 }
8164
8165 // OpenMP [2.14.3.7, linear clause]
8166 // A list item that appears in a linear clause is subject to the private
8167 // clause semantics described in Section 2.14.3.3 on page 159 except as
8168 // noted. In addition, the value of the new list item on each iteration
8169 // of the associated loop(s) corresponds to the value of the original
8170 // list item before entering the construct plus the logical number of
8171 // the iteration times linear-step.
8172
Alexey Bataeved09d242014-05-28 05:53:51 +00008173 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008174 // OpenMP [2.1, C/C++]
8175 // A list item is a variable name.
8176 // OpenMP [2.14.3.3, Restrictions, p.1]
8177 // A variable that is part of another variable (as an array or
8178 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008179 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008180 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008181 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8182 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008183 continue;
8184 }
8185
8186 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8187
8188 // OpenMP [2.14.3.7, linear clause]
8189 // A list-item cannot appear in more than one linear clause.
8190 // A list-item that appears in a linear clause cannot appear in any
8191 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008192 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008193 if (DVar.RefExpr) {
8194 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8195 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008196 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008197 continue;
8198 }
8199
8200 QualType QType = VD->getType();
8201 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8202 // It will be analyzed later.
8203 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008204 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008205 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008206 continue;
8207 }
8208
8209 // A variable must not have an incomplete type or a reference type.
8210 if (RequireCompleteType(ELoc, QType,
8211 diag::err_omp_linear_incomplete_type)) {
8212 continue;
8213 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008214 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8215 !QType->isReferenceType()) {
8216 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8217 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8218 continue;
8219 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008220 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008221
8222 // A list item must not be const-qualified.
8223 if (QType.isConstant(Context)) {
8224 Diag(ELoc, diag::err_omp_const_variable)
8225 << getOpenMPClauseName(OMPC_linear);
8226 bool IsDecl =
8227 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8228 Diag(VD->getLocation(),
8229 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8230 << VD;
8231 continue;
8232 }
8233
8234 // A list item must be of integral or pointer type.
8235 QType = QType.getUnqualifiedType().getCanonicalType();
8236 const Type *Ty = QType.getTypePtrOrNull();
8237 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8238 !Ty->isPointerType())) {
8239 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8240 bool IsDecl =
8241 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8242 Diag(VD->getLocation(),
8243 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8244 << VD;
8245 continue;
8246 }
8247
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008248 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008249 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8250 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008251 auto *PrivateRef = buildDeclRefExpr(
8252 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008253 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008254 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008255 Expr *InitExpr;
8256 if (LinKind == OMPC_LINEAR_uval)
8257 InitExpr = VD->getInit();
8258 else
8259 InitExpr = DE;
8260 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008261 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008262 auto InitRef = buildDeclRefExpr(
8263 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008264 DSAStack->addDSA(VD, DE, OMPC_linear);
8265 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008266 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008267 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008268 }
8269
8270 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008271 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008272
8273 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008274 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008275 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8276 !Step->isInstantiationDependent() &&
8277 !Step->containsUnexpandedParameterPack()) {
8278 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008279 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008280 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008281 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008282 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008283
Alexander Musman3276a272015-03-21 10:12:56 +00008284 // Build var to save the step value.
8285 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008286 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008287 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008288 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008289 ExprResult CalcStep =
8290 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008291 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008292
Alexander Musman8dba6642014-04-22 13:09:42 +00008293 // Warn about zero linear step (it would be probably better specified as
8294 // making corresponding variables 'const').
8295 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008296 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8297 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008298 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8299 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008300 if (!IsConstant && CalcStep.isUsable()) {
8301 // Calculate the step beforehand instead of doing this on each iteration.
8302 // (This is not used if the number of iterations may be kfold-ed).
8303 CalcStepExpr = CalcStep.get();
8304 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008305 }
8306
Alexey Bataev182227b2015-08-20 10:54:39 +00008307 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8308 ColonLoc, EndLoc, Vars, Privates, Inits,
8309 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008310}
8311
8312static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8313 Expr *NumIterations, Sema &SemaRef,
8314 Scope *S) {
8315 // Walk the vars and build update/final expressions for the CodeGen.
8316 SmallVector<Expr *, 8> Updates;
8317 SmallVector<Expr *, 8> Finals;
8318 Expr *Step = Clause.getStep();
8319 Expr *CalcStep = Clause.getCalcStep();
8320 // OpenMP [2.14.3.7, linear clause]
8321 // If linear-step is not specified it is assumed to be 1.
8322 if (Step == nullptr)
8323 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8324 else if (CalcStep)
8325 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8326 bool HasErrors = false;
8327 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008328 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008329 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008330 for (auto &RefExpr : Clause.varlists()) {
8331 Expr *InitExpr = *CurInit;
8332
8333 // Build privatized reference to the current linear var.
8334 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008335 Expr *CapturedRef;
8336 if (LinKind == OMPC_LINEAR_uval)
8337 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8338 else
8339 CapturedRef =
8340 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8341 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8342 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008343
8344 // Build update: Var = InitExpr + IV * Step
8345 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008346 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008347 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008348 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8349 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008350
8351 // Build final: Var = InitExpr + NumIterations * Step
8352 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008353 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008354 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008355 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8356 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008357 if (!Update.isUsable() || !Final.isUsable()) {
8358 Updates.push_back(nullptr);
8359 Finals.push_back(nullptr);
8360 HasErrors = true;
8361 } else {
8362 Updates.push_back(Update.get());
8363 Finals.push_back(Final.get());
8364 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008365 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008366 }
8367 Clause.setUpdates(Updates);
8368 Clause.setFinals(Finals);
8369 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008370}
8371
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008372OMPClause *Sema::ActOnOpenMPAlignedClause(
8373 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8374 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8375
8376 SmallVector<Expr *, 8> Vars;
8377 for (auto &RefExpr : VarList) {
8378 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8379 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8380 // It will be analyzed later.
8381 Vars.push_back(RefExpr);
8382 continue;
8383 }
8384
8385 SourceLocation ELoc = RefExpr->getExprLoc();
8386 // OpenMP [2.1, C/C++]
8387 // A list item is a variable name.
8388 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8389 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008390 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8391 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008392 continue;
8393 }
8394
8395 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8396
8397 // OpenMP [2.8.1, simd construct, Restrictions]
8398 // The type of list items appearing in the aligned clause must be
8399 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008400 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008401 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008402 const Type *Ty = QType.getTypePtrOrNull();
8403 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8404 !Ty->isPointerType())) {
8405 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8406 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8407 bool IsDecl =
8408 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8409 Diag(VD->getLocation(),
8410 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8411 << VD;
8412 continue;
8413 }
8414
8415 // OpenMP [2.8.1, simd construct, Restrictions]
8416 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008417 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008418 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8419 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8420 << getOpenMPClauseName(OMPC_aligned);
8421 continue;
8422 }
8423
8424 Vars.push_back(DE);
8425 }
8426
8427 // OpenMP [2.8.1, simd construct, Description]
8428 // The parameter of the aligned clause, alignment, must be a constant
8429 // positive integer expression.
8430 // If no optional parameter is specified, implementation-defined default
8431 // alignments for SIMD instructions on the target platforms are assumed.
8432 if (Alignment != nullptr) {
8433 ExprResult AlignResult =
8434 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8435 if (AlignResult.isInvalid())
8436 return nullptr;
8437 Alignment = AlignResult.get();
8438 }
8439 if (Vars.empty())
8440 return nullptr;
8441
8442 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8443 EndLoc, Vars, Alignment);
8444}
8445
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008446OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8447 SourceLocation StartLoc,
8448 SourceLocation LParenLoc,
8449 SourceLocation EndLoc) {
8450 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008451 SmallVector<Expr *, 8> SrcExprs;
8452 SmallVector<Expr *, 8> DstExprs;
8453 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008454 for (auto &RefExpr : VarList) {
8455 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8456 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008457 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008458 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008459 SrcExprs.push_back(nullptr);
8460 DstExprs.push_back(nullptr);
8461 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008462 continue;
8463 }
8464
Alexey Bataeved09d242014-05-28 05:53:51 +00008465 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008466 // OpenMP [2.1, C/C++]
8467 // A list item is a variable name.
8468 // OpenMP [2.14.4.1, Restrictions, p.1]
8469 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008470 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008471 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();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008474 continue;
8475 }
8476
8477 Decl *D = DE->getDecl();
8478 VarDecl *VD = cast<VarDecl>(D);
8479
8480 QualType Type = VD->getType();
8481 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8482 // It will be analyzed later.
8483 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008484 SrcExprs.push_back(nullptr);
8485 DstExprs.push_back(nullptr);
8486 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008487 continue;
8488 }
8489
8490 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8491 // A list item that appears in a copyin clause must be threadprivate.
8492 if (!DSAStack->isThreadPrivate(VD)) {
8493 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008494 << getOpenMPClauseName(OMPC_copyin)
8495 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008496 continue;
8497 }
8498
8499 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8500 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008501 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008502 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008503 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008504 auto *SrcVD =
8505 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8506 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008507 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008508 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8509 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008510 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8511 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008512 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008513 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008514 // For arrays generate assignment operation for single element and replace
8515 // it by the original array element in CodeGen.
8516 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8517 PseudoDstExpr, PseudoSrcExpr);
8518 if (AssignmentOp.isInvalid())
8519 continue;
8520 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8521 /*DiscardedValue=*/true);
8522 if (AssignmentOp.isInvalid())
8523 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008524
8525 DSAStack->addDSA(VD, DE, OMPC_copyin);
8526 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008527 SrcExprs.push_back(PseudoSrcExpr);
8528 DstExprs.push_back(PseudoDstExpr);
8529 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008530 }
8531
Alexey Bataeved09d242014-05-28 05:53:51 +00008532 if (Vars.empty())
8533 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008534
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008535 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8536 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008537}
8538
Alexey Bataevbae9a792014-06-27 10:37:06 +00008539OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8540 SourceLocation StartLoc,
8541 SourceLocation LParenLoc,
8542 SourceLocation EndLoc) {
8543 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008544 SmallVector<Expr *, 8> SrcExprs;
8545 SmallVector<Expr *, 8> DstExprs;
8546 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008547 for (auto &RefExpr : VarList) {
8548 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8549 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8550 // It will be analyzed later.
8551 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008552 SrcExprs.push_back(nullptr);
8553 DstExprs.push_back(nullptr);
8554 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008555 continue;
8556 }
8557
8558 SourceLocation ELoc = RefExpr->getExprLoc();
8559 // OpenMP [2.1, C/C++]
8560 // A list item is a variable name.
8561 // OpenMP [2.14.4.1, Restrictions, p.1]
8562 // A list item that appears in a copyin clause must be threadprivate.
8563 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8564 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008565 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8566 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008567 continue;
8568 }
8569
8570 Decl *D = DE->getDecl();
8571 VarDecl *VD = cast<VarDecl>(D);
8572
8573 QualType Type = VD->getType();
8574 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8575 // It will be analyzed later.
8576 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008577 SrcExprs.push_back(nullptr);
8578 DstExprs.push_back(nullptr);
8579 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008580 continue;
8581 }
8582
8583 // OpenMP [2.14.4.2, Restrictions, p.2]
8584 // A list item that appears in a copyprivate clause may not appear in a
8585 // private or firstprivate clause on the single construct.
8586 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008587 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008588 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8589 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008590 Diag(ELoc, diag::err_omp_wrong_dsa)
8591 << getOpenMPClauseName(DVar.CKind)
8592 << getOpenMPClauseName(OMPC_copyprivate);
8593 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8594 continue;
8595 }
8596
8597 // OpenMP [2.11.4.2, Restrictions, p.1]
8598 // All list items that appear in a copyprivate clause must be either
8599 // threadprivate or private in the enclosing context.
8600 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008601 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008602 if (DVar.CKind == OMPC_shared) {
8603 Diag(ELoc, diag::err_omp_required_access)
8604 << getOpenMPClauseName(OMPC_copyprivate)
8605 << "threadprivate or private in the enclosing context";
8606 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8607 continue;
8608 }
8609 }
8610 }
8611
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008612 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008613 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008614 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008615 << getOpenMPClauseName(OMPC_copyprivate) << Type
8616 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008617 bool IsDecl =
8618 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8619 Diag(VD->getLocation(),
8620 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8621 << VD;
8622 continue;
8623 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008624
Alexey Bataevbae9a792014-06-27 10:37:06 +00008625 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8626 // A variable of class type (or array thereof) that appears in a
8627 // copyin clause requires an accessible, unambiguous copy assignment
8628 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008629 Type = Context.getBaseElementType(Type.getNonReferenceType())
8630 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008631 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008632 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8633 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008634 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008635 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008636 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008637 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8638 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008639 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008640 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008641 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8642 PseudoDstExpr, PseudoSrcExpr);
8643 if (AssignmentOp.isInvalid())
8644 continue;
8645 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8646 /*DiscardedValue=*/true);
8647 if (AssignmentOp.isInvalid())
8648 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008649
8650 // No need to mark vars as copyprivate, they are already threadprivate or
8651 // implicitly private.
8652 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008653 SrcExprs.push_back(PseudoSrcExpr);
8654 DstExprs.push_back(PseudoDstExpr);
8655 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008656 }
8657
8658 if (Vars.empty())
8659 return nullptr;
8660
Alexey Bataeva63048e2015-03-23 06:18:07 +00008661 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8662 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008663}
8664
Alexey Bataev6125da92014-07-21 11:26:11 +00008665OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8666 SourceLocation StartLoc,
8667 SourceLocation LParenLoc,
8668 SourceLocation EndLoc) {
8669 if (VarList.empty())
8670 return nullptr;
8671
8672 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8673}
Alexey Bataevdea47612014-07-23 07:46:59 +00008674
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008675OMPClause *
8676Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8677 SourceLocation DepLoc, SourceLocation ColonLoc,
8678 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8679 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008680 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008681 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008682 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008683 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008684 return nullptr;
8685 }
8686 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008687 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8688 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008689 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008690 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008691 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8692 /*Last=*/OMPC_DEPEND_unknown, Except)
8693 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008694 return nullptr;
8695 }
8696 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008697 llvm::APSInt DepCounter(/*BitWidth=*/32);
8698 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8699 if (DepKind == OMPC_DEPEND_sink) {
8700 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8701 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8702 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008703 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008704 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008705 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8706 DSAStack->getParentOrderedRegionParam()) {
8707 for (auto &RefExpr : VarList) {
8708 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8709 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8710 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8711 // It will be analyzed later.
8712 Vars.push_back(RefExpr);
8713 continue;
8714 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008715
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008716 SourceLocation ELoc = RefExpr->getExprLoc();
8717 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8718 if (DepKind == OMPC_DEPEND_sink) {
8719 if (DepCounter >= TotalDepCount) {
8720 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8721 continue;
8722 }
8723 ++DepCounter;
8724 // OpenMP [2.13.9, Summary]
8725 // depend(dependence-type : vec), where dependence-type is:
8726 // 'sink' and where vec is the iteration vector, which has the form:
8727 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8728 // where n is the value specified by the ordered clause in the loop
8729 // directive, xi denotes the loop iteration variable of the i-th nested
8730 // loop associated with the loop directive, and di is a constant
8731 // non-negative integer.
8732 SimpleExpr = SimpleExpr->IgnoreImplicit();
8733 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8734 if (!DE) {
8735 OverloadedOperatorKind OOK = OO_None;
8736 SourceLocation OOLoc;
8737 Expr *LHS, *RHS;
8738 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8739 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8740 OOLoc = BO->getOperatorLoc();
8741 LHS = BO->getLHS()->IgnoreParenImpCasts();
8742 RHS = BO->getRHS()->IgnoreParenImpCasts();
8743 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8744 OOK = OCE->getOperator();
8745 OOLoc = OCE->getOperatorLoc();
8746 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8747 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8748 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8749 OOK = MCE->getMethodDecl()
8750 ->getNameInfo()
8751 .getName()
8752 .getCXXOverloadedOperator();
8753 OOLoc = MCE->getCallee()->getExprLoc();
8754 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8755 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8756 } else {
8757 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8758 continue;
8759 }
8760 DE = dyn_cast<DeclRefExpr>(LHS);
8761 if (!DE) {
8762 Diag(LHS->getExprLoc(),
8763 diag::err_omp_depend_sink_expected_loop_iteration)
8764 << DSAStack->getParentLoopControlVariable(
8765 DepCounter.getZExtValue());
8766 continue;
8767 }
8768 if (OOK != OO_Plus && OOK != OO_Minus) {
8769 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8770 continue;
8771 }
8772 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8773 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8774 if (Res.isInvalid())
8775 continue;
8776 }
8777 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8778 if (!CurContext->isDependentContext() &&
8779 DSAStack->getParentOrderedRegionParam() &&
8780 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8781 Diag(DE->getExprLoc(),
8782 diag::err_omp_depend_sink_expected_loop_iteration)
8783 << DSAStack->getParentLoopControlVariable(
8784 DepCounter.getZExtValue());
8785 continue;
8786 }
8787 } else {
8788 // OpenMP [2.11.1.1, Restrictions, p.3]
8789 // A variable that is part of another variable (such as a field of a
8790 // structure) but is not an array element or an array section cannot
8791 // appear in a depend clause.
8792 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8793 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8794 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8795 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8796 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008797 (ASE &&
8798 !ASE->getBase()
8799 ->getType()
8800 .getNonReferenceType()
8801 ->isPointerType() &&
8802 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008803 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8804 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008805 continue;
8806 }
8807 }
8808
8809 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8810 }
8811
8812 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8813 TotalDepCount > VarList.size() &&
8814 DSAStack->getParentOrderedRegionParam()) {
8815 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8816 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8817 }
8818 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8819 Vars.empty())
8820 return nullptr;
8821 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008822
8823 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8824 DepLoc, ColonLoc, Vars);
8825}
Michael Wonge710d542015-08-07 16:16:36 +00008826
8827OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8828 SourceLocation LParenLoc,
8829 SourceLocation EndLoc) {
8830 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008831
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008832 // OpenMP [2.9.1, Restrictions]
8833 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008834 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8835 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008836 return nullptr;
8837
Michael Wonge710d542015-08-07 16:16:36 +00008838 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8839}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008840
8841static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8842 DSAStackTy *Stack, CXXRecordDecl *RD) {
8843 if (!RD || RD->isInvalidDecl())
8844 return true;
8845
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008846 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8847 if (auto *CTD = CTSD->getSpecializedTemplate())
8848 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008849 auto QTy = SemaRef.Context.getRecordType(RD);
8850 if (RD->isDynamicClass()) {
8851 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8852 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8853 return false;
8854 }
8855 auto *DC = RD;
8856 bool IsCorrect = true;
8857 for (auto *I : DC->decls()) {
8858 if (I) {
8859 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8860 if (MD->isStatic()) {
8861 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8862 SemaRef.Diag(MD->getLocation(),
8863 diag::note_omp_static_member_in_target);
8864 IsCorrect = false;
8865 }
8866 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8867 if (VD->isStaticDataMember()) {
8868 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8869 SemaRef.Diag(VD->getLocation(),
8870 diag::note_omp_static_member_in_target);
8871 IsCorrect = false;
8872 }
8873 }
8874 }
8875 }
8876
8877 for (auto &I : RD->bases()) {
8878 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8879 I.getType()->getAsCXXRecordDecl()))
8880 IsCorrect = false;
8881 }
8882 return IsCorrect;
8883}
8884
8885static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8886 DSAStackTy *Stack, QualType QTy) {
8887 NamedDecl *ND;
8888 if (QTy->isIncompleteType(&ND)) {
8889 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8890 return false;
8891 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8892 if (!RD->isInvalidDecl() &&
8893 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8894 return false;
8895 }
8896 return true;
8897}
8898
Samuel Antao5de996e2016-01-22 20:21:36 +00008899// Return the expression of the base of the map clause or null if it cannot
8900// be determined and do all the necessary checks to see if the expression is
8901// valid as a standalone map clause expression.
8902static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8903 SourceLocation ELoc = E->getExprLoc();
8904 SourceRange ERange = E->getSourceRange();
8905
8906 // The base of elements of list in a map clause have to be either:
8907 // - a reference to variable or field.
8908 // - a member expression.
8909 // - an array expression.
8910 //
8911 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8912 // reference to 'r'.
8913 //
8914 // If we have:
8915 //
8916 // struct SS {
8917 // Bla S;
8918 // foo() {
8919 // #pragma omp target map (S.Arr[:12]);
8920 // }
8921 // }
8922 //
8923 // We want to retrieve the member expression 'this->S';
8924
8925 Expr *RelevantExpr = nullptr;
8926
8927 // Flags to help capture some memory
8928
8929 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8930 // If a list item is an array section, it must specify contiguous storage.
8931 //
8932 // For this restriction it is sufficient that we make sure only references
8933 // to variables or fields and array expressions, and that no array sections
8934 // exist except in the rightmost expression. E.g. these would be invalid:
8935 //
8936 // r.ArrS[3:5].Arr[6:7]
8937 //
8938 // r.ArrS[3:5].x
8939 //
8940 // but these would be valid:
8941 // r.ArrS[3].Arr[6:7]
8942 //
8943 // r.ArrS[3].x
8944
8945 bool IsRightMostExpression = true;
8946
8947 while (!RelevantExpr) {
8948 auto AllowArraySection = IsRightMostExpression;
8949 IsRightMostExpression = false;
8950
8951 E = E->IgnoreParenImpCasts();
8952
8953 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8954 if (!isa<VarDecl>(CurE->getDecl()))
8955 break;
8956
8957 RelevantExpr = CurE;
8958 continue;
8959 }
8960
8961 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8962 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8963
8964 if (isa<CXXThisExpr>(BaseE))
8965 // We found a base expression: this->Val.
8966 RelevantExpr = CurE;
8967 else
8968 E = BaseE;
8969
8970 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8971 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8972 << CurE->getSourceRange();
8973 break;
8974 }
8975
8976 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8977
8978 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8979 // A bit-field cannot appear in a map clause.
8980 //
8981 if (FD->isBitField()) {
8982 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8983 << CurE->getSourceRange();
8984 break;
8985 }
8986
8987 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8988 // If the type of a list item is a reference to a type T then the type
8989 // will be considered to be T for all purposes of this clause.
8990 QualType CurType = BaseE->getType();
8991 if (CurType->isReferenceType())
8992 CurType = CurType->getPointeeType();
8993
8994 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8995 // A list item cannot be a variable that is a member of a structure with
8996 // a union type.
8997 //
8998 if (auto *RT = CurType->getAs<RecordType>())
8999 if (RT->isUnionType()) {
9000 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9001 << CurE->getSourceRange();
9002 break;
9003 }
9004
9005 continue;
9006 }
9007
9008 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9009 E = CurE->getBase()->IgnoreParenImpCasts();
9010
9011 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9012 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9013 << 0 << CurE->getSourceRange();
9014 break;
9015 }
9016 continue;
9017 }
9018
9019 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9020 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9021 // If a list item is an element of a structure, only the rightmost symbol
9022 // of the variable reference can be an array section.
9023 //
9024 if (!AllowArraySection) {
9025 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9026 << CurE->getSourceRange();
9027 break;
9028 }
9029
9030 E = CurE->getBase()->IgnoreParenImpCasts();
9031
9032 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9033 // If the type of a list item is a reference to a type T then the type
9034 // will be considered to be T for all purposes of this clause.
9035 QualType CurType = E->getType();
9036 if (CurType->isReferenceType())
9037 CurType = CurType->getPointeeType();
9038
9039 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9040 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9041 << 0 << CurE->getSourceRange();
9042 break;
9043 }
9044
9045 continue;
9046 }
9047
9048 // If nothing else worked, this is not a valid map clause expression.
9049 SemaRef.Diag(ELoc,
9050 diag::err_omp_expected_named_var_member_or_array_expression)
9051 << ERange;
9052 break;
9053 }
9054
9055 return RelevantExpr;
9056}
9057
9058// Return true if expression E associated with value VD has conflicts with other
9059// map information.
9060static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9061 Expr *E, bool CurrentRegionOnly) {
9062 assert(VD && E);
9063
9064 // Types used to organize the components of a valid map clause.
9065 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9066 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9067
9068 // Helper to extract the components in the map clause expression E and store
9069 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9070 // it has already passed the single clause checks.
9071 auto ExtractMapExpressionComponents = [](Expr *TE,
9072 MapExpressionComponents &MEC) {
9073 while (true) {
9074 TE = TE->IgnoreParenImpCasts();
9075
9076 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9077 MEC.push_back(
9078 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9079 break;
9080 }
9081
9082 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9083 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9084
9085 MEC.push_back(MapExpressionComponent(
9086 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9087 if (isa<CXXThisExpr>(BaseE))
9088 break;
9089
9090 TE = BaseE;
9091 continue;
9092 }
9093
9094 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9095 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9096 TE = CurE->getBase()->IgnoreParenImpCasts();
9097 continue;
9098 }
9099
9100 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9101 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9102 TE = CurE->getBase()->IgnoreParenImpCasts();
9103 continue;
9104 }
9105
9106 llvm_unreachable(
9107 "Expecting only valid map clause expressions at this point!");
9108 }
9109 };
9110
9111 SourceLocation ELoc = E->getExprLoc();
9112 SourceRange ERange = E->getSourceRange();
9113
9114 // In order to easily check the conflicts we need to match each component of
9115 // the expression under test with the components of the expressions that are
9116 // already in the stack.
9117
9118 MapExpressionComponents CurComponents;
9119 ExtractMapExpressionComponents(E, CurComponents);
9120
9121 assert(!CurComponents.empty() && "Map clause expression with no components!");
9122 assert(CurComponents.back().second == VD &&
9123 "Map clause expression with unexpected base!");
9124
9125 // Variables to help detecting enclosing problems in data environment nests.
9126 bool IsEnclosedByDataEnvironmentExpr = false;
9127 Expr *EnclosingExpr = nullptr;
9128
9129 bool FoundError =
9130 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9131 MapExpressionComponents StackComponents;
9132 ExtractMapExpressionComponents(RE, StackComponents);
9133 assert(!StackComponents.empty() &&
9134 "Map clause expression with no components!");
9135 assert(StackComponents.back().second == VD &&
9136 "Map clause expression with unexpected base!");
9137
9138 // Expressions must start from the same base. Here we detect at which
9139 // point both expressions diverge from each other and see if we can
9140 // detect if the memory referred to both expressions is contiguous and
9141 // do not overlap.
9142 auto CI = CurComponents.rbegin();
9143 auto CE = CurComponents.rend();
9144 auto SI = StackComponents.rbegin();
9145 auto SE = StackComponents.rend();
9146 for (; CI != CE && SI != SE; ++CI, ++SI) {
9147
9148 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9149 // At most one list item can be an array item derived from a given
9150 // variable in map clauses of the same construct.
9151 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9152 isa<OMPArraySectionExpr>(CI->first)) &&
9153 (isa<ArraySubscriptExpr>(SI->first) ||
9154 isa<OMPArraySectionExpr>(SI->first))) {
9155 SemaRef.Diag(CI->first->getExprLoc(),
9156 diag::err_omp_multiple_array_items_in_map_clause)
9157 << CI->first->getSourceRange();
9158 ;
9159 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9160 << SI->first->getSourceRange();
9161 return true;
9162 }
9163
9164 // Do both expressions have the same kind?
9165 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9166 break;
9167
9168 // Are we dealing with different variables/fields?
9169 if (CI->second != SI->second)
9170 break;
9171 }
9172
9173 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9174 // List items of map clauses in the same construct must not share
9175 // original storage.
9176 //
9177 // If the expressions are exactly the same or one is a subset of the
9178 // other, it means they are sharing storage.
9179 if (CI == CE && SI == SE) {
9180 if (CurrentRegionOnly) {
9181 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9182 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9183 << RE->getSourceRange();
9184 return true;
9185 } else {
9186 // If we find the same expression in the enclosing data environment,
9187 // that is legal.
9188 IsEnclosedByDataEnvironmentExpr = true;
9189 return false;
9190 }
9191 }
9192
9193 QualType DerivedType = std::prev(CI)->first->getType();
9194 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9195
9196 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9197 // If the type of a list item is a reference to a type T then the type
9198 // will be considered to be T for all purposes of this clause.
9199 if (DerivedType->isReferenceType())
9200 DerivedType = DerivedType->getPointeeType();
9201
9202 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9203 // A variable for which the type is pointer and an array section
9204 // derived from that variable must not appear as list items of map
9205 // clauses of the same construct.
9206 //
9207 // Also, cover one of the cases in:
9208 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9209 // If any part of the original storage of a list item has corresponding
9210 // storage in the device data environment, all of the original storage
9211 // must have corresponding storage in the device data environment.
9212 //
9213 if (DerivedType->isAnyPointerType()) {
9214 if (CI == CE || SI == SE) {
9215 SemaRef.Diag(
9216 DerivedLoc,
9217 diag::err_omp_pointer_mapped_along_with_derived_section)
9218 << DerivedLoc;
9219 } else {
9220 assert(CI != CE && SI != SE);
9221 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9222 << DerivedLoc;
9223 }
9224 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9225 << RE->getSourceRange();
9226 return true;
9227 }
9228
9229 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9230 // List items of map clauses in the same construct must not share
9231 // original storage.
9232 //
9233 // An expression is a subset of the other.
9234 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9235 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9236 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9237 << RE->getSourceRange();
9238 return true;
9239 }
9240
9241 // The current expression uses the same base as other expression in the
9242 // data environment but does not contain it completelly.
9243 if (!CurrentRegionOnly && SI != SE)
9244 EnclosingExpr = RE;
9245
9246 // The current expression is a subset of the expression in the data
9247 // environment.
9248 IsEnclosedByDataEnvironmentExpr |=
9249 (!CurrentRegionOnly && CI != CE && SI == SE);
9250
9251 return false;
9252 });
9253
9254 if (CurrentRegionOnly)
9255 return FoundError;
9256
9257 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9258 // If any part of the original storage of a list item has corresponding
9259 // storage in the device data environment, all of the original storage must
9260 // have corresponding storage in the device data environment.
9261 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9262 // If a list item is an element of a structure, and a different element of
9263 // the structure has a corresponding list item in the device data environment
9264 // prior to a task encountering the construct associated with the map clause,
9265 // then the list item must also have a correspnding list item in the device
9266 // data environment prior to the task encountering the construct.
9267 //
9268 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9269 SemaRef.Diag(ELoc,
9270 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9271 << ERange;
9272 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9273 << EnclosingExpr->getSourceRange();
9274 return true;
9275 }
9276
9277 return FoundError;
9278}
9279
Samuel Antao23abd722016-01-19 20:40:49 +00009280OMPClause *
9281Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9282 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9283 SourceLocation MapLoc, SourceLocation ColonLoc,
9284 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9285 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009286 SmallVector<Expr *, 4> Vars;
9287
9288 for (auto &RE : VarList) {
9289 assert(RE && "Null expr in omp map");
9290 if (isa<DependentScopeDeclRefExpr>(RE)) {
9291 // It will be analyzed later.
9292 Vars.push_back(RE);
9293 continue;
9294 }
9295 SourceLocation ELoc = RE->getExprLoc();
9296
Kelvin Li0bff7af2015-11-23 05:32:03 +00009297 auto *VE = RE->IgnoreParenLValueCasts();
9298
9299 if (VE->isValueDependent() || VE->isTypeDependent() ||
9300 VE->isInstantiationDependent() ||
9301 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009302 // We can only analyze this information once the missing information is
9303 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009304 Vars.push_back(RE);
9305 continue;
9306 }
9307
9308 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009309
Samuel Antao5de996e2016-01-22 20:21:36 +00009310 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9311 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9312 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009313 continue;
9314 }
9315
Samuel Antao5de996e2016-01-22 20:21:36 +00009316 // Obtain the array or member expression bases if required.
9317 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9318 if (!BE)
9319 continue;
9320
9321 // If the base is a reference to a variable, we rely on that variable for
9322 // the following checks. If it is a 'this' expression we rely on the field.
9323 ValueDecl *D = nullptr;
9324 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9325 D = DRE->getDecl();
9326 } else {
9327 auto *ME = cast<MemberExpr>(BE);
9328 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9329 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009330 }
9331 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009332
Samuel Antao5de996e2016-01-22 20:21:36 +00009333 auto *VD = dyn_cast<VarDecl>(D);
9334 auto *FD = dyn_cast<FieldDecl>(D);
9335
9336 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009337 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009338
9339 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9340 // threadprivate variables cannot appear in a map clause.
9341 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009342 auto DVar = DSAStack->getTopDSA(VD, false);
9343 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9344 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9345 continue;
9346 }
9347
Samuel Antao5de996e2016-01-22 20:21:36 +00009348 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9349 // A list item cannot appear in both a map clause and a data-sharing
9350 // attribute clause on the same construct.
9351 //
9352 // TODO: Implement this check - it cannot currently be tested because of
9353 // missing implementation of the other data sharing clauses in target
9354 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009355
Samuel Antao5de996e2016-01-22 20:21:36 +00009356 // Check conflicts with other map clause expressions. We check the conflicts
9357 // with the current construct separately from the enclosing data
9358 // environment, because the restrictions are different.
9359 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9360 /*CurrentRegionOnly=*/true))
9361 break;
9362 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9363 /*CurrentRegionOnly=*/false))
9364 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009365
Samuel Antao5de996e2016-01-22 20:21:36 +00009366 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9367 // If the type of a list item is a reference to a type T then the type will
9368 // be considered to be T for all purposes of this clause.
9369 QualType Type = D->getType();
9370 if (Type->isReferenceType())
9371 Type = Type->getPointeeType();
9372
9373 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009374 // A list item must have a mappable type.
9375 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9376 DSAStack, Type))
9377 continue;
9378
Samuel Antaodf67fc42016-01-19 19:15:56 +00009379 // target enter data
9380 // OpenMP [2.10.2, Restrictions, p. 99]
9381 // A map-type must be specified in all map clauses and must be either
9382 // to or alloc.
9383 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9384 if (DKind == OMPD_target_enter_data &&
9385 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9386 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009387 << (IsMapTypeImplicit ? 1 : 0)
9388 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009389 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009390 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009391 }
9392
Samuel Antao72590762016-01-19 20:04:50 +00009393 // target exit_data
9394 // OpenMP [2.10.3, Restrictions, p. 102]
9395 // A map-type must be specified in all map clauses and must be either
9396 // from, release, or delete.
9397 DKind = DSAStack->getCurrentDirective();
9398 if (DKind == OMPD_target_exit_data &&
9399 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9400 MapType == OMPC_MAP_delete)) {
9401 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009402 << (IsMapTypeImplicit ? 1 : 0)
9403 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009404 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009405 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009406 }
9407
Kelvin Li0bff7af2015-11-23 05:32:03 +00009408 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009409 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009410 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009411
Samuel Antao5de996e2016-01-22 20:21:36 +00009412 // We need to produce a map clause even if we don't have variables so that
9413 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009414 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009415 MapTypeModifier, MapType, IsMapTypeImplicit,
9416 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009417}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009418
9419OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9420 SourceLocation StartLoc,
9421 SourceLocation LParenLoc,
9422 SourceLocation EndLoc) {
9423 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009424
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009425 // OpenMP [teams Constrcut, Restrictions]
9426 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009427 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9428 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009429 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009430
9431 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9432}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009433
9434OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9435 SourceLocation StartLoc,
9436 SourceLocation LParenLoc,
9437 SourceLocation EndLoc) {
9438 Expr *ValExpr = ThreadLimit;
9439
9440 // OpenMP [teams Constrcut, Restrictions]
9441 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009442 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9443 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009444 return nullptr;
9445
9446 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9447 EndLoc);
9448}
Alexey Bataeva0569352015-12-01 10:17:31 +00009449
9450OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9451 SourceLocation StartLoc,
9452 SourceLocation LParenLoc,
9453 SourceLocation EndLoc) {
9454 Expr *ValExpr = Priority;
9455
9456 // OpenMP [2.9.1, task Constrcut]
9457 // The priority-value is a non-negative numerical scalar expression.
9458 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9459 /*StrictlyPositive=*/false))
9460 return nullptr;
9461
9462 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9463}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009464
9465OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9466 SourceLocation StartLoc,
9467 SourceLocation LParenLoc,
9468 SourceLocation EndLoc) {
9469 Expr *ValExpr = Grainsize;
9470
9471 // OpenMP [2.9.2, taskloop Constrcut]
9472 // The parameter of the grainsize clause must be a positive integer
9473 // expression.
9474 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9475 /*StrictlyPositive=*/true))
9476 return nullptr;
9477
9478 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9479}
Alexey Bataev382967a2015-12-08 12:06:20 +00009480
9481OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9482 SourceLocation StartLoc,
9483 SourceLocation LParenLoc,
9484 SourceLocation EndLoc) {
9485 Expr *ValExpr = NumTasks;
9486
9487 // OpenMP [2.9.2, taskloop Constrcut]
9488 // The parameter of the num_tasks clause must be a positive integer
9489 // expression.
9490 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9491 /*StrictlyPositive=*/true))
9492 return nullptr;
9493
9494 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9495}
9496
Alexey Bataev28c75412015-12-15 08:19:24 +00009497OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9498 SourceLocation LParenLoc,
9499 SourceLocation EndLoc) {
9500 // OpenMP [2.13.2, critical construct, Description]
9501 // ... where hint-expression is an integer constant expression that evaluates
9502 // to a valid lock hint.
9503 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9504 if (HintExpr.isInvalid())
9505 return nullptr;
9506 return new (Context)
9507 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9508}
9509
Carlo Bertollib4adf552016-01-15 18:50:31 +00009510OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9511 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9512 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9513 SourceLocation EndLoc) {
9514 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9515 std::string Values;
9516 Values += "'";
9517 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9518 Values += "'";
9519 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9520 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9521 return nullptr;
9522 }
9523 Expr *ValExpr = ChunkSize;
9524 Expr *HelperValExpr = nullptr;
9525 if (ChunkSize) {
9526 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9527 !ChunkSize->isInstantiationDependent() &&
9528 !ChunkSize->containsUnexpandedParameterPack()) {
9529 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9530 ExprResult Val =
9531 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9532 if (Val.isInvalid())
9533 return nullptr;
9534
9535 ValExpr = Val.get();
9536
9537 // OpenMP [2.7.1, Restrictions]
9538 // chunk_size must be a loop invariant integer expression with a positive
9539 // value.
9540 llvm::APSInt Result;
9541 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9542 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9543 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9544 << "dist_schedule" << ChunkSize->getSourceRange();
9545 return nullptr;
9546 }
9547 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9548 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9549 ChunkSize->getType(), ".chunk.");
9550 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9551 ChunkSize->getExprLoc(),
9552 /*RefersToCapture=*/true);
9553 HelperValExpr = ImpVarRef;
9554 }
9555 }
9556 }
9557
9558 return new (Context)
9559 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9560 Kind, ValExpr, HelperValExpr);
9561}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009562
9563OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9564 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9565 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9566 SourceLocation KindLoc, SourceLocation EndLoc) {
9567 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9568 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9569 Kind != OMPC_DEFAULTMAP_scalar) {
9570 std::string Value;
9571 SourceLocation Loc;
9572 Value += "'";
9573 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9574 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9575 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9576 Loc = MLoc;
9577 } else {
9578 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9579 OMPC_DEFAULTMAP_scalar);
9580 Loc = KindLoc;
9581 }
9582 Value += "'";
9583 Diag(Loc, diag::err_omp_unexpected_clause_value)
9584 << Value << getOpenMPClauseName(OMPC_defaultmap);
9585 return nullptr;
9586 }
9587
9588 return new (Context)
9589 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9590}