blob: d4196b87e2ba704f09f059537a2acfbba8d0c124 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000073 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000074 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000075 SourceLocation ImplicitDSALoc;
76 DSAVarData()
77 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000078 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000079 };
Alexey Bataeved09d242014-05-28 05:53:51 +000080
Alexey Bataev758e55e2013-09-06 18:03:48 +000081private:
Samuel Antao5de996e2016-01-22 20:21:36 +000082 typedef SmallVector<Expr *, 4> MapInfo;
83
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 struct DSAInfo {
85 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000086 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000087 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
90 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000091 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000092 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000093 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
94 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095
96 struct SharingMapTy {
97 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000098 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000099 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000100 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 OpenMPDirectiveKind Directive;
104 DeclarationNameInfo DirectiveName;
105 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000106 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000107 /// \brief first argument (Expr *) contains optional argument of the
108 /// 'ordered' clause, the second one is true if the regions has 'ordered'
109 /// clause, false otherwise.
110 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000111 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000112 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000113 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000114 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000115 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000116 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000117 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000118 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000119 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000122 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000124 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 };
127
Axel Naumann323862e2016-02-03 10:45:22 +0000128 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129
130 /// \brief Stack of used declaration and their data-sharing attributes.
131 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000132 /// \brief true, if check for DSA must be from parent directive, false, if
133 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000135 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000136 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000137 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138
139 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
140
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000141 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000142
143 /// \brief Checks if the variable is a local for OpenMP region.
144 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000145
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000147 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000148 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
149 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000150
Alexey Bataevaac108a2015-06-23 04:51:00 +0000151 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
152 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000154 bool isForceVarCapturing() const { return ForceCapturing; }
155 void setForceVarCapturing(bool V) { ForceCapturing = V; }
156
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000158 Scope *CurScope, SourceLocation Loc) {
159 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
160 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161 }
162
163 void pop() {
164 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
165 Stack.pop_back();
166 }
167
Alexey Bataev28c75412015-12-15 08:19:24 +0000168 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
169 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
170 }
171 const std::pair<OMPCriticalDirective *, llvm::APSInt>
172 getCriticalWithHint(const DeclarationNameInfo &Name) const {
173 auto I = Criticals.find(Name.getAsString());
174 if (I != Criticals.end())
175 return I->second;
176 return std::make_pair(nullptr, llvm::APSInt());
177 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000178 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000179 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000181 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000182
Alexey Bataev9c821032015-04-30 04:23:23 +0000183 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000185 /// \brief Check if the specified variable is a loop control variable for
186 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000187 /// \return The index of the loop control variable in the list of associated
188 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000189 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \brief Check if the specified variable is a loop control variable for
191 /// parent region.
192 /// \return The index of the loop control variable in the list of associated
193 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000194 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000195 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
196 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000197 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000198
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000200 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
201 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data sharing attributes from top of the stack for the
204 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000205 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 /// \brief Checks if the specified variables has data-sharing attributes which
209 /// match specified \a CPred predicate in any directive which matches \a DPred
210 /// predicate.
211 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000212 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000213 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000214 /// \brief Checks if the specified variables has data-sharing attributes which
215 /// match specified \a CPred predicate in any innermost directive which
216 /// matches \a DPred predicate.
217 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000218 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
219 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000220 /// \brief Checks if the specified variables has explicit data-sharing
221 /// attributes which match specified \a CPred predicate at the specified
222 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000223 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000224 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
225 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000226
227 /// \brief Returns true if the directive at level \Level matches in the
228 /// specified \a DPred predicate.
229 bool hasExplicitDirective(
230 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
231 unsigned Level);
232
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000233 /// \brief Finds a directive which matches specified \a DPred predicate.
234 template <class NamedDirectivesPredicate>
235 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 /// \brief Returns currently analyzed directive.
238 OpenMPDirectiveKind getCurrentDirective() const {
239 return Stack.back().Directive;
240 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000241 /// \brief Returns parent directive.
242 OpenMPDirectiveKind getParentDirective() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].Directive;
245 return OMPD_unknown;
246 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000247 /// \brief Return the directive associated with the provided scope.
248 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249
250 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000251 void setDefaultDSANone(SourceLocation Loc) {
252 Stack.back().DefaultAttr = DSA_none;
253 Stack.back().DefaultAttrLoc = Loc;
254 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000256 void setDefaultDSAShared(SourceLocation Loc) {
257 Stack.back().DefaultAttr = DSA_shared;
258 Stack.back().DefaultAttrLoc = Loc;
259 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260
261 DefaultDataSharingAttributes getDefaultDSA() const {
262 return Stack.back().DefaultAttr;
263 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000264 SourceLocation getDefaultDSALocation() const {
265 return Stack.back().DefaultAttrLoc;
266 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000270 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 }
273
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000274 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000275 void setOrderedRegion(bool IsOrdered, Expr *Param) {
276 Stack.back().OrderedRegion.setInt(IsOrdered);
277 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000278 }
279 /// \brief Returns true, if parent region is ordered (has associated
280 /// 'ordered' clause), false - otherwise.
281 bool isParentOrderedRegion() const {
282 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000284 return false;
285 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 /// \brief Returns optional parameter for the ordered region.
287 Expr *getParentOrderedRegionParam() const {
288 if (Stack.size() > 2)
289 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
290 return nullptr;
291 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000292 /// \brief Marks current region as nowait (it has a 'nowait' clause).
293 void setNowaitRegion(bool IsNowait = true) {
294 Stack.back().NowaitRegion = IsNowait;
295 }
296 /// \brief Returns true, if parent region is nowait (has associated
297 /// 'nowait' clause), false - otherwise.
298 bool isParentNowaitRegion() const {
299 if (Stack.size() > 2)
300 return Stack[Stack.size() - 2].NowaitRegion;
301 return false;
302 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000303 /// \brief Marks parent region as cancel region.
304 void setParentCancelRegion(bool Cancel = true) {
305 if (Stack.size() > 2)
306 Stack[Stack.size() - 2].CancelRegion =
307 Stack[Stack.size() - 2].CancelRegion || Cancel;
308 }
309 /// \brief Return true if current region has inner cancel construct.
310 bool isCancelRegion() const {
311 return Stack.back().CancelRegion;
312 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000313
Alexey Bataev9c821032015-04-30 04:23:23 +0000314 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000315 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318
Alexey Bataev13314bf2014-10-09 04:18:56 +0000319 /// \brief Marks current target region as one with closely nested teams
320 /// region.
321 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
322 if (Stack.size() > 2)
323 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
324 }
325 /// \brief Returns true, if current region has closely nested teams region.
326 bool hasInnerTeamsRegion() const {
327 return getInnerTeamsRegionLoc().isValid();
328 }
329 /// \brief Returns location of the nested teams region (if any).
330 SourceLocation getInnerTeamsRegionLoc() const {
331 if (Stack.size() > 1)
332 return Stack.back().InnerTeamsRegionLoc;
333 return SourceLocation();
334 }
335
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000336 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000338 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000339
Samuel Antao5de996e2016-01-22 20:21:36 +0000340 // Do the check specified in MapInfoCheck and return true if any issue is
341 // found.
342 template <class MapInfoCheck>
343 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
344 MapInfoCheck Check) {
345 auto SI = Stack.rbegin();
346 auto SE = Stack.rend();
347
348 if (SI == SE)
349 return false;
350
351 if (CurrentRegionOnly) {
352 SE = std::next(SI);
353 } else {
354 ++SI;
355 }
356
357 for (; SI != SE; ++SI) {
358 auto MI = SI->MappedDecls.find(VD);
359 if (MI != SI->MappedDecls.end()) {
360 for (Expr *E : MI->second) {
361 if (Check(E))
362 return true;
363 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000364 }
365 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000371 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000372 }
373 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000375bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
376 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000377 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000378 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000379}
Alexey Bataeved09d242014-05-28 05:53:51 +0000380} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000382static ValueDecl *getCanonicalDecl(ValueDecl *D) {
383 auto *VD = dyn_cast<VarDecl>(D);
384 auto *FD = dyn_cast<FieldDecl>(D);
385 if (VD != nullptr) {
386 VD = VD->getCanonicalDecl();
387 D = VD;
388 } else {
389 assert(FD);
390 FD = FD->getCanonicalDecl();
391 D = FD;
392 }
393 return D;
394}
395
Alexey Bataev758e55e2013-09-06 18:03:48 +0000396DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000397 ValueDecl *D) {
398 D = getCanonicalDecl(D);
399 auto *VD = dyn_cast<VarDecl>(D);
400 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000402 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a region but not in construct]
405 // File-scope or namespace-scope variables referenced in called routines
406 // in the region are shared unless they appear in a threadprivate
407 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000409 DVar.CKind = OMPC_shared;
410
411 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
412 // in a region but not in construct]
413 // Variables with static storage duration that are declared in called
414 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000415 if (VD && VD->hasGlobalStorage())
416 DVar.CKind = OMPC_shared;
417
418 // Non-static data members are shared by default.
419 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 return DVar;
423 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000424
Alexey Bataev758e55e2013-09-06 18:03:48 +0000425 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427 // in a Construct, C/C++, predetermined, p.1]
428 // Variables with automatic storage duration that are declared in a scope
429 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000430 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
431 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000432 DVar.CKind = OMPC_private;
433 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000434 }
435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
438 if (Iter->SharingMap.count(D)) {
439 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000440 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000442 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 return DVar;
444 }
445
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, C/C++, implicitly determined, p.1]
448 // In a parallel or task construct, the data-sharing attributes of these
449 // variables are determined by the default clause, if present.
450 switch (Iter->DefaultAttr) {
451 case DSA_shared:
452 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 case DSA_none:
456 return DVar;
457 case DSA_unspecified:
458 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
459 // in a Construct, implicitly determined, p.2]
460 // In a parallel construct, if no default clause is present, these
461 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000462 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000463 if (isOpenMPParallelDirective(DVar.DKind) ||
464 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 DVar.CKind = OMPC_shared;
466 return DVar;
467 }
468
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.4]
471 // In a task construct, if no default clause is present, a variable that in
472 // the enclosing context is determined to be shared by all implicit tasks
473 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 if (DVar.DKind == OMPD_task) {
475 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000476 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000478 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
479 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 // in a Construct, implicitly determined, p.6]
481 // In a task construct, if no default clause is present, a variable
482 // whose data-sharing attribute is not determined by the rules above is
483 // firstprivate.
484 DVarTemp = getDSA(I, D);
485 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000486 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 return DVar;
490 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 }
494 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000496 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 return DVar;
498 }
499 }
500 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
501 // in a Construct, implicitly determined, p.3]
502 // For constructs other than task, if no default clause is present, these
503 // variables inherit their data-sharing attributes from the enclosing
504 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000505 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000506}
507
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000508Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000509 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000511 auto It = Stack.back().AlignedMap.find(D);
512 if (It == Stack.back().AlignedMap.end()) {
513 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
514 Stack.back().AlignedMap[D] = NewDE;
515 return nullptr;
516 } else {
517 assert(It->second && "Unexpected nullptr expr in the aligned map");
518 return It->second;
519 }
520 return nullptr;
521}
522
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000523void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000524 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000526 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000527}
528
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000529unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000530 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000532 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
533}
534
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000535unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000536 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
539 ? Stack[Stack.size() - 2].LCVMap[D]
540 : 0;
541}
542
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000543ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000544 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
545 if (Stack[Stack.size() - 2].LCVMap.size() < I)
546 return nullptr;
547 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
548 if (Pair.second == I)
549 return Pair.first;
550 }
551 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000552}
553
Alexey Bataev90c228f2016-02-08 09:29:13 +0000554void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
555 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000556 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000557 if (A == OMPC_threadprivate) {
558 Stack[0].SharingMap[D].Attributes = A;
559 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000560 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000561 } else {
562 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
563 Stack.back().SharingMap[D].Attributes = A;
564 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000565 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
566 if (PrivateCopy)
567 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568 }
569}
570
Alexey Bataeved09d242014-05-28 05:53:51 +0000571bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000572 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000574 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000575 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000577 ++I;
578 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000579 if (I == E)
580 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000581 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000582 Scope *CurScope = getCurScope();
583 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000584 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000585 }
586 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000588 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589}
590
Alexey Bataev39f915b82015-05-08 10:41:21 +0000591/// \brief Build a variable declaration for OpenMP loop iteration variable.
592static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000593 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000594 DeclContext *DC = SemaRef.CurContext;
595 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
596 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
597 VarDecl *Decl =
598 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000599 if (Attrs) {
600 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
601 I != E; ++I)
602 Decl->addAttr(*I);
603 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000604 Decl->setImplicit();
605 return Decl;
606}
607
608static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
609 SourceLocation Loc,
610 bool RefersToCapture = false) {
611 D->setReferenced();
612 D->markUsed(S.Context);
613 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
614 SourceLocation(), D, RefersToCapture, Loc, Ty,
615 VK_LValue);
616}
617
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000618DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
619 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620 DSAVarData DVar;
621
622 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
623 // in a Construct, C/C++, predetermined, p.1]
624 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000625 auto *VD = dyn_cast<VarDecl>(D);
626 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
627 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000628 SemaRef.getLangOpts().OpenMPUseTLS &&
629 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000630 (VD && VD->getStorageClass() == SC_Register &&
631 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
632 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000633 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000634 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000635 }
636 if (Stack[0].SharingMap.count(D)) {
637 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
638 DVar.CKind = OMPC_threadprivate;
639 return DVar;
640 }
641
642 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000643 // in a Construct, C/C++, predetermined, p.4]
644 // Static data members are shared.
645 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
646 // in a Construct, C/C++, predetermined, p.7]
647 // Variables with static storage duration that are declared in a scope
648 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000649 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000650 DSAVarData DVarTemp =
651 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
652 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000653 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000655 DVar.CKind = OMPC_shared;
656 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657 }
658
659 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000660 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
661 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
663 // in a Construct, C/C++, predetermined, p.6]
664 // Variables with const qualified type having no mutable member are
665 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000666 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000667 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000668 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
669 if (auto *CTD = CTSD->getSpecializedTemplate())
670 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000672 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
673 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674 // Variables with const-qualified type having no mutable member may be
675 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000676 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
677 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000678 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
679 return DVar;
680
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 DVar.CKind = OMPC_shared;
682 return DVar;
683 }
684
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685 // Explicitly specified attributes and local variables with predetermined
686 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000687 auto StartI = std::next(Stack.rbegin());
688 auto EndI = std::prev(Stack.rend());
689 if (FromParent && StartI != EndI) {
690 StartI = std::next(StartI);
691 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000692 auto I = std::prev(StartI);
693 if (I->SharingMap.count(D)) {
694 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000695 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000696 DVar.CKind = I->SharingMap[D].Attributes;
697 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000698 }
699
700 return DVar;
701}
702
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000703DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
704 bool FromParent) {
705 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 auto StartI = Stack.rbegin();
707 auto EndI = std::prev(Stack.rend());
708 if (FromParent && StartI != EndI) {
709 StartI = std::next(StartI);
710 }
711 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000712}
713
Alexey Bataevf29276e2014-06-18 04:14:57 +0000714template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000715DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000716 DirectivesPredicate DPred,
717 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000718 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
724 for (auto I = StartI, EE = EndI; I != EE; ++I) {
725 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000727 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000728 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 return DVar;
730 }
731 return DSAVarData();
732}
733
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000736DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000737 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000738 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000739 auto StartI = std::next(Stack.rbegin());
740 auto EndI = std::prev(Stack.rend());
741 if (FromParent && StartI != EndI) {
742 StartI = std::next(StartI);
743 }
744 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000745 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000746 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000747 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000748 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000749 return DVar;
750 return DSAVarData();
751 }
752 return DSAVarData();
753}
754
Alexey Bataevaac108a2015-06-23 04:51:00 +0000755bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000756 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000757 unsigned Level) {
758 if (CPred(ClauseKindMode))
759 return true;
760 if (isClauseParsingMode())
761 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000762 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000763 auto StartI = Stack.rbegin();
764 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000765 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000766 return false;
767 std::advance(StartI, Level);
768 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
769 CPred(StartI->SharingMap[D].Attributes);
770}
771
Samuel Antao4be30e92015-10-02 17:14:03 +0000772bool DSAStackTy::hasExplicitDirective(
773 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
774 unsigned Level) {
775 if (isClauseParsingMode())
776 ++Level;
777 auto StartI = Stack.rbegin();
778 auto EndI = std::prev(Stack.rend());
779 if (std::distance(StartI, EndI) <= (int)Level)
780 return false;
781 std::advance(StartI, Level);
782 return DPred(StartI->Directive);
783}
784
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785template <class NamedDirectivesPredicate>
786bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
787 auto StartI = std::next(Stack.rbegin());
788 auto EndI = std::prev(Stack.rend());
789 if (FromParent && StartI != EndI) {
790 StartI = std::next(StartI);
791 }
792 for (auto I = StartI, EE = EndI; I != EE; ++I) {
793 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
794 return true;
795 }
796 return false;
797}
798
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000799OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
800 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
801 if (I->CurScope == S)
802 return I->Directive;
803 return OMPD_unknown;
804}
805
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806void Sema::InitDataSharingAttributesStack() {
807 VarDataSharingAttributesStack = new DSAStackTy(*this);
808}
809
810#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
811
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000812bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000813 const CapturedRegionScopeInfo *RSI) {
814 assert(LangOpts.OpenMP && "OpenMP is not allowed");
815
816 auto &Ctx = getASTContext();
817 bool IsByRef = true;
818
819 // Find the directive that is associated with the provided scope.
820 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000821 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000822
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000823 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824 // This table summarizes how a given variable should be passed to the device
825 // given its type and the clauses where it appears. This table is based on
826 // the description in OpenMP 4.5 [2.10.4, target Construct] and
827 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
828 //
829 // =========================================================================
830 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
831 // | |(tofrom:scalar)| | pvt | | | |
832 // =========================================================================
833 // | scl | | | | - | | bycopy|
834 // | scl | | - | x | - | - | bycopy|
835 // | scl | | x | - | - | - | null |
836 // | scl | x | | | - | | byref |
837 // | scl | x | - | x | - | - | bycopy|
838 // | scl | x | x | - | - | - | null |
839 // | scl | | - | - | - | x | byref |
840 // | scl | x | - | - | - | x | byref |
841 //
842 // | agg | n.a. | | | - | | byref |
843 // | agg | n.a. | - | x | - | - | byref |
844 // | agg | n.a. | x | - | - | - | null |
845 // | agg | n.a. | - | - | - | x | byref |
846 // | agg | n.a. | - | - | - | x[] | byref |
847 //
848 // | ptr | n.a. | | | - | | bycopy|
849 // | ptr | n.a. | - | x | - | - | bycopy|
850 // | ptr | n.a. | x | - | - | - | null |
851 // | ptr | n.a. | - | - | - | x | byref |
852 // | ptr | n.a. | - | - | - | x[] | bycopy|
853 // | ptr | n.a. | - | - | x | | bycopy|
854 // | ptr | n.a. | - | - | x | x | bycopy|
855 // | ptr | n.a. | - | - | x | x[] | bycopy|
856 // =========================================================================
857 // Legend:
858 // scl - scalar
859 // ptr - pointer
860 // agg - aggregate
861 // x - applies
862 // - - invalid in this combination
863 // [] - mapped with an array section
864 // byref - should be mapped by reference
865 // byval - should be mapped by value
866 // null - initialize a local variable to null on the device
867 //
868 // Observations:
869 // - All scalar declarations that show up in a map clause have to be passed
870 // by reference, because they may have been mapped in the enclosing data
871 // environment.
872 // - If the scalar value does not fit the size of uintptr, it has to be
873 // passed by reference, regardless the result in the table above.
874 // - For pointers mapped by value that have either an implicit map or an
875 // array section, the runtime library may pass the NULL value to the
876 // device instead of the value passed to it by the compiler.
877
878 // FIXME: Right now, only implicit maps are implemented. Properly mapping
879 // values requires having the map, private, and firstprivate clauses SEMA
880 // and parsing in place, which we don't yet.
881
882 if (Ty->isReferenceType())
883 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
884 IsByRef = !Ty->isScalarType();
885 }
886
887 // When passing data by value, we need to make sure it fits the uintptr size
888 // and alignment, because the runtime library only deals with uintptr types.
889 // If it does not fit the uintptr size, we need to pass the data by reference
890 // instead.
891 if (!IsByRef &&
892 (Ctx.getTypeSizeInChars(Ty) >
893 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000894 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000895 IsByRef = true;
896
897 return IsByRef;
898}
899
Alexey Bataev90c228f2016-02-08 09:29:13 +0000900VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000901 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000902 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000903
904 // If we are attempting to capture a global variable in a directive with
905 // 'target' we return true so that this global is also mapped to the device.
906 //
907 // FIXME: If the declaration is enclosed in a 'declare target' directive,
908 // then it should not be captured. Therefore, an extra check has to be
909 // inserted here once support for 'declare target' is added.
910 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000911 auto *VD = dyn_cast<VarDecl>(D);
912 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000913 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000914 !DSAStack->isClauseParsingMode())
915 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000916 if (DSAStack->getCurScope() &&
917 DSAStack->hasDirective(
918 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
919 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000920 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000921 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000922 false))
923 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000924 }
925
Alexey Bataev48977c32015-08-04 08:10:48 +0000926 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
927 (!DSAStack->isClauseParsingMode() ||
928 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000929 if (DSAStack->isLoopControlVariable(D) ||
930 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000931 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000932 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000933 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000935 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000936 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000937 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000938 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000939 if (DVarPrivate.CKind != OMPC_unknown)
940 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000941 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000942 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000943}
944
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000946 assert(LangOpts.OpenMP && "OpenMP is not allowed");
947 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000948 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000949}
950
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000951bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000952 assert(LangOpts.OpenMP && "OpenMP is not allowed");
953 // Return true if the current level is no longer enclosed in a target region.
954
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955 auto *VD = dyn_cast<VarDecl>(D);
956 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000957 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
958 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000959}
960
Alexey Bataeved09d242014-05-28 05:53:51 +0000961void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962
963void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
964 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000965 Scope *CurScope, SourceLocation Loc) {
966 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967 PushExpressionEvaluationContext(PotentiallyEvaluated);
968}
969
Alexey Bataevaac108a2015-06-23 04:51:00 +0000970void Sema::StartOpenMPClause(OpenMPClauseKind K) {
971 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000972}
973
Alexey Bataevaac108a2015-06-23 04:51:00 +0000974void Sema::EndOpenMPClause() {
975 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000976}
977
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000979 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
980 // A variable of class type (or array thereof) that appears in a lastprivate
981 // clause requires an accessible, unambiguous default constructor for the
982 // class type, unless the list item is also specified in a firstprivate
983 // clause.
984 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000985 for (auto *C : D->clauses()) {
986 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
987 SmallVector<Expr *, 8> PrivateCopies;
988 for (auto *DE : Clause->varlists()) {
989 if (DE->isValueDependent() || DE->isTypeDependent()) {
990 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000991 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000992 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000993 VarDecl *VD = nullptr;
994 FieldDecl *FD = nullptr;
995 ValueDecl *D;
Alexey Bataev74caaf22016-02-20 04:09:36 +0000996 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
997 if (auto *OCE = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl())) {
998 FD = cast<FieldDecl>(
999 cast<MemberExpr>(OCE->getInit()->IgnoreImpCasts())
1000 ->getMemberDecl());
1001 D = FD;
1002 } else {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001003 VD = cast<VarDecl>(DRE->getDecl());
1004 D = VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001005 }
1006 QualType Type = D->getType().getNonReferenceType();
1007 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001008 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001009 // Generate helper private variable and initialize it with the
1010 // default value. The address of the original variable is replaced
1011 // by the address of the new private variable in CodeGen. This new
1012 // variable is not added to IdResolver, so the code in the OpenMP
1013 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001014 auto *VDPrivate = buildVarDecl(
1015 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001016 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1018 if (VDPrivate->isInvalidDecl())
1019 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001020 PrivateCopies.push_back(buildDeclRefExpr(
1021 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001022 } else {
1023 // The variable is also a firstprivate, so initialization sequence
1024 // for private copy is generated already.
1025 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001026 }
1027 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001028 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001030 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001031 }
1032 }
1033 }
1034
Alexey Bataev758e55e2013-09-06 18:03:48 +00001035 DSAStack->pop();
1036 DiscardCleanupsInEvaluationContext();
1037 PopExpressionEvaluationContext();
1038}
1039
Alexander Musman3276a272015-03-21 10:12:56 +00001040static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1041 Expr *NumIterations, Sema &SemaRef,
1042 Scope *S);
1043
Alexey Bataeva769e072013-03-22 06:34:35 +00001044namespace {
1045
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046class VarDeclFilterCCC : public CorrectionCandidateCallback {
1047private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001048 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001049
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001051 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001052 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053 NamedDecl *ND = Candidate.getCorrectionDecl();
1054 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1055 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001056 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1057 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001058 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001060 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001061};
Alexey Bataeved09d242014-05-28 05:53:51 +00001062} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001063
1064ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1065 CXXScopeSpec &ScopeSpec,
1066 const DeclarationNameInfo &Id) {
1067 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1068 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1069
1070 if (Lookup.isAmbiguous())
1071 return ExprError();
1072
1073 VarDecl *VD;
1074 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001075 if (TypoCorrection Corrected = CorrectTypo(
1076 Id, LookupOrdinaryName, CurScope, nullptr,
1077 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001078 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001079 PDiag(Lookup.empty()
1080 ? diag::err_undeclared_var_use_suggest
1081 : diag::err_omp_expected_var_arg_suggest)
1082 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001083 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001085 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1086 : diag::err_omp_expected_var_arg)
1087 << Id.getName();
1088 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001090 } else {
1091 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001093 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1094 return ExprError();
1095 }
1096 }
1097 Lookup.suppressDiagnostics();
1098
1099 // OpenMP [2.9.2, Syntax, C/C++]
1100 // Variables must be file-scope, namespace-scope, or static block-scope.
1101 if (!VD->hasGlobalStorage()) {
1102 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1104 bool IsDecl =
1105 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001106 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001107 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1108 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001109 return ExprError();
1110 }
1111
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001112 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1113 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1115 // A threadprivate directive for file-scope variables must appear outside
1116 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001117 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1118 !getCurLexicalContext()->isTranslationUnit()) {
1119 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001120 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1121 bool IsDecl =
1122 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1123 Diag(VD->getLocation(),
1124 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1125 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001126 return ExprError();
1127 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001128 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1129 // A threadprivate directive for static class member variables must appear
1130 // in the class definition, in the same scope in which the member
1131 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001132 if (CanonicalVD->isStaticDataMember() &&
1133 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1134 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001135 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1136 bool IsDecl =
1137 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1138 Diag(VD->getLocation(),
1139 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1140 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001141 return ExprError();
1142 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001143 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1144 // A threadprivate directive for namespace-scope variables must appear
1145 // outside any definition or declaration other than the namespace
1146 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001147 if (CanonicalVD->getDeclContext()->isNamespace() &&
1148 (!getCurLexicalContext()->isFileContext() ||
1149 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1150 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001151 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1152 bool IsDecl =
1153 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1154 Diag(VD->getLocation(),
1155 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1156 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001157 return ExprError();
1158 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1160 // A threadprivate directive for static block-scope variables must appear
1161 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001162 if (CanonicalVD->isStaticLocal() && CurScope &&
1163 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001164 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001165 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1166 bool IsDecl =
1167 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1168 Diag(VD->getLocation(),
1169 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1170 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 return ExprError();
1172 }
1173
1174 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1175 // A threadprivate directive must lexically precede all references to any
1176 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001177 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 return ExprError();
1181 }
1182
1183 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001184 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1185 SourceLocation(), VD,
1186 /*RefersToEnclosingVariableOrCapture=*/false,
1187 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188}
1189
Alexey Bataeved09d242014-05-28 05:53:51 +00001190Sema::DeclGroupPtrTy
1191Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1192 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001194 CurContext->addDecl(D);
1195 return DeclGroupPtrTy::make(DeclGroupRef(D));
1196 }
David Blaikie0403cb12016-01-15 23:43:25 +00001197 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001198}
1199
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001200namespace {
1201class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1202 Sema &SemaRef;
1203
1204public:
1205 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1206 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1207 if (VD->hasLocalStorage()) {
1208 SemaRef.Diag(E->getLocStart(),
1209 diag::err_omp_local_var_in_threadprivate_init)
1210 << E->getSourceRange();
1211 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1212 << VD << VD->getSourceRange();
1213 return true;
1214 }
1215 }
1216 return false;
1217 }
1218 bool VisitStmt(const Stmt *S) {
1219 for (auto Child : S->children()) {
1220 if (Child && Visit(Child))
1221 return true;
1222 }
1223 return false;
1224 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001225 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001226};
1227} // namespace
1228
Alexey Bataeved09d242014-05-28 05:53:51 +00001229OMPThreadPrivateDecl *
1230Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001231 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001232 for (auto &RefExpr : VarList) {
1233 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001234 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1235 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001236
Alexey Bataev376b4a42016-02-09 09:41:09 +00001237 // Mark variable as used.
1238 VD->setReferenced();
1239 VD->markUsed(Context);
1240
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001241 QualType QType = VD->getType();
1242 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1243 // It will be analyzed later.
1244 Vars.push_back(DE);
1245 continue;
1246 }
1247
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1249 // A threadprivate variable must not have an incomplete type.
1250 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001252 continue;
1253 }
1254
1255 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1256 // A threadprivate variable must not have a reference type.
1257 if (VD->getType()->isReferenceType()) {
1258 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001259 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1260 bool IsDecl =
1261 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1262 Diag(VD->getLocation(),
1263 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1264 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001265 continue;
1266 }
1267
Samuel Antaof8b50122015-07-13 22:54:53 +00001268 // Check if this is a TLS variable. If TLS is not being supported, produce
1269 // the corresponding diagnostic.
1270 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1271 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1272 getLangOpts().OpenMPUseTLS &&
1273 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001274 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1275 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001276 Diag(ILoc, diag::err_omp_var_thread_local)
1277 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001278 bool IsDecl =
1279 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1280 Diag(VD->getLocation(),
1281 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1282 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001283 continue;
1284 }
1285
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001286 // Check if initial value of threadprivate variable reference variable with
1287 // local storage (it is not supported by runtime).
1288 if (auto Init = VD->getAnyInitializer()) {
1289 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001290 if (Checker.Visit(Init))
1291 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001292 }
1293
Alexey Bataeved09d242014-05-28 05:53:51 +00001294 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001295 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001296 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1297 Context, SourceRange(Loc, Loc)));
1298 if (auto *ML = Context.getASTMutationListener())
1299 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001300 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001301 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001302 if (!Vars.empty()) {
1303 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1304 Vars);
1305 D->setAccess(AS_public);
1306 }
1307 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001308}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001309
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001311 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001312 bool IsLoopIterVar = false) {
1313 if (DVar.RefExpr) {
1314 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1315 << getOpenMPClauseName(DVar.CKind);
1316 return;
1317 }
1318 enum {
1319 PDSA_StaticMemberShared,
1320 PDSA_StaticLocalVarShared,
1321 PDSA_LoopIterVarPrivate,
1322 PDSA_LoopIterVarLinear,
1323 PDSA_LoopIterVarLastprivate,
1324 PDSA_ConstVarShared,
1325 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001326 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001327 PDSA_LocalVarPrivate,
1328 PDSA_Implicit
1329 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001330 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001331 auto ReportLoc = D->getLocation();
1332 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001333 if (IsLoopIterVar) {
1334 if (DVar.CKind == OMPC_private)
1335 Reason = PDSA_LoopIterVarPrivate;
1336 else if (DVar.CKind == OMPC_lastprivate)
1337 Reason = PDSA_LoopIterVarLastprivate;
1338 else
1339 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001340 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1341 Reason = PDSA_TaskVarFirstprivate;
1342 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001343 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001344 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001345 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001346 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001347 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001348 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001349 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001350 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001351 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001352 ReportHint = true;
1353 Reason = PDSA_LocalVarPrivate;
1354 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001355 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001356 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001357 << Reason << ReportHint
1358 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1359 } else if (DVar.ImplicitDSALoc.isValid()) {
1360 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1361 << getOpenMPClauseName(DVar.CKind);
1362 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001363}
1364
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365namespace {
1366class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1367 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001368 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369 bool ErrorFound;
1370 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001371 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001372 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001373
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374public:
1375 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001378 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1379 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001380
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001381 auto DVar = Stack->getTopDSA(VD, false);
1382 // Check if the variable has explicit DSA set and stop analysis if it so.
1383 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001384
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001385 auto ELoc = E->getExprLoc();
1386 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001387 // The default(none) clause requires that each variable that is referenced
1388 // in the construct, and does not have a predetermined data-sharing
1389 // attribute, must have its data-sharing attribute explicitly determined
1390 // by being listed in a data-sharing attribute clause.
1391 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001392 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001393 VarsWithInheritedDSA.count(VD) == 0) {
1394 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001395 return;
1396 }
1397
1398 // OpenMP [2.9.3.6, Restrictions, p.2]
1399 // A list item that appears in a reduction clause of the innermost
1400 // enclosing worksharing or parallel construct may not be accessed in an
1401 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001402 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001403 [](OpenMPDirectiveKind K) -> bool {
1404 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001405 isOpenMPWorksharingDirective(K) ||
1406 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 },
1408 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001409 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1410 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1412 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001413 return;
1414 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001415
1416 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001417 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001418 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001419 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001420 }
1421 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001422 void VisitMemberExpr(MemberExpr *E) {
1423 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1424 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1425 auto DVar = Stack->getTopDSA(FD, false);
1426 // Check if the variable has explicit DSA set and stop analysis if it
1427 // so.
1428 if (DVar.RefExpr)
1429 return;
1430
1431 auto ELoc = E->getExprLoc();
1432 auto DKind = Stack->getCurrentDirective();
1433 // OpenMP [2.9.3.6, Restrictions, p.2]
1434 // A list item that appears in a reduction clause of the innermost
1435 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001436 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 DVar =
1438 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1439 [](OpenMPDirectiveKind K) -> bool {
1440 return isOpenMPParallelDirective(K) ||
1441 isOpenMPWorksharingDirective(K) ||
1442 isOpenMPTeamsDirective(K);
1443 },
1444 false);
1445 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1446 ErrorFound = true;
1447 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1448 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1449 return;
1450 }
1451
1452 // Define implicit data-sharing attributes for task.
1453 DVar = Stack->getImplicitDSA(FD, false);
1454 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1455 ImplicitFirstprivate.push_back(E);
1456 }
1457 }
1458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001460 for (auto *C : S->clauses()) {
1461 // Skip analysis of arguments of implicitly defined firstprivate clause
1462 // for task directives.
1463 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1464 for (auto *CC : C->children()) {
1465 if (CC)
1466 Visit(CC);
1467 }
1468 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469 }
1470 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001471 for (auto *C : S->children()) {
1472 if (C && !isa<OMPExecutableDirective>(C))
1473 Visit(C);
1474 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476
1477 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001478 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001479 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001480 return VarsWithInheritedDSA;
1481 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482
Alexey Bataev7ff55242014-06-19 09:13:45 +00001483 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1484 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001485};
Alexey Bataeved09d242014-05-28 05:53:51 +00001486} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001487
Alexey Bataevbae9a792014-06-27 10:37:06 +00001488void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001489 switch (DKind) {
1490 case OMPD_parallel: {
1491 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001492 QualType KmpInt32PtrTy =
1493 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001494 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001495 std::make_pair(".global_tid.", KmpInt32PtrTy),
1496 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1497 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1500 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 break;
1502 }
1503 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001504 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001509 break;
1510 }
1511 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001512 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001513 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001514 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1516 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001517 break;
1518 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001519 case OMPD_for_simd: {
1520 Sema::CapturedParamNameType Params[] = {
1521 std::make_pair(StringRef(), QualType()) // __context with shared vars
1522 };
1523 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1524 Params);
1525 break;
1526 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001527 case OMPD_sections: {
1528 Sema::CapturedParamNameType Params[] = {
1529 std::make_pair(StringRef(), QualType()) // __context with shared vars
1530 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001531 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1532 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001533 break;
1534 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001535 case OMPD_section: {
1536 Sema::CapturedParamNameType Params[] = {
1537 std::make_pair(StringRef(), QualType()) // __context with shared vars
1538 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001539 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1540 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001541 break;
1542 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001543 case OMPD_single: {
1544 Sema::CapturedParamNameType Params[] = {
1545 std::make_pair(StringRef(), QualType()) // __context with shared vars
1546 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001547 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1548 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001549 break;
1550 }
Alexander Musman80c22892014-07-17 08:54:58 +00001551 case OMPD_master: {
1552 Sema::CapturedParamNameType Params[] = {
1553 std::make_pair(StringRef(), QualType()) // __context with shared vars
1554 };
1555 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1556 Params);
1557 break;
1558 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001559 case OMPD_critical: {
1560 Sema::CapturedParamNameType Params[] = {
1561 std::make_pair(StringRef(), QualType()) // __context with shared vars
1562 };
1563 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1564 Params);
1565 break;
1566 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001567 case OMPD_parallel_for: {
1568 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001569 QualType KmpInt32PtrTy =
1570 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001571 Sema::CapturedParamNameType Params[] = {
1572 std::make_pair(".global_tid.", KmpInt32PtrTy),
1573 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1574 std::make_pair(StringRef(), QualType()) // __context with shared vars
1575 };
1576 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1577 Params);
1578 break;
1579 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001580 case OMPD_parallel_for_simd: {
1581 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001582 QualType KmpInt32PtrTy =
1583 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001584 Sema::CapturedParamNameType Params[] = {
1585 std::make_pair(".global_tid.", KmpInt32PtrTy),
1586 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1587 std::make_pair(StringRef(), QualType()) // __context with shared vars
1588 };
1589 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1590 Params);
1591 break;
1592 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001593 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001594 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001595 QualType KmpInt32PtrTy =
1596 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001597 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001598 std::make_pair(".global_tid.", KmpInt32PtrTy),
1599 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001600 std::make_pair(StringRef(), QualType()) // __context with shared vars
1601 };
1602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1603 Params);
1604 break;
1605 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001606 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001607 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001608 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1609 FunctionProtoType::ExtProtoInfo EPI;
1610 EPI.Variadic = true;
1611 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001612 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001613 std::make_pair(".global_tid.", KmpInt32Ty),
1614 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001615 std::make_pair(".privates.",
1616 Context.VoidPtrTy.withConst().withRestrict()),
1617 std::make_pair(
1618 ".copy_fn.",
1619 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 std::make_pair(StringRef(), QualType()) // __context with shared vars
1621 };
1622 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1623 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001624 // Mark this captured region as inlined, because we don't use outlined
1625 // function directly.
1626 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1627 AlwaysInlineAttr::CreateImplicit(
1628 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001629 break;
1630 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001631 case OMPD_ordered: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
1635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
1637 break;
1638 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001639 case OMPD_atomic: {
1640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
1643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
1645 break;
1646 }
Michael Wong65f367f2015-07-21 13:44:28 +00001647 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001648 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001649 case OMPD_target_parallel:
1650 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001651 Sema::CapturedParamNameType Params[] = {
1652 std::make_pair(StringRef(), QualType()) // __context with shared vars
1653 };
1654 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1655 Params);
1656 break;
1657 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001658 case OMPD_teams: {
1659 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001660 QualType KmpInt32PtrTy =
1661 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001662 Sema::CapturedParamNameType Params[] = {
1663 std::make_pair(".global_tid.", KmpInt32PtrTy),
1664 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1665 std::make_pair(StringRef(), QualType()) // __context with shared vars
1666 };
1667 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1668 Params);
1669 break;
1670 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001671 case OMPD_taskgroup: {
1672 Sema::CapturedParamNameType Params[] = {
1673 std::make_pair(StringRef(), QualType()) // __context with shared vars
1674 };
1675 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1676 Params);
1677 break;
1678 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001679 case OMPD_taskloop: {
1680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(StringRef(), QualType()) // __context with shared vars
1682 };
1683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1684 Params);
1685 break;
1686 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001687 case OMPD_taskloop_simd: {
1688 Sema::CapturedParamNameType Params[] = {
1689 std::make_pair(StringRef(), QualType()) // __context with shared vars
1690 };
1691 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1692 Params);
1693 break;
1694 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001695 case OMPD_distribute: {
1696 Sema::CapturedParamNameType Params[] = {
1697 std::make_pair(StringRef(), QualType()) // __context with shared vars
1698 };
1699 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1700 Params);
1701 break;
1702 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_taskyield:
1705 case OMPD_barrier:
1706 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001707 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001708 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001709 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001710 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001711 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 llvm_unreachable("OpenMP Directive is not allowed");
1713 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("Unknown OpenMP directive");
1715 }
1716}
1717
Alexey Bataev3392d762016-02-16 11:18:12 +00001718static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1719 Expr *CaptureExpr) {
Alexey Bataev4244be22016-02-11 05:35:55 +00001720 ASTContext &C = S.getASTContext();
1721 Expr *Init = CaptureExpr->IgnoreImpCasts();
1722 QualType Ty = Init->getType();
1723 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1724 if (S.getLangOpts().CPlusPlus)
1725 Ty = C.getLValueReferenceType(Ty);
1726 else {
1727 Ty = C.getPointerType(Ty);
1728 ExprResult Res =
1729 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1730 if (!Res.isUsable())
1731 return nullptr;
1732 Init = Res.get();
1733 }
1734 }
1735 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1736 S.CurContext->addHiddenDecl(CED);
1737 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1738 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001739 return CED;
1740}
1741
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001742static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr) {
1743 OMPCapturedExprDecl *CD;
1744 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1745 CD = cast<OMPCapturedExprDecl>(VD);
1746 else
1747 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00001748 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1749 SourceLocation());
1750}
1751
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001752static DeclRefExpr *buildCapture(Sema &S, Expr *CaptureExpr) {
1753 auto *CD = buildCaptureDecl(
1754 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00001755 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1756 SourceLocation());
Alexey Bataev4244be22016-02-11 05:35:55 +00001757}
1758
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001759StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1760 ArrayRef<OMPClause *> Clauses) {
1761 if (!S.isUsable()) {
1762 ActOnCapturedRegionError();
1763 return StmtError();
1764 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001765
1766 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001767 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001768 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001769 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001770 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001771 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001772 Clause->getClauseKind() == OMPC_copyprivate ||
1773 (getLangOpts().OpenMPUseTLS &&
1774 getASTContext().getTargetInfo().isTLSSupported() &&
1775 Clause->getClauseKind() == OMPC_copyin)) {
1776 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001777 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001778 for (auto *VarRef : Clause->children()) {
1779 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001780 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001781 }
1782 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001783 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001784 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001785 // Mark all variables in private list clauses as used in inner region.
1786 // Required for proper codegen of combined directives.
1787 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001788 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1789 if (auto *S = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1790 for (auto *D : S->decls())
1791 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1792 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001793 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001794 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001795 if (Clause->getClauseKind() == OMPC_schedule)
1796 SC = cast<OMPScheduleClause>(Clause);
1797 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001798 OC = cast<OMPOrderedClause>(Clause);
1799 else if (Clause->getClauseKind() == OMPC_linear)
1800 LCs.push_back(cast<OMPLinearClause>(Clause));
1801 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001802 bool ErrorFound = false;
1803 // OpenMP, 2.7.1 Loop Construct, Restrictions
1804 // The nonmonotonic modifier cannot be specified if an ordered clause is
1805 // specified.
1806 if (SC &&
1807 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1808 SC->getSecondScheduleModifier() ==
1809 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1810 OC) {
1811 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1812 ? SC->getFirstScheduleModifierLoc()
1813 : SC->getSecondScheduleModifierLoc(),
1814 diag::err_omp_schedule_nonmonotonic_ordered)
1815 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1816 ErrorFound = true;
1817 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001818 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1819 for (auto *C : LCs) {
1820 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1821 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1822 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001823 ErrorFound = true;
1824 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001825 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1826 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1827 OC->getNumForLoops()) {
1828 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1829 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1830 ErrorFound = true;
1831 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001832 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001833 ActOnCapturedRegionError();
1834 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001835 }
1836 return ActOnCapturedRegionEnd(S.get());
1837}
1838
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001839static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1840 OpenMPDirectiveKind CurrentRegion,
1841 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001842 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001843 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001844 // Allowed nesting of constructs
1845 // +------------------+-----------------+------------------------------------+
1846 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1847 // +------------------+-----------------+------------------------------------+
1848 // | parallel | parallel | * |
1849 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001850 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001851 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001852 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001853 // | parallel | simd | * |
1854 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001855 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001856 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001857 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001858 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001859 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001860 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001861 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001862 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001863 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001864 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001865 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001866 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001867 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001868 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001869 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001870 // | parallel | target parallel | * |
1871 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001872 // | parallel | target enter | * |
1873 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001874 // | parallel | target exit | * |
1875 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001876 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001877 // | parallel | cancellation | |
1878 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001879 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001880 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001881 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001882 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001883 // +------------------+-----------------+------------------------------------+
1884 // | for | parallel | * |
1885 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001886 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001887 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001888 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001889 // | for | simd | * |
1890 // | for | sections | + |
1891 // | for | section | + |
1892 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001893 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001894 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001895 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001896 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001897 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001898 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001899 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001900 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001901 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001902 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001903 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001904 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001905 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001906 // | for | target parallel | * |
1907 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001908 // | for | target enter | * |
1909 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001910 // | for | target exit | * |
1911 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001912 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001913 // | for | cancellation | |
1914 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001915 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001916 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001917 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001918 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001919 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001920 // | master | parallel | * |
1921 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001922 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001923 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001924 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001925 // | master | simd | * |
1926 // | master | sections | + |
1927 // | master | section | + |
1928 // | master | single | + |
1929 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001930 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001931 // | master |parallel sections| * |
1932 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001933 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001934 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001935 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001936 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001937 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001938 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001939 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001940 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001941 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001942 // | master | target parallel | * |
1943 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001944 // | master | target enter | * |
1945 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001946 // | master | target exit | * |
1947 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001948 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001949 // | master | cancellation | |
1950 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001951 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001952 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001953 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001954 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001955 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001956 // | critical | parallel | * |
1957 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001958 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001959 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001960 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001961 // | critical | simd | * |
1962 // | critical | sections | + |
1963 // | critical | section | + |
1964 // | critical | single | + |
1965 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001966 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001967 // | critical |parallel sections| * |
1968 // | critical | task | * |
1969 // | critical | taskyield | * |
1970 // | critical | barrier | + |
1971 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001972 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001973 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001974 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001975 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001976 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001977 // | critical | target parallel | * |
1978 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001979 // | critical | target enter | * |
1980 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001981 // | critical | target exit | * |
1982 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001983 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001984 // | critical | cancellation | |
1985 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001986 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001987 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001988 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001989 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001990 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001991 // | simd | parallel | |
1992 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001993 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001994 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001995 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001996 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001997 // | simd | sections | |
1998 // | simd | section | |
1999 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002000 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002001 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002002 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002003 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002004 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002005 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002006 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002007 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002008 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002009 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002010 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002011 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002012 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002013 // | simd | target parallel | |
2014 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002015 // | simd | target enter | |
2016 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002017 // | simd | target exit | |
2018 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002019 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002020 // | simd | cancellation | |
2021 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002022 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002023 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002024 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002025 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002026 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002027 // | for simd | parallel | |
2028 // | for simd | for | |
2029 // | for simd | for simd | |
2030 // | for simd | master | |
2031 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002032 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002033 // | for simd | sections | |
2034 // | for simd | section | |
2035 // | for simd | single | |
2036 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002037 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002038 // | for simd |parallel sections| |
2039 // | for simd | task | |
2040 // | for simd | taskyield | |
2041 // | for simd | barrier | |
2042 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002043 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002044 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002045 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002046 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002047 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002048 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002049 // | for simd | target parallel | |
2050 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002051 // | for simd | target enter | |
2052 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002053 // | for simd | target exit | |
2054 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002055 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002056 // | for simd | cancellation | |
2057 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002058 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002059 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002060 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002061 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002062 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002063 // | parallel for simd| parallel | |
2064 // | parallel for simd| for | |
2065 // | parallel for simd| for simd | |
2066 // | parallel for simd| master | |
2067 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002068 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002069 // | parallel for simd| sections | |
2070 // | parallel for simd| section | |
2071 // | parallel for simd| single | |
2072 // | parallel for simd| parallel for | |
2073 // | parallel for simd|parallel for simd| |
2074 // | parallel for simd|parallel sections| |
2075 // | parallel for simd| task | |
2076 // | parallel for simd| taskyield | |
2077 // | parallel for simd| barrier | |
2078 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002079 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002080 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002081 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002082 // | parallel for simd| atomic | |
2083 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002084 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002085 // | parallel for simd| target parallel | |
2086 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002087 // | parallel for simd| target enter | |
2088 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002089 // | parallel for simd| target exit | |
2090 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002091 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002092 // | parallel for simd| cancellation | |
2093 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002094 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002095 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002096 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002097 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002098 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002099 // | sections | parallel | * |
2100 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002101 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002102 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002103 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002104 // | sections | simd | * |
2105 // | sections | sections | + |
2106 // | sections | section | * |
2107 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002108 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002109 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002110 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002111 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002112 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002113 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002114 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002115 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002116 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002117 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002118 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002119 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002120 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002121 // | sections | target parallel | * |
2122 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002123 // | sections | target enter | * |
2124 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002125 // | sections | target exit | * |
2126 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002127 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002128 // | sections | cancellation | |
2129 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002130 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002131 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002132 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002133 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002134 // +------------------+-----------------+------------------------------------+
2135 // | section | parallel | * |
2136 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002137 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002138 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002139 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002140 // | section | simd | * |
2141 // | section | sections | + |
2142 // | section | section | + |
2143 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002144 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002145 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002146 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002147 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002148 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002149 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002150 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002151 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002152 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002153 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002154 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002155 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002156 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002157 // | section | target parallel | * |
2158 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002159 // | section | target enter | * |
2160 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002161 // | section | target exit | * |
2162 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002163 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002164 // | section | cancellation | |
2165 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002166 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002167 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002168 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002169 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002170 // +------------------+-----------------+------------------------------------+
2171 // | single | parallel | * |
2172 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002173 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002174 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002175 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002176 // | single | simd | * |
2177 // | single | sections | + |
2178 // | single | section | + |
2179 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002180 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002181 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002182 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002183 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002184 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002185 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002186 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002187 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002188 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002189 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002190 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002191 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002192 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002193 // | single | target parallel | * |
2194 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002195 // | single | target enter | * |
2196 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002197 // | single | target exit | * |
2198 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002199 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002200 // | single | cancellation | |
2201 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002202 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002203 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002204 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002205 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002206 // +------------------+-----------------+------------------------------------+
2207 // | parallel for | parallel | * |
2208 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002209 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002210 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002211 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002212 // | parallel for | simd | * |
2213 // | parallel for | sections | + |
2214 // | parallel for | section | + |
2215 // | parallel for | single | + |
2216 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002217 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002218 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002219 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002220 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002221 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002222 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002223 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002224 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002225 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002226 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002227 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002228 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002229 // | parallel for | target parallel | * |
2230 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002231 // | parallel for | target enter | * |
2232 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002233 // | parallel for | target exit | * |
2234 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002235 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002236 // | parallel for | cancellation | |
2237 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002238 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002239 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002240 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002241 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002242 // +------------------+-----------------+------------------------------------+
2243 // | parallel sections| parallel | * |
2244 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002245 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002246 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002247 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002248 // | parallel sections| simd | * |
2249 // | parallel sections| sections | + |
2250 // | parallel sections| section | * |
2251 // | parallel sections| single | + |
2252 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002253 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002254 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002255 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002256 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002257 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002258 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002259 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002260 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002261 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002262 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002263 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002264 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002265 // | parallel sections| target parallel | * |
2266 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002267 // | parallel sections| target enter | * |
2268 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002269 // | parallel sections| target exit | * |
2270 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002271 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002272 // | parallel sections| cancellation | |
2273 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002274 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002275 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002276 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002277 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002278 // +------------------+-----------------+------------------------------------+
2279 // | task | parallel | * |
2280 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002281 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002282 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002283 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002284 // | task | simd | * |
2285 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002286 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002287 // | task | single | + |
2288 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002289 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002290 // | task |parallel sections| * |
2291 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002292 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002293 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002294 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002295 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002296 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002297 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002298 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002299 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002300 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002301 // | task | target parallel | * |
2302 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002303 // | task | target enter | * |
2304 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002305 // | task | target exit | * |
2306 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002307 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002308 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002309 // | | point | ! |
2310 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002311 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002312 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002313 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002314 // +------------------+-----------------+------------------------------------+
2315 // | ordered | parallel | * |
2316 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002317 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002318 // | ordered | master | * |
2319 // | ordered | critical | * |
2320 // | ordered | simd | * |
2321 // | ordered | sections | + |
2322 // | ordered | section | + |
2323 // | ordered | single | + |
2324 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002325 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002326 // | ordered |parallel sections| * |
2327 // | ordered | task | * |
2328 // | ordered | taskyield | * |
2329 // | ordered | barrier | + |
2330 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002331 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002332 // | ordered | flush | * |
2333 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002334 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002335 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002336 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002337 // | ordered | target parallel | * |
2338 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002339 // | ordered | target enter | * |
2340 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002341 // | ordered | target exit | * |
2342 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002343 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002344 // | ordered | cancellation | |
2345 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002346 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002347 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002348 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002349 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002350 // +------------------+-----------------+------------------------------------+
2351 // | atomic | parallel | |
2352 // | atomic | for | |
2353 // | atomic | for simd | |
2354 // | atomic | master | |
2355 // | atomic | critical | |
2356 // | atomic | simd | |
2357 // | atomic | sections | |
2358 // | atomic | section | |
2359 // | atomic | single | |
2360 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002361 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002362 // | atomic |parallel sections| |
2363 // | atomic | task | |
2364 // | atomic | taskyield | |
2365 // | atomic | barrier | |
2366 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002367 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002368 // | atomic | flush | |
2369 // | atomic | ordered | |
2370 // | atomic | atomic | |
2371 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002372 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002373 // | atomic | target parallel | |
2374 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002375 // | atomic | target enter | |
2376 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002377 // | atomic | target exit | |
2378 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002379 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002380 // | atomic | cancellation | |
2381 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002382 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002383 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002384 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002385 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002386 // +------------------+-----------------+------------------------------------+
2387 // | target | parallel | * |
2388 // | target | for | * |
2389 // | target | for simd | * |
2390 // | target | master | * |
2391 // | target | critical | * |
2392 // | target | simd | * |
2393 // | target | sections | * |
2394 // | target | section | * |
2395 // | target | single | * |
2396 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002397 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002398 // | target |parallel sections| * |
2399 // | target | task | * |
2400 // | target | taskyield | * |
2401 // | target | barrier | * |
2402 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002403 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002404 // | target | flush | * |
2405 // | target | ordered | * |
2406 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002407 // | target | target | |
2408 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002409 // | target | target parallel | |
2410 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002411 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002412 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002413 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002414 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002415 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002416 // | target | cancellation | |
2417 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002418 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002419 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002420 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002421 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002422 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002423 // | target parallel | parallel | * |
2424 // | target parallel | for | * |
2425 // | target parallel | for simd | * |
2426 // | target parallel | master | * |
2427 // | target parallel | critical | * |
2428 // | target parallel | simd | * |
2429 // | target parallel | sections | * |
2430 // | target parallel | section | * |
2431 // | target parallel | single | * |
2432 // | target parallel | parallel for | * |
2433 // | target parallel |parallel for simd| * |
2434 // | target parallel |parallel sections| * |
2435 // | target parallel | task | * |
2436 // | target parallel | taskyield | * |
2437 // | target parallel | barrier | * |
2438 // | target parallel | taskwait | * |
2439 // | target parallel | taskgroup | * |
2440 // | target parallel | flush | * |
2441 // | target parallel | ordered | * |
2442 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002443 // | target parallel | target | |
2444 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002445 // | target parallel | target parallel | |
2446 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002447 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002448 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002449 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002450 // | | data | |
2451 // | target parallel | teams | |
2452 // | target parallel | cancellation | |
2453 // | | point | ! |
2454 // | target parallel | cancel | ! |
2455 // | target parallel | taskloop | * |
2456 // | target parallel | taskloop simd | * |
2457 // | target parallel | distribute | |
2458 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002459 // | target parallel | parallel | * |
2460 // | for | | |
2461 // | target parallel | for | * |
2462 // | for | | |
2463 // | target parallel | for simd | * |
2464 // | for | | |
2465 // | target parallel | master | * |
2466 // | for | | |
2467 // | target parallel | critical | * |
2468 // | for | | |
2469 // | target parallel | simd | * |
2470 // | for | | |
2471 // | target parallel | sections | * |
2472 // | for | | |
2473 // | target parallel | section | * |
2474 // | for | | |
2475 // | target parallel | single | * |
2476 // | for | | |
2477 // | target parallel | parallel for | * |
2478 // | for | | |
2479 // | target parallel |parallel for simd| * |
2480 // | for | | |
2481 // | target parallel |parallel sections| * |
2482 // | for | | |
2483 // | target parallel | task | * |
2484 // | for | | |
2485 // | target parallel | taskyield | * |
2486 // | for | | |
2487 // | target parallel | barrier | * |
2488 // | for | | |
2489 // | target parallel | taskwait | * |
2490 // | for | | |
2491 // | target parallel | taskgroup | * |
2492 // | for | | |
2493 // | target parallel | flush | * |
2494 // | for | | |
2495 // | target parallel | ordered | * |
2496 // | for | | |
2497 // | target parallel | atomic | * |
2498 // | for | | |
2499 // | target parallel | target | |
2500 // | for | | |
2501 // | target parallel | target parallel | |
2502 // | for | | |
2503 // | target parallel | target parallel | |
2504 // | for | for | |
2505 // | target parallel | target enter | |
2506 // | for | data | |
2507 // | target parallel | target exit | |
2508 // | for | data | |
2509 // | target parallel | teams | |
2510 // | for | | |
2511 // | target parallel | cancellation | |
2512 // | for | point | ! |
2513 // | target parallel | cancel | ! |
2514 // | for | | |
2515 // | target parallel | taskloop | * |
2516 // | for | | |
2517 // | target parallel | taskloop simd | * |
2518 // | for | | |
2519 // | target parallel | distribute | |
2520 // | for | | |
2521 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002522 // | teams | parallel | * |
2523 // | teams | for | + |
2524 // | teams | for simd | + |
2525 // | teams | master | + |
2526 // | teams | critical | + |
2527 // | teams | simd | + |
2528 // | teams | sections | + |
2529 // | teams | section | + |
2530 // | teams | single | + |
2531 // | teams | parallel for | * |
2532 // | teams |parallel for simd| * |
2533 // | teams |parallel sections| * |
2534 // | teams | task | + |
2535 // | teams | taskyield | + |
2536 // | teams | barrier | + |
2537 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002538 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002539 // | teams | flush | + |
2540 // | teams | ordered | + |
2541 // | teams | atomic | + |
2542 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002543 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002544 // | teams | target parallel | + |
2545 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002546 // | teams | target enter | + |
2547 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002548 // | teams | target exit | + |
2549 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002550 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002551 // | teams | cancellation | |
2552 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002553 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002554 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002555 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002556 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002557 // +------------------+-----------------+------------------------------------+
2558 // | taskloop | parallel | * |
2559 // | taskloop | for | + |
2560 // | taskloop | for simd | + |
2561 // | taskloop | master | + |
2562 // | taskloop | critical | * |
2563 // | taskloop | simd | * |
2564 // | taskloop | sections | + |
2565 // | taskloop | section | + |
2566 // | taskloop | single | + |
2567 // | taskloop | parallel for | * |
2568 // | taskloop |parallel for simd| * |
2569 // | taskloop |parallel sections| * |
2570 // | taskloop | task | * |
2571 // | taskloop | taskyield | * |
2572 // | taskloop | barrier | + |
2573 // | taskloop | taskwait | * |
2574 // | taskloop | taskgroup | * |
2575 // | taskloop | flush | * |
2576 // | taskloop | ordered | + |
2577 // | taskloop | atomic | * |
2578 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002579 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002580 // | taskloop | target parallel | * |
2581 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002582 // | taskloop | target enter | * |
2583 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002584 // | taskloop | target exit | * |
2585 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002586 // | taskloop | teams | + |
2587 // | taskloop | cancellation | |
2588 // | | point | |
2589 // | taskloop | cancel | |
2590 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002591 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002592 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002593 // | taskloop simd | parallel | |
2594 // | taskloop simd | for | |
2595 // | taskloop simd | for simd | |
2596 // | taskloop simd | master | |
2597 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002598 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002599 // | taskloop simd | sections | |
2600 // | taskloop simd | section | |
2601 // | taskloop simd | single | |
2602 // | taskloop simd | parallel for | |
2603 // | taskloop simd |parallel for simd| |
2604 // | taskloop simd |parallel sections| |
2605 // | taskloop simd | task | |
2606 // | taskloop simd | taskyield | |
2607 // | taskloop simd | barrier | |
2608 // | taskloop simd | taskwait | |
2609 // | taskloop simd | taskgroup | |
2610 // | taskloop simd | flush | |
2611 // | taskloop simd | ordered | + (with simd clause) |
2612 // | taskloop simd | atomic | |
2613 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002614 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002615 // | taskloop simd | target parallel | |
2616 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002617 // | taskloop simd | target enter | |
2618 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002619 // | taskloop simd | target exit | |
2620 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002621 // | taskloop simd | teams | |
2622 // | taskloop simd | cancellation | |
2623 // | | point | |
2624 // | taskloop simd | cancel | |
2625 // | taskloop simd | taskloop | |
2626 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002627 // | taskloop simd | distribute | |
2628 // +------------------+-----------------+------------------------------------+
2629 // | distribute | parallel | * |
2630 // | distribute | for | * |
2631 // | distribute | for simd | * |
2632 // | distribute | master | * |
2633 // | distribute | critical | * |
2634 // | distribute | simd | * |
2635 // | distribute | sections | * |
2636 // | distribute | section | * |
2637 // | distribute | single | * |
2638 // | distribute | parallel for | * |
2639 // | distribute |parallel for simd| * |
2640 // | distribute |parallel sections| * |
2641 // | distribute | task | * |
2642 // | distribute | taskyield | * |
2643 // | distribute | barrier | * |
2644 // | distribute | taskwait | * |
2645 // | distribute | taskgroup | * |
2646 // | distribute | flush | * |
2647 // | distribute | ordered | + |
2648 // | distribute | atomic | * |
2649 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002650 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002651 // | distribute | target parallel | |
2652 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002653 // | distribute | target enter | |
2654 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002655 // | distribute | target exit | |
2656 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002657 // | distribute | teams | |
2658 // | distribute | cancellation | + |
2659 // | | point | |
2660 // | distribute | cancel | + |
2661 // | distribute | taskloop | * |
2662 // | distribute | taskloop simd | * |
2663 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002664 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002665 if (Stack->getCurScope()) {
2666 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002667 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002668 bool NestingProhibited = false;
2669 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002670 enum {
2671 NoRecommend,
2672 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002673 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002674 ShouldBeInTargetRegion,
2675 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002676 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002677 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2678 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002679 // OpenMP [2.16, Nesting of Regions]
2680 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002681 // OpenMP [2.8.1,simd Construct, Restrictions]
2682 // An ordered construct with the simd clause is the only OpenMP construct
2683 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002684 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2685 return true;
2686 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002687 if (ParentRegion == OMPD_atomic) {
2688 // OpenMP [2.16, Nesting of Regions]
2689 // OpenMP constructs may not be nested inside an atomic region.
2690 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2691 return true;
2692 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002693 if (CurrentRegion == OMPD_section) {
2694 // OpenMP [2.7.2, sections Construct, Restrictions]
2695 // Orphaned section directives are prohibited. That is, the section
2696 // directives must appear within the sections construct and must not be
2697 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002698 if (ParentRegion != OMPD_sections &&
2699 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002700 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2701 << (ParentRegion != OMPD_unknown)
2702 << getOpenMPDirectiveName(ParentRegion);
2703 return true;
2704 }
2705 return false;
2706 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002707 // Allow some constructs to be orphaned (they could be used in functions,
2708 // called from OpenMP regions with the required preconditions).
2709 if (ParentRegion == OMPD_unknown)
2710 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002711 if (CurrentRegion == OMPD_cancellation_point ||
2712 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002713 // OpenMP [2.16, Nesting of Regions]
2714 // A cancellation point construct for which construct-type-clause is
2715 // taskgroup must be nested inside a task construct. A cancellation
2716 // point construct for which construct-type-clause is not taskgroup must
2717 // be closely nested inside an OpenMP construct that matches the type
2718 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002719 // A cancel construct for which construct-type-clause is taskgroup must be
2720 // nested inside a task construct. A cancel construct for which
2721 // construct-type-clause is not taskgroup must be closely nested inside an
2722 // OpenMP construct that matches the type specified in
2723 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002724 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002725 !((CancelRegion == OMPD_parallel &&
2726 (ParentRegion == OMPD_parallel ||
2727 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002728 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002729 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2730 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002731 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2732 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002733 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2734 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002735 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002736 // OpenMP [2.16, Nesting of Regions]
2737 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002738 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002739 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002740 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002741 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002742 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2743 // OpenMP [2.16, Nesting of Regions]
2744 // A critical region may not be nested (closely or otherwise) inside a
2745 // critical region with the same name. Note that this restriction is not
2746 // sufficient to prevent deadlock.
2747 SourceLocation PreviousCriticalLoc;
2748 bool DeadLock =
2749 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2750 OpenMPDirectiveKind K,
2751 const DeclarationNameInfo &DNI,
2752 SourceLocation Loc)
2753 ->bool {
2754 if (K == OMPD_critical &&
2755 DNI.getName() == CurrentName.getName()) {
2756 PreviousCriticalLoc = Loc;
2757 return true;
2758 } else
2759 return false;
2760 },
2761 false /* skip top directive */);
2762 if (DeadLock) {
2763 SemaRef.Diag(StartLoc,
2764 diag::err_omp_prohibited_region_critical_same_name)
2765 << CurrentName.getName();
2766 if (PreviousCriticalLoc.isValid())
2767 SemaRef.Diag(PreviousCriticalLoc,
2768 diag::note_omp_previous_critical_region);
2769 return true;
2770 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002771 } else if (CurrentRegion == OMPD_barrier) {
2772 // OpenMP [2.16, Nesting of Regions]
2773 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002774 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002775 NestingProhibited =
2776 isOpenMPWorksharingDirective(ParentRegion) ||
2777 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002778 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002779 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002780 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002781 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002782 // OpenMP [2.16, Nesting of Regions]
2783 // A worksharing region may not be closely nested inside a worksharing,
2784 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002785 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002786 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002787 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002788 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002789 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002790 Recommend = ShouldBeInParallelRegion;
2791 } else if (CurrentRegion == OMPD_ordered) {
2792 // OpenMP [2.16, Nesting of Regions]
2793 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002794 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002795 // An ordered region must be closely nested inside a loop region (or
2796 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002797 // OpenMP [2.8.1,simd Construct, Restrictions]
2798 // An ordered construct with the simd clause is the only OpenMP construct
2799 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002800 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002801 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002802 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002803 !(isOpenMPSimdDirective(ParentRegion) ||
2804 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002805 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002806 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2807 // OpenMP [2.16, Nesting of Regions]
2808 // If specified, a teams construct must be contained within a target
2809 // construct.
2810 NestingProhibited = ParentRegion != OMPD_target;
2811 Recommend = ShouldBeInTargetRegion;
2812 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2813 }
2814 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2815 // OpenMP [2.16, Nesting of Regions]
2816 // distribute, parallel, parallel sections, parallel workshare, and the
2817 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2818 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002819 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2820 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002821 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002822 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002823 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2824 // OpenMP 4.5 [2.17 Nesting of Regions]
2825 // The region associated with the distribute construct must be strictly
2826 // nested inside a teams region
2827 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2828 Recommend = ShouldBeInTeamsRegion;
2829 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002830 if (!NestingProhibited &&
2831 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2832 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2833 // OpenMP 4.5 [2.17 Nesting of Regions]
2834 // If a target, target update, target data, target enter data, or
2835 // target exit data construct is encountered during execution of a
2836 // target region, the behavior is unspecified.
2837 NestingProhibited = Stack->hasDirective(
2838 [&OffendingRegion](OpenMPDirectiveKind K,
2839 const DeclarationNameInfo &DNI,
2840 SourceLocation Loc) -> bool {
2841 if (isOpenMPTargetExecutionDirective(K)) {
2842 OffendingRegion = K;
2843 return true;
2844 } else
2845 return false;
2846 },
2847 false /* don't skip top directive */);
2848 CloseNesting = false;
2849 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002850 if (NestingProhibited) {
2851 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002852 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2853 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002854 return true;
2855 }
2856 }
2857 return false;
2858}
2859
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002860static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2861 ArrayRef<OMPClause *> Clauses,
2862 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2863 bool ErrorFound = false;
2864 unsigned NamedModifiersNumber = 0;
2865 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2866 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002867 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002868 for (const auto *C : Clauses) {
2869 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2870 // At most one if clause without a directive-name-modifier can appear on
2871 // the directive.
2872 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2873 if (FoundNameModifiers[CurNM]) {
2874 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2875 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2876 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2877 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002878 } else if (CurNM != OMPD_unknown) {
2879 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002880 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002881 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002882 FoundNameModifiers[CurNM] = IC;
2883 if (CurNM == OMPD_unknown)
2884 continue;
2885 // Check if the specified name modifier is allowed for the current
2886 // directive.
2887 // At most one if clause with the particular directive-name-modifier can
2888 // appear on the directive.
2889 bool MatchFound = false;
2890 for (auto NM : AllowedNameModifiers) {
2891 if (CurNM == NM) {
2892 MatchFound = true;
2893 break;
2894 }
2895 }
2896 if (!MatchFound) {
2897 S.Diag(IC->getNameModifierLoc(),
2898 diag::err_omp_wrong_if_directive_name_modifier)
2899 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2900 ErrorFound = true;
2901 }
2902 }
2903 }
2904 // If any if clause on the directive includes a directive-name-modifier then
2905 // all if clauses on the directive must include a directive-name-modifier.
2906 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2907 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2908 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2909 diag::err_omp_no_more_if_clause);
2910 } else {
2911 std::string Values;
2912 std::string Sep(", ");
2913 unsigned AllowedCnt = 0;
2914 unsigned TotalAllowedNum =
2915 AllowedNameModifiers.size() - NamedModifiersNumber;
2916 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2917 ++Cnt) {
2918 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2919 if (!FoundNameModifiers[NM]) {
2920 Values += "'";
2921 Values += getOpenMPDirectiveName(NM);
2922 Values += "'";
2923 if (AllowedCnt + 2 == TotalAllowedNum)
2924 Values += " or ";
2925 else if (AllowedCnt + 1 != TotalAllowedNum)
2926 Values += Sep;
2927 ++AllowedCnt;
2928 }
2929 }
2930 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2931 diag::err_omp_unnamed_if_clause)
2932 << (TotalAllowedNum > 1) << Values;
2933 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002934 for (auto Loc : NameModifierLoc) {
2935 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2936 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002937 ErrorFound = true;
2938 }
2939 return ErrorFound;
2940}
2941
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002942StmtResult Sema::ActOnOpenMPExecutableDirective(
2943 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2944 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2945 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002946 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002947 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2948 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002949 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002950
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002951 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002952 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002953 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002954 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002955 if (AStmt) {
2956 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2957
2958 // Check default data sharing attributes for referenced variables.
2959 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2960 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2961 if (DSAChecker.isErrorFound())
2962 return StmtError();
2963 // Generate list of implicitly defined firstprivate variables.
2964 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002965
2966 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2967 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2968 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2969 SourceLocation(), SourceLocation())) {
2970 ClausesWithImplicit.push_back(Implicit);
2971 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2972 DSAChecker.getImplicitFirstprivate().size();
2973 } else
2974 ErrorFound = true;
2975 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002976 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002977
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002978 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002979 switch (Kind) {
2980 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002981 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2982 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002983 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002984 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002985 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002986 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2987 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002988 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002989 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002990 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2991 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002992 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002993 case OMPD_for_simd:
2994 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2995 EndLoc, VarsWithInheritedDSA);
2996 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002997 case OMPD_sections:
2998 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2999 EndLoc);
3000 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003001 case OMPD_section:
3002 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003003 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003004 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3005 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003006 case OMPD_single:
3007 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3008 EndLoc);
3009 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003010 case OMPD_master:
3011 assert(ClausesWithImplicit.empty() &&
3012 "No clauses are allowed for 'omp master' directive");
3013 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3014 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003015 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003016 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3017 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003018 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003019 case OMPD_parallel_for:
3020 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3021 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003022 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003023 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003024 case OMPD_parallel_for_simd:
3025 Res = ActOnOpenMPParallelForSimdDirective(
3026 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003027 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003028 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003029 case OMPD_parallel_sections:
3030 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3031 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003032 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003033 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003034 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003035 Res =
3036 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003037 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003038 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003039 case OMPD_taskyield:
3040 assert(ClausesWithImplicit.empty() &&
3041 "No clauses are allowed for 'omp taskyield' directive");
3042 assert(AStmt == nullptr &&
3043 "No associated statement allowed for 'omp taskyield' directive");
3044 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3045 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003046 case OMPD_barrier:
3047 assert(ClausesWithImplicit.empty() &&
3048 "No clauses are allowed for 'omp barrier' directive");
3049 assert(AStmt == nullptr &&
3050 "No associated statement allowed for 'omp barrier' directive");
3051 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3052 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003053 case OMPD_taskwait:
3054 assert(ClausesWithImplicit.empty() &&
3055 "No clauses are allowed for 'omp taskwait' directive");
3056 assert(AStmt == nullptr &&
3057 "No associated statement allowed for 'omp taskwait' directive");
3058 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3059 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003060 case OMPD_taskgroup:
3061 assert(ClausesWithImplicit.empty() &&
3062 "No clauses are allowed for 'omp taskgroup' directive");
3063 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3064 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003065 case OMPD_flush:
3066 assert(AStmt == nullptr &&
3067 "No associated statement allowed for 'omp flush' directive");
3068 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3069 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003070 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003071 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3072 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003073 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003074 case OMPD_atomic:
3075 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3076 EndLoc);
3077 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003078 case OMPD_teams:
3079 Res =
3080 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3081 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003082 case OMPD_target:
3083 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3084 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003085 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003086 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003087 case OMPD_target_parallel:
3088 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3089 StartLoc, EndLoc);
3090 AllowedNameModifiers.push_back(OMPD_target);
3091 AllowedNameModifiers.push_back(OMPD_parallel);
3092 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003093 case OMPD_target_parallel_for:
3094 Res = ActOnOpenMPTargetParallelForDirective(
3095 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3096 AllowedNameModifiers.push_back(OMPD_target);
3097 AllowedNameModifiers.push_back(OMPD_parallel);
3098 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003099 case OMPD_cancellation_point:
3100 assert(ClausesWithImplicit.empty() &&
3101 "No clauses are allowed for 'omp cancellation point' directive");
3102 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3103 "cancellation point' directive");
3104 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3105 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003106 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003107 assert(AStmt == nullptr &&
3108 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003109 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3110 CancelRegion);
3111 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003112 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003113 case OMPD_target_data:
3114 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3115 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003116 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003117 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003118 case OMPD_target_enter_data:
3119 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3120 EndLoc);
3121 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3122 break;
Samuel Antao72590762016-01-19 20:04:50 +00003123 case OMPD_target_exit_data:
3124 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3125 EndLoc);
3126 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3127 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003128 case OMPD_taskloop:
3129 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3130 EndLoc, VarsWithInheritedDSA);
3131 AllowedNameModifiers.push_back(OMPD_taskloop);
3132 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003133 case OMPD_taskloop_simd:
3134 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3135 EndLoc, VarsWithInheritedDSA);
3136 AllowedNameModifiers.push_back(OMPD_taskloop);
3137 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003138 case OMPD_distribute:
3139 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3140 EndLoc, VarsWithInheritedDSA);
3141 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003142 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003143 llvm_unreachable("OpenMP Directive is not allowed");
3144 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003145 llvm_unreachable("Unknown OpenMP directive");
3146 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003147
Alexey Bataev4acb8592014-07-07 13:01:15 +00003148 for (auto P : VarsWithInheritedDSA) {
3149 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3150 << P.first << P.second->getSourceRange();
3151 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003152 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3153
3154 if (!AllowedNameModifiers.empty())
3155 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3156 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003157
Alexey Bataeved09d242014-05-28 05:53:51 +00003158 if (ErrorFound)
3159 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003160 return Res;
3161}
3162
3163StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3164 Stmt *AStmt,
3165 SourceLocation StartLoc,
3166 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003167 if (!AStmt)
3168 return StmtError();
3169
Alexey Bataev9959db52014-05-06 10:08:46 +00003170 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3171 // 1.2.2 OpenMP Language Terminology
3172 // Structured block - An executable statement with a single entry at the
3173 // top and a single exit at the bottom.
3174 // The point of exit cannot be a branch out of the structured block.
3175 // longjmp() and throw() must not violate the entry/exit criteria.
3176 CS->getCapturedDecl()->setNothrow();
3177
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003178 getCurFunction()->setHasBranchProtectedScope();
3179
Alexey Bataev25e5b442015-09-15 12:52:43 +00003180 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3181 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003182}
3183
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003184namespace {
3185/// \brief Helper class for checking canonical form of the OpenMP loops and
3186/// extracting iteration space of each loop in the loop nest, that will be used
3187/// for IR generation.
3188class OpenMPIterationSpaceChecker {
3189 /// \brief Reference to Sema.
3190 Sema &SemaRef;
3191 /// \brief A location for diagnostics (when there is no some better location).
3192 SourceLocation DefaultLoc;
3193 /// \brief A location for diagnostics (when increment is not compatible).
3194 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003195 /// \brief A source location for referring to loop init later.
3196 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003197 /// \brief A source location for referring to condition later.
3198 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003199 /// \brief A source location for referring to increment later.
3200 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003201 /// \brief Loop variable.
3202 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003203 /// \brief Reference to loop variable.
3204 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003205 /// \brief Lower bound (initializer for the var).
3206 Expr *LB;
3207 /// \brief Upper bound.
3208 Expr *UB;
3209 /// \brief Loop step (increment).
3210 Expr *Step;
3211 /// \brief This flag is true when condition is one of:
3212 /// Var < UB
3213 /// Var <= UB
3214 /// UB > Var
3215 /// UB >= Var
3216 bool TestIsLessOp;
3217 /// \brief This flag is true when condition is strict ( < or > ).
3218 bool TestIsStrictOp;
3219 /// \brief This flag is true when step is subtracted on each iteration.
3220 bool SubtractStep;
3221
3222public:
3223 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3224 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003225 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3226 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003227 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3228 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003229 /// \brief Check init-expr for canonical loop form and save loop counter
3230 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003231 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003232 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3233 /// for less/greater and for strict/non-strict comparison.
3234 bool CheckCond(Expr *S);
3235 /// \brief Check incr-expr for canonical loop form and return true if it
3236 /// does not conform, otherwise save loop step (#Step).
3237 bool CheckInc(Expr *S);
3238 /// \brief Return the loop counter variable.
3239 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003240 /// \brief Return the reference expression to loop counter variable.
3241 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003242 /// \brief Source range of the loop init.
3243 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3244 /// \brief Source range of the loop condition.
3245 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3246 /// \brief Source range of the loop increment.
3247 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3248 /// \brief True if the step should be subtracted.
3249 bool ShouldSubtractStep() const { return SubtractStep; }
3250 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003251 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003252 /// \brief Build the precondition expression for the loops.
3253 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003254 /// \brief Build reference expression to the counter be used for codegen.
3255 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003256 /// \brief Build reference expression to the private counter be used for
3257 /// codegen.
3258 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003259 /// \brief Build initization of the counter be used for codegen.
3260 Expr *BuildCounterInit() const;
3261 /// \brief Build step of the counter be used for codegen.
3262 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003263 /// \brief Return true if any expression is dependent.
3264 bool Dependent() const;
3265
3266private:
3267 /// \brief Check the right-hand side of an assignment in the increment
3268 /// expression.
3269 bool CheckIncRHS(Expr *RHS);
3270 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003271 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003272 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003273 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003274 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003275 /// \brief Helper to set loop increment.
3276 bool SetStep(Expr *NewStep, bool Subtract);
3277};
3278
3279bool OpenMPIterationSpaceChecker::Dependent() const {
3280 if (!Var) {
3281 assert(!LB && !UB && !Step);
3282 return false;
3283 }
3284 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3285 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3286}
3287
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003288template <typename T>
3289static T *getExprAsWritten(T *E) {
3290 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3291 E = ExprTemp->getSubExpr();
3292
3293 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3294 E = MTE->GetTemporaryExpr();
3295
3296 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3297 E = Binder->getSubExpr();
3298
3299 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3300 E = ICE->getSubExprAsWritten();
3301 return E->IgnoreParens();
3302}
3303
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003304bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3305 DeclRefExpr *NewVarRefExpr,
3306 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003307 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003308 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3309 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003310 if (!NewVar || !NewLB)
3311 return true;
3312 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003313 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003314 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3315 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003316 if ((Ctor->isCopyOrMoveConstructor() ||
3317 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3318 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003319 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003320 LB = NewLB;
3321 return false;
3322}
3323
3324bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003325 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003326 // State consistency checking to ensure correct usage.
3327 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3328 !TestIsLessOp && !TestIsStrictOp);
3329 if (!NewUB)
3330 return true;
3331 UB = NewUB;
3332 TestIsLessOp = LessOp;
3333 TestIsStrictOp = StrictOp;
3334 ConditionSrcRange = SR;
3335 ConditionLoc = SL;
3336 return false;
3337}
3338
3339bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3340 // State consistency checking to ensure correct usage.
3341 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3342 if (!NewStep)
3343 return true;
3344 if (!NewStep->isValueDependent()) {
3345 // Check that the step is integer expression.
3346 SourceLocation StepLoc = NewStep->getLocStart();
3347 ExprResult Val =
3348 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3349 if (Val.isInvalid())
3350 return true;
3351 NewStep = Val.get();
3352
3353 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3354 // If test-expr is of form var relational-op b and relational-op is < or
3355 // <= then incr-expr must cause var to increase on each iteration of the
3356 // loop. If test-expr is of form var relational-op b and relational-op is
3357 // > or >= then incr-expr must cause var to decrease on each iteration of
3358 // the loop.
3359 // If test-expr is of form b relational-op var and relational-op is < or
3360 // <= then incr-expr must cause var to decrease on each iteration of the
3361 // loop. If test-expr is of form b relational-op var and relational-op is
3362 // > or >= then incr-expr must cause var to increase on each iteration of
3363 // the loop.
3364 llvm::APSInt Result;
3365 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3366 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3367 bool IsConstNeg =
3368 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003369 bool IsConstPos =
3370 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003371 bool IsConstZero = IsConstant && !Result.getBoolValue();
3372 if (UB && (IsConstZero ||
3373 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003374 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003375 SemaRef.Diag(NewStep->getExprLoc(),
3376 diag::err_omp_loop_incr_not_compatible)
3377 << Var << TestIsLessOp << NewStep->getSourceRange();
3378 SemaRef.Diag(ConditionLoc,
3379 diag::note_omp_loop_cond_requres_compatible_incr)
3380 << TestIsLessOp << ConditionSrcRange;
3381 return true;
3382 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003383 if (TestIsLessOp == Subtract) {
3384 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3385 NewStep).get();
3386 Subtract = !Subtract;
3387 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003388 }
3389
3390 Step = NewStep;
3391 SubtractStep = Subtract;
3392 return false;
3393}
3394
Alexey Bataev9c821032015-04-30 04:23:23 +00003395bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003396 // Check init-expr for canonical loop form and save loop counter
3397 // variable - #Var and its initialization value - #LB.
3398 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3399 // var = lb
3400 // integer-type var = lb
3401 // random-access-iterator-type var = lb
3402 // pointer-type var = lb
3403 //
3404 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003405 if (EmitDiags) {
3406 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3407 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003408 return true;
3409 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003410 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003411 if (Expr *E = dyn_cast<Expr>(S))
3412 S = E->IgnoreParens();
3413 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3414 if (BO->getOpcode() == BO_Assign)
3415 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003416 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003417 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003418 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3419 if (DS->isSingleDecl()) {
3420 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003421 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003422 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003423 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003424 SemaRef.Diag(S->getLocStart(),
3425 diag::ext_omp_loop_not_canonical_init)
3426 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003427 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003428 }
3429 }
3430 }
3431 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3432 if (CE->getOperator() == OO_Equal)
3433 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003434 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3435 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003436
Alexey Bataev9c821032015-04-30 04:23:23 +00003437 if (EmitDiags) {
3438 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3439 << S->getSourceRange();
3440 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003441 return true;
3442}
3443
Alexey Bataev23b69422014-06-18 07:08:49 +00003444/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003445/// variable (which may be the loop variable) if possible.
3446static const VarDecl *GetInitVarDecl(const Expr *E) {
3447 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003448 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003449 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003450 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3451 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003452 if ((Ctor->isCopyOrMoveConstructor() ||
3453 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3454 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003455 E = CE->getArg(0)->IgnoreParenImpCasts();
3456 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3457 if (!DRE)
3458 return nullptr;
3459 return dyn_cast<VarDecl>(DRE->getDecl());
3460}
3461
3462bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3463 // Check test-expr for canonical form, save upper-bound UB, flags for
3464 // less/greater and for strict/non-strict comparison.
3465 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3466 // var relational-op b
3467 // b relational-op var
3468 //
3469 if (!S) {
3470 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3471 return true;
3472 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003473 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003474 SourceLocation CondLoc = S->getLocStart();
3475 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3476 if (BO->isRelationalOp()) {
3477 if (GetInitVarDecl(BO->getLHS()) == Var)
3478 return SetUB(BO->getRHS(),
3479 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3480 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3481 BO->getSourceRange(), BO->getOperatorLoc());
3482 if (GetInitVarDecl(BO->getRHS()) == Var)
3483 return SetUB(BO->getLHS(),
3484 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3485 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3486 BO->getSourceRange(), BO->getOperatorLoc());
3487 }
3488 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3489 if (CE->getNumArgs() == 2) {
3490 auto Op = CE->getOperator();
3491 switch (Op) {
3492 case OO_Greater:
3493 case OO_GreaterEqual:
3494 case OO_Less:
3495 case OO_LessEqual:
3496 if (GetInitVarDecl(CE->getArg(0)) == Var)
3497 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3498 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3499 CE->getOperatorLoc());
3500 if (GetInitVarDecl(CE->getArg(1)) == Var)
3501 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3502 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3503 CE->getOperatorLoc());
3504 break;
3505 default:
3506 break;
3507 }
3508 }
3509 }
3510 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3511 << S->getSourceRange() << Var;
3512 return true;
3513}
3514
3515bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3516 // RHS of canonical loop form increment can be:
3517 // var + incr
3518 // incr + var
3519 // var - incr
3520 //
3521 RHS = RHS->IgnoreParenImpCasts();
3522 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3523 if (BO->isAdditiveOp()) {
3524 bool IsAdd = BO->getOpcode() == BO_Add;
3525 if (GetInitVarDecl(BO->getLHS()) == Var)
3526 return SetStep(BO->getRHS(), !IsAdd);
3527 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3528 return SetStep(BO->getLHS(), false);
3529 }
3530 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3531 bool IsAdd = CE->getOperator() == OO_Plus;
3532 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3533 if (GetInitVarDecl(CE->getArg(0)) == Var)
3534 return SetStep(CE->getArg(1), !IsAdd);
3535 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3536 return SetStep(CE->getArg(0), false);
3537 }
3538 }
3539 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3540 << RHS->getSourceRange() << Var;
3541 return true;
3542}
3543
3544bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3545 // Check incr-expr for canonical loop form and return true if it
3546 // does not conform.
3547 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3548 // ++var
3549 // var++
3550 // --var
3551 // var--
3552 // var += incr
3553 // var -= incr
3554 // var = var + incr
3555 // var = incr + var
3556 // var = var - incr
3557 //
3558 if (!S) {
3559 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3560 return true;
3561 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003562 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003563 S = S->IgnoreParens();
3564 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3565 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3566 return SetStep(
3567 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3568 (UO->isDecrementOp() ? -1 : 1)).get(),
3569 false);
3570 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3571 switch (BO->getOpcode()) {
3572 case BO_AddAssign:
3573 case BO_SubAssign:
3574 if (GetInitVarDecl(BO->getLHS()) == Var)
3575 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3576 break;
3577 case BO_Assign:
3578 if (GetInitVarDecl(BO->getLHS()) == Var)
3579 return CheckIncRHS(BO->getRHS());
3580 break;
3581 default:
3582 break;
3583 }
3584 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3585 switch (CE->getOperator()) {
3586 case OO_PlusPlus:
3587 case OO_MinusMinus:
3588 if (GetInitVarDecl(CE->getArg(0)) == Var)
3589 return SetStep(
3590 SemaRef.ActOnIntegerConstant(
3591 CE->getLocStart(),
3592 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3593 false);
3594 break;
3595 case OO_PlusEqual:
3596 case OO_MinusEqual:
3597 if (GetInitVarDecl(CE->getArg(0)) == Var)
3598 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3599 break;
3600 case OO_Equal:
3601 if (GetInitVarDecl(CE->getArg(0)) == Var)
3602 return CheckIncRHS(CE->getArg(1));
3603 break;
3604 default:
3605 break;
3606 }
3607 }
3608 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3609 << S->getSourceRange() << Var;
3610 return true;
3611}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003612
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003613namespace {
3614// Transform variables declared in GNU statement expressions to new ones to
3615// avoid crash on codegen.
3616class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3617 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3618
3619public:
3620 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3621
3622 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3623 if (auto *VD = cast<VarDecl>(D))
3624 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3625 !isa<ImplicitParamDecl>(D)) {
3626 auto *NewVD = VarDecl::Create(
3627 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3628 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3629 VD->getTypeSourceInfo(), VD->getStorageClass());
3630 NewVD->setTSCSpec(VD->getTSCSpec());
3631 NewVD->setInit(VD->getInit());
3632 NewVD->setInitStyle(VD->getInitStyle());
3633 NewVD->setExceptionVariable(VD->isExceptionVariable());
3634 NewVD->setNRVOVariable(VD->isNRVOVariable());
Alexey Bataev11481f52016-02-17 10:29:05 +00003635 NewVD->setCXXForRangeDecl(VD->isCXXForRangeDecl());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003636 NewVD->setConstexpr(VD->isConstexpr());
3637 NewVD->setInitCapture(VD->isInitCapture());
3638 NewVD->setPreviousDeclInSameBlockScope(
3639 VD->isPreviousDeclInSameBlockScope());
3640 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003641 if (VD->hasAttrs())
3642 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003643 transformedLocalDecl(VD, NewVD);
3644 return NewVD;
3645 }
3646 return BaseTransform::TransformDefinition(Loc, D);
3647 }
3648
3649 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3650 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3651 if (E->getDecl() != NewD) {
3652 NewD->setReferenced();
3653 NewD->markUsed(SemaRef.Context);
3654 return DeclRefExpr::Create(
3655 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3656 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3657 E->getNameInfo(), E->getType(), E->getValueKind());
3658 }
3659 return BaseTransform::TransformDeclRefExpr(E);
3660 }
3661};
3662}
3663
Alexander Musmana5f070a2014-10-01 06:03:56 +00003664/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003665Expr *
3666OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3667 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003668 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003669 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003670 auto VarType = Var->getType().getNonReferenceType();
3671 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003672 SemaRef.getLangOpts().CPlusPlus) {
3673 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003674 auto *UBExpr = TestIsLessOp ? UB : LB;
3675 auto *LBExpr = TestIsLessOp ? LB : UB;
3676 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3677 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3678 if (!Upper || !Lower)
3679 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003680 if (!SemaRef.Context.hasSameType(Upper->getType(), UBExpr->getType())) {
3681 Upper = SemaRef
3682 .PerformImplicitConversion(Upper, UBExpr->getType(),
3683 Sema::AA_Converting,
3684 /*AllowExplicit=*/true)
3685 .get();
3686 }
3687 if (!SemaRef.Context.hasSameType(Lower->getType(), LBExpr->getType())) {
3688 Lower = SemaRef
3689 .PerformImplicitConversion(Lower, LBExpr->getType(),
3690 Sema::AA_Converting,
3691 /*AllowExplicit=*/true)
3692 .get();
3693 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003694 if (!Upper || !Lower)
3695 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003696
3697 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3698
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003699 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003700 // BuildBinOp already emitted error, this one is to point user to upper
3701 // and lower bound, and to tell what is passed to 'operator-'.
3702 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3703 << Upper->getSourceRange() << Lower->getSourceRange();
3704 return nullptr;
3705 }
3706 }
3707
3708 if (!Diff.isUsable())
3709 return nullptr;
3710
3711 // Upper - Lower [- 1]
3712 if (TestIsStrictOp)
3713 Diff = SemaRef.BuildBinOp(
3714 S, DefaultLoc, BO_Sub, Diff.get(),
3715 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3716 if (!Diff.isUsable())
3717 return nullptr;
3718
3719 // Upper - Lower [- 1] + Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003720 auto *StepNoImp = Step->IgnoreImplicit();
3721 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003722 if (NewStep.isInvalid())
3723 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003724 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3725 StepNoImp->getType())) {
3726 NewStep = SemaRef.PerformImplicitConversion(
3727 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3728 /*AllowExplicit=*/true);
3729 if (NewStep.isInvalid())
3730 return nullptr;
3731 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003732 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003733 if (!Diff.isUsable())
3734 return nullptr;
3735
3736 // Parentheses (for dumping/debugging purposes only).
3737 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3738 if (!Diff.isUsable())
3739 return nullptr;
3740
3741 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003742 NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003743 if (NewStep.isInvalid())
3744 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003745 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3746 StepNoImp->getType())) {
3747 NewStep = SemaRef.PerformImplicitConversion(
3748 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3749 /*AllowExplicit=*/true);
3750 if (NewStep.isInvalid())
3751 return nullptr;
3752 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003753 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003754 if (!Diff.isUsable())
3755 return nullptr;
3756
Alexander Musman174b3ca2014-10-06 11:16:29 +00003757 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003758 QualType Type = Diff.get()->getType();
3759 auto &C = SemaRef.Context;
3760 bool UseVarType = VarType->hasIntegerRepresentation() &&
3761 C.getTypeSize(Type) > C.getTypeSize(VarType);
3762 if (!Type->isIntegerType() || UseVarType) {
3763 unsigned NewSize =
3764 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3765 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3766 : Type->hasSignedIntegerRepresentation();
3767 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003768 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3769 Diff = SemaRef.PerformImplicitConversion(
3770 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3771 if (!Diff.isUsable())
3772 return nullptr;
3773 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003774 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003775 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003776 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3777 if (NewSize != C.getTypeSize(Type)) {
3778 if (NewSize < C.getTypeSize(Type)) {
3779 assert(NewSize == 64 && "incorrect loop var size");
3780 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3781 << InitSrcRange << ConditionSrcRange;
3782 }
3783 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003784 NewSize, Type->hasSignedIntegerRepresentation() ||
3785 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003786 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3787 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3788 Sema::AA_Converting, true);
3789 if (!Diff.isUsable())
3790 return nullptr;
3791 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003792 }
3793 }
3794
Alexander Musmana5f070a2014-10-01 06:03:56 +00003795 return Diff.get();
3796}
3797
Alexey Bataev62dbb972015-04-22 11:59:37 +00003798Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3799 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3800 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3801 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003802 TransformToNewDefs Transform(SemaRef);
3803
3804 auto NewLB = Transform.TransformExpr(LB);
3805 auto NewUB = Transform.TransformExpr(UB);
3806 if (NewLB.isInvalid() || NewUB.isInvalid())
3807 return Cond;
Alexey Bataev11481f52016-02-17 10:29:05 +00003808 if (!SemaRef.Context.hasSameType(NewLB.get()->getType(), LB->getType())) {
3809 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3810 Sema::AA_Converting,
3811 /*AllowExplicit=*/true);
3812 }
3813 if (!SemaRef.Context.hasSameType(NewUB.get()->getType(), UB->getType())) {
3814 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3815 Sema::AA_Converting,
3816 /*AllowExplicit=*/true);
3817 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003818 if (NewLB.isInvalid() || NewUB.isInvalid())
3819 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003820 auto CondExpr = SemaRef.BuildBinOp(
3821 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3822 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003823 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003824 if (CondExpr.isUsable()) {
Alexey Bataev11481f52016-02-17 10:29:05 +00003825 if (!SemaRef.Context.hasSameType(CondExpr.get()->getType(),
3826 SemaRef.Context.BoolTy))
3827 CondExpr = SemaRef.PerformImplicitConversion(
3828 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3829 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003830 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003831 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3832 // Otherwise use original loop conditon and evaluate it in runtime.
3833 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3834}
3835
Alexander Musmana5f070a2014-10-01 06:03:56 +00003836/// \brief Build reference expression to the counter be used for codegen.
3837Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003838 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3839 DefaultLoc);
3840}
3841
3842Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3843 if (Var && !Var->isInvalidDecl()) {
3844 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003845 auto *PrivateVar =
3846 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3847 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003848 if (PrivateVar->isInvalidDecl())
3849 return nullptr;
3850 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3851 }
3852 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003853}
3854
3855/// \brief Build initization of the counter be used for codegen.
3856Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3857
3858/// \brief Build step of the counter be used for codegen.
3859Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3860
3861/// \brief Iteration space of a single for loop.
3862struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003863 /// \brief Condition of the loop.
3864 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003865 /// \brief This expression calculates the number of iterations in the loop.
3866 /// It is always possible to calculate it before starting the loop.
3867 Expr *NumIterations;
3868 /// \brief The loop counter variable.
3869 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003870 /// \brief Private loop counter variable.
3871 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003872 /// \brief This is initializer for the initial value of #CounterVar.
3873 Expr *CounterInit;
3874 /// \brief This is step for the #CounterVar used to generate its update:
3875 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3876 Expr *CounterStep;
3877 /// \brief Should step be subtracted?
3878 bool Subtract;
3879 /// \brief Source range of the loop init.
3880 SourceRange InitSrcRange;
3881 /// \brief Source range of the loop condition.
3882 SourceRange CondSrcRange;
3883 /// \brief Source range of the loop increment.
3884 SourceRange IncSrcRange;
3885};
3886
Alexey Bataev23b69422014-06-18 07:08:49 +00003887} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003888
Alexey Bataev9c821032015-04-30 04:23:23 +00003889void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3890 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3891 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003892 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3893 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003894 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3895 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003896 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003897 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003898 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003899 }
3900}
3901
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003902/// \brief Called on a for stmt to check and extract its iteration space
3903/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003904static bool CheckOpenMPIterationSpace(
3905 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3906 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003907 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003908 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003909 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 // OpenMP [2.6, Canonical Loop Form]
3911 // for (init-expr; test-expr; incr-expr) structured-block
3912 auto For = dyn_cast_or_null<ForStmt>(S);
3913 if (!For) {
3914 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003915 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3916 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3917 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3918 if (NestedLoopCount > 1) {
3919 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3920 SemaRef.Diag(DSA.getConstructLoc(),
3921 diag::note_omp_collapse_ordered_expr)
3922 << 2 << CollapseLoopCountExpr->getSourceRange()
3923 << OrderedLoopCountExpr->getSourceRange();
3924 else if (CollapseLoopCountExpr)
3925 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3926 diag::note_omp_collapse_ordered_expr)
3927 << 0 << CollapseLoopCountExpr->getSourceRange();
3928 else
3929 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3930 diag::note_omp_collapse_ordered_expr)
3931 << 1 << OrderedLoopCountExpr->getSourceRange();
3932 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003933 return true;
3934 }
3935 assert(For->getBody());
3936
3937 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3938
3939 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003940 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003941 if (ISC.CheckInit(Init)) {
3942 return true;
3943 }
3944
3945 bool HasErrors = false;
3946
3947 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003948 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003949
3950 // OpenMP [2.6, Canonical Loop Form]
3951 // Var is one of the following:
3952 // A variable of signed or unsigned integer type.
3953 // For C++, a variable of a random access iterator type.
3954 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003955 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003956 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3957 !VarType->isPointerType() &&
3958 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3959 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3960 << SemaRef.getLangOpts().CPlusPlus;
3961 HasErrors = true;
3962 }
3963
Alexey Bataev4acb8592014-07-07 13:01:15 +00003964 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3965 // Construct
3966 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3967 // parallel for construct is (are) private.
3968 // The loop iteration variable in the associated for-loop of a simd construct
3969 // with just one associated for-loop is linear with a constant-linear-step
3970 // that is the increment of the associated for-loop.
3971 // Exclude loop var from the list of variables with implicitly defined data
3972 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003973 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003974
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3976 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003977 // The loop iteration variable in the associated for-loop of a simd construct
3978 // with just one associated for-loop may be listed in a linear clause with a
3979 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003980 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3981 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003982 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003983 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3984 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3985 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003986 auto PredeterminedCKind =
3987 isOpenMPSimdDirective(DKind)
3988 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3989 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003990 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003991 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003992 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003993 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003994 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003995 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3996 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003997 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003998 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3999 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00004000 if (DVar.RefExpr == nullptr)
4001 DVar.CKind = PredeterminedCKind;
4002 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004003 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004004 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00004005 // Make the loop iteration variable private (for worksharing constructs),
4006 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00004007 // lastprivate (for simd directives with several collapsed or ordered
4008 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00004009 if (DVar.CKind == OMPC_unknown)
4010 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
4011 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00004012 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004013 }
4014
Alexey Bataev7ff55242014-06-19 09:13:45 +00004015 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00004016
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004017 // Check test-expr.
4018 HasErrors |= ISC.CheckCond(For->getCond());
4019
4020 // Check incr-expr.
4021 HasErrors |= ISC.CheckInc(For->getInc());
4022
Alexander Musmana5f070a2014-10-01 06:03:56 +00004023 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004024 return HasErrors;
4025
Alexander Musmana5f070a2014-10-01 06:03:56 +00004026 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004027 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00004028 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004029 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004030 isOpenMPTaskLoopDirective(DKind) ||
4031 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004032 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00004033 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004034 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4035 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4036 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4037 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4038 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4039 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4040
Alexey Bataev62dbb972015-04-22 11:59:37 +00004041 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4042 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004043 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004044 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004045 ResultIterSpace.CounterInit == nullptr ||
4046 ResultIterSpace.CounterStep == nullptr);
4047
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004048 return HasErrors;
4049}
4050
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004051/// \brief Build 'VarRef = Start.
4052static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4053 ExprResult VarRef, ExprResult Start) {
4054 TransformToNewDefs Transform(SemaRef);
4055 // Build 'VarRef = Start.
Alexey Bataev11481f52016-02-17 10:29:05 +00004056 auto *StartNoImp = Start.get()->IgnoreImplicit();
4057 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004058 if (NewStart.isInvalid())
4059 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004060 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4061 StartNoImp->getType())) {
4062 NewStart = SemaRef.PerformImplicitConversion(
4063 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4064 /*AllowExplicit=*/true);
4065 if (NewStart.isInvalid())
4066 return ExprError();
4067 }
4068 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4069 VarRef.get()->getType())) {
4070 NewStart = SemaRef.PerformImplicitConversion(
4071 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4072 /*AllowExplicit=*/true);
4073 if (!NewStart.isUsable())
4074 return ExprError();
4075 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004076
4077 auto Init =
4078 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4079 return Init;
4080}
4081
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082/// \brief Build 'VarRef = Start + Iter * Step'.
4083static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4084 SourceLocation Loc, ExprResult VarRef,
4085 ExprResult Start, ExprResult Iter,
4086 ExprResult Step, bool Subtract) {
4087 // Add parentheses (for debugging purposes only).
4088 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4089 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4090 !Step.isUsable())
4091 return ExprError();
4092
Alexey Bataev11481f52016-02-17 10:29:05 +00004093 auto *StepNoImp = Step.get()->IgnoreImplicit();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004094 TransformToNewDefs Transform(SemaRef);
Alexey Bataev11481f52016-02-17 10:29:05 +00004095 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004096 if (NewStep.isInvalid())
4097 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004098 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
4099 StepNoImp->getType())) {
4100 NewStep = SemaRef.PerformImplicitConversion(
4101 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
4102 /*AllowExplicit=*/true);
4103 if (NewStep.isInvalid())
4104 return ExprError();
4105 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004106 ExprResult Update =
4107 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004108 if (!Update.isUsable())
4109 return ExprError();
4110
Alexey Bataevc0214e02016-02-16 12:13:49 +00004111 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4112 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev11481f52016-02-17 10:29:05 +00004113 auto *StartNoImp = Start.get()->IgnoreImplicit();
4114 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004115 if (NewStart.isInvalid())
4116 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004117 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4118 StartNoImp->getType())) {
4119 NewStart = SemaRef.PerformImplicitConversion(
4120 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4121 /*AllowExplicit=*/true);
4122 if (NewStart.isInvalid())
4123 return ExprError();
4124 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004125
Alexey Bataevc0214e02016-02-16 12:13:49 +00004126 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4127 ExprResult SavedUpdate = Update;
4128 ExprResult UpdateVal;
4129 if (VarRef.get()->getType()->isOverloadableType() ||
4130 NewStart.get()->getType()->isOverloadableType() ||
4131 Update.get()->getType()->isOverloadableType()) {
4132 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4133 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4134 Update =
4135 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4136 if (Update.isUsable()) {
4137 UpdateVal =
4138 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4139 VarRef.get(), SavedUpdate.get());
4140 if (UpdateVal.isUsable()) {
4141 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4142 UpdateVal.get());
4143 }
4144 }
4145 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4146 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004147
Alexey Bataevc0214e02016-02-16 12:13:49 +00004148 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4149 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4150 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4151 NewStart.get(), SavedUpdate.get());
4152 if (!Update.isUsable())
4153 return ExprError();
4154
Alexey Bataev11481f52016-02-17 10:29:05 +00004155 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4156 VarRef.get()->getType())) {
4157 Update = SemaRef.PerformImplicitConversion(
4158 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4159 if (!Update.isUsable())
4160 return ExprError();
4161 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004162
4163 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4164 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004165 return Update;
4166}
4167
4168/// \brief Convert integer expression \a E to make it have at least \a Bits
4169/// bits.
4170static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4171 Sema &SemaRef) {
4172 if (E == nullptr)
4173 return ExprError();
4174 auto &C = SemaRef.Context;
4175 QualType OldType = E->getType();
4176 unsigned HasBits = C.getTypeSize(OldType);
4177 if (HasBits >= Bits)
4178 return ExprResult(E);
4179 // OK to convert to signed, because new type has more bits than old.
4180 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4181 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4182 true);
4183}
4184
4185/// \brief Check if the given expression \a E is a constant integer that fits
4186/// into \a Bits bits.
4187static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4188 if (E == nullptr)
4189 return false;
4190 llvm::APSInt Result;
4191 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4192 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4193 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004194}
4195
4196/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004197/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4198/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004199static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004200CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4201 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4202 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004203 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004204 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004205 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004206 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004207 // Found 'collapse' clause - calculate collapse number.
4208 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004209 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004210 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004211 }
4212 if (OrderedLoopCountExpr) {
4213 // Found 'ordered' clause - calculate collapse number.
4214 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004215 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4216 if (Result.getLimitedValue() < NestedLoopCount) {
4217 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4218 diag::err_omp_wrong_ordered_loop_count)
4219 << OrderedLoopCountExpr->getSourceRange();
4220 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4221 diag::note_collapse_loop_count)
4222 << CollapseLoopCountExpr->getSourceRange();
4223 }
4224 NestedLoopCount = Result.getLimitedValue();
4225 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004226 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227 // This is helper routine for loop directives (e.g., 'for', 'simd',
4228 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004229 SmallVector<LoopIterationSpace, 4> IterSpaces;
4230 IterSpaces.resize(NestedLoopCount);
4231 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004232 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004233 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004234 NestedLoopCount, CollapseLoopCountExpr,
4235 OrderedLoopCountExpr, VarsWithImplicitDSA,
4236 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004237 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004238 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004239 // OpenMP [2.8.1, simd construct, Restrictions]
4240 // All loops associated with the construct must be perfectly nested; that
4241 // is, there must be no intervening code nor any OpenMP directive between
4242 // any two loops.
4243 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004244 }
4245
Alexander Musmana5f070a2014-10-01 06:03:56 +00004246 Built.clear(/* size */ NestedLoopCount);
4247
4248 if (SemaRef.CurContext->isDependentContext())
4249 return NestedLoopCount;
4250
4251 // An example of what is generated for the following code:
4252 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004253 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004254 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004255 // for (k = 0; k < NK; ++k)
4256 // for (j = J0; j < NJ; j+=2) {
4257 // <loop body>
4258 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004259 //
4260 // We generate the code below.
4261 // Note: the loop body may be outlined in CodeGen.
4262 // Note: some counters may be C++ classes, operator- is used to find number of
4263 // iterations and operator+= to calculate counter value.
4264 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4265 // or i64 is currently supported).
4266 //
4267 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4268 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4269 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4270 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4271 // // similar updates for vars in clauses (e.g. 'linear')
4272 // <loop body (using local i and j)>
4273 // }
4274 // i = NI; // assign final values of counters
4275 // j = NJ;
4276 //
4277
4278 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4279 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004280 // Precondition tests if there is at least one iteration (all conditions are
4281 // true).
4282 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004283 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004284 ExprResult LastIteration32 = WidenIterationCount(
4285 32 /* Bits */, SemaRef.PerformImplicitConversion(
4286 N0->IgnoreImpCasts(), N0->getType(),
4287 Sema::AA_Converting, /*AllowExplicit=*/true)
4288 .get(),
4289 SemaRef);
4290 ExprResult LastIteration64 = WidenIterationCount(
4291 64 /* Bits */, SemaRef.PerformImplicitConversion(
4292 N0->IgnoreImpCasts(), N0->getType(),
4293 Sema::AA_Converting, /*AllowExplicit=*/true)
4294 .get(),
4295 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004296
4297 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4298 return NestedLoopCount;
4299
4300 auto &C = SemaRef.Context;
4301 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4302
4303 Scope *CurScope = DSA.getCurScope();
4304 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004305 if (PreCond.isUsable()) {
4306 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4307 PreCond.get(), IterSpaces[Cnt].PreCond);
4308 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004309 auto N = IterSpaces[Cnt].NumIterations;
4310 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4311 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004312 LastIteration32 = SemaRef.BuildBinOp(
4313 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4314 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4315 Sema::AA_Converting,
4316 /*AllowExplicit=*/true)
4317 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004318 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004319 LastIteration64 = SemaRef.BuildBinOp(
4320 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4321 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4322 Sema::AA_Converting,
4323 /*AllowExplicit=*/true)
4324 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004325 }
4326
4327 // Choose either the 32-bit or 64-bit version.
4328 ExprResult LastIteration = LastIteration64;
4329 if (LastIteration32.isUsable() &&
4330 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4331 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4332 FitsInto(
4333 32 /* Bits */,
4334 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4335 LastIteration64.get(), SemaRef)))
4336 LastIteration = LastIteration32;
4337
4338 if (!LastIteration.isUsable())
4339 return 0;
4340
4341 // Save the number of iterations.
4342 ExprResult NumIterations = LastIteration;
4343 {
4344 LastIteration = SemaRef.BuildBinOp(
4345 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4346 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4347 if (!LastIteration.isUsable())
4348 return 0;
4349 }
4350
4351 // Calculate the last iteration number beforehand instead of doing this on
4352 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4353 llvm::APSInt Result;
4354 bool IsConstant =
4355 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4356 ExprResult CalcLastIteration;
4357 if (!IsConstant) {
4358 SourceLocation SaveLoc;
4359 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004360 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004361 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004362 ExprResult SaveRef = buildDeclRefExpr(
4363 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004364 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4365 SaveRef.get(), LastIteration.get());
4366 LastIteration = SaveRef;
4367
4368 // Prepare SaveRef + 1.
4369 NumIterations = SemaRef.BuildBinOp(
4370 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4371 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4372 if (!NumIterations.isUsable())
4373 return 0;
4374 }
4375
4376 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4377
Alexander Musmanc6388682014-12-15 07:07:06 +00004378 QualType VType = LastIteration.get()->getType();
4379 // Build variables passed into runtime, nesessary for worksharing directives.
4380 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004381 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4382 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004383 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004384 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4385 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004386 SemaRef.AddInitializerToDecl(
4387 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4388 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4389
4390 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004391 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4392 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004393 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4394 /*DirectInit*/ false,
4395 /*TypeMayContainAuto*/ false);
4396
4397 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4398 // This will be used to implement clause 'lastprivate'.
4399 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004400 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4401 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004402 SemaRef.AddInitializerToDecl(
4403 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4404 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4405
4406 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004407 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4408 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004409 SemaRef.AddInitializerToDecl(
4410 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4411 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4412
4413 // Build expression: UB = min(UB, LastIteration)
4414 // It is nesessary for CodeGen of directives with static scheduling.
4415 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4416 UB.get(), LastIteration.get());
4417 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4418 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4419 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4420 CondOp.get());
4421 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4422 }
4423
4424 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004425 ExprResult IV;
4426 ExprResult Init;
4427 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004428 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4429 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004430 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004431 isOpenMPTaskLoopDirective(DKind) ||
4432 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004433 ? LB.get()
4434 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4435 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4436 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004437 }
4438
Alexander Musmanc6388682014-12-15 07:07:06 +00004439 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004440 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004441 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004442 (isOpenMPWorksharingDirective(DKind) ||
4443 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004444 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4445 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4446 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004447
4448 // Loop increment (IV = IV + 1)
4449 SourceLocation IncLoc;
4450 ExprResult Inc =
4451 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4452 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4453 if (!Inc.isUsable())
4454 return 0;
4455 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004456 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4457 if (!Inc.isUsable())
4458 return 0;
4459
4460 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4461 // Used for directives with static scheduling.
4462 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004463 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4464 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004465 // LB + ST
4466 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4467 if (!NextLB.isUsable())
4468 return 0;
4469 // LB = LB + ST
4470 NextLB =
4471 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4472 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4473 if (!NextLB.isUsable())
4474 return 0;
4475 // UB + ST
4476 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4477 if (!NextUB.isUsable())
4478 return 0;
4479 // UB = UB + ST
4480 NextUB =
4481 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4482 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4483 if (!NextUB.isUsable())
4484 return 0;
4485 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004486
4487 // Build updates and final values of the loop counters.
4488 bool HasErrors = false;
4489 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004490 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004491 Built.Updates.resize(NestedLoopCount);
4492 Built.Finals.resize(NestedLoopCount);
4493 {
4494 ExprResult Div;
4495 // Go from inner nested loop to outer.
4496 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4497 LoopIterationSpace &IS = IterSpaces[Cnt];
4498 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4499 // Build: Iter = (IV / Div) % IS.NumIters
4500 // where Div is product of previous iterations' IS.NumIters.
4501 ExprResult Iter;
4502 if (Div.isUsable()) {
4503 Iter =
4504 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4505 } else {
4506 Iter = IV;
4507 assert((Cnt == (int)NestedLoopCount - 1) &&
4508 "unusable div expected on first iteration only");
4509 }
4510
4511 if (Cnt != 0 && Iter.isUsable())
4512 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4513 IS.NumIterations);
4514 if (!Iter.isUsable()) {
4515 HasErrors = true;
4516 break;
4517 }
4518
Alexey Bataev39f915b82015-05-08 10:41:21 +00004519 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4520 auto *CounterVar = buildDeclRefExpr(
4521 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4522 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4523 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004524 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4525 IS.CounterInit);
4526 if (!Init.isUsable()) {
4527 HasErrors = true;
4528 break;
4529 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004530 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004531 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004532 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4533 if (!Update.isUsable()) {
4534 HasErrors = true;
4535 break;
4536 }
4537
4538 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4539 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004540 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004541 IS.NumIterations, IS.CounterStep, IS.Subtract);
4542 if (!Final.isUsable()) {
4543 HasErrors = true;
4544 break;
4545 }
4546
4547 // Build Div for the next iteration: Div <- Div * IS.NumIters
4548 if (Cnt != 0) {
4549 if (Div.isUnset())
4550 Div = IS.NumIterations;
4551 else
4552 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4553 IS.NumIterations);
4554
4555 // Add parentheses (for debugging purposes only).
4556 if (Div.isUsable())
4557 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4558 if (!Div.isUsable()) {
4559 HasErrors = true;
4560 break;
4561 }
4562 }
4563 if (!Update.isUsable() || !Final.isUsable()) {
4564 HasErrors = true;
4565 break;
4566 }
4567 // Save results
4568 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004569 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004570 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004571 Built.Updates[Cnt] = Update.get();
4572 Built.Finals[Cnt] = Final.get();
4573 }
4574 }
4575
4576 if (HasErrors)
4577 return 0;
4578
4579 // Save results
4580 Built.IterationVarRef = IV.get();
4581 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004582 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004583 Built.CalcLastIteration =
4584 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004585 Built.PreCond = PreCond.get();
4586 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004587 Built.Init = Init.get();
4588 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004589 Built.LB = LB.get();
4590 Built.UB = UB.get();
4591 Built.IL = IL.get();
4592 Built.ST = ST.get();
4593 Built.EUB = EUB.get();
4594 Built.NLB = NextLB.get();
4595 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004596
Alexey Bataevabfc0692014-06-25 06:52:00 +00004597 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004598}
4599
Alexey Bataev10e775f2015-07-30 11:36:16 +00004600static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004601 auto CollapseClauses =
4602 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4603 if (CollapseClauses.begin() != CollapseClauses.end())
4604 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004605 return nullptr;
4606}
4607
Alexey Bataev10e775f2015-07-30 11:36:16 +00004608static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004609 auto OrderedClauses =
4610 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4611 if (OrderedClauses.begin() != OrderedClauses.end())
4612 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004613 return nullptr;
4614}
4615
Alexey Bataev66b15b52015-08-21 11:14:16 +00004616static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4617 const Expr *Safelen) {
4618 llvm::APSInt SimdlenRes, SafelenRes;
4619 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4620 Simdlen->isInstantiationDependent() ||
4621 Simdlen->containsUnexpandedParameterPack())
4622 return false;
4623 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4624 Safelen->isInstantiationDependent() ||
4625 Safelen->containsUnexpandedParameterPack())
4626 return false;
4627 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4628 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4629 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4630 // If both simdlen and safelen clauses are specified, the value of the simdlen
4631 // parameter must be less than or equal to the value of the safelen parameter.
4632 if (SimdlenRes > SafelenRes) {
4633 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4634 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4635 return true;
4636 }
4637 return false;
4638}
4639
Alexey Bataev4acb8592014-07-07 13:01:15 +00004640StmtResult Sema::ActOnOpenMPSimdDirective(
4641 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4642 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004643 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004644 if (!AStmt)
4645 return StmtError();
4646
4647 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004648 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004649 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4650 // define the nested loops number.
4651 unsigned NestedLoopCount = CheckOpenMPLoop(
4652 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4653 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004654 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004655 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004656
Alexander Musmana5f070a2014-10-01 06:03:56 +00004657 assert((CurContext->isDependentContext() || B.builtAll()) &&
4658 "omp simd loop exprs were not built");
4659
Alexander Musman3276a272015-03-21 10:12:56 +00004660 if (!CurContext->isDependentContext()) {
4661 // Finalize the clauses that need pre-built expressions for CodeGen.
4662 for (auto C : Clauses) {
4663 if (auto LC = dyn_cast<OMPLinearClause>(C))
4664 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4665 B.NumIterations, *this, CurScope))
4666 return StmtError();
4667 }
4668 }
4669
Alexey Bataev66b15b52015-08-21 11:14:16 +00004670 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4671 // If both simdlen and safelen clauses are specified, the value of the simdlen
4672 // parameter must be less than or equal to the value of the safelen parameter.
4673 OMPSafelenClause *Safelen = nullptr;
4674 OMPSimdlenClause *Simdlen = nullptr;
4675 for (auto *Clause : Clauses) {
4676 if (Clause->getClauseKind() == OMPC_safelen)
4677 Safelen = cast<OMPSafelenClause>(Clause);
4678 else if (Clause->getClauseKind() == OMPC_simdlen)
4679 Simdlen = cast<OMPSimdlenClause>(Clause);
4680 if (Safelen && Simdlen)
4681 break;
4682 }
4683 if (Simdlen && Safelen &&
4684 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4685 Safelen->getSafelen()))
4686 return StmtError();
4687
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004688 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004689 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4690 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004691}
4692
Alexey Bataev4acb8592014-07-07 13:01:15 +00004693StmtResult Sema::ActOnOpenMPForDirective(
4694 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4695 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004696 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004697 if (!AStmt)
4698 return StmtError();
4699
4700 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004701 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004702 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4703 // define the nested loops number.
4704 unsigned NestedLoopCount = CheckOpenMPLoop(
4705 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4706 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004707 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004708 return StmtError();
4709
Alexander Musmana5f070a2014-10-01 06:03:56 +00004710 assert((CurContext->isDependentContext() || B.builtAll()) &&
4711 "omp for loop exprs were not built");
4712
Alexey Bataev54acd402015-08-04 11:18:19 +00004713 if (!CurContext->isDependentContext()) {
4714 // Finalize the clauses that need pre-built expressions for CodeGen.
4715 for (auto C : Clauses) {
4716 if (auto LC = dyn_cast<OMPLinearClause>(C))
4717 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4718 B.NumIterations, *this, CurScope))
4719 return StmtError();
4720 }
4721 }
4722
Alexey Bataevf29276e2014-06-18 04:14:57 +00004723 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004724 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004725 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004726}
4727
Alexander Musmanf82886e2014-09-18 05:12:34 +00004728StmtResult Sema::ActOnOpenMPForSimdDirective(
4729 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4730 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004731 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004732 if (!AStmt)
4733 return StmtError();
4734
4735 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004736 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004737 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4738 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004739 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004740 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4741 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4742 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004743 if (NestedLoopCount == 0)
4744 return StmtError();
4745
Alexander Musmanc6388682014-12-15 07:07:06 +00004746 assert((CurContext->isDependentContext() || B.builtAll()) &&
4747 "omp for simd loop exprs were not built");
4748
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004749 if (!CurContext->isDependentContext()) {
4750 // Finalize the clauses that need pre-built expressions for CodeGen.
4751 for (auto C : Clauses) {
4752 if (auto LC = dyn_cast<OMPLinearClause>(C))
4753 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4754 B.NumIterations, *this, CurScope))
4755 return StmtError();
4756 }
4757 }
4758
Alexey Bataev66b15b52015-08-21 11:14:16 +00004759 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4760 // If both simdlen and safelen clauses are specified, the value of the simdlen
4761 // parameter must be less than or equal to the value of the safelen parameter.
4762 OMPSafelenClause *Safelen = nullptr;
4763 OMPSimdlenClause *Simdlen = nullptr;
4764 for (auto *Clause : Clauses) {
4765 if (Clause->getClauseKind() == OMPC_safelen)
4766 Safelen = cast<OMPSafelenClause>(Clause);
4767 else if (Clause->getClauseKind() == OMPC_simdlen)
4768 Simdlen = cast<OMPSimdlenClause>(Clause);
4769 if (Safelen && Simdlen)
4770 break;
4771 }
4772 if (Simdlen && Safelen &&
4773 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4774 Safelen->getSafelen()))
4775 return StmtError();
4776
Alexander Musmanf82886e2014-09-18 05:12:34 +00004777 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004778 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4779 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004780}
4781
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004782StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4783 Stmt *AStmt,
4784 SourceLocation StartLoc,
4785 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004786 if (!AStmt)
4787 return StmtError();
4788
4789 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004790 auto BaseStmt = AStmt;
4791 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4792 BaseStmt = CS->getCapturedStmt();
4793 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4794 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004795 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004796 return StmtError();
4797 // All associated statements must be '#pragma omp section' except for
4798 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004799 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004800 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4801 if (SectionStmt)
4802 Diag(SectionStmt->getLocStart(),
4803 diag::err_omp_sections_substmt_not_section);
4804 return StmtError();
4805 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004806 cast<OMPSectionDirective>(SectionStmt)
4807 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004808 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004809 } else {
4810 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4811 return StmtError();
4812 }
4813
4814 getCurFunction()->setHasBranchProtectedScope();
4815
Alexey Bataev25e5b442015-09-15 12:52:43 +00004816 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4817 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004818}
4819
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004820StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4821 SourceLocation StartLoc,
4822 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004823 if (!AStmt)
4824 return StmtError();
4825
4826 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004827
4828 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004829 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004830
Alexey Bataev25e5b442015-09-15 12:52:43 +00004831 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4832 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004833}
4834
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004835StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4836 Stmt *AStmt,
4837 SourceLocation StartLoc,
4838 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004839 if (!AStmt)
4840 return StmtError();
4841
4842 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004843
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004844 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004845
Alexey Bataev3255bf32015-01-19 05:20:46 +00004846 // OpenMP [2.7.3, single Construct, Restrictions]
4847 // The copyprivate clause must not be used with the nowait clause.
4848 OMPClause *Nowait = nullptr;
4849 OMPClause *Copyprivate = nullptr;
4850 for (auto *Clause : Clauses) {
4851 if (Clause->getClauseKind() == OMPC_nowait)
4852 Nowait = Clause;
4853 else if (Clause->getClauseKind() == OMPC_copyprivate)
4854 Copyprivate = Clause;
4855 if (Copyprivate && Nowait) {
4856 Diag(Copyprivate->getLocStart(),
4857 diag::err_omp_single_copyprivate_with_nowait);
4858 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4859 return StmtError();
4860 }
4861 }
4862
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004863 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4864}
4865
Alexander Musman80c22892014-07-17 08:54:58 +00004866StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4867 SourceLocation StartLoc,
4868 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004869 if (!AStmt)
4870 return StmtError();
4871
4872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004873
4874 getCurFunction()->setHasBranchProtectedScope();
4875
4876 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4877}
4878
Alexey Bataev28c75412015-12-15 08:19:24 +00004879StmtResult Sema::ActOnOpenMPCriticalDirective(
4880 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4881 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004882 if (!AStmt)
4883 return StmtError();
4884
4885 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004886
Alexey Bataev28c75412015-12-15 08:19:24 +00004887 bool ErrorFound = false;
4888 llvm::APSInt Hint;
4889 SourceLocation HintLoc;
4890 bool DependentHint = false;
4891 for (auto *C : Clauses) {
4892 if (C->getClauseKind() == OMPC_hint) {
4893 if (!DirName.getName()) {
4894 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4895 ErrorFound = true;
4896 }
4897 Expr *E = cast<OMPHintClause>(C)->getHint();
4898 if (E->isTypeDependent() || E->isValueDependent() ||
4899 E->isInstantiationDependent())
4900 DependentHint = true;
4901 else {
4902 Hint = E->EvaluateKnownConstInt(Context);
4903 HintLoc = C->getLocStart();
4904 }
4905 }
4906 }
4907 if (ErrorFound)
4908 return StmtError();
4909 auto Pair = DSAStack->getCriticalWithHint(DirName);
4910 if (Pair.first && DirName.getName() && !DependentHint) {
4911 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4912 Diag(StartLoc, diag::err_omp_critical_with_hint);
4913 if (HintLoc.isValid()) {
4914 Diag(HintLoc, diag::note_omp_critical_hint_here)
4915 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4916 } else
4917 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4918 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4919 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4920 << 1
4921 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4922 /*Radix=*/10, /*Signed=*/false);
4923 } else
4924 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4925 }
4926 }
4927
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004928 getCurFunction()->setHasBranchProtectedScope();
4929
Alexey Bataev28c75412015-12-15 08:19:24 +00004930 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4931 Clauses, AStmt);
4932 if (!Pair.first && DirName.getName() && !DependentHint)
4933 DSAStack->addCriticalWithHint(Dir, Hint);
4934 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004935}
4936
Alexey Bataev4acb8592014-07-07 13:01:15 +00004937StmtResult Sema::ActOnOpenMPParallelForDirective(
4938 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4939 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004940 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004941 if (!AStmt)
4942 return StmtError();
4943
Alexey Bataev4acb8592014-07-07 13:01:15 +00004944 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4945 // 1.2.2 OpenMP Language Terminology
4946 // Structured block - An executable statement with a single entry at the
4947 // top and a single exit at the bottom.
4948 // The point of exit cannot be a branch out of the structured block.
4949 // longjmp() and throw() must not violate the entry/exit criteria.
4950 CS->getCapturedDecl()->setNothrow();
4951
Alexander Musmanc6388682014-12-15 07:07:06 +00004952 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004953 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4954 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004955 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004956 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4957 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4958 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004959 if (NestedLoopCount == 0)
4960 return StmtError();
4961
Alexander Musmana5f070a2014-10-01 06:03:56 +00004962 assert((CurContext->isDependentContext() || B.builtAll()) &&
4963 "omp parallel for loop exprs were not built");
4964
Alexey Bataev54acd402015-08-04 11:18:19 +00004965 if (!CurContext->isDependentContext()) {
4966 // Finalize the clauses that need pre-built expressions for CodeGen.
4967 for (auto C : Clauses) {
4968 if (auto LC = dyn_cast<OMPLinearClause>(C))
4969 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4970 B.NumIterations, *this, CurScope))
4971 return StmtError();
4972 }
4973 }
4974
Alexey Bataev4acb8592014-07-07 13:01:15 +00004975 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004976 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004977 NestedLoopCount, Clauses, AStmt, B,
4978 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004979}
4980
Alexander Musmane4e893b2014-09-23 09:33:00 +00004981StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4982 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4983 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004984 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004985 if (!AStmt)
4986 return StmtError();
4987
Alexander Musmane4e893b2014-09-23 09:33:00 +00004988 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4989 // 1.2.2 OpenMP Language Terminology
4990 // Structured block - An executable statement with a single entry at the
4991 // top and a single exit at the bottom.
4992 // The point of exit cannot be a branch out of the structured block.
4993 // longjmp() and throw() must not violate the entry/exit criteria.
4994 CS->getCapturedDecl()->setNothrow();
4995
Alexander Musmanc6388682014-12-15 07:07:06 +00004996 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004997 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4998 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004999 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005000 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5001 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5002 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005003 if (NestedLoopCount == 0)
5004 return StmtError();
5005
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005006 if (!CurContext->isDependentContext()) {
5007 // Finalize the clauses that need pre-built expressions for CodeGen.
5008 for (auto C : Clauses) {
5009 if (auto LC = dyn_cast<OMPLinearClause>(C))
5010 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5011 B.NumIterations, *this, CurScope))
5012 return StmtError();
5013 }
5014 }
5015
Alexey Bataev66b15b52015-08-21 11:14:16 +00005016 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5017 // If both simdlen and safelen clauses are specified, the value of the simdlen
5018 // parameter must be less than or equal to the value of the safelen parameter.
5019 OMPSafelenClause *Safelen = nullptr;
5020 OMPSimdlenClause *Simdlen = nullptr;
5021 for (auto *Clause : Clauses) {
5022 if (Clause->getClauseKind() == OMPC_safelen)
5023 Safelen = cast<OMPSafelenClause>(Clause);
5024 else if (Clause->getClauseKind() == OMPC_simdlen)
5025 Simdlen = cast<OMPSimdlenClause>(Clause);
5026 if (Safelen && Simdlen)
5027 break;
5028 }
5029 if (Simdlen && Safelen &&
5030 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5031 Safelen->getSafelen()))
5032 return StmtError();
5033
Alexander Musmane4e893b2014-09-23 09:33:00 +00005034 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005035 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005036 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005037}
5038
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005039StmtResult
5040Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5041 Stmt *AStmt, SourceLocation StartLoc,
5042 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005043 if (!AStmt)
5044 return StmtError();
5045
5046 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005047 auto BaseStmt = AStmt;
5048 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5049 BaseStmt = CS->getCapturedStmt();
5050 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5051 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005052 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005053 return StmtError();
5054 // All associated statements must be '#pragma omp section' except for
5055 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005056 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005057 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5058 if (SectionStmt)
5059 Diag(SectionStmt->getLocStart(),
5060 diag::err_omp_parallel_sections_substmt_not_section);
5061 return StmtError();
5062 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005063 cast<OMPSectionDirective>(SectionStmt)
5064 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005065 }
5066 } else {
5067 Diag(AStmt->getLocStart(),
5068 diag::err_omp_parallel_sections_not_compound_stmt);
5069 return StmtError();
5070 }
5071
5072 getCurFunction()->setHasBranchProtectedScope();
5073
Alexey Bataev25e5b442015-09-15 12:52:43 +00005074 return OMPParallelSectionsDirective::Create(
5075 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005076}
5077
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005078StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5079 Stmt *AStmt, SourceLocation StartLoc,
5080 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005081 if (!AStmt)
5082 return StmtError();
5083
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005084 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5085 // 1.2.2 OpenMP Language Terminology
5086 // Structured block - An executable statement with a single entry at the
5087 // top and a single exit at the bottom.
5088 // The point of exit cannot be a branch out of the structured block.
5089 // longjmp() and throw() must not violate the entry/exit criteria.
5090 CS->getCapturedDecl()->setNothrow();
5091
5092 getCurFunction()->setHasBranchProtectedScope();
5093
Alexey Bataev25e5b442015-09-15 12:52:43 +00005094 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5095 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005096}
5097
Alexey Bataev68446b72014-07-18 07:47:19 +00005098StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5099 SourceLocation EndLoc) {
5100 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5101}
5102
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005103StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5104 SourceLocation EndLoc) {
5105 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5106}
5107
Alexey Bataev2df347a2014-07-18 10:17:07 +00005108StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5109 SourceLocation EndLoc) {
5110 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5111}
5112
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005113StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5114 SourceLocation StartLoc,
5115 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005116 if (!AStmt)
5117 return StmtError();
5118
5119 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005120
5121 getCurFunction()->setHasBranchProtectedScope();
5122
5123 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5124}
5125
Alexey Bataev6125da92014-07-21 11:26:11 +00005126StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5127 SourceLocation StartLoc,
5128 SourceLocation EndLoc) {
5129 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5130 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5131}
5132
Alexey Bataev346265e2015-09-25 10:37:12 +00005133StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5134 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005135 SourceLocation StartLoc,
5136 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005137 OMPClause *DependFound = nullptr;
5138 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005139 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005140 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005141 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005142 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005143 for (auto *C : Clauses) {
5144 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5145 DependFound = C;
5146 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5147 if (DependSourceClause) {
5148 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5149 << getOpenMPDirectiveName(OMPD_ordered)
5150 << getOpenMPClauseName(OMPC_depend) << 2;
5151 ErrorFound = true;
5152 } else
5153 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005154 if (DependSinkClause) {
5155 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5156 << 0;
5157 ErrorFound = true;
5158 }
5159 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5160 if (DependSourceClause) {
5161 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5162 << 1;
5163 ErrorFound = true;
5164 }
5165 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005166 }
5167 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005168 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005169 else if (C->getClauseKind() == OMPC_simd)
5170 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005171 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005172 if (!ErrorFound && !SC &&
5173 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005174 // OpenMP [2.8.1,simd Construct, Restrictions]
5175 // An ordered construct with the simd clause is the only OpenMP construct
5176 // that can appear in the simd region.
5177 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005178 ErrorFound = true;
5179 } else if (DependFound && (TC || SC)) {
5180 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5181 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5182 ErrorFound = true;
5183 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5184 Diag(DependFound->getLocStart(),
5185 diag::err_omp_ordered_directive_without_param);
5186 ErrorFound = true;
5187 } else if (TC || Clauses.empty()) {
5188 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5189 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5190 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5191 << (TC != nullptr);
5192 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5193 ErrorFound = true;
5194 }
5195 }
5196 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005197 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005198
5199 if (AStmt) {
5200 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5201
5202 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005203 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005204
5205 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005206}
5207
Alexey Bataev1d160b12015-03-13 12:27:31 +00005208namespace {
5209/// \brief Helper class for checking expression in 'omp atomic [update]'
5210/// construct.
5211class OpenMPAtomicUpdateChecker {
5212 /// \brief Error results for atomic update expressions.
5213 enum ExprAnalysisErrorCode {
5214 /// \brief A statement is not an expression statement.
5215 NotAnExpression,
5216 /// \brief Expression is not builtin binary or unary operation.
5217 NotABinaryOrUnaryExpression,
5218 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5219 NotAnUnaryIncDecExpression,
5220 /// \brief An expression is not of scalar type.
5221 NotAScalarType,
5222 /// \brief A binary operation is not an assignment operation.
5223 NotAnAssignmentOp,
5224 /// \brief RHS part of the binary operation is not a binary expression.
5225 NotABinaryExpression,
5226 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5227 /// expression.
5228 NotABinaryOperator,
5229 /// \brief RHS binary operation does not have reference to the updated LHS
5230 /// part.
5231 NotAnUpdateExpression,
5232 /// \brief No errors is found.
5233 NoError
5234 };
5235 /// \brief Reference to Sema.
5236 Sema &SemaRef;
5237 /// \brief A location for note diagnostics (when error is found).
5238 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005239 /// \brief 'x' lvalue part of the source atomic expression.
5240 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005241 /// \brief 'expr' rvalue part of the source atomic expression.
5242 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005243 /// \brief Helper expression of the form
5244 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5245 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5246 Expr *UpdateExpr;
5247 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5248 /// important for non-associative operations.
5249 bool IsXLHSInRHSPart;
5250 BinaryOperatorKind Op;
5251 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005252 /// \brief true if the source expression is a postfix unary operation, false
5253 /// if it is a prefix unary operation.
5254 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005255
5256public:
5257 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005258 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005259 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005260 /// \brief Check specified statement that it is suitable for 'atomic update'
5261 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005262 /// expression. If DiagId and NoteId == 0, then only check is performed
5263 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005264 /// \param DiagId Diagnostic which should be emitted if error is found.
5265 /// \param NoteId Diagnostic note for the main error message.
5266 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005267 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005268 /// \brief Return the 'x' lvalue part of the source atomic expression.
5269 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005270 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5271 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005272 /// \brief Return the update expression used in calculation of the updated
5273 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5274 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5275 Expr *getUpdateExpr() const { return UpdateExpr; }
5276 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5277 /// false otherwise.
5278 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5279
Alexey Bataevb78ca832015-04-01 03:33:17 +00005280 /// \brief true if the source expression is a postfix unary operation, false
5281 /// if it is a prefix unary operation.
5282 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5283
Alexey Bataev1d160b12015-03-13 12:27:31 +00005284private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005285 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5286 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005287};
5288} // namespace
5289
5290bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5291 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5292 ExprAnalysisErrorCode ErrorFound = NoError;
5293 SourceLocation ErrorLoc, NoteLoc;
5294 SourceRange ErrorRange, NoteRange;
5295 // Allowed constructs are:
5296 // x = x binop expr;
5297 // x = expr binop x;
5298 if (AtomicBinOp->getOpcode() == BO_Assign) {
5299 X = AtomicBinOp->getLHS();
5300 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5301 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5302 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5303 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5304 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005305 Op = AtomicInnerBinOp->getOpcode();
5306 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005307 auto *LHS = AtomicInnerBinOp->getLHS();
5308 auto *RHS = AtomicInnerBinOp->getRHS();
5309 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5310 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5311 /*Canonical=*/true);
5312 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5313 /*Canonical=*/true);
5314 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5315 /*Canonical=*/true);
5316 if (XId == LHSId) {
5317 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005318 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005319 } else if (XId == RHSId) {
5320 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005321 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005322 } else {
5323 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5324 ErrorRange = AtomicInnerBinOp->getSourceRange();
5325 NoteLoc = X->getExprLoc();
5326 NoteRange = X->getSourceRange();
5327 ErrorFound = NotAnUpdateExpression;
5328 }
5329 } else {
5330 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5331 ErrorRange = AtomicInnerBinOp->getSourceRange();
5332 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5333 NoteRange = SourceRange(NoteLoc, NoteLoc);
5334 ErrorFound = NotABinaryOperator;
5335 }
5336 } else {
5337 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5338 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5339 ErrorFound = NotABinaryExpression;
5340 }
5341 } else {
5342 ErrorLoc = AtomicBinOp->getExprLoc();
5343 ErrorRange = AtomicBinOp->getSourceRange();
5344 NoteLoc = AtomicBinOp->getOperatorLoc();
5345 NoteRange = SourceRange(NoteLoc, NoteLoc);
5346 ErrorFound = NotAnAssignmentOp;
5347 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005348 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005349 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5350 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5351 return true;
5352 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005353 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005354 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005355}
5356
5357bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5358 unsigned NoteId) {
5359 ExprAnalysisErrorCode ErrorFound = NoError;
5360 SourceLocation ErrorLoc, NoteLoc;
5361 SourceRange ErrorRange, NoteRange;
5362 // Allowed constructs are:
5363 // x++;
5364 // x--;
5365 // ++x;
5366 // --x;
5367 // x binop= expr;
5368 // x = x binop expr;
5369 // x = expr binop x;
5370 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5371 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5372 if (AtomicBody->getType()->isScalarType() ||
5373 AtomicBody->isInstantiationDependent()) {
5374 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5375 AtomicBody->IgnoreParenImpCasts())) {
5376 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005377 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005378 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005379 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005380 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005381 X = AtomicCompAssignOp->getLHS();
5382 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005383 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5384 AtomicBody->IgnoreParenImpCasts())) {
5385 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005386 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5387 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005388 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005389 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5390 // Check for Unary Operation
5391 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005392 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005393 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5394 OpLoc = AtomicUnaryOp->getOperatorLoc();
5395 X = AtomicUnaryOp->getSubExpr();
5396 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5397 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005398 } else {
5399 ErrorFound = NotAnUnaryIncDecExpression;
5400 ErrorLoc = AtomicUnaryOp->getExprLoc();
5401 ErrorRange = AtomicUnaryOp->getSourceRange();
5402 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5403 NoteRange = SourceRange(NoteLoc, NoteLoc);
5404 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005405 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005406 ErrorFound = NotABinaryOrUnaryExpression;
5407 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5408 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5409 }
5410 } else {
5411 ErrorFound = NotAScalarType;
5412 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5413 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5414 }
5415 } else {
5416 ErrorFound = NotAnExpression;
5417 NoteLoc = ErrorLoc = S->getLocStart();
5418 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5419 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005420 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005421 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5422 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5423 return true;
5424 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005425 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005426 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005427 // Build an update expression of form 'OpaqueValueExpr(x) binop
5428 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5429 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5430 auto *OVEX = new (SemaRef.getASTContext())
5431 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5432 auto *OVEExpr = new (SemaRef.getASTContext())
5433 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5434 auto Update =
5435 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5436 IsXLHSInRHSPart ? OVEExpr : OVEX);
5437 if (Update.isInvalid())
5438 return true;
5439 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5440 Sema::AA_Casting);
5441 if (Update.isInvalid())
5442 return true;
5443 UpdateExpr = Update.get();
5444 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005445 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005446}
5447
Alexey Bataev0162e452014-07-22 10:10:35 +00005448StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5449 Stmt *AStmt,
5450 SourceLocation StartLoc,
5451 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005452 if (!AStmt)
5453 return StmtError();
5454
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005455 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005456 // 1.2.2 OpenMP Language Terminology
5457 // Structured block - An executable statement with a single entry at the
5458 // top and a single exit at the bottom.
5459 // The point of exit cannot be a branch out of the structured block.
5460 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005461 OpenMPClauseKind AtomicKind = OMPC_unknown;
5462 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005463 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005464 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005465 C->getClauseKind() == OMPC_update ||
5466 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005467 if (AtomicKind != OMPC_unknown) {
5468 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5469 << SourceRange(C->getLocStart(), C->getLocEnd());
5470 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5471 << getOpenMPClauseName(AtomicKind);
5472 } else {
5473 AtomicKind = C->getClauseKind();
5474 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005475 }
5476 }
5477 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005478
Alexey Bataev459dec02014-07-24 06:46:57 +00005479 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005480 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5481 Body = EWC->getSubExpr();
5482
Alexey Bataev62cec442014-11-18 10:14:22 +00005483 Expr *X = nullptr;
5484 Expr *V = nullptr;
5485 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005486 Expr *UE = nullptr;
5487 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005488 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005489 // OpenMP [2.12.6, atomic Construct]
5490 // In the next expressions:
5491 // * x and v (as applicable) are both l-value expressions with scalar type.
5492 // * During the execution of an atomic region, multiple syntactic
5493 // occurrences of x must designate the same storage location.
5494 // * Neither of v and expr (as applicable) may access the storage location
5495 // designated by x.
5496 // * Neither of x and expr (as applicable) may access the storage location
5497 // designated by v.
5498 // * expr is an expression with scalar type.
5499 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5500 // * binop, binop=, ++, and -- are not overloaded operators.
5501 // * The expression x binop expr must be numerically equivalent to x binop
5502 // (expr). This requirement is satisfied if the operators in expr have
5503 // precedence greater than binop, or by using parentheses around expr or
5504 // subexpressions of expr.
5505 // * The expression expr binop x must be numerically equivalent to (expr)
5506 // binop x. This requirement is satisfied if the operators in expr have
5507 // precedence equal to or greater than binop, or by using parentheses around
5508 // expr or subexpressions of expr.
5509 // * For forms that allow multiple occurrences of x, the number of times
5510 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005511 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005512 enum {
5513 NotAnExpression,
5514 NotAnAssignmentOp,
5515 NotAScalarType,
5516 NotAnLValue,
5517 NoError
5518 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005519 SourceLocation ErrorLoc, NoteLoc;
5520 SourceRange ErrorRange, NoteRange;
5521 // If clause is read:
5522 // v = x;
5523 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5524 auto AtomicBinOp =
5525 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5526 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5527 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5528 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5529 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5530 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5531 if (!X->isLValue() || !V->isLValue()) {
5532 auto NotLValueExpr = X->isLValue() ? V : X;
5533 ErrorFound = NotAnLValue;
5534 ErrorLoc = AtomicBinOp->getExprLoc();
5535 ErrorRange = AtomicBinOp->getSourceRange();
5536 NoteLoc = NotLValueExpr->getExprLoc();
5537 NoteRange = NotLValueExpr->getSourceRange();
5538 }
5539 } else if (!X->isInstantiationDependent() ||
5540 !V->isInstantiationDependent()) {
5541 auto NotScalarExpr =
5542 (X->isInstantiationDependent() || X->getType()->isScalarType())
5543 ? V
5544 : X;
5545 ErrorFound = NotAScalarType;
5546 ErrorLoc = AtomicBinOp->getExprLoc();
5547 ErrorRange = AtomicBinOp->getSourceRange();
5548 NoteLoc = NotScalarExpr->getExprLoc();
5549 NoteRange = NotScalarExpr->getSourceRange();
5550 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005551 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005552 ErrorFound = NotAnAssignmentOp;
5553 ErrorLoc = AtomicBody->getExprLoc();
5554 ErrorRange = AtomicBody->getSourceRange();
5555 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5556 : AtomicBody->getExprLoc();
5557 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5558 : AtomicBody->getSourceRange();
5559 }
5560 } else {
5561 ErrorFound = NotAnExpression;
5562 NoteLoc = ErrorLoc = Body->getLocStart();
5563 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005564 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005565 if (ErrorFound != NoError) {
5566 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5567 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005568 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5569 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005570 return StmtError();
5571 } else if (CurContext->isDependentContext())
5572 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005573 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005574 enum {
5575 NotAnExpression,
5576 NotAnAssignmentOp,
5577 NotAScalarType,
5578 NotAnLValue,
5579 NoError
5580 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005581 SourceLocation ErrorLoc, NoteLoc;
5582 SourceRange ErrorRange, NoteRange;
5583 // If clause is write:
5584 // x = expr;
5585 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5586 auto AtomicBinOp =
5587 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5588 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005589 X = AtomicBinOp->getLHS();
5590 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005591 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5592 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5593 if (!X->isLValue()) {
5594 ErrorFound = NotAnLValue;
5595 ErrorLoc = AtomicBinOp->getExprLoc();
5596 ErrorRange = AtomicBinOp->getSourceRange();
5597 NoteLoc = X->getExprLoc();
5598 NoteRange = X->getSourceRange();
5599 }
5600 } else if (!X->isInstantiationDependent() ||
5601 !E->isInstantiationDependent()) {
5602 auto NotScalarExpr =
5603 (X->isInstantiationDependent() || X->getType()->isScalarType())
5604 ? E
5605 : X;
5606 ErrorFound = NotAScalarType;
5607 ErrorLoc = AtomicBinOp->getExprLoc();
5608 ErrorRange = AtomicBinOp->getSourceRange();
5609 NoteLoc = NotScalarExpr->getExprLoc();
5610 NoteRange = NotScalarExpr->getSourceRange();
5611 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005612 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005613 ErrorFound = NotAnAssignmentOp;
5614 ErrorLoc = AtomicBody->getExprLoc();
5615 ErrorRange = AtomicBody->getSourceRange();
5616 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5617 : AtomicBody->getExprLoc();
5618 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5619 : AtomicBody->getSourceRange();
5620 }
5621 } else {
5622 ErrorFound = NotAnExpression;
5623 NoteLoc = ErrorLoc = Body->getLocStart();
5624 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005625 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005626 if (ErrorFound != NoError) {
5627 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5628 << ErrorRange;
5629 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5630 << NoteRange;
5631 return StmtError();
5632 } else if (CurContext->isDependentContext())
5633 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005634 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005635 // If clause is update:
5636 // x++;
5637 // x--;
5638 // ++x;
5639 // --x;
5640 // x binop= expr;
5641 // x = x binop expr;
5642 // x = expr binop x;
5643 OpenMPAtomicUpdateChecker Checker(*this);
5644 if (Checker.checkStatement(
5645 Body, (AtomicKind == OMPC_update)
5646 ? diag::err_omp_atomic_update_not_expression_statement
5647 : diag::err_omp_atomic_not_expression_statement,
5648 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005649 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005650 if (!CurContext->isDependentContext()) {
5651 E = Checker.getExpr();
5652 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005653 UE = Checker.getUpdateExpr();
5654 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005655 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005656 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005657 enum {
5658 NotAnAssignmentOp,
5659 NotACompoundStatement,
5660 NotTwoSubstatements,
5661 NotASpecificExpression,
5662 NoError
5663 } ErrorFound = NoError;
5664 SourceLocation ErrorLoc, NoteLoc;
5665 SourceRange ErrorRange, NoteRange;
5666 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5667 // If clause is a capture:
5668 // v = x++;
5669 // v = x--;
5670 // v = ++x;
5671 // v = --x;
5672 // v = x binop= expr;
5673 // v = x = x binop expr;
5674 // v = x = expr binop x;
5675 auto *AtomicBinOp =
5676 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5677 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5678 V = AtomicBinOp->getLHS();
5679 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5680 OpenMPAtomicUpdateChecker Checker(*this);
5681 if (Checker.checkStatement(
5682 Body, diag::err_omp_atomic_capture_not_expression_statement,
5683 diag::note_omp_atomic_update))
5684 return StmtError();
5685 E = Checker.getExpr();
5686 X = Checker.getX();
5687 UE = Checker.getUpdateExpr();
5688 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5689 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005690 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005691 ErrorLoc = AtomicBody->getExprLoc();
5692 ErrorRange = AtomicBody->getSourceRange();
5693 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5694 : AtomicBody->getExprLoc();
5695 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5696 : AtomicBody->getSourceRange();
5697 ErrorFound = NotAnAssignmentOp;
5698 }
5699 if (ErrorFound != NoError) {
5700 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5701 << ErrorRange;
5702 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5703 return StmtError();
5704 } else if (CurContext->isDependentContext()) {
5705 UE = V = E = X = nullptr;
5706 }
5707 } else {
5708 // If clause is a capture:
5709 // { v = x; x = expr; }
5710 // { v = x; x++; }
5711 // { v = x; x--; }
5712 // { v = x; ++x; }
5713 // { v = x; --x; }
5714 // { v = x; x binop= expr; }
5715 // { v = x; x = x binop expr; }
5716 // { v = x; x = expr binop x; }
5717 // { x++; v = x; }
5718 // { x--; v = x; }
5719 // { ++x; v = x; }
5720 // { --x; v = x; }
5721 // { x binop= expr; v = x; }
5722 // { x = x binop expr; v = x; }
5723 // { x = expr binop x; v = x; }
5724 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5725 // Check that this is { expr1; expr2; }
5726 if (CS->size() == 2) {
5727 auto *First = CS->body_front();
5728 auto *Second = CS->body_back();
5729 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5730 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5731 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5732 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5733 // Need to find what subexpression is 'v' and what is 'x'.
5734 OpenMPAtomicUpdateChecker Checker(*this);
5735 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5736 BinaryOperator *BinOp = nullptr;
5737 if (IsUpdateExprFound) {
5738 BinOp = dyn_cast<BinaryOperator>(First);
5739 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5740 }
5741 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5742 // { v = x; x++; }
5743 // { v = x; x--; }
5744 // { v = x; ++x; }
5745 // { v = x; --x; }
5746 // { v = x; x binop= expr; }
5747 // { v = x; x = x binop expr; }
5748 // { v = x; x = expr binop x; }
5749 // Check that the first expression has form v = x.
5750 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5751 llvm::FoldingSetNodeID XId, PossibleXId;
5752 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5753 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5754 IsUpdateExprFound = XId == PossibleXId;
5755 if (IsUpdateExprFound) {
5756 V = BinOp->getLHS();
5757 X = Checker.getX();
5758 E = Checker.getExpr();
5759 UE = Checker.getUpdateExpr();
5760 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005761 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005762 }
5763 }
5764 if (!IsUpdateExprFound) {
5765 IsUpdateExprFound = !Checker.checkStatement(First);
5766 BinOp = nullptr;
5767 if (IsUpdateExprFound) {
5768 BinOp = dyn_cast<BinaryOperator>(Second);
5769 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5770 }
5771 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5772 // { x++; v = x; }
5773 // { x--; v = x; }
5774 // { ++x; v = x; }
5775 // { --x; v = x; }
5776 // { x binop= expr; v = x; }
5777 // { x = x binop expr; v = x; }
5778 // { x = expr binop x; v = x; }
5779 // Check that the second expression has form v = x.
5780 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5781 llvm::FoldingSetNodeID XId, PossibleXId;
5782 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5783 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5784 IsUpdateExprFound = XId == PossibleXId;
5785 if (IsUpdateExprFound) {
5786 V = BinOp->getLHS();
5787 X = Checker.getX();
5788 E = Checker.getExpr();
5789 UE = Checker.getUpdateExpr();
5790 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005791 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005792 }
5793 }
5794 }
5795 if (!IsUpdateExprFound) {
5796 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005797 auto *FirstExpr = dyn_cast<Expr>(First);
5798 auto *SecondExpr = dyn_cast<Expr>(Second);
5799 if (!FirstExpr || !SecondExpr ||
5800 !(FirstExpr->isInstantiationDependent() ||
5801 SecondExpr->isInstantiationDependent())) {
5802 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5803 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005804 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005805 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5806 : First->getLocStart();
5807 NoteRange = ErrorRange = FirstBinOp
5808 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005809 : SourceRange(ErrorLoc, ErrorLoc);
5810 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005811 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5812 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5813 ErrorFound = NotAnAssignmentOp;
5814 NoteLoc = ErrorLoc = SecondBinOp
5815 ? SecondBinOp->getOperatorLoc()
5816 : Second->getLocStart();
5817 NoteRange = ErrorRange =
5818 SecondBinOp ? SecondBinOp->getSourceRange()
5819 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005820 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005821 auto *PossibleXRHSInFirst =
5822 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5823 auto *PossibleXLHSInSecond =
5824 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5825 llvm::FoldingSetNodeID X1Id, X2Id;
5826 PossibleXRHSInFirst->Profile(X1Id, Context,
5827 /*Canonical=*/true);
5828 PossibleXLHSInSecond->Profile(X2Id, Context,
5829 /*Canonical=*/true);
5830 IsUpdateExprFound = X1Id == X2Id;
5831 if (IsUpdateExprFound) {
5832 V = FirstBinOp->getLHS();
5833 X = SecondBinOp->getLHS();
5834 E = SecondBinOp->getRHS();
5835 UE = nullptr;
5836 IsXLHSInRHSPart = false;
5837 IsPostfixUpdate = true;
5838 } else {
5839 ErrorFound = NotASpecificExpression;
5840 ErrorLoc = FirstBinOp->getExprLoc();
5841 ErrorRange = FirstBinOp->getSourceRange();
5842 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5843 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5844 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005845 }
5846 }
5847 }
5848 }
5849 } else {
5850 NoteLoc = ErrorLoc = Body->getLocStart();
5851 NoteRange = ErrorRange =
5852 SourceRange(Body->getLocStart(), Body->getLocStart());
5853 ErrorFound = NotTwoSubstatements;
5854 }
5855 } else {
5856 NoteLoc = ErrorLoc = Body->getLocStart();
5857 NoteRange = ErrorRange =
5858 SourceRange(Body->getLocStart(), Body->getLocStart());
5859 ErrorFound = NotACompoundStatement;
5860 }
5861 if (ErrorFound != NoError) {
5862 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5863 << ErrorRange;
5864 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5865 return StmtError();
5866 } else if (CurContext->isDependentContext()) {
5867 UE = V = E = X = nullptr;
5868 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005869 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005870 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005871
5872 getCurFunction()->setHasBranchProtectedScope();
5873
Alexey Bataev62cec442014-11-18 10:14:22 +00005874 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005875 X, V, E, UE, IsXLHSInRHSPart,
5876 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005877}
5878
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005879StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5880 Stmt *AStmt,
5881 SourceLocation StartLoc,
5882 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005883 if (!AStmt)
5884 return StmtError();
5885
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005886 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5887 // 1.2.2 OpenMP Language Terminology
5888 // Structured block - An executable statement with a single entry at the
5889 // top and a single exit at the bottom.
5890 // The point of exit cannot be a branch out of the structured block.
5891 // longjmp() and throw() must not violate the entry/exit criteria.
5892 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005893
Alexey Bataev13314bf2014-10-09 04:18:56 +00005894 // OpenMP [2.16, Nesting of Regions]
5895 // If specified, a teams construct must be contained within a target
5896 // construct. That target construct must contain no statements or directives
5897 // outside of the teams construct.
5898 if (DSAStack->hasInnerTeamsRegion()) {
5899 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5900 bool OMPTeamsFound = true;
5901 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5902 auto I = CS->body_begin();
5903 while (I != CS->body_end()) {
5904 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5905 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5906 OMPTeamsFound = false;
5907 break;
5908 }
5909 ++I;
5910 }
5911 assert(I != CS->body_end() && "Not found statement");
5912 S = *I;
5913 }
5914 if (!OMPTeamsFound) {
5915 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5916 Diag(DSAStack->getInnerTeamsRegionLoc(),
5917 diag::note_omp_nested_teams_construct_here);
5918 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5919 << isa<OMPExecutableDirective>(S);
5920 return StmtError();
5921 }
5922 }
5923
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005924 getCurFunction()->setHasBranchProtectedScope();
5925
5926 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5927}
5928
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005929StmtResult
5930Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5931 Stmt *AStmt, SourceLocation StartLoc,
5932 SourceLocation EndLoc) {
5933 if (!AStmt)
5934 return StmtError();
5935
5936 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5937 // 1.2.2 OpenMP Language Terminology
5938 // Structured block - An executable statement with a single entry at the
5939 // top and a single exit at the bottom.
5940 // The point of exit cannot be a branch out of the structured block.
5941 // longjmp() and throw() must not violate the entry/exit criteria.
5942 CS->getCapturedDecl()->setNothrow();
5943
5944 getCurFunction()->setHasBranchProtectedScope();
5945
5946 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5947 AStmt);
5948}
5949
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005950StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5951 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5952 SourceLocation EndLoc,
5953 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5954 if (!AStmt)
5955 return StmtError();
5956
5957 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 OMPLoopDirective::HelperExprs B;
5966 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5967 // define the nested loops number.
5968 unsigned NestedLoopCount =
5969 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5970 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5971 VarsWithImplicitDSA, B);
5972 if (NestedLoopCount == 0)
5973 return StmtError();
5974
5975 assert((CurContext->isDependentContext() || B.builtAll()) &&
5976 "omp target parallel for loop exprs were not built");
5977
5978 if (!CurContext->isDependentContext()) {
5979 // Finalize the clauses that need pre-built expressions for CodeGen.
5980 for (auto C : Clauses) {
5981 if (auto LC = dyn_cast<OMPLinearClause>(C))
5982 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5983 B.NumIterations, *this, CurScope))
5984 return StmtError();
5985 }
5986 }
5987
5988 getCurFunction()->setHasBranchProtectedScope();
5989 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5990 NestedLoopCount, Clauses, AStmt,
5991 B, DSAStack->isCancelRegion());
5992}
5993
Samuel Antaodf67fc42016-01-19 19:15:56 +00005994/// \brief Check for existence of a map clause in the list of clauses.
5995static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5996 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5997 I != E; ++I) {
5998 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5999 return true;
6000 }
6001 }
6002
6003 return false;
6004}
6005
Michael Wong65f367f2015-07-21 13:44:28 +00006006StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6007 Stmt *AStmt,
6008 SourceLocation StartLoc,
6009 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006010 if (!AStmt)
6011 return StmtError();
6012
6013 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6014
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006015 // OpenMP [2.10.1, Restrictions, p. 97]
6016 // At least one map clause must appear on the directive.
6017 if (!HasMapClause(Clauses)) {
6018 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6019 getOpenMPDirectiveName(OMPD_target_data);
6020 return StmtError();
6021 }
6022
Michael Wong65f367f2015-07-21 13:44:28 +00006023 getCurFunction()->setHasBranchProtectedScope();
6024
6025 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6026 AStmt);
6027}
6028
Samuel Antaodf67fc42016-01-19 19:15:56 +00006029StmtResult
6030Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6031 SourceLocation StartLoc,
6032 SourceLocation EndLoc) {
6033 // OpenMP [2.10.2, Restrictions, p. 99]
6034 // At least one map clause must appear on the directive.
6035 if (!HasMapClause(Clauses)) {
6036 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6037 << getOpenMPDirectiveName(OMPD_target_enter_data);
6038 return StmtError();
6039 }
6040
6041 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6042 Clauses);
6043}
6044
Samuel Antao72590762016-01-19 20:04:50 +00006045StmtResult
6046Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6047 SourceLocation StartLoc,
6048 SourceLocation EndLoc) {
6049 // OpenMP [2.10.3, Restrictions, p. 102]
6050 // At least one map clause must appear on the directive.
6051 if (!HasMapClause(Clauses)) {
6052 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6053 << getOpenMPDirectiveName(OMPD_target_exit_data);
6054 return StmtError();
6055 }
6056
6057 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6058}
6059
Alexey Bataev13314bf2014-10-09 04:18:56 +00006060StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6061 Stmt *AStmt, SourceLocation StartLoc,
6062 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006063 if (!AStmt)
6064 return StmtError();
6065
Alexey Bataev13314bf2014-10-09 04:18:56 +00006066 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6067 // 1.2.2 OpenMP Language Terminology
6068 // Structured block - An executable statement with a single entry at the
6069 // top and a single exit at the bottom.
6070 // The point of exit cannot be a branch out of the structured block.
6071 // longjmp() and throw() must not violate the entry/exit criteria.
6072 CS->getCapturedDecl()->setNothrow();
6073
6074 getCurFunction()->setHasBranchProtectedScope();
6075
6076 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6077}
6078
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006079StmtResult
6080Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6081 SourceLocation EndLoc,
6082 OpenMPDirectiveKind CancelRegion) {
6083 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6084 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6085 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6086 << getOpenMPDirectiveName(CancelRegion);
6087 return StmtError();
6088 }
6089 if (DSAStack->isParentNowaitRegion()) {
6090 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6091 return StmtError();
6092 }
6093 if (DSAStack->isParentOrderedRegion()) {
6094 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6095 return StmtError();
6096 }
6097 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6098 CancelRegion);
6099}
6100
Alexey Bataev87933c72015-09-18 08:07:34 +00006101StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6102 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006103 SourceLocation EndLoc,
6104 OpenMPDirectiveKind CancelRegion) {
6105 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6106 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6107 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6108 << getOpenMPDirectiveName(CancelRegion);
6109 return StmtError();
6110 }
6111 if (DSAStack->isParentNowaitRegion()) {
6112 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6113 return StmtError();
6114 }
6115 if (DSAStack->isParentOrderedRegion()) {
6116 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6117 return StmtError();
6118 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006119 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006120 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6121 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006122}
6123
Alexey Bataev382967a2015-12-08 12:06:20 +00006124static bool checkGrainsizeNumTasksClauses(Sema &S,
6125 ArrayRef<OMPClause *> Clauses) {
6126 OMPClause *PrevClause = nullptr;
6127 bool ErrorFound = false;
6128 for (auto *C : Clauses) {
6129 if (C->getClauseKind() == OMPC_grainsize ||
6130 C->getClauseKind() == OMPC_num_tasks) {
6131 if (!PrevClause)
6132 PrevClause = C;
6133 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6134 S.Diag(C->getLocStart(),
6135 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6136 << getOpenMPClauseName(C->getClauseKind())
6137 << getOpenMPClauseName(PrevClause->getClauseKind());
6138 S.Diag(PrevClause->getLocStart(),
6139 diag::note_omp_previous_grainsize_num_tasks)
6140 << getOpenMPClauseName(PrevClause->getClauseKind());
6141 ErrorFound = true;
6142 }
6143 }
6144 }
6145 return ErrorFound;
6146}
6147
Alexey Bataev49f6e782015-12-01 04:18:41 +00006148StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6149 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6150 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006151 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006152 if (!AStmt)
6153 return StmtError();
6154
6155 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6156 OMPLoopDirective::HelperExprs B;
6157 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6158 // define the nested loops number.
6159 unsigned NestedLoopCount =
6160 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006161 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006162 VarsWithImplicitDSA, B);
6163 if (NestedLoopCount == 0)
6164 return StmtError();
6165
6166 assert((CurContext->isDependentContext() || B.builtAll()) &&
6167 "omp for loop exprs were not built");
6168
Alexey Bataev382967a2015-12-08 12:06:20 +00006169 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6170 // The grainsize clause and num_tasks clause are mutually exclusive and may
6171 // not appear on the same taskloop directive.
6172 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6173 return StmtError();
6174
Alexey Bataev49f6e782015-12-01 04:18:41 +00006175 getCurFunction()->setHasBranchProtectedScope();
6176 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6177 NestedLoopCount, Clauses, AStmt, B);
6178}
6179
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006180StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6181 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6182 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006183 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006184 if (!AStmt)
6185 return StmtError();
6186
6187 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6188 OMPLoopDirective::HelperExprs B;
6189 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6190 // define the nested loops number.
6191 unsigned NestedLoopCount =
6192 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6193 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6194 VarsWithImplicitDSA, B);
6195 if (NestedLoopCount == 0)
6196 return StmtError();
6197
6198 assert((CurContext->isDependentContext() || B.builtAll()) &&
6199 "omp for loop exprs were not built");
6200
Alexey Bataev382967a2015-12-08 12:06:20 +00006201 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6202 // The grainsize clause and num_tasks clause are mutually exclusive and may
6203 // not appear on the same taskloop directive.
6204 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6205 return StmtError();
6206
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006207 getCurFunction()->setHasBranchProtectedScope();
6208 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6209 NestedLoopCount, Clauses, AStmt, B);
6210}
6211
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006212StmtResult Sema::ActOnOpenMPDistributeDirective(
6213 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6214 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006215 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006216 if (!AStmt)
6217 return StmtError();
6218
6219 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6220 OMPLoopDirective::HelperExprs B;
6221 // In presence of clause 'collapse' with number of loops, it will
6222 // define the nested loops number.
6223 unsigned NestedLoopCount =
6224 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6225 nullptr /*ordered not a clause on distribute*/, AStmt,
6226 *this, *DSAStack, VarsWithImplicitDSA, B);
6227 if (NestedLoopCount == 0)
6228 return StmtError();
6229
6230 assert((CurContext->isDependentContext() || B.builtAll()) &&
6231 "omp for loop exprs were not built");
6232
6233 getCurFunction()->setHasBranchProtectedScope();
6234 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6235 NestedLoopCount, Clauses, AStmt, B);
6236}
6237
Alexey Bataeved09d242014-05-28 05:53:51 +00006238OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006239 SourceLocation StartLoc,
6240 SourceLocation LParenLoc,
6241 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006242 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006243 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006244 case OMPC_final:
6245 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6246 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006247 case OMPC_num_threads:
6248 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6249 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006250 case OMPC_safelen:
6251 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6252 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006253 case OMPC_simdlen:
6254 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6255 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006256 case OMPC_collapse:
6257 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6258 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006259 case OMPC_ordered:
6260 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6261 break;
Michael Wonge710d542015-08-07 16:16:36 +00006262 case OMPC_device:
6263 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6264 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006265 case OMPC_num_teams:
6266 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6267 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006268 case OMPC_thread_limit:
6269 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6270 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006271 case OMPC_priority:
6272 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6273 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006274 case OMPC_grainsize:
6275 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6276 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006277 case OMPC_num_tasks:
6278 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6279 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006280 case OMPC_hint:
6281 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6282 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006283 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006284 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006285 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006286 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006287 case OMPC_private:
6288 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006289 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006290 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006291 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006292 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006293 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006294 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006295 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006296 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006297 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006298 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006299 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006300 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006301 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006302 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006303 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006304 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006305 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006306 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006307 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006308 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006309 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006310 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006311 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006312 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006313 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006314 llvm_unreachable("Clause is not allowed.");
6315 }
6316 return Res;
6317}
6318
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006319OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6320 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006321 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006322 SourceLocation NameModifierLoc,
6323 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006324 SourceLocation EndLoc) {
6325 Expr *ValExpr = Condition;
6326 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6327 !Condition->isInstantiationDependent() &&
6328 !Condition->containsUnexpandedParameterPack()) {
6329 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006330 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006331 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006332 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006333
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006334 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006335 }
6336
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006337 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6338 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006339}
6340
Alexey Bataev3778b602014-07-17 07:32:53 +00006341OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6342 SourceLocation StartLoc,
6343 SourceLocation LParenLoc,
6344 SourceLocation EndLoc) {
6345 Expr *ValExpr = Condition;
6346 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6347 !Condition->isInstantiationDependent() &&
6348 !Condition->containsUnexpandedParameterPack()) {
6349 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6350 Condition->getExprLoc(), Condition);
6351 if (Val.isInvalid())
6352 return nullptr;
6353
6354 ValExpr = Val.get();
6355 }
6356
6357 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6358}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006359ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6360 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006361 if (!Op)
6362 return ExprError();
6363
6364 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6365 public:
6366 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006367 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006368 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6369 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006370 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6371 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006372 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6373 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006374 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6375 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006376 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6377 QualType T,
6378 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006379 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6380 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006381 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6382 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006383 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006384 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006385 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006386 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6387 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006388 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6389 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006390 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6391 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006392 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006393 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006394 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006395 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6396 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006397 llvm_unreachable("conversion functions are permitted");
6398 }
6399 } ConvertDiagnoser;
6400 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6401}
6402
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006403static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006404 OpenMPClauseKind CKind,
6405 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006406 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6407 !ValExpr->isInstantiationDependent()) {
6408 SourceLocation Loc = ValExpr->getExprLoc();
6409 ExprResult Value =
6410 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6411 if (Value.isInvalid())
6412 return false;
6413
6414 ValExpr = Value.get();
6415 // The expression must evaluate to a non-negative integer value.
6416 llvm::APSInt Result;
6417 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006418 Result.isSigned() &&
6419 !((!StrictlyPositive && Result.isNonNegative()) ||
6420 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006421 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006422 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6423 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006424 return false;
6425 }
6426 }
6427 return true;
6428}
6429
Alexey Bataev568a8332014-03-06 06:15:19 +00006430OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6431 SourceLocation StartLoc,
6432 SourceLocation LParenLoc,
6433 SourceLocation EndLoc) {
6434 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006435
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006436 // OpenMP [2.5, Restrictions]
6437 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006438 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6439 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006440 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006441
Alexey Bataeved09d242014-05-28 05:53:51 +00006442 return new (Context)
6443 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006444}
6445
Alexey Bataev62c87d22014-03-21 04:51:18 +00006446ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006447 OpenMPClauseKind CKind,
6448 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006449 if (!E)
6450 return ExprError();
6451 if (E->isValueDependent() || E->isTypeDependent() ||
6452 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006453 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006454 llvm::APSInt Result;
6455 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6456 if (ICE.isInvalid())
6457 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006458 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6459 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006460 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006461 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6462 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006463 return ExprError();
6464 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006465 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6466 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6467 << E->getSourceRange();
6468 return ExprError();
6469 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006470 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6471 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006472 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006473 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006474 return ICE;
6475}
6476
6477OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6478 SourceLocation LParenLoc,
6479 SourceLocation EndLoc) {
6480 // OpenMP [2.8.1, simd construct, Description]
6481 // The parameter of the safelen clause must be a constant
6482 // positive integer expression.
6483 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6484 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006485 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006486 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006487 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006488}
6489
Alexey Bataev66b15b52015-08-21 11:14:16 +00006490OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6491 SourceLocation LParenLoc,
6492 SourceLocation EndLoc) {
6493 // OpenMP [2.8.1, simd construct, Description]
6494 // The parameter of the simdlen clause must be a constant
6495 // positive integer expression.
6496 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6497 if (Simdlen.isInvalid())
6498 return nullptr;
6499 return new (Context)
6500 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6501}
6502
Alexander Musman64d33f12014-06-04 07:53:32 +00006503OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6504 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006505 SourceLocation LParenLoc,
6506 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006507 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006508 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006509 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006510 // The parameter of the collapse clause must be a constant
6511 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006512 ExprResult NumForLoopsResult =
6513 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6514 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006515 return nullptr;
6516 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006517 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006518}
6519
Alexey Bataev10e775f2015-07-30 11:36:16 +00006520OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6521 SourceLocation EndLoc,
6522 SourceLocation LParenLoc,
6523 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006524 // OpenMP [2.7.1, loop construct, Description]
6525 // OpenMP [2.8.1, simd construct, Description]
6526 // OpenMP [2.9.6, distribute construct, Description]
6527 // The parameter of the ordered clause must be a constant
6528 // positive integer expression if any.
6529 if (NumForLoops && LParenLoc.isValid()) {
6530 ExprResult NumForLoopsResult =
6531 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6532 if (NumForLoopsResult.isInvalid())
6533 return nullptr;
6534 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006535 } else
6536 NumForLoops = nullptr;
6537 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006538 return new (Context)
6539 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6540}
6541
Alexey Bataeved09d242014-05-28 05:53:51 +00006542OMPClause *Sema::ActOnOpenMPSimpleClause(
6543 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6544 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006545 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006546 switch (Kind) {
6547 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006548 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006549 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6550 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006551 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006552 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006553 Res = ActOnOpenMPProcBindClause(
6554 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6555 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006556 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006557 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006558 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006559 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006560 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006561 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006562 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006563 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006564 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006565 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006566 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006567 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006568 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006569 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006570 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006571 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006572 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006573 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006574 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006575 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006576 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006577 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006578 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006579 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006580 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006581 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006582 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006583 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006584 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006585 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006586 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006587 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006588 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006589 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006590 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006591 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006592 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006593 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006594 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006595 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006596 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006597 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006598 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006599 llvm_unreachable("Clause is not allowed.");
6600 }
6601 return Res;
6602}
6603
Alexey Bataev6402bca2015-12-28 07:25:51 +00006604static std::string
6605getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6606 ArrayRef<unsigned> Exclude = llvm::None) {
6607 std::string Values;
6608 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6609 unsigned Skipped = Exclude.size();
6610 auto S = Exclude.begin(), E = Exclude.end();
6611 for (unsigned i = First; i < Last; ++i) {
6612 if (std::find(S, E, i) != E) {
6613 --Skipped;
6614 continue;
6615 }
6616 Values += "'";
6617 Values += getOpenMPSimpleClauseTypeName(K, i);
6618 Values += "'";
6619 if (i == Bound - Skipped)
6620 Values += " or ";
6621 else if (i != Bound + 1 - Skipped)
6622 Values += ", ";
6623 }
6624 return Values;
6625}
6626
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006627OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6628 SourceLocation KindKwLoc,
6629 SourceLocation StartLoc,
6630 SourceLocation LParenLoc,
6631 SourceLocation EndLoc) {
6632 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006633 static_assert(OMPC_DEFAULT_unknown > 0,
6634 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006635 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006636 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6637 /*Last=*/OMPC_DEFAULT_unknown)
6638 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006639 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006640 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006641 switch (Kind) {
6642 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006643 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006644 break;
6645 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006646 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006647 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006648 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006649 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006650 break;
6651 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006652 return new (Context)
6653 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006654}
6655
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006656OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6657 SourceLocation KindKwLoc,
6658 SourceLocation StartLoc,
6659 SourceLocation LParenLoc,
6660 SourceLocation EndLoc) {
6661 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006662 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006663 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6664 /*Last=*/OMPC_PROC_BIND_unknown)
6665 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006666 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006667 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006668 return new (Context)
6669 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006670}
6671
Alexey Bataev56dafe82014-06-20 07:16:17 +00006672OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006673 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006674 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006675 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006676 SourceLocation EndLoc) {
6677 OMPClause *Res = nullptr;
6678 switch (Kind) {
6679 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006680 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6681 assert(Argument.size() == NumberOfElements &&
6682 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006683 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006684 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6685 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6686 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6687 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6688 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006689 break;
6690 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006691 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6692 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6693 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6694 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006695 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006696 case OMPC_dist_schedule:
6697 Res = ActOnOpenMPDistScheduleClause(
6698 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6699 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6700 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006701 case OMPC_defaultmap:
6702 enum { Modifier, DefaultmapKind };
6703 Res = ActOnOpenMPDefaultmapClause(
6704 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6705 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6706 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6707 ArgumentLoc[DefaultmapKind], EndLoc);
6708 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006709 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006710 case OMPC_num_threads:
6711 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006712 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006713 case OMPC_collapse:
6714 case OMPC_default:
6715 case OMPC_proc_bind:
6716 case OMPC_private:
6717 case OMPC_firstprivate:
6718 case OMPC_lastprivate:
6719 case OMPC_shared:
6720 case OMPC_reduction:
6721 case OMPC_linear:
6722 case OMPC_aligned:
6723 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006724 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006725 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006726 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006727 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006728 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006729 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006730 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006731 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006732 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006733 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006734 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006735 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006736 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006737 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006738 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006739 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006740 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006741 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006742 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006743 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006744 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006745 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006746 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006747 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006748 case OMPC_unknown:
6749 llvm_unreachable("Clause is not allowed.");
6750 }
6751 return Res;
6752}
6753
Alexey Bataev6402bca2015-12-28 07:25:51 +00006754static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6755 OpenMPScheduleClauseModifier M2,
6756 SourceLocation M1Loc, SourceLocation M2Loc) {
6757 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6758 SmallVector<unsigned, 2> Excluded;
6759 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6760 Excluded.push_back(M2);
6761 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6762 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6763 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6764 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6765 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6766 << getListOfPossibleValues(OMPC_schedule,
6767 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6768 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6769 Excluded)
6770 << getOpenMPClauseName(OMPC_schedule);
6771 return true;
6772 }
6773 return false;
6774}
6775
Alexey Bataev56dafe82014-06-20 07:16:17 +00006776OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006777 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006778 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006779 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6780 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6781 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6782 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6783 return nullptr;
6784 // OpenMP, 2.7.1, Loop Construct, Restrictions
6785 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6786 // but not both.
6787 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6788 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6789 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6790 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6791 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6792 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6793 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6794 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6795 return nullptr;
6796 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006797 if (Kind == OMPC_SCHEDULE_unknown) {
6798 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006799 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6800 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6801 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6802 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6803 Exclude);
6804 } else {
6805 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6806 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006807 }
6808 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6809 << Values << getOpenMPClauseName(OMPC_schedule);
6810 return nullptr;
6811 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006812 // OpenMP, 2.7.1, Loop Construct, Restrictions
6813 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6814 // schedule(guided).
6815 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6816 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6817 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6818 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6819 diag::err_omp_schedule_nonmonotonic_static);
6820 return nullptr;
6821 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006822 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006823 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006824 if (ChunkSize) {
6825 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6826 !ChunkSize->isInstantiationDependent() &&
6827 !ChunkSize->containsUnexpandedParameterPack()) {
6828 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6829 ExprResult Val =
6830 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6831 if (Val.isInvalid())
6832 return nullptr;
6833
6834 ValExpr = Val.get();
6835
6836 // OpenMP [2.7.1, Restrictions]
6837 // chunk_size must be a loop invariant integer expression with a positive
6838 // value.
6839 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006840 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6841 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6842 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006843 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006844 return nullptr;
6845 }
6846 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00006847 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00006848 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
6849 HelperValStmt =
6850 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
6851 /*NumDecls=*/1),
6852 SourceLocation(), SourceLocation());
6853 ValExpr = DefaultLvalueConversion(ValExpr).get();
Alexey Bataev56dafe82014-06-20 07:16:17 +00006854 }
6855 }
6856 }
6857
Alexey Bataev6402bca2015-12-28 07:25:51 +00006858 return new (Context)
6859 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006860 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006861}
6862
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006863OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6864 SourceLocation StartLoc,
6865 SourceLocation EndLoc) {
6866 OMPClause *Res = nullptr;
6867 switch (Kind) {
6868 case OMPC_ordered:
6869 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6870 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006871 case OMPC_nowait:
6872 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6873 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006874 case OMPC_untied:
6875 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6876 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006877 case OMPC_mergeable:
6878 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6879 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006880 case OMPC_read:
6881 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6882 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006883 case OMPC_write:
6884 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6885 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006886 case OMPC_update:
6887 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6888 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006889 case OMPC_capture:
6890 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6891 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006892 case OMPC_seq_cst:
6893 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6894 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006895 case OMPC_threads:
6896 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6897 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006898 case OMPC_simd:
6899 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6900 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006901 case OMPC_nogroup:
6902 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6903 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006904 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006905 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006906 case OMPC_num_threads:
6907 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006908 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006909 case OMPC_collapse:
6910 case OMPC_schedule:
6911 case OMPC_private:
6912 case OMPC_firstprivate:
6913 case OMPC_lastprivate:
6914 case OMPC_shared:
6915 case OMPC_reduction:
6916 case OMPC_linear:
6917 case OMPC_aligned:
6918 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006919 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006920 case OMPC_default:
6921 case OMPC_proc_bind:
6922 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006923 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006924 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006925 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006926 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006927 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006928 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006929 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006930 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006931 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006932 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006933 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006934 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006935 case OMPC_unknown:
6936 llvm_unreachable("Clause is not allowed.");
6937 }
6938 return Res;
6939}
6940
Alexey Bataev236070f2014-06-20 11:19:47 +00006941OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6942 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006943 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006944 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6945}
6946
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006947OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6948 SourceLocation EndLoc) {
6949 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6950}
6951
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006952OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6953 SourceLocation EndLoc) {
6954 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6955}
6956
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006957OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6958 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006959 return new (Context) OMPReadClause(StartLoc, EndLoc);
6960}
6961
Alexey Bataevdea47612014-07-23 07:46:59 +00006962OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6963 SourceLocation EndLoc) {
6964 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6965}
6966
Alexey Bataev67a4f222014-07-23 10:25:33 +00006967OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6968 SourceLocation EndLoc) {
6969 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6970}
6971
Alexey Bataev459dec02014-07-24 06:46:57 +00006972OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6973 SourceLocation EndLoc) {
6974 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6975}
6976
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006977OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6978 SourceLocation EndLoc) {
6979 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6980}
6981
Alexey Bataev346265e2015-09-25 10:37:12 +00006982OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6983 SourceLocation EndLoc) {
6984 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6985}
6986
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006987OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6988 SourceLocation EndLoc) {
6989 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6990}
6991
Alexey Bataevb825de12015-12-07 10:51:44 +00006992OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6993 SourceLocation EndLoc) {
6994 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6995}
6996
Alexey Bataevc5e02582014-06-16 07:08:35 +00006997OMPClause *Sema::ActOnOpenMPVarListClause(
6998 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6999 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7000 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007001 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007002 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7003 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7004 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007005 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007006 switch (Kind) {
7007 case OMPC_private:
7008 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7009 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007010 case OMPC_firstprivate:
7011 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7012 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007013 case OMPC_lastprivate:
7014 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7015 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007016 case OMPC_shared:
7017 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7018 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007019 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007020 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7021 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007022 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007023 case OMPC_linear:
7024 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007025 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007026 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007027 case OMPC_aligned:
7028 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7029 ColonLoc, EndLoc);
7030 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007031 case OMPC_copyin:
7032 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7033 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007034 case OMPC_copyprivate:
7035 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7036 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007037 case OMPC_flush:
7038 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7039 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007040 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007041 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7042 StartLoc, LParenLoc, EndLoc);
7043 break;
7044 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007045 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7046 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7047 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007048 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007049 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007050 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007051 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007052 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007053 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007054 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007055 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007056 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007057 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007058 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007059 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007060 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007061 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007062 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007063 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007064 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007065 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007066 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007067 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007068 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007069 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007070 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007071 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007072 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007073 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007074 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007075 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007076 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007077 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007078 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007079 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007080 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007081 llvm_unreachable("Clause is not allowed.");
7082 }
7083 return Res;
7084}
7085
Alexey Bataev90c228f2016-02-08 09:29:13 +00007086ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7087 ExprObjectKind OK) {
7088 SourceLocation Loc = Capture->getInit()->getExprLoc();
7089 ExprResult Res = BuildDeclRefExpr(
7090 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7091 if (!Res.isUsable())
7092 return ExprError();
7093 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7094 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7095 if (!Res.isUsable())
7096 return ExprError();
7097 }
7098 if (VK != VK_LValue && Res.get()->isGLValue()) {
7099 Res = DefaultLvalueConversion(Res.get());
7100 if (!Res.isUsable())
7101 return ExprError();
7102 }
7103 return Res;
7104}
7105
Alexey Bataevd985eda2016-02-10 11:29:16 +00007106static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *RefExpr) {
7107 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7108 RefExpr->containsUnexpandedParameterPack())
7109 return std::make_pair(nullptr, true);
7110
7111 SourceLocation ELoc = RefExpr->getExprLoc();
7112 SourceRange SR = RefExpr->getSourceRange();
7113 // OpenMP [3.1, C/C++]
7114 // A list item is a variable name.
7115 // OpenMP [2.9.3.3, Restrictions, p.1]
7116 // A variable that is part of another variable (as an array or
7117 // structure element) cannot appear in a private clause.
7118 RefExpr = RefExpr->IgnoreParens();
7119 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7120 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7121 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7122 (S.getCurrentThisType().isNull() || !ME ||
7123 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7124 !isa<FieldDecl>(ME->getMemberDecl()))) {
7125 S.Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7126 << (S.getCurrentThisType().isNull() ? 0 : 1) << SR;
7127 return std::make_pair(nullptr, false);
7128 }
7129 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7130}
7131
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007132OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7133 SourceLocation StartLoc,
7134 SourceLocation LParenLoc,
7135 SourceLocation EndLoc) {
7136 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007137 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007138 for (auto &RefExpr : VarList) {
7139 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007140 auto Res = getPrivateItem(*this, RefExpr);
7141 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007142 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007143 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007144 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007145 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007146 ValueDecl *D = Res.first;
7147 if (!D)
7148 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007149
Alexey Bataeved09d242014-05-28 05:53:51 +00007150 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007151 QualType Type = D->getType();
7152 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007153
7154 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7155 // A variable that appears in a private clause must not have an incomplete
7156 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007157 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007158 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007159 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007160
Alexey Bataev758e55e2013-09-06 18:03:48 +00007161 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7162 // in a Construct]
7163 // Variables with the predetermined data-sharing attributes may not be
7164 // listed in data-sharing attributes clauses, except for the cases
7165 // listed below. For these exceptions only, listing a predetermined
7166 // variable in a data-sharing attribute clause is allowed and overrides
7167 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007168 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007169 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007170 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7171 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007172 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007173 continue;
7174 }
7175
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007176 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007177 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007178 DSAStack->getCurrentDirective() == OMPD_task) {
7179 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7180 << getOpenMPClauseName(OMPC_private) << Type
7181 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7182 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007183 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007184 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007185 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007186 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007187 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007188 continue;
7189 }
7190
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007191 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7192 // A variable of class type (or array thereof) that appears in a private
7193 // clause requires an accessible, unambiguous default constructor for the
7194 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007195 // Generate helper private variable and initialize it with the default
7196 // value. The address of the original variable is replaced by the address of
7197 // the new private variable in CodeGen. This new variable is not added to
7198 // IdResolver, so the code in the OpenMP region uses original variable for
7199 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007200 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007201 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7202 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007203 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007204 if (VDPrivate->isInvalidDecl())
7205 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007206 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007207 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007208
Alexey Bataev90c228f2016-02-08 09:29:13 +00007209 DeclRefExpr *Ref = nullptr;
7210 if (!VD)
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007211 Ref = buildCapture(*this, D, RefExpr);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007212 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7213 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007214 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007215 }
7216
Alexey Bataeved09d242014-05-28 05:53:51 +00007217 if (Vars.empty())
7218 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007219
Alexey Bataev03b340a2014-10-21 03:16:40 +00007220 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7221 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007222}
7223
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007224namespace {
7225class DiagsUninitializedSeveretyRAII {
7226private:
7227 DiagnosticsEngine &Diags;
7228 SourceLocation SavedLoc;
7229 bool IsIgnored;
7230
7231public:
7232 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7233 bool IsIgnored)
7234 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7235 if (!IsIgnored) {
7236 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7237 /*Map*/ diag::Severity::Ignored, Loc);
7238 }
7239 }
7240 ~DiagsUninitializedSeveretyRAII() {
7241 if (!IsIgnored)
7242 Diags.popMappings(SavedLoc);
7243 }
7244};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007245}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007246
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007247OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7248 SourceLocation StartLoc,
7249 SourceLocation LParenLoc,
7250 SourceLocation EndLoc) {
7251 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007252 SmallVector<Expr *, 8> PrivateCopies;
7253 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007254 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007255 bool IsImplicitClause =
7256 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7257 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7258
Alexey Bataeved09d242014-05-28 05:53:51 +00007259 for (auto &RefExpr : VarList) {
7260 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007261 auto Res = getPrivateItem(*this, RefExpr);
7262 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007263 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007264 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007265 PrivateCopies.push_back(nullptr);
7266 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007267 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007268 ValueDecl *D = Res.first;
7269 if (!D)
7270 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007271
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007272 SourceLocation ELoc =
7273 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007274 QualType Type = D->getType();
7275 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007276
7277 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7278 // A variable that appears in a private clause must not have an incomplete
7279 // type or a reference type.
7280 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007281 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007282 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007283 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007284
7285 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7286 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007287 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007288 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007289 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007290
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007291 // If an implicit firstprivate variable found it was checked already.
7292 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007293 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007294 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007295 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7296 // A list item that specifies a given variable may not appear in more
7297 // than one clause on the same directive, except that a variable may be
7298 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007299 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007300 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007301 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007302 << getOpenMPClauseName(DVar.CKind)
7303 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007304 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007305 continue;
7306 }
7307
7308 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7309 // in a Construct]
7310 // Variables with the predetermined data-sharing attributes may not be
7311 // listed in data-sharing attributes clauses, except for the cases
7312 // listed below. For these exceptions only, listing a predetermined
7313 // variable in a data-sharing attribute clause is allowed and overrides
7314 // the variable's predetermined data-sharing attributes.
7315 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7316 // in a Construct, C/C++, p.2]
7317 // Variables with const-qualified type having no mutable member may be
7318 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007319 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007320 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7321 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007322 << getOpenMPClauseName(DVar.CKind)
7323 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007324 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007325 continue;
7326 }
7327
Alexey Bataevf29276e2014-06-18 04:14:57 +00007328 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007329 // OpenMP [2.9.3.4, Restrictions, p.2]
7330 // A list item that is private within a parallel region must not appear
7331 // in a firstprivate clause on a worksharing construct if any of the
7332 // worksharing regions arising from the worksharing construct ever bind
7333 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007334 if (isOpenMPWorksharingDirective(CurrDir) &&
7335 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007336 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007337 if (DVar.CKind != OMPC_shared &&
7338 (isOpenMPParallelDirective(DVar.DKind) ||
7339 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007340 Diag(ELoc, diag::err_omp_required_access)
7341 << getOpenMPClauseName(OMPC_firstprivate)
7342 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007343 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007344 continue;
7345 }
7346 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007347 // OpenMP [2.9.3.4, Restrictions, p.3]
7348 // A list item that appears in a reduction clause of a parallel construct
7349 // must not appear in a firstprivate clause on a worksharing or task
7350 // construct if any of the worksharing or task regions arising from the
7351 // worksharing or task construct ever bind to any of the parallel regions
7352 // arising from the parallel construct.
7353 // OpenMP [2.9.3.4, Restrictions, p.4]
7354 // A list item that appears in a reduction clause in worksharing
7355 // construct must not appear in a firstprivate clause in a task construct
7356 // encountered during execution of any of the worksharing regions arising
7357 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007358 if (CurrDir == OMPD_task) {
7359 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007360 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007361 [](OpenMPDirectiveKind K) -> bool {
7362 return isOpenMPParallelDirective(K) ||
7363 isOpenMPWorksharingDirective(K);
7364 },
7365 false);
7366 if (DVar.CKind == OMPC_reduction &&
7367 (isOpenMPParallelDirective(DVar.DKind) ||
7368 isOpenMPWorksharingDirective(DVar.DKind))) {
7369 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7370 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007371 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007372 continue;
7373 }
7374 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007375
7376 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7377 // A list item that is private within a teams region must not appear in a
7378 // firstprivate clause on a distribute construct if any of the distribute
7379 // regions arising from the distribute construct ever bind to any of the
7380 // teams regions arising from the teams construct.
7381 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7382 // A list item that appears in a reduction clause of a teams construct
7383 // must not appear in a firstprivate clause on a distribute construct if
7384 // any of the distribute regions arising from the distribute construct
7385 // ever bind to any of the teams regions arising from the teams construct.
7386 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7387 // A list item may appear in a firstprivate or lastprivate clause but not
7388 // both.
7389 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007390 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007391 [](OpenMPDirectiveKind K) -> bool {
7392 return isOpenMPTeamsDirective(K);
7393 },
7394 false);
7395 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7396 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007397 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007398 continue;
7399 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007400 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007401 [](OpenMPDirectiveKind K) -> bool {
7402 return isOpenMPTeamsDirective(K);
7403 },
7404 false);
7405 if (DVar.CKind == OMPC_reduction &&
7406 isOpenMPTeamsDirective(DVar.DKind)) {
7407 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007408 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007409 continue;
7410 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007411 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007412 if (DVar.CKind == OMPC_lastprivate) {
7413 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007414 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007415 continue;
7416 }
7417 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007418 }
7419
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007420 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007421 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007422 DSAStack->getCurrentDirective() == OMPD_task) {
7423 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7424 << getOpenMPClauseName(OMPC_firstprivate) << Type
7425 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7426 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007427 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007428 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007429 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007430 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007431 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007432 continue;
7433 }
7434
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007435 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007436 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7437 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007438 // Generate helper private variable and initialize it with the value of the
7439 // original variable. The address of the original variable is replaced by
7440 // the address of the new private variable in the CodeGen. This new variable
7441 // is not added to IdResolver, so the code in the OpenMP region uses
7442 // original variable for proper diagnostics and variable capturing.
7443 Expr *VDInitRefExpr = nullptr;
7444 // For arrays generate initializer for single element and replace it by the
7445 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007446 if (Type->isArrayType()) {
7447 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007448 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007449 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007450 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007451 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007452 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007453 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007454 InitializedEntity Entity =
7455 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007456 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7457
7458 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7459 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7460 if (Result.isInvalid())
7461 VDPrivate->setInvalidDecl();
7462 else
7463 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007464 // Remove temp variable declaration.
7465 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007466 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007467 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7468 ".firstprivate.temp");
7469 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7470 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007471 AddInitializerToDecl(VDPrivate,
7472 DefaultLvalueConversion(VDInitRefExpr).get(),
7473 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007474 }
7475 if (VDPrivate->isInvalidDecl()) {
7476 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007477 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007478 diag::note_omp_task_predetermined_firstprivate_here);
7479 }
7480 continue;
7481 }
7482 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007483 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007484 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7485 RefExpr->getExprLoc());
7486 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007487 if (!VD) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007488 Ref = buildCapture(*this, D, RefExpr);
Alexey Bataev417089f2016-02-17 13:19:37 +00007489 ExprCaptures.push_back(Ref->getDecl());
7490 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007491 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7492 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007493 PrivateCopies.push_back(VDPrivateRefExpr);
7494 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007495 }
7496
Alexey Bataeved09d242014-05-28 05:53:51 +00007497 if (Vars.empty())
7498 return nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007499 Stmt *PreInit = nullptr;
7500 if (!ExprCaptures.empty()) {
7501 PreInit = new (Context)
7502 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7503 ExprCaptures.size()),
7504 SourceLocation(), SourceLocation());
7505 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007506
7507 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev417089f2016-02-17 13:19:37 +00007508 Vars, PrivateCopies, Inits, PreInit);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007509}
7510
Alexander Musman1bb328c2014-06-04 13:06:39 +00007511OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7512 SourceLocation StartLoc,
7513 SourceLocation LParenLoc,
7514 SourceLocation EndLoc) {
7515 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007516 SmallVector<Expr *, 8> SrcExprs;
7517 SmallVector<Expr *, 8> DstExprs;
7518 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007519 for (auto &RefExpr : VarList) {
7520 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev74caaf22016-02-20 04:09:36 +00007521 auto Res = getPrivateItem(*this, RefExpr);
7522 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007523 // It will be analyzed later.
7524 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007525 SrcExprs.push_back(nullptr);
7526 DstExprs.push_back(nullptr);
7527 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007528 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007529 ValueDecl *D = Res.first;
7530 if (!D)
7531 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007532
7533 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev74caaf22016-02-20 04:09:36 +00007534 QualType Type = D->getType();
7535 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007536
7537 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7538 // A variable that appears in a lastprivate clause must not have an
7539 // incomplete type or a reference type.
7540 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007541 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007542 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007543 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007544
7545 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7546 // in a Construct]
7547 // Variables with the predetermined data-sharing attributes may not be
7548 // listed in data-sharing attributes clauses, except for the cases
7549 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007550 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007551 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7552 DVar.CKind != OMPC_firstprivate &&
7553 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7554 Diag(ELoc, diag::err_omp_wrong_dsa)
7555 << getOpenMPClauseName(DVar.CKind)
7556 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007557 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007558 continue;
7559 }
7560
Alexey Bataevf29276e2014-06-18 04:14:57 +00007561 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7562 // OpenMP [2.14.3.5, Restrictions, p.2]
7563 // A list item that is private within a parallel region, or that appears in
7564 // the reduction clause of a parallel construct, must not appear in a
7565 // lastprivate clause on a worksharing construct if any of the corresponding
7566 // worksharing regions ever binds to any of the corresponding parallel
7567 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007568 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007569 if (isOpenMPWorksharingDirective(CurrDir) &&
7570 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007571 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007572 if (DVar.CKind != OMPC_shared) {
7573 Diag(ELoc, diag::err_omp_required_access)
7574 << getOpenMPClauseName(OMPC_lastprivate)
7575 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007576 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007577 continue;
7578 }
7579 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007580
7581 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7582 // A list item may appear in a firstprivate or lastprivate clause but not
7583 // both.
7584 if (CurrDir == OMPD_distribute) {
7585 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7586 if (DVar.CKind == OMPC_firstprivate) {
7587 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7588 ReportOriginalDSA(*this, DSAStack, D, DVar);
7589 continue;
7590 }
7591 }
7592
Alexander Musman1bb328c2014-06-04 13:06:39 +00007593 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007594 // A variable of class type (or array thereof) that appears in a
7595 // lastprivate clause requires an accessible, unambiguous default
7596 // constructor for the class type, unless the list item is also specified
7597 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007598 // A variable of class type (or array thereof) that appears in a
7599 // lastprivate clause requires an accessible, unambiguous copy assignment
7600 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007601 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev74caaf22016-02-20 04:09:36 +00007602 auto *SrcVD = buildVarDecl(*this, RefExpr->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007603 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007604 D->hasAttrs() ? &D->getAttrs() : nullptr);
7605 auto *PseudoSrcExpr =
7606 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007607 auto *DstVD =
Alexey Bataev74caaf22016-02-20 04:09:36 +00007608 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".lastprivate.dst",
7609 D->hasAttrs() ? &D->getAttrs() : nullptr);
7610 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007611 // For arrays generate assignment operation for single element and replace
7612 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007613 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007614 PseudoDstExpr, PseudoSrcExpr);
7615 if (AssignmentOp.isInvalid())
7616 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007617 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007618 /*DiscardedValue=*/true);
7619 if (AssignmentOp.isInvalid())
7620 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007621
Alexey Bataev74caaf22016-02-20 04:09:36 +00007622 DeclRefExpr *Ref = nullptr;
7623 if (!VD)
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007624 Ref = buildCapture(*this, D, RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007625 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007626 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7627 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007628 SrcExprs.push_back(PseudoSrcExpr);
7629 DstExprs.push_back(PseudoDstExpr);
7630 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007631 }
7632
7633 if (Vars.empty())
7634 return nullptr;
7635
7636 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007637 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007638}
7639
Alexey Bataev758e55e2013-09-06 18:03:48 +00007640OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7641 SourceLocation StartLoc,
7642 SourceLocation LParenLoc,
7643 SourceLocation EndLoc) {
7644 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007645 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007646 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7647 auto Res = getPrivateItem(*this, RefExpr);
7648 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007649 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007650 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007651 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007652 ValueDecl *D = Res.first;
7653 if (!D)
7654 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007655
Alexey Bataeved09d242014-05-28 05:53:51 +00007656 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007657 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007658 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7659 // in a Construct]
7660 // Variables with the predetermined data-sharing attributes may not be
7661 // listed in data-sharing attributes clauses, except for the cases
7662 // listed below. For these exceptions only, listing a predetermined
7663 // variable in a data-sharing attribute clause is allowed and overrides
7664 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007665 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007666 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7667 DVar.RefExpr) {
7668 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7669 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007670 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007671 continue;
7672 }
7673
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007674 DeclRefExpr *Ref = nullptr;
7675 if (!VD)
7676 Ref = buildCapture(*this, D, RefExpr);
7677 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
7678 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007679 }
7680
Alexey Bataeved09d242014-05-28 05:53:51 +00007681 if (Vars.empty())
7682 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007683
7684 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7685}
7686
Alexey Bataevc5e02582014-06-16 07:08:35 +00007687namespace {
7688class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7689 DSAStackTy *Stack;
7690
7691public:
7692 bool VisitDeclRefExpr(DeclRefExpr *E) {
7693 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007694 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007695 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7696 return false;
7697 if (DVar.CKind != OMPC_unknown)
7698 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007699 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007700 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007701 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007702 return true;
7703 return false;
7704 }
7705 return false;
7706 }
7707 bool VisitStmt(Stmt *S) {
7708 for (auto Child : S->children()) {
7709 if (Child && Visit(Child))
7710 return true;
7711 }
7712 return false;
7713 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007714 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007715};
Alexey Bataev23b69422014-06-18 07:08:49 +00007716} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007717
7718OMPClause *Sema::ActOnOpenMPReductionClause(
7719 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7720 SourceLocation ColonLoc, SourceLocation EndLoc,
7721 CXXScopeSpec &ReductionIdScopeSpec,
7722 const DeclarationNameInfo &ReductionId) {
7723 // TODO: Allow scope specification search when 'declare reduction' is
7724 // supported.
7725 assert(ReductionIdScopeSpec.isEmpty() &&
7726 "No support for scoped reduction identifiers yet.");
7727
7728 auto DN = ReductionId.getName();
7729 auto OOK = DN.getCXXOverloadedOperator();
7730 BinaryOperatorKind BOK = BO_Comma;
7731
7732 // OpenMP [2.14.3.6, reduction clause]
7733 // C
7734 // reduction-identifier is either an identifier or one of the following
7735 // operators: +, -, *, &, |, ^, && and ||
7736 // C++
7737 // reduction-identifier is either an id-expression or one of the following
7738 // operators: +, -, *, &, |, ^, && and ||
7739 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7740 switch (OOK) {
7741 case OO_Plus:
7742 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007743 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007744 break;
7745 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007746 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007747 break;
7748 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007749 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007750 break;
7751 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007752 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007753 break;
7754 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007755 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007756 break;
7757 case OO_AmpAmp:
7758 BOK = BO_LAnd;
7759 break;
7760 case OO_PipePipe:
7761 BOK = BO_LOr;
7762 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007763 case OO_New:
7764 case OO_Delete:
7765 case OO_Array_New:
7766 case OO_Array_Delete:
7767 case OO_Slash:
7768 case OO_Percent:
7769 case OO_Tilde:
7770 case OO_Exclaim:
7771 case OO_Equal:
7772 case OO_Less:
7773 case OO_Greater:
7774 case OO_LessEqual:
7775 case OO_GreaterEqual:
7776 case OO_PlusEqual:
7777 case OO_MinusEqual:
7778 case OO_StarEqual:
7779 case OO_SlashEqual:
7780 case OO_PercentEqual:
7781 case OO_CaretEqual:
7782 case OO_AmpEqual:
7783 case OO_PipeEqual:
7784 case OO_LessLess:
7785 case OO_GreaterGreater:
7786 case OO_LessLessEqual:
7787 case OO_GreaterGreaterEqual:
7788 case OO_EqualEqual:
7789 case OO_ExclaimEqual:
7790 case OO_PlusPlus:
7791 case OO_MinusMinus:
7792 case OO_Comma:
7793 case OO_ArrowStar:
7794 case OO_Arrow:
7795 case OO_Call:
7796 case OO_Subscript:
7797 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007798 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007799 case NUM_OVERLOADED_OPERATORS:
7800 llvm_unreachable("Unexpected reduction identifier");
7801 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007802 if (auto II = DN.getAsIdentifierInfo()) {
7803 if (II->isStr("max"))
7804 BOK = BO_GT;
7805 else if (II->isStr("min"))
7806 BOK = BO_LT;
7807 }
7808 break;
7809 }
7810 SourceRange ReductionIdRange;
7811 if (ReductionIdScopeSpec.isValid()) {
7812 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7813 }
7814 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7815 if (BOK == BO_Comma) {
7816 // Not allowed reduction identifier is found.
7817 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7818 << ReductionIdRange;
7819 return nullptr;
7820 }
7821
7822 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007823 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007824 SmallVector<Expr *, 8> LHSs;
7825 SmallVector<Expr *, 8> RHSs;
7826 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007827 for (auto RefExpr : VarList) {
7828 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7829 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7830 // It will be analyzed later.
7831 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007832 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007833 LHSs.push_back(nullptr);
7834 RHSs.push_back(nullptr);
7835 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007836 continue;
7837 }
7838
7839 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7840 RefExpr->isInstantiationDependent() ||
7841 RefExpr->containsUnexpandedParameterPack()) {
7842 // It will be analyzed later.
7843 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007844 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007845 LHSs.push_back(nullptr);
7846 RHSs.push_back(nullptr);
7847 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007848 continue;
7849 }
7850
7851 auto ELoc = RefExpr->getExprLoc();
7852 auto ERange = RefExpr->getSourceRange();
7853 // OpenMP [2.1, C/C++]
7854 // A list item is a variable or array section, subject to the restrictions
7855 // specified in Section 2.4 on page 42 and in each of the sections
7856 // describing clauses and directives for which a list appears.
7857 // OpenMP [2.14.3.3, Restrictions, p.1]
7858 // A variable that is part of another variable (as an array or
7859 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007860 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7861 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7862 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7863 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007864 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7865 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007866 continue;
7867 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007868 QualType Type;
7869 VarDecl *VD = nullptr;
7870 if (DE) {
7871 auto D = DE->getDecl();
7872 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007873 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007874 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007875 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007876 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7877 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7878 Base = TempASE->getBase()->IgnoreParenImpCasts();
7879 DE = dyn_cast<DeclRefExpr>(Base);
7880 if (DE)
7881 VD = dyn_cast<VarDecl>(DE->getDecl());
7882 if (!VD) {
7883 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7884 << 0 << Base->getSourceRange();
7885 continue;
7886 }
7887 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007888 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7889 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7890 Type = ATy->getElementType();
7891 else
7892 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007893 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007894 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7895 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7896 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7897 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7898 Base = TempASE->getBase()->IgnoreParenImpCasts();
7899 DE = dyn_cast<DeclRefExpr>(Base);
7900 if (DE)
7901 VD = dyn_cast<VarDecl>(DE->getDecl());
7902 if (!VD) {
7903 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7904 << 1 << Base->getSourceRange();
7905 continue;
7906 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007907 }
7908
Alexey Bataevc5e02582014-06-16 07:08:35 +00007909 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7910 // A variable that appears in a private clause must not have an incomplete
7911 // type or a reference type.
7912 if (RequireCompleteType(ELoc, Type,
7913 diag::err_omp_reduction_incomplete_type))
7914 continue;
7915 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007916 // A list item that appears in a reduction clause must not be
7917 // const-qualified.
7918 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007919 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007920 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007921 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007922 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7923 VarDecl::DeclarationOnly;
7924 Diag(VD->getLocation(),
7925 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7926 << VD;
7927 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007928 continue;
7929 }
7930 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7931 // If a list-item is a reference type then it must bind to the same object
7932 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007933 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007934 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007935 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007936 DSARefChecker Check(DSAStack);
7937 if (Check.Visit(VDDef->getInit())) {
7938 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7939 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7940 continue;
7941 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007942 }
7943 }
7944 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7945 // The type of a list item that appears in a reduction clause must be valid
7946 // for the reduction-identifier. For a max or min reduction in C, the type
7947 // of the list item must be an allowed arithmetic data type: char, int,
7948 // float, double, or _Bool, possibly modified with long, short, signed, or
7949 // unsigned. For a max or min reduction in C++, the type of the list item
7950 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7951 // double, or bool, possibly modified with long, short, signed, or unsigned.
7952 if ((BOK == BO_GT || BOK == BO_LT) &&
7953 !(Type->isScalarType() ||
7954 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7955 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7956 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007957 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007958 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7959 VarDecl::DeclarationOnly;
7960 Diag(VD->getLocation(),
7961 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7962 << VD;
7963 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007964 continue;
7965 }
7966 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7967 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7968 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007969 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007970 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7971 VarDecl::DeclarationOnly;
7972 Diag(VD->getLocation(),
7973 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7974 << VD;
7975 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007976 continue;
7977 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007978 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7979 // in a Construct]
7980 // Variables with the predetermined data-sharing attributes may not be
7981 // listed in data-sharing attributes clauses, except for the cases
7982 // listed below. For these exceptions only, listing a predetermined
7983 // variable in a data-sharing attribute clause is allowed and overrides
7984 // the variable's predetermined data-sharing attributes.
7985 // OpenMP [2.14.3.6, Restrictions, p.3]
7986 // Any number of reduction clauses can be specified on the directive,
7987 // but a list item can appear only once in the reduction clauses for that
7988 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007989 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007990 DVar = DSAStack->getTopDSA(VD, false);
7991 if (DVar.CKind == OMPC_reduction) {
7992 Diag(ELoc, diag::err_omp_once_referenced)
7993 << getOpenMPClauseName(OMPC_reduction);
7994 if (DVar.RefExpr) {
7995 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007996 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007997 } else if (DVar.CKind != OMPC_unknown) {
7998 Diag(ELoc, diag::err_omp_wrong_dsa)
7999 << getOpenMPClauseName(DVar.CKind)
8000 << getOpenMPClauseName(OMPC_reduction);
8001 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8002 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008003 }
8004
8005 // OpenMP [2.14.3.6, Restrictions, p.1]
8006 // A list item that appears in a reduction clause of a worksharing
8007 // construct must be shared in the parallel regions to which any of the
8008 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008009 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8010 if (isOpenMPWorksharingDirective(CurrDir) &&
8011 !isOpenMPParallelDirective(CurrDir)) {
8012 DVar = DSAStack->getImplicitDSA(VD, true);
8013 if (DVar.CKind != OMPC_shared) {
8014 Diag(ELoc, diag::err_omp_required_access)
8015 << getOpenMPClauseName(OMPC_reduction)
8016 << getOpenMPClauseName(OMPC_shared);
8017 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8018 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008019 }
8020 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008021
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008022 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008023 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
8024 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
8025 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
8026 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
8027 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008028 if (OASE ||
8029 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
8030 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008031 // Create pseudo array type for private copy. The size for this array will
8032 // be generated during codegen.
8033 // For array subscripts or single variables Private Ty is the same as Type
8034 // (type of the variable or single array element).
8035 PrivateTy = Context.getVariableArrayType(
8036 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8037 Context.getSizeType(), VK_RValue),
8038 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00008039 } else if (DE &&
8040 Context.getAsArrayType(VD->getType().getNonReferenceType()))
8041 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008042 // Private copy.
8043 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
8044 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008045 // Add initializer for private variable.
8046 Expr *Init = nullptr;
8047 switch (BOK) {
8048 case BO_Add:
8049 case BO_Xor:
8050 case BO_Or:
8051 case BO_LOr:
8052 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8053 if (Type->isScalarType() || Type->isAnyComplexType()) {
8054 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008055 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008056 break;
8057 case BO_Mul:
8058 case BO_LAnd:
8059 if (Type->isScalarType() || Type->isAnyComplexType()) {
8060 // '*' and '&&' reduction ops - initializer is '1'.
8061 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8062 }
8063 break;
8064 case BO_And: {
8065 // '&' reduction op - initializer is '~0'.
8066 QualType OrigType = Type;
8067 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
8068 Type = ComplexTy->getElementType();
8069 }
8070 if (Type->isRealFloatingType()) {
8071 llvm::APFloat InitValue =
8072 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8073 /*isIEEE=*/true);
8074 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8075 Type, ELoc);
8076 } else if (Type->isScalarType()) {
8077 auto Size = Context.getTypeSize(Type);
8078 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8079 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8080 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8081 }
8082 if (Init && OrigType->isAnyComplexType()) {
8083 // Init = 0xFFFF + 0xFFFFi;
8084 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8085 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8086 }
8087 Type = OrigType;
8088 break;
8089 }
8090 case BO_LT:
8091 case BO_GT: {
8092 // 'min' reduction op - initializer is 'Largest representable number in
8093 // the reduction list item type'.
8094 // 'max' reduction op - initializer is 'Least representable number in
8095 // the reduction list item type'.
8096 if (Type->isIntegerType() || Type->isPointerType()) {
8097 bool IsSigned = Type->hasSignedIntegerRepresentation();
8098 auto Size = Context.getTypeSize(Type);
8099 QualType IntTy =
8100 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8101 llvm::APInt InitValue =
8102 (BOK != BO_LT)
8103 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8104 : llvm::APInt::getMinValue(Size)
8105 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8106 : llvm::APInt::getMaxValue(Size);
8107 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8108 if (Type->isPointerType()) {
8109 // Cast to pointer type.
8110 auto CastExpr = BuildCStyleCastExpr(
8111 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8112 SourceLocation(), Init);
8113 if (CastExpr.isInvalid())
8114 continue;
8115 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008116 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008117 } else if (Type->isRealFloatingType()) {
8118 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8119 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8120 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8121 Type, ELoc);
8122 }
8123 break;
8124 }
8125 case BO_PtrMemD:
8126 case BO_PtrMemI:
8127 case BO_MulAssign:
8128 case BO_Div:
8129 case BO_Rem:
8130 case BO_Sub:
8131 case BO_Shl:
8132 case BO_Shr:
8133 case BO_LE:
8134 case BO_GE:
8135 case BO_EQ:
8136 case BO_NE:
8137 case BO_AndAssign:
8138 case BO_XorAssign:
8139 case BO_OrAssign:
8140 case BO_Assign:
8141 case BO_AddAssign:
8142 case BO_SubAssign:
8143 case BO_DivAssign:
8144 case BO_RemAssign:
8145 case BO_ShlAssign:
8146 case BO_ShrAssign:
8147 case BO_Comma:
8148 llvm_unreachable("Unexpected reduction operation");
8149 }
8150 if (Init) {
8151 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8152 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008153 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008154 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008155 if (!RHSVD->hasInit()) {
8156 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8157 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008158 if (VD) {
8159 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8160 VarDecl::DeclarationOnly;
8161 Diag(VD->getLocation(),
8162 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8163 << VD;
8164 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008165 continue;
8166 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008167 // Store initializer for single element in private copy. Will be used during
8168 // codegen.
8169 PrivateVD->setInit(RHSVD->getInit());
8170 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008171 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8172 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008173 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008174 ExprResult ReductionOp =
8175 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8176 LHSDRE, RHSDRE);
8177 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008178 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008179 ReductionOp =
8180 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8181 BO_Assign, LHSDRE, ReductionOp.get());
8182 } else {
8183 auto *ConditionalOp = new (Context) ConditionalOperator(
8184 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8185 RHSDRE, Type, VK_LValue, OK_Ordinary);
8186 ReductionOp =
8187 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8188 BO_Assign, LHSDRE, ConditionalOp);
8189 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008190 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008191 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008192 if (ReductionOp.isInvalid())
8193 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008194
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008195 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008196 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008197 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008198 LHSs.push_back(LHSDRE);
8199 RHSs.push_back(RHSDRE);
8200 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008201 }
8202
8203 if (Vars.empty())
8204 return nullptr;
8205
8206 return OMPReductionClause::Create(
8207 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008208 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8209 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008210}
8211
Alexey Bataev182227b2015-08-20 10:54:39 +00008212OMPClause *Sema::ActOnOpenMPLinearClause(
8213 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8214 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8215 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008216 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008217 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008218 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008219 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8220 LinKind == OMPC_LINEAR_unknown) {
8221 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8222 LinKind = OMPC_LINEAR_val;
8223 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008224 for (auto &RefExpr : VarList) {
8225 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8226 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008227 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008228 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008229 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008230 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008231 continue;
8232 }
8233
8234 // OpenMP [2.14.3.7, linear clause]
8235 // A list item that appears in a linear clause is subject to the private
8236 // clause semantics described in Section 2.14.3.3 on page 159 except as
8237 // noted. In addition, the value of the new list item on each iteration
8238 // of the associated loop(s) corresponds to the value of the original
8239 // list item before entering the construct plus the logical number of
8240 // the iteration times linear-step.
8241
Alexey Bataeved09d242014-05-28 05:53:51 +00008242 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008243 // OpenMP [2.1, C/C++]
8244 // A list item is a variable name.
8245 // OpenMP [2.14.3.3, Restrictions, p.1]
8246 // A variable that is part of another variable (as an array or
8247 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008248 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008249 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008250 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8251 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008252 continue;
8253 }
8254
8255 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8256
8257 // OpenMP [2.14.3.7, linear clause]
8258 // A list-item cannot appear in more than one linear clause.
8259 // A list-item that appears in a linear clause cannot appear in any
8260 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008261 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008262 if (DVar.RefExpr) {
8263 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8264 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008265 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008266 continue;
8267 }
8268
8269 QualType QType = VD->getType();
8270 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8271 // It will be analyzed later.
8272 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008273 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008274 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008275 continue;
8276 }
8277
8278 // A variable must not have an incomplete type or a reference type.
8279 if (RequireCompleteType(ELoc, QType,
8280 diag::err_omp_linear_incomplete_type)) {
8281 continue;
8282 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008283 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8284 !QType->isReferenceType()) {
8285 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8286 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8287 continue;
8288 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008289 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008290
8291 // A list item must not be const-qualified.
8292 if (QType.isConstant(Context)) {
8293 Diag(ELoc, diag::err_omp_const_variable)
8294 << getOpenMPClauseName(OMPC_linear);
8295 bool IsDecl =
8296 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8297 Diag(VD->getLocation(),
8298 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8299 << VD;
8300 continue;
8301 }
8302
8303 // A list item must be of integral or pointer type.
8304 QType = QType.getUnqualifiedType().getCanonicalType();
8305 const Type *Ty = QType.getTypePtrOrNull();
8306 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8307 !Ty->isPointerType())) {
8308 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8309 bool IsDecl =
8310 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8311 Diag(VD->getLocation(),
8312 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8313 << VD;
8314 continue;
8315 }
8316
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008317 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008318 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8319 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008320 auto *PrivateRef = buildDeclRefExpr(
8321 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008322 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008323 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008324 Expr *InitExpr;
8325 if (LinKind == OMPC_LINEAR_uval)
8326 InitExpr = VD->getInit();
8327 else
8328 InitExpr = DE;
8329 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008330 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008331 auto InitRef = buildDeclRefExpr(
8332 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008333 DSAStack->addDSA(VD, DE, OMPC_linear);
8334 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008335 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008336 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008337 }
8338
8339 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008340 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008341
8342 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008343 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008344 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8345 !Step->isInstantiationDependent() &&
8346 !Step->containsUnexpandedParameterPack()) {
8347 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008348 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008349 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008350 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008351 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008352
Alexander Musman3276a272015-03-21 10:12:56 +00008353 // Build var to save the step value.
8354 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008355 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008356 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008357 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008358 ExprResult CalcStep =
8359 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008360 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008361
Alexander Musman8dba6642014-04-22 13:09:42 +00008362 // Warn about zero linear step (it would be probably better specified as
8363 // making corresponding variables 'const').
8364 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008365 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8366 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008367 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8368 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008369 if (!IsConstant && CalcStep.isUsable()) {
8370 // Calculate the step beforehand instead of doing this on each iteration.
8371 // (This is not used if the number of iterations may be kfold-ed).
8372 CalcStepExpr = CalcStep.get();
8373 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008374 }
8375
Alexey Bataev182227b2015-08-20 10:54:39 +00008376 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8377 ColonLoc, EndLoc, Vars, Privates, Inits,
8378 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008379}
8380
8381static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8382 Expr *NumIterations, Sema &SemaRef,
8383 Scope *S) {
8384 // Walk the vars and build update/final expressions for the CodeGen.
8385 SmallVector<Expr *, 8> Updates;
8386 SmallVector<Expr *, 8> Finals;
8387 Expr *Step = Clause.getStep();
8388 Expr *CalcStep = Clause.getCalcStep();
8389 // OpenMP [2.14.3.7, linear clause]
8390 // If linear-step is not specified it is assumed to be 1.
8391 if (Step == nullptr)
8392 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8393 else if (CalcStep)
8394 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8395 bool HasErrors = false;
8396 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008397 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008398 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008399 for (auto &RefExpr : Clause.varlists()) {
8400 Expr *InitExpr = *CurInit;
8401
8402 // Build privatized reference to the current linear var.
8403 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008404 Expr *CapturedRef;
8405 if (LinKind == OMPC_LINEAR_uval)
8406 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8407 else
8408 CapturedRef =
8409 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8410 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8411 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008412
8413 // Build update: Var = InitExpr + IV * Step
8414 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008415 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008416 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008417 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8418 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008419
8420 // Build final: Var = InitExpr + NumIterations * Step
8421 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008422 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008423 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008424 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8425 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008426 if (!Update.isUsable() || !Final.isUsable()) {
8427 Updates.push_back(nullptr);
8428 Finals.push_back(nullptr);
8429 HasErrors = true;
8430 } else {
8431 Updates.push_back(Update.get());
8432 Finals.push_back(Final.get());
8433 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008434 ++CurInit;
8435 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008436 }
8437 Clause.setUpdates(Updates);
8438 Clause.setFinals(Finals);
8439 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008440}
8441
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008442OMPClause *Sema::ActOnOpenMPAlignedClause(
8443 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8444 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8445
8446 SmallVector<Expr *, 8> Vars;
8447 for (auto &RefExpr : VarList) {
8448 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8449 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8450 // It will be analyzed later.
8451 Vars.push_back(RefExpr);
8452 continue;
8453 }
8454
8455 SourceLocation ELoc = RefExpr->getExprLoc();
8456 // OpenMP [2.1, C/C++]
8457 // A list item is a variable name.
8458 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8459 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008460 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8461 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008462 continue;
8463 }
8464
8465 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8466
8467 // OpenMP [2.8.1, simd construct, Restrictions]
8468 // The type of list items appearing in the aligned clause must be
8469 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008470 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008471 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008472 const Type *Ty = QType.getTypePtrOrNull();
8473 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8474 !Ty->isPointerType())) {
8475 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8476 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8477 bool IsDecl =
8478 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8479 Diag(VD->getLocation(),
8480 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8481 << VD;
8482 continue;
8483 }
8484
8485 // OpenMP [2.8.1, simd construct, Restrictions]
8486 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008487 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008488 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8489 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8490 << getOpenMPClauseName(OMPC_aligned);
8491 continue;
8492 }
8493
8494 Vars.push_back(DE);
8495 }
8496
8497 // OpenMP [2.8.1, simd construct, Description]
8498 // The parameter of the aligned clause, alignment, must be a constant
8499 // positive integer expression.
8500 // If no optional parameter is specified, implementation-defined default
8501 // alignments for SIMD instructions on the target platforms are assumed.
8502 if (Alignment != nullptr) {
8503 ExprResult AlignResult =
8504 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8505 if (AlignResult.isInvalid())
8506 return nullptr;
8507 Alignment = AlignResult.get();
8508 }
8509 if (Vars.empty())
8510 return nullptr;
8511
8512 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8513 EndLoc, Vars, Alignment);
8514}
8515
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008516OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8517 SourceLocation StartLoc,
8518 SourceLocation LParenLoc,
8519 SourceLocation EndLoc) {
8520 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008521 SmallVector<Expr *, 8> SrcExprs;
8522 SmallVector<Expr *, 8> DstExprs;
8523 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008524 for (auto &RefExpr : VarList) {
8525 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8526 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008527 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008528 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008529 SrcExprs.push_back(nullptr);
8530 DstExprs.push_back(nullptr);
8531 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008532 continue;
8533 }
8534
Alexey Bataeved09d242014-05-28 05:53:51 +00008535 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008536 // OpenMP [2.1, C/C++]
8537 // A list item is a variable name.
8538 // OpenMP [2.14.4.1, Restrictions, p.1]
8539 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008540 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008541 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008542 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8543 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008544 continue;
8545 }
8546
8547 Decl *D = DE->getDecl();
8548 VarDecl *VD = cast<VarDecl>(D);
8549
8550 QualType Type = VD->getType();
8551 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8552 // It will be analyzed later.
8553 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008554 SrcExprs.push_back(nullptr);
8555 DstExprs.push_back(nullptr);
8556 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008557 continue;
8558 }
8559
8560 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8561 // A list item that appears in a copyin clause must be threadprivate.
8562 if (!DSAStack->isThreadPrivate(VD)) {
8563 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008564 << getOpenMPClauseName(OMPC_copyin)
8565 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008566 continue;
8567 }
8568
8569 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8570 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008571 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008572 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008573 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008574 auto *SrcVD =
8575 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8576 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008577 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008578 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8579 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008580 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8581 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008582 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008583 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008584 // For arrays generate assignment operation for single element and replace
8585 // it by the original array element in CodeGen.
8586 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8587 PseudoDstExpr, PseudoSrcExpr);
8588 if (AssignmentOp.isInvalid())
8589 continue;
8590 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8591 /*DiscardedValue=*/true);
8592 if (AssignmentOp.isInvalid())
8593 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008594
8595 DSAStack->addDSA(VD, DE, OMPC_copyin);
8596 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008597 SrcExprs.push_back(PseudoSrcExpr);
8598 DstExprs.push_back(PseudoDstExpr);
8599 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008600 }
8601
Alexey Bataeved09d242014-05-28 05:53:51 +00008602 if (Vars.empty())
8603 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008604
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008605 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8606 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008607}
8608
Alexey Bataevbae9a792014-06-27 10:37:06 +00008609OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8610 SourceLocation StartLoc,
8611 SourceLocation LParenLoc,
8612 SourceLocation EndLoc) {
8613 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008614 SmallVector<Expr *, 8> SrcExprs;
8615 SmallVector<Expr *, 8> DstExprs;
8616 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008617 for (auto &RefExpr : VarList) {
8618 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8619 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8620 // It will be analyzed later.
8621 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008622 SrcExprs.push_back(nullptr);
8623 DstExprs.push_back(nullptr);
8624 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008625 continue;
8626 }
8627
8628 SourceLocation ELoc = RefExpr->getExprLoc();
8629 // OpenMP [2.1, C/C++]
8630 // A list item is a variable name.
8631 // OpenMP [2.14.4.1, Restrictions, p.1]
8632 // A list item that appears in a copyin clause must be threadprivate.
8633 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8634 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008635 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8636 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008637 continue;
8638 }
8639
8640 Decl *D = DE->getDecl();
8641 VarDecl *VD = cast<VarDecl>(D);
8642
8643 QualType Type = VD->getType();
8644 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8645 // It will be analyzed later.
8646 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008647 SrcExprs.push_back(nullptr);
8648 DstExprs.push_back(nullptr);
8649 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008650 continue;
8651 }
8652
8653 // OpenMP [2.14.4.2, Restrictions, p.2]
8654 // A list item that appears in a copyprivate clause may not appear in a
8655 // private or firstprivate clause on the single construct.
8656 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008657 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008658 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8659 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008660 Diag(ELoc, diag::err_omp_wrong_dsa)
8661 << getOpenMPClauseName(DVar.CKind)
8662 << getOpenMPClauseName(OMPC_copyprivate);
8663 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8664 continue;
8665 }
8666
8667 // OpenMP [2.11.4.2, Restrictions, p.1]
8668 // All list items that appear in a copyprivate clause must be either
8669 // threadprivate or private in the enclosing context.
8670 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008671 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008672 if (DVar.CKind == OMPC_shared) {
8673 Diag(ELoc, diag::err_omp_required_access)
8674 << getOpenMPClauseName(OMPC_copyprivate)
8675 << "threadprivate or private in the enclosing context";
8676 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8677 continue;
8678 }
8679 }
8680 }
8681
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008682 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008683 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008684 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008685 << getOpenMPClauseName(OMPC_copyprivate) << Type
8686 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008687 bool IsDecl =
8688 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8689 Diag(VD->getLocation(),
8690 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8691 << VD;
8692 continue;
8693 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008694
Alexey Bataevbae9a792014-06-27 10:37:06 +00008695 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8696 // A variable of class type (or array thereof) that appears in a
8697 // copyin clause requires an accessible, unambiguous copy assignment
8698 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008699 Type = Context.getBaseElementType(Type.getNonReferenceType())
8700 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008701 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008702 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8703 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008704 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008705 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008706 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008707 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8708 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008709 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008710 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008711 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8712 PseudoDstExpr, PseudoSrcExpr);
8713 if (AssignmentOp.isInvalid())
8714 continue;
8715 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8716 /*DiscardedValue=*/true);
8717 if (AssignmentOp.isInvalid())
8718 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008719
8720 // No need to mark vars as copyprivate, they are already threadprivate or
8721 // implicitly private.
8722 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008723 SrcExprs.push_back(PseudoSrcExpr);
8724 DstExprs.push_back(PseudoDstExpr);
8725 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008726 }
8727
8728 if (Vars.empty())
8729 return nullptr;
8730
Alexey Bataeva63048e2015-03-23 06:18:07 +00008731 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8732 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008733}
8734
Alexey Bataev6125da92014-07-21 11:26:11 +00008735OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8736 SourceLocation StartLoc,
8737 SourceLocation LParenLoc,
8738 SourceLocation EndLoc) {
8739 if (VarList.empty())
8740 return nullptr;
8741
8742 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8743}
Alexey Bataevdea47612014-07-23 07:46:59 +00008744
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008745OMPClause *
8746Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8747 SourceLocation DepLoc, SourceLocation ColonLoc,
8748 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8749 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008750 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008751 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008752 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008753 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008754 return nullptr;
8755 }
8756 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008757 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8758 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008759 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008760 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008761 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8762 /*Last=*/OMPC_DEPEND_unknown, Except)
8763 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008764 return nullptr;
8765 }
8766 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008767 llvm::APSInt DepCounter(/*BitWidth=*/32);
8768 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8769 if (DepKind == OMPC_DEPEND_sink) {
8770 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8771 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8772 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008773 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008774 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008775 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8776 DSAStack->getParentOrderedRegionParam()) {
8777 for (auto &RefExpr : VarList) {
8778 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8779 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8780 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8781 // It will be analyzed later.
8782 Vars.push_back(RefExpr);
8783 continue;
8784 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008785
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008786 SourceLocation ELoc = RefExpr->getExprLoc();
8787 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8788 if (DepKind == OMPC_DEPEND_sink) {
8789 if (DepCounter >= TotalDepCount) {
8790 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8791 continue;
8792 }
8793 ++DepCounter;
8794 // OpenMP [2.13.9, Summary]
8795 // depend(dependence-type : vec), where dependence-type is:
8796 // 'sink' and where vec is the iteration vector, which has the form:
8797 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8798 // where n is the value specified by the ordered clause in the loop
8799 // directive, xi denotes the loop iteration variable of the i-th nested
8800 // loop associated with the loop directive, and di is a constant
8801 // non-negative integer.
8802 SimpleExpr = SimpleExpr->IgnoreImplicit();
8803 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8804 if (!DE) {
8805 OverloadedOperatorKind OOK = OO_None;
8806 SourceLocation OOLoc;
8807 Expr *LHS, *RHS;
8808 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8809 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8810 OOLoc = BO->getOperatorLoc();
8811 LHS = BO->getLHS()->IgnoreParenImpCasts();
8812 RHS = BO->getRHS()->IgnoreParenImpCasts();
8813 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8814 OOK = OCE->getOperator();
8815 OOLoc = OCE->getOperatorLoc();
8816 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8817 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8818 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8819 OOK = MCE->getMethodDecl()
8820 ->getNameInfo()
8821 .getName()
8822 .getCXXOverloadedOperator();
8823 OOLoc = MCE->getCallee()->getExprLoc();
8824 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8825 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8826 } else {
8827 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8828 continue;
8829 }
8830 DE = dyn_cast<DeclRefExpr>(LHS);
8831 if (!DE) {
8832 Diag(LHS->getExprLoc(),
8833 diag::err_omp_depend_sink_expected_loop_iteration)
8834 << DSAStack->getParentLoopControlVariable(
8835 DepCounter.getZExtValue());
8836 continue;
8837 }
8838 if (OOK != OO_Plus && OOK != OO_Minus) {
8839 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8840 continue;
8841 }
8842 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8843 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8844 if (Res.isInvalid())
8845 continue;
8846 }
8847 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8848 if (!CurContext->isDependentContext() &&
8849 DSAStack->getParentOrderedRegionParam() &&
8850 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8851 Diag(DE->getExprLoc(),
8852 diag::err_omp_depend_sink_expected_loop_iteration)
8853 << DSAStack->getParentLoopControlVariable(
8854 DepCounter.getZExtValue());
8855 continue;
8856 }
8857 } else {
8858 // OpenMP [2.11.1.1, Restrictions, p.3]
8859 // A variable that is part of another variable (such as a field of a
8860 // structure) but is not an array element or an array section cannot
8861 // appear in a depend clause.
8862 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8863 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8864 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8865 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8866 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008867 (ASE &&
8868 !ASE->getBase()
8869 ->getType()
8870 .getNonReferenceType()
8871 ->isPointerType() &&
8872 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008873 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8874 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008875 continue;
8876 }
8877 }
8878
8879 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8880 }
8881
8882 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8883 TotalDepCount > VarList.size() &&
8884 DSAStack->getParentOrderedRegionParam()) {
8885 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8886 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8887 }
8888 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8889 Vars.empty())
8890 return nullptr;
8891 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008892
8893 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8894 DepLoc, ColonLoc, Vars);
8895}
Michael Wonge710d542015-08-07 16:16:36 +00008896
8897OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8898 SourceLocation LParenLoc,
8899 SourceLocation EndLoc) {
8900 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008901
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008902 // OpenMP [2.9.1, Restrictions]
8903 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008904 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8905 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008906 return nullptr;
8907
Michael Wonge710d542015-08-07 16:16:36 +00008908 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8909}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008910
8911static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8912 DSAStackTy *Stack, CXXRecordDecl *RD) {
8913 if (!RD || RD->isInvalidDecl())
8914 return true;
8915
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008916 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8917 if (auto *CTD = CTSD->getSpecializedTemplate())
8918 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008919 auto QTy = SemaRef.Context.getRecordType(RD);
8920 if (RD->isDynamicClass()) {
8921 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8922 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8923 return false;
8924 }
8925 auto *DC = RD;
8926 bool IsCorrect = true;
8927 for (auto *I : DC->decls()) {
8928 if (I) {
8929 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8930 if (MD->isStatic()) {
8931 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8932 SemaRef.Diag(MD->getLocation(),
8933 diag::note_omp_static_member_in_target);
8934 IsCorrect = false;
8935 }
8936 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8937 if (VD->isStaticDataMember()) {
8938 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8939 SemaRef.Diag(VD->getLocation(),
8940 diag::note_omp_static_member_in_target);
8941 IsCorrect = false;
8942 }
8943 }
8944 }
8945 }
8946
8947 for (auto &I : RD->bases()) {
8948 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8949 I.getType()->getAsCXXRecordDecl()))
8950 IsCorrect = false;
8951 }
8952 return IsCorrect;
8953}
8954
8955static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8956 DSAStackTy *Stack, QualType QTy) {
8957 NamedDecl *ND;
8958 if (QTy->isIncompleteType(&ND)) {
8959 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8960 return false;
8961 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8962 if (!RD->isInvalidDecl() &&
8963 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8964 return false;
8965 }
8966 return true;
8967}
8968
Samuel Antao5de996e2016-01-22 20:21:36 +00008969// Return the expression of the base of the map clause or null if it cannot
8970// be determined and do all the necessary checks to see if the expression is
8971// valid as a standalone map clause expression.
8972static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8973 SourceLocation ELoc = E->getExprLoc();
8974 SourceRange ERange = E->getSourceRange();
8975
8976 // The base of elements of list in a map clause have to be either:
8977 // - a reference to variable or field.
8978 // - a member expression.
8979 // - an array expression.
8980 //
8981 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8982 // reference to 'r'.
8983 //
8984 // If we have:
8985 //
8986 // struct SS {
8987 // Bla S;
8988 // foo() {
8989 // #pragma omp target map (S.Arr[:12]);
8990 // }
8991 // }
8992 //
8993 // We want to retrieve the member expression 'this->S';
8994
8995 Expr *RelevantExpr = nullptr;
8996
8997 // Flags to help capture some memory
8998
8999 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9000 // If a list item is an array section, it must specify contiguous storage.
9001 //
9002 // For this restriction it is sufficient that we make sure only references
9003 // to variables or fields and array expressions, and that no array sections
9004 // exist except in the rightmost expression. E.g. these would be invalid:
9005 //
9006 // r.ArrS[3:5].Arr[6:7]
9007 //
9008 // r.ArrS[3:5].x
9009 //
9010 // but these would be valid:
9011 // r.ArrS[3].Arr[6:7]
9012 //
9013 // r.ArrS[3].x
9014
9015 bool IsRightMostExpression = true;
9016
9017 while (!RelevantExpr) {
9018 auto AllowArraySection = IsRightMostExpression;
9019 IsRightMostExpression = false;
9020
9021 E = E->IgnoreParenImpCasts();
9022
9023 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9024 if (!isa<VarDecl>(CurE->getDecl()))
9025 break;
9026
9027 RelevantExpr = CurE;
9028 continue;
9029 }
9030
9031 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9032 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9033
9034 if (isa<CXXThisExpr>(BaseE))
9035 // We found a base expression: this->Val.
9036 RelevantExpr = CurE;
9037 else
9038 E = BaseE;
9039
9040 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9041 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9042 << CurE->getSourceRange();
9043 break;
9044 }
9045
9046 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9047
9048 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9049 // A bit-field cannot appear in a map clause.
9050 //
9051 if (FD->isBitField()) {
9052 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9053 << CurE->getSourceRange();
9054 break;
9055 }
9056
9057 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9058 // If the type of a list item is a reference to a type T then the type
9059 // will be considered to be T for all purposes of this clause.
9060 QualType CurType = BaseE->getType();
9061 if (CurType->isReferenceType())
9062 CurType = CurType->getPointeeType();
9063
9064 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9065 // A list item cannot be a variable that is a member of a structure with
9066 // a union type.
9067 //
9068 if (auto *RT = CurType->getAs<RecordType>())
9069 if (RT->isUnionType()) {
9070 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9071 << CurE->getSourceRange();
9072 break;
9073 }
9074
9075 continue;
9076 }
9077
9078 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9079 E = CurE->getBase()->IgnoreParenImpCasts();
9080
9081 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9082 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9083 << 0 << CurE->getSourceRange();
9084 break;
9085 }
9086 continue;
9087 }
9088
9089 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9090 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9091 // If a list item is an element of a structure, only the rightmost symbol
9092 // of the variable reference can be an array section.
9093 //
9094 if (!AllowArraySection) {
9095 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9096 << CurE->getSourceRange();
9097 break;
9098 }
9099
9100 E = CurE->getBase()->IgnoreParenImpCasts();
9101
9102 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9103 // If the type of a list item is a reference to a type T then the type
9104 // will be considered to be T for all purposes of this clause.
9105 QualType CurType = E->getType();
9106 if (CurType->isReferenceType())
9107 CurType = CurType->getPointeeType();
9108
9109 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9110 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9111 << 0 << CurE->getSourceRange();
9112 break;
9113 }
9114
9115 continue;
9116 }
9117
9118 // If nothing else worked, this is not a valid map clause expression.
9119 SemaRef.Diag(ELoc,
9120 diag::err_omp_expected_named_var_member_or_array_expression)
9121 << ERange;
9122 break;
9123 }
9124
9125 return RelevantExpr;
9126}
9127
9128// Return true if expression E associated with value VD has conflicts with other
9129// map information.
9130static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9131 Expr *E, bool CurrentRegionOnly) {
9132 assert(VD && E);
9133
9134 // Types used to organize the components of a valid map clause.
9135 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9136 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9137
9138 // Helper to extract the components in the map clause expression E and store
9139 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9140 // it has already passed the single clause checks.
9141 auto ExtractMapExpressionComponents = [](Expr *TE,
9142 MapExpressionComponents &MEC) {
9143 while (true) {
9144 TE = TE->IgnoreParenImpCasts();
9145
9146 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9147 MEC.push_back(
9148 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9149 break;
9150 }
9151
9152 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9153 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9154
9155 MEC.push_back(MapExpressionComponent(
9156 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9157 if (isa<CXXThisExpr>(BaseE))
9158 break;
9159
9160 TE = BaseE;
9161 continue;
9162 }
9163
9164 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9165 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9166 TE = CurE->getBase()->IgnoreParenImpCasts();
9167 continue;
9168 }
9169
9170 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9171 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9172 TE = CurE->getBase()->IgnoreParenImpCasts();
9173 continue;
9174 }
9175
9176 llvm_unreachable(
9177 "Expecting only valid map clause expressions at this point!");
9178 }
9179 };
9180
9181 SourceLocation ELoc = E->getExprLoc();
9182 SourceRange ERange = E->getSourceRange();
9183
9184 // In order to easily check the conflicts we need to match each component of
9185 // the expression under test with the components of the expressions that are
9186 // already in the stack.
9187
9188 MapExpressionComponents CurComponents;
9189 ExtractMapExpressionComponents(E, CurComponents);
9190
9191 assert(!CurComponents.empty() && "Map clause expression with no components!");
9192 assert(CurComponents.back().second == VD &&
9193 "Map clause expression with unexpected base!");
9194
9195 // Variables to help detecting enclosing problems in data environment nests.
9196 bool IsEnclosedByDataEnvironmentExpr = false;
9197 Expr *EnclosingExpr = nullptr;
9198
9199 bool FoundError =
9200 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9201 MapExpressionComponents StackComponents;
9202 ExtractMapExpressionComponents(RE, StackComponents);
9203 assert(!StackComponents.empty() &&
9204 "Map clause expression with no components!");
9205 assert(StackComponents.back().second == VD &&
9206 "Map clause expression with unexpected base!");
9207
9208 // Expressions must start from the same base. Here we detect at which
9209 // point both expressions diverge from each other and see if we can
9210 // detect if the memory referred to both expressions is contiguous and
9211 // do not overlap.
9212 auto CI = CurComponents.rbegin();
9213 auto CE = CurComponents.rend();
9214 auto SI = StackComponents.rbegin();
9215 auto SE = StackComponents.rend();
9216 for (; CI != CE && SI != SE; ++CI, ++SI) {
9217
9218 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9219 // At most one list item can be an array item derived from a given
9220 // variable in map clauses of the same construct.
9221 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9222 isa<OMPArraySectionExpr>(CI->first)) &&
9223 (isa<ArraySubscriptExpr>(SI->first) ||
9224 isa<OMPArraySectionExpr>(SI->first))) {
9225 SemaRef.Diag(CI->first->getExprLoc(),
9226 diag::err_omp_multiple_array_items_in_map_clause)
9227 << CI->first->getSourceRange();
9228 ;
9229 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9230 << SI->first->getSourceRange();
9231 return true;
9232 }
9233
9234 // Do both expressions have the same kind?
9235 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9236 break;
9237
9238 // Are we dealing with different variables/fields?
9239 if (CI->second != SI->second)
9240 break;
9241 }
9242
9243 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9244 // List items of map clauses in the same construct must not share
9245 // original storage.
9246 //
9247 // If the expressions are exactly the same or one is a subset of the
9248 // other, it means they are sharing storage.
9249 if (CI == CE && SI == SE) {
9250 if (CurrentRegionOnly) {
9251 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9252 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9253 << RE->getSourceRange();
9254 return true;
9255 } else {
9256 // If we find the same expression in the enclosing data environment,
9257 // that is legal.
9258 IsEnclosedByDataEnvironmentExpr = true;
9259 return false;
9260 }
9261 }
9262
9263 QualType DerivedType = std::prev(CI)->first->getType();
9264 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9265
9266 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9267 // If the type of a list item is a reference to a type T then the type
9268 // will be considered to be T for all purposes of this clause.
9269 if (DerivedType->isReferenceType())
9270 DerivedType = DerivedType->getPointeeType();
9271
9272 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9273 // A variable for which the type is pointer and an array section
9274 // derived from that variable must not appear as list items of map
9275 // clauses of the same construct.
9276 //
9277 // Also, cover one of the cases in:
9278 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9279 // If any part of the original storage of a list item has corresponding
9280 // storage in the device data environment, all of the original storage
9281 // must have corresponding storage in the device data environment.
9282 //
9283 if (DerivedType->isAnyPointerType()) {
9284 if (CI == CE || SI == SE) {
9285 SemaRef.Diag(
9286 DerivedLoc,
9287 diag::err_omp_pointer_mapped_along_with_derived_section)
9288 << DerivedLoc;
9289 } else {
9290 assert(CI != CE && SI != SE);
9291 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9292 << DerivedLoc;
9293 }
9294 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9295 << RE->getSourceRange();
9296 return true;
9297 }
9298
9299 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9300 // List items of map clauses in the same construct must not share
9301 // original storage.
9302 //
9303 // An expression is a subset of the other.
9304 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9305 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9306 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9307 << RE->getSourceRange();
9308 return true;
9309 }
9310
9311 // The current expression uses the same base as other expression in the
9312 // data environment but does not contain it completelly.
9313 if (!CurrentRegionOnly && SI != SE)
9314 EnclosingExpr = RE;
9315
9316 // The current expression is a subset of the expression in the data
9317 // environment.
9318 IsEnclosedByDataEnvironmentExpr |=
9319 (!CurrentRegionOnly && CI != CE && SI == SE);
9320
9321 return false;
9322 });
9323
9324 if (CurrentRegionOnly)
9325 return FoundError;
9326
9327 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9328 // If any part of the original storage of a list item has corresponding
9329 // storage in the device data environment, all of the original storage must
9330 // have corresponding storage in the device data environment.
9331 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9332 // If a list item is an element of a structure, and a different element of
9333 // the structure has a corresponding list item in the device data environment
9334 // prior to a task encountering the construct associated with the map clause,
9335 // then the list item must also have a correspnding list item in the device
9336 // data environment prior to the task encountering the construct.
9337 //
9338 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9339 SemaRef.Diag(ELoc,
9340 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9341 << ERange;
9342 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9343 << EnclosingExpr->getSourceRange();
9344 return true;
9345 }
9346
9347 return FoundError;
9348}
9349
Samuel Antao23abd722016-01-19 20:40:49 +00009350OMPClause *
9351Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9352 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9353 SourceLocation MapLoc, SourceLocation ColonLoc,
9354 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9355 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009356 SmallVector<Expr *, 4> Vars;
9357
9358 for (auto &RE : VarList) {
9359 assert(RE && "Null expr in omp map");
9360 if (isa<DependentScopeDeclRefExpr>(RE)) {
9361 // It will be analyzed later.
9362 Vars.push_back(RE);
9363 continue;
9364 }
9365 SourceLocation ELoc = RE->getExprLoc();
9366
Kelvin Li0bff7af2015-11-23 05:32:03 +00009367 auto *VE = RE->IgnoreParenLValueCasts();
9368
9369 if (VE->isValueDependent() || VE->isTypeDependent() ||
9370 VE->isInstantiationDependent() ||
9371 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009372 // We can only analyze this information once the missing information is
9373 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009374 Vars.push_back(RE);
9375 continue;
9376 }
9377
9378 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009379
Samuel Antao5de996e2016-01-22 20:21:36 +00009380 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9381 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9382 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009383 continue;
9384 }
9385
Samuel Antao5de996e2016-01-22 20:21:36 +00009386 // Obtain the array or member expression bases if required.
9387 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9388 if (!BE)
9389 continue;
9390
9391 // If the base is a reference to a variable, we rely on that variable for
9392 // the following checks. If it is a 'this' expression we rely on the field.
9393 ValueDecl *D = nullptr;
9394 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9395 D = DRE->getDecl();
9396 } else {
9397 auto *ME = cast<MemberExpr>(BE);
9398 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9399 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009400 }
9401 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009402
Samuel Antao5de996e2016-01-22 20:21:36 +00009403 auto *VD = dyn_cast<VarDecl>(D);
9404 auto *FD = dyn_cast<FieldDecl>(D);
9405
9406 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009407 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009408
9409 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9410 // threadprivate variables cannot appear in a map clause.
9411 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009412 auto DVar = DSAStack->getTopDSA(VD, false);
9413 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9414 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9415 continue;
9416 }
9417
Samuel Antao5de996e2016-01-22 20:21:36 +00009418 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9419 // A list item cannot appear in both a map clause and a data-sharing
9420 // attribute clause on the same construct.
9421 //
9422 // TODO: Implement this check - it cannot currently be tested because of
9423 // missing implementation of the other data sharing clauses in target
9424 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009425
Samuel Antao5de996e2016-01-22 20:21:36 +00009426 // Check conflicts with other map clause expressions. We check the conflicts
9427 // with the current construct separately from the enclosing data
9428 // environment, because the restrictions are different.
9429 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9430 /*CurrentRegionOnly=*/true))
9431 break;
9432 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9433 /*CurrentRegionOnly=*/false))
9434 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009435
Samuel Antao5de996e2016-01-22 20:21:36 +00009436 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9437 // If the type of a list item is a reference to a type T then the type will
9438 // be considered to be T for all purposes of this clause.
9439 QualType Type = D->getType();
9440 if (Type->isReferenceType())
9441 Type = Type->getPointeeType();
9442
9443 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009444 // A list item must have a mappable type.
9445 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9446 DSAStack, Type))
9447 continue;
9448
Samuel Antaodf67fc42016-01-19 19:15:56 +00009449 // target enter data
9450 // OpenMP [2.10.2, Restrictions, p. 99]
9451 // A map-type must be specified in all map clauses and must be either
9452 // to or alloc.
9453 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9454 if (DKind == OMPD_target_enter_data &&
9455 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9456 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009457 << (IsMapTypeImplicit ? 1 : 0)
9458 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009459 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009460 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009461 }
9462
Samuel Antao72590762016-01-19 20:04:50 +00009463 // target exit_data
9464 // OpenMP [2.10.3, Restrictions, p. 102]
9465 // A map-type must be specified in all map clauses and must be either
9466 // from, release, or delete.
9467 DKind = DSAStack->getCurrentDirective();
9468 if (DKind == OMPD_target_exit_data &&
9469 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9470 MapType == OMPC_MAP_delete)) {
9471 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009472 << (IsMapTypeImplicit ? 1 : 0)
9473 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009474 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009475 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009476 }
9477
Kelvin Li0bff7af2015-11-23 05:32:03 +00009478 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009479 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009480 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009481
Samuel Antao5de996e2016-01-22 20:21:36 +00009482 // We need to produce a map clause even if we don't have variables so that
9483 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009484 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009485 MapTypeModifier, MapType, IsMapTypeImplicit,
9486 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009487}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009488
9489OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9490 SourceLocation StartLoc,
9491 SourceLocation LParenLoc,
9492 SourceLocation EndLoc) {
9493 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009494
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009495 // OpenMP [teams Constrcut, Restrictions]
9496 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009497 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9498 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009499 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009500
9501 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9502}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009503
9504OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9505 SourceLocation StartLoc,
9506 SourceLocation LParenLoc,
9507 SourceLocation EndLoc) {
9508 Expr *ValExpr = ThreadLimit;
9509
9510 // OpenMP [teams Constrcut, Restrictions]
9511 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009512 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9513 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009514 return nullptr;
9515
9516 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9517 EndLoc);
9518}
Alexey Bataeva0569352015-12-01 10:17:31 +00009519
9520OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9521 SourceLocation StartLoc,
9522 SourceLocation LParenLoc,
9523 SourceLocation EndLoc) {
9524 Expr *ValExpr = Priority;
9525
9526 // OpenMP [2.9.1, task Constrcut]
9527 // The priority-value is a non-negative numerical scalar expression.
9528 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9529 /*StrictlyPositive=*/false))
9530 return nullptr;
9531
9532 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9533}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009534
9535OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9536 SourceLocation StartLoc,
9537 SourceLocation LParenLoc,
9538 SourceLocation EndLoc) {
9539 Expr *ValExpr = Grainsize;
9540
9541 // OpenMP [2.9.2, taskloop Constrcut]
9542 // The parameter of the grainsize clause must be a positive integer
9543 // expression.
9544 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9545 /*StrictlyPositive=*/true))
9546 return nullptr;
9547
9548 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9549}
Alexey Bataev382967a2015-12-08 12:06:20 +00009550
9551OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9552 SourceLocation StartLoc,
9553 SourceLocation LParenLoc,
9554 SourceLocation EndLoc) {
9555 Expr *ValExpr = NumTasks;
9556
9557 // OpenMP [2.9.2, taskloop Constrcut]
9558 // The parameter of the num_tasks clause must be a positive integer
9559 // expression.
9560 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9561 /*StrictlyPositive=*/true))
9562 return nullptr;
9563
9564 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9565}
9566
Alexey Bataev28c75412015-12-15 08:19:24 +00009567OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9568 SourceLocation LParenLoc,
9569 SourceLocation EndLoc) {
9570 // OpenMP [2.13.2, critical construct, Description]
9571 // ... where hint-expression is an integer constant expression that evaluates
9572 // to a valid lock hint.
9573 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9574 if (HintExpr.isInvalid())
9575 return nullptr;
9576 return new (Context)
9577 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9578}
9579
Carlo Bertollib4adf552016-01-15 18:50:31 +00009580OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9581 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9582 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9583 SourceLocation EndLoc) {
9584 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9585 std::string Values;
9586 Values += "'";
9587 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9588 Values += "'";
9589 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9590 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9591 return nullptr;
9592 }
9593 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009594 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009595 if (ChunkSize) {
9596 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9597 !ChunkSize->isInstantiationDependent() &&
9598 !ChunkSize->containsUnexpandedParameterPack()) {
9599 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9600 ExprResult Val =
9601 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9602 if (Val.isInvalid())
9603 return nullptr;
9604
9605 ValExpr = Val.get();
9606
9607 // OpenMP [2.7.1, Restrictions]
9608 // chunk_size must be a loop invariant integer expression with a positive
9609 // value.
9610 llvm::APSInt Result;
9611 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9612 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9613 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9614 << "dist_schedule" << ChunkSize->getSourceRange();
9615 return nullptr;
9616 }
9617 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009618 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00009619 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
9620 HelperValStmt =
9621 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
9622 /*NumDecls=*/1),
9623 SourceLocation(), SourceLocation());
9624 ValExpr = DefaultLvalueConversion(ValExpr).get();
Carlo Bertollib4adf552016-01-15 18:50:31 +00009625 }
9626 }
9627 }
9628
9629 return new (Context)
9630 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +00009631 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +00009632}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009633
9634OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9635 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9636 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9637 SourceLocation KindLoc, SourceLocation EndLoc) {
9638 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9639 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9640 Kind != OMPC_DEFAULTMAP_scalar) {
9641 std::string Value;
9642 SourceLocation Loc;
9643 Value += "'";
9644 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9645 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9646 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9647 Loc = MLoc;
9648 } else {
9649 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9650 OMPC_DEFAULTMAP_scalar);
9651 Loc = KindLoc;
9652 }
9653 Value += "'";
9654 Diag(Loc, diag::err_omp_unexpected_clause_value)
9655 << Value << getOpenMPClauseName(OMPC_defaultmap);
9656 return nullptr;
9657 }
9658
9659 return new (Context)
9660 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9661}