blob: 3817704a1acb74f65401b8956d9352a426686e0f [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;
73 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Kelvin Li0bff7af2015-11-23 05:32:03 +000080public:
81 struct MapInfo {
82 Expr *RefExpr;
83 };
84
Alexey Bataev758e55e2013-09-06 18:03:48 +000085private:
86 struct DSAInfo {
87 OpenMPClauseKind Attributes;
88 DeclRefExpr *RefExpr;
89 };
90 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000092 typedef llvm::DenseMap<VarDecl *, unsigned> LoopControlVariablesMapTy;
Kelvin Li0bff7af2015-11-23 05:32:03 +000093 typedef llvm::SmallDenseMap<VarDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000094 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
95 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000096
97 struct SharingMapTy {
98 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000099 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000100 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000101 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 OpenMPDirectiveKind Directive;
105 DeclarationNameInfo DirectiveName;
106 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000108 /// \brief first argument (Expr *) contains optional argument of the
109 /// 'ordered' clause, the second one is true if the regions has 'ordered'
110 /// clause, false otherwise.
111 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000112 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000113 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000114 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000115 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000116 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000118 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000120 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000121 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000124 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000125 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000126 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 };
128
129 typedef SmallVector<SharingMapTy, 64> StackTy;
130
131 /// \brief Stack of used declaration and their data-sharing attributes.
132 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133 /// \brief true, if check for DSA must be from parent directive, false, if
134 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000135 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000136 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000138 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139
140 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
141
142 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000143
144 /// \brief Checks if the variable is a local for OpenMP region.
145 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000146
Alexey Bataev758e55e2013-09-06 18:03:48 +0000147public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000148 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000149 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
150 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000151
Alexey Bataevaac108a2015-06-23 04:51:00 +0000152 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
153 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000155 bool isForceVarCapturing() const { return ForceCapturing; }
156 void setForceVarCapturing(bool V) { ForceCapturing = V; }
157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000159 Scope *CurScope, SourceLocation Loc) {
160 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
161 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162 }
163
164 void pop() {
165 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
166 Stack.pop_back();
167 }
168
Alexey Bataev28c75412015-12-15 08:19:24 +0000169 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
170 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
171 }
172 const std::pair<OMPCriticalDirective *, llvm::APSInt>
173 getCriticalWithHint(const DeclarationNameInfo &Name) const {
174 auto I = Criticals.find(Name.getAsString());
175 if (I != Criticals.end())
176 return I->second;
177 return std::make_pair(nullptr, llvm::APSInt());
178 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000179 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000180 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// for diagnostics.
182 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
183
Alexey Bataev9c821032015-04-30 04:23:23 +0000184 /// \brief Register specified variable as loop control variable.
185 void addLoopControlVariable(VarDecl *D);
186 /// \brief Check if the specified variable is a loop control variable for
187 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000188 /// \return The index of the loop control variable in the list of associated
189 /// for-loops (from outer to inner).
190 unsigned isLoopControlVariable(VarDecl *D);
191 /// \brief Check if the specified variable is a loop control variable for
192 /// parent region.
193 /// \return The index of the loop control variable in the list of associated
194 /// for-loops (from outer to inner).
195 unsigned isParentLoopControlVariable(VarDecl *D);
196 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
197 /// parent directive.
198 VarDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000199
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200 /// \brief Adds explicit data sharing attribute to the specified declaration.
201 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
202
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data sharing attributes from top of the stack for the
204 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000205 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000207 DSAVarData getImplicitDSA(VarDecl *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>
212 DSAVarData hasDSA(VarDecl *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>
218 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000219 DirectivesPredicate DPred,
220 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000221 /// \brief Checks if the specified variables has explicit data-sharing
222 /// attributes which match specified \a CPred predicate at the specified
223 /// OpenMP region.
224 bool hasExplicitDSA(VarDecl *D,
225 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
226 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000227
228 /// \brief Returns true if the directive at level \Level matches in the
229 /// specified \a DPred predicate.
230 bool hasExplicitDirective(
231 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
232 unsigned Level);
233
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000234 /// \brief Finds a directive which matches specified \a DPred predicate.
235 template <class NamedDirectivesPredicate>
236 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000237
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238 /// \brief Returns currently analyzed directive.
239 OpenMPDirectiveKind getCurrentDirective() const {
240 return Stack.back().Directive;
241 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000242 /// \brief Returns parent directive.
243 OpenMPDirectiveKind getParentDirective() const {
244 if (Stack.size() > 2)
245 return Stack[Stack.size() - 2].Directive;
246 return OMPD_unknown;
247 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000248 /// \brief Return the directive associated with the provided scope.
249 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000250
251 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000252 void setDefaultDSANone(SourceLocation Loc) {
253 Stack.back().DefaultAttr = DSA_none;
254 Stack.back().DefaultAttrLoc = Loc;
255 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000257 void setDefaultDSAShared(SourceLocation Loc) {
258 Stack.back().DefaultAttr = DSA_shared;
259 Stack.back().DefaultAttrLoc = Loc;
260 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000261
262 DefaultDataSharingAttributes getDefaultDSA() const {
263 return Stack.back().DefaultAttr;
264 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000265 SourceLocation getDefaultDSALocation() const {
266 return Stack.back().DefaultAttrLoc;
267 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268
Alexey Bataevf29276e2014-06-18 04:14:57 +0000269 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000270 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000271 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000272 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000273 }
274
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000275 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000276 void setOrderedRegion(bool IsOrdered, Expr *Param) {
277 Stack.back().OrderedRegion.setInt(IsOrdered);
278 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000279 }
280 /// \brief Returns true, if parent region is ordered (has associated
281 /// 'ordered' clause), false - otherwise.
282 bool isParentOrderedRegion() const {
283 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000284 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000285 return false;
286 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000287 /// \brief Returns optional parameter for the ordered region.
288 Expr *getParentOrderedRegionParam() const {
289 if (Stack.size() > 2)
290 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
291 return nullptr;
292 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000293 /// \brief Marks current region as nowait (it has a 'nowait' clause).
294 void setNowaitRegion(bool IsNowait = true) {
295 Stack.back().NowaitRegion = IsNowait;
296 }
297 /// \brief Returns true, if parent region is nowait (has associated
298 /// 'nowait' clause), false - otherwise.
299 bool isParentNowaitRegion() const {
300 if (Stack.size() > 2)
301 return Stack[Stack.size() - 2].NowaitRegion;
302 return false;
303 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000304 /// \brief Marks parent region as cancel region.
305 void setParentCancelRegion(bool Cancel = true) {
306 if (Stack.size() > 2)
307 Stack[Stack.size() - 2].CancelRegion =
308 Stack[Stack.size() - 2].CancelRegion || Cancel;
309 }
310 /// \brief Return true if current region has inner cancel construct.
311 bool isCancelRegion() const {
312 return Stack.back().CancelRegion;
313 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000314
Alexey Bataev9c821032015-04-30 04:23:23 +0000315 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000316 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000317 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000318 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000319
Alexey Bataev13314bf2014-10-09 04:18:56 +0000320 /// \brief Marks current target region as one with closely nested teams
321 /// region.
322 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
323 if (Stack.size() > 2)
324 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
325 }
326 /// \brief Returns true, if current region has closely nested teams region.
327 bool hasInnerTeamsRegion() const {
328 return getInnerTeamsRegionLoc().isValid();
329 }
330 /// \brief Returns location of the nested teams region (if any).
331 SourceLocation getInnerTeamsRegionLoc() const {
332 if (Stack.size() > 1)
333 return Stack.back().InnerTeamsRegionLoc;
334 return SourceLocation();
335 }
336
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000337 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000338 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000339 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000340
341 MapInfo getMapInfoForVar(VarDecl *VD) {
342 MapInfo VarMI = {0};
343 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
344 if (Stack[Cnt].MappedDecls.count(VD)) {
345 VarMI = Stack[Cnt].MappedDecls[VD];
346 break;
347 }
348 }
349 return VarMI;
350 }
351
352 void addMapInfoForVar(VarDecl *VD, MapInfo MI) {
353 if (Stack.size() > 1) {
354 Stack.back().MappedDecls[VD] = MI;
355 }
356 }
357
358 MapInfo IsMappedInCurrentRegion(VarDecl *VD) {
359 assert(Stack.size() > 1 && "Target level is 0");
360 MapInfo VarMI = {0};
361 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
362 VarMI = Stack.back().MappedDecls[VD];
363 }
364 return VarMI;
365 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000367bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
368 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000369 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000370 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000371}
Alexey Bataeved09d242014-05-28 05:53:51 +0000372} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373
374DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
375 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000376 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000378 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000379 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
380 // in a region but not in construct]
381 // File-scope or namespace-scope variables referenced in called routines
382 // in the region are shared unless they appear in a threadprivate
383 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000384 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000385 DVar.CKind = OMPC_shared;
386
387 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
388 // in a region but not in construct]
389 // Variables with static storage duration that are declared in called
390 // routines in the region are shared.
391 if (D->hasGlobalStorage())
392 DVar.CKind = OMPC_shared;
393
Alexey Bataev758e55e2013-09-06 18:03:48 +0000394 return DVar;
395 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000396
Alexey Bataev758e55e2013-09-06 18:03:48 +0000397 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000398 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
399 // in a Construct, C/C++, predetermined, p.1]
400 // Variables with automatic storage duration that are declared in a scope
401 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000402 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
403 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
404 DVar.CKind = OMPC_private;
405 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000406 }
407
Alexey Bataev758e55e2013-09-06 18:03:48 +0000408 // Explicitly specified attributes and local variables with predetermined
409 // attributes.
410 if (Iter->SharingMap.count(D)) {
411 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
412 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000413 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 return DVar;
415 }
416
417 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
418 // in a Construct, C/C++, implicitly determined, p.1]
419 // In a parallel or task construct, the data-sharing attributes of these
420 // variables are determined by the default clause, if present.
421 switch (Iter->DefaultAttr) {
422 case DSA_shared:
423 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000424 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000425 return DVar;
426 case DSA_none:
427 return DVar;
428 case DSA_unspecified:
429 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
430 // in a Construct, implicitly determined, p.2]
431 // In a parallel construct, if no default clause is present, these
432 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000433 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000434 if (isOpenMPParallelDirective(DVar.DKind) ||
435 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.CKind = OMPC_shared;
437 return DVar;
438 }
439
440 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
441 // in a Construct, implicitly determined, p.4]
442 // In a task construct, if no default clause is present, a variable that in
443 // the enclosing context is determined to be shared by all implicit tasks
444 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 if (DVar.DKind == OMPD_task) {
446 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000447 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000448 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000449 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
450 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000451 // in a Construct, implicitly determined, p.6]
452 // In a task construct, if no default clause is present, a variable
453 // whose data-sharing attribute is not determined by the rules above is
454 // firstprivate.
455 DVarTemp = getDSA(I, D);
456 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000457 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000458 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000459 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000460 return DVar;
461 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000462 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000463 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000464 }
465 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000466 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000467 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000468 return DVar;
469 }
470 }
471 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
472 // in a Construct, implicitly determined, p.3]
473 // For constructs other than task, if no default clause is present, these
474 // variables inherit their data-sharing attributes from the enclosing
475 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000476 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477}
478
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000479DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
480 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000481 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000482 auto It = Stack.back().AlignedMap.find(D);
483 if (It == Stack.back().AlignedMap.end()) {
484 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
485 Stack.back().AlignedMap[D] = NewDE;
486 return nullptr;
487 } else {
488 assert(It->second && "Unexpected nullptr expr in the aligned map");
489 return It->second;
490 }
491 return nullptr;
492}
493
Alexey Bataev9c821032015-04-30 04:23:23 +0000494void DSAStackTy::addLoopControlVariable(VarDecl *D) {
495 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
496 D = D->getCanonicalDecl();
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000497 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000498}
499
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000500unsigned DSAStackTy::isLoopControlVariable(VarDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000501 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
502 D = D->getCanonicalDecl();
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000503 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
504}
505
506unsigned DSAStackTy::isParentLoopControlVariable(VarDecl *D) {
507 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
508 D = D->getCanonicalDecl();
509 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
510 ? Stack[Stack.size() - 2].LCVMap[D]
511 : 0;
512}
513
514VarDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
515 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
516 if (Stack[Stack.size() - 2].LCVMap.size() < I)
517 return nullptr;
518 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
519 if (Pair.second == I)
520 return Pair.first;
521 }
522 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000523}
524
Alexey Bataev758e55e2013-09-06 18:03:48 +0000525void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000526 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000527 if (A == OMPC_threadprivate) {
528 Stack[0].SharingMap[D].Attributes = A;
529 Stack[0].SharingMap[D].RefExpr = E;
530 } else {
531 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
532 Stack.back().SharingMap[D].Attributes = A;
533 Stack.back().SharingMap[D].RefExpr = E;
534 }
535}
536
Alexey Bataeved09d242014-05-28 05:53:51 +0000537bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000538 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000539 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000540 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000541 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000542 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000543 ++I;
544 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000545 if (I == E)
546 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000547 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000548 Scope *CurScope = getCurScope();
549 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000550 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000551 }
552 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000553 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000554 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555}
556
Alexey Bataev39f915b82015-05-08 10:41:21 +0000557/// \brief Build a variable declaration for OpenMP loop iteration variable.
558static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000559 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000560 DeclContext *DC = SemaRef.CurContext;
561 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
562 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
563 VarDecl *Decl =
564 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000565 if (Attrs) {
566 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
567 I != E; ++I)
568 Decl->addAttr(*I);
569 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000570 Decl->setImplicit();
571 return Decl;
572}
573
574static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
575 SourceLocation Loc,
576 bool RefersToCapture = false) {
577 D->setReferenced();
578 D->markUsed(S.Context);
579 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
580 SourceLocation(), D, RefersToCapture, Loc, Ty,
581 VK_LValue);
582}
583
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000584DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000585 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000586 DSAVarData DVar;
587
588 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
589 // in a Construct, C/C++, predetermined, p.1]
590 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000591 if ((D->getTLSKind() != VarDecl::TLS_None &&
592 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
593 SemaRef.getLangOpts().OpenMPUseTLS &&
594 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000595 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
596 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000597 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
598 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000599 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000600 }
601 if (Stack[0].SharingMap.count(D)) {
602 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
603 DVar.CKind = OMPC_threadprivate;
604 return DVar;
605 }
606
607 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000608 // in a Construct, C/C++, predetermined, p.4]
609 // Static data members are shared.
610 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
611 // in a Construct, C/C++, predetermined, p.7]
612 // Variables with static storage duration that are declared in a scope
613 // inside the construct are shared.
614 if (D->isStaticDataMember()) {
615 DSAVarData DVarTemp =
616 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
617 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000618 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000619
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000620 DVar.CKind = OMPC_shared;
621 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000622 }
623
624 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000625 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
626 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000627 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
628 // in a Construct, C/C++, predetermined, p.6]
629 // Variables with const qualified type having no mutable member are
630 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000631 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000632 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000633 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
634 if (auto *CTD = CTSD->getSpecializedTemplate())
635 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000636 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000637 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000638 // Variables with const-qualified type having no mutable member may be
639 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000640 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
641 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000642 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
643 return DVar;
644
Alexey Bataev758e55e2013-09-06 18:03:48 +0000645 DVar.CKind = OMPC_shared;
646 return DVar;
647 }
648
Alexey Bataev758e55e2013-09-06 18:03:48 +0000649 // Explicitly specified attributes and local variables with predetermined
650 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000651 auto StartI = std::next(Stack.rbegin());
652 auto EndI = std::prev(Stack.rend());
653 if (FromParent && StartI != EndI) {
654 StartI = std::next(StartI);
655 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000656 auto I = std::prev(StartI);
657 if (I->SharingMap.count(D)) {
658 DVar.RefExpr = I->SharingMap[D].RefExpr;
659 DVar.CKind = I->SharingMap[D].Attributes;
660 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000661 }
662
663 return DVar;
664}
665
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000666DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000667 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000668 auto StartI = Stack.rbegin();
669 auto EndI = std::prev(Stack.rend());
670 if (FromParent && StartI != EndI) {
671 StartI = std::next(StartI);
672 }
673 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674}
675
Alexey Bataevf29276e2014-06-18 04:14:57 +0000676template <class ClausesPredicate, class DirectivesPredicate>
677DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000678 DirectivesPredicate DPred,
679 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000680 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000681 auto StartI = std::next(Stack.rbegin());
682 auto EndI = std::prev(Stack.rend());
683 if (FromParent && StartI != EndI) {
684 StartI = std::next(StartI);
685 }
686 for (auto I = StartI, EE = EndI; I != EE; ++I) {
687 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000688 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000689 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000690 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000691 return DVar;
692 }
693 return DSAVarData();
694}
695
Alexey Bataevf29276e2014-06-18 04:14:57 +0000696template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000697DSAStackTy::DSAVarData
698DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
699 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000700 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000701 auto StartI = std::next(Stack.rbegin());
702 auto EndI = std::prev(Stack.rend());
703 if (FromParent && StartI != EndI) {
704 StartI = std::next(StartI);
705 }
706 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000707 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000708 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000709 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000710 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000711 return DVar;
712 return DSAVarData();
713 }
714 return DSAVarData();
715}
716
Alexey Bataevaac108a2015-06-23 04:51:00 +0000717bool DSAStackTy::hasExplicitDSA(
718 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
719 unsigned Level) {
720 if (CPred(ClauseKindMode))
721 return true;
722 if (isClauseParsingMode())
723 ++Level;
724 D = D->getCanonicalDecl();
725 auto StartI = Stack.rbegin();
726 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000727 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000728 return false;
729 std::advance(StartI, Level);
730 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
731 CPred(StartI->SharingMap[D].Attributes);
732}
733
Samuel Antao4be30e92015-10-02 17:14:03 +0000734bool DSAStackTy::hasExplicitDirective(
735 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
736 unsigned Level) {
737 if (isClauseParsingMode())
738 ++Level;
739 auto StartI = Stack.rbegin();
740 auto EndI = std::prev(Stack.rend());
741 if (std::distance(StartI, EndI) <= (int)Level)
742 return false;
743 std::advance(StartI, Level);
744 return DPred(StartI->Directive);
745}
746
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000747template <class NamedDirectivesPredicate>
748bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
749 auto StartI = std::next(Stack.rbegin());
750 auto EndI = std::prev(Stack.rend());
751 if (FromParent && StartI != EndI) {
752 StartI = std::next(StartI);
753 }
754 for (auto I = StartI, EE = EndI; I != EE; ++I) {
755 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
756 return true;
757 }
758 return false;
759}
760
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000761OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
762 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
763 if (I->CurScope == S)
764 return I->Directive;
765 return OMPD_unknown;
766}
767
Alexey Bataev758e55e2013-09-06 18:03:48 +0000768void Sema::InitDataSharingAttributesStack() {
769 VarDataSharingAttributesStack = new DSAStackTy(*this);
770}
771
772#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
773
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000774bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
775 const CapturedRegionScopeInfo *RSI) {
776 assert(LangOpts.OpenMP && "OpenMP is not allowed");
777
778 auto &Ctx = getASTContext();
779 bool IsByRef = true;
780
781 // Find the directive that is associated with the provided scope.
782 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
783 auto Ty = VD->getType();
784
785 if (isOpenMPTargetDirective(DKind)) {
786 // This table summarizes how a given variable should be passed to the device
787 // given its type and the clauses where it appears. This table is based on
788 // the description in OpenMP 4.5 [2.10.4, target Construct] and
789 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
790 //
791 // =========================================================================
792 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
793 // | |(tofrom:scalar)| | pvt | | | |
794 // =========================================================================
795 // | scl | | | | - | | bycopy|
796 // | scl | | - | x | - | - | bycopy|
797 // | scl | | x | - | - | - | null |
798 // | scl | x | | | - | | byref |
799 // | scl | x | - | x | - | - | bycopy|
800 // | scl | x | x | - | - | - | null |
801 // | scl | | - | - | - | x | byref |
802 // | scl | x | - | - | - | x | byref |
803 //
804 // | agg | n.a. | | | - | | byref |
805 // | agg | n.a. | - | x | - | - | byref |
806 // | agg | n.a. | x | - | - | - | null |
807 // | agg | n.a. | - | - | - | x | byref |
808 // | agg | n.a. | - | - | - | x[] | byref |
809 //
810 // | ptr | n.a. | | | - | | bycopy|
811 // | ptr | n.a. | - | x | - | - | bycopy|
812 // | ptr | n.a. | x | - | - | - | null |
813 // | ptr | n.a. | - | - | - | x | byref |
814 // | ptr | n.a. | - | - | - | x[] | bycopy|
815 // | ptr | n.a. | - | - | x | | bycopy|
816 // | ptr | n.a. | - | - | x | x | bycopy|
817 // | ptr | n.a. | - | - | x | x[] | bycopy|
818 // =========================================================================
819 // Legend:
820 // scl - scalar
821 // ptr - pointer
822 // agg - aggregate
823 // x - applies
824 // - - invalid in this combination
825 // [] - mapped with an array section
826 // byref - should be mapped by reference
827 // byval - should be mapped by value
828 // null - initialize a local variable to null on the device
829 //
830 // Observations:
831 // - All scalar declarations that show up in a map clause have to be passed
832 // by reference, because they may have been mapped in the enclosing data
833 // environment.
834 // - If the scalar value does not fit the size of uintptr, it has to be
835 // passed by reference, regardless the result in the table above.
836 // - For pointers mapped by value that have either an implicit map or an
837 // array section, the runtime library may pass the NULL value to the
838 // device instead of the value passed to it by the compiler.
839
840 // FIXME: Right now, only implicit maps are implemented. Properly mapping
841 // values requires having the map, private, and firstprivate clauses SEMA
842 // and parsing in place, which we don't yet.
843
844 if (Ty->isReferenceType())
845 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
846 IsByRef = !Ty->isScalarType();
847 }
848
849 // When passing data by value, we need to make sure it fits the uintptr size
850 // and alignment, because the runtime library only deals with uintptr types.
851 // If it does not fit the uintptr size, we need to pass the data by reference
852 // instead.
853 if (!IsByRef &&
854 (Ctx.getTypeSizeInChars(Ty) >
855 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
856 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
857 IsByRef = true;
858
859 return IsByRef;
860}
861
Alexey Bataevf841bd92014-12-16 07:00:22 +0000862bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
863 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000864 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000865
866 // If we are attempting to capture a global variable in a directive with
867 // 'target' we return true so that this global is also mapped to the device.
868 //
869 // FIXME: If the declaration is enclosed in a 'declare target' directive,
870 // then it should not be captured. Therefore, an extra check has to be
871 // inserted here once support for 'declare target' is added.
872 //
873 if (!VD->hasLocalStorage()) {
874 if (DSAStack->getCurrentDirective() == OMPD_target &&
875 !DSAStack->isClauseParsingMode()) {
876 return true;
877 }
878 if (DSAStack->getCurScope() &&
879 DSAStack->hasDirective(
880 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
881 SourceLocation Loc) -> bool {
882 return isOpenMPTargetDirective(K);
883 },
884 false)) {
885 return true;
886 }
887 }
888
Alexey Bataev48977c32015-08-04 08:10:48 +0000889 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
890 (!DSAStack->isClauseParsingMode() ||
891 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000892 if (DSAStack->isLoopControlVariable(VD) ||
893 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000894 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
895 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000896 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000897 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000898 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
899 return true;
900 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000901 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000902 return DVarPrivate.CKind != OMPC_unknown;
903 }
904 return false;
905}
906
Alexey Bataevaac108a2015-06-23 04:51:00 +0000907bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
908 assert(LangOpts.OpenMP && "OpenMP is not allowed");
909 return DSAStack->hasExplicitDSA(
910 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
911}
912
Samuel Antao4be30e92015-10-02 17:14:03 +0000913bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
914 assert(LangOpts.OpenMP && "OpenMP is not allowed");
915 // Return true if the current level is no longer enclosed in a target region.
916
917 return !VD->hasLocalStorage() &&
918 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
919}
920
Alexey Bataeved09d242014-05-28 05:53:51 +0000921void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000922
923void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
924 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000925 Scope *CurScope, SourceLocation Loc) {
926 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000927 PushExpressionEvaluationContext(PotentiallyEvaluated);
928}
929
Alexey Bataevaac108a2015-06-23 04:51:00 +0000930void Sema::StartOpenMPClause(OpenMPClauseKind K) {
931 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000932}
933
Alexey Bataevaac108a2015-06-23 04:51:00 +0000934void Sema::EndOpenMPClause() {
935 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000936}
937
Alexey Bataev758e55e2013-09-06 18:03:48 +0000938void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000939 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
940 // A variable of class type (or array thereof) that appears in a lastprivate
941 // clause requires an accessible, unambiguous default constructor for the
942 // class type, unless the list item is also specified in a firstprivate
943 // clause.
944 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000945 for (auto *C : D->clauses()) {
946 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
947 SmallVector<Expr *, 8> PrivateCopies;
948 for (auto *DE : Clause->varlists()) {
949 if (DE->isValueDependent() || DE->isTypeDependent()) {
950 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000951 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000952 }
953 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000954 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000955 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000956 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000957 // Generate helper private variable and initialize it with the
958 // default value. The address of the original variable is replaced
959 // by the address of the new private variable in CodeGen. This new
960 // variable is not added to IdResolver, so the code in the OpenMP
961 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000962 auto *VDPrivate = buildVarDecl(
963 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
964 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000965 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
966 if (VDPrivate->isInvalidDecl())
967 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000968 PrivateCopies.push_back(buildDeclRefExpr(
969 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000970 } else {
971 // The variable is also a firstprivate, so initialization sequence
972 // for private copy is generated already.
973 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000974 }
975 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000976 // Set initializers to private copies if no errors were found.
977 if (PrivateCopies.size() == Clause->varlist_size()) {
978 Clause->setPrivateCopies(PrivateCopies);
979 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000980 }
981 }
982 }
983
Alexey Bataev758e55e2013-09-06 18:03:48 +0000984 DSAStack->pop();
985 DiscardCleanupsInEvaluationContext();
986 PopExpressionEvaluationContext();
987}
988
Alexander Musman3276a272015-03-21 10:12:56 +0000989static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
990 Expr *NumIterations, Sema &SemaRef,
991 Scope *S);
992
Alexey Bataeva769e072013-03-22 06:34:35 +0000993namespace {
994
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000995class VarDeclFilterCCC : public CorrectionCandidateCallback {
996private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000997 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000998
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000999public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001000 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001001 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001002 NamedDecl *ND = Candidate.getCorrectionDecl();
1003 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1004 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001005 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1006 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001007 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001008 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001009 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001010};
Alexey Bataeved09d242014-05-28 05:53:51 +00001011} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012
1013ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1014 CXXScopeSpec &ScopeSpec,
1015 const DeclarationNameInfo &Id) {
1016 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1017 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1018
1019 if (Lookup.isAmbiguous())
1020 return ExprError();
1021
1022 VarDecl *VD;
1023 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001024 if (TypoCorrection Corrected = CorrectTypo(
1025 Id, LookupOrdinaryName, CurScope, nullptr,
1026 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001027 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001028 PDiag(Lookup.empty()
1029 ? diag::err_undeclared_var_use_suggest
1030 : diag::err_omp_expected_var_arg_suggest)
1031 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001032 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001033 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001034 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1035 : diag::err_omp_expected_var_arg)
1036 << Id.getName();
1037 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001038 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039 } else {
1040 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001041 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1043 return ExprError();
1044 }
1045 }
1046 Lookup.suppressDiagnostics();
1047
1048 // OpenMP [2.9.2, Syntax, C/C++]
1049 // Variables must be file-scope, namespace-scope, or static block-scope.
1050 if (!VD->hasGlobalStorage()) {
1051 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001052 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1053 bool IsDecl =
1054 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001055 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001056 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1057 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001058 return ExprError();
1059 }
1060
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001061 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1062 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001063 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1064 // A threadprivate directive for file-scope variables must appear outside
1065 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001066 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1067 !getCurLexicalContext()->isTranslationUnit()) {
1068 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001069 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1070 bool IsDecl =
1071 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1072 Diag(VD->getLocation(),
1073 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1074 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001075 return ExprError();
1076 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001077 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1078 // A threadprivate directive for static class member variables must appear
1079 // in the class definition, in the same scope in which the member
1080 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001081 if (CanonicalVD->isStaticDataMember() &&
1082 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1083 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001084 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1085 bool IsDecl =
1086 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1087 Diag(VD->getLocation(),
1088 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1089 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001090 return ExprError();
1091 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001092 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1093 // A threadprivate directive for namespace-scope variables must appear
1094 // outside any definition or declaration other than the namespace
1095 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001096 if (CanonicalVD->getDeclContext()->isNamespace() &&
1097 (!getCurLexicalContext()->isFileContext() ||
1098 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1099 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001100 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1101 bool IsDecl =
1102 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1103 Diag(VD->getLocation(),
1104 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1105 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001106 return ExprError();
1107 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001108 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1109 // A threadprivate directive for static block-scope variables must appear
1110 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001111 if (CanonicalVD->isStaticLocal() && CurScope &&
1112 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001113 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001114 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1115 bool IsDecl =
1116 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1117 Diag(VD->getLocation(),
1118 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1119 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001120 return ExprError();
1121 }
1122
1123 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1124 // A threadprivate directive must lexically precede all references to any
1125 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001126 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001127 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001128 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001129 return ExprError();
1130 }
1131
1132 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001133 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001134 return DE;
1135}
1136
Alexey Bataeved09d242014-05-28 05:53:51 +00001137Sema::DeclGroupPtrTy
1138Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1139 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001140 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001141 CurContext->addDecl(D);
1142 return DeclGroupPtrTy::make(DeclGroupRef(D));
1143 }
David Blaikie0403cb12016-01-15 23:43:25 +00001144 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001145}
1146
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001147namespace {
1148class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1149 Sema &SemaRef;
1150
1151public:
1152 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1153 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1154 if (VD->hasLocalStorage()) {
1155 SemaRef.Diag(E->getLocStart(),
1156 diag::err_omp_local_var_in_threadprivate_init)
1157 << E->getSourceRange();
1158 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1159 << VD << VD->getSourceRange();
1160 return true;
1161 }
1162 }
1163 return false;
1164 }
1165 bool VisitStmt(const Stmt *S) {
1166 for (auto Child : S->children()) {
1167 if (Child && Visit(Child))
1168 return true;
1169 }
1170 return false;
1171 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001172 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001173};
1174} // namespace
1175
Alexey Bataeved09d242014-05-28 05:53:51 +00001176OMPThreadPrivateDecl *
1177Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 for (auto &RefExpr : VarList) {
1180 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001181 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1182 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001183
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001184 QualType QType = VD->getType();
1185 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1186 // It will be analyzed later.
1187 Vars.push_back(DE);
1188 continue;
1189 }
1190
Alexey Bataeva769e072013-03-22 06:34:35 +00001191 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1192 // A threadprivate variable must not have an incomplete type.
1193 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001194 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001195 continue;
1196 }
1197
1198 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1199 // A threadprivate variable must not have a reference type.
1200 if (VD->getType()->isReferenceType()) {
1201 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001202 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1203 bool IsDecl =
1204 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1205 Diag(VD->getLocation(),
1206 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1207 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001208 continue;
1209 }
1210
Samuel Antaof8b50122015-07-13 22:54:53 +00001211 // Check if this is a TLS variable. If TLS is not being supported, produce
1212 // the corresponding diagnostic.
1213 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1214 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1215 getLangOpts().OpenMPUseTLS &&
1216 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001217 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1218 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001219 Diag(ILoc, diag::err_omp_var_thread_local)
1220 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001221 bool IsDecl =
1222 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1223 Diag(VD->getLocation(),
1224 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1225 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001226 continue;
1227 }
1228
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001229 // Check if initial value of threadprivate variable reference variable with
1230 // local storage (it is not supported by runtime).
1231 if (auto Init = VD->getAnyInitializer()) {
1232 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001233 if (Checker.Visit(Init))
1234 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001235 }
1236
Alexey Bataeved09d242014-05-28 05:53:51 +00001237 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001238 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001239 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1240 Context, SourceRange(Loc, Loc)));
1241 if (auto *ML = Context.getASTMutationListener())
1242 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001243 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001244 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001245 if (!Vars.empty()) {
1246 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1247 Vars);
1248 D->setAccess(AS_public);
1249 }
1250 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001251}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001252
Alexey Bataev7ff55242014-06-19 09:13:45 +00001253static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1254 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1255 bool IsLoopIterVar = false) {
1256 if (DVar.RefExpr) {
1257 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1258 << getOpenMPClauseName(DVar.CKind);
1259 return;
1260 }
1261 enum {
1262 PDSA_StaticMemberShared,
1263 PDSA_StaticLocalVarShared,
1264 PDSA_LoopIterVarPrivate,
1265 PDSA_LoopIterVarLinear,
1266 PDSA_LoopIterVarLastprivate,
1267 PDSA_ConstVarShared,
1268 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001269 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001270 PDSA_LocalVarPrivate,
1271 PDSA_Implicit
1272 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001273 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001274 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001275 if (IsLoopIterVar) {
1276 if (DVar.CKind == OMPC_private)
1277 Reason = PDSA_LoopIterVarPrivate;
1278 else if (DVar.CKind == OMPC_lastprivate)
1279 Reason = PDSA_LoopIterVarLastprivate;
1280 else
1281 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001282 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1283 Reason = PDSA_TaskVarFirstprivate;
1284 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001285 } else if (VD->isStaticLocal())
1286 Reason = PDSA_StaticLocalVarShared;
1287 else if (VD->isStaticDataMember())
1288 Reason = PDSA_StaticMemberShared;
1289 else if (VD->isFileVarDecl())
1290 Reason = PDSA_GlobalVarShared;
1291 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1292 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001293 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001294 ReportHint = true;
1295 Reason = PDSA_LocalVarPrivate;
1296 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001297 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001298 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001299 << Reason << ReportHint
1300 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1301 } else if (DVar.ImplicitDSALoc.isValid()) {
1302 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1303 << getOpenMPClauseName(DVar.CKind);
1304 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001305}
1306
Alexey Bataev758e55e2013-09-06 18:03:48 +00001307namespace {
1308class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1309 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001311 bool ErrorFound;
1312 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001313 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001314 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001315
Alexey Bataev758e55e2013-09-06 18:03:48 +00001316public:
1317 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001319 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001320 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1321 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001322
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001323 auto DVar = Stack->getTopDSA(VD, false);
1324 // Check if the variable has explicit DSA set and stop analysis if it so.
1325 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001326
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001327 auto ELoc = E->getExprLoc();
1328 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001329 // The default(none) clause requires that each variable that is referenced
1330 // in the construct, and does not have a predetermined data-sharing
1331 // attribute, must have its data-sharing attribute explicitly determined
1332 // by being listed in a data-sharing attribute clause.
1333 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001334 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001335 VarsWithInheritedDSA.count(VD) == 0) {
1336 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001337 return;
1338 }
1339
1340 // OpenMP [2.9.3.6, Restrictions, p.2]
1341 // A list item that appears in a reduction clause of the innermost
1342 // enclosing worksharing or parallel construct may not be accessed in an
1343 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001344 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001345 [](OpenMPDirectiveKind K) -> bool {
1346 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001347 isOpenMPWorksharingDirective(K) ||
1348 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001349 },
1350 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001351 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1352 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001353 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1354 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001355 return;
1356 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001357
1358 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001359 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001360 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001361 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001362 }
1363 }
1364 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001365 for (auto *C : S->clauses()) {
1366 // Skip analysis of arguments of implicitly defined firstprivate clause
1367 // for task directives.
1368 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1369 for (auto *CC : C->children()) {
1370 if (CC)
1371 Visit(CC);
1372 }
1373 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374 }
1375 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 for (auto *C : S->children()) {
1377 if (C && !isa<OMPExecutableDirective>(C))
1378 Visit(C);
1379 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001380 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001381
1382 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001383 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001384 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1385 return VarsWithInheritedDSA;
1386 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001387
Alexey Bataev7ff55242014-06-19 09:13:45 +00001388 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1389 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001390};
Alexey Bataeved09d242014-05-28 05:53:51 +00001391} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001392
Alexey Bataevbae9a792014-06-27 10:37:06 +00001393void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001394 switch (DKind) {
1395 case OMPD_parallel: {
1396 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001397 QualType KmpInt32PtrTy =
1398 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001399 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001400 std::make_pair(".global_tid.", KmpInt32PtrTy),
1401 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1402 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001403 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001404 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1405 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001406 break;
1407 }
1408 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001409 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001410 std::make_pair(StringRef(), QualType()) // __context with shared vars
1411 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001412 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1413 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001414 break;
1415 }
1416 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001417 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001418 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001419 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001420 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1421 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001422 break;
1423 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001424 case OMPD_for_simd: {
1425 Sema::CapturedParamNameType Params[] = {
1426 std::make_pair(StringRef(), QualType()) // __context with shared vars
1427 };
1428 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1429 Params);
1430 break;
1431 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001432 case OMPD_sections: {
1433 Sema::CapturedParamNameType Params[] = {
1434 std::make_pair(StringRef(), QualType()) // __context with shared vars
1435 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001436 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1437 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001438 break;
1439 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001440 case OMPD_section: {
1441 Sema::CapturedParamNameType Params[] = {
1442 std::make_pair(StringRef(), QualType()) // __context with shared vars
1443 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001444 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1445 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001446 break;
1447 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001448 case OMPD_single: {
1449 Sema::CapturedParamNameType Params[] = {
1450 std::make_pair(StringRef(), QualType()) // __context with shared vars
1451 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001452 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1453 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001454 break;
1455 }
Alexander Musman80c22892014-07-17 08:54:58 +00001456 case OMPD_master: {
1457 Sema::CapturedParamNameType Params[] = {
1458 std::make_pair(StringRef(), QualType()) // __context with shared vars
1459 };
1460 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1461 Params);
1462 break;
1463 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001464 case OMPD_critical: {
1465 Sema::CapturedParamNameType Params[] = {
1466 std::make_pair(StringRef(), QualType()) // __context with shared vars
1467 };
1468 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1469 Params);
1470 break;
1471 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001472 case OMPD_parallel_for: {
1473 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001474 QualType KmpInt32PtrTy =
1475 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001476 Sema::CapturedParamNameType Params[] = {
1477 std::make_pair(".global_tid.", KmpInt32PtrTy),
1478 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1479 std::make_pair(StringRef(), QualType()) // __context with shared vars
1480 };
1481 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1482 Params);
1483 break;
1484 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001485 case OMPD_parallel_for_simd: {
1486 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001487 QualType KmpInt32PtrTy =
1488 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001489 Sema::CapturedParamNameType Params[] = {
1490 std::make_pair(".global_tid.", KmpInt32PtrTy),
1491 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1492 std::make_pair(StringRef(), QualType()) // __context with shared vars
1493 };
1494 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1495 Params);
1496 break;
1497 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001498 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001499 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001500 QualType KmpInt32PtrTy =
1501 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001502 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001503 std::make_pair(".global_tid.", KmpInt32PtrTy),
1504 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
1507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
1509 break;
1510 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001511 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001512 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001513 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1514 FunctionProtoType::ExtProtoInfo EPI;
1515 EPI.Variadic = true;
1516 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001517 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001518 std::make_pair(".global_tid.", KmpInt32Ty),
1519 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001520 std::make_pair(".privates.",
1521 Context.VoidPtrTy.withConst().withRestrict()),
1522 std::make_pair(
1523 ".copy_fn.",
1524 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001525 std::make_pair(StringRef(), QualType()) // __context with shared vars
1526 };
1527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1528 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001529 // Mark this captured region as inlined, because we don't use outlined
1530 // function directly.
1531 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1532 AlwaysInlineAttr::CreateImplicit(
1533 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001534 break;
1535 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001536 case OMPD_ordered: {
1537 Sema::CapturedParamNameType Params[] = {
1538 std::make_pair(StringRef(), QualType()) // __context with shared vars
1539 };
1540 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1541 Params);
1542 break;
1543 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001544 case OMPD_atomic: {
1545 Sema::CapturedParamNameType Params[] = {
1546 std::make_pair(StringRef(), QualType()) // __context with shared vars
1547 };
1548 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1549 Params);
1550 break;
1551 }
Michael Wong65f367f2015-07-21 13:44:28 +00001552 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001553 case OMPD_target: {
1554 Sema::CapturedParamNameType Params[] = {
1555 std::make_pair(StringRef(), QualType()) // __context with shared vars
1556 };
1557 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1558 Params);
1559 break;
1560 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001561 case OMPD_teams: {
1562 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001563 QualType KmpInt32PtrTy =
1564 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001565 Sema::CapturedParamNameType Params[] = {
1566 std::make_pair(".global_tid.", KmpInt32PtrTy),
1567 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1568 std::make_pair(StringRef(), QualType()) // __context with shared vars
1569 };
1570 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1571 Params);
1572 break;
1573 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001574 case OMPD_taskgroup: {
1575 Sema::CapturedParamNameType Params[] = {
1576 std::make_pair(StringRef(), QualType()) // __context with shared vars
1577 };
1578 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1579 Params);
1580 break;
1581 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001582 case OMPD_taskloop: {
1583 Sema::CapturedParamNameType Params[] = {
1584 std::make_pair(StringRef(), QualType()) // __context with shared vars
1585 };
1586 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1587 Params);
1588 break;
1589 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001590 case OMPD_taskloop_simd: {
1591 Sema::CapturedParamNameType Params[] = {
1592 std::make_pair(StringRef(), QualType()) // __context with shared vars
1593 };
1594 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1595 Params);
1596 break;
1597 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001598 case OMPD_distribute: {
1599 Sema::CapturedParamNameType Params[] = {
1600 std::make_pair(StringRef(), QualType()) // __context with shared vars
1601 };
1602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1603 Params);
1604 break;
1605 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001606 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001607 case OMPD_taskyield:
1608 case OMPD_barrier:
1609 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001610 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001611 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001612 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001613 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001614 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001615 llvm_unreachable("OpenMP Directive is not allowed");
1616 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001617 llvm_unreachable("Unknown OpenMP directive");
1618 }
1619}
1620
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001621StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1622 ArrayRef<OMPClause *> Clauses) {
1623 if (!S.isUsable()) {
1624 ActOnCapturedRegionError();
1625 return StmtError();
1626 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001627
1628 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001629 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001630 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001631 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001632 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001633 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001634 Clause->getClauseKind() == OMPC_copyprivate ||
1635 (getLangOpts().OpenMPUseTLS &&
1636 getASTContext().getTargetInfo().isTLSSupported() &&
1637 Clause->getClauseKind() == OMPC_copyin)) {
1638 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001639 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001640 for (auto *VarRef : Clause->children()) {
1641 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001642 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001643 }
1644 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001645 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001646 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1647 Clause->getClauseKind() == OMPC_schedule) {
1648 // Mark all variables in private list clauses as used in inner region.
1649 // Required for proper codegen of combined directives.
1650 // TODO: add processing for other clauses.
1651 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001652 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1653 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001654 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001655 if (Clause->getClauseKind() == OMPC_schedule)
1656 SC = cast<OMPScheduleClause>(Clause);
1657 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001658 OC = cast<OMPOrderedClause>(Clause);
1659 else if (Clause->getClauseKind() == OMPC_linear)
1660 LCs.push_back(cast<OMPLinearClause>(Clause));
1661 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001662 bool ErrorFound = false;
1663 // OpenMP, 2.7.1 Loop Construct, Restrictions
1664 // The nonmonotonic modifier cannot be specified if an ordered clause is
1665 // specified.
1666 if (SC &&
1667 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1668 SC->getSecondScheduleModifier() ==
1669 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1670 OC) {
1671 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1672 ? SC->getFirstScheduleModifierLoc()
1673 : SC->getSecondScheduleModifierLoc(),
1674 diag::err_omp_schedule_nonmonotonic_ordered)
1675 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1676 ErrorFound = true;
1677 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001678 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1679 for (auto *C : LCs) {
1680 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1681 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1682 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001683 ErrorFound = true;
1684 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001685 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1686 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1687 OC->getNumForLoops()) {
1688 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1689 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1690 ErrorFound = true;
1691 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001692 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001693 ActOnCapturedRegionError();
1694 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001695 }
1696 return ActOnCapturedRegionEnd(S.get());
1697}
1698
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001699static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1700 OpenMPDirectiveKind CurrentRegion,
1701 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001702 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001703 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001704 // Allowed nesting of constructs
1705 // +------------------+-----------------+------------------------------------+
1706 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1707 // +------------------+-----------------+------------------------------------+
1708 // | parallel | parallel | * |
1709 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001710 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001711 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001712 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001713 // | parallel | simd | * |
1714 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001715 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001716 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001717 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001718 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001719 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001720 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001721 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001722 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001723 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001724 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001725 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001726 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001727 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001728 // | parallel | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001729 // | parallel | target enter | * |
1730 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001731 // | parallel | target exit | * |
1732 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001733 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001734 // | parallel | cancellation | |
1735 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001736 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001737 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001738 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001739 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001740 // +------------------+-----------------+------------------------------------+
1741 // | for | parallel | * |
1742 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001743 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001744 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001745 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001746 // | for | simd | * |
1747 // | for | sections | + |
1748 // | for | section | + |
1749 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001750 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001751 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001752 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001753 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001754 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001755 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001756 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001757 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001758 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001759 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001760 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001761 // | for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001762 // | for | target enter | * |
1763 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001764 // | for | target exit | * |
1765 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001766 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001767 // | for | cancellation | |
1768 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001769 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001770 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001771 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001772 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001773 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001774 // | master | parallel | * |
1775 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001776 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001777 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001778 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001779 // | master | simd | * |
1780 // | master | sections | + |
1781 // | master | section | + |
1782 // | master | single | + |
1783 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001784 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001785 // | master |parallel sections| * |
1786 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001787 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001788 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001789 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001790 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001791 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001792 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001793 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001794 // | master | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001795 // | master | target enter | * |
1796 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001797 // | master | target exit | * |
1798 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001799 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001800 // | master | cancellation | |
1801 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001802 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001803 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001804 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001805 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001806 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001807 // | critical | parallel | * |
1808 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001809 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001810 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001811 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001812 // | critical | simd | * |
1813 // | critical | sections | + |
1814 // | critical | section | + |
1815 // | critical | single | + |
1816 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001817 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001818 // | critical |parallel sections| * |
1819 // | critical | task | * |
1820 // | critical | taskyield | * |
1821 // | critical | barrier | + |
1822 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001823 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001824 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001825 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001826 // | critical | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001827 // | critical | target enter | * |
1828 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001829 // | critical | target exit | * |
1830 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001831 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001832 // | critical | cancellation | |
1833 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001834 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001835 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001836 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001837 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001838 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001839 // | simd | parallel | |
1840 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001841 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001842 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001843 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001844 // | simd | simd | |
1845 // | simd | sections | |
1846 // | simd | section | |
1847 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001848 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001849 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001850 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001851 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001852 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001853 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001854 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001855 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001856 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001857 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001858 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001859 // | simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001860 // | simd | target enter | |
1861 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001862 // | simd | target exit | |
1863 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001864 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001865 // | simd | cancellation | |
1866 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001867 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001868 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001869 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001870 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001871 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001872 // | for simd | parallel | |
1873 // | for simd | for | |
1874 // | for simd | for simd | |
1875 // | for simd | master | |
1876 // | for simd | critical | |
1877 // | for simd | simd | |
1878 // | for simd | sections | |
1879 // | for simd | section | |
1880 // | for simd | single | |
1881 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001882 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001883 // | for simd |parallel sections| |
1884 // | for simd | task | |
1885 // | for simd | taskyield | |
1886 // | for simd | barrier | |
1887 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001888 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001889 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001890 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001891 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001892 // | for simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001893 // | for simd | target enter | |
1894 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001895 // | for simd | target exit | |
1896 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001897 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001898 // | for simd | cancellation | |
1899 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001900 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001901 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001902 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001903 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001904 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001905 // | parallel for simd| parallel | |
1906 // | parallel for simd| for | |
1907 // | parallel for simd| for simd | |
1908 // | parallel for simd| master | |
1909 // | parallel for simd| critical | |
1910 // | parallel for simd| simd | |
1911 // | parallel for simd| sections | |
1912 // | parallel for simd| section | |
1913 // | parallel for simd| single | |
1914 // | parallel for simd| parallel for | |
1915 // | parallel for simd|parallel for simd| |
1916 // | parallel for simd|parallel sections| |
1917 // | parallel for simd| task | |
1918 // | parallel for simd| taskyield | |
1919 // | parallel for simd| barrier | |
1920 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001921 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001922 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001923 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001924 // | parallel for simd| atomic | |
1925 // | parallel for simd| target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001926 // | parallel for simd| target enter | |
1927 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001928 // | parallel for simd| target exit | |
1929 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001930 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001931 // | parallel for simd| cancellation | |
1932 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001933 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001934 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001935 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001936 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001937 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001938 // | sections | parallel | * |
1939 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001940 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001941 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001942 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001943 // | sections | simd | * |
1944 // | sections | sections | + |
1945 // | sections | section | * |
1946 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001947 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001948 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001949 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001950 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001951 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001952 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001953 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001954 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001955 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001956 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001957 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001958 // | sections | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001959 // | sections | target enter | * |
1960 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001961 // | sections | target exit | * |
1962 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001963 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001964 // | sections | cancellation | |
1965 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001966 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001967 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001968 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001969 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001970 // +------------------+-----------------+------------------------------------+
1971 // | section | parallel | * |
1972 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001973 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001974 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001975 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001976 // | section | simd | * |
1977 // | section | sections | + |
1978 // | section | section | + |
1979 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001980 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001981 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001982 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001983 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001984 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001985 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001986 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001987 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001988 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001989 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001990 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001991 // | section | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001992 // | section | target enter | * |
1993 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001994 // | section | target exit | * |
1995 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001996 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001997 // | section | cancellation | |
1998 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001999 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002000 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002001 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002002 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002003 // +------------------+-----------------+------------------------------------+
2004 // | single | parallel | * |
2005 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002006 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002007 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002008 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002009 // | single | simd | * |
2010 // | single | sections | + |
2011 // | single | section | + |
2012 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002013 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002014 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002015 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002016 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002017 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002018 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002019 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002020 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002021 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002022 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002023 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002024 // | single | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002025 // | single | target enter | * |
2026 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002027 // | single | target exit | * |
2028 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002029 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002030 // | single | cancellation | |
2031 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002032 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002033 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002034 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002035 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002036 // +------------------+-----------------+------------------------------------+
2037 // | parallel for | parallel | * |
2038 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002039 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002040 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002041 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002042 // | parallel for | simd | * |
2043 // | parallel for | sections | + |
2044 // | parallel for | section | + |
2045 // | parallel for | single | + |
2046 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002047 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002048 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002049 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002050 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002051 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002052 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002053 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002054 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002055 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002056 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002057 // | parallel for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002058 // | parallel for | target enter | * |
2059 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002060 // | parallel for | target exit | * |
2061 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002062 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002063 // | parallel for | cancellation | |
2064 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002065 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002066 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002067 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002068 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002069 // +------------------+-----------------+------------------------------------+
2070 // | parallel sections| parallel | * |
2071 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002072 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002073 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002074 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002075 // | parallel sections| simd | * |
2076 // | parallel sections| sections | + |
2077 // | parallel sections| section | * |
2078 // | parallel sections| single | + |
2079 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002080 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002081 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002082 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002083 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002084 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002085 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002086 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002087 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002088 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002089 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002090 // | parallel sections| target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002091 // | parallel sections| target enter | * |
2092 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002093 // | parallel sections| target exit | * |
2094 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002095 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002096 // | parallel sections| cancellation | |
2097 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002098 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002099 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002100 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002101 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002102 // +------------------+-----------------+------------------------------------+
2103 // | task | parallel | * |
2104 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002105 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002106 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002107 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002108 // | task | simd | * |
2109 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002110 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002111 // | task | single | + |
2112 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002113 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002114 // | task |parallel sections| * |
2115 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002116 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002117 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002118 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002119 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002120 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002121 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002122 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002123 // | task | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002124 // | task | target enter | * |
2125 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002126 // | task | target exit | * |
2127 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002128 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002129 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002130 // | | point | ! |
2131 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002132 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002133 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002134 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002135 // +------------------+-----------------+------------------------------------+
2136 // | ordered | parallel | * |
2137 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002138 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002139 // | ordered | master | * |
2140 // | ordered | critical | * |
2141 // | ordered | simd | * |
2142 // | ordered | sections | + |
2143 // | ordered | section | + |
2144 // | ordered | single | + |
2145 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002146 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002147 // | ordered |parallel sections| * |
2148 // | ordered | task | * |
2149 // | ordered | taskyield | * |
2150 // | ordered | barrier | + |
2151 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002152 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002153 // | ordered | flush | * |
2154 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002155 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002156 // | ordered | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002157 // | ordered | target enter | * |
2158 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002159 // | ordered | target exit | * |
2160 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002161 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002162 // | ordered | cancellation | |
2163 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002164 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002165 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002166 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002167 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002168 // +------------------+-----------------+------------------------------------+
2169 // | atomic | parallel | |
2170 // | atomic | for | |
2171 // | atomic | for simd | |
2172 // | atomic | master | |
2173 // | atomic | critical | |
2174 // | atomic | simd | |
2175 // | atomic | sections | |
2176 // | atomic | section | |
2177 // | atomic | single | |
2178 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002179 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002180 // | atomic |parallel sections| |
2181 // | atomic | task | |
2182 // | atomic | taskyield | |
2183 // | atomic | barrier | |
2184 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002185 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002186 // | atomic | flush | |
2187 // | atomic | ordered | |
2188 // | atomic | atomic | |
2189 // | atomic | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002190 // | atomic | target enter | |
2191 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002192 // | atomic | target exit | |
2193 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002194 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002195 // | atomic | cancellation | |
2196 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002197 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002198 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002199 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002200 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002201 // +------------------+-----------------+------------------------------------+
2202 // | target | parallel | * |
2203 // | target | for | * |
2204 // | target | for simd | * |
2205 // | target | master | * |
2206 // | target | critical | * |
2207 // | target | simd | * |
2208 // | target | sections | * |
2209 // | target | section | * |
2210 // | target | single | * |
2211 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002212 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002213 // | target |parallel sections| * |
2214 // | target | task | * |
2215 // | target | taskyield | * |
2216 // | target | barrier | * |
2217 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002218 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002219 // | target | flush | * |
2220 // | target | ordered | * |
2221 // | target | atomic | * |
2222 // | target | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002223 // | target | target enter | * |
2224 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002225 // | target | target exit | * |
2226 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002227 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002228 // | target | cancellation | |
2229 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002230 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002231 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002232 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002233 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002234 // +------------------+-----------------+------------------------------------+
2235 // | teams | parallel | * |
2236 // | teams | for | + |
2237 // | teams | for simd | + |
2238 // | teams | master | + |
2239 // | teams | critical | + |
2240 // | teams | simd | + |
2241 // | teams | sections | + |
2242 // | teams | section | + |
2243 // | teams | single | + |
2244 // | teams | parallel for | * |
2245 // | teams |parallel for simd| * |
2246 // | teams |parallel sections| * |
2247 // | teams | task | + |
2248 // | teams | taskyield | + |
2249 // | teams | barrier | + |
2250 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002251 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002252 // | teams | flush | + |
2253 // | teams | ordered | + |
2254 // | teams | atomic | + |
2255 // | teams | target | + |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002256 // | teams | target enter | + |
2257 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002258 // | teams | target exit | + |
2259 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002260 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002261 // | teams | cancellation | |
2262 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002263 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002264 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002265 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002266 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002267 // +------------------+-----------------+------------------------------------+
2268 // | taskloop | parallel | * |
2269 // | taskloop | for | + |
2270 // | taskloop | for simd | + |
2271 // | taskloop | master | + |
2272 // | taskloop | critical | * |
2273 // | taskloop | simd | * |
2274 // | taskloop | sections | + |
2275 // | taskloop | section | + |
2276 // | taskloop | single | + |
2277 // | taskloop | parallel for | * |
2278 // | taskloop |parallel for simd| * |
2279 // | taskloop |parallel sections| * |
2280 // | taskloop | task | * |
2281 // | taskloop | taskyield | * |
2282 // | taskloop | barrier | + |
2283 // | taskloop | taskwait | * |
2284 // | taskloop | taskgroup | * |
2285 // | taskloop | flush | * |
2286 // | taskloop | ordered | + |
2287 // | taskloop | atomic | * |
2288 // | taskloop | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002289 // | taskloop | target enter | * |
2290 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002291 // | taskloop | target exit | * |
2292 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002293 // | taskloop | teams | + |
2294 // | taskloop | cancellation | |
2295 // | | point | |
2296 // | taskloop | cancel | |
2297 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002298 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002299 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002300 // | taskloop simd | parallel | |
2301 // | taskloop simd | for | |
2302 // | taskloop simd | for simd | |
2303 // | taskloop simd | master | |
2304 // | taskloop simd | critical | |
2305 // | taskloop simd | simd | |
2306 // | taskloop simd | sections | |
2307 // | taskloop simd | section | |
2308 // | taskloop simd | single | |
2309 // | taskloop simd | parallel for | |
2310 // | taskloop simd |parallel for simd| |
2311 // | taskloop simd |parallel sections| |
2312 // | taskloop simd | task | |
2313 // | taskloop simd | taskyield | |
2314 // | taskloop simd | barrier | |
2315 // | taskloop simd | taskwait | |
2316 // | taskloop simd | taskgroup | |
2317 // | taskloop simd | flush | |
2318 // | taskloop simd | ordered | + (with simd clause) |
2319 // | taskloop simd | atomic | |
2320 // | taskloop simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002321 // | taskloop simd | target enter | |
2322 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002323 // | taskloop simd | target exit | |
2324 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002325 // | taskloop simd | teams | |
2326 // | taskloop simd | cancellation | |
2327 // | | point | |
2328 // | taskloop simd | cancel | |
2329 // | taskloop simd | taskloop | |
2330 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002331 // | taskloop simd | distribute | |
2332 // +------------------+-----------------+------------------------------------+
2333 // | distribute | parallel | * |
2334 // | distribute | for | * |
2335 // | distribute | for simd | * |
2336 // | distribute | master | * |
2337 // | distribute | critical | * |
2338 // | distribute | simd | * |
2339 // | distribute | sections | * |
2340 // | distribute | section | * |
2341 // | distribute | single | * |
2342 // | distribute | parallel for | * |
2343 // | distribute |parallel for simd| * |
2344 // | distribute |parallel sections| * |
2345 // | distribute | task | * |
2346 // | distribute | taskyield | * |
2347 // | distribute | barrier | * |
2348 // | distribute | taskwait | * |
2349 // | distribute | taskgroup | * |
2350 // | distribute | flush | * |
2351 // | distribute | ordered | + |
2352 // | distribute | atomic | * |
2353 // | distribute | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002354 // | distribute | target enter | |
2355 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002356 // | distribute | target exit | |
2357 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002358 // | distribute | teams | |
2359 // | distribute | cancellation | + |
2360 // | | point | |
2361 // | distribute | cancel | + |
2362 // | distribute | taskloop | * |
2363 // | distribute | taskloop simd | * |
2364 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002365 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002366 if (Stack->getCurScope()) {
2367 auto ParentRegion = Stack->getParentDirective();
2368 bool NestingProhibited = false;
2369 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002370 enum {
2371 NoRecommend,
2372 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002373 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002374 ShouldBeInTargetRegion,
2375 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002376 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002377 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002378 // OpenMP [2.16, Nesting of Regions]
2379 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002380 // OpenMP [2.8.1,simd Construct, Restrictions]
2381 // An ordered construct with the simd clause is the only OpenMP construct
2382 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002383 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2384 return true;
2385 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002386 if (ParentRegion == OMPD_atomic) {
2387 // OpenMP [2.16, Nesting of Regions]
2388 // OpenMP constructs may not be nested inside an atomic region.
2389 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2390 return true;
2391 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002392 if (CurrentRegion == OMPD_section) {
2393 // OpenMP [2.7.2, sections Construct, Restrictions]
2394 // Orphaned section directives are prohibited. That is, the section
2395 // directives must appear within the sections construct and must not be
2396 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002397 if (ParentRegion != OMPD_sections &&
2398 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002399 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2400 << (ParentRegion != OMPD_unknown)
2401 << getOpenMPDirectiveName(ParentRegion);
2402 return true;
2403 }
2404 return false;
2405 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002406 // Allow some constructs to be orphaned (they could be used in functions,
2407 // called from OpenMP regions with the required preconditions).
2408 if (ParentRegion == OMPD_unknown)
2409 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002410 if (CurrentRegion == OMPD_cancellation_point ||
2411 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002412 // OpenMP [2.16, Nesting of Regions]
2413 // A cancellation point construct for which construct-type-clause is
2414 // taskgroup must be nested inside a task construct. A cancellation
2415 // point construct for which construct-type-clause is not taskgroup must
2416 // be closely nested inside an OpenMP construct that matches the type
2417 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002418 // A cancel construct for which construct-type-clause is taskgroup must be
2419 // nested inside a task construct. A cancel construct for which
2420 // construct-type-clause is not taskgroup must be closely nested inside an
2421 // OpenMP construct that matches the type specified in
2422 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002423 NestingProhibited =
2424 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002425 (CancelRegion == OMPD_for &&
2426 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002427 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2428 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002429 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2430 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002431 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002432 // OpenMP [2.16, Nesting of Regions]
2433 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002434 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002435 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002436 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002437 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002438 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2439 // OpenMP [2.16, Nesting of Regions]
2440 // A critical region may not be nested (closely or otherwise) inside a
2441 // critical region with the same name. Note that this restriction is not
2442 // sufficient to prevent deadlock.
2443 SourceLocation PreviousCriticalLoc;
2444 bool DeadLock =
2445 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2446 OpenMPDirectiveKind K,
2447 const DeclarationNameInfo &DNI,
2448 SourceLocation Loc)
2449 ->bool {
2450 if (K == OMPD_critical &&
2451 DNI.getName() == CurrentName.getName()) {
2452 PreviousCriticalLoc = Loc;
2453 return true;
2454 } else
2455 return false;
2456 },
2457 false /* skip top directive */);
2458 if (DeadLock) {
2459 SemaRef.Diag(StartLoc,
2460 diag::err_omp_prohibited_region_critical_same_name)
2461 << CurrentName.getName();
2462 if (PreviousCriticalLoc.isValid())
2463 SemaRef.Diag(PreviousCriticalLoc,
2464 diag::note_omp_previous_critical_region);
2465 return true;
2466 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002467 } else if (CurrentRegion == OMPD_barrier) {
2468 // OpenMP [2.16, Nesting of Regions]
2469 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002470 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002471 NestingProhibited =
2472 isOpenMPWorksharingDirective(ParentRegion) ||
2473 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002474 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002475 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002476 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002477 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002478 // OpenMP [2.16, Nesting of Regions]
2479 // A worksharing region may not be closely nested inside a worksharing,
2480 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002481 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002482 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002483 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002484 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002485 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002486 Recommend = ShouldBeInParallelRegion;
2487 } else if (CurrentRegion == OMPD_ordered) {
2488 // OpenMP [2.16, Nesting of Regions]
2489 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002490 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002491 // An ordered region must be closely nested inside a loop region (or
2492 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002493 // OpenMP [2.8.1,simd Construct, Restrictions]
2494 // An ordered construct with the simd clause is the only OpenMP construct
2495 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002496 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002497 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002498 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002499 !(isOpenMPSimdDirective(ParentRegion) ||
2500 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002501 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002502 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2503 // OpenMP [2.16, Nesting of Regions]
2504 // If specified, a teams construct must be contained within a target
2505 // construct.
2506 NestingProhibited = ParentRegion != OMPD_target;
2507 Recommend = ShouldBeInTargetRegion;
2508 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2509 }
2510 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2511 // OpenMP [2.16, Nesting of Regions]
2512 // distribute, parallel, parallel sections, parallel workshare, and the
2513 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2514 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002515 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2516 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002517 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002518 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002519 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2520 // OpenMP 4.5 [2.17 Nesting of Regions]
2521 // The region associated with the distribute construct must be strictly
2522 // nested inside a teams region
2523 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2524 Recommend = ShouldBeInTeamsRegion;
2525 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002526 if (NestingProhibited) {
2527 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002528 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2529 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002530 return true;
2531 }
2532 }
2533 return false;
2534}
2535
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002536static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2537 ArrayRef<OMPClause *> Clauses,
2538 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2539 bool ErrorFound = false;
2540 unsigned NamedModifiersNumber = 0;
2541 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2542 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002543 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002544 for (const auto *C : Clauses) {
2545 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2546 // At most one if clause without a directive-name-modifier can appear on
2547 // the directive.
2548 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2549 if (FoundNameModifiers[CurNM]) {
2550 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2551 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2552 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2553 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002554 } else if (CurNM != OMPD_unknown) {
2555 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002556 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002557 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002558 FoundNameModifiers[CurNM] = IC;
2559 if (CurNM == OMPD_unknown)
2560 continue;
2561 // Check if the specified name modifier is allowed for the current
2562 // directive.
2563 // At most one if clause with the particular directive-name-modifier can
2564 // appear on the directive.
2565 bool MatchFound = false;
2566 for (auto NM : AllowedNameModifiers) {
2567 if (CurNM == NM) {
2568 MatchFound = true;
2569 break;
2570 }
2571 }
2572 if (!MatchFound) {
2573 S.Diag(IC->getNameModifierLoc(),
2574 diag::err_omp_wrong_if_directive_name_modifier)
2575 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2576 ErrorFound = true;
2577 }
2578 }
2579 }
2580 // If any if clause on the directive includes a directive-name-modifier then
2581 // all if clauses on the directive must include a directive-name-modifier.
2582 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2583 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2584 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2585 diag::err_omp_no_more_if_clause);
2586 } else {
2587 std::string Values;
2588 std::string Sep(", ");
2589 unsigned AllowedCnt = 0;
2590 unsigned TotalAllowedNum =
2591 AllowedNameModifiers.size() - NamedModifiersNumber;
2592 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2593 ++Cnt) {
2594 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2595 if (!FoundNameModifiers[NM]) {
2596 Values += "'";
2597 Values += getOpenMPDirectiveName(NM);
2598 Values += "'";
2599 if (AllowedCnt + 2 == TotalAllowedNum)
2600 Values += " or ";
2601 else if (AllowedCnt + 1 != TotalAllowedNum)
2602 Values += Sep;
2603 ++AllowedCnt;
2604 }
2605 }
2606 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2607 diag::err_omp_unnamed_if_clause)
2608 << (TotalAllowedNum > 1) << Values;
2609 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002610 for (auto Loc : NameModifierLoc) {
2611 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2612 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002613 ErrorFound = true;
2614 }
2615 return ErrorFound;
2616}
2617
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002618StmtResult Sema::ActOnOpenMPExecutableDirective(
2619 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2620 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2621 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002622 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002623 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2624 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002625 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002626
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002627 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002628 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002629 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002630 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002631 if (AStmt) {
2632 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2633
2634 // Check default data sharing attributes for referenced variables.
2635 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2636 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2637 if (DSAChecker.isErrorFound())
2638 return StmtError();
2639 // Generate list of implicitly defined firstprivate variables.
2640 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002641
2642 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2643 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2644 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2645 SourceLocation(), SourceLocation())) {
2646 ClausesWithImplicit.push_back(Implicit);
2647 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2648 DSAChecker.getImplicitFirstprivate().size();
2649 } else
2650 ErrorFound = true;
2651 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002652 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002653
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002654 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002655 switch (Kind) {
2656 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002657 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2658 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002659 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002660 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002661 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002662 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2663 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002664 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002665 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002666 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2667 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002668 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002669 case OMPD_for_simd:
2670 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2671 EndLoc, VarsWithInheritedDSA);
2672 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002673 case OMPD_sections:
2674 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2675 EndLoc);
2676 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002677 case OMPD_section:
2678 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002679 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002680 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2681 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002682 case OMPD_single:
2683 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2684 EndLoc);
2685 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002686 case OMPD_master:
2687 assert(ClausesWithImplicit.empty() &&
2688 "No clauses are allowed for 'omp master' directive");
2689 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2690 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002691 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002692 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2693 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002694 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002695 case OMPD_parallel_for:
2696 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2697 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002698 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002699 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002700 case OMPD_parallel_for_simd:
2701 Res = ActOnOpenMPParallelForSimdDirective(
2702 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002703 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002704 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002705 case OMPD_parallel_sections:
2706 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2707 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002708 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002709 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002710 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002711 Res =
2712 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002713 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002714 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002715 case OMPD_taskyield:
2716 assert(ClausesWithImplicit.empty() &&
2717 "No clauses are allowed for 'omp taskyield' directive");
2718 assert(AStmt == nullptr &&
2719 "No associated statement allowed for 'omp taskyield' directive");
2720 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2721 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002722 case OMPD_barrier:
2723 assert(ClausesWithImplicit.empty() &&
2724 "No clauses are allowed for 'omp barrier' directive");
2725 assert(AStmt == nullptr &&
2726 "No associated statement allowed for 'omp barrier' directive");
2727 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2728 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002729 case OMPD_taskwait:
2730 assert(ClausesWithImplicit.empty() &&
2731 "No clauses are allowed for 'omp taskwait' directive");
2732 assert(AStmt == nullptr &&
2733 "No associated statement allowed for 'omp taskwait' directive");
2734 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2735 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002736 case OMPD_taskgroup:
2737 assert(ClausesWithImplicit.empty() &&
2738 "No clauses are allowed for 'omp taskgroup' directive");
2739 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2740 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002741 case OMPD_flush:
2742 assert(AStmt == nullptr &&
2743 "No associated statement allowed for 'omp flush' directive");
2744 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2745 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002746 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002747 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2748 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002749 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002750 case OMPD_atomic:
2751 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2752 EndLoc);
2753 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002754 case OMPD_teams:
2755 Res =
2756 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2757 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002758 case OMPD_target:
2759 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2760 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002761 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002762 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002763 case OMPD_cancellation_point:
2764 assert(ClausesWithImplicit.empty() &&
2765 "No clauses are allowed for 'omp cancellation point' directive");
2766 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2767 "cancellation point' directive");
2768 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2769 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002770 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002771 assert(AStmt == nullptr &&
2772 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002773 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2774 CancelRegion);
2775 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002776 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002777 case OMPD_target_data:
2778 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2779 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002780 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002781 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002782 case OMPD_target_enter_data:
2783 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2784 EndLoc);
2785 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2786 break;
Samuel Antao72590762016-01-19 20:04:50 +00002787 case OMPD_target_exit_data:
2788 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2789 EndLoc);
2790 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2791 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002792 case OMPD_taskloop:
2793 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2794 EndLoc, VarsWithInheritedDSA);
2795 AllowedNameModifiers.push_back(OMPD_taskloop);
2796 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002797 case OMPD_taskloop_simd:
2798 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2799 EndLoc, VarsWithInheritedDSA);
2800 AllowedNameModifiers.push_back(OMPD_taskloop);
2801 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002802 case OMPD_distribute:
2803 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2804 EndLoc, VarsWithInheritedDSA);
2805 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002806 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002807 llvm_unreachable("OpenMP Directive is not allowed");
2808 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002809 llvm_unreachable("Unknown OpenMP directive");
2810 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002811
Alexey Bataev4acb8592014-07-07 13:01:15 +00002812 for (auto P : VarsWithInheritedDSA) {
2813 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2814 << P.first << P.second->getSourceRange();
2815 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002816 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2817
2818 if (!AllowedNameModifiers.empty())
2819 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2820 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002821
Alexey Bataeved09d242014-05-28 05:53:51 +00002822 if (ErrorFound)
2823 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002824 return Res;
2825}
2826
2827StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2828 Stmt *AStmt,
2829 SourceLocation StartLoc,
2830 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002831 if (!AStmt)
2832 return StmtError();
2833
Alexey Bataev9959db52014-05-06 10:08:46 +00002834 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2835 // 1.2.2 OpenMP Language Terminology
2836 // Structured block - An executable statement with a single entry at the
2837 // top and a single exit at the bottom.
2838 // The point of exit cannot be a branch out of the structured block.
2839 // longjmp() and throw() must not violate the entry/exit criteria.
2840 CS->getCapturedDecl()->setNothrow();
2841
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002842 getCurFunction()->setHasBranchProtectedScope();
2843
Alexey Bataev25e5b442015-09-15 12:52:43 +00002844 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2845 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002846}
2847
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002848namespace {
2849/// \brief Helper class for checking canonical form of the OpenMP loops and
2850/// extracting iteration space of each loop in the loop nest, that will be used
2851/// for IR generation.
2852class OpenMPIterationSpaceChecker {
2853 /// \brief Reference to Sema.
2854 Sema &SemaRef;
2855 /// \brief A location for diagnostics (when there is no some better location).
2856 SourceLocation DefaultLoc;
2857 /// \brief A location for diagnostics (when increment is not compatible).
2858 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002859 /// \brief A source location for referring to loop init later.
2860 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002861 /// \brief A source location for referring to condition later.
2862 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002863 /// \brief A source location for referring to increment later.
2864 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002865 /// \brief Loop variable.
2866 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002867 /// \brief Reference to loop variable.
2868 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002869 /// \brief Lower bound (initializer for the var).
2870 Expr *LB;
2871 /// \brief Upper bound.
2872 Expr *UB;
2873 /// \brief Loop step (increment).
2874 Expr *Step;
2875 /// \brief This flag is true when condition is one of:
2876 /// Var < UB
2877 /// Var <= UB
2878 /// UB > Var
2879 /// UB >= Var
2880 bool TestIsLessOp;
2881 /// \brief This flag is true when condition is strict ( < or > ).
2882 bool TestIsStrictOp;
2883 /// \brief This flag is true when step is subtracted on each iteration.
2884 bool SubtractStep;
2885
2886public:
2887 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2888 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002889 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2890 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002891 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2892 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002893 /// \brief Check init-expr for canonical loop form and save loop counter
2894 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002895 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002896 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2897 /// for less/greater and for strict/non-strict comparison.
2898 bool CheckCond(Expr *S);
2899 /// \brief Check incr-expr for canonical loop form and return true if it
2900 /// does not conform, otherwise save loop step (#Step).
2901 bool CheckInc(Expr *S);
2902 /// \brief Return the loop counter variable.
2903 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002904 /// \brief Return the reference expression to loop counter variable.
2905 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002906 /// \brief Source range of the loop init.
2907 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2908 /// \brief Source range of the loop condition.
2909 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2910 /// \brief Source range of the loop increment.
2911 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2912 /// \brief True if the step should be subtracted.
2913 bool ShouldSubtractStep() const { return SubtractStep; }
2914 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002915 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002916 /// \brief Build the precondition expression for the loops.
2917 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002918 /// \brief Build reference expression to the counter be used for codegen.
2919 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002920 /// \brief Build reference expression to the private counter be used for
2921 /// codegen.
2922 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002923 /// \brief Build initization of the counter be used for codegen.
2924 Expr *BuildCounterInit() const;
2925 /// \brief Build step of the counter be used for codegen.
2926 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002927 /// \brief Return true if any expression is dependent.
2928 bool Dependent() const;
2929
2930private:
2931 /// \brief Check the right-hand side of an assignment in the increment
2932 /// expression.
2933 bool CheckIncRHS(Expr *RHS);
2934 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002935 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002936 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002937 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002938 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002939 /// \brief Helper to set loop increment.
2940 bool SetStep(Expr *NewStep, bool Subtract);
2941};
2942
2943bool OpenMPIterationSpaceChecker::Dependent() const {
2944 if (!Var) {
2945 assert(!LB && !UB && !Step);
2946 return false;
2947 }
2948 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2949 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2950}
2951
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002952template <typename T>
2953static T *getExprAsWritten(T *E) {
2954 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2955 E = ExprTemp->getSubExpr();
2956
2957 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2958 E = MTE->GetTemporaryExpr();
2959
2960 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2961 E = Binder->getSubExpr();
2962
2963 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2964 E = ICE->getSubExprAsWritten();
2965 return E->IgnoreParens();
2966}
2967
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002968bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2969 DeclRefExpr *NewVarRefExpr,
2970 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002971 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002972 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2973 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002974 if (!NewVar || !NewLB)
2975 return true;
2976 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002977 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002978 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2979 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002980 if ((Ctor->isCopyOrMoveConstructor() ||
2981 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2982 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002983 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002984 LB = NewLB;
2985 return false;
2986}
2987
2988bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002989 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002990 // State consistency checking to ensure correct usage.
2991 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2992 !TestIsLessOp && !TestIsStrictOp);
2993 if (!NewUB)
2994 return true;
2995 UB = NewUB;
2996 TestIsLessOp = LessOp;
2997 TestIsStrictOp = StrictOp;
2998 ConditionSrcRange = SR;
2999 ConditionLoc = SL;
3000 return false;
3001}
3002
3003bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3004 // State consistency checking to ensure correct usage.
3005 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3006 if (!NewStep)
3007 return true;
3008 if (!NewStep->isValueDependent()) {
3009 // Check that the step is integer expression.
3010 SourceLocation StepLoc = NewStep->getLocStart();
3011 ExprResult Val =
3012 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3013 if (Val.isInvalid())
3014 return true;
3015 NewStep = Val.get();
3016
3017 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3018 // If test-expr is of form var relational-op b and relational-op is < or
3019 // <= then incr-expr must cause var to increase on each iteration of the
3020 // loop. If test-expr is of form var relational-op b and relational-op is
3021 // > or >= then incr-expr must cause var to decrease on each iteration of
3022 // the loop.
3023 // If test-expr is of form b relational-op var and relational-op is < or
3024 // <= then incr-expr must cause var to decrease on each iteration of the
3025 // loop. If test-expr is of form b relational-op var and relational-op is
3026 // > or >= then incr-expr must cause var to increase on each iteration of
3027 // the loop.
3028 llvm::APSInt Result;
3029 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3030 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3031 bool IsConstNeg =
3032 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003033 bool IsConstPos =
3034 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003035 bool IsConstZero = IsConstant && !Result.getBoolValue();
3036 if (UB && (IsConstZero ||
3037 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003038 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003039 SemaRef.Diag(NewStep->getExprLoc(),
3040 diag::err_omp_loop_incr_not_compatible)
3041 << Var << TestIsLessOp << NewStep->getSourceRange();
3042 SemaRef.Diag(ConditionLoc,
3043 diag::note_omp_loop_cond_requres_compatible_incr)
3044 << TestIsLessOp << ConditionSrcRange;
3045 return true;
3046 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003047 if (TestIsLessOp == Subtract) {
3048 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3049 NewStep).get();
3050 Subtract = !Subtract;
3051 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003052 }
3053
3054 Step = NewStep;
3055 SubtractStep = Subtract;
3056 return false;
3057}
3058
Alexey Bataev9c821032015-04-30 04:23:23 +00003059bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003060 // Check init-expr for canonical loop form and save loop counter
3061 // variable - #Var and its initialization value - #LB.
3062 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3063 // var = lb
3064 // integer-type var = lb
3065 // random-access-iterator-type var = lb
3066 // pointer-type var = lb
3067 //
3068 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003069 if (EmitDiags) {
3070 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3071 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003072 return true;
3073 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003074 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003075 if (Expr *E = dyn_cast<Expr>(S))
3076 S = E->IgnoreParens();
3077 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3078 if (BO->getOpcode() == BO_Assign)
3079 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003080 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003081 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003082 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3083 if (DS->isSingleDecl()) {
3084 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003085 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003086 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003087 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003088 SemaRef.Diag(S->getLocStart(),
3089 diag::ext_omp_loop_not_canonical_init)
3090 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003091 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003092 }
3093 }
3094 }
3095 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3096 if (CE->getOperator() == OO_Equal)
3097 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003098 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3099 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003100
Alexey Bataev9c821032015-04-30 04:23:23 +00003101 if (EmitDiags) {
3102 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3103 << S->getSourceRange();
3104 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003105 return true;
3106}
3107
Alexey Bataev23b69422014-06-18 07:08:49 +00003108/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003109/// variable (which may be the loop variable) if possible.
3110static const VarDecl *GetInitVarDecl(const Expr *E) {
3111 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003112 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003113 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003114 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3115 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003116 if ((Ctor->isCopyOrMoveConstructor() ||
3117 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3118 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003119 E = CE->getArg(0)->IgnoreParenImpCasts();
3120 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3121 if (!DRE)
3122 return nullptr;
3123 return dyn_cast<VarDecl>(DRE->getDecl());
3124}
3125
3126bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3127 // Check test-expr for canonical form, save upper-bound UB, flags for
3128 // less/greater and for strict/non-strict comparison.
3129 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3130 // var relational-op b
3131 // b relational-op var
3132 //
3133 if (!S) {
3134 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3135 return true;
3136 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003137 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003138 SourceLocation CondLoc = S->getLocStart();
3139 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3140 if (BO->isRelationalOp()) {
3141 if (GetInitVarDecl(BO->getLHS()) == Var)
3142 return SetUB(BO->getRHS(),
3143 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3144 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3145 BO->getSourceRange(), BO->getOperatorLoc());
3146 if (GetInitVarDecl(BO->getRHS()) == Var)
3147 return SetUB(BO->getLHS(),
3148 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3149 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3150 BO->getSourceRange(), BO->getOperatorLoc());
3151 }
3152 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3153 if (CE->getNumArgs() == 2) {
3154 auto Op = CE->getOperator();
3155 switch (Op) {
3156 case OO_Greater:
3157 case OO_GreaterEqual:
3158 case OO_Less:
3159 case OO_LessEqual:
3160 if (GetInitVarDecl(CE->getArg(0)) == Var)
3161 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3162 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3163 CE->getOperatorLoc());
3164 if (GetInitVarDecl(CE->getArg(1)) == Var)
3165 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3166 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3167 CE->getOperatorLoc());
3168 break;
3169 default:
3170 break;
3171 }
3172 }
3173 }
3174 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3175 << S->getSourceRange() << Var;
3176 return true;
3177}
3178
3179bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3180 // RHS of canonical loop form increment can be:
3181 // var + incr
3182 // incr + var
3183 // var - incr
3184 //
3185 RHS = RHS->IgnoreParenImpCasts();
3186 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3187 if (BO->isAdditiveOp()) {
3188 bool IsAdd = BO->getOpcode() == BO_Add;
3189 if (GetInitVarDecl(BO->getLHS()) == Var)
3190 return SetStep(BO->getRHS(), !IsAdd);
3191 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3192 return SetStep(BO->getLHS(), false);
3193 }
3194 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3195 bool IsAdd = CE->getOperator() == OO_Plus;
3196 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3197 if (GetInitVarDecl(CE->getArg(0)) == Var)
3198 return SetStep(CE->getArg(1), !IsAdd);
3199 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3200 return SetStep(CE->getArg(0), false);
3201 }
3202 }
3203 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3204 << RHS->getSourceRange() << Var;
3205 return true;
3206}
3207
3208bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3209 // Check incr-expr for canonical loop form and return true if it
3210 // does not conform.
3211 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3212 // ++var
3213 // var++
3214 // --var
3215 // var--
3216 // var += incr
3217 // var -= incr
3218 // var = var + incr
3219 // var = incr + var
3220 // var = var - incr
3221 //
3222 if (!S) {
3223 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3224 return true;
3225 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003226 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003227 S = S->IgnoreParens();
3228 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3229 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3230 return SetStep(
3231 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3232 (UO->isDecrementOp() ? -1 : 1)).get(),
3233 false);
3234 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3235 switch (BO->getOpcode()) {
3236 case BO_AddAssign:
3237 case BO_SubAssign:
3238 if (GetInitVarDecl(BO->getLHS()) == Var)
3239 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3240 break;
3241 case BO_Assign:
3242 if (GetInitVarDecl(BO->getLHS()) == Var)
3243 return CheckIncRHS(BO->getRHS());
3244 break;
3245 default:
3246 break;
3247 }
3248 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3249 switch (CE->getOperator()) {
3250 case OO_PlusPlus:
3251 case OO_MinusMinus:
3252 if (GetInitVarDecl(CE->getArg(0)) == Var)
3253 return SetStep(
3254 SemaRef.ActOnIntegerConstant(
3255 CE->getLocStart(),
3256 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3257 false);
3258 break;
3259 case OO_PlusEqual:
3260 case OO_MinusEqual:
3261 if (GetInitVarDecl(CE->getArg(0)) == Var)
3262 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3263 break;
3264 case OO_Equal:
3265 if (GetInitVarDecl(CE->getArg(0)) == Var)
3266 return CheckIncRHS(CE->getArg(1));
3267 break;
3268 default:
3269 break;
3270 }
3271 }
3272 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3273 << S->getSourceRange() << Var;
3274 return true;
3275}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003276
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003277namespace {
3278// Transform variables declared in GNU statement expressions to new ones to
3279// avoid crash on codegen.
3280class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3281 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3282
3283public:
3284 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3285
3286 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3287 if (auto *VD = cast<VarDecl>(D))
3288 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3289 !isa<ImplicitParamDecl>(D)) {
3290 auto *NewVD = VarDecl::Create(
3291 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3292 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3293 VD->getTypeSourceInfo(), VD->getStorageClass());
3294 NewVD->setTSCSpec(VD->getTSCSpec());
3295 NewVD->setInit(VD->getInit());
3296 NewVD->setInitStyle(VD->getInitStyle());
3297 NewVD->setExceptionVariable(VD->isExceptionVariable());
3298 NewVD->setNRVOVariable(VD->isNRVOVariable());
3299 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3300 NewVD->setConstexpr(VD->isConstexpr());
3301 NewVD->setInitCapture(VD->isInitCapture());
3302 NewVD->setPreviousDeclInSameBlockScope(
3303 VD->isPreviousDeclInSameBlockScope());
3304 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003305 if (VD->hasAttrs())
3306 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003307 transformedLocalDecl(VD, NewVD);
3308 return NewVD;
3309 }
3310 return BaseTransform::TransformDefinition(Loc, D);
3311 }
3312
3313 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3314 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3315 if (E->getDecl() != NewD) {
3316 NewD->setReferenced();
3317 NewD->markUsed(SemaRef.Context);
3318 return DeclRefExpr::Create(
3319 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3320 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3321 E->getNameInfo(), E->getType(), E->getValueKind());
3322 }
3323 return BaseTransform::TransformDeclRefExpr(E);
3324 }
3325};
3326}
3327
Alexander Musmana5f070a2014-10-01 06:03:56 +00003328/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003329Expr *
3330OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3331 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003332 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003333 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003334 auto VarType = Var->getType().getNonReferenceType();
3335 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003336 SemaRef.getLangOpts().CPlusPlus) {
3337 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003338 auto *UBExpr = TestIsLessOp ? UB : LB;
3339 auto *LBExpr = TestIsLessOp ? LB : UB;
3340 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3341 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3342 if (!Upper || !Lower)
3343 return nullptr;
3344 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3345 Sema::AA_Converting,
3346 /*AllowExplicit=*/true)
3347 .get();
3348 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3349 Sema::AA_Converting,
3350 /*AllowExplicit=*/true)
3351 .get();
3352 if (!Upper || !Lower)
3353 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003354
3355 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3356
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003357 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003358 // BuildBinOp already emitted error, this one is to point user to upper
3359 // and lower bound, and to tell what is passed to 'operator-'.
3360 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3361 << Upper->getSourceRange() << Lower->getSourceRange();
3362 return nullptr;
3363 }
3364 }
3365
3366 if (!Diff.isUsable())
3367 return nullptr;
3368
3369 // Upper - Lower [- 1]
3370 if (TestIsStrictOp)
3371 Diff = SemaRef.BuildBinOp(
3372 S, DefaultLoc, BO_Sub, Diff.get(),
3373 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3374 if (!Diff.isUsable())
3375 return nullptr;
3376
3377 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003378 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3379 if (NewStep.isInvalid())
3380 return nullptr;
3381 NewStep = SemaRef.PerformImplicitConversion(
3382 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3383 /*AllowExplicit=*/true);
3384 if (NewStep.isInvalid())
3385 return nullptr;
3386 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003387 if (!Diff.isUsable())
3388 return nullptr;
3389
3390 // Parentheses (for dumping/debugging purposes only).
3391 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3392 if (!Diff.isUsable())
3393 return nullptr;
3394
3395 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003396 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3397 if (NewStep.isInvalid())
3398 return nullptr;
3399 NewStep = SemaRef.PerformImplicitConversion(
3400 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3401 /*AllowExplicit=*/true);
3402 if (NewStep.isInvalid())
3403 return nullptr;
3404 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003405 if (!Diff.isUsable())
3406 return nullptr;
3407
Alexander Musman174b3ca2014-10-06 11:16:29 +00003408 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003409 QualType Type = Diff.get()->getType();
3410 auto &C = SemaRef.Context;
3411 bool UseVarType = VarType->hasIntegerRepresentation() &&
3412 C.getTypeSize(Type) > C.getTypeSize(VarType);
3413 if (!Type->isIntegerType() || UseVarType) {
3414 unsigned NewSize =
3415 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3416 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3417 : Type->hasSignedIntegerRepresentation();
3418 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3419 Diff = SemaRef.PerformImplicitConversion(
3420 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3421 if (!Diff.isUsable())
3422 return nullptr;
3423 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003424 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003425 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3426 if (NewSize != C.getTypeSize(Type)) {
3427 if (NewSize < C.getTypeSize(Type)) {
3428 assert(NewSize == 64 && "incorrect loop var size");
3429 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3430 << InitSrcRange << ConditionSrcRange;
3431 }
3432 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003433 NewSize, Type->hasSignedIntegerRepresentation() ||
3434 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003435 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3436 Sema::AA_Converting, true);
3437 if (!Diff.isUsable())
3438 return nullptr;
3439 }
3440 }
3441
Alexander Musmana5f070a2014-10-01 06:03:56 +00003442 return Diff.get();
3443}
3444
Alexey Bataev62dbb972015-04-22 11:59:37 +00003445Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3446 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3447 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3448 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003449 TransformToNewDefs Transform(SemaRef);
3450
3451 auto NewLB = Transform.TransformExpr(LB);
3452 auto NewUB = Transform.TransformExpr(UB);
3453 if (NewLB.isInvalid() || NewUB.isInvalid())
3454 return Cond;
3455 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3456 Sema::AA_Converting,
3457 /*AllowExplicit=*/true);
3458 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3459 Sema::AA_Converting,
3460 /*AllowExplicit=*/true);
3461 if (NewLB.isInvalid() || NewUB.isInvalid())
3462 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003463 auto CondExpr = SemaRef.BuildBinOp(
3464 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3465 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003466 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003467 if (CondExpr.isUsable()) {
3468 CondExpr = SemaRef.PerformImplicitConversion(
3469 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3470 /*AllowExplicit=*/true);
3471 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003472 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3473 // Otherwise use original loop conditon and evaluate it in runtime.
3474 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3475}
3476
Alexander Musmana5f070a2014-10-01 06:03:56 +00003477/// \brief Build reference expression to the counter be used for codegen.
3478Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003479 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3480 DefaultLoc);
3481}
3482
3483Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3484 if (Var && !Var->isInvalidDecl()) {
3485 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003486 auto *PrivateVar =
3487 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3488 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003489 if (PrivateVar->isInvalidDecl())
3490 return nullptr;
3491 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3492 }
3493 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003494}
3495
3496/// \brief Build initization of the counter be used for codegen.
3497Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3498
3499/// \brief Build step of the counter be used for codegen.
3500Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3501
3502/// \brief Iteration space of a single for loop.
3503struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003504 /// \brief Condition of the loop.
3505 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003506 /// \brief This expression calculates the number of iterations in the loop.
3507 /// It is always possible to calculate it before starting the loop.
3508 Expr *NumIterations;
3509 /// \brief The loop counter variable.
3510 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003511 /// \brief Private loop counter variable.
3512 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003513 /// \brief This is initializer for the initial value of #CounterVar.
3514 Expr *CounterInit;
3515 /// \brief This is step for the #CounterVar used to generate its update:
3516 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3517 Expr *CounterStep;
3518 /// \brief Should step be subtracted?
3519 bool Subtract;
3520 /// \brief Source range of the loop init.
3521 SourceRange InitSrcRange;
3522 /// \brief Source range of the loop condition.
3523 SourceRange CondSrcRange;
3524 /// \brief Source range of the loop increment.
3525 SourceRange IncSrcRange;
3526};
3527
Alexey Bataev23b69422014-06-18 07:08:49 +00003528} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003529
Alexey Bataev9c821032015-04-30 04:23:23 +00003530void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3531 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3532 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003533 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3534 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003535 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3536 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003537 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003538 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003539 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003540 }
3541}
3542
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003543/// \brief Called on a for stmt to check and extract its iteration space
3544/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003545static bool CheckOpenMPIterationSpace(
3546 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3547 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003548 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003549 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3550 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003551 // OpenMP [2.6, Canonical Loop Form]
3552 // for (init-expr; test-expr; incr-expr) structured-block
3553 auto For = dyn_cast_or_null<ForStmt>(S);
3554 if (!For) {
3555 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003556 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3557 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3558 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3559 if (NestedLoopCount > 1) {
3560 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3561 SemaRef.Diag(DSA.getConstructLoc(),
3562 diag::note_omp_collapse_ordered_expr)
3563 << 2 << CollapseLoopCountExpr->getSourceRange()
3564 << OrderedLoopCountExpr->getSourceRange();
3565 else if (CollapseLoopCountExpr)
3566 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3567 diag::note_omp_collapse_ordered_expr)
3568 << 0 << CollapseLoopCountExpr->getSourceRange();
3569 else
3570 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3571 diag::note_omp_collapse_ordered_expr)
3572 << 1 << OrderedLoopCountExpr->getSourceRange();
3573 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003574 return true;
3575 }
3576 assert(For->getBody());
3577
3578 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3579
3580 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003581 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003582 if (ISC.CheckInit(Init)) {
3583 return true;
3584 }
3585
3586 bool HasErrors = false;
3587
3588 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003589 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003590
3591 // OpenMP [2.6, Canonical Loop Form]
3592 // Var is one of the following:
3593 // A variable of signed or unsigned integer type.
3594 // For C++, a variable of a random access iterator type.
3595 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003596 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003597 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3598 !VarType->isPointerType() &&
3599 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3600 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3601 << SemaRef.getLangOpts().CPlusPlus;
3602 HasErrors = true;
3603 }
3604
Alexey Bataev4acb8592014-07-07 13:01:15 +00003605 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3606 // Construct
3607 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3608 // parallel for construct is (are) private.
3609 // The loop iteration variable in the associated for-loop of a simd construct
3610 // with just one associated for-loop is linear with a constant-linear-step
3611 // that is the increment of the associated for-loop.
3612 // Exclude loop var from the list of variables with implicitly defined data
3613 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003614 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003615
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003616 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3617 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003618 // The loop iteration variable in the associated for-loop of a simd construct
3619 // with just one associated for-loop may be listed in a linear clause with a
3620 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003621 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3622 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003623 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003624 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3625 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3626 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003627 auto PredeterminedCKind =
3628 isOpenMPSimdDirective(DKind)
3629 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3630 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003631 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003632 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003633 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003634 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003635 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003636 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3637 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003638 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003639 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3640 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003641 if (DVar.RefExpr == nullptr)
3642 DVar.CKind = PredeterminedCKind;
3643 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003644 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003645 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003646 // Make the loop iteration variable private (for worksharing constructs),
3647 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003648 // lastprivate (for simd directives with several collapsed or ordered
3649 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003650 if (DVar.CKind == OMPC_unknown)
3651 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3652 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003653 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003654 }
3655
Alexey Bataev7ff55242014-06-19 09:13:45 +00003656 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003657
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003658 // Check test-expr.
3659 HasErrors |= ISC.CheckCond(For->getCond());
3660
3661 // Check incr-expr.
3662 HasErrors |= ISC.CheckInc(For->getInc());
3663
Alexander Musmana5f070a2014-10-01 06:03:56 +00003664 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003665 return HasErrors;
3666
Alexander Musmana5f070a2014-10-01 06:03:56 +00003667 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003668 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003669 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003670 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003671 isOpenMPTaskLoopDirective(DKind) ||
3672 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003673 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003674 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003675 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3676 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3677 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3678 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3679 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3680 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3681
Alexey Bataev62dbb972015-04-22 11:59:37 +00003682 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3683 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003684 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003685 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003686 ResultIterSpace.CounterInit == nullptr ||
3687 ResultIterSpace.CounterStep == nullptr);
3688
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003689 return HasErrors;
3690}
3691
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003692/// \brief Build 'VarRef = Start.
3693static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3694 ExprResult VarRef, ExprResult Start) {
3695 TransformToNewDefs Transform(SemaRef);
3696 // Build 'VarRef = Start.
3697 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3698 if (NewStart.isInvalid())
3699 return ExprError();
3700 NewStart = SemaRef.PerformImplicitConversion(
3701 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3702 Sema::AA_Converting,
3703 /*AllowExplicit=*/true);
3704 if (NewStart.isInvalid())
3705 return ExprError();
3706 NewStart = SemaRef.PerformImplicitConversion(
3707 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3708 /*AllowExplicit=*/true);
3709 if (!NewStart.isUsable())
3710 return ExprError();
3711
3712 auto Init =
3713 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3714 return Init;
3715}
3716
Alexander Musmana5f070a2014-10-01 06:03:56 +00003717/// \brief Build 'VarRef = Start + Iter * Step'.
3718static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3719 SourceLocation Loc, ExprResult VarRef,
3720 ExprResult Start, ExprResult Iter,
3721 ExprResult Step, bool Subtract) {
3722 // Add parentheses (for debugging purposes only).
3723 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3724 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3725 !Step.isUsable())
3726 return ExprError();
3727
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003728 TransformToNewDefs Transform(SemaRef);
3729 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3730 if (NewStep.isInvalid())
3731 return ExprError();
3732 NewStep = SemaRef.PerformImplicitConversion(
3733 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3734 Sema::AA_Converting,
3735 /*AllowExplicit=*/true);
3736 if (NewStep.isInvalid())
3737 return ExprError();
3738 ExprResult Update =
3739 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003740 if (!Update.isUsable())
3741 return ExprError();
3742
3743 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003744 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3745 if (NewStart.isInvalid())
3746 return ExprError();
3747 NewStart = SemaRef.PerformImplicitConversion(
3748 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3749 Sema::AA_Converting,
3750 /*AllowExplicit=*/true);
3751 if (NewStart.isInvalid())
3752 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003753 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003754 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003755 if (!Update.isUsable())
3756 return ExprError();
3757
3758 Update = SemaRef.PerformImplicitConversion(
3759 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3760 if (!Update.isUsable())
3761 return ExprError();
3762
3763 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3764 return Update;
3765}
3766
3767/// \brief Convert integer expression \a E to make it have at least \a Bits
3768/// bits.
3769static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3770 Sema &SemaRef) {
3771 if (E == nullptr)
3772 return ExprError();
3773 auto &C = SemaRef.Context;
3774 QualType OldType = E->getType();
3775 unsigned HasBits = C.getTypeSize(OldType);
3776 if (HasBits >= Bits)
3777 return ExprResult(E);
3778 // OK to convert to signed, because new type has more bits than old.
3779 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3780 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3781 true);
3782}
3783
3784/// \brief Check if the given expression \a E is a constant integer that fits
3785/// into \a Bits bits.
3786static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3787 if (E == nullptr)
3788 return false;
3789 llvm::APSInt Result;
3790 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3791 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3792 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003793}
3794
3795/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003796/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3797/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003798static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003799CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3800 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3801 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003802 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003803 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003804 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003805 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003806 // Found 'collapse' clause - calculate collapse number.
3807 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003808 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003809 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003810 }
3811 if (OrderedLoopCountExpr) {
3812 // Found 'ordered' clause - calculate collapse number.
3813 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003814 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3815 if (Result.getLimitedValue() < NestedLoopCount) {
3816 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3817 diag::err_omp_wrong_ordered_loop_count)
3818 << OrderedLoopCountExpr->getSourceRange();
3819 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3820 diag::note_collapse_loop_count)
3821 << CollapseLoopCountExpr->getSourceRange();
3822 }
3823 NestedLoopCount = Result.getLimitedValue();
3824 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003825 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003826 // This is helper routine for loop directives (e.g., 'for', 'simd',
3827 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003828 SmallVector<LoopIterationSpace, 4> IterSpaces;
3829 IterSpaces.resize(NestedLoopCount);
3830 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003831 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003832 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003833 NestedLoopCount, CollapseLoopCountExpr,
3834 OrderedLoopCountExpr, VarsWithImplicitDSA,
3835 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003836 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003837 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003838 // OpenMP [2.8.1, simd construct, Restrictions]
3839 // All loops associated with the construct must be perfectly nested; that
3840 // is, there must be no intervening code nor any OpenMP directive between
3841 // any two loops.
3842 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003843 }
3844
Alexander Musmana5f070a2014-10-01 06:03:56 +00003845 Built.clear(/* size */ NestedLoopCount);
3846
3847 if (SemaRef.CurContext->isDependentContext())
3848 return NestedLoopCount;
3849
3850 // An example of what is generated for the following code:
3851 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003852 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003853 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003854 // for (k = 0; k < NK; ++k)
3855 // for (j = J0; j < NJ; j+=2) {
3856 // <loop body>
3857 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003858 //
3859 // We generate the code below.
3860 // Note: the loop body may be outlined in CodeGen.
3861 // Note: some counters may be C++ classes, operator- is used to find number of
3862 // iterations and operator+= to calculate counter value.
3863 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3864 // or i64 is currently supported).
3865 //
3866 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3867 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3868 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3869 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3870 // // similar updates for vars in clauses (e.g. 'linear')
3871 // <loop body (using local i and j)>
3872 // }
3873 // i = NI; // assign final values of counters
3874 // j = NJ;
3875 //
3876
3877 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3878 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003879 // Precondition tests if there is at least one iteration (all conditions are
3880 // true).
3881 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003882 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003883 ExprResult LastIteration32 = WidenIterationCount(
3884 32 /* Bits */, SemaRef.PerformImplicitConversion(
3885 N0->IgnoreImpCasts(), N0->getType(),
3886 Sema::AA_Converting, /*AllowExplicit=*/true)
3887 .get(),
3888 SemaRef);
3889 ExprResult LastIteration64 = WidenIterationCount(
3890 64 /* Bits */, SemaRef.PerformImplicitConversion(
3891 N0->IgnoreImpCasts(), N0->getType(),
3892 Sema::AA_Converting, /*AllowExplicit=*/true)
3893 .get(),
3894 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003895
3896 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3897 return NestedLoopCount;
3898
3899 auto &C = SemaRef.Context;
3900 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3901
3902 Scope *CurScope = DSA.getCurScope();
3903 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003904 if (PreCond.isUsable()) {
3905 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3906 PreCond.get(), IterSpaces[Cnt].PreCond);
3907 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003908 auto N = IterSpaces[Cnt].NumIterations;
3909 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3910 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003911 LastIteration32 = SemaRef.BuildBinOp(
3912 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3913 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3914 Sema::AA_Converting,
3915 /*AllowExplicit=*/true)
3916 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003917 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003918 LastIteration64 = SemaRef.BuildBinOp(
3919 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3920 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3921 Sema::AA_Converting,
3922 /*AllowExplicit=*/true)
3923 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003924 }
3925
3926 // Choose either the 32-bit or 64-bit version.
3927 ExprResult LastIteration = LastIteration64;
3928 if (LastIteration32.isUsable() &&
3929 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3930 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3931 FitsInto(
3932 32 /* Bits */,
3933 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3934 LastIteration64.get(), SemaRef)))
3935 LastIteration = LastIteration32;
3936
3937 if (!LastIteration.isUsable())
3938 return 0;
3939
3940 // Save the number of iterations.
3941 ExprResult NumIterations = LastIteration;
3942 {
3943 LastIteration = SemaRef.BuildBinOp(
3944 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3945 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3946 if (!LastIteration.isUsable())
3947 return 0;
3948 }
3949
3950 // Calculate the last iteration number beforehand instead of doing this on
3951 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3952 llvm::APSInt Result;
3953 bool IsConstant =
3954 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3955 ExprResult CalcLastIteration;
3956 if (!IsConstant) {
3957 SourceLocation SaveLoc;
3958 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003959 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003960 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003961 ExprResult SaveRef = buildDeclRefExpr(
3962 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003963 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3964 SaveRef.get(), LastIteration.get());
3965 LastIteration = SaveRef;
3966
3967 // Prepare SaveRef + 1.
3968 NumIterations = SemaRef.BuildBinOp(
3969 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3970 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3971 if (!NumIterations.isUsable())
3972 return 0;
3973 }
3974
3975 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3976
Alexander Musmanc6388682014-12-15 07:07:06 +00003977 QualType VType = LastIteration.get()->getType();
3978 // Build variables passed into runtime, nesessary for worksharing directives.
3979 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003980 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3981 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003982 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003983 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3984 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003985 SemaRef.AddInitializerToDecl(
3986 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3987 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3988
3989 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003990 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3991 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003992 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3993 /*DirectInit*/ false,
3994 /*TypeMayContainAuto*/ false);
3995
3996 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3997 // This will be used to implement clause 'lastprivate'.
3998 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003999 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4000 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004001 SemaRef.AddInitializerToDecl(
4002 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4003 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4004
4005 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004006 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4007 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004008 SemaRef.AddInitializerToDecl(
4009 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4010 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4011
4012 // Build expression: UB = min(UB, LastIteration)
4013 // It is nesessary for CodeGen of directives with static scheduling.
4014 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4015 UB.get(), LastIteration.get());
4016 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4017 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4018 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4019 CondOp.get());
4020 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4021 }
4022
4023 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004024 ExprResult IV;
4025 ExprResult Init;
4026 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004027 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4028 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004029 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004030 isOpenMPTaskLoopDirective(DKind) ||
4031 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004032 ? LB.get()
4033 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4034 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4035 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004036 }
4037
Alexander Musmanc6388682014-12-15 07:07:06 +00004038 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004039 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004040 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004041 (isOpenMPWorksharingDirective(DKind) ||
4042 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004043 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4044 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4045 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004046
4047 // Loop increment (IV = IV + 1)
4048 SourceLocation IncLoc;
4049 ExprResult Inc =
4050 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4051 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4052 if (!Inc.isUsable())
4053 return 0;
4054 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004055 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4056 if (!Inc.isUsable())
4057 return 0;
4058
4059 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4060 // Used for directives with static scheduling.
4061 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004062 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4063 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004064 // LB + ST
4065 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4066 if (!NextLB.isUsable())
4067 return 0;
4068 // LB = LB + ST
4069 NextLB =
4070 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4071 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4072 if (!NextLB.isUsable())
4073 return 0;
4074 // UB + ST
4075 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4076 if (!NextUB.isUsable())
4077 return 0;
4078 // UB = UB + ST
4079 NextUB =
4080 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4081 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4082 if (!NextUB.isUsable())
4083 return 0;
4084 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004085
4086 // Build updates and final values of the loop counters.
4087 bool HasErrors = false;
4088 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004089 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004090 Built.Updates.resize(NestedLoopCount);
4091 Built.Finals.resize(NestedLoopCount);
4092 {
4093 ExprResult Div;
4094 // Go from inner nested loop to outer.
4095 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4096 LoopIterationSpace &IS = IterSpaces[Cnt];
4097 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4098 // Build: Iter = (IV / Div) % IS.NumIters
4099 // where Div is product of previous iterations' IS.NumIters.
4100 ExprResult Iter;
4101 if (Div.isUsable()) {
4102 Iter =
4103 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4104 } else {
4105 Iter = IV;
4106 assert((Cnt == (int)NestedLoopCount - 1) &&
4107 "unusable div expected on first iteration only");
4108 }
4109
4110 if (Cnt != 0 && Iter.isUsable())
4111 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4112 IS.NumIterations);
4113 if (!Iter.isUsable()) {
4114 HasErrors = true;
4115 break;
4116 }
4117
Alexey Bataev39f915b82015-05-08 10:41:21 +00004118 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4119 auto *CounterVar = buildDeclRefExpr(
4120 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4121 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4122 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004123 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4124 IS.CounterInit);
4125 if (!Init.isUsable()) {
4126 HasErrors = true;
4127 break;
4128 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004129 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004130 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004131 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4132 if (!Update.isUsable()) {
4133 HasErrors = true;
4134 break;
4135 }
4136
4137 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4138 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004139 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004140 IS.NumIterations, IS.CounterStep, IS.Subtract);
4141 if (!Final.isUsable()) {
4142 HasErrors = true;
4143 break;
4144 }
4145
4146 // Build Div for the next iteration: Div <- Div * IS.NumIters
4147 if (Cnt != 0) {
4148 if (Div.isUnset())
4149 Div = IS.NumIterations;
4150 else
4151 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4152 IS.NumIterations);
4153
4154 // Add parentheses (for debugging purposes only).
4155 if (Div.isUsable())
4156 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4157 if (!Div.isUsable()) {
4158 HasErrors = true;
4159 break;
4160 }
4161 }
4162 if (!Update.isUsable() || !Final.isUsable()) {
4163 HasErrors = true;
4164 break;
4165 }
4166 // Save results
4167 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004168 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004169 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004170 Built.Updates[Cnt] = Update.get();
4171 Built.Finals[Cnt] = Final.get();
4172 }
4173 }
4174
4175 if (HasErrors)
4176 return 0;
4177
4178 // Save results
4179 Built.IterationVarRef = IV.get();
4180 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004181 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004182 Built.CalcLastIteration =
4183 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004184 Built.PreCond = PreCond.get();
4185 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004186 Built.Init = Init.get();
4187 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004188 Built.LB = LB.get();
4189 Built.UB = UB.get();
4190 Built.IL = IL.get();
4191 Built.ST = ST.get();
4192 Built.EUB = EUB.get();
4193 Built.NLB = NextLB.get();
4194 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004195
Alexey Bataevabfc0692014-06-25 06:52:00 +00004196 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197}
4198
Alexey Bataev10e775f2015-07-30 11:36:16 +00004199static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004200 auto CollapseClauses =
4201 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4202 if (CollapseClauses.begin() != CollapseClauses.end())
4203 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004204 return nullptr;
4205}
4206
Alexey Bataev10e775f2015-07-30 11:36:16 +00004207static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004208 auto OrderedClauses =
4209 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4210 if (OrderedClauses.begin() != OrderedClauses.end())
4211 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004212 return nullptr;
4213}
4214
Alexey Bataev66b15b52015-08-21 11:14:16 +00004215static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4216 const Expr *Safelen) {
4217 llvm::APSInt SimdlenRes, SafelenRes;
4218 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4219 Simdlen->isInstantiationDependent() ||
4220 Simdlen->containsUnexpandedParameterPack())
4221 return false;
4222 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4223 Safelen->isInstantiationDependent() ||
4224 Safelen->containsUnexpandedParameterPack())
4225 return false;
4226 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4227 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4228 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4229 // If both simdlen and safelen clauses are specified, the value of the simdlen
4230 // parameter must be less than or equal to the value of the safelen parameter.
4231 if (SimdlenRes > SafelenRes) {
4232 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4233 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4234 return true;
4235 }
4236 return false;
4237}
4238
Alexey Bataev4acb8592014-07-07 13:01:15 +00004239StmtResult Sema::ActOnOpenMPSimdDirective(
4240 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4241 SourceLocation EndLoc,
4242 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004243 if (!AStmt)
4244 return StmtError();
4245
4246 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004247 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004248 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4249 // define the nested loops number.
4250 unsigned NestedLoopCount = CheckOpenMPLoop(
4251 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4252 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004253 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004254 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004255
Alexander Musmana5f070a2014-10-01 06:03:56 +00004256 assert((CurContext->isDependentContext() || B.builtAll()) &&
4257 "omp simd loop exprs were not built");
4258
Alexander Musman3276a272015-03-21 10:12:56 +00004259 if (!CurContext->isDependentContext()) {
4260 // Finalize the clauses that need pre-built expressions for CodeGen.
4261 for (auto C : Clauses) {
4262 if (auto LC = dyn_cast<OMPLinearClause>(C))
4263 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4264 B.NumIterations, *this, CurScope))
4265 return StmtError();
4266 }
4267 }
4268
Alexey Bataev66b15b52015-08-21 11:14:16 +00004269 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4270 // If both simdlen and safelen clauses are specified, the value of the simdlen
4271 // parameter must be less than or equal to the value of the safelen parameter.
4272 OMPSafelenClause *Safelen = nullptr;
4273 OMPSimdlenClause *Simdlen = nullptr;
4274 for (auto *Clause : Clauses) {
4275 if (Clause->getClauseKind() == OMPC_safelen)
4276 Safelen = cast<OMPSafelenClause>(Clause);
4277 else if (Clause->getClauseKind() == OMPC_simdlen)
4278 Simdlen = cast<OMPSimdlenClause>(Clause);
4279 if (Safelen && Simdlen)
4280 break;
4281 }
4282 if (Simdlen && Safelen &&
4283 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4284 Safelen->getSafelen()))
4285 return StmtError();
4286
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004287 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004288 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4289 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004290}
4291
Alexey Bataev4acb8592014-07-07 13:01:15 +00004292StmtResult Sema::ActOnOpenMPForDirective(
4293 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4294 SourceLocation EndLoc,
4295 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004296 if (!AStmt)
4297 return StmtError();
4298
4299 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004300 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004301 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4302 // define the nested loops number.
4303 unsigned NestedLoopCount = CheckOpenMPLoop(
4304 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4305 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004306 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004307 return StmtError();
4308
Alexander Musmana5f070a2014-10-01 06:03:56 +00004309 assert((CurContext->isDependentContext() || B.builtAll()) &&
4310 "omp for loop exprs were not built");
4311
Alexey Bataev54acd402015-08-04 11:18:19 +00004312 if (!CurContext->isDependentContext()) {
4313 // Finalize the clauses that need pre-built expressions for CodeGen.
4314 for (auto C : Clauses) {
4315 if (auto LC = dyn_cast<OMPLinearClause>(C))
4316 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4317 B.NumIterations, *this, CurScope))
4318 return StmtError();
4319 }
4320 }
4321
Alexey Bataevf29276e2014-06-18 04:14:57 +00004322 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004323 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004324 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004325}
4326
Alexander Musmanf82886e2014-09-18 05:12:34 +00004327StmtResult Sema::ActOnOpenMPForSimdDirective(
4328 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4329 SourceLocation EndLoc,
4330 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004331 if (!AStmt)
4332 return StmtError();
4333
4334 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004335 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004336 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4337 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004338 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004339 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4340 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4341 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004342 if (NestedLoopCount == 0)
4343 return StmtError();
4344
Alexander Musmanc6388682014-12-15 07:07:06 +00004345 assert((CurContext->isDependentContext() || B.builtAll()) &&
4346 "omp for simd loop exprs were not built");
4347
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004348 if (!CurContext->isDependentContext()) {
4349 // Finalize the clauses that need pre-built expressions for CodeGen.
4350 for (auto C : Clauses) {
4351 if (auto LC = dyn_cast<OMPLinearClause>(C))
4352 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4353 B.NumIterations, *this, CurScope))
4354 return StmtError();
4355 }
4356 }
4357
Alexey Bataev66b15b52015-08-21 11:14:16 +00004358 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4359 // If both simdlen and safelen clauses are specified, the value of the simdlen
4360 // parameter must be less than or equal to the value of the safelen parameter.
4361 OMPSafelenClause *Safelen = nullptr;
4362 OMPSimdlenClause *Simdlen = nullptr;
4363 for (auto *Clause : Clauses) {
4364 if (Clause->getClauseKind() == OMPC_safelen)
4365 Safelen = cast<OMPSafelenClause>(Clause);
4366 else if (Clause->getClauseKind() == OMPC_simdlen)
4367 Simdlen = cast<OMPSimdlenClause>(Clause);
4368 if (Safelen && Simdlen)
4369 break;
4370 }
4371 if (Simdlen && Safelen &&
4372 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4373 Safelen->getSafelen()))
4374 return StmtError();
4375
Alexander Musmanf82886e2014-09-18 05:12:34 +00004376 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004377 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4378 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004379}
4380
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004381StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4382 Stmt *AStmt,
4383 SourceLocation StartLoc,
4384 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004385 if (!AStmt)
4386 return StmtError();
4387
4388 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004389 auto BaseStmt = AStmt;
4390 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4391 BaseStmt = CS->getCapturedStmt();
4392 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4393 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004394 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004395 return StmtError();
4396 // All associated statements must be '#pragma omp section' except for
4397 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004398 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004399 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4400 if (SectionStmt)
4401 Diag(SectionStmt->getLocStart(),
4402 diag::err_omp_sections_substmt_not_section);
4403 return StmtError();
4404 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004405 cast<OMPSectionDirective>(SectionStmt)
4406 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004407 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004408 } else {
4409 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4410 return StmtError();
4411 }
4412
4413 getCurFunction()->setHasBranchProtectedScope();
4414
Alexey Bataev25e5b442015-09-15 12:52:43 +00004415 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4416 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004417}
4418
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004419StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4420 SourceLocation StartLoc,
4421 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004422 if (!AStmt)
4423 return StmtError();
4424
4425 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004426
4427 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004428 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004429
Alexey Bataev25e5b442015-09-15 12:52:43 +00004430 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4431 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004432}
4433
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004434StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4435 Stmt *AStmt,
4436 SourceLocation StartLoc,
4437 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004438 if (!AStmt)
4439 return StmtError();
4440
4441 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004442
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004443 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004444
Alexey Bataev3255bf32015-01-19 05:20:46 +00004445 // OpenMP [2.7.3, single Construct, Restrictions]
4446 // The copyprivate clause must not be used with the nowait clause.
4447 OMPClause *Nowait = nullptr;
4448 OMPClause *Copyprivate = nullptr;
4449 for (auto *Clause : Clauses) {
4450 if (Clause->getClauseKind() == OMPC_nowait)
4451 Nowait = Clause;
4452 else if (Clause->getClauseKind() == OMPC_copyprivate)
4453 Copyprivate = Clause;
4454 if (Copyprivate && Nowait) {
4455 Diag(Copyprivate->getLocStart(),
4456 diag::err_omp_single_copyprivate_with_nowait);
4457 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4458 return StmtError();
4459 }
4460 }
4461
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004462 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4463}
4464
Alexander Musman80c22892014-07-17 08:54:58 +00004465StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4466 SourceLocation StartLoc,
4467 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004468 if (!AStmt)
4469 return StmtError();
4470
4471 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004472
4473 getCurFunction()->setHasBranchProtectedScope();
4474
4475 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4476}
4477
Alexey Bataev28c75412015-12-15 08:19:24 +00004478StmtResult Sema::ActOnOpenMPCriticalDirective(
4479 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4480 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004481 if (!AStmt)
4482 return StmtError();
4483
4484 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004485
Alexey Bataev28c75412015-12-15 08:19:24 +00004486 bool ErrorFound = false;
4487 llvm::APSInt Hint;
4488 SourceLocation HintLoc;
4489 bool DependentHint = false;
4490 for (auto *C : Clauses) {
4491 if (C->getClauseKind() == OMPC_hint) {
4492 if (!DirName.getName()) {
4493 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4494 ErrorFound = true;
4495 }
4496 Expr *E = cast<OMPHintClause>(C)->getHint();
4497 if (E->isTypeDependent() || E->isValueDependent() ||
4498 E->isInstantiationDependent())
4499 DependentHint = true;
4500 else {
4501 Hint = E->EvaluateKnownConstInt(Context);
4502 HintLoc = C->getLocStart();
4503 }
4504 }
4505 }
4506 if (ErrorFound)
4507 return StmtError();
4508 auto Pair = DSAStack->getCriticalWithHint(DirName);
4509 if (Pair.first && DirName.getName() && !DependentHint) {
4510 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4511 Diag(StartLoc, diag::err_omp_critical_with_hint);
4512 if (HintLoc.isValid()) {
4513 Diag(HintLoc, diag::note_omp_critical_hint_here)
4514 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4515 } else
4516 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4517 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4518 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4519 << 1
4520 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4521 /*Radix=*/10, /*Signed=*/false);
4522 } else
4523 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4524 }
4525 }
4526
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004527 getCurFunction()->setHasBranchProtectedScope();
4528
Alexey Bataev28c75412015-12-15 08:19:24 +00004529 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4530 Clauses, AStmt);
4531 if (!Pair.first && DirName.getName() && !DependentHint)
4532 DSAStack->addCriticalWithHint(Dir, Hint);
4533 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004534}
4535
Alexey Bataev4acb8592014-07-07 13:01:15 +00004536StmtResult Sema::ActOnOpenMPParallelForDirective(
4537 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4538 SourceLocation EndLoc,
4539 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004540 if (!AStmt)
4541 return StmtError();
4542
Alexey Bataev4acb8592014-07-07 13:01:15 +00004543 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4544 // 1.2.2 OpenMP Language Terminology
4545 // Structured block - An executable statement with a single entry at the
4546 // top and a single exit at the bottom.
4547 // The point of exit cannot be a branch out of the structured block.
4548 // longjmp() and throw() must not violate the entry/exit criteria.
4549 CS->getCapturedDecl()->setNothrow();
4550
Alexander Musmanc6388682014-12-15 07:07:06 +00004551 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004552 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4553 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004554 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004555 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4556 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4557 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004558 if (NestedLoopCount == 0)
4559 return StmtError();
4560
Alexander Musmana5f070a2014-10-01 06:03:56 +00004561 assert((CurContext->isDependentContext() || B.builtAll()) &&
4562 "omp parallel for loop exprs were not built");
4563
Alexey Bataev54acd402015-08-04 11:18:19 +00004564 if (!CurContext->isDependentContext()) {
4565 // Finalize the clauses that need pre-built expressions for CodeGen.
4566 for (auto C : Clauses) {
4567 if (auto LC = dyn_cast<OMPLinearClause>(C))
4568 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4569 B.NumIterations, *this, CurScope))
4570 return StmtError();
4571 }
4572 }
4573
Alexey Bataev4acb8592014-07-07 13:01:15 +00004574 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004575 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004576 NestedLoopCount, Clauses, AStmt, B,
4577 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004578}
4579
Alexander Musmane4e893b2014-09-23 09:33:00 +00004580StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4581 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4582 SourceLocation EndLoc,
4583 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004584 if (!AStmt)
4585 return StmtError();
4586
Alexander Musmane4e893b2014-09-23 09:33:00 +00004587 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4588 // 1.2.2 OpenMP Language Terminology
4589 // Structured block - An executable statement with a single entry at the
4590 // top and a single exit at the bottom.
4591 // The point of exit cannot be a branch out of the structured block.
4592 // longjmp() and throw() must not violate the entry/exit criteria.
4593 CS->getCapturedDecl()->setNothrow();
4594
Alexander Musmanc6388682014-12-15 07:07:06 +00004595 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004596 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4597 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004598 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004599 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4600 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4601 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004602 if (NestedLoopCount == 0)
4603 return StmtError();
4604
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004605 if (!CurContext->isDependentContext()) {
4606 // Finalize the clauses that need pre-built expressions for CodeGen.
4607 for (auto C : Clauses) {
4608 if (auto LC = dyn_cast<OMPLinearClause>(C))
4609 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4610 B.NumIterations, *this, CurScope))
4611 return StmtError();
4612 }
4613 }
4614
Alexey Bataev66b15b52015-08-21 11:14:16 +00004615 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4616 // If both simdlen and safelen clauses are specified, the value of the simdlen
4617 // parameter must be less than or equal to the value of the safelen parameter.
4618 OMPSafelenClause *Safelen = nullptr;
4619 OMPSimdlenClause *Simdlen = nullptr;
4620 for (auto *Clause : Clauses) {
4621 if (Clause->getClauseKind() == OMPC_safelen)
4622 Safelen = cast<OMPSafelenClause>(Clause);
4623 else if (Clause->getClauseKind() == OMPC_simdlen)
4624 Simdlen = cast<OMPSimdlenClause>(Clause);
4625 if (Safelen && Simdlen)
4626 break;
4627 }
4628 if (Simdlen && Safelen &&
4629 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4630 Safelen->getSafelen()))
4631 return StmtError();
4632
Alexander Musmane4e893b2014-09-23 09:33:00 +00004633 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004634 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004635 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004636}
4637
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004638StmtResult
4639Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4640 Stmt *AStmt, SourceLocation StartLoc,
4641 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004642 if (!AStmt)
4643 return StmtError();
4644
4645 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004646 auto BaseStmt = AStmt;
4647 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4648 BaseStmt = CS->getCapturedStmt();
4649 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4650 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004651 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004652 return StmtError();
4653 // All associated statements must be '#pragma omp section' except for
4654 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004655 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004656 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4657 if (SectionStmt)
4658 Diag(SectionStmt->getLocStart(),
4659 diag::err_omp_parallel_sections_substmt_not_section);
4660 return StmtError();
4661 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004662 cast<OMPSectionDirective>(SectionStmt)
4663 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004664 }
4665 } else {
4666 Diag(AStmt->getLocStart(),
4667 diag::err_omp_parallel_sections_not_compound_stmt);
4668 return StmtError();
4669 }
4670
4671 getCurFunction()->setHasBranchProtectedScope();
4672
Alexey Bataev25e5b442015-09-15 12:52:43 +00004673 return OMPParallelSectionsDirective::Create(
4674 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004675}
4676
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004677StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4678 Stmt *AStmt, SourceLocation StartLoc,
4679 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004680 if (!AStmt)
4681 return StmtError();
4682
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004683 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4684 // 1.2.2 OpenMP Language Terminology
4685 // Structured block - An executable statement with a single entry at the
4686 // top and a single exit at the bottom.
4687 // The point of exit cannot be a branch out of the structured block.
4688 // longjmp() and throw() must not violate the entry/exit criteria.
4689 CS->getCapturedDecl()->setNothrow();
4690
4691 getCurFunction()->setHasBranchProtectedScope();
4692
Alexey Bataev25e5b442015-09-15 12:52:43 +00004693 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4694 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004695}
4696
Alexey Bataev68446b72014-07-18 07:47:19 +00004697StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4698 SourceLocation EndLoc) {
4699 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4700}
4701
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004702StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4703 SourceLocation EndLoc) {
4704 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4705}
4706
Alexey Bataev2df347a2014-07-18 10:17:07 +00004707StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4708 SourceLocation EndLoc) {
4709 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4710}
4711
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004712StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4713 SourceLocation StartLoc,
4714 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004715 if (!AStmt)
4716 return StmtError();
4717
4718 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004719
4720 getCurFunction()->setHasBranchProtectedScope();
4721
4722 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4723}
4724
Alexey Bataev6125da92014-07-21 11:26:11 +00004725StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4726 SourceLocation StartLoc,
4727 SourceLocation EndLoc) {
4728 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4729 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4730}
4731
Alexey Bataev346265e2015-09-25 10:37:12 +00004732StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4733 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004734 SourceLocation StartLoc,
4735 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004736 OMPClause *DependFound = nullptr;
4737 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004738 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004739 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004740 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004741 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004742 for (auto *C : Clauses) {
4743 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4744 DependFound = C;
4745 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4746 if (DependSourceClause) {
4747 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4748 << getOpenMPDirectiveName(OMPD_ordered)
4749 << getOpenMPClauseName(OMPC_depend) << 2;
4750 ErrorFound = true;
4751 } else
4752 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004753 if (DependSinkClause) {
4754 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4755 << 0;
4756 ErrorFound = true;
4757 }
4758 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4759 if (DependSourceClause) {
4760 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4761 << 1;
4762 ErrorFound = true;
4763 }
4764 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004765 }
4766 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004767 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004768 else if (C->getClauseKind() == OMPC_simd)
4769 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004770 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004771 if (!ErrorFound && !SC &&
4772 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004773 // OpenMP [2.8.1,simd Construct, Restrictions]
4774 // An ordered construct with the simd clause is the only OpenMP construct
4775 // that can appear in the simd region.
4776 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004777 ErrorFound = true;
4778 } else if (DependFound && (TC || SC)) {
4779 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4780 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4781 ErrorFound = true;
4782 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4783 Diag(DependFound->getLocStart(),
4784 diag::err_omp_ordered_directive_without_param);
4785 ErrorFound = true;
4786 } else if (TC || Clauses.empty()) {
4787 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4788 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4789 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4790 << (TC != nullptr);
4791 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4792 ErrorFound = true;
4793 }
4794 }
4795 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004796 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004797
4798 if (AStmt) {
4799 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4800
4801 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004802 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004803
4804 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004805}
4806
Alexey Bataev1d160b12015-03-13 12:27:31 +00004807namespace {
4808/// \brief Helper class for checking expression in 'omp atomic [update]'
4809/// construct.
4810class OpenMPAtomicUpdateChecker {
4811 /// \brief Error results for atomic update expressions.
4812 enum ExprAnalysisErrorCode {
4813 /// \brief A statement is not an expression statement.
4814 NotAnExpression,
4815 /// \brief Expression is not builtin binary or unary operation.
4816 NotABinaryOrUnaryExpression,
4817 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4818 NotAnUnaryIncDecExpression,
4819 /// \brief An expression is not of scalar type.
4820 NotAScalarType,
4821 /// \brief A binary operation is not an assignment operation.
4822 NotAnAssignmentOp,
4823 /// \brief RHS part of the binary operation is not a binary expression.
4824 NotABinaryExpression,
4825 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4826 /// expression.
4827 NotABinaryOperator,
4828 /// \brief RHS binary operation does not have reference to the updated LHS
4829 /// part.
4830 NotAnUpdateExpression,
4831 /// \brief No errors is found.
4832 NoError
4833 };
4834 /// \brief Reference to Sema.
4835 Sema &SemaRef;
4836 /// \brief A location for note diagnostics (when error is found).
4837 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004838 /// \brief 'x' lvalue part of the source atomic expression.
4839 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004840 /// \brief 'expr' rvalue part of the source atomic expression.
4841 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004842 /// \brief Helper expression of the form
4843 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4844 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4845 Expr *UpdateExpr;
4846 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4847 /// important for non-associative operations.
4848 bool IsXLHSInRHSPart;
4849 BinaryOperatorKind Op;
4850 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004851 /// \brief true if the source expression is a postfix unary operation, false
4852 /// if it is a prefix unary operation.
4853 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004854
4855public:
4856 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004857 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004858 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004859 /// \brief Check specified statement that it is suitable for 'atomic update'
4860 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004861 /// expression. If DiagId and NoteId == 0, then only check is performed
4862 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004863 /// \param DiagId Diagnostic which should be emitted if error is found.
4864 /// \param NoteId Diagnostic note for the main error message.
4865 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004866 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004867 /// \brief Return the 'x' lvalue part of the source atomic expression.
4868 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004869 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4870 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004871 /// \brief Return the update expression used in calculation of the updated
4872 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4873 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4874 Expr *getUpdateExpr() const { return UpdateExpr; }
4875 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4876 /// false otherwise.
4877 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4878
Alexey Bataevb78ca832015-04-01 03:33:17 +00004879 /// \brief true if the source expression is a postfix unary operation, false
4880 /// if it is a prefix unary operation.
4881 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4882
Alexey Bataev1d160b12015-03-13 12:27:31 +00004883private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004884 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4885 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004886};
4887} // namespace
4888
4889bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4890 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4891 ExprAnalysisErrorCode ErrorFound = NoError;
4892 SourceLocation ErrorLoc, NoteLoc;
4893 SourceRange ErrorRange, NoteRange;
4894 // Allowed constructs are:
4895 // x = x binop expr;
4896 // x = expr binop x;
4897 if (AtomicBinOp->getOpcode() == BO_Assign) {
4898 X = AtomicBinOp->getLHS();
4899 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4900 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4901 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4902 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4903 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004904 Op = AtomicInnerBinOp->getOpcode();
4905 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004906 auto *LHS = AtomicInnerBinOp->getLHS();
4907 auto *RHS = AtomicInnerBinOp->getRHS();
4908 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4909 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4910 /*Canonical=*/true);
4911 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4912 /*Canonical=*/true);
4913 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4914 /*Canonical=*/true);
4915 if (XId == LHSId) {
4916 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004917 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004918 } else if (XId == RHSId) {
4919 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004920 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004921 } else {
4922 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4923 ErrorRange = AtomicInnerBinOp->getSourceRange();
4924 NoteLoc = X->getExprLoc();
4925 NoteRange = X->getSourceRange();
4926 ErrorFound = NotAnUpdateExpression;
4927 }
4928 } else {
4929 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4930 ErrorRange = AtomicInnerBinOp->getSourceRange();
4931 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4932 NoteRange = SourceRange(NoteLoc, NoteLoc);
4933 ErrorFound = NotABinaryOperator;
4934 }
4935 } else {
4936 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4937 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4938 ErrorFound = NotABinaryExpression;
4939 }
4940 } else {
4941 ErrorLoc = AtomicBinOp->getExprLoc();
4942 ErrorRange = AtomicBinOp->getSourceRange();
4943 NoteLoc = AtomicBinOp->getOperatorLoc();
4944 NoteRange = SourceRange(NoteLoc, NoteLoc);
4945 ErrorFound = NotAnAssignmentOp;
4946 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004947 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004948 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4949 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4950 return true;
4951 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004952 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004953 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004954}
4955
4956bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4957 unsigned NoteId) {
4958 ExprAnalysisErrorCode ErrorFound = NoError;
4959 SourceLocation ErrorLoc, NoteLoc;
4960 SourceRange ErrorRange, NoteRange;
4961 // Allowed constructs are:
4962 // x++;
4963 // x--;
4964 // ++x;
4965 // --x;
4966 // x binop= expr;
4967 // x = x binop expr;
4968 // x = expr binop x;
4969 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4970 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4971 if (AtomicBody->getType()->isScalarType() ||
4972 AtomicBody->isInstantiationDependent()) {
4973 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4974 AtomicBody->IgnoreParenImpCasts())) {
4975 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004976 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004977 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004978 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004979 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004980 X = AtomicCompAssignOp->getLHS();
4981 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004982 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4983 AtomicBody->IgnoreParenImpCasts())) {
4984 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004985 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4986 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004987 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004988 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4989 // Check for Unary Operation
4990 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004991 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004992 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4993 OpLoc = AtomicUnaryOp->getOperatorLoc();
4994 X = AtomicUnaryOp->getSubExpr();
4995 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4996 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004997 } else {
4998 ErrorFound = NotAnUnaryIncDecExpression;
4999 ErrorLoc = AtomicUnaryOp->getExprLoc();
5000 ErrorRange = AtomicUnaryOp->getSourceRange();
5001 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5002 NoteRange = SourceRange(NoteLoc, NoteLoc);
5003 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005004 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005005 ErrorFound = NotABinaryOrUnaryExpression;
5006 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5007 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5008 }
5009 } else {
5010 ErrorFound = NotAScalarType;
5011 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5012 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5013 }
5014 } else {
5015 ErrorFound = NotAnExpression;
5016 NoteLoc = ErrorLoc = S->getLocStart();
5017 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5018 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005019 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005020 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5021 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5022 return true;
5023 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005024 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005025 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005026 // Build an update expression of form 'OpaqueValueExpr(x) binop
5027 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5028 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5029 auto *OVEX = new (SemaRef.getASTContext())
5030 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5031 auto *OVEExpr = new (SemaRef.getASTContext())
5032 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5033 auto Update =
5034 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5035 IsXLHSInRHSPart ? OVEExpr : OVEX);
5036 if (Update.isInvalid())
5037 return true;
5038 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5039 Sema::AA_Casting);
5040 if (Update.isInvalid())
5041 return true;
5042 UpdateExpr = Update.get();
5043 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005044 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005045}
5046
Alexey Bataev0162e452014-07-22 10:10:35 +00005047StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5048 Stmt *AStmt,
5049 SourceLocation StartLoc,
5050 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005051 if (!AStmt)
5052 return StmtError();
5053
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005054 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005055 // 1.2.2 OpenMP Language Terminology
5056 // Structured block - An executable statement with a single entry at the
5057 // top and a single exit at the bottom.
5058 // The point of exit cannot be a branch out of the structured block.
5059 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005060 OpenMPClauseKind AtomicKind = OMPC_unknown;
5061 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005062 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005063 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005064 C->getClauseKind() == OMPC_update ||
5065 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005066 if (AtomicKind != OMPC_unknown) {
5067 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5068 << SourceRange(C->getLocStart(), C->getLocEnd());
5069 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5070 << getOpenMPClauseName(AtomicKind);
5071 } else {
5072 AtomicKind = C->getClauseKind();
5073 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005074 }
5075 }
5076 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005077
Alexey Bataev459dec02014-07-24 06:46:57 +00005078 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005079 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5080 Body = EWC->getSubExpr();
5081
Alexey Bataev62cec442014-11-18 10:14:22 +00005082 Expr *X = nullptr;
5083 Expr *V = nullptr;
5084 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005085 Expr *UE = nullptr;
5086 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005087 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005088 // OpenMP [2.12.6, atomic Construct]
5089 // In the next expressions:
5090 // * x and v (as applicable) are both l-value expressions with scalar type.
5091 // * During the execution of an atomic region, multiple syntactic
5092 // occurrences of x must designate the same storage location.
5093 // * Neither of v and expr (as applicable) may access the storage location
5094 // designated by x.
5095 // * Neither of x and expr (as applicable) may access the storage location
5096 // designated by v.
5097 // * expr is an expression with scalar type.
5098 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5099 // * binop, binop=, ++, and -- are not overloaded operators.
5100 // * The expression x binop expr must be numerically equivalent to x binop
5101 // (expr). This requirement is satisfied if the operators in expr have
5102 // precedence greater than binop, or by using parentheses around expr or
5103 // subexpressions of expr.
5104 // * The expression expr binop x must be numerically equivalent to (expr)
5105 // binop x. This requirement is satisfied if the operators in expr have
5106 // precedence equal to or greater than binop, or by using parentheses around
5107 // expr or subexpressions of expr.
5108 // * For forms that allow multiple occurrences of x, the number of times
5109 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005110 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005111 enum {
5112 NotAnExpression,
5113 NotAnAssignmentOp,
5114 NotAScalarType,
5115 NotAnLValue,
5116 NoError
5117 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005118 SourceLocation ErrorLoc, NoteLoc;
5119 SourceRange ErrorRange, NoteRange;
5120 // If clause is read:
5121 // v = x;
5122 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5123 auto AtomicBinOp =
5124 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5125 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5126 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5127 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5128 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5129 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5130 if (!X->isLValue() || !V->isLValue()) {
5131 auto NotLValueExpr = X->isLValue() ? V : X;
5132 ErrorFound = NotAnLValue;
5133 ErrorLoc = AtomicBinOp->getExprLoc();
5134 ErrorRange = AtomicBinOp->getSourceRange();
5135 NoteLoc = NotLValueExpr->getExprLoc();
5136 NoteRange = NotLValueExpr->getSourceRange();
5137 }
5138 } else if (!X->isInstantiationDependent() ||
5139 !V->isInstantiationDependent()) {
5140 auto NotScalarExpr =
5141 (X->isInstantiationDependent() || X->getType()->isScalarType())
5142 ? V
5143 : X;
5144 ErrorFound = NotAScalarType;
5145 ErrorLoc = AtomicBinOp->getExprLoc();
5146 ErrorRange = AtomicBinOp->getSourceRange();
5147 NoteLoc = NotScalarExpr->getExprLoc();
5148 NoteRange = NotScalarExpr->getSourceRange();
5149 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005150 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005151 ErrorFound = NotAnAssignmentOp;
5152 ErrorLoc = AtomicBody->getExprLoc();
5153 ErrorRange = AtomicBody->getSourceRange();
5154 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5155 : AtomicBody->getExprLoc();
5156 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5157 : AtomicBody->getSourceRange();
5158 }
5159 } else {
5160 ErrorFound = NotAnExpression;
5161 NoteLoc = ErrorLoc = Body->getLocStart();
5162 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005163 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005164 if (ErrorFound != NoError) {
5165 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5166 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005167 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5168 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005169 return StmtError();
5170 } else if (CurContext->isDependentContext())
5171 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005172 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005173 enum {
5174 NotAnExpression,
5175 NotAnAssignmentOp,
5176 NotAScalarType,
5177 NotAnLValue,
5178 NoError
5179 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005180 SourceLocation ErrorLoc, NoteLoc;
5181 SourceRange ErrorRange, NoteRange;
5182 // If clause is write:
5183 // x = expr;
5184 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5185 auto AtomicBinOp =
5186 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5187 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005188 X = AtomicBinOp->getLHS();
5189 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005190 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5191 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5192 if (!X->isLValue()) {
5193 ErrorFound = NotAnLValue;
5194 ErrorLoc = AtomicBinOp->getExprLoc();
5195 ErrorRange = AtomicBinOp->getSourceRange();
5196 NoteLoc = X->getExprLoc();
5197 NoteRange = X->getSourceRange();
5198 }
5199 } else if (!X->isInstantiationDependent() ||
5200 !E->isInstantiationDependent()) {
5201 auto NotScalarExpr =
5202 (X->isInstantiationDependent() || X->getType()->isScalarType())
5203 ? E
5204 : X;
5205 ErrorFound = NotAScalarType;
5206 ErrorLoc = AtomicBinOp->getExprLoc();
5207 ErrorRange = AtomicBinOp->getSourceRange();
5208 NoteLoc = NotScalarExpr->getExprLoc();
5209 NoteRange = NotScalarExpr->getSourceRange();
5210 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005211 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005212 ErrorFound = NotAnAssignmentOp;
5213 ErrorLoc = AtomicBody->getExprLoc();
5214 ErrorRange = AtomicBody->getSourceRange();
5215 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5216 : AtomicBody->getExprLoc();
5217 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5218 : AtomicBody->getSourceRange();
5219 }
5220 } else {
5221 ErrorFound = NotAnExpression;
5222 NoteLoc = ErrorLoc = Body->getLocStart();
5223 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005224 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005225 if (ErrorFound != NoError) {
5226 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5227 << ErrorRange;
5228 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5229 << NoteRange;
5230 return StmtError();
5231 } else if (CurContext->isDependentContext())
5232 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005233 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005234 // If clause is update:
5235 // x++;
5236 // x--;
5237 // ++x;
5238 // --x;
5239 // x binop= expr;
5240 // x = x binop expr;
5241 // x = expr binop x;
5242 OpenMPAtomicUpdateChecker Checker(*this);
5243 if (Checker.checkStatement(
5244 Body, (AtomicKind == OMPC_update)
5245 ? diag::err_omp_atomic_update_not_expression_statement
5246 : diag::err_omp_atomic_not_expression_statement,
5247 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005248 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005249 if (!CurContext->isDependentContext()) {
5250 E = Checker.getExpr();
5251 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005252 UE = Checker.getUpdateExpr();
5253 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005254 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005255 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005256 enum {
5257 NotAnAssignmentOp,
5258 NotACompoundStatement,
5259 NotTwoSubstatements,
5260 NotASpecificExpression,
5261 NoError
5262 } ErrorFound = NoError;
5263 SourceLocation ErrorLoc, NoteLoc;
5264 SourceRange ErrorRange, NoteRange;
5265 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5266 // If clause is a capture:
5267 // v = x++;
5268 // v = x--;
5269 // v = ++x;
5270 // v = --x;
5271 // v = x binop= expr;
5272 // v = x = x binop expr;
5273 // v = x = expr binop x;
5274 auto *AtomicBinOp =
5275 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5276 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5277 V = AtomicBinOp->getLHS();
5278 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5279 OpenMPAtomicUpdateChecker Checker(*this);
5280 if (Checker.checkStatement(
5281 Body, diag::err_omp_atomic_capture_not_expression_statement,
5282 diag::note_omp_atomic_update))
5283 return StmtError();
5284 E = Checker.getExpr();
5285 X = Checker.getX();
5286 UE = Checker.getUpdateExpr();
5287 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5288 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005289 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005290 ErrorLoc = AtomicBody->getExprLoc();
5291 ErrorRange = AtomicBody->getSourceRange();
5292 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5293 : AtomicBody->getExprLoc();
5294 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5295 : AtomicBody->getSourceRange();
5296 ErrorFound = NotAnAssignmentOp;
5297 }
5298 if (ErrorFound != NoError) {
5299 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5300 << ErrorRange;
5301 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5302 return StmtError();
5303 } else if (CurContext->isDependentContext()) {
5304 UE = V = E = X = nullptr;
5305 }
5306 } else {
5307 // If clause is a capture:
5308 // { v = x; x = expr; }
5309 // { v = x; x++; }
5310 // { v = x; x--; }
5311 // { v = x; ++x; }
5312 // { v = x; --x; }
5313 // { v = x; x binop= expr; }
5314 // { v = x; x = x binop expr; }
5315 // { v = x; x = expr binop x; }
5316 // { x++; v = x; }
5317 // { x--; v = x; }
5318 // { ++x; v = x; }
5319 // { --x; v = x; }
5320 // { x binop= expr; v = x; }
5321 // { x = x binop expr; v = x; }
5322 // { x = expr binop x; v = x; }
5323 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5324 // Check that this is { expr1; expr2; }
5325 if (CS->size() == 2) {
5326 auto *First = CS->body_front();
5327 auto *Second = CS->body_back();
5328 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5329 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5330 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5331 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5332 // Need to find what subexpression is 'v' and what is 'x'.
5333 OpenMPAtomicUpdateChecker Checker(*this);
5334 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5335 BinaryOperator *BinOp = nullptr;
5336 if (IsUpdateExprFound) {
5337 BinOp = dyn_cast<BinaryOperator>(First);
5338 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5339 }
5340 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5341 // { v = x; x++; }
5342 // { v = x; x--; }
5343 // { v = x; ++x; }
5344 // { v = x; --x; }
5345 // { v = x; x binop= expr; }
5346 // { v = x; x = x binop expr; }
5347 // { v = x; x = expr binop x; }
5348 // Check that the first expression has form v = x.
5349 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5350 llvm::FoldingSetNodeID XId, PossibleXId;
5351 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5352 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5353 IsUpdateExprFound = XId == PossibleXId;
5354 if (IsUpdateExprFound) {
5355 V = BinOp->getLHS();
5356 X = Checker.getX();
5357 E = Checker.getExpr();
5358 UE = Checker.getUpdateExpr();
5359 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005360 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005361 }
5362 }
5363 if (!IsUpdateExprFound) {
5364 IsUpdateExprFound = !Checker.checkStatement(First);
5365 BinOp = nullptr;
5366 if (IsUpdateExprFound) {
5367 BinOp = dyn_cast<BinaryOperator>(Second);
5368 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5369 }
5370 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5371 // { x++; v = x; }
5372 // { x--; v = x; }
5373 // { ++x; v = x; }
5374 // { --x; v = x; }
5375 // { x binop= expr; v = x; }
5376 // { x = x binop expr; v = x; }
5377 // { x = expr binop x; v = x; }
5378 // Check that the second expression has form v = x.
5379 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5380 llvm::FoldingSetNodeID XId, PossibleXId;
5381 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5382 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5383 IsUpdateExprFound = XId == PossibleXId;
5384 if (IsUpdateExprFound) {
5385 V = BinOp->getLHS();
5386 X = Checker.getX();
5387 E = Checker.getExpr();
5388 UE = Checker.getUpdateExpr();
5389 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005390 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005391 }
5392 }
5393 }
5394 if (!IsUpdateExprFound) {
5395 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005396 auto *FirstExpr = dyn_cast<Expr>(First);
5397 auto *SecondExpr = dyn_cast<Expr>(Second);
5398 if (!FirstExpr || !SecondExpr ||
5399 !(FirstExpr->isInstantiationDependent() ||
5400 SecondExpr->isInstantiationDependent())) {
5401 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5402 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005403 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005404 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5405 : First->getLocStart();
5406 NoteRange = ErrorRange = FirstBinOp
5407 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005408 : SourceRange(ErrorLoc, ErrorLoc);
5409 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005410 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5411 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5412 ErrorFound = NotAnAssignmentOp;
5413 NoteLoc = ErrorLoc = SecondBinOp
5414 ? SecondBinOp->getOperatorLoc()
5415 : Second->getLocStart();
5416 NoteRange = ErrorRange =
5417 SecondBinOp ? SecondBinOp->getSourceRange()
5418 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005419 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005420 auto *PossibleXRHSInFirst =
5421 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5422 auto *PossibleXLHSInSecond =
5423 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5424 llvm::FoldingSetNodeID X1Id, X2Id;
5425 PossibleXRHSInFirst->Profile(X1Id, Context,
5426 /*Canonical=*/true);
5427 PossibleXLHSInSecond->Profile(X2Id, Context,
5428 /*Canonical=*/true);
5429 IsUpdateExprFound = X1Id == X2Id;
5430 if (IsUpdateExprFound) {
5431 V = FirstBinOp->getLHS();
5432 X = SecondBinOp->getLHS();
5433 E = SecondBinOp->getRHS();
5434 UE = nullptr;
5435 IsXLHSInRHSPart = false;
5436 IsPostfixUpdate = true;
5437 } else {
5438 ErrorFound = NotASpecificExpression;
5439 ErrorLoc = FirstBinOp->getExprLoc();
5440 ErrorRange = FirstBinOp->getSourceRange();
5441 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5442 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5443 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005444 }
5445 }
5446 }
5447 }
5448 } else {
5449 NoteLoc = ErrorLoc = Body->getLocStart();
5450 NoteRange = ErrorRange =
5451 SourceRange(Body->getLocStart(), Body->getLocStart());
5452 ErrorFound = NotTwoSubstatements;
5453 }
5454 } else {
5455 NoteLoc = ErrorLoc = Body->getLocStart();
5456 NoteRange = ErrorRange =
5457 SourceRange(Body->getLocStart(), Body->getLocStart());
5458 ErrorFound = NotACompoundStatement;
5459 }
5460 if (ErrorFound != NoError) {
5461 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5462 << ErrorRange;
5463 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5464 return StmtError();
5465 } else if (CurContext->isDependentContext()) {
5466 UE = V = E = X = nullptr;
5467 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005468 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005469 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005470
5471 getCurFunction()->setHasBranchProtectedScope();
5472
Alexey Bataev62cec442014-11-18 10:14:22 +00005473 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005474 X, V, E, UE, IsXLHSInRHSPart,
5475 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005476}
5477
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005478StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5479 Stmt *AStmt,
5480 SourceLocation StartLoc,
5481 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005482 if (!AStmt)
5483 return StmtError();
5484
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005485 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5486 // 1.2.2 OpenMP Language Terminology
5487 // Structured block - An executable statement with a single entry at the
5488 // top and a single exit at the bottom.
5489 // The point of exit cannot be a branch out of the structured block.
5490 // longjmp() and throw() must not violate the entry/exit criteria.
5491 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005492
Alexey Bataev13314bf2014-10-09 04:18:56 +00005493 // OpenMP [2.16, Nesting of Regions]
5494 // If specified, a teams construct must be contained within a target
5495 // construct. That target construct must contain no statements or directives
5496 // outside of the teams construct.
5497 if (DSAStack->hasInnerTeamsRegion()) {
5498 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5499 bool OMPTeamsFound = true;
5500 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5501 auto I = CS->body_begin();
5502 while (I != CS->body_end()) {
5503 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5504 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5505 OMPTeamsFound = false;
5506 break;
5507 }
5508 ++I;
5509 }
5510 assert(I != CS->body_end() && "Not found statement");
5511 S = *I;
5512 }
5513 if (!OMPTeamsFound) {
5514 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5515 Diag(DSAStack->getInnerTeamsRegionLoc(),
5516 diag::note_omp_nested_teams_construct_here);
5517 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5518 << isa<OMPExecutableDirective>(S);
5519 return StmtError();
5520 }
5521 }
5522
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005523 getCurFunction()->setHasBranchProtectedScope();
5524
5525 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5526}
5527
Samuel Antaodf67fc42016-01-19 19:15:56 +00005528/// \brief Check for existence of a map clause in the list of clauses.
5529static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5530 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5531 I != E; ++I) {
5532 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5533 return true;
5534 }
5535 }
5536
5537 return false;
5538}
5539
Michael Wong65f367f2015-07-21 13:44:28 +00005540StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5541 Stmt *AStmt,
5542 SourceLocation StartLoc,
5543 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005544 if (!AStmt)
5545 return StmtError();
5546
5547 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5548
Michael Wong65f367f2015-07-21 13:44:28 +00005549 getCurFunction()->setHasBranchProtectedScope();
5550
5551 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5552 AStmt);
5553}
5554
Samuel Antaodf67fc42016-01-19 19:15:56 +00005555StmtResult
5556Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5557 SourceLocation StartLoc,
5558 SourceLocation EndLoc) {
5559 // OpenMP [2.10.2, Restrictions, p. 99]
5560 // At least one map clause must appear on the directive.
5561 if (!HasMapClause(Clauses)) {
5562 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5563 << getOpenMPDirectiveName(OMPD_target_enter_data);
5564 return StmtError();
5565 }
5566
5567 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5568 Clauses);
5569}
5570
Samuel Antao72590762016-01-19 20:04:50 +00005571StmtResult
5572Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5573 SourceLocation StartLoc,
5574 SourceLocation EndLoc) {
5575 // OpenMP [2.10.3, Restrictions, p. 102]
5576 // At least one map clause must appear on the directive.
5577 if (!HasMapClause(Clauses)) {
5578 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5579 << getOpenMPDirectiveName(OMPD_target_exit_data);
5580 return StmtError();
5581 }
5582
5583 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5584}
5585
Alexey Bataev13314bf2014-10-09 04:18:56 +00005586StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5587 Stmt *AStmt, SourceLocation StartLoc,
5588 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005589 if (!AStmt)
5590 return StmtError();
5591
Alexey Bataev13314bf2014-10-09 04:18:56 +00005592 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5593 // 1.2.2 OpenMP Language Terminology
5594 // Structured block - An executable statement with a single entry at the
5595 // top and a single exit at the bottom.
5596 // The point of exit cannot be a branch out of the structured block.
5597 // longjmp() and throw() must not violate the entry/exit criteria.
5598 CS->getCapturedDecl()->setNothrow();
5599
5600 getCurFunction()->setHasBranchProtectedScope();
5601
5602 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5603}
5604
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005605StmtResult
5606Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5607 SourceLocation EndLoc,
5608 OpenMPDirectiveKind CancelRegion) {
5609 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5610 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5611 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5612 << getOpenMPDirectiveName(CancelRegion);
5613 return StmtError();
5614 }
5615 if (DSAStack->isParentNowaitRegion()) {
5616 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5617 return StmtError();
5618 }
5619 if (DSAStack->isParentOrderedRegion()) {
5620 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5621 return StmtError();
5622 }
5623 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5624 CancelRegion);
5625}
5626
Alexey Bataev87933c72015-09-18 08:07:34 +00005627StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5628 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005629 SourceLocation EndLoc,
5630 OpenMPDirectiveKind CancelRegion) {
5631 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5632 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5633 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5634 << getOpenMPDirectiveName(CancelRegion);
5635 return StmtError();
5636 }
5637 if (DSAStack->isParentNowaitRegion()) {
5638 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5639 return StmtError();
5640 }
5641 if (DSAStack->isParentOrderedRegion()) {
5642 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5643 return StmtError();
5644 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005645 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005646 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5647 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005648}
5649
Alexey Bataev382967a2015-12-08 12:06:20 +00005650static bool checkGrainsizeNumTasksClauses(Sema &S,
5651 ArrayRef<OMPClause *> Clauses) {
5652 OMPClause *PrevClause = nullptr;
5653 bool ErrorFound = false;
5654 for (auto *C : Clauses) {
5655 if (C->getClauseKind() == OMPC_grainsize ||
5656 C->getClauseKind() == OMPC_num_tasks) {
5657 if (!PrevClause)
5658 PrevClause = C;
5659 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5660 S.Diag(C->getLocStart(),
5661 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5662 << getOpenMPClauseName(C->getClauseKind())
5663 << getOpenMPClauseName(PrevClause->getClauseKind());
5664 S.Diag(PrevClause->getLocStart(),
5665 diag::note_omp_previous_grainsize_num_tasks)
5666 << getOpenMPClauseName(PrevClause->getClauseKind());
5667 ErrorFound = true;
5668 }
5669 }
5670 }
5671 return ErrorFound;
5672}
5673
Alexey Bataev49f6e782015-12-01 04:18:41 +00005674StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5675 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5676 SourceLocation EndLoc,
5677 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5678 if (!AStmt)
5679 return StmtError();
5680
5681 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5682 OMPLoopDirective::HelperExprs B;
5683 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5684 // define the nested loops number.
5685 unsigned NestedLoopCount =
5686 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005687 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005688 VarsWithImplicitDSA, B);
5689 if (NestedLoopCount == 0)
5690 return StmtError();
5691
5692 assert((CurContext->isDependentContext() || B.builtAll()) &&
5693 "omp for loop exprs were not built");
5694
Alexey Bataev382967a2015-12-08 12:06:20 +00005695 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5696 // The grainsize clause and num_tasks clause are mutually exclusive and may
5697 // not appear on the same taskloop directive.
5698 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5699 return StmtError();
5700
Alexey Bataev49f6e782015-12-01 04:18:41 +00005701 getCurFunction()->setHasBranchProtectedScope();
5702 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5703 NestedLoopCount, Clauses, AStmt, B);
5704}
5705
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005706StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5707 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5708 SourceLocation EndLoc,
5709 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5710 if (!AStmt)
5711 return StmtError();
5712
5713 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5714 OMPLoopDirective::HelperExprs B;
5715 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5716 // define the nested loops number.
5717 unsigned NestedLoopCount =
5718 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5719 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5720 VarsWithImplicitDSA, B);
5721 if (NestedLoopCount == 0)
5722 return StmtError();
5723
5724 assert((CurContext->isDependentContext() || B.builtAll()) &&
5725 "omp for loop exprs were not built");
5726
Alexey Bataev382967a2015-12-08 12:06:20 +00005727 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5728 // The grainsize clause and num_tasks clause are mutually exclusive and may
5729 // not appear on the same taskloop directive.
5730 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5731 return StmtError();
5732
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005733 getCurFunction()->setHasBranchProtectedScope();
5734 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5735 NestedLoopCount, Clauses, AStmt, B);
5736}
5737
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005738StmtResult Sema::ActOnOpenMPDistributeDirective(
5739 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5740 SourceLocation EndLoc,
5741 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5742 if (!AStmt)
5743 return StmtError();
5744
5745 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5746 OMPLoopDirective::HelperExprs B;
5747 // In presence of clause 'collapse' with number of loops, it will
5748 // define the nested loops number.
5749 unsigned NestedLoopCount =
5750 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5751 nullptr /*ordered not a clause on distribute*/, AStmt,
5752 *this, *DSAStack, VarsWithImplicitDSA, B);
5753 if (NestedLoopCount == 0)
5754 return StmtError();
5755
5756 assert((CurContext->isDependentContext() || B.builtAll()) &&
5757 "omp for loop exprs were not built");
5758
5759 getCurFunction()->setHasBranchProtectedScope();
5760 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5761 NestedLoopCount, Clauses, AStmt, B);
5762}
5763
Alexey Bataeved09d242014-05-28 05:53:51 +00005764OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005765 SourceLocation StartLoc,
5766 SourceLocation LParenLoc,
5767 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005768 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005769 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005770 case OMPC_final:
5771 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5772 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005773 case OMPC_num_threads:
5774 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5775 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005776 case OMPC_safelen:
5777 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5778 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005779 case OMPC_simdlen:
5780 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5781 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005782 case OMPC_collapse:
5783 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5784 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005785 case OMPC_ordered:
5786 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5787 break;
Michael Wonge710d542015-08-07 16:16:36 +00005788 case OMPC_device:
5789 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5790 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005791 case OMPC_num_teams:
5792 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5793 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005794 case OMPC_thread_limit:
5795 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5796 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005797 case OMPC_priority:
5798 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5799 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005800 case OMPC_grainsize:
5801 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5802 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005803 case OMPC_num_tasks:
5804 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5805 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005806 case OMPC_hint:
5807 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5808 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005809 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005810 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005811 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005812 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005813 case OMPC_private:
5814 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005815 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005816 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005817 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005818 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005819 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005820 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005821 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005822 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005823 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005824 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005825 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005826 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005827 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005828 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005829 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005830 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005831 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005832 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005833 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005834 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005835 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005836 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00005837 case OMPC_dist_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005838 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005839 llvm_unreachable("Clause is not allowed.");
5840 }
5841 return Res;
5842}
5843
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005844OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5845 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005846 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005847 SourceLocation NameModifierLoc,
5848 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005849 SourceLocation EndLoc) {
5850 Expr *ValExpr = Condition;
5851 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5852 !Condition->isInstantiationDependent() &&
5853 !Condition->containsUnexpandedParameterPack()) {
5854 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005855 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005856 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005857 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005858
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005859 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005860 }
5861
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005862 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5863 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005864}
5865
Alexey Bataev3778b602014-07-17 07:32:53 +00005866OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5867 SourceLocation StartLoc,
5868 SourceLocation LParenLoc,
5869 SourceLocation EndLoc) {
5870 Expr *ValExpr = Condition;
5871 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5872 !Condition->isInstantiationDependent() &&
5873 !Condition->containsUnexpandedParameterPack()) {
5874 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5875 Condition->getExprLoc(), Condition);
5876 if (Val.isInvalid())
5877 return nullptr;
5878
5879 ValExpr = Val.get();
5880 }
5881
5882 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5883}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005884ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5885 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005886 if (!Op)
5887 return ExprError();
5888
5889 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5890 public:
5891 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005892 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005893 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5894 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005895 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5896 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005897 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5898 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005899 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5900 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005901 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5902 QualType T,
5903 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005904 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5905 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005906 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5907 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005908 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005909 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005910 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005911 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5912 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005913 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5914 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005915 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5916 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005917 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005918 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005919 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005920 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5921 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005922 llvm_unreachable("conversion functions are permitted");
5923 }
5924 } ConvertDiagnoser;
5925 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5926}
5927
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005928static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005929 OpenMPClauseKind CKind,
5930 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005931 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5932 !ValExpr->isInstantiationDependent()) {
5933 SourceLocation Loc = ValExpr->getExprLoc();
5934 ExprResult Value =
5935 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5936 if (Value.isInvalid())
5937 return false;
5938
5939 ValExpr = Value.get();
5940 // The expression must evaluate to a non-negative integer value.
5941 llvm::APSInt Result;
5942 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005943 Result.isSigned() &&
5944 !((!StrictlyPositive && Result.isNonNegative()) ||
5945 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005946 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005947 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5948 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005949 return false;
5950 }
5951 }
5952 return true;
5953}
5954
Alexey Bataev568a8332014-03-06 06:15:19 +00005955OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5956 SourceLocation StartLoc,
5957 SourceLocation LParenLoc,
5958 SourceLocation EndLoc) {
5959 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005960
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005961 // OpenMP [2.5, Restrictions]
5962 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005963 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5964 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005965 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005966
Alexey Bataeved09d242014-05-28 05:53:51 +00005967 return new (Context)
5968 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005969}
5970
Alexey Bataev62c87d22014-03-21 04:51:18 +00005971ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005972 OpenMPClauseKind CKind,
5973 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005974 if (!E)
5975 return ExprError();
5976 if (E->isValueDependent() || E->isTypeDependent() ||
5977 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005978 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005979 llvm::APSInt Result;
5980 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5981 if (ICE.isInvalid())
5982 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005983 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
5984 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005985 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005986 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5987 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005988 return ExprError();
5989 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005990 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5991 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5992 << E->getSourceRange();
5993 return ExprError();
5994 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005995 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
5996 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005997 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005998 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005999 return ICE;
6000}
6001
6002OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6003 SourceLocation LParenLoc,
6004 SourceLocation EndLoc) {
6005 // OpenMP [2.8.1, simd construct, Description]
6006 // The parameter of the safelen clause must be a constant
6007 // positive integer expression.
6008 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6009 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006010 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006011 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006012 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006013}
6014
Alexey Bataev66b15b52015-08-21 11:14:16 +00006015OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6016 SourceLocation LParenLoc,
6017 SourceLocation EndLoc) {
6018 // OpenMP [2.8.1, simd construct, Description]
6019 // The parameter of the simdlen clause must be a constant
6020 // positive integer expression.
6021 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6022 if (Simdlen.isInvalid())
6023 return nullptr;
6024 return new (Context)
6025 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6026}
6027
Alexander Musman64d33f12014-06-04 07:53:32 +00006028OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6029 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006030 SourceLocation LParenLoc,
6031 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006032 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006033 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006034 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006035 // The parameter of the collapse clause must be a constant
6036 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006037 ExprResult NumForLoopsResult =
6038 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6039 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006040 return nullptr;
6041 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006042 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006043}
6044
Alexey Bataev10e775f2015-07-30 11:36:16 +00006045OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6046 SourceLocation EndLoc,
6047 SourceLocation LParenLoc,
6048 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006049 // OpenMP [2.7.1, loop construct, Description]
6050 // OpenMP [2.8.1, simd construct, Description]
6051 // OpenMP [2.9.6, distribute construct, Description]
6052 // The parameter of the ordered clause must be a constant
6053 // positive integer expression if any.
6054 if (NumForLoops && LParenLoc.isValid()) {
6055 ExprResult NumForLoopsResult =
6056 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6057 if (NumForLoopsResult.isInvalid())
6058 return nullptr;
6059 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006060 } else
6061 NumForLoops = nullptr;
6062 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006063 return new (Context)
6064 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6065}
6066
Alexey Bataeved09d242014-05-28 05:53:51 +00006067OMPClause *Sema::ActOnOpenMPSimpleClause(
6068 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6069 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006070 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006071 switch (Kind) {
6072 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006073 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006074 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6075 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006076 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006077 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006078 Res = ActOnOpenMPProcBindClause(
6079 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6080 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006081 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006082 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006083 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006084 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006085 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006086 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006087 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006088 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006089 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006090 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006091 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006092 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006093 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006094 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006095 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006096 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006097 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006098 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006099 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006100 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006101 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006102 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006103 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006104 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006105 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006106 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006107 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006108 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006109 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006110 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006111 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006112 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006113 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006114 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006115 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006116 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006117 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006118 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006119 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006120 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006121 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006122 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006123 llvm_unreachable("Clause is not allowed.");
6124 }
6125 return Res;
6126}
6127
Alexey Bataev6402bca2015-12-28 07:25:51 +00006128static std::string
6129getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6130 ArrayRef<unsigned> Exclude = llvm::None) {
6131 std::string Values;
6132 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6133 unsigned Skipped = Exclude.size();
6134 auto S = Exclude.begin(), E = Exclude.end();
6135 for (unsigned i = First; i < Last; ++i) {
6136 if (std::find(S, E, i) != E) {
6137 --Skipped;
6138 continue;
6139 }
6140 Values += "'";
6141 Values += getOpenMPSimpleClauseTypeName(K, i);
6142 Values += "'";
6143 if (i == Bound - Skipped)
6144 Values += " or ";
6145 else if (i != Bound + 1 - Skipped)
6146 Values += ", ";
6147 }
6148 return Values;
6149}
6150
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006151OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6152 SourceLocation KindKwLoc,
6153 SourceLocation StartLoc,
6154 SourceLocation LParenLoc,
6155 SourceLocation EndLoc) {
6156 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006157 static_assert(OMPC_DEFAULT_unknown > 0,
6158 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006159 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006160 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6161 /*Last=*/OMPC_DEFAULT_unknown)
6162 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006163 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006164 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006165 switch (Kind) {
6166 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006167 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006168 break;
6169 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006170 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006171 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006172 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006173 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006174 break;
6175 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006176 return new (Context)
6177 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006178}
6179
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006180OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6181 SourceLocation KindKwLoc,
6182 SourceLocation StartLoc,
6183 SourceLocation LParenLoc,
6184 SourceLocation EndLoc) {
6185 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006186 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006187 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6188 /*Last=*/OMPC_PROC_BIND_unknown)
6189 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006190 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006191 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006192 return new (Context)
6193 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006194}
6195
Alexey Bataev56dafe82014-06-20 07:16:17 +00006196OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006197 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006198 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006199 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006200 SourceLocation EndLoc) {
6201 OMPClause *Res = nullptr;
6202 switch (Kind) {
6203 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006204 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6205 assert(Argument.size() == NumberOfElements &&
6206 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006207 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006208 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6209 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6210 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6211 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6212 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006213 break;
6214 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006215 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6216 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6217 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6218 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006219 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006220 case OMPC_dist_schedule:
6221 Res = ActOnOpenMPDistScheduleClause(
6222 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6223 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6224 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006225 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006226 case OMPC_num_threads:
6227 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006228 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006229 case OMPC_collapse:
6230 case OMPC_default:
6231 case OMPC_proc_bind:
6232 case OMPC_private:
6233 case OMPC_firstprivate:
6234 case OMPC_lastprivate:
6235 case OMPC_shared:
6236 case OMPC_reduction:
6237 case OMPC_linear:
6238 case OMPC_aligned:
6239 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006240 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006241 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006242 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006243 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006244 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006245 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006246 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006247 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006248 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006249 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006250 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006251 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006252 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006253 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006254 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006255 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006256 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006257 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006258 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006259 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006260 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006261 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006262 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006263 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006264 case OMPC_unknown:
6265 llvm_unreachable("Clause is not allowed.");
6266 }
6267 return Res;
6268}
6269
Alexey Bataev6402bca2015-12-28 07:25:51 +00006270static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6271 OpenMPScheduleClauseModifier M2,
6272 SourceLocation M1Loc, SourceLocation M2Loc) {
6273 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6274 SmallVector<unsigned, 2> Excluded;
6275 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6276 Excluded.push_back(M2);
6277 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6278 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6279 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6280 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6281 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6282 << getListOfPossibleValues(OMPC_schedule,
6283 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6284 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6285 Excluded)
6286 << getOpenMPClauseName(OMPC_schedule);
6287 return true;
6288 }
6289 return false;
6290}
6291
Alexey Bataev56dafe82014-06-20 07:16:17 +00006292OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006293 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006294 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006295 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6296 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6297 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6298 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6299 return nullptr;
6300 // OpenMP, 2.7.1, Loop Construct, Restrictions
6301 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6302 // but not both.
6303 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6304 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6305 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6306 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6307 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6308 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6309 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6310 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6311 return nullptr;
6312 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006313 if (Kind == OMPC_SCHEDULE_unknown) {
6314 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006315 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6316 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6317 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6318 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6319 Exclude);
6320 } else {
6321 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6322 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006323 }
6324 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6325 << Values << getOpenMPClauseName(OMPC_schedule);
6326 return nullptr;
6327 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006328 // OpenMP, 2.7.1, Loop Construct, Restrictions
6329 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6330 // schedule(guided).
6331 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6332 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6333 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6334 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6335 diag::err_omp_schedule_nonmonotonic_static);
6336 return nullptr;
6337 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006338 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006339 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006340 if (ChunkSize) {
6341 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6342 !ChunkSize->isInstantiationDependent() &&
6343 !ChunkSize->containsUnexpandedParameterPack()) {
6344 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6345 ExprResult Val =
6346 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6347 if (Val.isInvalid())
6348 return nullptr;
6349
6350 ValExpr = Val.get();
6351
6352 // OpenMP [2.7.1, Restrictions]
6353 // chunk_size must be a loop invariant integer expression with a positive
6354 // value.
6355 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006356 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6357 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6358 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006359 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006360 return nullptr;
6361 }
6362 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6363 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6364 ChunkSize->getType(), ".chunk.");
6365 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6366 ChunkSize->getExprLoc(),
6367 /*RefersToCapture=*/true);
6368 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006369 }
6370 }
6371 }
6372
Alexey Bataev6402bca2015-12-28 07:25:51 +00006373 return new (Context)
6374 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6375 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006376}
6377
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006378OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6379 SourceLocation StartLoc,
6380 SourceLocation EndLoc) {
6381 OMPClause *Res = nullptr;
6382 switch (Kind) {
6383 case OMPC_ordered:
6384 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6385 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006386 case OMPC_nowait:
6387 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6388 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006389 case OMPC_untied:
6390 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6391 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006392 case OMPC_mergeable:
6393 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6394 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006395 case OMPC_read:
6396 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6397 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006398 case OMPC_write:
6399 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6400 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006401 case OMPC_update:
6402 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6403 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006404 case OMPC_capture:
6405 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6406 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006407 case OMPC_seq_cst:
6408 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6409 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006410 case OMPC_threads:
6411 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6412 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006413 case OMPC_simd:
6414 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6415 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006416 case OMPC_nogroup:
6417 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6418 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006419 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006420 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006421 case OMPC_num_threads:
6422 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006423 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006424 case OMPC_collapse:
6425 case OMPC_schedule:
6426 case OMPC_private:
6427 case OMPC_firstprivate:
6428 case OMPC_lastprivate:
6429 case OMPC_shared:
6430 case OMPC_reduction:
6431 case OMPC_linear:
6432 case OMPC_aligned:
6433 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006434 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006435 case OMPC_default:
6436 case OMPC_proc_bind:
6437 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006438 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006439 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006440 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006441 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006442 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006443 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006444 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006445 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006446 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006447 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006448 case OMPC_dist_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006449 case OMPC_unknown:
6450 llvm_unreachable("Clause is not allowed.");
6451 }
6452 return Res;
6453}
6454
Alexey Bataev236070f2014-06-20 11:19:47 +00006455OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6456 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006457 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006458 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6459}
6460
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006461OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6462 SourceLocation EndLoc) {
6463 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6464}
6465
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006466OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6467 SourceLocation EndLoc) {
6468 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6469}
6470
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006471OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6472 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006473 return new (Context) OMPReadClause(StartLoc, EndLoc);
6474}
6475
Alexey Bataevdea47612014-07-23 07:46:59 +00006476OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6477 SourceLocation EndLoc) {
6478 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6479}
6480
Alexey Bataev67a4f222014-07-23 10:25:33 +00006481OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6482 SourceLocation EndLoc) {
6483 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6484}
6485
Alexey Bataev459dec02014-07-24 06:46:57 +00006486OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6487 SourceLocation EndLoc) {
6488 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6489}
6490
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006491OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6492 SourceLocation EndLoc) {
6493 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6494}
6495
Alexey Bataev346265e2015-09-25 10:37:12 +00006496OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6497 SourceLocation EndLoc) {
6498 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6499}
6500
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006501OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6502 SourceLocation EndLoc) {
6503 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6504}
6505
Alexey Bataevb825de12015-12-07 10:51:44 +00006506OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6507 SourceLocation EndLoc) {
6508 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6509}
6510
Alexey Bataevc5e02582014-06-16 07:08:35 +00006511OMPClause *Sema::ActOnOpenMPVarListClause(
6512 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6513 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6514 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006515 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006516 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6517 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6518 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006519 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006520 switch (Kind) {
6521 case OMPC_private:
6522 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6523 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006524 case OMPC_firstprivate:
6525 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6526 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006527 case OMPC_lastprivate:
6528 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6529 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006530 case OMPC_shared:
6531 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6532 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006533 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006534 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6535 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006536 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006537 case OMPC_linear:
6538 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006539 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006540 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006541 case OMPC_aligned:
6542 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6543 ColonLoc, EndLoc);
6544 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006545 case OMPC_copyin:
6546 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6547 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006548 case OMPC_copyprivate:
6549 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6550 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006551 case OMPC_flush:
6552 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6553 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006554 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006555 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6556 StartLoc, LParenLoc, EndLoc);
6557 break;
6558 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006559 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6560 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6561 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006562 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006563 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006564 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006565 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006566 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006567 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006568 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006569 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006570 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006571 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006572 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006573 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006574 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006575 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006576 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006577 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006578 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006579 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006580 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006581 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006582 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006583 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006584 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006585 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006586 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006587 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006588 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006589 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006590 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006591 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006592 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006593 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006594 llvm_unreachable("Clause is not allowed.");
6595 }
6596 return Res;
6597}
6598
6599OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6600 SourceLocation StartLoc,
6601 SourceLocation LParenLoc,
6602 SourceLocation EndLoc) {
6603 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006604 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006605 for (auto &RefExpr : VarList) {
6606 assert(RefExpr && "NULL expr in OpenMP private clause.");
6607 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006608 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006609 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006610 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006611 continue;
6612 }
6613
Alexey Bataeved09d242014-05-28 05:53:51 +00006614 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006615 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006616 // A list item is a variable name.
6617 // OpenMP [2.9.3.3, Restrictions, p.1]
6618 // A variable that is part of another variable (as an array or
6619 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006620 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006621 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006622 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006623 continue;
6624 }
6625 Decl *D = DE->getDecl();
6626 VarDecl *VD = cast<VarDecl>(D);
6627
6628 QualType Type = VD->getType();
6629 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6630 // It will be analyzed later.
6631 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006632 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006633 continue;
6634 }
6635
6636 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6637 // A variable that appears in a private clause must not have an incomplete
6638 // type or a reference type.
6639 if (RequireCompleteType(ELoc, Type,
6640 diag::err_omp_private_incomplete_type)) {
6641 continue;
6642 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006643 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006644
Alexey Bataev758e55e2013-09-06 18:03:48 +00006645 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6646 // in a Construct]
6647 // Variables with the predetermined data-sharing attributes may not be
6648 // listed in data-sharing attributes clauses, except for the cases
6649 // listed below. For these exceptions only, listing a predetermined
6650 // variable in a data-sharing attribute clause is allowed and overrides
6651 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006652 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006653 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006654 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6655 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006656 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006657 continue;
6658 }
6659
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006660 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006661 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006662 DSAStack->getCurrentDirective() == OMPD_task) {
6663 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6664 << getOpenMPClauseName(OMPC_private) << Type
6665 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6666 bool IsDecl =
6667 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6668 Diag(VD->getLocation(),
6669 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6670 << VD;
6671 continue;
6672 }
6673
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006674 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6675 // A variable of class type (or array thereof) that appears in a private
6676 // clause requires an accessible, unambiguous default constructor for the
6677 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006678 // Generate helper private variable and initialize it with the default
6679 // value. The address of the original variable is replaced by the address of
6680 // the new private variable in CodeGen. This new variable is not added to
6681 // IdResolver, so the code in the OpenMP region uses original variable for
6682 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006683 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006684 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6685 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006686 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006687 if (VDPrivate->isInvalidDecl())
6688 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006689 auto VDPrivateRefExpr = buildDeclRefExpr(
6690 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006691
Alexey Bataev758e55e2013-09-06 18:03:48 +00006692 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006693 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006694 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006695 }
6696
Alexey Bataeved09d242014-05-28 05:53:51 +00006697 if (Vars.empty())
6698 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006699
Alexey Bataev03b340a2014-10-21 03:16:40 +00006700 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6701 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006702}
6703
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006704namespace {
6705class DiagsUninitializedSeveretyRAII {
6706private:
6707 DiagnosticsEngine &Diags;
6708 SourceLocation SavedLoc;
6709 bool IsIgnored;
6710
6711public:
6712 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6713 bool IsIgnored)
6714 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6715 if (!IsIgnored) {
6716 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6717 /*Map*/ diag::Severity::Ignored, Loc);
6718 }
6719 }
6720 ~DiagsUninitializedSeveretyRAII() {
6721 if (!IsIgnored)
6722 Diags.popMappings(SavedLoc);
6723 }
6724};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006725}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006726
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006727OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6728 SourceLocation StartLoc,
6729 SourceLocation LParenLoc,
6730 SourceLocation EndLoc) {
6731 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006732 SmallVector<Expr *, 8> PrivateCopies;
6733 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006734 bool IsImplicitClause =
6735 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6736 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6737
Alexey Bataeved09d242014-05-28 05:53:51 +00006738 for (auto &RefExpr : VarList) {
6739 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6740 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006741 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006742 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006743 PrivateCopies.push_back(nullptr);
6744 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006745 continue;
6746 }
6747
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006748 SourceLocation ELoc =
6749 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006750 // OpenMP [2.1, C/C++]
6751 // A list item is a variable name.
6752 // OpenMP [2.9.3.3, Restrictions, p.1]
6753 // A variable that is part of another variable (as an array or
6754 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006755 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006756 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006757 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006758 continue;
6759 }
6760 Decl *D = DE->getDecl();
6761 VarDecl *VD = cast<VarDecl>(D);
6762
6763 QualType Type = VD->getType();
6764 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6765 // It will be analyzed later.
6766 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006767 PrivateCopies.push_back(nullptr);
6768 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006769 continue;
6770 }
6771
6772 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6773 // A variable that appears in a private clause must not have an incomplete
6774 // type or a reference type.
6775 if (RequireCompleteType(ELoc, Type,
6776 diag::err_omp_firstprivate_incomplete_type)) {
6777 continue;
6778 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006779 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006780
6781 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6782 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006783 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006784 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006785 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006786
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006787 // If an implicit firstprivate variable found it was checked already.
6788 if (!IsImplicitClause) {
6789 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006790 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006791 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6792 // A list item that specifies a given variable may not appear in more
6793 // than one clause on the same directive, except that a variable may be
6794 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006795 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006796 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006797 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006798 << getOpenMPClauseName(DVar.CKind)
6799 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006800 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006801 continue;
6802 }
6803
6804 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6805 // in a Construct]
6806 // Variables with the predetermined data-sharing attributes may not be
6807 // listed in data-sharing attributes clauses, except for the cases
6808 // listed below. For these exceptions only, listing a predetermined
6809 // variable in a data-sharing attribute clause is allowed and overrides
6810 // the variable's predetermined data-sharing attributes.
6811 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6812 // in a Construct, C/C++, p.2]
6813 // Variables with const-qualified type having no mutable member may be
6814 // listed in a firstprivate clause, even if they are static data members.
6815 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6816 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6817 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006818 << getOpenMPClauseName(DVar.CKind)
6819 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006820 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006821 continue;
6822 }
6823
Alexey Bataevf29276e2014-06-18 04:14:57 +00006824 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006825 // OpenMP [2.9.3.4, Restrictions, p.2]
6826 // A list item that is private within a parallel region must not appear
6827 // in a firstprivate clause on a worksharing construct if any of the
6828 // worksharing regions arising from the worksharing construct ever bind
6829 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006830 if (isOpenMPWorksharingDirective(CurrDir) &&
6831 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006832 DVar = DSAStack->getImplicitDSA(VD, true);
6833 if (DVar.CKind != OMPC_shared &&
6834 (isOpenMPParallelDirective(DVar.DKind) ||
6835 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006836 Diag(ELoc, diag::err_omp_required_access)
6837 << getOpenMPClauseName(OMPC_firstprivate)
6838 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006839 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006840 continue;
6841 }
6842 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006843 // OpenMP [2.9.3.4, Restrictions, p.3]
6844 // A list item that appears in a reduction clause of a parallel construct
6845 // must not appear in a firstprivate clause on a worksharing or task
6846 // construct if any of the worksharing or task regions arising from the
6847 // worksharing or task construct ever bind to any of the parallel regions
6848 // arising from the parallel construct.
6849 // OpenMP [2.9.3.4, Restrictions, p.4]
6850 // A list item that appears in a reduction clause in worksharing
6851 // construct must not appear in a firstprivate clause in a task construct
6852 // encountered during execution of any of the worksharing regions arising
6853 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006854 if (CurrDir == OMPD_task) {
6855 DVar =
6856 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6857 [](OpenMPDirectiveKind K) -> bool {
6858 return isOpenMPParallelDirective(K) ||
6859 isOpenMPWorksharingDirective(K);
6860 },
6861 false);
6862 if (DVar.CKind == OMPC_reduction &&
6863 (isOpenMPParallelDirective(DVar.DKind) ||
6864 isOpenMPWorksharingDirective(DVar.DKind))) {
6865 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6866 << getOpenMPDirectiveName(DVar.DKind);
6867 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6868 continue;
6869 }
6870 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006871
6872 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6873 // A list item that is private within a teams region must not appear in a
6874 // firstprivate clause on a distribute construct if any of the distribute
6875 // regions arising from the distribute construct ever bind to any of the
6876 // teams regions arising from the teams construct.
6877 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6878 // A list item that appears in a reduction clause of a teams construct
6879 // must not appear in a firstprivate clause on a distribute construct if
6880 // any of the distribute regions arising from the distribute construct
6881 // ever bind to any of the teams regions arising from the teams construct.
6882 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6883 // A list item may appear in a firstprivate or lastprivate clause but not
6884 // both.
6885 if (CurrDir == OMPD_distribute) {
6886 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6887 [](OpenMPDirectiveKind K) -> bool {
6888 return isOpenMPTeamsDirective(K);
6889 },
6890 false);
6891 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6892 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6893 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6894 continue;
6895 }
6896 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6897 [](OpenMPDirectiveKind K) -> bool {
6898 return isOpenMPTeamsDirective(K);
6899 },
6900 false);
6901 if (DVar.CKind == OMPC_reduction &&
6902 isOpenMPTeamsDirective(DVar.DKind)) {
6903 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6904 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6905 continue;
6906 }
6907 DVar = DSAStack->getTopDSA(VD, false);
6908 if (DVar.CKind == OMPC_lastprivate) {
6909 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6910 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6911 continue;
6912 }
6913 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006914 }
6915
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006916 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006917 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006918 DSAStack->getCurrentDirective() == OMPD_task) {
6919 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6920 << getOpenMPClauseName(OMPC_firstprivate) << Type
6921 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6922 bool IsDecl =
6923 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6924 Diag(VD->getLocation(),
6925 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6926 << VD;
6927 continue;
6928 }
6929
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006930 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006931 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6932 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006933 // Generate helper private variable and initialize it with the value of the
6934 // original variable. The address of the original variable is replaced by
6935 // the address of the new private variable in the CodeGen. This new variable
6936 // is not added to IdResolver, so the code in the OpenMP region uses
6937 // original variable for proper diagnostics and variable capturing.
6938 Expr *VDInitRefExpr = nullptr;
6939 // For arrays generate initializer for single element and replace it by the
6940 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006941 if (Type->isArrayType()) {
6942 auto VDInit =
6943 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6944 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006945 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006946 ElemType = ElemType.getUnqualifiedType();
6947 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6948 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006949 InitializedEntity Entity =
6950 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006951 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6952
6953 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6954 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6955 if (Result.isInvalid())
6956 VDPrivate->setInvalidDecl();
6957 else
6958 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006959 // Remove temp variable declaration.
6960 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006961 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006962 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006963 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006964 VDInitRefExpr =
6965 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006966 AddInitializerToDecl(VDPrivate,
6967 DefaultLvalueConversion(VDInitRefExpr).get(),
6968 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006969 }
6970 if (VDPrivate->isInvalidDecl()) {
6971 if (IsImplicitClause) {
6972 Diag(DE->getExprLoc(),
6973 diag::note_omp_task_predetermined_firstprivate_here);
6974 }
6975 continue;
6976 }
6977 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006978 auto VDPrivateRefExpr = buildDeclRefExpr(
6979 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006980 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6981 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006982 PrivateCopies.push_back(VDPrivateRefExpr);
6983 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006984 }
6985
Alexey Bataeved09d242014-05-28 05:53:51 +00006986 if (Vars.empty())
6987 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006988
6989 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006990 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006991}
6992
Alexander Musman1bb328c2014-06-04 13:06:39 +00006993OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6994 SourceLocation StartLoc,
6995 SourceLocation LParenLoc,
6996 SourceLocation EndLoc) {
6997 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006998 SmallVector<Expr *, 8> SrcExprs;
6999 SmallVector<Expr *, 8> DstExprs;
7000 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007001 for (auto &RefExpr : VarList) {
7002 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7003 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7004 // It will be analyzed later.
7005 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007006 SrcExprs.push_back(nullptr);
7007 DstExprs.push_back(nullptr);
7008 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007009 continue;
7010 }
7011
7012 SourceLocation ELoc = RefExpr->getExprLoc();
7013 // OpenMP [2.1, C/C++]
7014 // A list item is a variable name.
7015 // OpenMP [2.14.3.5, Restrictions, p.1]
7016 // A variable that is part of another variable (as an array or structure
7017 // element) cannot appear in a lastprivate clause.
7018 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7019 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7020 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7021 continue;
7022 }
7023 Decl *D = DE->getDecl();
7024 VarDecl *VD = cast<VarDecl>(D);
7025
7026 QualType Type = VD->getType();
7027 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7028 // It will be analyzed later.
7029 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007030 SrcExprs.push_back(nullptr);
7031 DstExprs.push_back(nullptr);
7032 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007033 continue;
7034 }
7035
7036 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7037 // A variable that appears in a lastprivate clause must not have an
7038 // incomplete type or a reference type.
7039 if (RequireCompleteType(ELoc, Type,
7040 diag::err_omp_lastprivate_incomplete_type)) {
7041 continue;
7042 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007043 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007044
7045 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7046 // in a Construct]
7047 // Variables with the predetermined data-sharing attributes may not be
7048 // listed in data-sharing attributes clauses, except for the cases
7049 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007050 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007051 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7052 DVar.CKind != OMPC_firstprivate &&
7053 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7054 Diag(ELoc, diag::err_omp_wrong_dsa)
7055 << getOpenMPClauseName(DVar.CKind)
7056 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007057 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007058 continue;
7059 }
7060
Alexey Bataevf29276e2014-06-18 04:14:57 +00007061 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7062 // OpenMP [2.14.3.5, Restrictions, p.2]
7063 // A list item that is private within a parallel region, or that appears in
7064 // the reduction clause of a parallel construct, must not appear in a
7065 // lastprivate clause on a worksharing construct if any of the corresponding
7066 // worksharing regions ever binds to any of the corresponding parallel
7067 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007068 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007069 if (isOpenMPWorksharingDirective(CurrDir) &&
7070 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007071 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007072 if (DVar.CKind != OMPC_shared) {
7073 Diag(ELoc, diag::err_omp_required_access)
7074 << getOpenMPClauseName(OMPC_lastprivate)
7075 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007076 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007077 continue;
7078 }
7079 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007080 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007081 // A variable of class type (or array thereof) that appears in a
7082 // lastprivate clause requires an accessible, unambiguous default
7083 // constructor for the class type, unless the list item is also specified
7084 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007085 // A variable of class type (or array thereof) that appears in a
7086 // lastprivate clause requires an accessible, unambiguous copy assignment
7087 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007088 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007089 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007090 Type.getUnqualifiedType(), ".lastprivate.src",
7091 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007092 auto *PseudoSrcExpr = buildDeclRefExpr(
7093 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007094 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007095 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7096 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007097 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007098 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007099 // For arrays generate assignment operation for single element and replace
7100 // it by the original array element in CodeGen.
7101 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7102 PseudoDstExpr, PseudoSrcExpr);
7103 if (AssignmentOp.isInvalid())
7104 continue;
7105 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7106 /*DiscardedValue=*/true);
7107 if (AssignmentOp.isInvalid())
7108 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007109
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007110 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7111 // A list item may appear in a firstprivate or lastprivate clause but not
7112 // both.
7113 if (CurrDir == OMPD_distribute) {
7114 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7115 if (DVar.CKind == OMPC_firstprivate) {
7116 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7117 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7118 continue;
7119 }
7120 }
7121
Alexey Bataev39f915b82015-05-08 10:41:21 +00007122 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007123 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007124 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007125 SrcExprs.push_back(PseudoSrcExpr);
7126 DstExprs.push_back(PseudoDstExpr);
7127 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007128 }
7129
7130 if (Vars.empty())
7131 return nullptr;
7132
7133 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007134 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007135}
7136
Alexey Bataev758e55e2013-09-06 18:03:48 +00007137OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7138 SourceLocation StartLoc,
7139 SourceLocation LParenLoc,
7140 SourceLocation EndLoc) {
7141 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007142 for (auto &RefExpr : VarList) {
7143 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7144 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007145 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007146 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007147 continue;
7148 }
7149
Alexey Bataeved09d242014-05-28 05:53:51 +00007150 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007151 // OpenMP [2.1, C/C++]
7152 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007153 // OpenMP [2.14.3.2, Restrictions, p.1]
7154 // A variable that is part of another variable (as an array or structure
7155 // element) cannot appear in a shared unless it is a static data member
7156 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007157 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007158 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007159 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007160 continue;
7161 }
7162 Decl *D = DE->getDecl();
7163 VarDecl *VD = cast<VarDecl>(D);
7164
7165 QualType Type = VD->getType();
7166 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7167 // It will be analyzed later.
7168 Vars.push_back(DE);
7169 continue;
7170 }
7171
7172 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7173 // in a Construct]
7174 // Variables with the predetermined data-sharing attributes may not be
7175 // listed in data-sharing attributes clauses, except for the cases
7176 // listed below. For these exceptions only, listing a predetermined
7177 // variable in a data-sharing attribute clause is allowed and overrides
7178 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007179 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007180 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7181 DVar.RefExpr) {
7182 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7183 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007184 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007185 continue;
7186 }
7187
7188 DSAStack->addDSA(VD, DE, OMPC_shared);
7189 Vars.push_back(DE);
7190 }
7191
Alexey Bataeved09d242014-05-28 05:53:51 +00007192 if (Vars.empty())
7193 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007194
7195 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7196}
7197
Alexey Bataevc5e02582014-06-16 07:08:35 +00007198namespace {
7199class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7200 DSAStackTy *Stack;
7201
7202public:
7203 bool VisitDeclRefExpr(DeclRefExpr *E) {
7204 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007205 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007206 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7207 return false;
7208 if (DVar.CKind != OMPC_unknown)
7209 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007210 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007211 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007212 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007213 return true;
7214 return false;
7215 }
7216 return false;
7217 }
7218 bool VisitStmt(Stmt *S) {
7219 for (auto Child : S->children()) {
7220 if (Child && Visit(Child))
7221 return true;
7222 }
7223 return false;
7224 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007225 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007226};
Alexey Bataev23b69422014-06-18 07:08:49 +00007227} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007228
7229OMPClause *Sema::ActOnOpenMPReductionClause(
7230 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7231 SourceLocation ColonLoc, SourceLocation EndLoc,
7232 CXXScopeSpec &ReductionIdScopeSpec,
7233 const DeclarationNameInfo &ReductionId) {
7234 // TODO: Allow scope specification search when 'declare reduction' is
7235 // supported.
7236 assert(ReductionIdScopeSpec.isEmpty() &&
7237 "No support for scoped reduction identifiers yet.");
7238
7239 auto DN = ReductionId.getName();
7240 auto OOK = DN.getCXXOverloadedOperator();
7241 BinaryOperatorKind BOK = BO_Comma;
7242
7243 // OpenMP [2.14.3.6, reduction clause]
7244 // C
7245 // reduction-identifier is either an identifier or one of the following
7246 // operators: +, -, *, &, |, ^, && and ||
7247 // C++
7248 // reduction-identifier is either an id-expression or one of the following
7249 // operators: +, -, *, &, |, ^, && and ||
7250 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7251 switch (OOK) {
7252 case OO_Plus:
7253 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007254 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007255 break;
7256 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007257 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007258 break;
7259 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007260 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007261 break;
7262 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007263 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007264 break;
7265 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007266 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007267 break;
7268 case OO_AmpAmp:
7269 BOK = BO_LAnd;
7270 break;
7271 case OO_PipePipe:
7272 BOK = BO_LOr;
7273 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007274 case OO_New:
7275 case OO_Delete:
7276 case OO_Array_New:
7277 case OO_Array_Delete:
7278 case OO_Slash:
7279 case OO_Percent:
7280 case OO_Tilde:
7281 case OO_Exclaim:
7282 case OO_Equal:
7283 case OO_Less:
7284 case OO_Greater:
7285 case OO_LessEqual:
7286 case OO_GreaterEqual:
7287 case OO_PlusEqual:
7288 case OO_MinusEqual:
7289 case OO_StarEqual:
7290 case OO_SlashEqual:
7291 case OO_PercentEqual:
7292 case OO_CaretEqual:
7293 case OO_AmpEqual:
7294 case OO_PipeEqual:
7295 case OO_LessLess:
7296 case OO_GreaterGreater:
7297 case OO_LessLessEqual:
7298 case OO_GreaterGreaterEqual:
7299 case OO_EqualEqual:
7300 case OO_ExclaimEqual:
7301 case OO_PlusPlus:
7302 case OO_MinusMinus:
7303 case OO_Comma:
7304 case OO_ArrowStar:
7305 case OO_Arrow:
7306 case OO_Call:
7307 case OO_Subscript:
7308 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007309 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007310 case NUM_OVERLOADED_OPERATORS:
7311 llvm_unreachable("Unexpected reduction identifier");
7312 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007313 if (auto II = DN.getAsIdentifierInfo()) {
7314 if (II->isStr("max"))
7315 BOK = BO_GT;
7316 else if (II->isStr("min"))
7317 BOK = BO_LT;
7318 }
7319 break;
7320 }
7321 SourceRange ReductionIdRange;
7322 if (ReductionIdScopeSpec.isValid()) {
7323 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7324 }
7325 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7326 if (BOK == BO_Comma) {
7327 // Not allowed reduction identifier is found.
7328 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7329 << ReductionIdRange;
7330 return nullptr;
7331 }
7332
7333 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007334 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007335 SmallVector<Expr *, 8> LHSs;
7336 SmallVector<Expr *, 8> RHSs;
7337 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007338 for (auto RefExpr : VarList) {
7339 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7340 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7341 // It will be analyzed later.
7342 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007343 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007344 LHSs.push_back(nullptr);
7345 RHSs.push_back(nullptr);
7346 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007347 continue;
7348 }
7349
7350 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7351 RefExpr->isInstantiationDependent() ||
7352 RefExpr->containsUnexpandedParameterPack()) {
7353 // It will be analyzed later.
7354 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007355 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007356 LHSs.push_back(nullptr);
7357 RHSs.push_back(nullptr);
7358 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007359 continue;
7360 }
7361
7362 auto ELoc = RefExpr->getExprLoc();
7363 auto ERange = RefExpr->getSourceRange();
7364 // OpenMP [2.1, C/C++]
7365 // A list item is a variable or array section, subject to the restrictions
7366 // specified in Section 2.4 on page 42 and in each of the sections
7367 // describing clauses and directives for which a list appears.
7368 // OpenMP [2.14.3.3, Restrictions, p.1]
7369 // A variable that is part of another variable (as an array or
7370 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007371 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7372 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7373 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7374 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7375 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007376 continue;
7377 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007378 QualType Type;
7379 VarDecl *VD = nullptr;
7380 if (DE) {
7381 auto D = DE->getDecl();
7382 VD = cast<VarDecl>(D);
7383 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007384 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007385 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007386 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7387 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7388 Base = TempASE->getBase()->IgnoreParenImpCasts();
7389 DE = dyn_cast<DeclRefExpr>(Base);
7390 if (DE)
7391 VD = dyn_cast<VarDecl>(DE->getDecl());
7392 if (!VD) {
7393 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7394 << 0 << Base->getSourceRange();
7395 continue;
7396 }
7397 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007398 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7399 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7400 Type = ATy->getElementType();
7401 else
7402 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007403 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7404 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7405 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7406 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7407 Base = TempASE->getBase()->IgnoreParenImpCasts();
7408 DE = dyn_cast<DeclRefExpr>(Base);
7409 if (DE)
7410 VD = dyn_cast<VarDecl>(DE->getDecl());
7411 if (!VD) {
7412 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7413 << 1 << Base->getSourceRange();
7414 continue;
7415 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007416 }
7417
Alexey Bataevc5e02582014-06-16 07:08:35 +00007418 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7419 // A variable that appears in a private clause must not have an incomplete
7420 // type or a reference type.
7421 if (RequireCompleteType(ELoc, Type,
7422 diag::err_omp_reduction_incomplete_type))
7423 continue;
7424 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7425 // Arrays may not appear in a reduction clause.
7426 if (Type.getNonReferenceType()->isArrayType()) {
7427 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007428 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007429 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7430 VarDecl::DeclarationOnly;
7431 Diag(VD->getLocation(),
7432 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7433 << VD;
7434 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007435 continue;
7436 }
7437 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7438 // A list item that appears in a reduction clause must not be
7439 // const-qualified.
7440 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007441 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007442 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007443 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007444 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7445 VarDecl::DeclarationOnly;
7446 Diag(VD->getLocation(),
7447 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7448 << VD;
7449 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007450 continue;
7451 }
7452 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7453 // If a list-item is a reference type then it must bind to the same object
7454 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007455 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007456 VarDecl *VDDef = VD->getDefinition();
7457 if (Type->isReferenceType() && VDDef) {
7458 DSARefChecker Check(DSAStack);
7459 if (Check.Visit(VDDef->getInit())) {
7460 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7461 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7462 continue;
7463 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007464 }
7465 }
7466 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7467 // The type of a list item that appears in a reduction clause must be valid
7468 // for the reduction-identifier. For a max or min reduction in C, the type
7469 // of the list item must be an allowed arithmetic data type: char, int,
7470 // float, double, or _Bool, possibly modified with long, short, signed, or
7471 // unsigned. For a max or min reduction in C++, the type of the list item
7472 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7473 // double, or bool, possibly modified with long, short, signed, or unsigned.
7474 if ((BOK == BO_GT || BOK == BO_LT) &&
7475 !(Type->isScalarType() ||
7476 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7477 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7478 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007479 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007480 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7481 VarDecl::DeclarationOnly;
7482 Diag(VD->getLocation(),
7483 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7484 << VD;
7485 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007486 continue;
7487 }
7488 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7489 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7490 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007491 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007492 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7493 VarDecl::DeclarationOnly;
7494 Diag(VD->getLocation(),
7495 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7496 << VD;
7497 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007498 continue;
7499 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007500 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7501 // in a Construct]
7502 // Variables with the predetermined data-sharing attributes may not be
7503 // listed in data-sharing attributes clauses, except for the cases
7504 // listed below. For these exceptions only, listing a predetermined
7505 // variable in a data-sharing attribute clause is allowed and overrides
7506 // the variable's predetermined data-sharing attributes.
7507 // OpenMP [2.14.3.6, Restrictions, p.3]
7508 // Any number of reduction clauses can be specified on the directive,
7509 // but a list item can appear only once in the reduction clauses for that
7510 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007511 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007512 DVar = DSAStack->getTopDSA(VD, false);
7513 if (DVar.CKind == OMPC_reduction) {
7514 Diag(ELoc, diag::err_omp_once_referenced)
7515 << getOpenMPClauseName(OMPC_reduction);
7516 if (DVar.RefExpr) {
7517 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007518 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007519 } else if (DVar.CKind != OMPC_unknown) {
7520 Diag(ELoc, diag::err_omp_wrong_dsa)
7521 << getOpenMPClauseName(DVar.CKind)
7522 << getOpenMPClauseName(OMPC_reduction);
7523 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7524 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007525 }
7526
7527 // OpenMP [2.14.3.6, Restrictions, p.1]
7528 // A list item that appears in a reduction clause of a worksharing
7529 // construct must be shared in the parallel regions to which any of the
7530 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007531 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7532 if (isOpenMPWorksharingDirective(CurrDir) &&
7533 !isOpenMPParallelDirective(CurrDir)) {
7534 DVar = DSAStack->getImplicitDSA(VD, true);
7535 if (DVar.CKind != OMPC_shared) {
7536 Diag(ELoc, diag::err_omp_required_access)
7537 << getOpenMPClauseName(OMPC_reduction)
7538 << getOpenMPClauseName(OMPC_shared);
7539 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7540 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007541 }
7542 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007543
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007544 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007545 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7546 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7547 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7548 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7549 auto PrivateTy = Type;
7550 if (OASE) {
7551 // For array sections only:
7552 // Create pseudo array type for private copy. The size for this array will
7553 // be generated during codegen.
7554 // For array subscripts or single variables Private Ty is the same as Type
7555 // (type of the variable or single array element).
7556 PrivateTy = Context.getVariableArrayType(
7557 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7558 Context.getSizeType(), VK_RValue),
7559 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7560 }
7561 // Private copy.
7562 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7563 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007564 // Add initializer for private variable.
7565 Expr *Init = nullptr;
7566 switch (BOK) {
7567 case BO_Add:
7568 case BO_Xor:
7569 case BO_Or:
7570 case BO_LOr:
7571 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7572 if (Type->isScalarType() || Type->isAnyComplexType()) {
7573 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007574 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007575 break;
7576 case BO_Mul:
7577 case BO_LAnd:
7578 if (Type->isScalarType() || Type->isAnyComplexType()) {
7579 // '*' and '&&' reduction ops - initializer is '1'.
7580 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7581 }
7582 break;
7583 case BO_And: {
7584 // '&' reduction op - initializer is '~0'.
7585 QualType OrigType = Type;
7586 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7587 Type = ComplexTy->getElementType();
7588 }
7589 if (Type->isRealFloatingType()) {
7590 llvm::APFloat InitValue =
7591 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7592 /*isIEEE=*/true);
7593 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7594 Type, ELoc);
7595 } else if (Type->isScalarType()) {
7596 auto Size = Context.getTypeSize(Type);
7597 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7598 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7599 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7600 }
7601 if (Init && OrigType->isAnyComplexType()) {
7602 // Init = 0xFFFF + 0xFFFFi;
7603 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7604 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7605 }
7606 Type = OrigType;
7607 break;
7608 }
7609 case BO_LT:
7610 case BO_GT: {
7611 // 'min' reduction op - initializer is 'Largest representable number in
7612 // the reduction list item type'.
7613 // 'max' reduction op - initializer is 'Least representable number in
7614 // the reduction list item type'.
7615 if (Type->isIntegerType() || Type->isPointerType()) {
7616 bool IsSigned = Type->hasSignedIntegerRepresentation();
7617 auto Size = Context.getTypeSize(Type);
7618 QualType IntTy =
7619 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7620 llvm::APInt InitValue =
7621 (BOK != BO_LT)
7622 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7623 : llvm::APInt::getMinValue(Size)
7624 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7625 : llvm::APInt::getMaxValue(Size);
7626 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7627 if (Type->isPointerType()) {
7628 // Cast to pointer type.
7629 auto CastExpr = BuildCStyleCastExpr(
7630 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7631 SourceLocation(), Init);
7632 if (CastExpr.isInvalid())
7633 continue;
7634 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007635 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007636 } else if (Type->isRealFloatingType()) {
7637 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7638 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7639 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7640 Type, ELoc);
7641 }
7642 break;
7643 }
7644 case BO_PtrMemD:
7645 case BO_PtrMemI:
7646 case BO_MulAssign:
7647 case BO_Div:
7648 case BO_Rem:
7649 case BO_Sub:
7650 case BO_Shl:
7651 case BO_Shr:
7652 case BO_LE:
7653 case BO_GE:
7654 case BO_EQ:
7655 case BO_NE:
7656 case BO_AndAssign:
7657 case BO_XorAssign:
7658 case BO_OrAssign:
7659 case BO_Assign:
7660 case BO_AddAssign:
7661 case BO_SubAssign:
7662 case BO_DivAssign:
7663 case BO_RemAssign:
7664 case BO_ShlAssign:
7665 case BO_ShrAssign:
7666 case BO_Comma:
7667 llvm_unreachable("Unexpected reduction operation");
7668 }
7669 if (Init) {
7670 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7671 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007672 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007673 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007674 if (!RHSVD->hasInit()) {
7675 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7676 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007677 if (VD) {
7678 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7679 VarDecl::DeclarationOnly;
7680 Diag(VD->getLocation(),
7681 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7682 << VD;
7683 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007684 continue;
7685 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007686 // Store initializer for single element in private copy. Will be used during
7687 // codegen.
7688 PrivateVD->setInit(RHSVD->getInit());
7689 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007690 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7691 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007692 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007693 ExprResult ReductionOp =
7694 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7695 LHSDRE, RHSDRE);
7696 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007697 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007698 ReductionOp =
7699 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7700 BO_Assign, LHSDRE, ReductionOp.get());
7701 } else {
7702 auto *ConditionalOp = new (Context) ConditionalOperator(
7703 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7704 RHSDRE, Type, VK_LValue, OK_Ordinary);
7705 ReductionOp =
7706 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7707 BO_Assign, LHSDRE, ConditionalOp);
7708 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007709 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007710 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007711 if (ReductionOp.isInvalid())
7712 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007713
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007714 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007715 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007716 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007717 LHSs.push_back(LHSDRE);
7718 RHSs.push_back(RHSDRE);
7719 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007720 }
7721
7722 if (Vars.empty())
7723 return nullptr;
7724
7725 return OMPReductionClause::Create(
7726 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007727 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7728 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007729}
7730
Alexey Bataev182227b2015-08-20 10:54:39 +00007731OMPClause *Sema::ActOnOpenMPLinearClause(
7732 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7733 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7734 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007735 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007736 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007737 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007738 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7739 LinKind == OMPC_LINEAR_unknown) {
7740 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7741 LinKind = OMPC_LINEAR_val;
7742 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007743 for (auto &RefExpr : VarList) {
7744 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7745 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007746 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007747 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007748 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007749 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007750 continue;
7751 }
7752
7753 // OpenMP [2.14.3.7, linear clause]
7754 // A list item that appears in a linear clause is subject to the private
7755 // clause semantics described in Section 2.14.3.3 on page 159 except as
7756 // noted. In addition, the value of the new list item on each iteration
7757 // of the associated loop(s) corresponds to the value of the original
7758 // list item before entering the construct plus the logical number of
7759 // the iteration times linear-step.
7760
Alexey Bataeved09d242014-05-28 05:53:51 +00007761 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007762 // OpenMP [2.1, C/C++]
7763 // A list item is a variable name.
7764 // OpenMP [2.14.3.3, Restrictions, p.1]
7765 // A variable that is part of another variable (as an array or
7766 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007767 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007768 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007769 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007770 continue;
7771 }
7772
7773 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7774
7775 // OpenMP [2.14.3.7, linear clause]
7776 // A list-item cannot appear in more than one linear clause.
7777 // A list-item that appears in a linear clause cannot appear in any
7778 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007779 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007780 if (DVar.RefExpr) {
7781 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7782 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007783 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007784 continue;
7785 }
7786
7787 QualType QType = VD->getType();
7788 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7789 // It will be analyzed later.
7790 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007791 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007792 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007793 continue;
7794 }
7795
7796 // A variable must not have an incomplete type or a reference type.
7797 if (RequireCompleteType(ELoc, QType,
7798 diag::err_omp_linear_incomplete_type)) {
7799 continue;
7800 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007801 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7802 !QType->isReferenceType()) {
7803 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7804 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7805 continue;
7806 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007807 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007808
7809 // A list item must not be const-qualified.
7810 if (QType.isConstant(Context)) {
7811 Diag(ELoc, diag::err_omp_const_variable)
7812 << getOpenMPClauseName(OMPC_linear);
7813 bool IsDecl =
7814 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7815 Diag(VD->getLocation(),
7816 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7817 << VD;
7818 continue;
7819 }
7820
7821 // A list item must be of integral or pointer type.
7822 QType = QType.getUnqualifiedType().getCanonicalType();
7823 const Type *Ty = QType.getTypePtrOrNull();
7824 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7825 !Ty->isPointerType())) {
7826 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7827 bool IsDecl =
7828 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7829 Diag(VD->getLocation(),
7830 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7831 << VD;
7832 continue;
7833 }
7834
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007835 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007836 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7837 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007838 auto *PrivateRef = buildDeclRefExpr(
7839 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007840 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007841 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007842 Expr *InitExpr;
7843 if (LinKind == OMPC_LINEAR_uval)
7844 InitExpr = VD->getInit();
7845 else
7846 InitExpr = DE;
7847 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007848 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007849 auto InitRef = buildDeclRefExpr(
7850 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007851 DSAStack->addDSA(VD, DE, OMPC_linear);
7852 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007853 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007854 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007855 }
7856
7857 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007858 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007859
7860 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007861 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007862 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7863 !Step->isInstantiationDependent() &&
7864 !Step->containsUnexpandedParameterPack()) {
7865 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007866 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007867 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007868 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007869 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007870
Alexander Musman3276a272015-03-21 10:12:56 +00007871 // Build var to save the step value.
7872 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007873 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007874 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007875 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007876 ExprResult CalcStep =
7877 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007878 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007879
Alexander Musman8dba6642014-04-22 13:09:42 +00007880 // Warn about zero linear step (it would be probably better specified as
7881 // making corresponding variables 'const').
7882 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007883 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7884 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007885 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7886 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007887 if (!IsConstant && CalcStep.isUsable()) {
7888 // Calculate the step beforehand instead of doing this on each iteration.
7889 // (This is not used if the number of iterations may be kfold-ed).
7890 CalcStepExpr = CalcStep.get();
7891 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007892 }
7893
Alexey Bataev182227b2015-08-20 10:54:39 +00007894 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7895 ColonLoc, EndLoc, Vars, Privates, Inits,
7896 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007897}
7898
7899static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7900 Expr *NumIterations, Sema &SemaRef,
7901 Scope *S) {
7902 // Walk the vars and build update/final expressions for the CodeGen.
7903 SmallVector<Expr *, 8> Updates;
7904 SmallVector<Expr *, 8> Finals;
7905 Expr *Step = Clause.getStep();
7906 Expr *CalcStep = Clause.getCalcStep();
7907 // OpenMP [2.14.3.7, linear clause]
7908 // If linear-step is not specified it is assumed to be 1.
7909 if (Step == nullptr)
7910 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7911 else if (CalcStep)
7912 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7913 bool HasErrors = false;
7914 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007915 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007916 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007917 for (auto &RefExpr : Clause.varlists()) {
7918 Expr *InitExpr = *CurInit;
7919
7920 // Build privatized reference to the current linear var.
7921 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007922 Expr *CapturedRef;
7923 if (LinKind == OMPC_LINEAR_uval)
7924 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7925 else
7926 CapturedRef =
7927 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7928 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7929 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007930
7931 // Build update: Var = InitExpr + IV * Step
7932 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007933 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007934 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007935 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7936 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007937
7938 // Build final: Var = InitExpr + NumIterations * Step
7939 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007940 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007941 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007942 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7943 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007944 if (!Update.isUsable() || !Final.isUsable()) {
7945 Updates.push_back(nullptr);
7946 Finals.push_back(nullptr);
7947 HasErrors = true;
7948 } else {
7949 Updates.push_back(Update.get());
7950 Finals.push_back(Final.get());
7951 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007952 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007953 }
7954 Clause.setUpdates(Updates);
7955 Clause.setFinals(Finals);
7956 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007957}
7958
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007959OMPClause *Sema::ActOnOpenMPAlignedClause(
7960 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7961 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7962
7963 SmallVector<Expr *, 8> Vars;
7964 for (auto &RefExpr : VarList) {
7965 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7966 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7967 // It will be analyzed later.
7968 Vars.push_back(RefExpr);
7969 continue;
7970 }
7971
7972 SourceLocation ELoc = RefExpr->getExprLoc();
7973 // OpenMP [2.1, C/C++]
7974 // A list item is a variable name.
7975 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7976 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7977 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7978 continue;
7979 }
7980
7981 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7982
7983 // OpenMP [2.8.1, simd construct, Restrictions]
7984 // The type of list items appearing in the aligned clause must be
7985 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007986 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007987 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007988 const Type *Ty = QType.getTypePtrOrNull();
7989 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7990 !Ty->isPointerType())) {
7991 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7992 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7993 bool IsDecl =
7994 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7995 Diag(VD->getLocation(),
7996 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7997 << VD;
7998 continue;
7999 }
8000
8001 // OpenMP [2.8.1, simd construct, Restrictions]
8002 // A list-item cannot appear in more than one aligned clause.
8003 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
8004 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8005 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8006 << getOpenMPClauseName(OMPC_aligned);
8007 continue;
8008 }
8009
8010 Vars.push_back(DE);
8011 }
8012
8013 // OpenMP [2.8.1, simd construct, Description]
8014 // The parameter of the aligned clause, alignment, must be a constant
8015 // positive integer expression.
8016 // If no optional parameter is specified, implementation-defined default
8017 // alignments for SIMD instructions on the target platforms are assumed.
8018 if (Alignment != nullptr) {
8019 ExprResult AlignResult =
8020 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8021 if (AlignResult.isInvalid())
8022 return nullptr;
8023 Alignment = AlignResult.get();
8024 }
8025 if (Vars.empty())
8026 return nullptr;
8027
8028 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8029 EndLoc, Vars, Alignment);
8030}
8031
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008032OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8033 SourceLocation StartLoc,
8034 SourceLocation LParenLoc,
8035 SourceLocation EndLoc) {
8036 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008037 SmallVector<Expr *, 8> SrcExprs;
8038 SmallVector<Expr *, 8> DstExprs;
8039 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008040 for (auto &RefExpr : VarList) {
8041 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8042 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008043 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008044 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008045 SrcExprs.push_back(nullptr);
8046 DstExprs.push_back(nullptr);
8047 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008048 continue;
8049 }
8050
Alexey Bataeved09d242014-05-28 05:53:51 +00008051 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008052 // OpenMP [2.1, C/C++]
8053 // A list item is a variable name.
8054 // OpenMP [2.14.4.1, Restrictions, p.1]
8055 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008056 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008057 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008058 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008059 continue;
8060 }
8061
8062 Decl *D = DE->getDecl();
8063 VarDecl *VD = cast<VarDecl>(D);
8064
8065 QualType Type = VD->getType();
8066 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8067 // It will be analyzed later.
8068 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008069 SrcExprs.push_back(nullptr);
8070 DstExprs.push_back(nullptr);
8071 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008072 continue;
8073 }
8074
8075 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8076 // A list item that appears in a copyin clause must be threadprivate.
8077 if (!DSAStack->isThreadPrivate(VD)) {
8078 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008079 << getOpenMPClauseName(OMPC_copyin)
8080 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008081 continue;
8082 }
8083
8084 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8085 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008086 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008087 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008088 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008089 auto *SrcVD =
8090 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8091 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008092 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008093 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8094 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008095 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8096 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008097 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008098 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008099 // For arrays generate assignment operation for single element and replace
8100 // it by the original array element in CodeGen.
8101 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8102 PseudoDstExpr, PseudoSrcExpr);
8103 if (AssignmentOp.isInvalid())
8104 continue;
8105 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8106 /*DiscardedValue=*/true);
8107 if (AssignmentOp.isInvalid())
8108 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008109
8110 DSAStack->addDSA(VD, DE, OMPC_copyin);
8111 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008112 SrcExprs.push_back(PseudoSrcExpr);
8113 DstExprs.push_back(PseudoDstExpr);
8114 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008115 }
8116
Alexey Bataeved09d242014-05-28 05:53:51 +00008117 if (Vars.empty())
8118 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008119
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008120 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8121 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008122}
8123
Alexey Bataevbae9a792014-06-27 10:37:06 +00008124OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8125 SourceLocation StartLoc,
8126 SourceLocation LParenLoc,
8127 SourceLocation EndLoc) {
8128 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008129 SmallVector<Expr *, 8> SrcExprs;
8130 SmallVector<Expr *, 8> DstExprs;
8131 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008132 for (auto &RefExpr : VarList) {
8133 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8134 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8135 // It will be analyzed later.
8136 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008137 SrcExprs.push_back(nullptr);
8138 DstExprs.push_back(nullptr);
8139 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008140 continue;
8141 }
8142
8143 SourceLocation ELoc = RefExpr->getExprLoc();
8144 // OpenMP [2.1, C/C++]
8145 // A list item is a variable name.
8146 // OpenMP [2.14.4.1, Restrictions, p.1]
8147 // A list item that appears in a copyin clause must be threadprivate.
8148 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8149 if (!DE || !isa<VarDecl>(DE->getDecl())) {
8150 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
8151 continue;
8152 }
8153
8154 Decl *D = DE->getDecl();
8155 VarDecl *VD = cast<VarDecl>(D);
8156
8157 QualType Type = VD->getType();
8158 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8159 // It will be analyzed later.
8160 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008161 SrcExprs.push_back(nullptr);
8162 DstExprs.push_back(nullptr);
8163 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008164 continue;
8165 }
8166
8167 // OpenMP [2.14.4.2, Restrictions, p.2]
8168 // A list item that appears in a copyprivate clause may not appear in a
8169 // private or firstprivate clause on the single construct.
8170 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008171 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008172 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8173 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008174 Diag(ELoc, diag::err_omp_wrong_dsa)
8175 << getOpenMPClauseName(DVar.CKind)
8176 << getOpenMPClauseName(OMPC_copyprivate);
8177 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8178 continue;
8179 }
8180
8181 // OpenMP [2.11.4.2, Restrictions, p.1]
8182 // All list items that appear in a copyprivate clause must be either
8183 // threadprivate or private in the enclosing context.
8184 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008185 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008186 if (DVar.CKind == OMPC_shared) {
8187 Diag(ELoc, diag::err_omp_required_access)
8188 << getOpenMPClauseName(OMPC_copyprivate)
8189 << "threadprivate or private in the enclosing context";
8190 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8191 continue;
8192 }
8193 }
8194 }
8195
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008196 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008197 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008198 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008199 << getOpenMPClauseName(OMPC_copyprivate) << Type
8200 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008201 bool IsDecl =
8202 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8203 Diag(VD->getLocation(),
8204 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8205 << VD;
8206 continue;
8207 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008208
Alexey Bataevbae9a792014-06-27 10:37:06 +00008209 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8210 // A variable of class type (or array thereof) that appears in a
8211 // copyin clause requires an accessible, unambiguous copy assignment
8212 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008213 Type = Context.getBaseElementType(Type.getNonReferenceType())
8214 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008215 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008216 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8217 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008218 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008219 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008220 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008221 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8222 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008223 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008224 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008225 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8226 PseudoDstExpr, PseudoSrcExpr);
8227 if (AssignmentOp.isInvalid())
8228 continue;
8229 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8230 /*DiscardedValue=*/true);
8231 if (AssignmentOp.isInvalid())
8232 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008233
8234 // No need to mark vars as copyprivate, they are already threadprivate or
8235 // implicitly private.
8236 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008237 SrcExprs.push_back(PseudoSrcExpr);
8238 DstExprs.push_back(PseudoDstExpr);
8239 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008240 }
8241
8242 if (Vars.empty())
8243 return nullptr;
8244
Alexey Bataeva63048e2015-03-23 06:18:07 +00008245 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8246 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008247}
8248
Alexey Bataev6125da92014-07-21 11:26:11 +00008249OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8250 SourceLocation StartLoc,
8251 SourceLocation LParenLoc,
8252 SourceLocation EndLoc) {
8253 if (VarList.empty())
8254 return nullptr;
8255
8256 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8257}
Alexey Bataevdea47612014-07-23 07:46:59 +00008258
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008259OMPClause *
8260Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8261 SourceLocation DepLoc, SourceLocation ColonLoc,
8262 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8263 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008264 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008265 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008266 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008267 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008268 return nullptr;
8269 }
8270 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008271 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8272 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008273 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008274 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008275 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8276 /*Last=*/OMPC_DEPEND_unknown, Except)
8277 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008278 return nullptr;
8279 }
8280 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008281 llvm::APSInt DepCounter(/*BitWidth=*/32);
8282 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8283 if (DepKind == OMPC_DEPEND_sink) {
8284 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8285 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8286 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008287 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008288 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008289 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8290 DSAStack->getParentOrderedRegionParam()) {
8291 for (auto &RefExpr : VarList) {
8292 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8293 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8294 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8295 // It will be analyzed later.
8296 Vars.push_back(RefExpr);
8297 continue;
8298 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008299
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008300 SourceLocation ELoc = RefExpr->getExprLoc();
8301 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8302 if (DepKind == OMPC_DEPEND_sink) {
8303 if (DepCounter >= TotalDepCount) {
8304 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8305 continue;
8306 }
8307 ++DepCounter;
8308 // OpenMP [2.13.9, Summary]
8309 // depend(dependence-type : vec), where dependence-type is:
8310 // 'sink' and where vec is the iteration vector, which has the form:
8311 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8312 // where n is the value specified by the ordered clause in the loop
8313 // directive, xi denotes the loop iteration variable of the i-th nested
8314 // loop associated with the loop directive, and di is a constant
8315 // non-negative integer.
8316 SimpleExpr = SimpleExpr->IgnoreImplicit();
8317 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8318 if (!DE) {
8319 OverloadedOperatorKind OOK = OO_None;
8320 SourceLocation OOLoc;
8321 Expr *LHS, *RHS;
8322 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8323 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8324 OOLoc = BO->getOperatorLoc();
8325 LHS = BO->getLHS()->IgnoreParenImpCasts();
8326 RHS = BO->getRHS()->IgnoreParenImpCasts();
8327 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8328 OOK = OCE->getOperator();
8329 OOLoc = OCE->getOperatorLoc();
8330 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8331 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8332 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8333 OOK = MCE->getMethodDecl()
8334 ->getNameInfo()
8335 .getName()
8336 .getCXXOverloadedOperator();
8337 OOLoc = MCE->getCallee()->getExprLoc();
8338 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8339 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8340 } else {
8341 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8342 continue;
8343 }
8344 DE = dyn_cast<DeclRefExpr>(LHS);
8345 if (!DE) {
8346 Diag(LHS->getExprLoc(),
8347 diag::err_omp_depend_sink_expected_loop_iteration)
8348 << DSAStack->getParentLoopControlVariable(
8349 DepCounter.getZExtValue());
8350 continue;
8351 }
8352 if (OOK != OO_Plus && OOK != OO_Minus) {
8353 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8354 continue;
8355 }
8356 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8357 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8358 if (Res.isInvalid())
8359 continue;
8360 }
8361 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8362 if (!CurContext->isDependentContext() &&
8363 DSAStack->getParentOrderedRegionParam() &&
8364 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8365 Diag(DE->getExprLoc(),
8366 diag::err_omp_depend_sink_expected_loop_iteration)
8367 << DSAStack->getParentLoopControlVariable(
8368 DepCounter.getZExtValue());
8369 continue;
8370 }
8371 } else {
8372 // OpenMP [2.11.1.1, Restrictions, p.3]
8373 // A variable that is part of another variable (such as a field of a
8374 // structure) but is not an array element or an array section cannot
8375 // appear in a depend clause.
8376 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8377 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8378 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8379 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8380 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8381 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8382 !ASE->getBase()->getType()->isArrayType())) {
8383 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8384 << RefExpr->getSourceRange();
8385 continue;
8386 }
8387 }
8388
8389 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8390 }
8391
8392 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8393 TotalDepCount > VarList.size() &&
8394 DSAStack->getParentOrderedRegionParam()) {
8395 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8396 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8397 }
8398 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8399 Vars.empty())
8400 return nullptr;
8401 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008402
8403 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8404 DepLoc, ColonLoc, Vars);
8405}
Michael Wonge710d542015-08-07 16:16:36 +00008406
8407OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8408 SourceLocation LParenLoc,
8409 SourceLocation EndLoc) {
8410 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008411
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008412 // OpenMP [2.9.1, Restrictions]
8413 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008414 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8415 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008416 return nullptr;
8417
Michael Wonge710d542015-08-07 16:16:36 +00008418 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8419}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008420
8421static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8422 DSAStackTy *Stack, CXXRecordDecl *RD) {
8423 if (!RD || RD->isInvalidDecl())
8424 return true;
8425
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008426 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8427 if (auto *CTD = CTSD->getSpecializedTemplate())
8428 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008429 auto QTy = SemaRef.Context.getRecordType(RD);
8430 if (RD->isDynamicClass()) {
8431 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8432 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8433 return false;
8434 }
8435 auto *DC = RD;
8436 bool IsCorrect = true;
8437 for (auto *I : DC->decls()) {
8438 if (I) {
8439 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8440 if (MD->isStatic()) {
8441 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8442 SemaRef.Diag(MD->getLocation(),
8443 diag::note_omp_static_member_in_target);
8444 IsCorrect = false;
8445 }
8446 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8447 if (VD->isStaticDataMember()) {
8448 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8449 SemaRef.Diag(VD->getLocation(),
8450 diag::note_omp_static_member_in_target);
8451 IsCorrect = false;
8452 }
8453 }
8454 }
8455 }
8456
8457 for (auto &I : RD->bases()) {
8458 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8459 I.getType()->getAsCXXRecordDecl()))
8460 IsCorrect = false;
8461 }
8462 return IsCorrect;
8463}
8464
8465static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8466 DSAStackTy *Stack, QualType QTy) {
8467 NamedDecl *ND;
8468 if (QTy->isIncompleteType(&ND)) {
8469 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8470 return false;
8471 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8472 if (!RD->isInvalidDecl() &&
8473 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8474 return false;
8475 }
8476 return true;
8477}
8478
Samuel Antao23abd722016-01-19 20:40:49 +00008479OMPClause *
8480Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
8481 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8482 SourceLocation MapLoc, SourceLocation ColonLoc,
8483 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8484 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00008485 SmallVector<Expr *, 4> Vars;
8486
8487 for (auto &RE : VarList) {
8488 assert(RE && "Null expr in omp map");
8489 if (isa<DependentScopeDeclRefExpr>(RE)) {
8490 // It will be analyzed later.
8491 Vars.push_back(RE);
8492 continue;
8493 }
8494 SourceLocation ELoc = RE->getExprLoc();
8495
8496 // OpenMP [2.14.5, Restrictions]
8497 // A variable that is part of another variable (such as field of a
8498 // structure) but is not an array element or an array section cannot appear
8499 // in a map clause.
8500 auto *VE = RE->IgnoreParenLValueCasts();
8501
8502 if (VE->isValueDependent() || VE->isTypeDependent() ||
8503 VE->isInstantiationDependent() ||
8504 VE->containsUnexpandedParameterPack()) {
8505 // It will be analyzed later.
8506 Vars.push_back(RE);
8507 continue;
8508 }
8509
8510 auto *SimpleExpr = RE->IgnoreParenCasts();
8511 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8512 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8513 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8514
8515 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8516 (!OASE && !ASE && !DE) ||
8517 (DE && !isa<VarDecl>(DE->getDecl())) ||
8518 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8519 !ASE->getBase()->getType()->isArrayType())) {
8520 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8521 << RE->getSourceRange();
8522 continue;
8523 }
8524
8525 Decl *D = nullptr;
8526 if (DE) {
8527 D = DE->getDecl();
8528 } else if (ASE) {
8529 auto *B = ASE->getBase()->IgnoreParenCasts();
8530 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8531 } else if (OASE) {
8532 auto *B = OASE->getBase();
8533 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8534 }
8535 assert(D && "Null decl on map clause.");
8536 auto *VD = cast<VarDecl>(D);
8537
8538 // OpenMP [2.14.5, Restrictions, p.8]
8539 // threadprivate variables cannot appear in a map clause.
8540 if (DSAStack->isThreadPrivate(VD)) {
8541 auto DVar = DSAStack->getTopDSA(VD, false);
8542 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8543 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8544 continue;
8545 }
8546
8547 // OpenMP [2.14.5, Restrictions, p.2]
8548 // At most one list item can be an array item derived from a given variable
8549 // in map clauses of the same construct.
8550 // OpenMP [2.14.5, Restrictions, p.3]
8551 // List items of map clauses in the same construct must not share original
8552 // storage.
8553 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8554 // A variable for which the type is pointer, reference to array, or
8555 // reference to pointer and an array section derived from that variable
8556 // must not appear as list items of map clauses of the same construct.
8557 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8558 if (MI.RefExpr) {
8559 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8560 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8561 << MI.RefExpr->getSourceRange();
8562 continue;
8563 }
8564
8565 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8566 // A variable for which the type is pointer, reference to array, or
8567 // reference to pointer must not appear as a list item if the enclosing
8568 // device data environment already contains an array section derived from
8569 // that variable.
8570 // An array section derived from a variable for which the type is pointer,
8571 // reference to array, or reference to pointer must not appear as a list
8572 // item if the enclosing device data environment already contains that
8573 // variable.
8574 QualType Type = VD->getType();
8575 MI = DSAStack->getMapInfoForVar(VD);
8576 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8577 isa<DeclRefExpr>(VE)) &&
8578 (Type->isPointerType() || Type->isReferenceType())) {
8579 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8580 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8581 << MI.RefExpr->getSourceRange();
8582 continue;
8583 }
8584
8585 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8586 // A list item must have a mappable type.
8587 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8588 DSAStack, Type))
8589 continue;
8590
Samuel Antaodf67fc42016-01-19 19:15:56 +00008591 // target enter data
8592 // OpenMP [2.10.2, Restrictions, p. 99]
8593 // A map-type must be specified in all map clauses and must be either
8594 // to or alloc.
8595 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8596 if (DKind == OMPD_target_enter_data &&
8597 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
8598 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00008599 << (IsMapTypeImplicit ? 1 : 0)
8600 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00008601 << getOpenMPDirectiveName(DKind);
8602 // Proceed to add the variable in a map clause anyway, to prevent
8603 // further spurious messages
8604 }
8605
Samuel Antao72590762016-01-19 20:04:50 +00008606 // target exit_data
8607 // OpenMP [2.10.3, Restrictions, p. 102]
8608 // A map-type must be specified in all map clauses and must be either
8609 // from, release, or delete.
8610 DKind = DSAStack->getCurrentDirective();
8611 if (DKind == OMPD_target_exit_data &&
8612 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
8613 MapType == OMPC_MAP_delete)) {
8614 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00008615 << (IsMapTypeImplicit ? 1 : 0)
8616 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00008617 << getOpenMPDirectiveName(DKind);
8618 // Proceed to add the variable in a map clause anyway, to prevent
8619 // further spurious messages
8620 }
8621
Kelvin Li0bff7af2015-11-23 05:32:03 +00008622 Vars.push_back(RE);
8623 MI.RefExpr = RE;
8624 DSAStack->addMapInfoForVar(VD, MI);
8625 }
8626 if (Vars.empty())
8627 return nullptr;
8628
8629 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00008630 MapTypeModifier, MapType, IsMapTypeImplicit,
8631 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00008632}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008633
8634OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8635 SourceLocation StartLoc,
8636 SourceLocation LParenLoc,
8637 SourceLocation EndLoc) {
8638 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008639
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008640 // OpenMP [teams Constrcut, Restrictions]
8641 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008642 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8643 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008644 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008645
8646 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8647}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008648
8649OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8650 SourceLocation StartLoc,
8651 SourceLocation LParenLoc,
8652 SourceLocation EndLoc) {
8653 Expr *ValExpr = ThreadLimit;
8654
8655 // OpenMP [teams Constrcut, Restrictions]
8656 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008657 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8658 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008659 return nullptr;
8660
8661 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8662 EndLoc);
8663}
Alexey Bataeva0569352015-12-01 10:17:31 +00008664
8665OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8666 SourceLocation StartLoc,
8667 SourceLocation LParenLoc,
8668 SourceLocation EndLoc) {
8669 Expr *ValExpr = Priority;
8670
8671 // OpenMP [2.9.1, task Constrcut]
8672 // The priority-value is a non-negative numerical scalar expression.
8673 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8674 /*StrictlyPositive=*/false))
8675 return nullptr;
8676
8677 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8678}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008679
8680OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8681 SourceLocation StartLoc,
8682 SourceLocation LParenLoc,
8683 SourceLocation EndLoc) {
8684 Expr *ValExpr = Grainsize;
8685
8686 // OpenMP [2.9.2, taskloop Constrcut]
8687 // The parameter of the grainsize clause must be a positive integer
8688 // expression.
8689 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8690 /*StrictlyPositive=*/true))
8691 return nullptr;
8692
8693 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8694}
Alexey Bataev382967a2015-12-08 12:06:20 +00008695
8696OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8697 SourceLocation StartLoc,
8698 SourceLocation LParenLoc,
8699 SourceLocation EndLoc) {
8700 Expr *ValExpr = NumTasks;
8701
8702 // OpenMP [2.9.2, taskloop Constrcut]
8703 // The parameter of the num_tasks clause must be a positive integer
8704 // expression.
8705 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8706 /*StrictlyPositive=*/true))
8707 return nullptr;
8708
8709 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8710}
8711
Alexey Bataev28c75412015-12-15 08:19:24 +00008712OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8713 SourceLocation LParenLoc,
8714 SourceLocation EndLoc) {
8715 // OpenMP [2.13.2, critical construct, Description]
8716 // ... where hint-expression is an integer constant expression that evaluates
8717 // to a valid lock hint.
8718 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8719 if (HintExpr.isInvalid())
8720 return nullptr;
8721 return new (Context)
8722 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8723}
8724
Carlo Bertollib4adf552016-01-15 18:50:31 +00008725OMPClause *Sema::ActOnOpenMPDistScheduleClause(
8726 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8727 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
8728 SourceLocation EndLoc) {
8729 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
8730 std::string Values;
8731 Values += "'";
8732 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
8733 Values += "'";
8734 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8735 << Values << getOpenMPClauseName(OMPC_dist_schedule);
8736 return nullptr;
8737 }
8738 Expr *ValExpr = ChunkSize;
8739 Expr *HelperValExpr = nullptr;
8740 if (ChunkSize) {
8741 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8742 !ChunkSize->isInstantiationDependent() &&
8743 !ChunkSize->containsUnexpandedParameterPack()) {
8744 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8745 ExprResult Val =
8746 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8747 if (Val.isInvalid())
8748 return nullptr;
8749
8750 ValExpr = Val.get();
8751
8752 // OpenMP [2.7.1, Restrictions]
8753 // chunk_size must be a loop invariant integer expression with a positive
8754 // value.
8755 llvm::APSInt Result;
8756 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8757 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8758 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8759 << "dist_schedule" << ChunkSize->getSourceRange();
8760 return nullptr;
8761 }
8762 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
8763 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
8764 ChunkSize->getType(), ".chunk.");
8765 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
8766 ChunkSize->getExprLoc(),
8767 /*RefersToCapture=*/true);
8768 HelperValExpr = ImpVarRef;
8769 }
8770 }
8771 }
8772
8773 return new (Context)
8774 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
8775 Kind, ValExpr, HelperValExpr);
8776}