blob: 3ba5daa03042f246562acf320e6335e99bef252c [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 Bataev376b4a42016-02-09 09:41:09 +00001182 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1183 SourceLocation(), VD,
1184 /*RefersToEnclosingVariableOrCapture=*/false,
1185 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001186}
1187
Alexey Bataeved09d242014-05-28 05:53:51 +00001188Sema::DeclGroupPtrTy
1189Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1190 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001191 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001192 CurContext->addDecl(D);
1193 return DeclGroupPtrTy::make(DeclGroupRef(D));
1194 }
David Blaikie0403cb12016-01-15 23:43:25 +00001195 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001196}
1197
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001198namespace {
1199class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1200 Sema &SemaRef;
1201
1202public:
1203 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1204 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1205 if (VD->hasLocalStorage()) {
1206 SemaRef.Diag(E->getLocStart(),
1207 diag::err_omp_local_var_in_threadprivate_init)
1208 << E->getSourceRange();
1209 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1210 << VD << VD->getSourceRange();
1211 return true;
1212 }
1213 }
1214 return false;
1215 }
1216 bool VisitStmt(const Stmt *S) {
1217 for (auto Child : S->children()) {
1218 if (Child && Visit(Child))
1219 return true;
1220 }
1221 return false;
1222 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001223 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001224};
1225} // namespace
1226
Alexey Bataeved09d242014-05-28 05:53:51 +00001227OMPThreadPrivateDecl *
1228Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001229 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001230 for (auto &RefExpr : VarList) {
1231 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001232 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1233 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001234
Alexey Bataev376b4a42016-02-09 09:41:09 +00001235 // Mark variable as used.
1236 VD->setReferenced();
1237 VD->markUsed(Context);
1238
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001239 QualType QType = VD->getType();
1240 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1241 // It will be analyzed later.
1242 Vars.push_back(DE);
1243 continue;
1244 }
1245
Alexey Bataeva769e072013-03-22 06:34:35 +00001246 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1247 // A threadprivate variable must not have an incomplete type.
1248 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001249 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001250 continue;
1251 }
1252
1253 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1254 // A threadprivate variable must not have a reference type.
1255 if (VD->getType()->isReferenceType()) {
1256 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001257 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1258 bool IsDecl =
1259 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1260 Diag(VD->getLocation(),
1261 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1262 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001263 continue;
1264 }
1265
Samuel Antaof8b50122015-07-13 22:54:53 +00001266 // Check if this is a TLS variable. If TLS is not being supported, produce
1267 // the corresponding diagnostic.
1268 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1269 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1270 getLangOpts().OpenMPUseTLS &&
1271 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001272 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1273 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001274 Diag(ILoc, diag::err_omp_var_thread_local)
1275 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001276 bool IsDecl =
1277 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1278 Diag(VD->getLocation(),
1279 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1280 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001281 continue;
1282 }
1283
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001284 // Check if initial value of threadprivate variable reference variable with
1285 // local storage (it is not supported by runtime).
1286 if (auto Init = VD->getAnyInitializer()) {
1287 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001288 if (Checker.Visit(Init))
1289 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001290 }
1291
Alexey Bataeved09d242014-05-28 05:53:51 +00001292 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001293 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001294 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1295 Context, SourceRange(Loc, Loc)));
1296 if (auto *ML = Context.getASTMutationListener())
1297 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001298 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001299 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001300 if (!Vars.empty()) {
1301 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1302 Vars);
1303 D->setAccess(AS_public);
1304 }
1305 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001306}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001307
Alexey Bataev7ff55242014-06-19 09:13:45 +00001308static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001309 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310 bool IsLoopIterVar = false) {
1311 if (DVar.RefExpr) {
1312 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1313 << getOpenMPClauseName(DVar.CKind);
1314 return;
1315 }
1316 enum {
1317 PDSA_StaticMemberShared,
1318 PDSA_StaticLocalVarShared,
1319 PDSA_LoopIterVarPrivate,
1320 PDSA_LoopIterVarLinear,
1321 PDSA_LoopIterVarLastprivate,
1322 PDSA_ConstVarShared,
1323 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001324 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001325 PDSA_LocalVarPrivate,
1326 PDSA_Implicit
1327 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001328 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001329 auto ReportLoc = D->getLocation();
1330 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001331 if (IsLoopIterVar) {
1332 if (DVar.CKind == OMPC_private)
1333 Reason = PDSA_LoopIterVarPrivate;
1334 else if (DVar.CKind == OMPC_lastprivate)
1335 Reason = PDSA_LoopIterVarLastprivate;
1336 else
1337 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1339 Reason = PDSA_TaskVarFirstprivate;
1340 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001341 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001342 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001343 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001344 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001345 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001346 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001347 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001348 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001349 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001350 ReportHint = true;
1351 Reason = PDSA_LocalVarPrivate;
1352 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001353 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001354 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001355 << Reason << ReportHint
1356 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1357 } else if (DVar.ImplicitDSALoc.isValid()) {
1358 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1359 << getOpenMPClauseName(DVar.CKind);
1360 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001361}
1362
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363namespace {
1364class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1365 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001366 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001367 bool ErrorFound;
1368 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001369 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001370 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001371
Alexey Bataev758e55e2013-09-06 18:03:48 +00001372public:
1373 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001374 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001375 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001376 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1377 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001378
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001379 auto DVar = Stack->getTopDSA(VD, false);
1380 // Check if the variable has explicit DSA set and stop analysis if it so.
1381 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001382
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001383 auto ELoc = E->getExprLoc();
1384 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001385 // The default(none) clause requires that each variable that is referenced
1386 // in the construct, and does not have a predetermined data-sharing
1387 // attribute, must have its data-sharing attribute explicitly determined
1388 // by being listed in a data-sharing attribute clause.
1389 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001390 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001391 VarsWithInheritedDSA.count(VD) == 0) {
1392 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001393 return;
1394 }
1395
1396 // OpenMP [2.9.3.6, Restrictions, p.2]
1397 // A list item that appears in a reduction clause of the innermost
1398 // enclosing worksharing or parallel construct may not be accessed in an
1399 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001400 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001401 [](OpenMPDirectiveKind K) -> bool {
1402 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001403 isOpenMPWorksharingDirective(K) ||
1404 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001405 },
1406 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001407 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1408 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001409 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1410 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001411 return;
1412 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001413
1414 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001415 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001416 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001417 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001418 }
1419 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001420 void VisitMemberExpr(MemberExpr *E) {
1421 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1422 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1423 auto DVar = Stack->getTopDSA(FD, false);
1424 // Check if the variable has explicit DSA set and stop analysis if it
1425 // so.
1426 if (DVar.RefExpr)
1427 return;
1428
1429 auto ELoc = E->getExprLoc();
1430 auto DKind = Stack->getCurrentDirective();
1431 // OpenMP [2.9.3.6, Restrictions, p.2]
1432 // A list item that appears in a reduction clause of the innermost
1433 // enclosing worksharing or parallel construct may not be accessed in
1434 // an
1435 // explicit task.
1436 DVar =
1437 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1438 [](OpenMPDirectiveKind K) -> bool {
1439 return isOpenMPParallelDirective(K) ||
1440 isOpenMPWorksharingDirective(K) ||
1441 isOpenMPTeamsDirective(K);
1442 },
1443 false);
1444 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1445 ErrorFound = true;
1446 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1447 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1448 return;
1449 }
1450
1451 // Define implicit data-sharing attributes for task.
1452 DVar = Stack->getImplicitDSA(FD, false);
1453 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1454 ImplicitFirstprivate.push_back(E);
1455 }
1456 }
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 for (auto *C : S->clauses()) {
1460 // Skip analysis of arguments of implicitly defined firstprivate clause
1461 // for task directives.
1462 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1463 for (auto *CC : C->children()) {
1464 if (CC)
1465 Visit(CC);
1466 }
1467 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 }
1469 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 for (auto *C : S->children()) {
1471 if (C && !isa<OMPExecutableDirective>(C))
1472 Visit(C);
1473 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
1476 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001477 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001478 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001479 return VarsWithInheritedDSA;
1480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev7ff55242014-06-19 09:13:45 +00001482 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1483 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484};
Alexey Bataeved09d242014-05-28 05:53:51 +00001485} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486
Alexey Bataevbae9a792014-06-27 10:37:06 +00001487void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001488 switch (DKind) {
1489 case OMPD_parallel: {
1490 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001491 QualType KmpInt32PtrTy =
1492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 std::make_pair(".global_tid.", KmpInt32PtrTy),
1495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1496 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001500 break;
1501 }
1502 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001503 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001504 std::make_pair(StringRef(), QualType()) // __context with shared vars
1505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001508 break;
1509 }
1510 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001511 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001512 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001513 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001516 break;
1517 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001518 case OMPD_for_simd: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
1522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
1524 break;
1525 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001526 case OMPD_sections: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001532 break;
1533 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001534 case OMPD_section: {
1535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(StringRef(), QualType()) // __context with shared vars
1537 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001538 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1539 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001540 break;
1541 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542 case OMPD_single: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001548 break;
1549 }
Alexander Musman80c22892014-07-17 08:54:58 +00001550 case OMPD_master: {
1551 Sema::CapturedParamNameType Params[] = {
1552 std::make_pair(StringRef(), QualType()) // __context with shared vars
1553 };
1554 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1555 Params);
1556 break;
1557 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001558 case OMPD_critical: {
1559 Sema::CapturedParamNameType Params[] = {
1560 std::make_pair(StringRef(), QualType()) // __context with shared vars
1561 };
1562 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1563 Params);
1564 break;
1565 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001566 case OMPD_parallel_for: {
1567 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001568 QualType KmpInt32PtrTy =
1569 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 Sema::CapturedParamNameType Params[] = {
1571 std::make_pair(".global_tid.", KmpInt32PtrTy),
1572 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1573 std::make_pair(StringRef(), QualType()) // __context with shared vars
1574 };
1575 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1576 Params);
1577 break;
1578 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001579 case OMPD_parallel_for_simd: {
1580 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001581 QualType KmpInt32PtrTy =
1582 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 Sema::CapturedParamNameType Params[] = {
1584 std::make_pair(".global_tid.", KmpInt32PtrTy),
1585 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1586 std::make_pair(StringRef(), QualType()) // __context with shared vars
1587 };
1588 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1589 Params);
1590 break;
1591 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001592 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001593 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001594 QualType KmpInt32PtrTy =
1595 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001597 std::make_pair(".global_tid.", KmpInt32PtrTy),
1598 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001599 std::make_pair(StringRef(), QualType()) // __context with shared vars
1600 };
1601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1602 Params);
1603 break;
1604 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001605 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001606 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001607 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1608 FunctionProtoType::ExtProtoInfo EPI;
1609 EPI.Variadic = true;
1610 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001612 std::make_pair(".global_tid.", KmpInt32Ty),
1613 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001614 std::make_pair(".privates.",
1615 Context.VoidPtrTy.withConst().withRestrict()),
1616 std::make_pair(
1617 ".copy_fn.",
1618 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001619 std::make_pair(StringRef(), QualType()) // __context with shared vars
1620 };
1621 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1622 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001623 // Mark this captured region as inlined, because we don't use outlined
1624 // function directly.
1625 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1626 AlwaysInlineAttr::CreateImplicit(
1627 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001628 break;
1629 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001630 case OMPD_ordered: {
1631 Sema::CapturedParamNameType Params[] = {
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001638 case OMPD_atomic: {
1639 Sema::CapturedParamNameType Params[] = {
1640 std::make_pair(StringRef(), QualType()) // __context with shared vars
1641 };
1642 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1643 Params);
1644 break;
1645 }
Michael Wong65f367f2015-07-21 13:44:28 +00001646 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001647 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001648 case OMPD_target_parallel:
1649 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001650 Sema::CapturedParamNameType Params[] = {
1651 std::make_pair(StringRef(), QualType()) // __context with shared vars
1652 };
1653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1654 Params);
1655 break;
1656 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 case OMPD_teams: {
1658 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001659 QualType KmpInt32PtrTy =
1660 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001661 Sema::CapturedParamNameType Params[] = {
1662 std::make_pair(".global_tid.", KmpInt32PtrTy),
1663 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001670 case OMPD_taskgroup: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001678 case OMPD_taskloop: {
1679 Sema::CapturedParamNameType Params[] = {
1680 std::make_pair(StringRef(), QualType()) // __context with shared vars
1681 };
1682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1683 Params);
1684 break;
1685 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001686 case OMPD_taskloop_simd: {
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(StringRef(), QualType()) // __context with shared vars
1689 };
1690 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1691 Params);
1692 break;
1693 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001694 case OMPD_distribute: {
1695 Sema::CapturedParamNameType Params[] = {
1696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001702 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001703 case OMPD_taskyield:
1704 case OMPD_barrier:
1705 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001706 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001707 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001708 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001709 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001710 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001711 llvm_unreachable("OpenMP Directive is not allowed");
1712 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001713 llvm_unreachable("Unknown OpenMP directive");
1714 }
1715}
1716
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001717StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1718 ArrayRef<OMPClause *> Clauses) {
1719 if (!S.isUsable()) {
1720 ActOnCapturedRegionError();
1721 return StmtError();
1722 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001723
1724 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001725 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001726 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001727 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001728 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001729 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001730 Clause->getClauseKind() == OMPC_copyprivate ||
1731 (getLangOpts().OpenMPUseTLS &&
1732 getASTContext().getTargetInfo().isTLSSupported() &&
1733 Clause->getClauseKind() == OMPC_copyin)) {
1734 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001735 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001736 for (auto *VarRef : Clause->children()) {
1737 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001738 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001739 }
1740 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001741 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001742 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1743 Clause->getClauseKind() == OMPC_schedule) {
1744 // Mark all variables in private list clauses as used in inner region.
1745 // Required for proper codegen of combined directives.
1746 // TODO: add processing for other clauses.
1747 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001748 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1749 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001750 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001751 if (Clause->getClauseKind() == OMPC_schedule)
1752 SC = cast<OMPScheduleClause>(Clause);
1753 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001754 OC = cast<OMPOrderedClause>(Clause);
1755 else if (Clause->getClauseKind() == OMPC_linear)
1756 LCs.push_back(cast<OMPLinearClause>(Clause));
1757 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001758 bool ErrorFound = false;
1759 // OpenMP, 2.7.1 Loop Construct, Restrictions
1760 // The nonmonotonic modifier cannot be specified if an ordered clause is
1761 // specified.
1762 if (SC &&
1763 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1764 SC->getSecondScheduleModifier() ==
1765 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1766 OC) {
1767 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1768 ? SC->getFirstScheduleModifierLoc()
1769 : SC->getSecondScheduleModifierLoc(),
1770 diag::err_omp_schedule_nonmonotonic_ordered)
1771 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1772 ErrorFound = true;
1773 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001774 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1775 for (auto *C : LCs) {
1776 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1777 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1778 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001779 ErrorFound = true;
1780 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001781 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1782 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1783 OC->getNumForLoops()) {
1784 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1785 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1786 ErrorFound = true;
1787 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001788 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001789 ActOnCapturedRegionError();
1790 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001791 }
1792 return ActOnCapturedRegionEnd(S.get());
1793}
1794
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001795static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1796 OpenMPDirectiveKind CurrentRegion,
1797 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001798 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001799 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001800 // Allowed nesting of constructs
1801 // +------------------+-----------------+------------------------------------+
1802 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1803 // +------------------+-----------------+------------------------------------+
1804 // | parallel | parallel | * |
1805 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001806 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001807 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001808 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001809 // | parallel | simd | * |
1810 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001811 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001812 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001813 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001814 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001815 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001816 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001817 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001818 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001819 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001820 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001821 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001822 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001823 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001824 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001825 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001826 // | parallel | target parallel | * |
1827 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001828 // | parallel | target enter | * |
1829 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001830 // | parallel | target exit | * |
1831 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001832 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001833 // | parallel | cancellation | |
1834 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001835 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001836 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001837 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001838 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001839 // +------------------+-----------------+------------------------------------+
1840 // | for | parallel | * |
1841 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001842 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001843 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001844 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001845 // | for | simd | * |
1846 // | for | sections | + |
1847 // | for | section | + |
1848 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001849 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001850 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001851 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001852 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001853 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001854 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001855 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001856 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001857 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001858 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001859 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001860 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001861 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001862 // | for | target parallel | * |
1863 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001864 // | for | target enter | * |
1865 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001866 // | for | target exit | * |
1867 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001868 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001869 // | for | cancellation | |
1870 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001871 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001872 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001873 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001874 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001875 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001876 // | master | parallel | * |
1877 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001878 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001879 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001880 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001881 // | master | simd | * |
1882 // | master | sections | + |
1883 // | master | section | + |
1884 // | master | single | + |
1885 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001886 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001887 // | master |parallel sections| * |
1888 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001889 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001890 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001891 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001892 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001893 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001894 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001895 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001896 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001897 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001898 // | master | target parallel | * |
1899 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001900 // | master | target enter | * |
1901 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001902 // | master | target exit | * |
1903 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001904 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001905 // | master | cancellation | |
1906 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001907 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001908 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001909 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001910 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001911 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001912 // | critical | parallel | * |
1913 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001914 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001915 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001916 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001917 // | critical | simd | * |
1918 // | critical | sections | + |
1919 // | critical | section | + |
1920 // | critical | single | + |
1921 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001922 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001923 // | critical |parallel sections| * |
1924 // | critical | task | * |
1925 // | critical | taskyield | * |
1926 // | critical | barrier | + |
1927 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001928 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001929 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001930 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001931 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001932 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001933 // | critical | target parallel | * |
1934 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001935 // | critical | target enter | * |
1936 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001937 // | critical | target exit | * |
1938 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001939 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001940 // | critical | cancellation | |
1941 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001942 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001943 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001944 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001945 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001946 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001947 // | simd | parallel | |
1948 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001949 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001950 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001951 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001952 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001953 // | simd | sections | |
1954 // | simd | section | |
1955 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001956 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001957 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001958 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001959 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001960 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001961 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001962 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001963 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001964 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001965 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001966 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001967 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001968 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001969 // | simd | target parallel | |
1970 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001971 // | simd | target enter | |
1972 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001973 // | simd | target exit | |
1974 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001975 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001976 // | simd | cancellation | |
1977 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001978 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001979 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001980 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001981 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001982 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001983 // | for simd | parallel | |
1984 // | for simd | for | |
1985 // | for simd | for simd | |
1986 // | for simd | master | |
1987 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001988 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001989 // | for simd | sections | |
1990 // | for simd | section | |
1991 // | for simd | single | |
1992 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001993 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001994 // | for simd |parallel sections| |
1995 // | for simd | task | |
1996 // | for simd | taskyield | |
1997 // | for simd | barrier | |
1998 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001999 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002000 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002001 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002002 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002003 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002004 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002005 // | for simd | target parallel | |
2006 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002007 // | for simd | target enter | |
2008 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002009 // | for simd | target exit | |
2010 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002011 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002012 // | for simd | cancellation | |
2013 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002014 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002015 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002016 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002017 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002018 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002019 // | parallel for simd| parallel | |
2020 // | parallel for simd| for | |
2021 // | parallel for simd| for simd | |
2022 // | parallel for simd| master | |
2023 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002024 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002025 // | parallel for simd| sections | |
2026 // | parallel for simd| section | |
2027 // | parallel for simd| single | |
2028 // | parallel for simd| parallel for | |
2029 // | parallel for simd|parallel for simd| |
2030 // | parallel for simd|parallel sections| |
2031 // | parallel for simd| task | |
2032 // | parallel for simd| taskyield | |
2033 // | parallel for simd| barrier | |
2034 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002035 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002036 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002037 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002038 // | parallel for simd| atomic | |
2039 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002040 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002041 // | parallel for simd| target parallel | |
2042 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002043 // | parallel for simd| target enter | |
2044 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002045 // | parallel for simd| target exit | |
2046 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002047 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002048 // | parallel for simd| cancellation | |
2049 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002050 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002051 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002052 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002053 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002054 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002055 // | sections | parallel | * |
2056 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002057 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002058 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002059 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002060 // | sections | simd | * |
2061 // | sections | sections | + |
2062 // | sections | section | * |
2063 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002064 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002065 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002066 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002067 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002068 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002069 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002070 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002071 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002072 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002073 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002074 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002075 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002076 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002077 // | sections | target parallel | * |
2078 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002079 // | sections | target enter | * |
2080 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002081 // | sections | target exit | * |
2082 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002083 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002084 // | sections | cancellation | |
2085 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002086 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002087 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002088 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002089 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002090 // +------------------+-----------------+------------------------------------+
2091 // | section | parallel | * |
2092 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002093 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002094 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002095 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002096 // | section | simd | * |
2097 // | section | sections | + |
2098 // | section | section | + |
2099 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002100 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002101 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002102 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002103 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002104 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002105 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002106 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002107 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002108 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002109 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002110 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002111 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002112 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002113 // | section | target parallel | * |
2114 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002115 // | section | target enter | * |
2116 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002117 // | section | target exit | * |
2118 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002119 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002120 // | section | cancellation | |
2121 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002122 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002123 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002124 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002125 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002126 // +------------------+-----------------+------------------------------------+
2127 // | single | parallel | * |
2128 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002129 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002130 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002131 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002132 // | single | simd | * |
2133 // | single | sections | + |
2134 // | single | section | + |
2135 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002136 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002137 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002138 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002139 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002140 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002141 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002142 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002143 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002144 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002145 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002146 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002147 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002148 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002149 // | single | target parallel | * |
2150 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002151 // | single | target enter | * |
2152 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002153 // | single | target exit | * |
2154 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002155 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002156 // | single | cancellation | |
2157 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002158 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002159 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002160 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002161 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002162 // +------------------+-----------------+------------------------------------+
2163 // | parallel for | parallel | * |
2164 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002165 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002166 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002167 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002168 // | parallel for | simd | * |
2169 // | parallel for | sections | + |
2170 // | parallel for | section | + |
2171 // | parallel for | single | + |
2172 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002173 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002174 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002175 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002176 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002177 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002178 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002179 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002180 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002181 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002182 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002183 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002184 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002185 // | parallel for | target parallel | * |
2186 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002187 // | parallel for | target enter | * |
2188 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002189 // | parallel for | target exit | * |
2190 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002191 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002192 // | parallel for | cancellation | |
2193 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002194 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002195 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002196 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002197 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002198 // +------------------+-----------------+------------------------------------+
2199 // | parallel sections| parallel | * |
2200 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002201 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002202 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002203 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002204 // | parallel sections| simd | * |
2205 // | parallel sections| sections | + |
2206 // | parallel sections| section | * |
2207 // | parallel sections| single | + |
2208 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002209 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002210 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002211 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002212 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002213 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002214 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002215 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002216 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002217 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002218 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002219 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002220 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002221 // | parallel sections| target parallel | * |
2222 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002223 // | parallel sections| target enter | * |
2224 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002225 // | parallel sections| target exit | * |
2226 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002227 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002228 // | parallel sections| cancellation | |
2229 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002230 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002231 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002232 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002233 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002234 // +------------------+-----------------+------------------------------------+
2235 // | task | parallel | * |
2236 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002237 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002238 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002239 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002240 // | task | simd | * |
2241 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002242 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002243 // | task | single | + |
2244 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002245 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002246 // | task |parallel sections| * |
2247 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002248 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002249 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002250 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002251 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002252 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002253 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002254 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002255 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002256 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002257 // | task | target parallel | * |
2258 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002259 // | task | target enter | * |
2260 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002261 // | task | target exit | * |
2262 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002263 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002264 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002265 // | | point | ! |
2266 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002267 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002268 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002269 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002270 // +------------------+-----------------+------------------------------------+
2271 // | ordered | parallel | * |
2272 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002273 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002274 // | ordered | master | * |
2275 // | ordered | critical | * |
2276 // | ordered | simd | * |
2277 // | ordered | sections | + |
2278 // | ordered | section | + |
2279 // | ordered | single | + |
2280 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002281 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002282 // | ordered |parallel sections| * |
2283 // | ordered | task | * |
2284 // | ordered | taskyield | * |
2285 // | ordered | barrier | + |
2286 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002287 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002288 // | ordered | flush | * |
2289 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002290 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002291 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002292 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002293 // | ordered | target parallel | * |
2294 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002295 // | ordered | target enter | * |
2296 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002297 // | ordered | target exit | * |
2298 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002299 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002300 // | ordered | cancellation | |
2301 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002302 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002303 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002304 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002305 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002306 // +------------------+-----------------+------------------------------------+
2307 // | atomic | parallel | |
2308 // | atomic | for | |
2309 // | atomic | for simd | |
2310 // | atomic | master | |
2311 // | atomic | critical | |
2312 // | atomic | simd | |
2313 // | atomic | sections | |
2314 // | atomic | section | |
2315 // | atomic | single | |
2316 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002317 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002318 // | atomic |parallel sections| |
2319 // | atomic | task | |
2320 // | atomic | taskyield | |
2321 // | atomic | barrier | |
2322 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002323 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002324 // | atomic | flush | |
2325 // | atomic | ordered | |
2326 // | atomic | atomic | |
2327 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002328 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002329 // | atomic | target parallel | |
2330 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002331 // | atomic | target enter | |
2332 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002333 // | atomic | target exit | |
2334 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002335 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002336 // | atomic | cancellation | |
2337 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002338 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002339 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002340 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002341 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002342 // +------------------+-----------------+------------------------------------+
2343 // | target | parallel | * |
2344 // | target | for | * |
2345 // | target | for simd | * |
2346 // | target | master | * |
2347 // | target | critical | * |
2348 // | target | simd | * |
2349 // | target | sections | * |
2350 // | target | section | * |
2351 // | target | single | * |
2352 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002353 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002354 // | target |parallel sections| * |
2355 // | target | task | * |
2356 // | target | taskyield | * |
2357 // | target | barrier | * |
2358 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002359 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002360 // | target | flush | * |
2361 // | target | ordered | * |
2362 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002363 // | target | target | |
2364 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002365 // | target | target parallel | |
2366 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002367 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002368 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002369 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002370 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002371 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002372 // | target | cancellation | |
2373 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002374 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002375 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002376 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002377 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002378 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002379 // | target parallel | parallel | * |
2380 // | target parallel | for | * |
2381 // | target parallel | for simd | * |
2382 // | target parallel | master | * |
2383 // | target parallel | critical | * |
2384 // | target parallel | simd | * |
2385 // | target parallel | sections | * |
2386 // | target parallel | section | * |
2387 // | target parallel | single | * |
2388 // | target parallel | parallel for | * |
2389 // | target parallel |parallel for simd| * |
2390 // | target parallel |parallel sections| * |
2391 // | target parallel | task | * |
2392 // | target parallel | taskyield | * |
2393 // | target parallel | barrier | * |
2394 // | target parallel | taskwait | * |
2395 // | target parallel | taskgroup | * |
2396 // | target parallel | flush | * |
2397 // | target parallel | ordered | * |
2398 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002399 // | target parallel | target | |
2400 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002401 // | target parallel | target parallel | |
2402 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002403 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002404 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002405 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002406 // | | data | |
2407 // | target parallel | teams | |
2408 // | target parallel | cancellation | |
2409 // | | point | ! |
2410 // | target parallel | cancel | ! |
2411 // | target parallel | taskloop | * |
2412 // | target parallel | taskloop simd | * |
2413 // | target parallel | distribute | |
2414 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002415 // | target parallel | parallel | * |
2416 // | for | | |
2417 // | target parallel | for | * |
2418 // | for | | |
2419 // | target parallel | for simd | * |
2420 // | for | | |
2421 // | target parallel | master | * |
2422 // | for | | |
2423 // | target parallel | critical | * |
2424 // | for | | |
2425 // | target parallel | simd | * |
2426 // | for | | |
2427 // | target parallel | sections | * |
2428 // | for | | |
2429 // | target parallel | section | * |
2430 // | for | | |
2431 // | target parallel | single | * |
2432 // | for | | |
2433 // | target parallel | parallel for | * |
2434 // | for | | |
2435 // | target parallel |parallel for simd| * |
2436 // | for | | |
2437 // | target parallel |parallel sections| * |
2438 // | for | | |
2439 // | target parallel | task | * |
2440 // | for | | |
2441 // | target parallel | taskyield | * |
2442 // | for | | |
2443 // | target parallel | barrier | * |
2444 // | for | | |
2445 // | target parallel | taskwait | * |
2446 // | for | | |
2447 // | target parallel | taskgroup | * |
2448 // | for | | |
2449 // | target parallel | flush | * |
2450 // | for | | |
2451 // | target parallel | ordered | * |
2452 // | for | | |
2453 // | target parallel | atomic | * |
2454 // | for | | |
2455 // | target parallel | target | |
2456 // | for | | |
2457 // | target parallel | target parallel | |
2458 // | for | | |
2459 // | target parallel | target parallel | |
2460 // | for | for | |
2461 // | target parallel | target enter | |
2462 // | for | data | |
2463 // | target parallel | target exit | |
2464 // | for | data | |
2465 // | target parallel | teams | |
2466 // | for | | |
2467 // | target parallel | cancellation | |
2468 // | for | point | ! |
2469 // | target parallel | cancel | ! |
2470 // | for | | |
2471 // | target parallel | taskloop | * |
2472 // | for | | |
2473 // | target parallel | taskloop simd | * |
2474 // | for | | |
2475 // | target parallel | distribute | |
2476 // | for | | |
2477 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002478 // | teams | parallel | * |
2479 // | teams | for | + |
2480 // | teams | for simd | + |
2481 // | teams | master | + |
2482 // | teams | critical | + |
2483 // | teams | simd | + |
2484 // | teams | sections | + |
2485 // | teams | section | + |
2486 // | teams | single | + |
2487 // | teams | parallel for | * |
2488 // | teams |parallel for simd| * |
2489 // | teams |parallel sections| * |
2490 // | teams | task | + |
2491 // | teams | taskyield | + |
2492 // | teams | barrier | + |
2493 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002494 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002495 // | teams | flush | + |
2496 // | teams | ordered | + |
2497 // | teams | atomic | + |
2498 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002499 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002500 // | teams | target parallel | + |
2501 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002502 // | teams | target enter | + |
2503 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002504 // | teams | target exit | + |
2505 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002506 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002507 // | teams | cancellation | |
2508 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002509 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002510 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002511 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002512 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002513 // +------------------+-----------------+------------------------------------+
2514 // | taskloop | parallel | * |
2515 // | taskloop | for | + |
2516 // | taskloop | for simd | + |
2517 // | taskloop | master | + |
2518 // | taskloop | critical | * |
2519 // | taskloop | simd | * |
2520 // | taskloop | sections | + |
2521 // | taskloop | section | + |
2522 // | taskloop | single | + |
2523 // | taskloop | parallel for | * |
2524 // | taskloop |parallel for simd| * |
2525 // | taskloop |parallel sections| * |
2526 // | taskloop | task | * |
2527 // | taskloop | taskyield | * |
2528 // | taskloop | barrier | + |
2529 // | taskloop | taskwait | * |
2530 // | taskloop | taskgroup | * |
2531 // | taskloop | flush | * |
2532 // | taskloop | ordered | + |
2533 // | taskloop | atomic | * |
2534 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002535 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002536 // | taskloop | target parallel | * |
2537 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002538 // | taskloop | target enter | * |
2539 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002540 // | taskloop | target exit | * |
2541 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002542 // | taskloop | teams | + |
2543 // | taskloop | cancellation | |
2544 // | | point | |
2545 // | taskloop | cancel | |
2546 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002547 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002548 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002549 // | taskloop simd | parallel | |
2550 // | taskloop simd | for | |
2551 // | taskloop simd | for simd | |
2552 // | taskloop simd | master | |
2553 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002554 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002555 // | taskloop simd | sections | |
2556 // | taskloop simd | section | |
2557 // | taskloop simd | single | |
2558 // | taskloop simd | parallel for | |
2559 // | taskloop simd |parallel for simd| |
2560 // | taskloop simd |parallel sections| |
2561 // | taskloop simd | task | |
2562 // | taskloop simd | taskyield | |
2563 // | taskloop simd | barrier | |
2564 // | taskloop simd | taskwait | |
2565 // | taskloop simd | taskgroup | |
2566 // | taskloop simd | flush | |
2567 // | taskloop simd | ordered | + (with simd clause) |
2568 // | taskloop simd | atomic | |
2569 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002570 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002571 // | taskloop simd | target parallel | |
2572 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002573 // | taskloop simd | target enter | |
2574 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002575 // | taskloop simd | target exit | |
2576 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002577 // | taskloop simd | teams | |
2578 // | taskloop simd | cancellation | |
2579 // | | point | |
2580 // | taskloop simd | cancel | |
2581 // | taskloop simd | taskloop | |
2582 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002583 // | taskloop simd | distribute | |
2584 // +------------------+-----------------+------------------------------------+
2585 // | distribute | parallel | * |
2586 // | distribute | for | * |
2587 // | distribute | for simd | * |
2588 // | distribute | master | * |
2589 // | distribute | critical | * |
2590 // | distribute | simd | * |
2591 // | distribute | sections | * |
2592 // | distribute | section | * |
2593 // | distribute | single | * |
2594 // | distribute | parallel for | * |
2595 // | distribute |parallel for simd| * |
2596 // | distribute |parallel sections| * |
2597 // | distribute | task | * |
2598 // | distribute | taskyield | * |
2599 // | distribute | barrier | * |
2600 // | distribute | taskwait | * |
2601 // | distribute | taskgroup | * |
2602 // | distribute | flush | * |
2603 // | distribute | ordered | + |
2604 // | distribute | atomic | * |
2605 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002606 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002607 // | distribute | target parallel | |
2608 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002609 // | distribute | target enter | |
2610 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002611 // | distribute | target exit | |
2612 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002613 // | distribute | teams | |
2614 // | distribute | cancellation | + |
2615 // | | point | |
2616 // | distribute | cancel | + |
2617 // | distribute | taskloop | * |
2618 // | distribute | taskloop simd | * |
2619 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002620 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002621 if (Stack->getCurScope()) {
2622 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002623 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002624 bool NestingProhibited = false;
2625 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002626 enum {
2627 NoRecommend,
2628 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002629 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002630 ShouldBeInTargetRegion,
2631 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002632 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002633 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2634 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002635 // OpenMP [2.16, Nesting of Regions]
2636 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002637 // OpenMP [2.8.1,simd Construct, Restrictions]
2638 // An ordered construct with the simd clause is the only OpenMP construct
2639 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002640 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2641 return true;
2642 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002643 if (ParentRegion == OMPD_atomic) {
2644 // OpenMP [2.16, Nesting of Regions]
2645 // OpenMP constructs may not be nested inside an atomic region.
2646 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2647 return true;
2648 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002649 if (CurrentRegion == OMPD_section) {
2650 // OpenMP [2.7.2, sections Construct, Restrictions]
2651 // Orphaned section directives are prohibited. That is, the section
2652 // directives must appear within the sections construct and must not be
2653 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002654 if (ParentRegion != OMPD_sections &&
2655 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002656 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2657 << (ParentRegion != OMPD_unknown)
2658 << getOpenMPDirectiveName(ParentRegion);
2659 return true;
2660 }
2661 return false;
2662 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002663 // Allow some constructs to be orphaned (they could be used in functions,
2664 // called from OpenMP regions with the required preconditions).
2665 if (ParentRegion == OMPD_unknown)
2666 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002667 if (CurrentRegion == OMPD_cancellation_point ||
2668 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002669 // OpenMP [2.16, Nesting of Regions]
2670 // A cancellation point construct for which construct-type-clause is
2671 // taskgroup must be nested inside a task construct. A cancellation
2672 // point construct for which construct-type-clause is not taskgroup must
2673 // be closely nested inside an OpenMP construct that matches the type
2674 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002675 // A cancel construct for which construct-type-clause is taskgroup must be
2676 // nested inside a task construct. A cancel construct for which
2677 // construct-type-clause is not taskgroup must be closely nested inside an
2678 // OpenMP construct that matches the type specified in
2679 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002680 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002681 !((CancelRegion == OMPD_parallel &&
2682 (ParentRegion == OMPD_parallel ||
2683 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002684 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002685 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2686 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002687 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2688 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002689 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2690 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002691 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002692 // OpenMP [2.16, Nesting of Regions]
2693 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002694 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002695 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002696 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002697 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002698 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2699 // OpenMP [2.16, Nesting of Regions]
2700 // A critical region may not be nested (closely or otherwise) inside a
2701 // critical region with the same name. Note that this restriction is not
2702 // sufficient to prevent deadlock.
2703 SourceLocation PreviousCriticalLoc;
2704 bool DeadLock =
2705 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2706 OpenMPDirectiveKind K,
2707 const DeclarationNameInfo &DNI,
2708 SourceLocation Loc)
2709 ->bool {
2710 if (K == OMPD_critical &&
2711 DNI.getName() == CurrentName.getName()) {
2712 PreviousCriticalLoc = Loc;
2713 return true;
2714 } else
2715 return false;
2716 },
2717 false /* skip top directive */);
2718 if (DeadLock) {
2719 SemaRef.Diag(StartLoc,
2720 diag::err_omp_prohibited_region_critical_same_name)
2721 << CurrentName.getName();
2722 if (PreviousCriticalLoc.isValid())
2723 SemaRef.Diag(PreviousCriticalLoc,
2724 diag::note_omp_previous_critical_region);
2725 return true;
2726 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002727 } else if (CurrentRegion == OMPD_barrier) {
2728 // OpenMP [2.16, Nesting of Regions]
2729 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002730 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002731 NestingProhibited =
2732 isOpenMPWorksharingDirective(ParentRegion) ||
2733 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002734 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002735 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002736 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002737 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002738 // OpenMP [2.16, Nesting of Regions]
2739 // A worksharing region may not be closely nested inside a worksharing,
2740 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002741 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002742 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002743 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002744 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002745 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002746 Recommend = ShouldBeInParallelRegion;
2747 } else if (CurrentRegion == OMPD_ordered) {
2748 // OpenMP [2.16, Nesting of Regions]
2749 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002750 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002751 // An ordered region must be closely nested inside a loop region (or
2752 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002753 // OpenMP [2.8.1,simd Construct, Restrictions]
2754 // An ordered construct with the simd clause is the only OpenMP construct
2755 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002756 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002757 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002758 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002759 !(isOpenMPSimdDirective(ParentRegion) ||
2760 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002761 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002762 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2763 // OpenMP [2.16, Nesting of Regions]
2764 // If specified, a teams construct must be contained within a target
2765 // construct.
2766 NestingProhibited = ParentRegion != OMPD_target;
2767 Recommend = ShouldBeInTargetRegion;
2768 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2769 }
2770 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2771 // OpenMP [2.16, Nesting of Regions]
2772 // distribute, parallel, parallel sections, parallel workshare, and the
2773 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2774 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002775 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2776 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002777 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002778 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002779 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2780 // OpenMP 4.5 [2.17 Nesting of Regions]
2781 // The region associated with the distribute construct must be strictly
2782 // nested inside a teams region
2783 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2784 Recommend = ShouldBeInTeamsRegion;
2785 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002786 if (!NestingProhibited &&
2787 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2788 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2789 // OpenMP 4.5 [2.17 Nesting of Regions]
2790 // If a target, target update, target data, target enter data, or
2791 // target exit data construct is encountered during execution of a
2792 // target region, the behavior is unspecified.
2793 NestingProhibited = Stack->hasDirective(
2794 [&OffendingRegion](OpenMPDirectiveKind K,
2795 const DeclarationNameInfo &DNI,
2796 SourceLocation Loc) -> bool {
2797 if (isOpenMPTargetExecutionDirective(K)) {
2798 OffendingRegion = K;
2799 return true;
2800 } else
2801 return false;
2802 },
2803 false /* don't skip top directive */);
2804 CloseNesting = false;
2805 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002806 if (NestingProhibited) {
2807 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002808 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2809 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002810 return true;
2811 }
2812 }
2813 return false;
2814}
2815
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002816static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2817 ArrayRef<OMPClause *> Clauses,
2818 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2819 bool ErrorFound = false;
2820 unsigned NamedModifiersNumber = 0;
2821 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2822 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002823 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002824 for (const auto *C : Clauses) {
2825 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2826 // At most one if clause without a directive-name-modifier can appear on
2827 // the directive.
2828 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2829 if (FoundNameModifiers[CurNM]) {
2830 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2831 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2832 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2833 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002834 } else if (CurNM != OMPD_unknown) {
2835 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002836 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002837 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002838 FoundNameModifiers[CurNM] = IC;
2839 if (CurNM == OMPD_unknown)
2840 continue;
2841 // Check if the specified name modifier is allowed for the current
2842 // directive.
2843 // At most one if clause with the particular directive-name-modifier can
2844 // appear on the directive.
2845 bool MatchFound = false;
2846 for (auto NM : AllowedNameModifiers) {
2847 if (CurNM == NM) {
2848 MatchFound = true;
2849 break;
2850 }
2851 }
2852 if (!MatchFound) {
2853 S.Diag(IC->getNameModifierLoc(),
2854 diag::err_omp_wrong_if_directive_name_modifier)
2855 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2856 ErrorFound = true;
2857 }
2858 }
2859 }
2860 // If any if clause on the directive includes a directive-name-modifier then
2861 // all if clauses on the directive must include a directive-name-modifier.
2862 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2863 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2864 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2865 diag::err_omp_no_more_if_clause);
2866 } else {
2867 std::string Values;
2868 std::string Sep(", ");
2869 unsigned AllowedCnt = 0;
2870 unsigned TotalAllowedNum =
2871 AllowedNameModifiers.size() - NamedModifiersNumber;
2872 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2873 ++Cnt) {
2874 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2875 if (!FoundNameModifiers[NM]) {
2876 Values += "'";
2877 Values += getOpenMPDirectiveName(NM);
2878 Values += "'";
2879 if (AllowedCnt + 2 == TotalAllowedNum)
2880 Values += " or ";
2881 else if (AllowedCnt + 1 != TotalAllowedNum)
2882 Values += Sep;
2883 ++AllowedCnt;
2884 }
2885 }
2886 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2887 diag::err_omp_unnamed_if_clause)
2888 << (TotalAllowedNum > 1) << Values;
2889 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002890 for (auto Loc : NameModifierLoc) {
2891 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2892 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002893 ErrorFound = true;
2894 }
2895 return ErrorFound;
2896}
2897
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002898StmtResult Sema::ActOnOpenMPExecutableDirective(
2899 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2900 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2901 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002902 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002903 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2904 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002905 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002906
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002907 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002908 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002909 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002910 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002911 if (AStmt) {
2912 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2913
2914 // Check default data sharing attributes for referenced variables.
2915 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2916 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2917 if (DSAChecker.isErrorFound())
2918 return StmtError();
2919 // Generate list of implicitly defined firstprivate variables.
2920 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002921
2922 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2923 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2924 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2925 SourceLocation(), SourceLocation())) {
2926 ClausesWithImplicit.push_back(Implicit);
2927 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2928 DSAChecker.getImplicitFirstprivate().size();
2929 } else
2930 ErrorFound = true;
2931 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002932 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002933
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002934 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002935 switch (Kind) {
2936 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002937 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2938 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002939 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002940 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002941 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002942 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2943 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002944 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002945 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002946 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2947 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002948 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002949 case OMPD_for_simd:
2950 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2951 EndLoc, VarsWithInheritedDSA);
2952 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002953 case OMPD_sections:
2954 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2955 EndLoc);
2956 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002957 case OMPD_section:
2958 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002959 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002960 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2961 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002962 case OMPD_single:
2963 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2964 EndLoc);
2965 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002966 case OMPD_master:
2967 assert(ClausesWithImplicit.empty() &&
2968 "No clauses are allowed for 'omp master' directive");
2969 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2970 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002971 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002972 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2973 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002974 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002975 case OMPD_parallel_for:
2976 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2977 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002978 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002979 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002980 case OMPD_parallel_for_simd:
2981 Res = ActOnOpenMPParallelForSimdDirective(
2982 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002983 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002984 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002985 case OMPD_parallel_sections:
2986 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2987 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002988 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002989 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002990 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002991 Res =
2992 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002993 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002994 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002995 case OMPD_taskyield:
2996 assert(ClausesWithImplicit.empty() &&
2997 "No clauses are allowed for 'omp taskyield' directive");
2998 assert(AStmt == nullptr &&
2999 "No associated statement allowed for 'omp taskyield' directive");
3000 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3001 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003002 case OMPD_barrier:
3003 assert(ClausesWithImplicit.empty() &&
3004 "No clauses are allowed for 'omp barrier' directive");
3005 assert(AStmt == nullptr &&
3006 "No associated statement allowed for 'omp barrier' directive");
3007 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3008 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003009 case OMPD_taskwait:
3010 assert(ClausesWithImplicit.empty() &&
3011 "No clauses are allowed for 'omp taskwait' directive");
3012 assert(AStmt == nullptr &&
3013 "No associated statement allowed for 'omp taskwait' directive");
3014 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3015 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003016 case OMPD_taskgroup:
3017 assert(ClausesWithImplicit.empty() &&
3018 "No clauses are allowed for 'omp taskgroup' directive");
3019 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3020 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003021 case OMPD_flush:
3022 assert(AStmt == nullptr &&
3023 "No associated statement allowed for 'omp flush' directive");
3024 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3025 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003026 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003027 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3028 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003029 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003030 case OMPD_atomic:
3031 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3032 EndLoc);
3033 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003034 case OMPD_teams:
3035 Res =
3036 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3037 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003038 case OMPD_target:
3039 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3040 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003041 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003042 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003043 case OMPD_target_parallel:
3044 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3045 StartLoc, EndLoc);
3046 AllowedNameModifiers.push_back(OMPD_target);
3047 AllowedNameModifiers.push_back(OMPD_parallel);
3048 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003049 case OMPD_target_parallel_for:
3050 Res = ActOnOpenMPTargetParallelForDirective(
3051 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3052 AllowedNameModifiers.push_back(OMPD_target);
3053 AllowedNameModifiers.push_back(OMPD_parallel);
3054 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003055 case OMPD_cancellation_point:
3056 assert(ClausesWithImplicit.empty() &&
3057 "No clauses are allowed for 'omp cancellation point' directive");
3058 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3059 "cancellation point' directive");
3060 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3061 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003062 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003063 assert(AStmt == nullptr &&
3064 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003065 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3066 CancelRegion);
3067 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003068 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003069 case OMPD_target_data:
3070 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3071 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003072 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003073 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003074 case OMPD_target_enter_data:
3075 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3076 EndLoc);
3077 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3078 break;
Samuel Antao72590762016-01-19 20:04:50 +00003079 case OMPD_target_exit_data:
3080 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3081 EndLoc);
3082 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3083 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003084 case OMPD_taskloop:
3085 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3086 EndLoc, VarsWithInheritedDSA);
3087 AllowedNameModifiers.push_back(OMPD_taskloop);
3088 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003089 case OMPD_taskloop_simd:
3090 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3091 EndLoc, VarsWithInheritedDSA);
3092 AllowedNameModifiers.push_back(OMPD_taskloop);
3093 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003094 case OMPD_distribute:
3095 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3096 EndLoc, VarsWithInheritedDSA);
3097 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003098 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003099 llvm_unreachable("OpenMP Directive is not allowed");
3100 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003101 llvm_unreachable("Unknown OpenMP directive");
3102 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003103
Alexey Bataev4acb8592014-07-07 13:01:15 +00003104 for (auto P : VarsWithInheritedDSA) {
3105 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3106 << P.first << P.second->getSourceRange();
3107 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003108 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3109
3110 if (!AllowedNameModifiers.empty())
3111 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3112 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003113
Alexey Bataeved09d242014-05-28 05:53:51 +00003114 if (ErrorFound)
3115 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003116 return Res;
3117}
3118
3119StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3120 Stmt *AStmt,
3121 SourceLocation StartLoc,
3122 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003123 if (!AStmt)
3124 return StmtError();
3125
Alexey Bataev9959db52014-05-06 10:08:46 +00003126 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3127 // 1.2.2 OpenMP Language Terminology
3128 // Structured block - An executable statement with a single entry at the
3129 // top and a single exit at the bottom.
3130 // The point of exit cannot be a branch out of the structured block.
3131 // longjmp() and throw() must not violate the entry/exit criteria.
3132 CS->getCapturedDecl()->setNothrow();
3133
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003134 getCurFunction()->setHasBranchProtectedScope();
3135
Alexey Bataev25e5b442015-09-15 12:52:43 +00003136 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3137 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003138}
3139
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003140namespace {
3141/// \brief Helper class for checking canonical form of the OpenMP loops and
3142/// extracting iteration space of each loop in the loop nest, that will be used
3143/// for IR generation.
3144class OpenMPIterationSpaceChecker {
3145 /// \brief Reference to Sema.
3146 Sema &SemaRef;
3147 /// \brief A location for diagnostics (when there is no some better location).
3148 SourceLocation DefaultLoc;
3149 /// \brief A location for diagnostics (when increment is not compatible).
3150 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003151 /// \brief A source location for referring to loop init later.
3152 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003153 /// \brief A source location for referring to condition later.
3154 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003155 /// \brief A source location for referring to increment later.
3156 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003157 /// \brief Loop variable.
3158 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003159 /// \brief Reference to loop variable.
3160 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003161 /// \brief Lower bound (initializer for the var).
3162 Expr *LB;
3163 /// \brief Upper bound.
3164 Expr *UB;
3165 /// \brief Loop step (increment).
3166 Expr *Step;
3167 /// \brief This flag is true when condition is one of:
3168 /// Var < UB
3169 /// Var <= UB
3170 /// UB > Var
3171 /// UB >= Var
3172 bool TestIsLessOp;
3173 /// \brief This flag is true when condition is strict ( < or > ).
3174 bool TestIsStrictOp;
3175 /// \brief This flag is true when step is subtracted on each iteration.
3176 bool SubtractStep;
3177
3178public:
3179 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3180 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003181 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3182 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003183 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3184 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003185 /// \brief Check init-expr for canonical loop form and save loop counter
3186 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003187 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003188 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3189 /// for less/greater and for strict/non-strict comparison.
3190 bool CheckCond(Expr *S);
3191 /// \brief Check incr-expr for canonical loop form and return true if it
3192 /// does not conform, otherwise save loop step (#Step).
3193 bool CheckInc(Expr *S);
3194 /// \brief Return the loop counter variable.
3195 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003196 /// \brief Return the reference expression to loop counter variable.
3197 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003198 /// \brief Source range of the loop init.
3199 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3200 /// \brief Source range of the loop condition.
3201 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3202 /// \brief Source range of the loop increment.
3203 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3204 /// \brief True if the step should be subtracted.
3205 bool ShouldSubtractStep() const { return SubtractStep; }
3206 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003207 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003208 /// \brief Build the precondition expression for the loops.
3209 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003210 /// \brief Build reference expression to the counter be used for codegen.
3211 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003212 /// \brief Build reference expression to the private counter be used for
3213 /// codegen.
3214 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003215 /// \brief Build initization of the counter be used for codegen.
3216 Expr *BuildCounterInit() const;
3217 /// \brief Build step of the counter be used for codegen.
3218 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003219 /// \brief Return true if any expression is dependent.
3220 bool Dependent() const;
3221
3222private:
3223 /// \brief Check the right-hand side of an assignment in the increment
3224 /// expression.
3225 bool CheckIncRHS(Expr *RHS);
3226 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003227 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003228 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003229 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003230 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003231 /// \brief Helper to set loop increment.
3232 bool SetStep(Expr *NewStep, bool Subtract);
3233};
3234
3235bool OpenMPIterationSpaceChecker::Dependent() const {
3236 if (!Var) {
3237 assert(!LB && !UB && !Step);
3238 return false;
3239 }
3240 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3241 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3242}
3243
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003244template <typename T>
3245static T *getExprAsWritten(T *E) {
3246 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3247 E = ExprTemp->getSubExpr();
3248
3249 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3250 E = MTE->GetTemporaryExpr();
3251
3252 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3253 E = Binder->getSubExpr();
3254
3255 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3256 E = ICE->getSubExprAsWritten();
3257 return E->IgnoreParens();
3258}
3259
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003260bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3261 DeclRefExpr *NewVarRefExpr,
3262 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003263 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003264 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3265 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003266 if (!NewVar || !NewLB)
3267 return true;
3268 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003269 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003270 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3271 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003272 if ((Ctor->isCopyOrMoveConstructor() ||
3273 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3274 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003275 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003276 LB = NewLB;
3277 return false;
3278}
3279
3280bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003281 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003282 // State consistency checking to ensure correct usage.
3283 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3284 !TestIsLessOp && !TestIsStrictOp);
3285 if (!NewUB)
3286 return true;
3287 UB = NewUB;
3288 TestIsLessOp = LessOp;
3289 TestIsStrictOp = StrictOp;
3290 ConditionSrcRange = SR;
3291 ConditionLoc = SL;
3292 return false;
3293}
3294
3295bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3296 // State consistency checking to ensure correct usage.
3297 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3298 if (!NewStep)
3299 return true;
3300 if (!NewStep->isValueDependent()) {
3301 // Check that the step is integer expression.
3302 SourceLocation StepLoc = NewStep->getLocStart();
3303 ExprResult Val =
3304 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3305 if (Val.isInvalid())
3306 return true;
3307 NewStep = Val.get();
3308
3309 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3310 // If test-expr is of form var relational-op b and relational-op is < or
3311 // <= then incr-expr must cause var to increase on each iteration of the
3312 // loop. If test-expr is of form var relational-op b and relational-op is
3313 // > or >= then incr-expr must cause var to decrease on each iteration of
3314 // the loop.
3315 // If test-expr is of form b relational-op var and relational-op is < or
3316 // <= then incr-expr must cause var to decrease on each iteration of the
3317 // loop. If test-expr is of form b relational-op var and relational-op is
3318 // > or >= then incr-expr must cause var to increase on each iteration of
3319 // the loop.
3320 llvm::APSInt Result;
3321 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3322 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3323 bool IsConstNeg =
3324 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003325 bool IsConstPos =
3326 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003327 bool IsConstZero = IsConstant && !Result.getBoolValue();
3328 if (UB && (IsConstZero ||
3329 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003330 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003331 SemaRef.Diag(NewStep->getExprLoc(),
3332 diag::err_omp_loop_incr_not_compatible)
3333 << Var << TestIsLessOp << NewStep->getSourceRange();
3334 SemaRef.Diag(ConditionLoc,
3335 diag::note_omp_loop_cond_requres_compatible_incr)
3336 << TestIsLessOp << ConditionSrcRange;
3337 return true;
3338 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003339 if (TestIsLessOp == Subtract) {
3340 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3341 NewStep).get();
3342 Subtract = !Subtract;
3343 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003344 }
3345
3346 Step = NewStep;
3347 SubtractStep = Subtract;
3348 return false;
3349}
3350
Alexey Bataev9c821032015-04-30 04:23:23 +00003351bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003352 // Check init-expr for canonical loop form and save loop counter
3353 // variable - #Var and its initialization value - #LB.
3354 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3355 // var = lb
3356 // integer-type var = lb
3357 // random-access-iterator-type var = lb
3358 // pointer-type var = lb
3359 //
3360 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003361 if (EmitDiags) {
3362 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3363 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003364 return true;
3365 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003366 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003367 if (Expr *E = dyn_cast<Expr>(S))
3368 S = E->IgnoreParens();
3369 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3370 if (BO->getOpcode() == BO_Assign)
3371 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003372 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003373 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003374 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3375 if (DS->isSingleDecl()) {
3376 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003377 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003378 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003379 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003380 SemaRef.Diag(S->getLocStart(),
3381 diag::ext_omp_loop_not_canonical_init)
3382 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003383 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003384 }
3385 }
3386 }
3387 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3388 if (CE->getOperator() == OO_Equal)
3389 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003390 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3391 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003392
Alexey Bataev9c821032015-04-30 04:23:23 +00003393 if (EmitDiags) {
3394 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3395 << S->getSourceRange();
3396 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003397 return true;
3398}
3399
Alexey Bataev23b69422014-06-18 07:08:49 +00003400/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003401/// variable (which may be the loop variable) if possible.
3402static const VarDecl *GetInitVarDecl(const Expr *E) {
3403 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003404 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003405 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003406 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3407 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003408 if ((Ctor->isCopyOrMoveConstructor() ||
3409 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3410 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003411 E = CE->getArg(0)->IgnoreParenImpCasts();
3412 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3413 if (!DRE)
3414 return nullptr;
3415 return dyn_cast<VarDecl>(DRE->getDecl());
3416}
3417
3418bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3419 // Check test-expr for canonical form, save upper-bound UB, flags for
3420 // less/greater and for strict/non-strict comparison.
3421 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3422 // var relational-op b
3423 // b relational-op var
3424 //
3425 if (!S) {
3426 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3427 return true;
3428 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003429 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003430 SourceLocation CondLoc = S->getLocStart();
3431 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3432 if (BO->isRelationalOp()) {
3433 if (GetInitVarDecl(BO->getLHS()) == Var)
3434 return SetUB(BO->getRHS(),
3435 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3436 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3437 BO->getSourceRange(), BO->getOperatorLoc());
3438 if (GetInitVarDecl(BO->getRHS()) == Var)
3439 return SetUB(BO->getLHS(),
3440 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3441 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3442 BO->getSourceRange(), BO->getOperatorLoc());
3443 }
3444 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3445 if (CE->getNumArgs() == 2) {
3446 auto Op = CE->getOperator();
3447 switch (Op) {
3448 case OO_Greater:
3449 case OO_GreaterEqual:
3450 case OO_Less:
3451 case OO_LessEqual:
3452 if (GetInitVarDecl(CE->getArg(0)) == Var)
3453 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3454 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3455 CE->getOperatorLoc());
3456 if (GetInitVarDecl(CE->getArg(1)) == Var)
3457 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3458 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3459 CE->getOperatorLoc());
3460 break;
3461 default:
3462 break;
3463 }
3464 }
3465 }
3466 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3467 << S->getSourceRange() << Var;
3468 return true;
3469}
3470
3471bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3472 // RHS of canonical loop form increment can be:
3473 // var + incr
3474 // incr + var
3475 // var - incr
3476 //
3477 RHS = RHS->IgnoreParenImpCasts();
3478 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3479 if (BO->isAdditiveOp()) {
3480 bool IsAdd = BO->getOpcode() == BO_Add;
3481 if (GetInitVarDecl(BO->getLHS()) == Var)
3482 return SetStep(BO->getRHS(), !IsAdd);
3483 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3484 return SetStep(BO->getLHS(), false);
3485 }
3486 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3487 bool IsAdd = CE->getOperator() == OO_Plus;
3488 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3489 if (GetInitVarDecl(CE->getArg(0)) == Var)
3490 return SetStep(CE->getArg(1), !IsAdd);
3491 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3492 return SetStep(CE->getArg(0), false);
3493 }
3494 }
3495 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3496 << RHS->getSourceRange() << Var;
3497 return true;
3498}
3499
3500bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3501 // Check incr-expr for canonical loop form and return true if it
3502 // does not conform.
3503 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3504 // ++var
3505 // var++
3506 // --var
3507 // var--
3508 // var += incr
3509 // var -= incr
3510 // var = var + incr
3511 // var = incr + var
3512 // var = var - incr
3513 //
3514 if (!S) {
3515 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3516 return true;
3517 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003518 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003519 S = S->IgnoreParens();
3520 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3521 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3522 return SetStep(
3523 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3524 (UO->isDecrementOp() ? -1 : 1)).get(),
3525 false);
3526 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3527 switch (BO->getOpcode()) {
3528 case BO_AddAssign:
3529 case BO_SubAssign:
3530 if (GetInitVarDecl(BO->getLHS()) == Var)
3531 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3532 break;
3533 case BO_Assign:
3534 if (GetInitVarDecl(BO->getLHS()) == Var)
3535 return CheckIncRHS(BO->getRHS());
3536 break;
3537 default:
3538 break;
3539 }
3540 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3541 switch (CE->getOperator()) {
3542 case OO_PlusPlus:
3543 case OO_MinusMinus:
3544 if (GetInitVarDecl(CE->getArg(0)) == Var)
3545 return SetStep(
3546 SemaRef.ActOnIntegerConstant(
3547 CE->getLocStart(),
3548 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3549 false);
3550 break;
3551 case OO_PlusEqual:
3552 case OO_MinusEqual:
3553 if (GetInitVarDecl(CE->getArg(0)) == Var)
3554 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3555 break;
3556 case OO_Equal:
3557 if (GetInitVarDecl(CE->getArg(0)) == Var)
3558 return CheckIncRHS(CE->getArg(1));
3559 break;
3560 default:
3561 break;
3562 }
3563 }
3564 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3565 << S->getSourceRange() << Var;
3566 return true;
3567}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003568
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003569namespace {
3570// Transform variables declared in GNU statement expressions to new ones to
3571// avoid crash on codegen.
3572class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3573 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3574
3575public:
3576 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3577
3578 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3579 if (auto *VD = cast<VarDecl>(D))
3580 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3581 !isa<ImplicitParamDecl>(D)) {
3582 auto *NewVD = VarDecl::Create(
3583 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3584 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3585 VD->getTypeSourceInfo(), VD->getStorageClass());
3586 NewVD->setTSCSpec(VD->getTSCSpec());
3587 NewVD->setInit(VD->getInit());
3588 NewVD->setInitStyle(VD->getInitStyle());
3589 NewVD->setExceptionVariable(VD->isExceptionVariable());
3590 NewVD->setNRVOVariable(VD->isNRVOVariable());
3591 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3592 NewVD->setConstexpr(VD->isConstexpr());
3593 NewVD->setInitCapture(VD->isInitCapture());
3594 NewVD->setPreviousDeclInSameBlockScope(
3595 VD->isPreviousDeclInSameBlockScope());
3596 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003597 if (VD->hasAttrs())
3598 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003599 transformedLocalDecl(VD, NewVD);
3600 return NewVD;
3601 }
3602 return BaseTransform::TransformDefinition(Loc, D);
3603 }
3604
3605 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3606 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3607 if (E->getDecl() != NewD) {
3608 NewD->setReferenced();
3609 NewD->markUsed(SemaRef.Context);
3610 return DeclRefExpr::Create(
3611 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3612 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3613 E->getNameInfo(), E->getType(), E->getValueKind());
3614 }
3615 return BaseTransform::TransformDeclRefExpr(E);
3616 }
3617};
3618}
3619
Alexander Musmana5f070a2014-10-01 06:03:56 +00003620/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003621Expr *
3622OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3623 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003624 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003625 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003626 auto VarType = Var->getType().getNonReferenceType();
3627 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003628 SemaRef.getLangOpts().CPlusPlus) {
3629 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003630 auto *UBExpr = TestIsLessOp ? UB : LB;
3631 auto *LBExpr = TestIsLessOp ? LB : UB;
3632 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3633 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3634 if (!Upper || !Lower)
3635 return nullptr;
3636 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3637 Sema::AA_Converting,
3638 /*AllowExplicit=*/true)
3639 .get();
3640 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3641 Sema::AA_Converting,
3642 /*AllowExplicit=*/true)
3643 .get();
3644 if (!Upper || !Lower)
3645 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003646
3647 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3648
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003649 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003650 // BuildBinOp already emitted error, this one is to point user to upper
3651 // and lower bound, and to tell what is passed to 'operator-'.
3652 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3653 << Upper->getSourceRange() << Lower->getSourceRange();
3654 return nullptr;
3655 }
3656 }
3657
3658 if (!Diff.isUsable())
3659 return nullptr;
3660
3661 // Upper - Lower [- 1]
3662 if (TestIsStrictOp)
3663 Diff = SemaRef.BuildBinOp(
3664 S, DefaultLoc, BO_Sub, Diff.get(),
3665 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3666 if (!Diff.isUsable())
3667 return nullptr;
3668
3669 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003670 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3671 if (NewStep.isInvalid())
3672 return nullptr;
3673 NewStep = SemaRef.PerformImplicitConversion(
3674 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3675 /*AllowExplicit=*/true);
3676 if (NewStep.isInvalid())
3677 return nullptr;
3678 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003679 if (!Diff.isUsable())
3680 return nullptr;
3681
3682 // Parentheses (for dumping/debugging purposes only).
3683 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3684 if (!Diff.isUsable())
3685 return nullptr;
3686
3687 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003688 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3689 if (NewStep.isInvalid())
3690 return nullptr;
3691 NewStep = SemaRef.PerformImplicitConversion(
3692 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3693 /*AllowExplicit=*/true);
3694 if (NewStep.isInvalid())
3695 return nullptr;
3696 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003697 if (!Diff.isUsable())
3698 return nullptr;
3699
Alexander Musman174b3ca2014-10-06 11:16:29 +00003700 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003701 QualType Type = Diff.get()->getType();
3702 auto &C = SemaRef.Context;
3703 bool UseVarType = VarType->hasIntegerRepresentation() &&
3704 C.getTypeSize(Type) > C.getTypeSize(VarType);
3705 if (!Type->isIntegerType() || UseVarType) {
3706 unsigned NewSize =
3707 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3708 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3709 : Type->hasSignedIntegerRepresentation();
3710 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3711 Diff = SemaRef.PerformImplicitConversion(
3712 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3713 if (!Diff.isUsable())
3714 return nullptr;
3715 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003716 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003717 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3718 if (NewSize != C.getTypeSize(Type)) {
3719 if (NewSize < C.getTypeSize(Type)) {
3720 assert(NewSize == 64 && "incorrect loop var size");
3721 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3722 << InitSrcRange << ConditionSrcRange;
3723 }
3724 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003725 NewSize, Type->hasSignedIntegerRepresentation() ||
3726 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003727 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3728 Sema::AA_Converting, true);
3729 if (!Diff.isUsable())
3730 return nullptr;
3731 }
3732 }
3733
Alexander Musmana5f070a2014-10-01 06:03:56 +00003734 return Diff.get();
3735}
3736
Alexey Bataev62dbb972015-04-22 11:59:37 +00003737Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3738 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3739 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3740 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003741 TransformToNewDefs Transform(SemaRef);
3742
3743 auto NewLB = Transform.TransformExpr(LB);
3744 auto NewUB = Transform.TransformExpr(UB);
3745 if (NewLB.isInvalid() || NewUB.isInvalid())
3746 return Cond;
3747 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3748 Sema::AA_Converting,
3749 /*AllowExplicit=*/true);
3750 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3751 Sema::AA_Converting,
3752 /*AllowExplicit=*/true);
3753 if (NewLB.isInvalid() || NewUB.isInvalid())
3754 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003755 auto CondExpr = SemaRef.BuildBinOp(
3756 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3757 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003758 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003759 if (CondExpr.isUsable()) {
3760 CondExpr = SemaRef.PerformImplicitConversion(
3761 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3762 /*AllowExplicit=*/true);
3763 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003764 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3765 // Otherwise use original loop conditon and evaluate it in runtime.
3766 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3767}
3768
Alexander Musmana5f070a2014-10-01 06:03:56 +00003769/// \brief Build reference expression to the counter be used for codegen.
3770Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003771 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3772 DefaultLoc);
3773}
3774
3775Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3776 if (Var && !Var->isInvalidDecl()) {
3777 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003778 auto *PrivateVar =
3779 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3780 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003781 if (PrivateVar->isInvalidDecl())
3782 return nullptr;
3783 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3784 }
3785 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003786}
3787
3788/// \brief Build initization of the counter be used for codegen.
3789Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3790
3791/// \brief Build step of the counter be used for codegen.
3792Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3793
3794/// \brief Iteration space of a single for loop.
3795struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003796 /// \brief Condition of the loop.
3797 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003798 /// \brief This expression calculates the number of iterations in the loop.
3799 /// It is always possible to calculate it before starting the loop.
3800 Expr *NumIterations;
3801 /// \brief The loop counter variable.
3802 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003803 /// \brief Private loop counter variable.
3804 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003805 /// \brief This is initializer for the initial value of #CounterVar.
3806 Expr *CounterInit;
3807 /// \brief This is step for the #CounterVar used to generate its update:
3808 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3809 Expr *CounterStep;
3810 /// \brief Should step be subtracted?
3811 bool Subtract;
3812 /// \brief Source range of the loop init.
3813 SourceRange InitSrcRange;
3814 /// \brief Source range of the loop condition.
3815 SourceRange CondSrcRange;
3816 /// \brief Source range of the loop increment.
3817 SourceRange IncSrcRange;
3818};
3819
Alexey Bataev23b69422014-06-18 07:08:49 +00003820} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003821
Alexey Bataev9c821032015-04-30 04:23:23 +00003822void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3823 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3824 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003825 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3826 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003827 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3828 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003829 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003830 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003831 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003832 }
3833}
3834
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003835/// \brief Called on a for stmt to check and extract its iteration space
3836/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003837static bool CheckOpenMPIterationSpace(
3838 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3839 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003840 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003841 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003842 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003843 // OpenMP [2.6, Canonical Loop Form]
3844 // for (init-expr; test-expr; incr-expr) structured-block
3845 auto For = dyn_cast_or_null<ForStmt>(S);
3846 if (!For) {
3847 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003848 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3849 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3850 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3851 if (NestedLoopCount > 1) {
3852 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3853 SemaRef.Diag(DSA.getConstructLoc(),
3854 diag::note_omp_collapse_ordered_expr)
3855 << 2 << CollapseLoopCountExpr->getSourceRange()
3856 << OrderedLoopCountExpr->getSourceRange();
3857 else if (CollapseLoopCountExpr)
3858 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3859 diag::note_omp_collapse_ordered_expr)
3860 << 0 << CollapseLoopCountExpr->getSourceRange();
3861 else
3862 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3863 diag::note_omp_collapse_ordered_expr)
3864 << 1 << OrderedLoopCountExpr->getSourceRange();
3865 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003866 return true;
3867 }
3868 assert(For->getBody());
3869
3870 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3871
3872 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003873 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003874 if (ISC.CheckInit(Init)) {
3875 return true;
3876 }
3877
3878 bool HasErrors = false;
3879
3880 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003881 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003882
3883 // OpenMP [2.6, Canonical Loop Form]
3884 // Var is one of the following:
3885 // A variable of signed or unsigned integer type.
3886 // For C++, a variable of a random access iterator type.
3887 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003888 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003889 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3890 !VarType->isPointerType() &&
3891 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3892 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3893 << SemaRef.getLangOpts().CPlusPlus;
3894 HasErrors = true;
3895 }
3896
Alexey Bataev4acb8592014-07-07 13:01:15 +00003897 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3898 // Construct
3899 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3900 // parallel for construct is (are) private.
3901 // The loop iteration variable in the associated for-loop of a simd construct
3902 // with just one associated for-loop is linear with a constant-linear-step
3903 // that is the increment of the associated for-loop.
3904 // Exclude loop var from the list of variables with implicitly defined data
3905 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003906 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003907
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003908 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3909 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003910 // The loop iteration variable in the associated for-loop of a simd construct
3911 // with just one associated for-loop may be listed in a linear clause with a
3912 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003913 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3914 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003915 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003916 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3917 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3918 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003919 auto PredeterminedCKind =
3920 isOpenMPSimdDirective(DKind)
3921 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3922 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003923 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003924 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003925 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003926 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003927 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003928 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3929 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003931 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3932 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003933 if (DVar.RefExpr == nullptr)
3934 DVar.CKind = PredeterminedCKind;
3935 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003936 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003937 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003938 // Make the loop iteration variable private (for worksharing constructs),
3939 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003940 // lastprivate (for simd directives with several collapsed or ordered
3941 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003942 if (DVar.CKind == OMPC_unknown)
3943 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3944 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003945 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946 }
3947
Alexey Bataev7ff55242014-06-19 09:13:45 +00003948 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003949
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003950 // Check test-expr.
3951 HasErrors |= ISC.CheckCond(For->getCond());
3952
3953 // Check incr-expr.
3954 HasErrors |= ISC.CheckInc(For->getInc());
3955
Alexander Musmana5f070a2014-10-01 06:03:56 +00003956 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003957 return HasErrors;
3958
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003960 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003961 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003962 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003963 isOpenMPTaskLoopDirective(DKind) ||
3964 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003965 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003966 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003967 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3968 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3969 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3970 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3971 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3972 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3973
Alexey Bataev62dbb972015-04-22 11:59:37 +00003974 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3975 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003976 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003977 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003978 ResultIterSpace.CounterInit == nullptr ||
3979 ResultIterSpace.CounterStep == nullptr);
3980
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003981 return HasErrors;
3982}
3983
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003984/// \brief Build 'VarRef = Start.
3985static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3986 ExprResult VarRef, ExprResult Start) {
3987 TransformToNewDefs Transform(SemaRef);
3988 // Build 'VarRef = Start.
3989 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3990 if (NewStart.isInvalid())
3991 return ExprError();
3992 NewStart = SemaRef.PerformImplicitConversion(
3993 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3994 Sema::AA_Converting,
3995 /*AllowExplicit=*/true);
3996 if (NewStart.isInvalid())
3997 return ExprError();
3998 NewStart = SemaRef.PerformImplicitConversion(
3999 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4000 /*AllowExplicit=*/true);
4001 if (!NewStart.isUsable())
4002 return ExprError();
4003
4004 auto Init =
4005 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4006 return Init;
4007}
4008
Alexander Musmana5f070a2014-10-01 06:03:56 +00004009/// \brief Build 'VarRef = Start + Iter * Step'.
4010static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4011 SourceLocation Loc, ExprResult VarRef,
4012 ExprResult Start, ExprResult Iter,
4013 ExprResult Step, bool Subtract) {
4014 // Add parentheses (for debugging purposes only).
4015 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4016 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4017 !Step.isUsable())
4018 return ExprError();
4019
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004020 TransformToNewDefs Transform(SemaRef);
4021 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
4022 if (NewStep.isInvalid())
4023 return ExprError();
4024 NewStep = SemaRef.PerformImplicitConversion(
4025 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
4026 Sema::AA_Converting,
4027 /*AllowExplicit=*/true);
4028 if (NewStep.isInvalid())
4029 return ExprError();
4030 ExprResult Update =
4031 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004032 if (!Update.isUsable())
4033 return ExprError();
4034
4035 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004036 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4037 if (NewStart.isInvalid())
4038 return ExprError();
4039 NewStart = SemaRef.PerformImplicitConversion(
4040 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4041 Sema::AA_Converting,
4042 /*AllowExplicit=*/true);
4043 if (NewStart.isInvalid())
4044 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004045 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004046 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004047 if (!Update.isUsable())
4048 return ExprError();
4049
4050 Update = SemaRef.PerformImplicitConversion(
4051 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4052 if (!Update.isUsable())
4053 return ExprError();
4054
4055 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4056 return Update;
4057}
4058
4059/// \brief Convert integer expression \a E to make it have at least \a Bits
4060/// bits.
4061static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4062 Sema &SemaRef) {
4063 if (E == nullptr)
4064 return ExprError();
4065 auto &C = SemaRef.Context;
4066 QualType OldType = E->getType();
4067 unsigned HasBits = C.getTypeSize(OldType);
4068 if (HasBits >= Bits)
4069 return ExprResult(E);
4070 // OK to convert to signed, because new type has more bits than old.
4071 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4072 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4073 true);
4074}
4075
4076/// \brief Check if the given expression \a E is a constant integer that fits
4077/// into \a Bits bits.
4078static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4079 if (E == nullptr)
4080 return false;
4081 llvm::APSInt Result;
4082 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4083 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4084 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004085}
4086
4087/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004088/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4089/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004090static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004091CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4092 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4093 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004094 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004095 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004096 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004097 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004098 // Found 'collapse' clause - calculate collapse number.
4099 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004100 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004101 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004102 }
4103 if (OrderedLoopCountExpr) {
4104 // Found 'ordered' clause - calculate collapse number.
4105 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004106 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4107 if (Result.getLimitedValue() < NestedLoopCount) {
4108 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4109 diag::err_omp_wrong_ordered_loop_count)
4110 << OrderedLoopCountExpr->getSourceRange();
4111 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4112 diag::note_collapse_loop_count)
4113 << CollapseLoopCountExpr->getSourceRange();
4114 }
4115 NestedLoopCount = Result.getLimitedValue();
4116 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004117 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004118 // This is helper routine for loop directives (e.g., 'for', 'simd',
4119 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004120 SmallVector<LoopIterationSpace, 4> IterSpaces;
4121 IterSpaces.resize(NestedLoopCount);
4122 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004123 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004124 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004125 NestedLoopCount, CollapseLoopCountExpr,
4126 OrderedLoopCountExpr, VarsWithImplicitDSA,
4127 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004128 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004130 // OpenMP [2.8.1, simd construct, Restrictions]
4131 // All loops associated with the construct must be perfectly nested; that
4132 // is, there must be no intervening code nor any OpenMP directive between
4133 // any two loops.
4134 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004135 }
4136
Alexander Musmana5f070a2014-10-01 06:03:56 +00004137 Built.clear(/* size */ NestedLoopCount);
4138
4139 if (SemaRef.CurContext->isDependentContext())
4140 return NestedLoopCount;
4141
4142 // An example of what is generated for the following code:
4143 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004144 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004145 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004146 // for (k = 0; k < NK; ++k)
4147 // for (j = J0; j < NJ; j+=2) {
4148 // <loop body>
4149 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004150 //
4151 // We generate the code below.
4152 // Note: the loop body may be outlined in CodeGen.
4153 // Note: some counters may be C++ classes, operator- is used to find number of
4154 // iterations and operator+= to calculate counter value.
4155 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4156 // or i64 is currently supported).
4157 //
4158 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4159 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4160 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4161 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4162 // // similar updates for vars in clauses (e.g. 'linear')
4163 // <loop body (using local i and j)>
4164 // }
4165 // i = NI; // assign final values of counters
4166 // j = NJ;
4167 //
4168
4169 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4170 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004171 // Precondition tests if there is at least one iteration (all conditions are
4172 // true).
4173 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004174 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004175 ExprResult LastIteration32 = WidenIterationCount(
4176 32 /* Bits */, SemaRef.PerformImplicitConversion(
4177 N0->IgnoreImpCasts(), N0->getType(),
4178 Sema::AA_Converting, /*AllowExplicit=*/true)
4179 .get(),
4180 SemaRef);
4181 ExprResult LastIteration64 = WidenIterationCount(
4182 64 /* Bits */, SemaRef.PerformImplicitConversion(
4183 N0->IgnoreImpCasts(), N0->getType(),
4184 Sema::AA_Converting, /*AllowExplicit=*/true)
4185 .get(),
4186 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004187
4188 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4189 return NestedLoopCount;
4190
4191 auto &C = SemaRef.Context;
4192 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4193
4194 Scope *CurScope = DSA.getCurScope();
4195 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004196 if (PreCond.isUsable()) {
4197 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4198 PreCond.get(), IterSpaces[Cnt].PreCond);
4199 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004200 auto N = IterSpaces[Cnt].NumIterations;
4201 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4202 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004203 LastIteration32 = SemaRef.BuildBinOp(
4204 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4205 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4206 Sema::AA_Converting,
4207 /*AllowExplicit=*/true)
4208 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004209 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004210 LastIteration64 = SemaRef.BuildBinOp(
4211 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4212 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4213 Sema::AA_Converting,
4214 /*AllowExplicit=*/true)
4215 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004216 }
4217
4218 // Choose either the 32-bit or 64-bit version.
4219 ExprResult LastIteration = LastIteration64;
4220 if (LastIteration32.isUsable() &&
4221 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4222 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4223 FitsInto(
4224 32 /* Bits */,
4225 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4226 LastIteration64.get(), SemaRef)))
4227 LastIteration = LastIteration32;
4228
4229 if (!LastIteration.isUsable())
4230 return 0;
4231
4232 // Save the number of iterations.
4233 ExprResult NumIterations = LastIteration;
4234 {
4235 LastIteration = SemaRef.BuildBinOp(
4236 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4237 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4238 if (!LastIteration.isUsable())
4239 return 0;
4240 }
4241
4242 // Calculate the last iteration number beforehand instead of doing this on
4243 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4244 llvm::APSInt Result;
4245 bool IsConstant =
4246 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4247 ExprResult CalcLastIteration;
4248 if (!IsConstant) {
4249 SourceLocation SaveLoc;
4250 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004251 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004252 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004253 ExprResult SaveRef = buildDeclRefExpr(
4254 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004255 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4256 SaveRef.get(), LastIteration.get());
4257 LastIteration = SaveRef;
4258
4259 // Prepare SaveRef + 1.
4260 NumIterations = SemaRef.BuildBinOp(
4261 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4262 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4263 if (!NumIterations.isUsable())
4264 return 0;
4265 }
4266
4267 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4268
Alexander Musmanc6388682014-12-15 07:07:06 +00004269 QualType VType = LastIteration.get()->getType();
4270 // Build variables passed into runtime, nesessary for worksharing directives.
4271 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004272 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4273 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004274 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004275 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4276 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004277 SemaRef.AddInitializerToDecl(
4278 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4279 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4280
4281 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004282 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4283 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004284 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4285 /*DirectInit*/ false,
4286 /*TypeMayContainAuto*/ false);
4287
4288 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4289 // This will be used to implement clause 'lastprivate'.
4290 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004291 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4292 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004293 SemaRef.AddInitializerToDecl(
4294 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4295 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4296
4297 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004298 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4299 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004300 SemaRef.AddInitializerToDecl(
4301 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4302 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4303
4304 // Build expression: UB = min(UB, LastIteration)
4305 // It is nesessary for CodeGen of directives with static scheduling.
4306 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4307 UB.get(), LastIteration.get());
4308 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4309 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4310 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4311 CondOp.get());
4312 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4313 }
4314
4315 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004316 ExprResult IV;
4317 ExprResult Init;
4318 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004319 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4320 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004321 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004322 isOpenMPTaskLoopDirective(DKind) ||
4323 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004324 ? LB.get()
4325 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4326 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4327 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004328 }
4329
Alexander Musmanc6388682014-12-15 07:07:06 +00004330 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004331 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004332 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004333 (isOpenMPWorksharingDirective(DKind) ||
4334 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004335 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4336 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4337 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004338
4339 // Loop increment (IV = IV + 1)
4340 SourceLocation IncLoc;
4341 ExprResult Inc =
4342 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4343 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4344 if (!Inc.isUsable())
4345 return 0;
4346 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004347 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4348 if (!Inc.isUsable())
4349 return 0;
4350
4351 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4352 // Used for directives with static scheduling.
4353 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004354 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4355 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004356 // LB + ST
4357 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4358 if (!NextLB.isUsable())
4359 return 0;
4360 // LB = LB + ST
4361 NextLB =
4362 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4363 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4364 if (!NextLB.isUsable())
4365 return 0;
4366 // UB + ST
4367 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4368 if (!NextUB.isUsable())
4369 return 0;
4370 // UB = UB + ST
4371 NextUB =
4372 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4373 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4374 if (!NextUB.isUsable())
4375 return 0;
4376 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004377
4378 // Build updates and final values of the loop counters.
4379 bool HasErrors = false;
4380 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004381 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004382 Built.Updates.resize(NestedLoopCount);
4383 Built.Finals.resize(NestedLoopCount);
4384 {
4385 ExprResult Div;
4386 // Go from inner nested loop to outer.
4387 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4388 LoopIterationSpace &IS = IterSpaces[Cnt];
4389 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4390 // Build: Iter = (IV / Div) % IS.NumIters
4391 // where Div is product of previous iterations' IS.NumIters.
4392 ExprResult Iter;
4393 if (Div.isUsable()) {
4394 Iter =
4395 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4396 } else {
4397 Iter = IV;
4398 assert((Cnt == (int)NestedLoopCount - 1) &&
4399 "unusable div expected on first iteration only");
4400 }
4401
4402 if (Cnt != 0 && Iter.isUsable())
4403 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4404 IS.NumIterations);
4405 if (!Iter.isUsable()) {
4406 HasErrors = true;
4407 break;
4408 }
4409
Alexey Bataev39f915b82015-05-08 10:41:21 +00004410 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4411 auto *CounterVar = buildDeclRefExpr(
4412 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4413 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4414 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004415 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4416 IS.CounterInit);
4417 if (!Init.isUsable()) {
4418 HasErrors = true;
4419 break;
4420 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004421 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004422 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004423 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4424 if (!Update.isUsable()) {
4425 HasErrors = true;
4426 break;
4427 }
4428
4429 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4430 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004431 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004432 IS.NumIterations, IS.CounterStep, IS.Subtract);
4433 if (!Final.isUsable()) {
4434 HasErrors = true;
4435 break;
4436 }
4437
4438 // Build Div for the next iteration: Div <- Div * IS.NumIters
4439 if (Cnt != 0) {
4440 if (Div.isUnset())
4441 Div = IS.NumIterations;
4442 else
4443 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4444 IS.NumIterations);
4445
4446 // Add parentheses (for debugging purposes only).
4447 if (Div.isUsable())
4448 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4449 if (!Div.isUsable()) {
4450 HasErrors = true;
4451 break;
4452 }
4453 }
4454 if (!Update.isUsable() || !Final.isUsable()) {
4455 HasErrors = true;
4456 break;
4457 }
4458 // Save results
4459 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004460 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004461 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004462 Built.Updates[Cnt] = Update.get();
4463 Built.Finals[Cnt] = Final.get();
4464 }
4465 }
4466
4467 if (HasErrors)
4468 return 0;
4469
4470 // Save results
4471 Built.IterationVarRef = IV.get();
4472 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004473 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004474 Built.CalcLastIteration =
4475 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004476 Built.PreCond = PreCond.get();
4477 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004478 Built.Init = Init.get();
4479 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004480 Built.LB = LB.get();
4481 Built.UB = UB.get();
4482 Built.IL = IL.get();
4483 Built.ST = ST.get();
4484 Built.EUB = EUB.get();
4485 Built.NLB = NextLB.get();
4486 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004487
Alexey Bataevabfc0692014-06-25 06:52:00 +00004488 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004489}
4490
Alexey Bataev10e775f2015-07-30 11:36:16 +00004491static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004492 auto CollapseClauses =
4493 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4494 if (CollapseClauses.begin() != CollapseClauses.end())
4495 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004496 return nullptr;
4497}
4498
Alexey Bataev10e775f2015-07-30 11:36:16 +00004499static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004500 auto OrderedClauses =
4501 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4502 if (OrderedClauses.begin() != OrderedClauses.end())
4503 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004504 return nullptr;
4505}
4506
Alexey Bataev66b15b52015-08-21 11:14:16 +00004507static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4508 const Expr *Safelen) {
4509 llvm::APSInt SimdlenRes, SafelenRes;
4510 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4511 Simdlen->isInstantiationDependent() ||
4512 Simdlen->containsUnexpandedParameterPack())
4513 return false;
4514 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4515 Safelen->isInstantiationDependent() ||
4516 Safelen->containsUnexpandedParameterPack())
4517 return false;
4518 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4519 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4520 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4521 // If both simdlen and safelen clauses are specified, the value of the simdlen
4522 // parameter must be less than or equal to the value of the safelen parameter.
4523 if (SimdlenRes > SafelenRes) {
4524 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4525 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4526 return true;
4527 }
4528 return false;
4529}
4530
Alexey Bataev4acb8592014-07-07 13:01:15 +00004531StmtResult Sema::ActOnOpenMPSimdDirective(
4532 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4533 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004534 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004535 if (!AStmt)
4536 return StmtError();
4537
4538 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004539 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004540 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4541 // define the nested loops number.
4542 unsigned NestedLoopCount = CheckOpenMPLoop(
4543 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4544 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004545 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004546 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004547
Alexander Musmana5f070a2014-10-01 06:03:56 +00004548 assert((CurContext->isDependentContext() || B.builtAll()) &&
4549 "omp simd loop exprs were not built");
4550
Alexander Musman3276a272015-03-21 10:12:56 +00004551 if (!CurContext->isDependentContext()) {
4552 // Finalize the clauses that need pre-built expressions for CodeGen.
4553 for (auto C : Clauses) {
4554 if (auto LC = dyn_cast<OMPLinearClause>(C))
4555 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4556 B.NumIterations, *this, CurScope))
4557 return StmtError();
4558 }
4559 }
4560
Alexey Bataev66b15b52015-08-21 11:14:16 +00004561 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4562 // If both simdlen and safelen clauses are specified, the value of the simdlen
4563 // parameter must be less than or equal to the value of the safelen parameter.
4564 OMPSafelenClause *Safelen = nullptr;
4565 OMPSimdlenClause *Simdlen = nullptr;
4566 for (auto *Clause : Clauses) {
4567 if (Clause->getClauseKind() == OMPC_safelen)
4568 Safelen = cast<OMPSafelenClause>(Clause);
4569 else if (Clause->getClauseKind() == OMPC_simdlen)
4570 Simdlen = cast<OMPSimdlenClause>(Clause);
4571 if (Safelen && Simdlen)
4572 break;
4573 }
4574 if (Simdlen && Safelen &&
4575 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4576 Safelen->getSafelen()))
4577 return StmtError();
4578
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004579 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004580 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4581 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004582}
4583
Alexey Bataev4acb8592014-07-07 13:01:15 +00004584StmtResult Sema::ActOnOpenMPForDirective(
4585 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4586 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004587 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004588 if (!AStmt)
4589 return StmtError();
4590
4591 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004592 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004593 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4594 // define the nested loops number.
4595 unsigned NestedLoopCount = CheckOpenMPLoop(
4596 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4597 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004598 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004599 return StmtError();
4600
Alexander Musmana5f070a2014-10-01 06:03:56 +00004601 assert((CurContext->isDependentContext() || B.builtAll()) &&
4602 "omp for loop exprs were not built");
4603
Alexey Bataev54acd402015-08-04 11:18:19 +00004604 if (!CurContext->isDependentContext()) {
4605 // Finalize the clauses that need pre-built expressions for CodeGen.
4606 for (auto C : Clauses) {
4607 if (auto LC = dyn_cast<OMPLinearClause>(C))
4608 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4609 B.NumIterations, *this, CurScope))
4610 return StmtError();
4611 }
4612 }
4613
Alexey Bataevf29276e2014-06-18 04:14:57 +00004614 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004615 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004616 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004617}
4618
Alexander Musmanf82886e2014-09-18 05:12:34 +00004619StmtResult Sema::ActOnOpenMPForSimdDirective(
4620 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4621 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004622 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004623 if (!AStmt)
4624 return StmtError();
4625
4626 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004627 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004628 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4629 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004630 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004631 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4632 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4633 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004634 if (NestedLoopCount == 0)
4635 return StmtError();
4636
Alexander Musmanc6388682014-12-15 07:07:06 +00004637 assert((CurContext->isDependentContext() || B.builtAll()) &&
4638 "omp for simd loop exprs were not built");
4639
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004640 if (!CurContext->isDependentContext()) {
4641 // Finalize the clauses that need pre-built expressions for CodeGen.
4642 for (auto C : Clauses) {
4643 if (auto LC = dyn_cast<OMPLinearClause>(C))
4644 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4645 B.NumIterations, *this, CurScope))
4646 return StmtError();
4647 }
4648 }
4649
Alexey Bataev66b15b52015-08-21 11:14:16 +00004650 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4651 // If both simdlen and safelen clauses are specified, the value of the simdlen
4652 // parameter must be less than or equal to the value of the safelen parameter.
4653 OMPSafelenClause *Safelen = nullptr;
4654 OMPSimdlenClause *Simdlen = nullptr;
4655 for (auto *Clause : Clauses) {
4656 if (Clause->getClauseKind() == OMPC_safelen)
4657 Safelen = cast<OMPSafelenClause>(Clause);
4658 else if (Clause->getClauseKind() == OMPC_simdlen)
4659 Simdlen = cast<OMPSimdlenClause>(Clause);
4660 if (Safelen && Simdlen)
4661 break;
4662 }
4663 if (Simdlen && Safelen &&
4664 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4665 Safelen->getSafelen()))
4666 return StmtError();
4667
Alexander Musmanf82886e2014-09-18 05:12:34 +00004668 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004669 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4670 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004671}
4672
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004673StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4674 Stmt *AStmt,
4675 SourceLocation StartLoc,
4676 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004677 if (!AStmt)
4678 return StmtError();
4679
4680 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004681 auto BaseStmt = AStmt;
4682 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4683 BaseStmt = CS->getCapturedStmt();
4684 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4685 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004686 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004687 return StmtError();
4688 // All associated statements must be '#pragma omp section' except for
4689 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004690 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004691 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4692 if (SectionStmt)
4693 Diag(SectionStmt->getLocStart(),
4694 diag::err_omp_sections_substmt_not_section);
4695 return StmtError();
4696 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004697 cast<OMPSectionDirective>(SectionStmt)
4698 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004699 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004700 } else {
4701 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4702 return StmtError();
4703 }
4704
4705 getCurFunction()->setHasBranchProtectedScope();
4706
Alexey Bataev25e5b442015-09-15 12:52:43 +00004707 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4708 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004709}
4710
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004711StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4712 SourceLocation StartLoc,
4713 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004714 if (!AStmt)
4715 return StmtError();
4716
4717 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004718
4719 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004720 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004721
Alexey Bataev25e5b442015-09-15 12:52:43 +00004722 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4723 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004724}
4725
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004726StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4727 Stmt *AStmt,
4728 SourceLocation StartLoc,
4729 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004730 if (!AStmt)
4731 return StmtError();
4732
4733 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004734
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004735 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004736
Alexey Bataev3255bf32015-01-19 05:20:46 +00004737 // OpenMP [2.7.3, single Construct, Restrictions]
4738 // The copyprivate clause must not be used with the nowait clause.
4739 OMPClause *Nowait = nullptr;
4740 OMPClause *Copyprivate = nullptr;
4741 for (auto *Clause : Clauses) {
4742 if (Clause->getClauseKind() == OMPC_nowait)
4743 Nowait = Clause;
4744 else if (Clause->getClauseKind() == OMPC_copyprivate)
4745 Copyprivate = Clause;
4746 if (Copyprivate && Nowait) {
4747 Diag(Copyprivate->getLocStart(),
4748 diag::err_omp_single_copyprivate_with_nowait);
4749 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4750 return StmtError();
4751 }
4752 }
4753
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004754 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4755}
4756
Alexander Musman80c22892014-07-17 08:54:58 +00004757StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4758 SourceLocation StartLoc,
4759 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004760 if (!AStmt)
4761 return StmtError();
4762
4763 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004764
4765 getCurFunction()->setHasBranchProtectedScope();
4766
4767 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4768}
4769
Alexey Bataev28c75412015-12-15 08:19:24 +00004770StmtResult Sema::ActOnOpenMPCriticalDirective(
4771 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4772 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004773 if (!AStmt)
4774 return StmtError();
4775
4776 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004777
Alexey Bataev28c75412015-12-15 08:19:24 +00004778 bool ErrorFound = false;
4779 llvm::APSInt Hint;
4780 SourceLocation HintLoc;
4781 bool DependentHint = false;
4782 for (auto *C : Clauses) {
4783 if (C->getClauseKind() == OMPC_hint) {
4784 if (!DirName.getName()) {
4785 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4786 ErrorFound = true;
4787 }
4788 Expr *E = cast<OMPHintClause>(C)->getHint();
4789 if (E->isTypeDependent() || E->isValueDependent() ||
4790 E->isInstantiationDependent())
4791 DependentHint = true;
4792 else {
4793 Hint = E->EvaluateKnownConstInt(Context);
4794 HintLoc = C->getLocStart();
4795 }
4796 }
4797 }
4798 if (ErrorFound)
4799 return StmtError();
4800 auto Pair = DSAStack->getCriticalWithHint(DirName);
4801 if (Pair.first && DirName.getName() && !DependentHint) {
4802 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4803 Diag(StartLoc, diag::err_omp_critical_with_hint);
4804 if (HintLoc.isValid()) {
4805 Diag(HintLoc, diag::note_omp_critical_hint_here)
4806 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4807 } else
4808 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4809 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4810 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4811 << 1
4812 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4813 /*Radix=*/10, /*Signed=*/false);
4814 } else
4815 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4816 }
4817 }
4818
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004819 getCurFunction()->setHasBranchProtectedScope();
4820
Alexey Bataev28c75412015-12-15 08:19:24 +00004821 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4822 Clauses, AStmt);
4823 if (!Pair.first && DirName.getName() && !DependentHint)
4824 DSAStack->addCriticalWithHint(Dir, Hint);
4825 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004826}
4827
Alexey Bataev4acb8592014-07-07 13:01:15 +00004828StmtResult Sema::ActOnOpenMPParallelForDirective(
4829 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4830 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004831 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004832 if (!AStmt)
4833 return StmtError();
4834
Alexey Bataev4acb8592014-07-07 13:01:15 +00004835 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4836 // 1.2.2 OpenMP Language Terminology
4837 // Structured block - An executable statement with a single entry at the
4838 // top and a single exit at the bottom.
4839 // The point of exit cannot be a branch out of the structured block.
4840 // longjmp() and throw() must not violate the entry/exit criteria.
4841 CS->getCapturedDecl()->setNothrow();
4842
Alexander Musmanc6388682014-12-15 07:07:06 +00004843 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004844 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4845 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004846 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004847 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4848 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4849 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004850 if (NestedLoopCount == 0)
4851 return StmtError();
4852
Alexander Musmana5f070a2014-10-01 06:03:56 +00004853 assert((CurContext->isDependentContext() || B.builtAll()) &&
4854 "omp parallel for loop exprs were not built");
4855
Alexey Bataev54acd402015-08-04 11:18:19 +00004856 if (!CurContext->isDependentContext()) {
4857 // Finalize the clauses that need pre-built expressions for CodeGen.
4858 for (auto C : Clauses) {
4859 if (auto LC = dyn_cast<OMPLinearClause>(C))
4860 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4861 B.NumIterations, *this, CurScope))
4862 return StmtError();
4863 }
4864 }
4865
Alexey Bataev4acb8592014-07-07 13:01:15 +00004866 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004867 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004868 NestedLoopCount, Clauses, AStmt, B,
4869 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004870}
4871
Alexander Musmane4e893b2014-09-23 09:33:00 +00004872StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4873 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4874 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004875 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004876 if (!AStmt)
4877 return StmtError();
4878
Alexander Musmane4e893b2014-09-23 09:33:00 +00004879 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4880 // 1.2.2 OpenMP Language Terminology
4881 // Structured block - An executable statement with a single entry at the
4882 // top and a single exit at the bottom.
4883 // The point of exit cannot be a branch out of the structured block.
4884 // longjmp() and throw() must not violate the entry/exit criteria.
4885 CS->getCapturedDecl()->setNothrow();
4886
Alexander Musmanc6388682014-12-15 07:07:06 +00004887 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004888 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4889 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004890 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004891 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4892 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4893 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004894 if (NestedLoopCount == 0)
4895 return StmtError();
4896
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004897 if (!CurContext->isDependentContext()) {
4898 // Finalize the clauses that need pre-built expressions for CodeGen.
4899 for (auto C : Clauses) {
4900 if (auto LC = dyn_cast<OMPLinearClause>(C))
4901 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4902 B.NumIterations, *this, CurScope))
4903 return StmtError();
4904 }
4905 }
4906
Alexey Bataev66b15b52015-08-21 11:14:16 +00004907 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4908 // If both simdlen and safelen clauses are specified, the value of the simdlen
4909 // parameter must be less than or equal to the value of the safelen parameter.
4910 OMPSafelenClause *Safelen = nullptr;
4911 OMPSimdlenClause *Simdlen = nullptr;
4912 for (auto *Clause : Clauses) {
4913 if (Clause->getClauseKind() == OMPC_safelen)
4914 Safelen = cast<OMPSafelenClause>(Clause);
4915 else if (Clause->getClauseKind() == OMPC_simdlen)
4916 Simdlen = cast<OMPSimdlenClause>(Clause);
4917 if (Safelen && Simdlen)
4918 break;
4919 }
4920 if (Simdlen && Safelen &&
4921 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4922 Safelen->getSafelen()))
4923 return StmtError();
4924
Alexander Musmane4e893b2014-09-23 09:33:00 +00004925 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004926 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004927 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004928}
4929
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004930StmtResult
4931Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4932 Stmt *AStmt, SourceLocation StartLoc,
4933 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004934 if (!AStmt)
4935 return StmtError();
4936
4937 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004938 auto BaseStmt = AStmt;
4939 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4940 BaseStmt = CS->getCapturedStmt();
4941 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4942 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004943 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004944 return StmtError();
4945 // All associated statements must be '#pragma omp section' except for
4946 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004947 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004948 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4949 if (SectionStmt)
4950 Diag(SectionStmt->getLocStart(),
4951 diag::err_omp_parallel_sections_substmt_not_section);
4952 return StmtError();
4953 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004954 cast<OMPSectionDirective>(SectionStmt)
4955 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004956 }
4957 } else {
4958 Diag(AStmt->getLocStart(),
4959 diag::err_omp_parallel_sections_not_compound_stmt);
4960 return StmtError();
4961 }
4962
4963 getCurFunction()->setHasBranchProtectedScope();
4964
Alexey Bataev25e5b442015-09-15 12:52:43 +00004965 return OMPParallelSectionsDirective::Create(
4966 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004967}
4968
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004969StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4970 Stmt *AStmt, SourceLocation StartLoc,
4971 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004972 if (!AStmt)
4973 return StmtError();
4974
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004975 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4976 // 1.2.2 OpenMP Language Terminology
4977 // Structured block - An executable statement with a single entry at the
4978 // top and a single exit at the bottom.
4979 // The point of exit cannot be a branch out of the structured block.
4980 // longjmp() and throw() must not violate the entry/exit criteria.
4981 CS->getCapturedDecl()->setNothrow();
4982
4983 getCurFunction()->setHasBranchProtectedScope();
4984
Alexey Bataev25e5b442015-09-15 12:52:43 +00004985 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4986 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004987}
4988
Alexey Bataev68446b72014-07-18 07:47:19 +00004989StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4990 SourceLocation EndLoc) {
4991 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4992}
4993
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004994StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4995 SourceLocation EndLoc) {
4996 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4997}
4998
Alexey Bataev2df347a2014-07-18 10:17:07 +00004999StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5000 SourceLocation EndLoc) {
5001 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5002}
5003
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005004StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5005 SourceLocation StartLoc,
5006 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005007 if (!AStmt)
5008 return StmtError();
5009
5010 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005011
5012 getCurFunction()->setHasBranchProtectedScope();
5013
5014 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5015}
5016
Alexey Bataev6125da92014-07-21 11:26:11 +00005017StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5018 SourceLocation StartLoc,
5019 SourceLocation EndLoc) {
5020 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5021 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5022}
5023
Alexey Bataev346265e2015-09-25 10:37:12 +00005024StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5025 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005026 SourceLocation StartLoc,
5027 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005028 OMPClause *DependFound = nullptr;
5029 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005030 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005031 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005032 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005033 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005034 for (auto *C : Clauses) {
5035 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5036 DependFound = C;
5037 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5038 if (DependSourceClause) {
5039 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5040 << getOpenMPDirectiveName(OMPD_ordered)
5041 << getOpenMPClauseName(OMPC_depend) << 2;
5042 ErrorFound = true;
5043 } else
5044 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005045 if (DependSinkClause) {
5046 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5047 << 0;
5048 ErrorFound = true;
5049 }
5050 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5051 if (DependSourceClause) {
5052 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5053 << 1;
5054 ErrorFound = true;
5055 }
5056 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005057 }
5058 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005059 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005060 else if (C->getClauseKind() == OMPC_simd)
5061 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005062 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005063 if (!ErrorFound && !SC &&
5064 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005065 // OpenMP [2.8.1,simd Construct, Restrictions]
5066 // An ordered construct with the simd clause is the only OpenMP construct
5067 // that can appear in the simd region.
5068 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005069 ErrorFound = true;
5070 } else if (DependFound && (TC || SC)) {
5071 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5072 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5073 ErrorFound = true;
5074 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5075 Diag(DependFound->getLocStart(),
5076 diag::err_omp_ordered_directive_without_param);
5077 ErrorFound = true;
5078 } else if (TC || Clauses.empty()) {
5079 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5080 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5081 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5082 << (TC != nullptr);
5083 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5084 ErrorFound = true;
5085 }
5086 }
5087 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005088 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005089
5090 if (AStmt) {
5091 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5092
5093 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005094 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005095
5096 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005097}
5098
Alexey Bataev1d160b12015-03-13 12:27:31 +00005099namespace {
5100/// \brief Helper class for checking expression in 'omp atomic [update]'
5101/// construct.
5102class OpenMPAtomicUpdateChecker {
5103 /// \brief Error results for atomic update expressions.
5104 enum ExprAnalysisErrorCode {
5105 /// \brief A statement is not an expression statement.
5106 NotAnExpression,
5107 /// \brief Expression is not builtin binary or unary operation.
5108 NotABinaryOrUnaryExpression,
5109 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5110 NotAnUnaryIncDecExpression,
5111 /// \brief An expression is not of scalar type.
5112 NotAScalarType,
5113 /// \brief A binary operation is not an assignment operation.
5114 NotAnAssignmentOp,
5115 /// \brief RHS part of the binary operation is not a binary expression.
5116 NotABinaryExpression,
5117 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5118 /// expression.
5119 NotABinaryOperator,
5120 /// \brief RHS binary operation does not have reference to the updated LHS
5121 /// part.
5122 NotAnUpdateExpression,
5123 /// \brief No errors is found.
5124 NoError
5125 };
5126 /// \brief Reference to Sema.
5127 Sema &SemaRef;
5128 /// \brief A location for note diagnostics (when error is found).
5129 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005130 /// \brief 'x' lvalue part of the source atomic expression.
5131 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005132 /// \brief 'expr' rvalue part of the source atomic expression.
5133 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005134 /// \brief Helper expression of the form
5135 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5136 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5137 Expr *UpdateExpr;
5138 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5139 /// important for non-associative operations.
5140 bool IsXLHSInRHSPart;
5141 BinaryOperatorKind Op;
5142 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005143 /// \brief true if the source expression is a postfix unary operation, false
5144 /// if it is a prefix unary operation.
5145 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005146
5147public:
5148 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005149 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005150 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005151 /// \brief Check specified statement that it is suitable for 'atomic update'
5152 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005153 /// expression. If DiagId and NoteId == 0, then only check is performed
5154 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005155 /// \param DiagId Diagnostic which should be emitted if error is found.
5156 /// \param NoteId Diagnostic note for the main error message.
5157 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005158 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005159 /// \brief Return the 'x' lvalue part of the source atomic expression.
5160 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005161 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5162 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005163 /// \brief Return the update expression used in calculation of the updated
5164 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5165 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5166 Expr *getUpdateExpr() const { return UpdateExpr; }
5167 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5168 /// false otherwise.
5169 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5170
Alexey Bataevb78ca832015-04-01 03:33:17 +00005171 /// \brief true if the source expression is a postfix unary operation, false
5172 /// if it is a prefix unary operation.
5173 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5174
Alexey Bataev1d160b12015-03-13 12:27:31 +00005175private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005176 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5177 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005178};
5179} // namespace
5180
5181bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5182 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5183 ExprAnalysisErrorCode ErrorFound = NoError;
5184 SourceLocation ErrorLoc, NoteLoc;
5185 SourceRange ErrorRange, NoteRange;
5186 // Allowed constructs are:
5187 // x = x binop expr;
5188 // x = expr binop x;
5189 if (AtomicBinOp->getOpcode() == BO_Assign) {
5190 X = AtomicBinOp->getLHS();
5191 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5192 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5193 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5194 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5195 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005196 Op = AtomicInnerBinOp->getOpcode();
5197 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005198 auto *LHS = AtomicInnerBinOp->getLHS();
5199 auto *RHS = AtomicInnerBinOp->getRHS();
5200 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5201 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5202 /*Canonical=*/true);
5203 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5204 /*Canonical=*/true);
5205 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5206 /*Canonical=*/true);
5207 if (XId == LHSId) {
5208 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005209 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005210 } else if (XId == RHSId) {
5211 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005212 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005213 } else {
5214 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5215 ErrorRange = AtomicInnerBinOp->getSourceRange();
5216 NoteLoc = X->getExprLoc();
5217 NoteRange = X->getSourceRange();
5218 ErrorFound = NotAnUpdateExpression;
5219 }
5220 } else {
5221 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5222 ErrorRange = AtomicInnerBinOp->getSourceRange();
5223 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5224 NoteRange = SourceRange(NoteLoc, NoteLoc);
5225 ErrorFound = NotABinaryOperator;
5226 }
5227 } else {
5228 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5229 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5230 ErrorFound = NotABinaryExpression;
5231 }
5232 } else {
5233 ErrorLoc = AtomicBinOp->getExprLoc();
5234 ErrorRange = AtomicBinOp->getSourceRange();
5235 NoteLoc = AtomicBinOp->getOperatorLoc();
5236 NoteRange = SourceRange(NoteLoc, NoteLoc);
5237 ErrorFound = NotAnAssignmentOp;
5238 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005239 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005240 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5241 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5242 return true;
5243 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005244 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005245 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005246}
5247
5248bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5249 unsigned NoteId) {
5250 ExprAnalysisErrorCode ErrorFound = NoError;
5251 SourceLocation ErrorLoc, NoteLoc;
5252 SourceRange ErrorRange, NoteRange;
5253 // Allowed constructs are:
5254 // x++;
5255 // x--;
5256 // ++x;
5257 // --x;
5258 // x binop= expr;
5259 // x = x binop expr;
5260 // x = expr binop x;
5261 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5262 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5263 if (AtomicBody->getType()->isScalarType() ||
5264 AtomicBody->isInstantiationDependent()) {
5265 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5266 AtomicBody->IgnoreParenImpCasts())) {
5267 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005268 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005269 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005270 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005271 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005272 X = AtomicCompAssignOp->getLHS();
5273 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005274 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5275 AtomicBody->IgnoreParenImpCasts())) {
5276 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005277 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5278 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005279 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005280 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5281 // Check for Unary Operation
5282 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005283 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005284 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5285 OpLoc = AtomicUnaryOp->getOperatorLoc();
5286 X = AtomicUnaryOp->getSubExpr();
5287 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5288 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005289 } else {
5290 ErrorFound = NotAnUnaryIncDecExpression;
5291 ErrorLoc = AtomicUnaryOp->getExprLoc();
5292 ErrorRange = AtomicUnaryOp->getSourceRange();
5293 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5294 NoteRange = SourceRange(NoteLoc, NoteLoc);
5295 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005296 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005297 ErrorFound = NotABinaryOrUnaryExpression;
5298 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5299 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5300 }
5301 } else {
5302 ErrorFound = NotAScalarType;
5303 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5304 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5305 }
5306 } else {
5307 ErrorFound = NotAnExpression;
5308 NoteLoc = ErrorLoc = S->getLocStart();
5309 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5310 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005311 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005312 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5313 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5314 return true;
5315 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005316 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005317 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005318 // Build an update expression of form 'OpaqueValueExpr(x) binop
5319 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5320 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5321 auto *OVEX = new (SemaRef.getASTContext())
5322 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5323 auto *OVEExpr = new (SemaRef.getASTContext())
5324 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5325 auto Update =
5326 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5327 IsXLHSInRHSPart ? OVEExpr : OVEX);
5328 if (Update.isInvalid())
5329 return true;
5330 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5331 Sema::AA_Casting);
5332 if (Update.isInvalid())
5333 return true;
5334 UpdateExpr = Update.get();
5335 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005336 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005337}
5338
Alexey Bataev0162e452014-07-22 10:10:35 +00005339StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5340 Stmt *AStmt,
5341 SourceLocation StartLoc,
5342 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005343 if (!AStmt)
5344 return StmtError();
5345
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005346 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005347 // 1.2.2 OpenMP Language Terminology
5348 // Structured block - An executable statement with a single entry at the
5349 // top and a single exit at the bottom.
5350 // The point of exit cannot be a branch out of the structured block.
5351 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005352 OpenMPClauseKind AtomicKind = OMPC_unknown;
5353 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005354 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005355 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005356 C->getClauseKind() == OMPC_update ||
5357 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005358 if (AtomicKind != OMPC_unknown) {
5359 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5360 << SourceRange(C->getLocStart(), C->getLocEnd());
5361 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5362 << getOpenMPClauseName(AtomicKind);
5363 } else {
5364 AtomicKind = C->getClauseKind();
5365 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005366 }
5367 }
5368 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005369
Alexey Bataev459dec02014-07-24 06:46:57 +00005370 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005371 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5372 Body = EWC->getSubExpr();
5373
Alexey Bataev62cec442014-11-18 10:14:22 +00005374 Expr *X = nullptr;
5375 Expr *V = nullptr;
5376 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005377 Expr *UE = nullptr;
5378 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005379 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005380 // OpenMP [2.12.6, atomic Construct]
5381 // In the next expressions:
5382 // * x and v (as applicable) are both l-value expressions with scalar type.
5383 // * During the execution of an atomic region, multiple syntactic
5384 // occurrences of x must designate the same storage location.
5385 // * Neither of v and expr (as applicable) may access the storage location
5386 // designated by x.
5387 // * Neither of x and expr (as applicable) may access the storage location
5388 // designated by v.
5389 // * expr is an expression with scalar type.
5390 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5391 // * binop, binop=, ++, and -- are not overloaded operators.
5392 // * The expression x binop expr must be numerically equivalent to x binop
5393 // (expr). This requirement is satisfied if the operators in expr have
5394 // precedence greater than binop, or by using parentheses around expr or
5395 // subexpressions of expr.
5396 // * The expression expr binop x must be numerically equivalent to (expr)
5397 // binop x. This requirement is satisfied if the operators in expr have
5398 // precedence equal to or greater than binop, or by using parentheses around
5399 // expr or subexpressions of expr.
5400 // * For forms that allow multiple occurrences of x, the number of times
5401 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005402 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005403 enum {
5404 NotAnExpression,
5405 NotAnAssignmentOp,
5406 NotAScalarType,
5407 NotAnLValue,
5408 NoError
5409 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005410 SourceLocation ErrorLoc, NoteLoc;
5411 SourceRange ErrorRange, NoteRange;
5412 // If clause is read:
5413 // v = x;
5414 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5415 auto AtomicBinOp =
5416 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5417 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5418 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5419 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5420 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5421 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5422 if (!X->isLValue() || !V->isLValue()) {
5423 auto NotLValueExpr = X->isLValue() ? V : X;
5424 ErrorFound = NotAnLValue;
5425 ErrorLoc = AtomicBinOp->getExprLoc();
5426 ErrorRange = AtomicBinOp->getSourceRange();
5427 NoteLoc = NotLValueExpr->getExprLoc();
5428 NoteRange = NotLValueExpr->getSourceRange();
5429 }
5430 } else if (!X->isInstantiationDependent() ||
5431 !V->isInstantiationDependent()) {
5432 auto NotScalarExpr =
5433 (X->isInstantiationDependent() || X->getType()->isScalarType())
5434 ? V
5435 : X;
5436 ErrorFound = NotAScalarType;
5437 ErrorLoc = AtomicBinOp->getExprLoc();
5438 ErrorRange = AtomicBinOp->getSourceRange();
5439 NoteLoc = NotScalarExpr->getExprLoc();
5440 NoteRange = NotScalarExpr->getSourceRange();
5441 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005442 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005443 ErrorFound = NotAnAssignmentOp;
5444 ErrorLoc = AtomicBody->getExprLoc();
5445 ErrorRange = AtomicBody->getSourceRange();
5446 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5447 : AtomicBody->getExprLoc();
5448 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5449 : AtomicBody->getSourceRange();
5450 }
5451 } else {
5452 ErrorFound = NotAnExpression;
5453 NoteLoc = ErrorLoc = Body->getLocStart();
5454 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005455 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005456 if (ErrorFound != NoError) {
5457 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5458 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005459 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5460 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005461 return StmtError();
5462 } else if (CurContext->isDependentContext())
5463 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005464 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005465 enum {
5466 NotAnExpression,
5467 NotAnAssignmentOp,
5468 NotAScalarType,
5469 NotAnLValue,
5470 NoError
5471 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005472 SourceLocation ErrorLoc, NoteLoc;
5473 SourceRange ErrorRange, NoteRange;
5474 // If clause is write:
5475 // x = expr;
5476 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5477 auto AtomicBinOp =
5478 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5479 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005480 X = AtomicBinOp->getLHS();
5481 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005482 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5483 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5484 if (!X->isLValue()) {
5485 ErrorFound = NotAnLValue;
5486 ErrorLoc = AtomicBinOp->getExprLoc();
5487 ErrorRange = AtomicBinOp->getSourceRange();
5488 NoteLoc = X->getExprLoc();
5489 NoteRange = X->getSourceRange();
5490 }
5491 } else if (!X->isInstantiationDependent() ||
5492 !E->isInstantiationDependent()) {
5493 auto NotScalarExpr =
5494 (X->isInstantiationDependent() || X->getType()->isScalarType())
5495 ? E
5496 : X;
5497 ErrorFound = NotAScalarType;
5498 ErrorLoc = AtomicBinOp->getExprLoc();
5499 ErrorRange = AtomicBinOp->getSourceRange();
5500 NoteLoc = NotScalarExpr->getExprLoc();
5501 NoteRange = NotScalarExpr->getSourceRange();
5502 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005503 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005504 ErrorFound = NotAnAssignmentOp;
5505 ErrorLoc = AtomicBody->getExprLoc();
5506 ErrorRange = AtomicBody->getSourceRange();
5507 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5508 : AtomicBody->getExprLoc();
5509 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5510 : AtomicBody->getSourceRange();
5511 }
5512 } else {
5513 ErrorFound = NotAnExpression;
5514 NoteLoc = ErrorLoc = Body->getLocStart();
5515 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005516 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005517 if (ErrorFound != NoError) {
5518 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5519 << ErrorRange;
5520 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5521 << NoteRange;
5522 return StmtError();
5523 } else if (CurContext->isDependentContext())
5524 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005525 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005526 // If clause is update:
5527 // x++;
5528 // x--;
5529 // ++x;
5530 // --x;
5531 // x binop= expr;
5532 // x = x binop expr;
5533 // x = expr binop x;
5534 OpenMPAtomicUpdateChecker Checker(*this);
5535 if (Checker.checkStatement(
5536 Body, (AtomicKind == OMPC_update)
5537 ? diag::err_omp_atomic_update_not_expression_statement
5538 : diag::err_omp_atomic_not_expression_statement,
5539 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005540 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005541 if (!CurContext->isDependentContext()) {
5542 E = Checker.getExpr();
5543 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005544 UE = Checker.getUpdateExpr();
5545 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005546 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005547 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005548 enum {
5549 NotAnAssignmentOp,
5550 NotACompoundStatement,
5551 NotTwoSubstatements,
5552 NotASpecificExpression,
5553 NoError
5554 } ErrorFound = NoError;
5555 SourceLocation ErrorLoc, NoteLoc;
5556 SourceRange ErrorRange, NoteRange;
5557 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5558 // If clause is a capture:
5559 // v = x++;
5560 // v = x--;
5561 // v = ++x;
5562 // v = --x;
5563 // v = x binop= expr;
5564 // v = x = x binop expr;
5565 // v = x = expr binop x;
5566 auto *AtomicBinOp =
5567 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5568 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5569 V = AtomicBinOp->getLHS();
5570 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5571 OpenMPAtomicUpdateChecker Checker(*this);
5572 if (Checker.checkStatement(
5573 Body, diag::err_omp_atomic_capture_not_expression_statement,
5574 diag::note_omp_atomic_update))
5575 return StmtError();
5576 E = Checker.getExpr();
5577 X = Checker.getX();
5578 UE = Checker.getUpdateExpr();
5579 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5580 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005581 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005582 ErrorLoc = AtomicBody->getExprLoc();
5583 ErrorRange = AtomicBody->getSourceRange();
5584 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5585 : AtomicBody->getExprLoc();
5586 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5587 : AtomicBody->getSourceRange();
5588 ErrorFound = NotAnAssignmentOp;
5589 }
5590 if (ErrorFound != NoError) {
5591 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5592 << ErrorRange;
5593 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5594 return StmtError();
5595 } else if (CurContext->isDependentContext()) {
5596 UE = V = E = X = nullptr;
5597 }
5598 } else {
5599 // If clause is a capture:
5600 // { v = x; x = expr; }
5601 // { v = x; x++; }
5602 // { v = x; x--; }
5603 // { v = x; ++x; }
5604 // { v = x; --x; }
5605 // { v = x; x binop= expr; }
5606 // { v = x; x = x binop expr; }
5607 // { v = x; x = expr binop x; }
5608 // { x++; v = x; }
5609 // { x--; v = x; }
5610 // { ++x; v = x; }
5611 // { --x; v = x; }
5612 // { x binop= expr; v = x; }
5613 // { x = x binop expr; v = x; }
5614 // { x = expr binop x; v = x; }
5615 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5616 // Check that this is { expr1; expr2; }
5617 if (CS->size() == 2) {
5618 auto *First = CS->body_front();
5619 auto *Second = CS->body_back();
5620 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5621 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5622 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5623 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5624 // Need to find what subexpression is 'v' and what is 'x'.
5625 OpenMPAtomicUpdateChecker Checker(*this);
5626 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5627 BinaryOperator *BinOp = nullptr;
5628 if (IsUpdateExprFound) {
5629 BinOp = dyn_cast<BinaryOperator>(First);
5630 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5631 }
5632 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5633 // { v = x; x++; }
5634 // { v = x; x--; }
5635 // { v = x; ++x; }
5636 // { v = x; --x; }
5637 // { v = x; x binop= expr; }
5638 // { v = x; x = x binop expr; }
5639 // { v = x; x = expr binop x; }
5640 // Check that the first expression has form v = x.
5641 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5642 llvm::FoldingSetNodeID XId, PossibleXId;
5643 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5644 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5645 IsUpdateExprFound = XId == PossibleXId;
5646 if (IsUpdateExprFound) {
5647 V = BinOp->getLHS();
5648 X = Checker.getX();
5649 E = Checker.getExpr();
5650 UE = Checker.getUpdateExpr();
5651 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005652 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005653 }
5654 }
5655 if (!IsUpdateExprFound) {
5656 IsUpdateExprFound = !Checker.checkStatement(First);
5657 BinOp = nullptr;
5658 if (IsUpdateExprFound) {
5659 BinOp = dyn_cast<BinaryOperator>(Second);
5660 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5661 }
5662 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5663 // { x++; v = x; }
5664 // { x--; v = x; }
5665 // { ++x; v = x; }
5666 // { --x; v = x; }
5667 // { x binop= expr; v = x; }
5668 // { x = x binop expr; v = x; }
5669 // { x = expr binop x; v = x; }
5670 // Check that the second expression has form v = x.
5671 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5672 llvm::FoldingSetNodeID XId, PossibleXId;
5673 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5674 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5675 IsUpdateExprFound = XId == PossibleXId;
5676 if (IsUpdateExprFound) {
5677 V = BinOp->getLHS();
5678 X = Checker.getX();
5679 E = Checker.getExpr();
5680 UE = Checker.getUpdateExpr();
5681 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005682 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005683 }
5684 }
5685 }
5686 if (!IsUpdateExprFound) {
5687 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005688 auto *FirstExpr = dyn_cast<Expr>(First);
5689 auto *SecondExpr = dyn_cast<Expr>(Second);
5690 if (!FirstExpr || !SecondExpr ||
5691 !(FirstExpr->isInstantiationDependent() ||
5692 SecondExpr->isInstantiationDependent())) {
5693 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5694 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005695 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005696 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5697 : First->getLocStart();
5698 NoteRange = ErrorRange = FirstBinOp
5699 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005700 : SourceRange(ErrorLoc, ErrorLoc);
5701 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005702 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5703 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5704 ErrorFound = NotAnAssignmentOp;
5705 NoteLoc = ErrorLoc = SecondBinOp
5706 ? SecondBinOp->getOperatorLoc()
5707 : Second->getLocStart();
5708 NoteRange = ErrorRange =
5709 SecondBinOp ? SecondBinOp->getSourceRange()
5710 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005711 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005712 auto *PossibleXRHSInFirst =
5713 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5714 auto *PossibleXLHSInSecond =
5715 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5716 llvm::FoldingSetNodeID X1Id, X2Id;
5717 PossibleXRHSInFirst->Profile(X1Id, Context,
5718 /*Canonical=*/true);
5719 PossibleXLHSInSecond->Profile(X2Id, Context,
5720 /*Canonical=*/true);
5721 IsUpdateExprFound = X1Id == X2Id;
5722 if (IsUpdateExprFound) {
5723 V = FirstBinOp->getLHS();
5724 X = SecondBinOp->getLHS();
5725 E = SecondBinOp->getRHS();
5726 UE = nullptr;
5727 IsXLHSInRHSPart = false;
5728 IsPostfixUpdate = true;
5729 } else {
5730 ErrorFound = NotASpecificExpression;
5731 ErrorLoc = FirstBinOp->getExprLoc();
5732 ErrorRange = FirstBinOp->getSourceRange();
5733 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5734 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5735 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005736 }
5737 }
5738 }
5739 }
5740 } else {
5741 NoteLoc = ErrorLoc = Body->getLocStart();
5742 NoteRange = ErrorRange =
5743 SourceRange(Body->getLocStart(), Body->getLocStart());
5744 ErrorFound = NotTwoSubstatements;
5745 }
5746 } else {
5747 NoteLoc = ErrorLoc = Body->getLocStart();
5748 NoteRange = ErrorRange =
5749 SourceRange(Body->getLocStart(), Body->getLocStart());
5750 ErrorFound = NotACompoundStatement;
5751 }
5752 if (ErrorFound != NoError) {
5753 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5754 << ErrorRange;
5755 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5756 return StmtError();
5757 } else if (CurContext->isDependentContext()) {
5758 UE = V = E = X = nullptr;
5759 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005760 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005761 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005762
5763 getCurFunction()->setHasBranchProtectedScope();
5764
Alexey Bataev62cec442014-11-18 10:14:22 +00005765 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005766 X, V, E, UE, IsXLHSInRHSPart,
5767 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005768}
5769
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005770StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5771 Stmt *AStmt,
5772 SourceLocation StartLoc,
5773 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005774 if (!AStmt)
5775 return StmtError();
5776
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005777 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5778 // 1.2.2 OpenMP Language Terminology
5779 // Structured block - An executable statement with a single entry at the
5780 // top and a single exit at the bottom.
5781 // The point of exit cannot be a branch out of the structured block.
5782 // longjmp() and throw() must not violate the entry/exit criteria.
5783 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005784
Alexey Bataev13314bf2014-10-09 04:18:56 +00005785 // OpenMP [2.16, Nesting of Regions]
5786 // If specified, a teams construct must be contained within a target
5787 // construct. That target construct must contain no statements or directives
5788 // outside of the teams construct.
5789 if (DSAStack->hasInnerTeamsRegion()) {
5790 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5791 bool OMPTeamsFound = true;
5792 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5793 auto I = CS->body_begin();
5794 while (I != CS->body_end()) {
5795 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5796 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5797 OMPTeamsFound = false;
5798 break;
5799 }
5800 ++I;
5801 }
5802 assert(I != CS->body_end() && "Not found statement");
5803 S = *I;
5804 }
5805 if (!OMPTeamsFound) {
5806 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5807 Diag(DSAStack->getInnerTeamsRegionLoc(),
5808 diag::note_omp_nested_teams_construct_here);
5809 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5810 << isa<OMPExecutableDirective>(S);
5811 return StmtError();
5812 }
5813 }
5814
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005815 getCurFunction()->setHasBranchProtectedScope();
5816
5817 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5818}
5819
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005820StmtResult
5821Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5822 Stmt *AStmt, SourceLocation StartLoc,
5823 SourceLocation EndLoc) {
5824 if (!AStmt)
5825 return StmtError();
5826
5827 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5828 // 1.2.2 OpenMP Language Terminology
5829 // Structured block - An executable statement with a single entry at the
5830 // top and a single exit at the bottom.
5831 // The point of exit cannot be a branch out of the structured block.
5832 // longjmp() and throw() must not violate the entry/exit criteria.
5833 CS->getCapturedDecl()->setNothrow();
5834
5835 getCurFunction()->setHasBranchProtectedScope();
5836
5837 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5838 AStmt);
5839}
5840
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005841StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5842 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5843 SourceLocation EndLoc,
5844 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5845 if (!AStmt)
5846 return StmtError();
5847
5848 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5849 // 1.2.2 OpenMP Language Terminology
5850 // Structured block - An executable statement with a single entry at the
5851 // top and a single exit at the bottom.
5852 // The point of exit cannot be a branch out of the structured block.
5853 // longjmp() and throw() must not violate the entry/exit criteria.
5854 CS->getCapturedDecl()->setNothrow();
5855
5856 OMPLoopDirective::HelperExprs B;
5857 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5858 // define the nested loops number.
5859 unsigned NestedLoopCount =
5860 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5861 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5862 VarsWithImplicitDSA, B);
5863 if (NestedLoopCount == 0)
5864 return StmtError();
5865
5866 assert((CurContext->isDependentContext() || B.builtAll()) &&
5867 "omp target parallel for loop exprs were not built");
5868
5869 if (!CurContext->isDependentContext()) {
5870 // Finalize the clauses that need pre-built expressions for CodeGen.
5871 for (auto C : Clauses) {
5872 if (auto LC = dyn_cast<OMPLinearClause>(C))
5873 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5874 B.NumIterations, *this, CurScope))
5875 return StmtError();
5876 }
5877 }
5878
5879 getCurFunction()->setHasBranchProtectedScope();
5880 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5881 NestedLoopCount, Clauses, AStmt,
5882 B, DSAStack->isCancelRegion());
5883}
5884
Samuel Antaodf67fc42016-01-19 19:15:56 +00005885/// \brief Check for existence of a map clause in the list of clauses.
5886static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5887 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5888 I != E; ++I) {
5889 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5890 return true;
5891 }
5892 }
5893
5894 return false;
5895}
5896
Michael Wong65f367f2015-07-21 13:44:28 +00005897StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5898 Stmt *AStmt,
5899 SourceLocation StartLoc,
5900 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005901 if (!AStmt)
5902 return StmtError();
5903
5904 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5905
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005906 // OpenMP [2.10.1, Restrictions, p. 97]
5907 // At least one map clause must appear on the directive.
5908 if (!HasMapClause(Clauses)) {
5909 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5910 getOpenMPDirectiveName(OMPD_target_data);
5911 return StmtError();
5912 }
5913
Michael Wong65f367f2015-07-21 13:44:28 +00005914 getCurFunction()->setHasBranchProtectedScope();
5915
5916 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5917 AStmt);
5918}
5919
Samuel Antaodf67fc42016-01-19 19:15:56 +00005920StmtResult
5921Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5922 SourceLocation StartLoc,
5923 SourceLocation EndLoc) {
5924 // OpenMP [2.10.2, Restrictions, p. 99]
5925 // At least one map clause must appear on the directive.
5926 if (!HasMapClause(Clauses)) {
5927 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5928 << getOpenMPDirectiveName(OMPD_target_enter_data);
5929 return StmtError();
5930 }
5931
5932 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5933 Clauses);
5934}
5935
Samuel Antao72590762016-01-19 20:04:50 +00005936StmtResult
5937Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5938 SourceLocation StartLoc,
5939 SourceLocation EndLoc) {
5940 // OpenMP [2.10.3, Restrictions, p. 102]
5941 // At least one map clause must appear on the directive.
5942 if (!HasMapClause(Clauses)) {
5943 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5944 << getOpenMPDirectiveName(OMPD_target_exit_data);
5945 return StmtError();
5946 }
5947
5948 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5949}
5950
Alexey Bataev13314bf2014-10-09 04:18:56 +00005951StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5952 Stmt *AStmt, SourceLocation StartLoc,
5953 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005954 if (!AStmt)
5955 return StmtError();
5956
Alexey Bataev13314bf2014-10-09 04:18:56 +00005957 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5958 // 1.2.2 OpenMP Language Terminology
5959 // Structured block - An executable statement with a single entry at the
5960 // top and a single exit at the bottom.
5961 // The point of exit cannot be a branch out of the structured block.
5962 // longjmp() and throw() must not violate the entry/exit criteria.
5963 CS->getCapturedDecl()->setNothrow();
5964
5965 getCurFunction()->setHasBranchProtectedScope();
5966
5967 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5968}
5969
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005970StmtResult
5971Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5972 SourceLocation EndLoc,
5973 OpenMPDirectiveKind CancelRegion) {
5974 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5975 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5976 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5977 << getOpenMPDirectiveName(CancelRegion);
5978 return StmtError();
5979 }
5980 if (DSAStack->isParentNowaitRegion()) {
5981 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5982 return StmtError();
5983 }
5984 if (DSAStack->isParentOrderedRegion()) {
5985 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5986 return StmtError();
5987 }
5988 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5989 CancelRegion);
5990}
5991
Alexey Bataev87933c72015-09-18 08:07:34 +00005992StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5993 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005994 SourceLocation EndLoc,
5995 OpenMPDirectiveKind CancelRegion) {
5996 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5997 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5998 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5999 << getOpenMPDirectiveName(CancelRegion);
6000 return StmtError();
6001 }
6002 if (DSAStack->isParentNowaitRegion()) {
6003 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6004 return StmtError();
6005 }
6006 if (DSAStack->isParentOrderedRegion()) {
6007 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6008 return StmtError();
6009 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006010 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006011 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6012 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006013}
6014
Alexey Bataev382967a2015-12-08 12:06:20 +00006015static bool checkGrainsizeNumTasksClauses(Sema &S,
6016 ArrayRef<OMPClause *> Clauses) {
6017 OMPClause *PrevClause = nullptr;
6018 bool ErrorFound = false;
6019 for (auto *C : Clauses) {
6020 if (C->getClauseKind() == OMPC_grainsize ||
6021 C->getClauseKind() == OMPC_num_tasks) {
6022 if (!PrevClause)
6023 PrevClause = C;
6024 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6025 S.Diag(C->getLocStart(),
6026 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6027 << getOpenMPClauseName(C->getClauseKind())
6028 << getOpenMPClauseName(PrevClause->getClauseKind());
6029 S.Diag(PrevClause->getLocStart(),
6030 diag::note_omp_previous_grainsize_num_tasks)
6031 << getOpenMPClauseName(PrevClause->getClauseKind());
6032 ErrorFound = true;
6033 }
6034 }
6035 }
6036 return ErrorFound;
6037}
6038
Alexey Bataev49f6e782015-12-01 04:18:41 +00006039StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6040 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6041 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006042 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006043 if (!AStmt)
6044 return StmtError();
6045
6046 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6047 OMPLoopDirective::HelperExprs B;
6048 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6049 // define the nested loops number.
6050 unsigned NestedLoopCount =
6051 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006052 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006053 VarsWithImplicitDSA, B);
6054 if (NestedLoopCount == 0)
6055 return StmtError();
6056
6057 assert((CurContext->isDependentContext() || B.builtAll()) &&
6058 "omp for loop exprs were not built");
6059
Alexey Bataev382967a2015-12-08 12:06:20 +00006060 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6061 // The grainsize clause and num_tasks clause are mutually exclusive and may
6062 // not appear on the same taskloop directive.
6063 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6064 return StmtError();
6065
Alexey Bataev49f6e782015-12-01 04:18:41 +00006066 getCurFunction()->setHasBranchProtectedScope();
6067 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6068 NestedLoopCount, Clauses, AStmt, B);
6069}
6070
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006071StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6072 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6073 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006074 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006075 if (!AStmt)
6076 return StmtError();
6077
6078 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6079 OMPLoopDirective::HelperExprs B;
6080 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6081 // define the nested loops number.
6082 unsigned NestedLoopCount =
6083 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6084 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6085 VarsWithImplicitDSA, B);
6086 if (NestedLoopCount == 0)
6087 return StmtError();
6088
6089 assert((CurContext->isDependentContext() || B.builtAll()) &&
6090 "omp for loop exprs were not built");
6091
Alexey Bataev382967a2015-12-08 12:06:20 +00006092 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6093 // The grainsize clause and num_tasks clause are mutually exclusive and may
6094 // not appear on the same taskloop directive.
6095 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6096 return StmtError();
6097
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006098 getCurFunction()->setHasBranchProtectedScope();
6099 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6100 NestedLoopCount, Clauses, AStmt, B);
6101}
6102
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006103StmtResult Sema::ActOnOpenMPDistributeDirective(
6104 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6105 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006106 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006107 if (!AStmt)
6108 return StmtError();
6109
6110 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6111 OMPLoopDirective::HelperExprs B;
6112 // In presence of clause 'collapse' with number of loops, it will
6113 // define the nested loops number.
6114 unsigned NestedLoopCount =
6115 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6116 nullptr /*ordered not a clause on distribute*/, AStmt,
6117 *this, *DSAStack, VarsWithImplicitDSA, B);
6118 if (NestedLoopCount == 0)
6119 return StmtError();
6120
6121 assert((CurContext->isDependentContext() || B.builtAll()) &&
6122 "omp for loop exprs were not built");
6123
6124 getCurFunction()->setHasBranchProtectedScope();
6125 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6126 NestedLoopCount, Clauses, AStmt, B);
6127}
6128
Alexey Bataeved09d242014-05-28 05:53:51 +00006129OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006130 SourceLocation StartLoc,
6131 SourceLocation LParenLoc,
6132 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006133 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006134 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006135 case OMPC_final:
6136 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6137 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006138 case OMPC_num_threads:
6139 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6140 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006141 case OMPC_safelen:
6142 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6143 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006144 case OMPC_simdlen:
6145 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6146 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006147 case OMPC_collapse:
6148 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6149 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006150 case OMPC_ordered:
6151 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6152 break;
Michael Wonge710d542015-08-07 16:16:36 +00006153 case OMPC_device:
6154 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6155 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006156 case OMPC_num_teams:
6157 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6158 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006159 case OMPC_thread_limit:
6160 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6161 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006162 case OMPC_priority:
6163 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6164 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006165 case OMPC_grainsize:
6166 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6167 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006168 case OMPC_num_tasks:
6169 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6170 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006171 case OMPC_hint:
6172 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6173 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006174 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006175 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006176 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006177 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006178 case OMPC_private:
6179 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006180 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006181 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006182 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006183 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006184 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006185 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006186 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006187 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006188 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006189 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006190 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006191 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006192 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006193 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006194 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006195 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006196 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006197 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006198 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006199 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006200 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006201 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006202 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006203 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006204 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006205 llvm_unreachable("Clause is not allowed.");
6206 }
6207 return Res;
6208}
6209
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006210OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6211 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006212 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006213 SourceLocation NameModifierLoc,
6214 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006215 SourceLocation EndLoc) {
6216 Expr *ValExpr = Condition;
6217 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6218 !Condition->isInstantiationDependent() &&
6219 !Condition->containsUnexpandedParameterPack()) {
6220 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006221 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006222 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006223 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006224
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006225 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006226 }
6227
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006228 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6229 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006230}
6231
Alexey Bataev3778b602014-07-17 07:32:53 +00006232OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6233 SourceLocation StartLoc,
6234 SourceLocation LParenLoc,
6235 SourceLocation EndLoc) {
6236 Expr *ValExpr = Condition;
6237 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6238 !Condition->isInstantiationDependent() &&
6239 !Condition->containsUnexpandedParameterPack()) {
6240 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6241 Condition->getExprLoc(), Condition);
6242 if (Val.isInvalid())
6243 return nullptr;
6244
6245 ValExpr = Val.get();
6246 }
6247
6248 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6249}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006250ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6251 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006252 if (!Op)
6253 return ExprError();
6254
6255 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6256 public:
6257 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006258 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006259 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6260 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006261 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6262 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006263 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6264 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006265 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6266 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006267 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6268 QualType T,
6269 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006270 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6271 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006272 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6273 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006274 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006275 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006276 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006277 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6278 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006279 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6280 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006281 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6282 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006283 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006284 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006285 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006286 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6287 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006288 llvm_unreachable("conversion functions are permitted");
6289 }
6290 } ConvertDiagnoser;
6291 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6292}
6293
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006294static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006295 OpenMPClauseKind CKind,
6296 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006297 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6298 !ValExpr->isInstantiationDependent()) {
6299 SourceLocation Loc = ValExpr->getExprLoc();
6300 ExprResult Value =
6301 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6302 if (Value.isInvalid())
6303 return false;
6304
6305 ValExpr = Value.get();
6306 // The expression must evaluate to a non-negative integer value.
6307 llvm::APSInt Result;
6308 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006309 Result.isSigned() &&
6310 !((!StrictlyPositive && Result.isNonNegative()) ||
6311 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006312 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006313 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6314 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006315 return false;
6316 }
6317 }
6318 return true;
6319}
6320
Alexey Bataev568a8332014-03-06 06:15:19 +00006321OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6322 SourceLocation StartLoc,
6323 SourceLocation LParenLoc,
6324 SourceLocation EndLoc) {
6325 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006326
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006327 // OpenMP [2.5, Restrictions]
6328 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006329 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6330 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006331 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006332
Alexey Bataeved09d242014-05-28 05:53:51 +00006333 return new (Context)
6334 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006335}
6336
Alexey Bataev62c87d22014-03-21 04:51:18 +00006337ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006338 OpenMPClauseKind CKind,
6339 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006340 if (!E)
6341 return ExprError();
6342 if (E->isValueDependent() || E->isTypeDependent() ||
6343 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006344 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006345 llvm::APSInt Result;
6346 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6347 if (ICE.isInvalid())
6348 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006349 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6350 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006351 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006352 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6353 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006354 return ExprError();
6355 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006356 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6357 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6358 << E->getSourceRange();
6359 return ExprError();
6360 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006361 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6362 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006363 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006364 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006365 return ICE;
6366}
6367
6368OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6369 SourceLocation LParenLoc,
6370 SourceLocation EndLoc) {
6371 // OpenMP [2.8.1, simd construct, Description]
6372 // The parameter of the safelen clause must be a constant
6373 // positive integer expression.
6374 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6375 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006376 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006377 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006378 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006379}
6380
Alexey Bataev66b15b52015-08-21 11:14:16 +00006381OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6382 SourceLocation LParenLoc,
6383 SourceLocation EndLoc) {
6384 // OpenMP [2.8.1, simd construct, Description]
6385 // The parameter of the simdlen clause must be a constant
6386 // positive integer expression.
6387 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6388 if (Simdlen.isInvalid())
6389 return nullptr;
6390 return new (Context)
6391 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6392}
6393
Alexander Musman64d33f12014-06-04 07:53:32 +00006394OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6395 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006396 SourceLocation LParenLoc,
6397 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006398 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006399 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006400 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006401 // The parameter of the collapse clause must be a constant
6402 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006403 ExprResult NumForLoopsResult =
6404 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6405 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006406 return nullptr;
6407 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006408 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006409}
6410
Alexey Bataev10e775f2015-07-30 11:36:16 +00006411OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6412 SourceLocation EndLoc,
6413 SourceLocation LParenLoc,
6414 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006415 // OpenMP [2.7.1, loop construct, Description]
6416 // OpenMP [2.8.1, simd construct, Description]
6417 // OpenMP [2.9.6, distribute construct, Description]
6418 // The parameter of the ordered clause must be a constant
6419 // positive integer expression if any.
6420 if (NumForLoops && LParenLoc.isValid()) {
6421 ExprResult NumForLoopsResult =
6422 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6423 if (NumForLoopsResult.isInvalid())
6424 return nullptr;
6425 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006426 } else
6427 NumForLoops = nullptr;
6428 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006429 return new (Context)
6430 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6431}
6432
Alexey Bataeved09d242014-05-28 05:53:51 +00006433OMPClause *Sema::ActOnOpenMPSimpleClause(
6434 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6435 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006436 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006437 switch (Kind) {
6438 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006439 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006440 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6441 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006442 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006443 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006444 Res = ActOnOpenMPProcBindClause(
6445 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6446 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006447 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006448 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006449 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006450 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006451 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006452 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006453 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006454 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006455 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006456 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006457 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006458 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006459 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006460 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006461 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006462 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006463 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006464 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006465 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006466 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006467 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006468 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006469 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006470 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006471 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006472 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006473 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006474 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006475 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006476 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006477 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006478 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006479 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006480 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006481 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006482 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006483 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006484 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006485 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006486 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006487 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006488 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006489 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006490 llvm_unreachable("Clause is not allowed.");
6491 }
6492 return Res;
6493}
6494
Alexey Bataev6402bca2015-12-28 07:25:51 +00006495static std::string
6496getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6497 ArrayRef<unsigned> Exclude = llvm::None) {
6498 std::string Values;
6499 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6500 unsigned Skipped = Exclude.size();
6501 auto S = Exclude.begin(), E = Exclude.end();
6502 for (unsigned i = First; i < Last; ++i) {
6503 if (std::find(S, E, i) != E) {
6504 --Skipped;
6505 continue;
6506 }
6507 Values += "'";
6508 Values += getOpenMPSimpleClauseTypeName(K, i);
6509 Values += "'";
6510 if (i == Bound - Skipped)
6511 Values += " or ";
6512 else if (i != Bound + 1 - Skipped)
6513 Values += ", ";
6514 }
6515 return Values;
6516}
6517
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006518OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6519 SourceLocation KindKwLoc,
6520 SourceLocation StartLoc,
6521 SourceLocation LParenLoc,
6522 SourceLocation EndLoc) {
6523 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006524 static_assert(OMPC_DEFAULT_unknown > 0,
6525 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006526 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006527 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6528 /*Last=*/OMPC_DEFAULT_unknown)
6529 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006530 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006531 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006532 switch (Kind) {
6533 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006534 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006535 break;
6536 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006537 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006538 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006539 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006540 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006541 break;
6542 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006543 return new (Context)
6544 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006545}
6546
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006547OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6548 SourceLocation KindKwLoc,
6549 SourceLocation StartLoc,
6550 SourceLocation LParenLoc,
6551 SourceLocation EndLoc) {
6552 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006553 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006554 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6555 /*Last=*/OMPC_PROC_BIND_unknown)
6556 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006557 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006558 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006559 return new (Context)
6560 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006561}
6562
Alexey Bataev56dafe82014-06-20 07:16:17 +00006563OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006564 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006565 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006566 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006567 SourceLocation EndLoc) {
6568 OMPClause *Res = nullptr;
6569 switch (Kind) {
6570 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006571 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6572 assert(Argument.size() == NumberOfElements &&
6573 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006574 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006575 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6576 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6577 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6578 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6579 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006580 break;
6581 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006582 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6583 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6584 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6585 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006586 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006587 case OMPC_dist_schedule:
6588 Res = ActOnOpenMPDistScheduleClause(
6589 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6590 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6591 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006592 case OMPC_defaultmap:
6593 enum { Modifier, DefaultmapKind };
6594 Res = ActOnOpenMPDefaultmapClause(
6595 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6596 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6597 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6598 ArgumentLoc[DefaultmapKind], EndLoc);
6599 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006600 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006601 case OMPC_num_threads:
6602 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006603 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006604 case OMPC_collapse:
6605 case OMPC_default:
6606 case OMPC_proc_bind:
6607 case OMPC_private:
6608 case OMPC_firstprivate:
6609 case OMPC_lastprivate:
6610 case OMPC_shared:
6611 case OMPC_reduction:
6612 case OMPC_linear:
6613 case OMPC_aligned:
6614 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006615 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006616 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006617 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006618 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006619 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006620 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006621 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006622 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006623 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006624 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006625 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006626 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006627 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006628 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006629 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006630 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006631 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006632 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006633 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006634 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006635 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006636 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006637 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006638 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006639 case OMPC_unknown:
6640 llvm_unreachable("Clause is not allowed.");
6641 }
6642 return Res;
6643}
6644
Alexey Bataev6402bca2015-12-28 07:25:51 +00006645static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6646 OpenMPScheduleClauseModifier M2,
6647 SourceLocation M1Loc, SourceLocation M2Loc) {
6648 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6649 SmallVector<unsigned, 2> Excluded;
6650 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6651 Excluded.push_back(M2);
6652 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6653 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6654 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6655 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6656 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6657 << getListOfPossibleValues(OMPC_schedule,
6658 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6659 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6660 Excluded)
6661 << getOpenMPClauseName(OMPC_schedule);
6662 return true;
6663 }
6664 return false;
6665}
6666
Alexey Bataev56dafe82014-06-20 07:16:17 +00006667OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006668 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006669 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006670 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6671 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6672 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6673 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6674 return nullptr;
6675 // OpenMP, 2.7.1, Loop Construct, Restrictions
6676 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6677 // but not both.
6678 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6679 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6680 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6681 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6682 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6683 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6684 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6685 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6686 return nullptr;
6687 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006688 if (Kind == OMPC_SCHEDULE_unknown) {
6689 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006690 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6691 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6692 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6693 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6694 Exclude);
6695 } else {
6696 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6697 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006698 }
6699 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6700 << Values << getOpenMPClauseName(OMPC_schedule);
6701 return nullptr;
6702 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006703 // OpenMP, 2.7.1, Loop Construct, Restrictions
6704 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6705 // schedule(guided).
6706 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6707 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6708 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6709 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6710 diag::err_omp_schedule_nonmonotonic_static);
6711 return nullptr;
6712 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006713 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006714 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006715 if (ChunkSize) {
6716 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6717 !ChunkSize->isInstantiationDependent() &&
6718 !ChunkSize->containsUnexpandedParameterPack()) {
6719 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6720 ExprResult Val =
6721 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6722 if (Val.isInvalid())
6723 return nullptr;
6724
6725 ValExpr = Val.get();
6726
6727 // OpenMP [2.7.1, Restrictions]
6728 // chunk_size must be a loop invariant integer expression with a positive
6729 // value.
6730 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006731 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6732 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6733 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006734 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006735 return nullptr;
6736 }
6737 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6738 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6739 ChunkSize->getType(), ".chunk.");
6740 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6741 ChunkSize->getExprLoc(),
6742 /*RefersToCapture=*/true);
6743 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006744 }
6745 }
6746 }
6747
Alexey Bataev6402bca2015-12-28 07:25:51 +00006748 return new (Context)
6749 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6750 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006751}
6752
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006753OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6754 SourceLocation StartLoc,
6755 SourceLocation EndLoc) {
6756 OMPClause *Res = nullptr;
6757 switch (Kind) {
6758 case OMPC_ordered:
6759 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6760 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006761 case OMPC_nowait:
6762 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6763 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006764 case OMPC_untied:
6765 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6766 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006767 case OMPC_mergeable:
6768 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6769 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006770 case OMPC_read:
6771 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6772 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006773 case OMPC_write:
6774 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6775 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006776 case OMPC_update:
6777 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6778 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006779 case OMPC_capture:
6780 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6781 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006782 case OMPC_seq_cst:
6783 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6784 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006785 case OMPC_threads:
6786 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6787 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006788 case OMPC_simd:
6789 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6790 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006791 case OMPC_nogroup:
6792 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6793 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006794 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006795 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006796 case OMPC_num_threads:
6797 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006798 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006799 case OMPC_collapse:
6800 case OMPC_schedule:
6801 case OMPC_private:
6802 case OMPC_firstprivate:
6803 case OMPC_lastprivate:
6804 case OMPC_shared:
6805 case OMPC_reduction:
6806 case OMPC_linear:
6807 case OMPC_aligned:
6808 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006809 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006810 case OMPC_default:
6811 case OMPC_proc_bind:
6812 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006813 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006814 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006815 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006816 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006817 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006818 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006819 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006820 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006821 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006822 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006823 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006824 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006825 case OMPC_unknown:
6826 llvm_unreachable("Clause is not allowed.");
6827 }
6828 return Res;
6829}
6830
Alexey Bataev236070f2014-06-20 11:19:47 +00006831OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6832 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006833 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006834 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6835}
6836
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006837OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6838 SourceLocation EndLoc) {
6839 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6840}
6841
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006842OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6843 SourceLocation EndLoc) {
6844 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6845}
6846
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006847OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6848 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006849 return new (Context) OMPReadClause(StartLoc, EndLoc);
6850}
6851
Alexey Bataevdea47612014-07-23 07:46:59 +00006852OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6853 SourceLocation EndLoc) {
6854 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6855}
6856
Alexey Bataev67a4f222014-07-23 10:25:33 +00006857OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6858 SourceLocation EndLoc) {
6859 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6860}
6861
Alexey Bataev459dec02014-07-24 06:46:57 +00006862OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6863 SourceLocation EndLoc) {
6864 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6865}
6866
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006867OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6868 SourceLocation EndLoc) {
6869 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6870}
6871
Alexey Bataev346265e2015-09-25 10:37:12 +00006872OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6873 SourceLocation EndLoc) {
6874 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6875}
6876
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006877OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6878 SourceLocation EndLoc) {
6879 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6880}
6881
Alexey Bataevb825de12015-12-07 10:51:44 +00006882OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6883 SourceLocation EndLoc) {
6884 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6885}
6886
Alexey Bataevc5e02582014-06-16 07:08:35 +00006887OMPClause *Sema::ActOnOpenMPVarListClause(
6888 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6889 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6890 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006891 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006892 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6893 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6894 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006895 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006896 switch (Kind) {
6897 case OMPC_private:
6898 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6899 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006900 case OMPC_firstprivate:
6901 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6902 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006903 case OMPC_lastprivate:
6904 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6905 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006906 case OMPC_shared:
6907 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6908 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006909 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006910 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6911 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006912 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006913 case OMPC_linear:
6914 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006915 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006916 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006917 case OMPC_aligned:
6918 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6919 ColonLoc, EndLoc);
6920 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006921 case OMPC_copyin:
6922 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6923 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006924 case OMPC_copyprivate:
6925 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6926 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006927 case OMPC_flush:
6928 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6929 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006930 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006931 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6932 StartLoc, LParenLoc, EndLoc);
6933 break;
6934 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006935 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6936 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6937 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006938 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006939 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006940 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006941 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006942 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006943 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006944 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006945 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006946 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006947 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006948 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006949 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006950 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006951 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006952 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006953 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006954 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006955 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006956 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006957 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006958 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006959 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006960 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006961 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006962 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006963 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006964 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006965 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006966 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006967 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006968 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006969 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006970 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006971 llvm_unreachable("Clause is not allowed.");
6972 }
6973 return Res;
6974}
6975
Alexey Bataev90c228f2016-02-08 09:29:13 +00006976static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
6977 Expr *CaptureExpr) {
6978 ASTContext &C = S.getASTContext();
6979 Expr *Init = CaptureExpr->IgnoreImpCasts();
6980 QualType Ty = Init->getType();
6981 if (CaptureExpr->getObjectKind() == OK_Ordinary) {
6982 if (S.getLangOpts().CPlusPlus)
6983 Ty = C.getLValueReferenceType(Ty);
6984 else {
6985 Ty = C.getPointerType(Ty);
6986 ExprResult Res =
6987 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
6988 if (!Res.isUsable())
6989 return nullptr;
6990 Init = Res.get();
6991 }
6992 }
6993 auto *CFD = OMPCapturedFieldDecl::Create(C, S.CurContext, Id, Ty);
6994 S.CurContext->addHiddenDecl(CFD);
6995 S.AddInitializerToDecl(CFD, Init, /*DirectInit=*/false,
6996 /*TypeMayContainAuto=*/true);
6997 return buildDeclRefExpr(S, CFD, Ty.getNonReferenceType(), SourceLocation());
6998}
6999
7000ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7001 ExprObjectKind OK) {
7002 SourceLocation Loc = Capture->getInit()->getExprLoc();
7003 ExprResult Res = BuildDeclRefExpr(
7004 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7005 if (!Res.isUsable())
7006 return ExprError();
7007 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7008 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7009 if (!Res.isUsable())
7010 return ExprError();
7011 }
7012 if (VK != VK_LValue && Res.get()->isGLValue()) {
7013 Res = DefaultLvalueConversion(Res.get());
7014 if (!Res.isUsable())
7015 return ExprError();
7016 }
7017 return Res;
7018}
7019
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007020OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7021 SourceLocation StartLoc,
7022 SourceLocation LParenLoc,
7023 SourceLocation EndLoc) {
7024 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007025 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007026 for (auto &RefExpr : VarList) {
7027 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007028 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7029 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007030 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007031 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007032 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007033 continue;
7034 }
7035
Alexey Bataeved09d242014-05-28 05:53:51 +00007036 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00007037 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007038 // A list item is a variable name.
7039 // OpenMP [2.9.3.3, Restrictions, p.1]
7040 // A variable that is part of another variable (as an array or
7041 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007042 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
7043 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
7044 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7045 (getCurrentThisType().isNull() || !ME ||
7046 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7047 !isa<FieldDecl>(ME->getMemberDecl()))) {
7048 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7049 << (getCurrentThisType().isNull() ? 0 : 1)
7050 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007051 continue;
7052 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007053 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
7054 QualType Type = D->getType();
7055 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007056
7057 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7058 // A variable that appears in a private clause must not have an incomplete
7059 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007060 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007061 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007062 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007063
Alexey Bataev758e55e2013-09-06 18:03:48 +00007064 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7065 // in a Construct]
7066 // Variables with the predetermined data-sharing attributes may not be
7067 // listed in data-sharing attributes clauses, except for the cases
7068 // listed below. For these exceptions only, listing a predetermined
7069 // variable in a data-sharing attribute clause is allowed and overrides
7070 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007071 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007072 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007073 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7074 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007075 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007076 continue;
7077 }
7078
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007079 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007080 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007081 DSAStack->getCurrentDirective() == OMPD_task) {
7082 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7083 << getOpenMPClauseName(OMPC_private) << Type
7084 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7085 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007086 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007087 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007088 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007089 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007090 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007091 continue;
7092 }
7093
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007094 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7095 // A variable of class type (or array thereof) that appears in a private
7096 // clause requires an accessible, unambiguous default constructor for the
7097 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007098 // Generate helper private variable and initialize it with the default
7099 // value. The address of the original variable is replaced by the address of
7100 // the new private variable in CodeGen. This new variable is not added to
7101 // IdResolver, so the code in the OpenMP region uses original variable for
7102 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007103 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007104 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7105 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007106 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007107 if (VDPrivate->isInvalidDecl())
7108 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007109 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007110 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007111
Alexey Bataev90c228f2016-02-08 09:29:13 +00007112 DeclRefExpr *Ref = nullptr;
7113 if (!VD)
7114 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7115 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7116 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007117 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007118 }
7119
Alexey Bataeved09d242014-05-28 05:53:51 +00007120 if (Vars.empty())
7121 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007122
Alexey Bataev03b340a2014-10-21 03:16:40 +00007123 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7124 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007125}
7126
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007127namespace {
7128class DiagsUninitializedSeveretyRAII {
7129private:
7130 DiagnosticsEngine &Diags;
7131 SourceLocation SavedLoc;
7132 bool IsIgnored;
7133
7134public:
7135 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7136 bool IsIgnored)
7137 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7138 if (!IsIgnored) {
7139 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7140 /*Map*/ diag::Severity::Ignored, Loc);
7141 }
7142 }
7143 ~DiagsUninitializedSeveretyRAII() {
7144 if (!IsIgnored)
7145 Diags.popMappings(SavedLoc);
7146 }
7147};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007148}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007149
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007150OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7151 SourceLocation StartLoc,
7152 SourceLocation LParenLoc,
7153 SourceLocation EndLoc) {
7154 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007155 SmallVector<Expr *, 8> PrivateCopies;
7156 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007157 bool IsImplicitClause =
7158 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7159 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7160
Alexey Bataeved09d242014-05-28 05:53:51 +00007161 for (auto &RefExpr : VarList) {
7162 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
7163 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007164 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007165 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007166 PrivateCopies.push_back(nullptr);
7167 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007168 continue;
7169 }
7170
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007171 SourceLocation ELoc =
7172 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007173 // OpenMP [2.1, C/C++]
7174 // A list item is a variable name.
7175 // OpenMP [2.9.3.3, Restrictions, p.1]
7176 // A variable that is part of another variable (as an array or
7177 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007178 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007179 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007180 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7181 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007182 continue;
7183 }
7184 Decl *D = DE->getDecl();
7185 VarDecl *VD = cast<VarDecl>(D);
7186
7187 QualType Type = VD->getType();
7188 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7189 // It will be analyzed later.
7190 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007191 PrivateCopies.push_back(nullptr);
7192 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007193 continue;
7194 }
7195
7196 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7197 // A variable that appears in a private clause must not have an incomplete
7198 // type or a reference type.
7199 if (RequireCompleteType(ELoc, Type,
7200 diag::err_omp_firstprivate_incomplete_type)) {
7201 continue;
7202 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007203 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007204
7205 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7206 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007207 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007208 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007209 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007210
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007211 // If an implicit firstprivate variable found it was checked already.
7212 if (!IsImplicitClause) {
7213 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007214 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007215 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7216 // A list item that specifies a given variable may not appear in more
7217 // than one clause on the same directive, except that a variable may be
7218 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007219 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007220 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007221 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007222 << getOpenMPClauseName(DVar.CKind)
7223 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007224 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007225 continue;
7226 }
7227
7228 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7229 // in a Construct]
7230 // Variables with the predetermined data-sharing attributes may not be
7231 // listed in data-sharing attributes clauses, except for the cases
7232 // listed below. For these exceptions only, listing a predetermined
7233 // variable in a data-sharing attribute clause is allowed and overrides
7234 // the variable's predetermined data-sharing attributes.
7235 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7236 // in a Construct, C/C++, p.2]
7237 // Variables with const-qualified type having no mutable member may be
7238 // listed in a firstprivate clause, even if they are static data members.
7239 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7240 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7241 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007242 << getOpenMPClauseName(DVar.CKind)
7243 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007244 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007245 continue;
7246 }
7247
Alexey Bataevf29276e2014-06-18 04:14:57 +00007248 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007249 // OpenMP [2.9.3.4, Restrictions, p.2]
7250 // A list item that is private within a parallel region must not appear
7251 // in a firstprivate clause on a worksharing construct if any of the
7252 // worksharing regions arising from the worksharing construct ever bind
7253 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007254 if (isOpenMPWorksharingDirective(CurrDir) &&
7255 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007256 DVar = DSAStack->getImplicitDSA(VD, true);
7257 if (DVar.CKind != OMPC_shared &&
7258 (isOpenMPParallelDirective(DVar.DKind) ||
7259 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007260 Diag(ELoc, diag::err_omp_required_access)
7261 << getOpenMPClauseName(OMPC_firstprivate)
7262 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007263 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007264 continue;
7265 }
7266 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007267 // OpenMP [2.9.3.4, Restrictions, p.3]
7268 // A list item that appears in a reduction clause of a parallel construct
7269 // must not appear in a firstprivate clause on a worksharing or task
7270 // construct if any of the worksharing or task regions arising from the
7271 // worksharing or task construct ever bind to any of the parallel regions
7272 // arising from the parallel construct.
7273 // OpenMP [2.9.3.4, Restrictions, p.4]
7274 // A list item that appears in a reduction clause in worksharing
7275 // construct must not appear in a firstprivate clause in a task construct
7276 // encountered during execution of any of the worksharing regions arising
7277 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007278 if (CurrDir == OMPD_task) {
7279 DVar =
7280 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7281 [](OpenMPDirectiveKind K) -> bool {
7282 return isOpenMPParallelDirective(K) ||
7283 isOpenMPWorksharingDirective(K);
7284 },
7285 false);
7286 if (DVar.CKind == OMPC_reduction &&
7287 (isOpenMPParallelDirective(DVar.DKind) ||
7288 isOpenMPWorksharingDirective(DVar.DKind))) {
7289 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7290 << getOpenMPDirectiveName(DVar.DKind);
7291 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7292 continue;
7293 }
7294 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007295
7296 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7297 // A list item that is private within a teams region must not appear in a
7298 // firstprivate clause on a distribute construct if any of the distribute
7299 // regions arising from the distribute construct ever bind to any of the
7300 // teams regions arising from the teams construct.
7301 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7302 // A list item that appears in a reduction clause of a teams construct
7303 // must not appear in a firstprivate clause on a distribute construct if
7304 // any of the distribute regions arising from the distribute construct
7305 // ever bind to any of the teams regions arising from the teams construct.
7306 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7307 // A list item may appear in a firstprivate or lastprivate clause but not
7308 // both.
7309 if (CurrDir == OMPD_distribute) {
7310 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7311 [](OpenMPDirectiveKind K) -> bool {
7312 return isOpenMPTeamsDirective(K);
7313 },
7314 false);
7315 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7316 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7317 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7318 continue;
7319 }
7320 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7321 [](OpenMPDirectiveKind K) -> bool {
7322 return isOpenMPTeamsDirective(K);
7323 },
7324 false);
7325 if (DVar.CKind == OMPC_reduction &&
7326 isOpenMPTeamsDirective(DVar.DKind)) {
7327 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7328 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7329 continue;
7330 }
7331 DVar = DSAStack->getTopDSA(VD, false);
7332 if (DVar.CKind == OMPC_lastprivate) {
7333 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7334 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7335 continue;
7336 }
7337 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007338 }
7339
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007340 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007341 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007342 DSAStack->getCurrentDirective() == OMPD_task) {
7343 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7344 << getOpenMPClauseName(OMPC_firstprivate) << Type
7345 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7346 bool IsDecl =
7347 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7348 Diag(VD->getLocation(),
7349 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7350 << VD;
7351 continue;
7352 }
7353
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007354 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007355 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7356 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007357 // Generate helper private variable and initialize it with the value of the
7358 // original variable. The address of the original variable is replaced by
7359 // the address of the new private variable in the CodeGen. This new variable
7360 // is not added to IdResolver, so the code in the OpenMP region uses
7361 // original variable for proper diagnostics and variable capturing.
7362 Expr *VDInitRefExpr = nullptr;
7363 // For arrays generate initializer for single element and replace it by the
7364 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007365 if (Type->isArrayType()) {
7366 auto VDInit =
7367 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7368 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007369 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007370 ElemType = ElemType.getUnqualifiedType();
7371 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7372 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007373 InitializedEntity Entity =
7374 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007375 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7376
7377 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7378 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7379 if (Result.isInvalid())
7380 VDPrivate->setInvalidDecl();
7381 else
7382 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007383 // Remove temp variable declaration.
7384 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007385 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007386 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007387 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007388 VDInitRefExpr =
7389 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007390 AddInitializerToDecl(VDPrivate,
7391 DefaultLvalueConversion(VDInitRefExpr).get(),
7392 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007393 }
7394 if (VDPrivate->isInvalidDecl()) {
7395 if (IsImplicitClause) {
7396 Diag(DE->getExprLoc(),
7397 diag::note_omp_task_predetermined_firstprivate_here);
7398 }
7399 continue;
7400 }
7401 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007402 auto VDPrivateRefExpr = buildDeclRefExpr(
7403 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007404 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7405 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007406 PrivateCopies.push_back(VDPrivateRefExpr);
7407 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007408 }
7409
Alexey Bataeved09d242014-05-28 05:53:51 +00007410 if (Vars.empty())
7411 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007412
7413 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007414 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007415}
7416
Alexander Musman1bb328c2014-06-04 13:06:39 +00007417OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7418 SourceLocation StartLoc,
7419 SourceLocation LParenLoc,
7420 SourceLocation EndLoc) {
7421 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007422 SmallVector<Expr *, 8> SrcExprs;
7423 SmallVector<Expr *, 8> DstExprs;
7424 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007425 for (auto &RefExpr : VarList) {
7426 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7427 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7428 // It will be analyzed later.
7429 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007430 SrcExprs.push_back(nullptr);
7431 DstExprs.push_back(nullptr);
7432 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007433 continue;
7434 }
7435
7436 SourceLocation ELoc = RefExpr->getExprLoc();
7437 // OpenMP [2.1, C/C++]
7438 // A list item is a variable name.
7439 // OpenMP [2.14.3.5, Restrictions, p.1]
7440 // A variable that is part of another variable (as an array or structure
7441 // element) cannot appear in a lastprivate clause.
7442 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7443 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007444 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7445 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007446 continue;
7447 }
7448 Decl *D = DE->getDecl();
7449 VarDecl *VD = cast<VarDecl>(D);
7450
7451 QualType Type = VD->getType();
7452 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7453 // It will be analyzed later.
7454 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007455 SrcExprs.push_back(nullptr);
7456 DstExprs.push_back(nullptr);
7457 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007458 continue;
7459 }
7460
7461 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7462 // A variable that appears in a lastprivate clause must not have an
7463 // incomplete type or a reference type.
7464 if (RequireCompleteType(ELoc, Type,
7465 diag::err_omp_lastprivate_incomplete_type)) {
7466 continue;
7467 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007468 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007469
7470 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7471 // in a Construct]
7472 // Variables with the predetermined data-sharing attributes may not be
7473 // listed in data-sharing attributes clauses, except for the cases
7474 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007475 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007476 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7477 DVar.CKind != OMPC_firstprivate &&
7478 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7479 Diag(ELoc, diag::err_omp_wrong_dsa)
7480 << getOpenMPClauseName(DVar.CKind)
7481 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007482 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007483 continue;
7484 }
7485
Alexey Bataevf29276e2014-06-18 04:14:57 +00007486 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7487 // OpenMP [2.14.3.5, Restrictions, p.2]
7488 // A list item that is private within a parallel region, or that appears in
7489 // the reduction clause of a parallel construct, must not appear in a
7490 // lastprivate clause on a worksharing construct if any of the corresponding
7491 // worksharing regions ever binds to any of the corresponding parallel
7492 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007493 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007494 if (isOpenMPWorksharingDirective(CurrDir) &&
7495 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007496 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007497 if (DVar.CKind != OMPC_shared) {
7498 Diag(ELoc, diag::err_omp_required_access)
7499 << getOpenMPClauseName(OMPC_lastprivate)
7500 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007501 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007502 continue;
7503 }
7504 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007505 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007506 // A variable of class type (or array thereof) that appears in a
7507 // lastprivate clause requires an accessible, unambiguous default
7508 // constructor for the class type, unless the list item is also specified
7509 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007510 // A variable of class type (or array thereof) that appears in a
7511 // lastprivate clause requires an accessible, unambiguous copy assignment
7512 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007513 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007514 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007515 Type.getUnqualifiedType(), ".lastprivate.src",
7516 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007517 auto *PseudoSrcExpr = buildDeclRefExpr(
7518 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007519 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007520 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7521 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007522 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007523 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007524 // For arrays generate assignment operation for single element and replace
7525 // it by the original array element in CodeGen.
7526 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7527 PseudoDstExpr, PseudoSrcExpr);
7528 if (AssignmentOp.isInvalid())
7529 continue;
7530 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7531 /*DiscardedValue=*/true);
7532 if (AssignmentOp.isInvalid())
7533 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007534
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007535 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7536 // A list item may appear in a firstprivate or lastprivate clause but not
7537 // both.
7538 if (CurrDir == OMPD_distribute) {
7539 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7540 if (DVar.CKind == OMPC_firstprivate) {
7541 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7542 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7543 continue;
7544 }
7545 }
7546
Alexey Bataev39f915b82015-05-08 10:41:21 +00007547 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007548 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007549 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007550 SrcExprs.push_back(PseudoSrcExpr);
7551 DstExprs.push_back(PseudoDstExpr);
7552 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007553 }
7554
7555 if (Vars.empty())
7556 return nullptr;
7557
7558 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007559 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007560}
7561
Alexey Bataev758e55e2013-09-06 18:03:48 +00007562OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7563 SourceLocation StartLoc,
7564 SourceLocation LParenLoc,
7565 SourceLocation EndLoc) {
7566 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007567 for (auto &RefExpr : VarList) {
7568 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7569 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007570 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007571 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007572 continue;
7573 }
7574
Alexey Bataeved09d242014-05-28 05:53:51 +00007575 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007576 // OpenMP [2.1, C/C++]
7577 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007578 // OpenMP [2.14.3.2, Restrictions, p.1]
7579 // A variable that is part of another variable (as an array or structure
7580 // element) cannot appear in a shared unless it is a static data member
7581 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007582 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007583 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007584 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7585 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007586 continue;
7587 }
7588 Decl *D = DE->getDecl();
7589 VarDecl *VD = cast<VarDecl>(D);
7590
7591 QualType Type = VD->getType();
7592 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7593 // It will be analyzed later.
7594 Vars.push_back(DE);
7595 continue;
7596 }
7597
7598 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7599 // in a Construct]
7600 // Variables with the predetermined data-sharing attributes may not be
7601 // listed in data-sharing attributes clauses, except for the cases
7602 // listed below. For these exceptions only, listing a predetermined
7603 // variable in a data-sharing attribute clause is allowed and overrides
7604 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007605 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007606 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7607 DVar.RefExpr) {
7608 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7609 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007610 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007611 continue;
7612 }
7613
7614 DSAStack->addDSA(VD, DE, OMPC_shared);
7615 Vars.push_back(DE);
7616 }
7617
Alexey Bataeved09d242014-05-28 05:53:51 +00007618 if (Vars.empty())
7619 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007620
7621 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7622}
7623
Alexey Bataevc5e02582014-06-16 07:08:35 +00007624namespace {
7625class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7626 DSAStackTy *Stack;
7627
7628public:
7629 bool VisitDeclRefExpr(DeclRefExpr *E) {
7630 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007631 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007632 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7633 return false;
7634 if (DVar.CKind != OMPC_unknown)
7635 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007636 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007637 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007638 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007639 return true;
7640 return false;
7641 }
7642 return false;
7643 }
7644 bool VisitStmt(Stmt *S) {
7645 for (auto Child : S->children()) {
7646 if (Child && Visit(Child))
7647 return true;
7648 }
7649 return false;
7650 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007651 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007652};
Alexey Bataev23b69422014-06-18 07:08:49 +00007653} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007654
7655OMPClause *Sema::ActOnOpenMPReductionClause(
7656 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7657 SourceLocation ColonLoc, SourceLocation EndLoc,
7658 CXXScopeSpec &ReductionIdScopeSpec,
7659 const DeclarationNameInfo &ReductionId) {
7660 // TODO: Allow scope specification search when 'declare reduction' is
7661 // supported.
7662 assert(ReductionIdScopeSpec.isEmpty() &&
7663 "No support for scoped reduction identifiers yet.");
7664
7665 auto DN = ReductionId.getName();
7666 auto OOK = DN.getCXXOverloadedOperator();
7667 BinaryOperatorKind BOK = BO_Comma;
7668
7669 // OpenMP [2.14.3.6, reduction clause]
7670 // C
7671 // reduction-identifier is either an identifier or one of the following
7672 // operators: +, -, *, &, |, ^, && and ||
7673 // C++
7674 // reduction-identifier is either an id-expression or one of the following
7675 // operators: +, -, *, &, |, ^, && and ||
7676 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7677 switch (OOK) {
7678 case OO_Plus:
7679 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007680 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007681 break;
7682 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007683 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007684 break;
7685 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007686 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007687 break;
7688 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007689 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007690 break;
7691 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007692 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007693 break;
7694 case OO_AmpAmp:
7695 BOK = BO_LAnd;
7696 break;
7697 case OO_PipePipe:
7698 BOK = BO_LOr;
7699 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007700 case OO_New:
7701 case OO_Delete:
7702 case OO_Array_New:
7703 case OO_Array_Delete:
7704 case OO_Slash:
7705 case OO_Percent:
7706 case OO_Tilde:
7707 case OO_Exclaim:
7708 case OO_Equal:
7709 case OO_Less:
7710 case OO_Greater:
7711 case OO_LessEqual:
7712 case OO_GreaterEqual:
7713 case OO_PlusEqual:
7714 case OO_MinusEqual:
7715 case OO_StarEqual:
7716 case OO_SlashEqual:
7717 case OO_PercentEqual:
7718 case OO_CaretEqual:
7719 case OO_AmpEqual:
7720 case OO_PipeEqual:
7721 case OO_LessLess:
7722 case OO_GreaterGreater:
7723 case OO_LessLessEqual:
7724 case OO_GreaterGreaterEqual:
7725 case OO_EqualEqual:
7726 case OO_ExclaimEqual:
7727 case OO_PlusPlus:
7728 case OO_MinusMinus:
7729 case OO_Comma:
7730 case OO_ArrowStar:
7731 case OO_Arrow:
7732 case OO_Call:
7733 case OO_Subscript:
7734 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007735 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007736 case NUM_OVERLOADED_OPERATORS:
7737 llvm_unreachable("Unexpected reduction identifier");
7738 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007739 if (auto II = DN.getAsIdentifierInfo()) {
7740 if (II->isStr("max"))
7741 BOK = BO_GT;
7742 else if (II->isStr("min"))
7743 BOK = BO_LT;
7744 }
7745 break;
7746 }
7747 SourceRange ReductionIdRange;
7748 if (ReductionIdScopeSpec.isValid()) {
7749 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7750 }
7751 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7752 if (BOK == BO_Comma) {
7753 // Not allowed reduction identifier is found.
7754 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7755 << ReductionIdRange;
7756 return nullptr;
7757 }
7758
7759 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007760 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007761 SmallVector<Expr *, 8> LHSs;
7762 SmallVector<Expr *, 8> RHSs;
7763 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007764 for (auto RefExpr : VarList) {
7765 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7766 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7767 // It will be analyzed later.
7768 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007769 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007770 LHSs.push_back(nullptr);
7771 RHSs.push_back(nullptr);
7772 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007773 continue;
7774 }
7775
7776 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7777 RefExpr->isInstantiationDependent() ||
7778 RefExpr->containsUnexpandedParameterPack()) {
7779 // It will be analyzed later.
7780 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007781 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007782 LHSs.push_back(nullptr);
7783 RHSs.push_back(nullptr);
7784 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007785 continue;
7786 }
7787
7788 auto ELoc = RefExpr->getExprLoc();
7789 auto ERange = RefExpr->getSourceRange();
7790 // OpenMP [2.1, C/C++]
7791 // A list item is a variable or array section, subject to the restrictions
7792 // specified in Section 2.4 on page 42 and in each of the sections
7793 // describing clauses and directives for which a list appears.
7794 // OpenMP [2.14.3.3, Restrictions, p.1]
7795 // A variable that is part of another variable (as an array or
7796 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007797 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7798 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7799 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7800 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007801 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7802 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007803 continue;
7804 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007805 QualType Type;
7806 VarDecl *VD = nullptr;
7807 if (DE) {
7808 auto D = DE->getDecl();
7809 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007810 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007811 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007812 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007813 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7814 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7815 Base = TempASE->getBase()->IgnoreParenImpCasts();
7816 DE = dyn_cast<DeclRefExpr>(Base);
7817 if (DE)
7818 VD = dyn_cast<VarDecl>(DE->getDecl());
7819 if (!VD) {
7820 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7821 << 0 << Base->getSourceRange();
7822 continue;
7823 }
7824 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007825 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7826 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7827 Type = ATy->getElementType();
7828 else
7829 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007830 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007831 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7832 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7833 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7834 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7835 Base = TempASE->getBase()->IgnoreParenImpCasts();
7836 DE = dyn_cast<DeclRefExpr>(Base);
7837 if (DE)
7838 VD = dyn_cast<VarDecl>(DE->getDecl());
7839 if (!VD) {
7840 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7841 << 1 << Base->getSourceRange();
7842 continue;
7843 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007844 }
7845
Alexey Bataevc5e02582014-06-16 07:08:35 +00007846 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7847 // A variable that appears in a private clause must not have an incomplete
7848 // type or a reference type.
7849 if (RequireCompleteType(ELoc, Type,
7850 diag::err_omp_reduction_incomplete_type))
7851 continue;
7852 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007853 // A list item that appears in a reduction clause must not be
7854 // const-qualified.
7855 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007856 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007857 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007858 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007859 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7860 VarDecl::DeclarationOnly;
7861 Diag(VD->getLocation(),
7862 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7863 << VD;
7864 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007865 continue;
7866 }
7867 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7868 // If a list-item is a reference type then it must bind to the same object
7869 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007870 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007871 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007872 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007873 DSARefChecker Check(DSAStack);
7874 if (Check.Visit(VDDef->getInit())) {
7875 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7876 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7877 continue;
7878 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007879 }
7880 }
7881 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7882 // The type of a list item that appears in a reduction clause must be valid
7883 // for the reduction-identifier. For a max or min reduction in C, the type
7884 // of the list item must be an allowed arithmetic data type: char, int,
7885 // float, double, or _Bool, possibly modified with long, short, signed, or
7886 // unsigned. For a max or min reduction in C++, the type of the list item
7887 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7888 // double, or bool, possibly modified with long, short, signed, or unsigned.
7889 if ((BOK == BO_GT || BOK == BO_LT) &&
7890 !(Type->isScalarType() ||
7891 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7892 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7893 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007894 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007895 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7896 VarDecl::DeclarationOnly;
7897 Diag(VD->getLocation(),
7898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7899 << VD;
7900 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007901 continue;
7902 }
7903 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7904 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7905 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007906 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007907 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7908 VarDecl::DeclarationOnly;
7909 Diag(VD->getLocation(),
7910 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7911 << VD;
7912 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007913 continue;
7914 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007915 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7916 // in a Construct]
7917 // Variables with the predetermined data-sharing attributes may not be
7918 // listed in data-sharing attributes clauses, except for the cases
7919 // listed below. For these exceptions only, listing a predetermined
7920 // variable in a data-sharing attribute clause is allowed and overrides
7921 // the variable's predetermined data-sharing attributes.
7922 // OpenMP [2.14.3.6, Restrictions, p.3]
7923 // Any number of reduction clauses can be specified on the directive,
7924 // but a list item can appear only once in the reduction clauses for that
7925 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007926 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007927 DVar = DSAStack->getTopDSA(VD, false);
7928 if (DVar.CKind == OMPC_reduction) {
7929 Diag(ELoc, diag::err_omp_once_referenced)
7930 << getOpenMPClauseName(OMPC_reduction);
7931 if (DVar.RefExpr) {
7932 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007933 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007934 } else if (DVar.CKind != OMPC_unknown) {
7935 Diag(ELoc, diag::err_omp_wrong_dsa)
7936 << getOpenMPClauseName(DVar.CKind)
7937 << getOpenMPClauseName(OMPC_reduction);
7938 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7939 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007940 }
7941
7942 // OpenMP [2.14.3.6, Restrictions, p.1]
7943 // A list item that appears in a reduction clause of a worksharing
7944 // construct must be shared in the parallel regions to which any of the
7945 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007946 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7947 if (isOpenMPWorksharingDirective(CurrDir) &&
7948 !isOpenMPParallelDirective(CurrDir)) {
7949 DVar = DSAStack->getImplicitDSA(VD, true);
7950 if (DVar.CKind != OMPC_shared) {
7951 Diag(ELoc, diag::err_omp_required_access)
7952 << getOpenMPClauseName(OMPC_reduction)
7953 << getOpenMPClauseName(OMPC_shared);
7954 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7955 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007956 }
7957 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007958
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007959 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007960 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7961 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7962 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7963 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7964 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007965 if (OASE ||
7966 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7967 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007968 // Create pseudo array type for private copy. The size for this array will
7969 // be generated during codegen.
7970 // For array subscripts or single variables Private Ty is the same as Type
7971 // (type of the variable or single array element).
7972 PrivateTy = Context.getVariableArrayType(
7973 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7974 Context.getSizeType(), VK_RValue),
7975 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007976 } else if (DE &&
7977 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7978 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007979 // Private copy.
7980 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7981 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007982 // Add initializer for private variable.
7983 Expr *Init = nullptr;
7984 switch (BOK) {
7985 case BO_Add:
7986 case BO_Xor:
7987 case BO_Or:
7988 case BO_LOr:
7989 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7990 if (Type->isScalarType() || Type->isAnyComplexType()) {
7991 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007992 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007993 break;
7994 case BO_Mul:
7995 case BO_LAnd:
7996 if (Type->isScalarType() || Type->isAnyComplexType()) {
7997 // '*' and '&&' reduction ops - initializer is '1'.
7998 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7999 }
8000 break;
8001 case BO_And: {
8002 // '&' reduction op - initializer is '~0'.
8003 QualType OrigType = Type;
8004 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
8005 Type = ComplexTy->getElementType();
8006 }
8007 if (Type->isRealFloatingType()) {
8008 llvm::APFloat InitValue =
8009 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8010 /*isIEEE=*/true);
8011 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8012 Type, ELoc);
8013 } else if (Type->isScalarType()) {
8014 auto Size = Context.getTypeSize(Type);
8015 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8016 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8017 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8018 }
8019 if (Init && OrigType->isAnyComplexType()) {
8020 // Init = 0xFFFF + 0xFFFFi;
8021 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8022 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8023 }
8024 Type = OrigType;
8025 break;
8026 }
8027 case BO_LT:
8028 case BO_GT: {
8029 // 'min' reduction op - initializer is 'Largest representable number in
8030 // the reduction list item type'.
8031 // 'max' reduction op - initializer is 'Least representable number in
8032 // the reduction list item type'.
8033 if (Type->isIntegerType() || Type->isPointerType()) {
8034 bool IsSigned = Type->hasSignedIntegerRepresentation();
8035 auto Size = Context.getTypeSize(Type);
8036 QualType IntTy =
8037 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8038 llvm::APInt InitValue =
8039 (BOK != BO_LT)
8040 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8041 : llvm::APInt::getMinValue(Size)
8042 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8043 : llvm::APInt::getMaxValue(Size);
8044 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8045 if (Type->isPointerType()) {
8046 // Cast to pointer type.
8047 auto CastExpr = BuildCStyleCastExpr(
8048 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8049 SourceLocation(), Init);
8050 if (CastExpr.isInvalid())
8051 continue;
8052 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008053 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008054 } else if (Type->isRealFloatingType()) {
8055 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8056 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8057 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8058 Type, ELoc);
8059 }
8060 break;
8061 }
8062 case BO_PtrMemD:
8063 case BO_PtrMemI:
8064 case BO_MulAssign:
8065 case BO_Div:
8066 case BO_Rem:
8067 case BO_Sub:
8068 case BO_Shl:
8069 case BO_Shr:
8070 case BO_LE:
8071 case BO_GE:
8072 case BO_EQ:
8073 case BO_NE:
8074 case BO_AndAssign:
8075 case BO_XorAssign:
8076 case BO_OrAssign:
8077 case BO_Assign:
8078 case BO_AddAssign:
8079 case BO_SubAssign:
8080 case BO_DivAssign:
8081 case BO_RemAssign:
8082 case BO_ShlAssign:
8083 case BO_ShrAssign:
8084 case BO_Comma:
8085 llvm_unreachable("Unexpected reduction operation");
8086 }
8087 if (Init) {
8088 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8089 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008090 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008091 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008092 if (!RHSVD->hasInit()) {
8093 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8094 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008095 if (VD) {
8096 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8097 VarDecl::DeclarationOnly;
8098 Diag(VD->getLocation(),
8099 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8100 << VD;
8101 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008102 continue;
8103 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008104 // Store initializer for single element in private copy. Will be used during
8105 // codegen.
8106 PrivateVD->setInit(RHSVD->getInit());
8107 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008108 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8109 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008110 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008111 ExprResult ReductionOp =
8112 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8113 LHSDRE, RHSDRE);
8114 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008115 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008116 ReductionOp =
8117 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8118 BO_Assign, LHSDRE, ReductionOp.get());
8119 } else {
8120 auto *ConditionalOp = new (Context) ConditionalOperator(
8121 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8122 RHSDRE, Type, VK_LValue, OK_Ordinary);
8123 ReductionOp =
8124 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8125 BO_Assign, LHSDRE, ConditionalOp);
8126 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008127 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008128 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008129 if (ReductionOp.isInvalid())
8130 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008131
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008132 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008133 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008134 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008135 LHSs.push_back(LHSDRE);
8136 RHSs.push_back(RHSDRE);
8137 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008138 }
8139
8140 if (Vars.empty())
8141 return nullptr;
8142
8143 return OMPReductionClause::Create(
8144 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008145 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8146 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008147}
8148
Alexey Bataev182227b2015-08-20 10:54:39 +00008149OMPClause *Sema::ActOnOpenMPLinearClause(
8150 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8151 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8152 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008153 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008154 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008155 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008156 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8157 LinKind == OMPC_LINEAR_unknown) {
8158 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8159 LinKind = OMPC_LINEAR_val;
8160 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008161 for (auto &RefExpr : VarList) {
8162 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8163 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008164 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008165 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008166 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008167 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008168 continue;
8169 }
8170
8171 // OpenMP [2.14.3.7, linear clause]
8172 // A list item that appears in a linear clause is subject to the private
8173 // clause semantics described in Section 2.14.3.3 on page 159 except as
8174 // noted. In addition, the value of the new list item on each iteration
8175 // of the associated loop(s) corresponds to the value of the original
8176 // list item before entering the construct plus the logical number of
8177 // the iteration times linear-step.
8178
Alexey Bataeved09d242014-05-28 05:53:51 +00008179 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008180 // OpenMP [2.1, C/C++]
8181 // A list item is a variable name.
8182 // OpenMP [2.14.3.3, Restrictions, p.1]
8183 // A variable that is part of another variable (as an array or
8184 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008185 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008186 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008187 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8188 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008189 continue;
8190 }
8191
8192 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8193
8194 // OpenMP [2.14.3.7, linear clause]
8195 // A list-item cannot appear in more than one linear clause.
8196 // A list-item that appears in a linear clause cannot appear in any
8197 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008198 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008199 if (DVar.RefExpr) {
8200 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8201 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008202 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008203 continue;
8204 }
8205
8206 QualType QType = VD->getType();
8207 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8208 // It will be analyzed later.
8209 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008210 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008211 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008212 continue;
8213 }
8214
8215 // A variable must not have an incomplete type or a reference type.
8216 if (RequireCompleteType(ELoc, QType,
8217 diag::err_omp_linear_incomplete_type)) {
8218 continue;
8219 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008220 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8221 !QType->isReferenceType()) {
8222 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8223 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8224 continue;
8225 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008226 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008227
8228 // A list item must not be const-qualified.
8229 if (QType.isConstant(Context)) {
8230 Diag(ELoc, diag::err_omp_const_variable)
8231 << getOpenMPClauseName(OMPC_linear);
8232 bool IsDecl =
8233 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8234 Diag(VD->getLocation(),
8235 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8236 << VD;
8237 continue;
8238 }
8239
8240 // A list item must be of integral or pointer type.
8241 QType = QType.getUnqualifiedType().getCanonicalType();
8242 const Type *Ty = QType.getTypePtrOrNull();
8243 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8244 !Ty->isPointerType())) {
8245 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8246 bool IsDecl =
8247 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8248 Diag(VD->getLocation(),
8249 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8250 << VD;
8251 continue;
8252 }
8253
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008254 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008255 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8256 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008257 auto *PrivateRef = buildDeclRefExpr(
8258 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008259 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008260 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008261 Expr *InitExpr;
8262 if (LinKind == OMPC_LINEAR_uval)
8263 InitExpr = VD->getInit();
8264 else
8265 InitExpr = DE;
8266 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008267 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008268 auto InitRef = buildDeclRefExpr(
8269 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008270 DSAStack->addDSA(VD, DE, OMPC_linear);
8271 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008272 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008273 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008274 }
8275
8276 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008277 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008278
8279 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008280 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008281 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8282 !Step->isInstantiationDependent() &&
8283 !Step->containsUnexpandedParameterPack()) {
8284 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008285 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008286 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008287 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008288 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008289
Alexander Musman3276a272015-03-21 10:12:56 +00008290 // Build var to save the step value.
8291 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008292 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008293 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008294 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008295 ExprResult CalcStep =
8296 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008297 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008298
Alexander Musman8dba6642014-04-22 13:09:42 +00008299 // Warn about zero linear step (it would be probably better specified as
8300 // making corresponding variables 'const').
8301 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008302 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8303 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008304 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8305 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008306 if (!IsConstant && CalcStep.isUsable()) {
8307 // Calculate the step beforehand instead of doing this on each iteration.
8308 // (This is not used if the number of iterations may be kfold-ed).
8309 CalcStepExpr = CalcStep.get();
8310 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008311 }
8312
Alexey Bataev182227b2015-08-20 10:54:39 +00008313 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8314 ColonLoc, EndLoc, Vars, Privates, Inits,
8315 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008316}
8317
8318static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8319 Expr *NumIterations, Sema &SemaRef,
8320 Scope *S) {
8321 // Walk the vars and build update/final expressions for the CodeGen.
8322 SmallVector<Expr *, 8> Updates;
8323 SmallVector<Expr *, 8> Finals;
8324 Expr *Step = Clause.getStep();
8325 Expr *CalcStep = Clause.getCalcStep();
8326 // OpenMP [2.14.3.7, linear clause]
8327 // If linear-step is not specified it is assumed to be 1.
8328 if (Step == nullptr)
8329 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8330 else if (CalcStep)
8331 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8332 bool HasErrors = false;
8333 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008334 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008335 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008336 for (auto &RefExpr : Clause.varlists()) {
8337 Expr *InitExpr = *CurInit;
8338
8339 // Build privatized reference to the current linear var.
8340 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008341 Expr *CapturedRef;
8342 if (LinKind == OMPC_LINEAR_uval)
8343 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8344 else
8345 CapturedRef =
8346 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8347 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8348 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008349
8350 // Build update: Var = InitExpr + IV * Step
8351 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008352 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008353 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008354 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8355 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008356
8357 // Build final: Var = InitExpr + NumIterations * Step
8358 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008359 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008360 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008361 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8362 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008363 if (!Update.isUsable() || !Final.isUsable()) {
8364 Updates.push_back(nullptr);
8365 Finals.push_back(nullptr);
8366 HasErrors = true;
8367 } else {
8368 Updates.push_back(Update.get());
8369 Finals.push_back(Final.get());
8370 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008371 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008372 }
8373 Clause.setUpdates(Updates);
8374 Clause.setFinals(Finals);
8375 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008376}
8377
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008378OMPClause *Sema::ActOnOpenMPAlignedClause(
8379 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8380 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8381
8382 SmallVector<Expr *, 8> Vars;
8383 for (auto &RefExpr : VarList) {
8384 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8385 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8386 // It will be analyzed later.
8387 Vars.push_back(RefExpr);
8388 continue;
8389 }
8390
8391 SourceLocation ELoc = RefExpr->getExprLoc();
8392 // OpenMP [2.1, C/C++]
8393 // A list item is a variable name.
8394 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8395 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008396 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8397 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008398 continue;
8399 }
8400
8401 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8402
8403 // OpenMP [2.8.1, simd construct, Restrictions]
8404 // The type of list items appearing in the aligned clause must be
8405 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008406 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008407 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008408 const Type *Ty = QType.getTypePtrOrNull();
8409 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8410 !Ty->isPointerType())) {
8411 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8412 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8413 bool IsDecl =
8414 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8415 Diag(VD->getLocation(),
8416 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8417 << VD;
8418 continue;
8419 }
8420
8421 // OpenMP [2.8.1, simd construct, Restrictions]
8422 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008423 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008424 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8425 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8426 << getOpenMPClauseName(OMPC_aligned);
8427 continue;
8428 }
8429
8430 Vars.push_back(DE);
8431 }
8432
8433 // OpenMP [2.8.1, simd construct, Description]
8434 // The parameter of the aligned clause, alignment, must be a constant
8435 // positive integer expression.
8436 // If no optional parameter is specified, implementation-defined default
8437 // alignments for SIMD instructions on the target platforms are assumed.
8438 if (Alignment != nullptr) {
8439 ExprResult AlignResult =
8440 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8441 if (AlignResult.isInvalid())
8442 return nullptr;
8443 Alignment = AlignResult.get();
8444 }
8445 if (Vars.empty())
8446 return nullptr;
8447
8448 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8449 EndLoc, Vars, Alignment);
8450}
8451
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008452OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8453 SourceLocation StartLoc,
8454 SourceLocation LParenLoc,
8455 SourceLocation EndLoc) {
8456 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008457 SmallVector<Expr *, 8> SrcExprs;
8458 SmallVector<Expr *, 8> DstExprs;
8459 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008460 for (auto &RefExpr : VarList) {
8461 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8462 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008463 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008464 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008465 SrcExprs.push_back(nullptr);
8466 DstExprs.push_back(nullptr);
8467 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008468 continue;
8469 }
8470
Alexey Bataeved09d242014-05-28 05:53:51 +00008471 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008472 // OpenMP [2.1, C/C++]
8473 // A list item is a variable name.
8474 // OpenMP [2.14.4.1, Restrictions, p.1]
8475 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008476 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008477 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008478 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8479 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008480 continue;
8481 }
8482
8483 Decl *D = DE->getDecl();
8484 VarDecl *VD = cast<VarDecl>(D);
8485
8486 QualType Type = VD->getType();
8487 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8488 // It will be analyzed later.
8489 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008490 SrcExprs.push_back(nullptr);
8491 DstExprs.push_back(nullptr);
8492 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008493 continue;
8494 }
8495
8496 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8497 // A list item that appears in a copyin clause must be threadprivate.
8498 if (!DSAStack->isThreadPrivate(VD)) {
8499 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008500 << getOpenMPClauseName(OMPC_copyin)
8501 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008502 continue;
8503 }
8504
8505 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8506 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008507 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008508 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008509 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008510 auto *SrcVD =
8511 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8512 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008513 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008514 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8515 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008516 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8517 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008518 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008519 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008520 // For arrays generate assignment operation for single element and replace
8521 // it by the original array element in CodeGen.
8522 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8523 PseudoDstExpr, PseudoSrcExpr);
8524 if (AssignmentOp.isInvalid())
8525 continue;
8526 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8527 /*DiscardedValue=*/true);
8528 if (AssignmentOp.isInvalid())
8529 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008530
8531 DSAStack->addDSA(VD, DE, OMPC_copyin);
8532 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008533 SrcExprs.push_back(PseudoSrcExpr);
8534 DstExprs.push_back(PseudoDstExpr);
8535 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008536 }
8537
Alexey Bataeved09d242014-05-28 05:53:51 +00008538 if (Vars.empty())
8539 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008540
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008541 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8542 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008543}
8544
Alexey Bataevbae9a792014-06-27 10:37:06 +00008545OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8546 SourceLocation StartLoc,
8547 SourceLocation LParenLoc,
8548 SourceLocation EndLoc) {
8549 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008550 SmallVector<Expr *, 8> SrcExprs;
8551 SmallVector<Expr *, 8> DstExprs;
8552 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008553 for (auto &RefExpr : VarList) {
8554 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8555 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8556 // It will be analyzed later.
8557 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008558 SrcExprs.push_back(nullptr);
8559 DstExprs.push_back(nullptr);
8560 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008561 continue;
8562 }
8563
8564 SourceLocation ELoc = RefExpr->getExprLoc();
8565 // OpenMP [2.1, C/C++]
8566 // A list item is a variable name.
8567 // OpenMP [2.14.4.1, Restrictions, p.1]
8568 // A list item that appears in a copyin clause must be threadprivate.
8569 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8570 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008571 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8572 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008573 continue;
8574 }
8575
8576 Decl *D = DE->getDecl();
8577 VarDecl *VD = cast<VarDecl>(D);
8578
8579 QualType Type = VD->getType();
8580 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8581 // It will be analyzed later.
8582 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008583 SrcExprs.push_back(nullptr);
8584 DstExprs.push_back(nullptr);
8585 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008586 continue;
8587 }
8588
8589 // OpenMP [2.14.4.2, Restrictions, p.2]
8590 // A list item that appears in a copyprivate clause may not appear in a
8591 // private or firstprivate clause on the single construct.
8592 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008593 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008594 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8595 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008596 Diag(ELoc, diag::err_omp_wrong_dsa)
8597 << getOpenMPClauseName(DVar.CKind)
8598 << getOpenMPClauseName(OMPC_copyprivate);
8599 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8600 continue;
8601 }
8602
8603 // OpenMP [2.11.4.2, Restrictions, p.1]
8604 // All list items that appear in a copyprivate clause must be either
8605 // threadprivate or private in the enclosing context.
8606 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008607 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008608 if (DVar.CKind == OMPC_shared) {
8609 Diag(ELoc, diag::err_omp_required_access)
8610 << getOpenMPClauseName(OMPC_copyprivate)
8611 << "threadprivate or private in the enclosing context";
8612 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8613 continue;
8614 }
8615 }
8616 }
8617
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008618 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008619 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008620 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008621 << getOpenMPClauseName(OMPC_copyprivate) << Type
8622 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008623 bool IsDecl =
8624 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8625 Diag(VD->getLocation(),
8626 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8627 << VD;
8628 continue;
8629 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008630
Alexey Bataevbae9a792014-06-27 10:37:06 +00008631 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8632 // A variable of class type (or array thereof) that appears in a
8633 // copyin clause requires an accessible, unambiguous copy assignment
8634 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008635 Type = Context.getBaseElementType(Type.getNonReferenceType())
8636 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008637 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008638 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8639 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008640 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008641 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008642 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008643 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8644 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008645 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008646 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008647 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8648 PseudoDstExpr, PseudoSrcExpr);
8649 if (AssignmentOp.isInvalid())
8650 continue;
8651 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8652 /*DiscardedValue=*/true);
8653 if (AssignmentOp.isInvalid())
8654 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008655
8656 // No need to mark vars as copyprivate, they are already threadprivate or
8657 // implicitly private.
8658 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008659 SrcExprs.push_back(PseudoSrcExpr);
8660 DstExprs.push_back(PseudoDstExpr);
8661 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008662 }
8663
8664 if (Vars.empty())
8665 return nullptr;
8666
Alexey Bataeva63048e2015-03-23 06:18:07 +00008667 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8668 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008669}
8670
Alexey Bataev6125da92014-07-21 11:26:11 +00008671OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8672 SourceLocation StartLoc,
8673 SourceLocation LParenLoc,
8674 SourceLocation EndLoc) {
8675 if (VarList.empty())
8676 return nullptr;
8677
8678 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8679}
Alexey Bataevdea47612014-07-23 07:46:59 +00008680
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008681OMPClause *
8682Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8683 SourceLocation DepLoc, SourceLocation ColonLoc,
8684 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8685 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008686 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008687 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008688 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008689 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008690 return nullptr;
8691 }
8692 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008693 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8694 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008695 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008696 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008697 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8698 /*Last=*/OMPC_DEPEND_unknown, Except)
8699 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008700 return nullptr;
8701 }
8702 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008703 llvm::APSInt DepCounter(/*BitWidth=*/32);
8704 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8705 if (DepKind == OMPC_DEPEND_sink) {
8706 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8707 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8708 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008709 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008710 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008711 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8712 DSAStack->getParentOrderedRegionParam()) {
8713 for (auto &RefExpr : VarList) {
8714 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8715 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8716 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8717 // It will be analyzed later.
8718 Vars.push_back(RefExpr);
8719 continue;
8720 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008721
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008722 SourceLocation ELoc = RefExpr->getExprLoc();
8723 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8724 if (DepKind == OMPC_DEPEND_sink) {
8725 if (DepCounter >= TotalDepCount) {
8726 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8727 continue;
8728 }
8729 ++DepCounter;
8730 // OpenMP [2.13.9, Summary]
8731 // depend(dependence-type : vec), where dependence-type is:
8732 // 'sink' and where vec is the iteration vector, which has the form:
8733 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8734 // where n is the value specified by the ordered clause in the loop
8735 // directive, xi denotes the loop iteration variable of the i-th nested
8736 // loop associated with the loop directive, and di is a constant
8737 // non-negative integer.
8738 SimpleExpr = SimpleExpr->IgnoreImplicit();
8739 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8740 if (!DE) {
8741 OverloadedOperatorKind OOK = OO_None;
8742 SourceLocation OOLoc;
8743 Expr *LHS, *RHS;
8744 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8745 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8746 OOLoc = BO->getOperatorLoc();
8747 LHS = BO->getLHS()->IgnoreParenImpCasts();
8748 RHS = BO->getRHS()->IgnoreParenImpCasts();
8749 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8750 OOK = OCE->getOperator();
8751 OOLoc = OCE->getOperatorLoc();
8752 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8753 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8754 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8755 OOK = MCE->getMethodDecl()
8756 ->getNameInfo()
8757 .getName()
8758 .getCXXOverloadedOperator();
8759 OOLoc = MCE->getCallee()->getExprLoc();
8760 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8761 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8762 } else {
8763 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8764 continue;
8765 }
8766 DE = dyn_cast<DeclRefExpr>(LHS);
8767 if (!DE) {
8768 Diag(LHS->getExprLoc(),
8769 diag::err_omp_depend_sink_expected_loop_iteration)
8770 << DSAStack->getParentLoopControlVariable(
8771 DepCounter.getZExtValue());
8772 continue;
8773 }
8774 if (OOK != OO_Plus && OOK != OO_Minus) {
8775 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8776 continue;
8777 }
8778 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8779 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8780 if (Res.isInvalid())
8781 continue;
8782 }
8783 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8784 if (!CurContext->isDependentContext() &&
8785 DSAStack->getParentOrderedRegionParam() &&
8786 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8787 Diag(DE->getExprLoc(),
8788 diag::err_omp_depend_sink_expected_loop_iteration)
8789 << DSAStack->getParentLoopControlVariable(
8790 DepCounter.getZExtValue());
8791 continue;
8792 }
8793 } else {
8794 // OpenMP [2.11.1.1, Restrictions, p.3]
8795 // A variable that is part of another variable (such as a field of a
8796 // structure) but is not an array element or an array section cannot
8797 // appear in a depend clause.
8798 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8799 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8800 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8801 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8802 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008803 (ASE &&
8804 !ASE->getBase()
8805 ->getType()
8806 .getNonReferenceType()
8807 ->isPointerType() &&
8808 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008809 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8810 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008811 continue;
8812 }
8813 }
8814
8815 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8816 }
8817
8818 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8819 TotalDepCount > VarList.size() &&
8820 DSAStack->getParentOrderedRegionParam()) {
8821 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8822 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8823 }
8824 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8825 Vars.empty())
8826 return nullptr;
8827 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008828
8829 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8830 DepLoc, ColonLoc, Vars);
8831}
Michael Wonge710d542015-08-07 16:16:36 +00008832
8833OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8834 SourceLocation LParenLoc,
8835 SourceLocation EndLoc) {
8836 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008837
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008838 // OpenMP [2.9.1, Restrictions]
8839 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008840 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8841 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008842 return nullptr;
8843
Michael Wonge710d542015-08-07 16:16:36 +00008844 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8845}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008846
8847static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8848 DSAStackTy *Stack, CXXRecordDecl *RD) {
8849 if (!RD || RD->isInvalidDecl())
8850 return true;
8851
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008852 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8853 if (auto *CTD = CTSD->getSpecializedTemplate())
8854 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008855 auto QTy = SemaRef.Context.getRecordType(RD);
8856 if (RD->isDynamicClass()) {
8857 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8858 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8859 return false;
8860 }
8861 auto *DC = RD;
8862 bool IsCorrect = true;
8863 for (auto *I : DC->decls()) {
8864 if (I) {
8865 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8866 if (MD->isStatic()) {
8867 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8868 SemaRef.Diag(MD->getLocation(),
8869 diag::note_omp_static_member_in_target);
8870 IsCorrect = false;
8871 }
8872 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8873 if (VD->isStaticDataMember()) {
8874 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8875 SemaRef.Diag(VD->getLocation(),
8876 diag::note_omp_static_member_in_target);
8877 IsCorrect = false;
8878 }
8879 }
8880 }
8881 }
8882
8883 for (auto &I : RD->bases()) {
8884 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8885 I.getType()->getAsCXXRecordDecl()))
8886 IsCorrect = false;
8887 }
8888 return IsCorrect;
8889}
8890
8891static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8892 DSAStackTy *Stack, QualType QTy) {
8893 NamedDecl *ND;
8894 if (QTy->isIncompleteType(&ND)) {
8895 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8896 return false;
8897 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8898 if (!RD->isInvalidDecl() &&
8899 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8900 return false;
8901 }
8902 return true;
8903}
8904
Samuel Antao5de996e2016-01-22 20:21:36 +00008905// Return the expression of the base of the map clause or null if it cannot
8906// be determined and do all the necessary checks to see if the expression is
8907// valid as a standalone map clause expression.
8908static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8909 SourceLocation ELoc = E->getExprLoc();
8910 SourceRange ERange = E->getSourceRange();
8911
8912 // The base of elements of list in a map clause have to be either:
8913 // - a reference to variable or field.
8914 // - a member expression.
8915 // - an array expression.
8916 //
8917 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8918 // reference to 'r'.
8919 //
8920 // If we have:
8921 //
8922 // struct SS {
8923 // Bla S;
8924 // foo() {
8925 // #pragma omp target map (S.Arr[:12]);
8926 // }
8927 // }
8928 //
8929 // We want to retrieve the member expression 'this->S';
8930
8931 Expr *RelevantExpr = nullptr;
8932
8933 // Flags to help capture some memory
8934
8935 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8936 // If a list item is an array section, it must specify contiguous storage.
8937 //
8938 // For this restriction it is sufficient that we make sure only references
8939 // to variables or fields and array expressions, and that no array sections
8940 // exist except in the rightmost expression. E.g. these would be invalid:
8941 //
8942 // r.ArrS[3:5].Arr[6:7]
8943 //
8944 // r.ArrS[3:5].x
8945 //
8946 // but these would be valid:
8947 // r.ArrS[3].Arr[6:7]
8948 //
8949 // r.ArrS[3].x
8950
8951 bool IsRightMostExpression = true;
8952
8953 while (!RelevantExpr) {
8954 auto AllowArraySection = IsRightMostExpression;
8955 IsRightMostExpression = false;
8956
8957 E = E->IgnoreParenImpCasts();
8958
8959 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8960 if (!isa<VarDecl>(CurE->getDecl()))
8961 break;
8962
8963 RelevantExpr = CurE;
8964 continue;
8965 }
8966
8967 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8968 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8969
8970 if (isa<CXXThisExpr>(BaseE))
8971 // We found a base expression: this->Val.
8972 RelevantExpr = CurE;
8973 else
8974 E = BaseE;
8975
8976 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8977 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8978 << CurE->getSourceRange();
8979 break;
8980 }
8981
8982 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8983
8984 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8985 // A bit-field cannot appear in a map clause.
8986 //
8987 if (FD->isBitField()) {
8988 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8989 << CurE->getSourceRange();
8990 break;
8991 }
8992
8993 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8994 // If the type of a list item is a reference to a type T then the type
8995 // will be considered to be T for all purposes of this clause.
8996 QualType CurType = BaseE->getType();
8997 if (CurType->isReferenceType())
8998 CurType = CurType->getPointeeType();
8999
9000 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9001 // A list item cannot be a variable that is a member of a structure with
9002 // a union type.
9003 //
9004 if (auto *RT = CurType->getAs<RecordType>())
9005 if (RT->isUnionType()) {
9006 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9007 << CurE->getSourceRange();
9008 break;
9009 }
9010
9011 continue;
9012 }
9013
9014 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9015 E = CurE->getBase()->IgnoreParenImpCasts();
9016
9017 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9018 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9019 << 0 << CurE->getSourceRange();
9020 break;
9021 }
9022 continue;
9023 }
9024
9025 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9026 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9027 // If a list item is an element of a structure, only the rightmost symbol
9028 // of the variable reference can be an array section.
9029 //
9030 if (!AllowArraySection) {
9031 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9032 << CurE->getSourceRange();
9033 break;
9034 }
9035
9036 E = CurE->getBase()->IgnoreParenImpCasts();
9037
9038 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9039 // If the type of a list item is a reference to a type T then the type
9040 // will be considered to be T for all purposes of this clause.
9041 QualType CurType = E->getType();
9042 if (CurType->isReferenceType())
9043 CurType = CurType->getPointeeType();
9044
9045 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9046 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9047 << 0 << CurE->getSourceRange();
9048 break;
9049 }
9050
9051 continue;
9052 }
9053
9054 // If nothing else worked, this is not a valid map clause expression.
9055 SemaRef.Diag(ELoc,
9056 diag::err_omp_expected_named_var_member_or_array_expression)
9057 << ERange;
9058 break;
9059 }
9060
9061 return RelevantExpr;
9062}
9063
9064// Return true if expression E associated with value VD has conflicts with other
9065// map information.
9066static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9067 Expr *E, bool CurrentRegionOnly) {
9068 assert(VD && E);
9069
9070 // Types used to organize the components of a valid map clause.
9071 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9072 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9073
9074 // Helper to extract the components in the map clause expression E and store
9075 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9076 // it has already passed the single clause checks.
9077 auto ExtractMapExpressionComponents = [](Expr *TE,
9078 MapExpressionComponents &MEC) {
9079 while (true) {
9080 TE = TE->IgnoreParenImpCasts();
9081
9082 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9083 MEC.push_back(
9084 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9085 break;
9086 }
9087
9088 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9089 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9090
9091 MEC.push_back(MapExpressionComponent(
9092 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9093 if (isa<CXXThisExpr>(BaseE))
9094 break;
9095
9096 TE = BaseE;
9097 continue;
9098 }
9099
9100 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9101 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9102 TE = CurE->getBase()->IgnoreParenImpCasts();
9103 continue;
9104 }
9105
9106 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9107 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9108 TE = CurE->getBase()->IgnoreParenImpCasts();
9109 continue;
9110 }
9111
9112 llvm_unreachable(
9113 "Expecting only valid map clause expressions at this point!");
9114 }
9115 };
9116
9117 SourceLocation ELoc = E->getExprLoc();
9118 SourceRange ERange = E->getSourceRange();
9119
9120 // In order to easily check the conflicts we need to match each component of
9121 // the expression under test with the components of the expressions that are
9122 // already in the stack.
9123
9124 MapExpressionComponents CurComponents;
9125 ExtractMapExpressionComponents(E, CurComponents);
9126
9127 assert(!CurComponents.empty() && "Map clause expression with no components!");
9128 assert(CurComponents.back().second == VD &&
9129 "Map clause expression with unexpected base!");
9130
9131 // Variables to help detecting enclosing problems in data environment nests.
9132 bool IsEnclosedByDataEnvironmentExpr = false;
9133 Expr *EnclosingExpr = nullptr;
9134
9135 bool FoundError =
9136 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9137 MapExpressionComponents StackComponents;
9138 ExtractMapExpressionComponents(RE, StackComponents);
9139 assert(!StackComponents.empty() &&
9140 "Map clause expression with no components!");
9141 assert(StackComponents.back().second == VD &&
9142 "Map clause expression with unexpected base!");
9143
9144 // Expressions must start from the same base. Here we detect at which
9145 // point both expressions diverge from each other and see if we can
9146 // detect if the memory referred to both expressions is contiguous and
9147 // do not overlap.
9148 auto CI = CurComponents.rbegin();
9149 auto CE = CurComponents.rend();
9150 auto SI = StackComponents.rbegin();
9151 auto SE = StackComponents.rend();
9152 for (; CI != CE && SI != SE; ++CI, ++SI) {
9153
9154 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9155 // At most one list item can be an array item derived from a given
9156 // variable in map clauses of the same construct.
9157 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9158 isa<OMPArraySectionExpr>(CI->first)) &&
9159 (isa<ArraySubscriptExpr>(SI->first) ||
9160 isa<OMPArraySectionExpr>(SI->first))) {
9161 SemaRef.Diag(CI->first->getExprLoc(),
9162 diag::err_omp_multiple_array_items_in_map_clause)
9163 << CI->first->getSourceRange();
9164 ;
9165 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9166 << SI->first->getSourceRange();
9167 return true;
9168 }
9169
9170 // Do both expressions have the same kind?
9171 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9172 break;
9173
9174 // Are we dealing with different variables/fields?
9175 if (CI->second != SI->second)
9176 break;
9177 }
9178
9179 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9180 // List items of map clauses in the same construct must not share
9181 // original storage.
9182 //
9183 // If the expressions are exactly the same or one is a subset of the
9184 // other, it means they are sharing storage.
9185 if (CI == CE && SI == SE) {
9186 if (CurrentRegionOnly) {
9187 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9188 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9189 << RE->getSourceRange();
9190 return true;
9191 } else {
9192 // If we find the same expression in the enclosing data environment,
9193 // that is legal.
9194 IsEnclosedByDataEnvironmentExpr = true;
9195 return false;
9196 }
9197 }
9198
9199 QualType DerivedType = std::prev(CI)->first->getType();
9200 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9201
9202 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9203 // If the type of a list item is a reference to a type T then the type
9204 // will be considered to be T for all purposes of this clause.
9205 if (DerivedType->isReferenceType())
9206 DerivedType = DerivedType->getPointeeType();
9207
9208 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9209 // A variable for which the type is pointer and an array section
9210 // derived from that variable must not appear as list items of map
9211 // clauses of the same construct.
9212 //
9213 // Also, cover one of the cases in:
9214 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9215 // If any part of the original storage of a list item has corresponding
9216 // storage in the device data environment, all of the original storage
9217 // must have corresponding storage in the device data environment.
9218 //
9219 if (DerivedType->isAnyPointerType()) {
9220 if (CI == CE || SI == SE) {
9221 SemaRef.Diag(
9222 DerivedLoc,
9223 diag::err_omp_pointer_mapped_along_with_derived_section)
9224 << DerivedLoc;
9225 } else {
9226 assert(CI != CE && SI != SE);
9227 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9228 << DerivedLoc;
9229 }
9230 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9231 << RE->getSourceRange();
9232 return true;
9233 }
9234
9235 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9236 // List items of map clauses in the same construct must not share
9237 // original storage.
9238 //
9239 // An expression is a subset of the other.
9240 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9241 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9242 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9243 << RE->getSourceRange();
9244 return true;
9245 }
9246
9247 // The current expression uses the same base as other expression in the
9248 // data environment but does not contain it completelly.
9249 if (!CurrentRegionOnly && SI != SE)
9250 EnclosingExpr = RE;
9251
9252 // The current expression is a subset of the expression in the data
9253 // environment.
9254 IsEnclosedByDataEnvironmentExpr |=
9255 (!CurrentRegionOnly && CI != CE && SI == SE);
9256
9257 return false;
9258 });
9259
9260 if (CurrentRegionOnly)
9261 return FoundError;
9262
9263 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9264 // If any part of the original storage of a list item has corresponding
9265 // storage in the device data environment, all of the original storage must
9266 // have corresponding storage in the device data environment.
9267 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9268 // If a list item is an element of a structure, and a different element of
9269 // the structure has a corresponding list item in the device data environment
9270 // prior to a task encountering the construct associated with the map clause,
9271 // then the list item must also have a correspnding list item in the device
9272 // data environment prior to the task encountering the construct.
9273 //
9274 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9275 SemaRef.Diag(ELoc,
9276 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9277 << ERange;
9278 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9279 << EnclosingExpr->getSourceRange();
9280 return true;
9281 }
9282
9283 return FoundError;
9284}
9285
Samuel Antao23abd722016-01-19 20:40:49 +00009286OMPClause *
9287Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9288 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9289 SourceLocation MapLoc, SourceLocation ColonLoc,
9290 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9291 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009292 SmallVector<Expr *, 4> Vars;
9293
9294 for (auto &RE : VarList) {
9295 assert(RE && "Null expr in omp map");
9296 if (isa<DependentScopeDeclRefExpr>(RE)) {
9297 // It will be analyzed later.
9298 Vars.push_back(RE);
9299 continue;
9300 }
9301 SourceLocation ELoc = RE->getExprLoc();
9302
Kelvin Li0bff7af2015-11-23 05:32:03 +00009303 auto *VE = RE->IgnoreParenLValueCasts();
9304
9305 if (VE->isValueDependent() || VE->isTypeDependent() ||
9306 VE->isInstantiationDependent() ||
9307 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009308 // We can only analyze this information once the missing information is
9309 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009310 Vars.push_back(RE);
9311 continue;
9312 }
9313
9314 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009315
Samuel Antao5de996e2016-01-22 20:21:36 +00009316 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9317 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9318 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009319 continue;
9320 }
9321
Samuel Antao5de996e2016-01-22 20:21:36 +00009322 // Obtain the array or member expression bases if required.
9323 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9324 if (!BE)
9325 continue;
9326
9327 // If the base is a reference to a variable, we rely on that variable for
9328 // the following checks. If it is a 'this' expression we rely on the field.
9329 ValueDecl *D = nullptr;
9330 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9331 D = DRE->getDecl();
9332 } else {
9333 auto *ME = cast<MemberExpr>(BE);
9334 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9335 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009336 }
9337 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009338
Samuel Antao5de996e2016-01-22 20:21:36 +00009339 auto *VD = dyn_cast<VarDecl>(D);
9340 auto *FD = dyn_cast<FieldDecl>(D);
9341
9342 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009343 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009344
9345 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9346 // threadprivate variables cannot appear in a map clause.
9347 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009348 auto DVar = DSAStack->getTopDSA(VD, false);
9349 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9350 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9351 continue;
9352 }
9353
Samuel Antao5de996e2016-01-22 20:21:36 +00009354 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9355 // A list item cannot appear in both a map clause and a data-sharing
9356 // attribute clause on the same construct.
9357 //
9358 // TODO: Implement this check - it cannot currently be tested because of
9359 // missing implementation of the other data sharing clauses in target
9360 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009361
Samuel Antao5de996e2016-01-22 20:21:36 +00009362 // Check conflicts with other map clause expressions. We check the conflicts
9363 // with the current construct separately from the enclosing data
9364 // environment, because the restrictions are different.
9365 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9366 /*CurrentRegionOnly=*/true))
9367 break;
9368 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9369 /*CurrentRegionOnly=*/false))
9370 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009371
Samuel Antao5de996e2016-01-22 20:21:36 +00009372 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9373 // If the type of a list item is a reference to a type T then the type will
9374 // be considered to be T for all purposes of this clause.
9375 QualType Type = D->getType();
9376 if (Type->isReferenceType())
9377 Type = Type->getPointeeType();
9378
9379 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009380 // A list item must have a mappable type.
9381 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9382 DSAStack, Type))
9383 continue;
9384
Samuel Antaodf67fc42016-01-19 19:15:56 +00009385 // target enter data
9386 // OpenMP [2.10.2, Restrictions, p. 99]
9387 // A map-type must be specified in all map clauses and must be either
9388 // to or alloc.
9389 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9390 if (DKind == OMPD_target_enter_data &&
9391 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9392 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009393 << (IsMapTypeImplicit ? 1 : 0)
9394 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009395 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009396 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009397 }
9398
Samuel Antao72590762016-01-19 20:04:50 +00009399 // target exit_data
9400 // OpenMP [2.10.3, Restrictions, p. 102]
9401 // A map-type must be specified in all map clauses and must be either
9402 // from, release, or delete.
9403 DKind = DSAStack->getCurrentDirective();
9404 if (DKind == OMPD_target_exit_data &&
9405 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9406 MapType == OMPC_MAP_delete)) {
9407 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009408 << (IsMapTypeImplicit ? 1 : 0)
9409 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009410 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009411 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009412 }
9413
Kelvin Li0bff7af2015-11-23 05:32:03 +00009414 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009415 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009416 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009417
Samuel Antao5de996e2016-01-22 20:21:36 +00009418 // We need to produce a map clause even if we don't have variables so that
9419 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009420 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009421 MapTypeModifier, MapType, IsMapTypeImplicit,
9422 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009423}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009424
9425OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9426 SourceLocation StartLoc,
9427 SourceLocation LParenLoc,
9428 SourceLocation EndLoc) {
9429 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009430
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009431 // OpenMP [teams Constrcut, Restrictions]
9432 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009433 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9434 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009435 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009436
9437 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9438}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009439
9440OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9441 SourceLocation StartLoc,
9442 SourceLocation LParenLoc,
9443 SourceLocation EndLoc) {
9444 Expr *ValExpr = ThreadLimit;
9445
9446 // OpenMP [teams Constrcut, Restrictions]
9447 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009448 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9449 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009450 return nullptr;
9451
9452 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9453 EndLoc);
9454}
Alexey Bataeva0569352015-12-01 10:17:31 +00009455
9456OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9457 SourceLocation StartLoc,
9458 SourceLocation LParenLoc,
9459 SourceLocation EndLoc) {
9460 Expr *ValExpr = Priority;
9461
9462 // OpenMP [2.9.1, task Constrcut]
9463 // The priority-value is a non-negative numerical scalar expression.
9464 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9465 /*StrictlyPositive=*/false))
9466 return nullptr;
9467
9468 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9469}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009470
9471OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9472 SourceLocation StartLoc,
9473 SourceLocation LParenLoc,
9474 SourceLocation EndLoc) {
9475 Expr *ValExpr = Grainsize;
9476
9477 // OpenMP [2.9.2, taskloop Constrcut]
9478 // The parameter of the grainsize clause must be a positive integer
9479 // expression.
9480 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9481 /*StrictlyPositive=*/true))
9482 return nullptr;
9483
9484 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9485}
Alexey Bataev382967a2015-12-08 12:06:20 +00009486
9487OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9488 SourceLocation StartLoc,
9489 SourceLocation LParenLoc,
9490 SourceLocation EndLoc) {
9491 Expr *ValExpr = NumTasks;
9492
9493 // OpenMP [2.9.2, taskloop Constrcut]
9494 // The parameter of the num_tasks clause must be a positive integer
9495 // expression.
9496 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9497 /*StrictlyPositive=*/true))
9498 return nullptr;
9499
9500 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9501}
9502
Alexey Bataev28c75412015-12-15 08:19:24 +00009503OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9504 SourceLocation LParenLoc,
9505 SourceLocation EndLoc) {
9506 // OpenMP [2.13.2, critical construct, Description]
9507 // ... where hint-expression is an integer constant expression that evaluates
9508 // to a valid lock hint.
9509 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9510 if (HintExpr.isInvalid())
9511 return nullptr;
9512 return new (Context)
9513 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9514}
9515
Carlo Bertollib4adf552016-01-15 18:50:31 +00009516OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9517 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9518 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9519 SourceLocation EndLoc) {
9520 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9521 std::string Values;
9522 Values += "'";
9523 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9524 Values += "'";
9525 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9526 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9527 return nullptr;
9528 }
9529 Expr *ValExpr = ChunkSize;
9530 Expr *HelperValExpr = nullptr;
9531 if (ChunkSize) {
9532 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9533 !ChunkSize->isInstantiationDependent() &&
9534 !ChunkSize->containsUnexpandedParameterPack()) {
9535 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9536 ExprResult Val =
9537 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9538 if (Val.isInvalid())
9539 return nullptr;
9540
9541 ValExpr = Val.get();
9542
9543 // OpenMP [2.7.1, Restrictions]
9544 // chunk_size must be a loop invariant integer expression with a positive
9545 // value.
9546 llvm::APSInt Result;
9547 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9548 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9549 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9550 << "dist_schedule" << ChunkSize->getSourceRange();
9551 return nullptr;
9552 }
9553 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9554 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9555 ChunkSize->getType(), ".chunk.");
9556 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9557 ChunkSize->getExprLoc(),
9558 /*RefersToCapture=*/true);
9559 HelperValExpr = ImpVarRef;
9560 }
9561 }
9562 }
9563
9564 return new (Context)
9565 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9566 Kind, ValExpr, HelperValExpr);
9567}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009568
9569OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9570 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9571 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9572 SourceLocation KindLoc, SourceLocation EndLoc) {
9573 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9574 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9575 Kind != OMPC_DEFAULTMAP_scalar) {
9576 std::string Value;
9577 SourceLocation Loc;
9578 Value += "'";
9579 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9580 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9581 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9582 Loc = MLoc;
9583 } else {
9584 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9585 OMPC_DEFAULTMAP_scalar);
9586 Loc = KindLoc;
9587 }
9588 Value += "'";
9589 Diag(Loc, diag::err_omp_unexpected_clause_value)
9590 << Value << getOpenMPClauseName(OMPC_defaultmap);
9591 return nullptr;
9592 }
9593
9594 return new (Context)
9595 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9596}