blob: a3e33bc6580299b9f68820c65f506570c2996bc6 [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:
Alexey Bataev9959db52014-05-06 10:08:46 +00001614 llvm_unreachable("OpenMP Directive is not allowed");
1615 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001616 llvm_unreachable("Unknown OpenMP directive");
1617 }
1618}
1619
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001620StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1621 ArrayRef<OMPClause *> Clauses) {
1622 if (!S.isUsable()) {
1623 ActOnCapturedRegionError();
1624 return StmtError();
1625 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001626
1627 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001628 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001629 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001630 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001631 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001632 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001633 Clause->getClauseKind() == OMPC_copyprivate ||
1634 (getLangOpts().OpenMPUseTLS &&
1635 getASTContext().getTargetInfo().isTLSSupported() &&
1636 Clause->getClauseKind() == OMPC_copyin)) {
1637 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001638 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001639 for (auto *VarRef : Clause->children()) {
1640 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001641 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001642 }
1643 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001644 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001645 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1646 Clause->getClauseKind() == OMPC_schedule) {
1647 // Mark all variables in private list clauses as used in inner region.
1648 // Required for proper codegen of combined directives.
1649 // TODO: add processing for other clauses.
1650 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001651 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1652 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001653 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001654 if (Clause->getClauseKind() == OMPC_schedule)
1655 SC = cast<OMPScheduleClause>(Clause);
1656 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001657 OC = cast<OMPOrderedClause>(Clause);
1658 else if (Clause->getClauseKind() == OMPC_linear)
1659 LCs.push_back(cast<OMPLinearClause>(Clause));
1660 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001661 bool ErrorFound = false;
1662 // OpenMP, 2.7.1 Loop Construct, Restrictions
1663 // The nonmonotonic modifier cannot be specified if an ordered clause is
1664 // specified.
1665 if (SC &&
1666 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1667 SC->getSecondScheduleModifier() ==
1668 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1669 OC) {
1670 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1671 ? SC->getFirstScheduleModifierLoc()
1672 : SC->getSecondScheduleModifierLoc(),
1673 diag::err_omp_schedule_nonmonotonic_ordered)
1674 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1675 ErrorFound = true;
1676 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001677 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1678 for (auto *C : LCs) {
1679 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1680 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1681 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001682 ErrorFound = true;
1683 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001684 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1685 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1686 OC->getNumForLoops()) {
1687 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1688 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1689 ErrorFound = true;
1690 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001691 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001692 ActOnCapturedRegionError();
1693 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001694 }
1695 return ActOnCapturedRegionEnd(S.get());
1696}
1697
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001698static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1699 OpenMPDirectiveKind CurrentRegion,
1700 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001701 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001702 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001703 // Allowed nesting of constructs
1704 // +------------------+-----------------+------------------------------------+
1705 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1706 // +------------------+-----------------+------------------------------------+
1707 // | parallel | parallel | * |
1708 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001709 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001710 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001711 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001712 // | parallel | simd | * |
1713 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001714 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001715 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001716 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001717 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001718 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001719 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001720 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001721 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001722 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001723 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001724 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001725 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001726 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001727 // | parallel | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001728 // | parallel | target enter | * |
1729 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001730 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001731 // | parallel | cancellation | |
1732 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001733 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001734 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001735 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001736 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001737 // +------------------+-----------------+------------------------------------+
1738 // | for | parallel | * |
1739 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001740 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001741 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001742 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001743 // | for | simd | * |
1744 // | for | sections | + |
1745 // | for | section | + |
1746 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001747 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001748 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001749 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001750 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001751 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001752 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001753 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001754 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001755 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001756 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001757 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001758 // | for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001759 // | for | target enter | * |
1760 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001761 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001762 // | for | cancellation | |
1763 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001764 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001765 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001766 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001767 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001768 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001769 // | master | parallel | * |
1770 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001771 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001772 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001773 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001774 // | master | simd | * |
1775 // | master | sections | + |
1776 // | master | section | + |
1777 // | master | single | + |
1778 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001779 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001780 // | master |parallel sections| * |
1781 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001782 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001783 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001784 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001785 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001786 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001787 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001788 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001789 // | master | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001790 // | master | target enter | * |
1791 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001792 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001793 // | master | cancellation | |
1794 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001795 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001796 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001797 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001798 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001799 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001800 // | critical | parallel | * |
1801 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001802 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001803 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001804 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001805 // | critical | simd | * |
1806 // | critical | sections | + |
1807 // | critical | section | + |
1808 // | critical | single | + |
1809 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001810 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001811 // | critical |parallel sections| * |
1812 // | critical | task | * |
1813 // | critical | taskyield | * |
1814 // | critical | barrier | + |
1815 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001816 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001817 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001818 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001819 // | critical | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001820 // | critical | target enter | * |
1821 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001822 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001823 // | critical | cancellation | |
1824 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001825 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001826 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001827 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001828 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001829 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001830 // | simd | parallel | |
1831 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001832 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001833 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001834 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001835 // | simd | simd | |
1836 // | simd | sections | |
1837 // | simd | section | |
1838 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001839 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001840 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001841 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001842 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001843 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001844 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001845 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001846 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001847 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001848 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001849 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001850 // | simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001851 // | simd | target enter | |
1852 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001853 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001854 // | simd | cancellation | |
1855 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001856 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001857 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001858 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001859 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001860 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001861 // | for simd | parallel | |
1862 // | for simd | for | |
1863 // | for simd | for simd | |
1864 // | for simd | master | |
1865 // | for simd | critical | |
1866 // | for simd | simd | |
1867 // | for simd | sections | |
1868 // | for simd | section | |
1869 // | for simd | single | |
1870 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001871 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001872 // | for simd |parallel sections| |
1873 // | for simd | task | |
1874 // | for simd | taskyield | |
1875 // | for simd | barrier | |
1876 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001877 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001878 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001879 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001880 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001881 // | for simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001882 // | for simd | target enter | |
1883 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001884 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001885 // | for simd | cancellation | |
1886 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001887 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001888 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001889 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001890 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001891 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001892 // | parallel for simd| parallel | |
1893 // | parallel for simd| for | |
1894 // | parallel for simd| for simd | |
1895 // | parallel for simd| master | |
1896 // | parallel for simd| critical | |
1897 // | parallel for simd| simd | |
1898 // | parallel for simd| sections | |
1899 // | parallel for simd| section | |
1900 // | parallel for simd| single | |
1901 // | parallel for simd| parallel for | |
1902 // | parallel for simd|parallel for simd| |
1903 // | parallel for simd|parallel sections| |
1904 // | parallel for simd| task | |
1905 // | parallel for simd| taskyield | |
1906 // | parallel for simd| barrier | |
1907 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001908 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001909 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001910 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001911 // | parallel for simd| atomic | |
1912 // | parallel for simd| target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001913 // | parallel for simd| target enter | |
1914 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001915 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001916 // | parallel for simd| cancellation | |
1917 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001918 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001919 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001920 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001921 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001922 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001923 // | sections | parallel | * |
1924 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001925 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001926 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001927 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001928 // | sections | simd | * |
1929 // | sections | sections | + |
1930 // | sections | section | * |
1931 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001932 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001933 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001935 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001936 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001937 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001938 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001939 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001940 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001941 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001942 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001943 // | sections | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001944 // | sections | target enter | * |
1945 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001946 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001947 // | sections | cancellation | |
1948 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001949 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001950 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001951 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001952 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001953 // +------------------+-----------------+------------------------------------+
1954 // | section | parallel | * |
1955 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001956 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001957 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001958 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001959 // | section | simd | * |
1960 // | section | sections | + |
1961 // | section | section | + |
1962 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001963 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001964 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001965 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001966 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001967 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001968 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001969 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001970 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001971 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001972 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001973 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001974 // | section | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001975 // | section | target enter | * |
1976 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001977 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001978 // | section | cancellation | |
1979 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001980 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001981 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001982 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001983 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001984 // +------------------+-----------------+------------------------------------+
1985 // | single | parallel | * |
1986 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001987 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001988 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001989 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001990 // | single | simd | * |
1991 // | single | sections | + |
1992 // | single | section | + |
1993 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001994 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001995 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001996 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001997 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001998 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001999 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002000 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002001 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002002 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002003 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002004 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002005 // | single | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002006 // | single | target enter | * |
2007 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002008 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002009 // | single | cancellation | |
2010 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002011 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002012 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002013 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002014 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002015 // +------------------+-----------------+------------------------------------+
2016 // | parallel for | parallel | * |
2017 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002018 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002019 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002020 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002021 // | parallel for | simd | * |
2022 // | parallel for | sections | + |
2023 // | parallel for | section | + |
2024 // | parallel for | single | + |
2025 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002026 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002027 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002028 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002029 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002030 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002031 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002032 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002033 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002034 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002035 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002036 // | parallel for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002037 // | parallel for | target enter | * |
2038 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002039 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002040 // | parallel for | cancellation | |
2041 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002042 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002043 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002044 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002045 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002046 // +------------------+-----------------+------------------------------------+
2047 // | parallel sections| parallel | * |
2048 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002049 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002050 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002051 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002052 // | parallel sections| simd | * |
2053 // | parallel sections| sections | + |
2054 // | parallel sections| section | * |
2055 // | parallel sections| single | + |
2056 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002057 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002058 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002059 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002060 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002061 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002062 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002063 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002064 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002065 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002066 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002067 // | parallel sections| target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002068 // | parallel sections| target enter | * |
2069 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002070 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002071 // | parallel sections| cancellation | |
2072 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002073 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002074 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002075 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002076 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002077 // +------------------+-----------------+------------------------------------+
2078 // | task | parallel | * |
2079 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002080 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002081 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002082 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002083 // | task | simd | * |
2084 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002085 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002086 // | task | single | + |
2087 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002088 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002089 // | task |parallel sections| * |
2090 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002091 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002092 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002093 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002094 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002095 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002096 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002097 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002098 // | task | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002099 // | task | target enter | * |
2100 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002101 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002102 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002103 // | | point | ! |
2104 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002105 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002106 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002107 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002108 // +------------------+-----------------+------------------------------------+
2109 // | ordered | parallel | * |
2110 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002111 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002112 // | ordered | master | * |
2113 // | ordered | critical | * |
2114 // | ordered | simd | * |
2115 // | ordered | sections | + |
2116 // | ordered | section | + |
2117 // | ordered | single | + |
2118 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002119 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002120 // | ordered |parallel sections| * |
2121 // | ordered | task | * |
2122 // | ordered | taskyield | * |
2123 // | ordered | barrier | + |
2124 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002125 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002126 // | ordered | flush | * |
2127 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002128 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002129 // | ordered | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002130 // | ordered | target enter | * |
2131 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002132 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002133 // | ordered | cancellation | |
2134 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002135 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002136 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002137 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002138 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002139 // +------------------+-----------------+------------------------------------+
2140 // | atomic | parallel | |
2141 // | atomic | for | |
2142 // | atomic | for simd | |
2143 // | atomic | master | |
2144 // | atomic | critical | |
2145 // | atomic | simd | |
2146 // | atomic | sections | |
2147 // | atomic | section | |
2148 // | atomic | single | |
2149 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002150 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002151 // | atomic |parallel sections| |
2152 // | atomic | task | |
2153 // | atomic | taskyield | |
2154 // | atomic | barrier | |
2155 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002156 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002157 // | atomic | flush | |
2158 // | atomic | ordered | |
2159 // | atomic | atomic | |
2160 // | atomic | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002161 // | atomic | target enter | |
2162 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002163 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002164 // | atomic | cancellation | |
2165 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002166 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002167 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002168 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002169 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002170 // +------------------+-----------------+------------------------------------+
2171 // | target | parallel | * |
2172 // | target | for | * |
2173 // | target | for simd | * |
2174 // | target | master | * |
2175 // | target | critical | * |
2176 // | target | simd | * |
2177 // | target | sections | * |
2178 // | target | section | * |
2179 // | target | single | * |
2180 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002181 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002182 // | target |parallel sections| * |
2183 // | target | task | * |
2184 // | target | taskyield | * |
2185 // | target | barrier | * |
2186 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002187 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002188 // | target | flush | * |
2189 // | target | ordered | * |
2190 // | target | atomic | * |
2191 // | target | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002192 // | target | target enter | * |
2193 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002194 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002195 // | target | cancellation | |
2196 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002197 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002198 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002199 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002200 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002201 // +------------------+-----------------+------------------------------------+
2202 // | teams | parallel | * |
2203 // | teams | for | + |
2204 // | teams | for simd | + |
2205 // | teams | master | + |
2206 // | teams | critical | + |
2207 // | teams | simd | + |
2208 // | teams | sections | + |
2209 // | teams | section | + |
2210 // | teams | single | + |
2211 // | teams | parallel for | * |
2212 // | teams |parallel for simd| * |
2213 // | teams |parallel sections| * |
2214 // | teams | task | + |
2215 // | teams | taskyield | + |
2216 // | teams | barrier | + |
2217 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002218 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002219 // | teams | flush | + |
2220 // | teams | ordered | + |
2221 // | teams | atomic | + |
2222 // | teams | target | + |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002223 // | teams | target enter | + |
2224 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002225 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002226 // | teams | cancellation | |
2227 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002228 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002229 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002230 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002231 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002232 // +------------------+-----------------+------------------------------------+
2233 // | taskloop | parallel | * |
2234 // | taskloop | for | + |
2235 // | taskloop | for simd | + |
2236 // | taskloop | master | + |
2237 // | taskloop | critical | * |
2238 // | taskloop | simd | * |
2239 // | taskloop | sections | + |
2240 // | taskloop | section | + |
2241 // | taskloop | single | + |
2242 // | taskloop | parallel for | * |
2243 // | taskloop |parallel for simd| * |
2244 // | taskloop |parallel sections| * |
2245 // | taskloop | task | * |
2246 // | taskloop | taskyield | * |
2247 // | taskloop | barrier | + |
2248 // | taskloop | taskwait | * |
2249 // | taskloop | taskgroup | * |
2250 // | taskloop | flush | * |
2251 // | taskloop | ordered | + |
2252 // | taskloop | atomic | * |
2253 // | taskloop | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002254 // | taskloop | target enter | * |
2255 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002256 // | taskloop | teams | + |
2257 // | taskloop | cancellation | |
2258 // | | point | |
2259 // | taskloop | cancel | |
2260 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002261 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002262 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002263 // | taskloop simd | parallel | |
2264 // | taskloop simd | for | |
2265 // | taskloop simd | for simd | |
2266 // | taskloop simd | master | |
2267 // | taskloop simd | critical | |
2268 // | taskloop simd | simd | |
2269 // | taskloop simd | sections | |
2270 // | taskloop simd | section | |
2271 // | taskloop simd | single | |
2272 // | taskloop simd | parallel for | |
2273 // | taskloop simd |parallel for simd| |
2274 // | taskloop simd |parallel sections| |
2275 // | taskloop simd | task | |
2276 // | taskloop simd | taskyield | |
2277 // | taskloop simd | barrier | |
2278 // | taskloop simd | taskwait | |
2279 // | taskloop simd | taskgroup | |
2280 // | taskloop simd | flush | |
2281 // | taskloop simd | ordered | + (with simd clause) |
2282 // | taskloop simd | atomic | |
2283 // | taskloop simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002284 // | taskloop simd | target enter | |
2285 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002286 // | taskloop simd | teams | |
2287 // | taskloop simd | cancellation | |
2288 // | | point | |
2289 // | taskloop simd | cancel | |
2290 // | taskloop simd | taskloop | |
2291 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002292 // | taskloop simd | distribute | |
2293 // +------------------+-----------------+------------------------------------+
2294 // | distribute | parallel | * |
2295 // | distribute | for | * |
2296 // | distribute | for simd | * |
2297 // | distribute | master | * |
2298 // | distribute | critical | * |
2299 // | distribute | simd | * |
2300 // | distribute | sections | * |
2301 // | distribute | section | * |
2302 // | distribute | single | * |
2303 // | distribute | parallel for | * |
2304 // | distribute |parallel for simd| * |
2305 // | distribute |parallel sections| * |
2306 // | distribute | task | * |
2307 // | distribute | taskyield | * |
2308 // | distribute | barrier | * |
2309 // | distribute | taskwait | * |
2310 // | distribute | taskgroup | * |
2311 // | distribute | flush | * |
2312 // | distribute | ordered | + |
2313 // | distribute | atomic | * |
2314 // | distribute | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002315 // | distribute | target enter | |
2316 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002317 // | distribute | teams | |
2318 // | distribute | cancellation | + |
2319 // | | point | |
2320 // | distribute | cancel | + |
2321 // | distribute | taskloop | * |
2322 // | distribute | taskloop simd | * |
2323 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002324 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002325 if (Stack->getCurScope()) {
2326 auto ParentRegion = Stack->getParentDirective();
2327 bool NestingProhibited = false;
2328 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002329 enum {
2330 NoRecommend,
2331 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002332 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002333 ShouldBeInTargetRegion,
2334 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002335 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002336 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002337 // OpenMP [2.16, Nesting of Regions]
2338 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002339 // OpenMP [2.8.1,simd Construct, Restrictions]
2340 // An ordered construct with the simd clause is the only OpenMP construct
2341 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002342 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2343 return true;
2344 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002345 if (ParentRegion == OMPD_atomic) {
2346 // OpenMP [2.16, Nesting of Regions]
2347 // OpenMP constructs may not be nested inside an atomic region.
2348 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2349 return true;
2350 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002351 if (CurrentRegion == OMPD_section) {
2352 // OpenMP [2.7.2, sections Construct, Restrictions]
2353 // Orphaned section directives are prohibited. That is, the section
2354 // directives must appear within the sections construct and must not be
2355 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002356 if (ParentRegion != OMPD_sections &&
2357 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002358 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2359 << (ParentRegion != OMPD_unknown)
2360 << getOpenMPDirectiveName(ParentRegion);
2361 return true;
2362 }
2363 return false;
2364 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002365 // Allow some constructs to be orphaned (they could be used in functions,
2366 // called from OpenMP regions with the required preconditions).
2367 if (ParentRegion == OMPD_unknown)
2368 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002369 if (CurrentRegion == OMPD_cancellation_point ||
2370 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002371 // OpenMP [2.16, Nesting of Regions]
2372 // A cancellation point construct for which construct-type-clause is
2373 // taskgroup must be nested inside a task construct. A cancellation
2374 // point construct for which construct-type-clause is not taskgroup must
2375 // be closely nested inside an OpenMP construct that matches the type
2376 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002377 // A cancel construct for which construct-type-clause is taskgroup must be
2378 // nested inside a task construct. A cancel construct for which
2379 // construct-type-clause is not taskgroup must be closely nested inside an
2380 // OpenMP construct that matches the type specified in
2381 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002382 NestingProhibited =
2383 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002384 (CancelRegion == OMPD_for &&
2385 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002386 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2387 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002388 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2389 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002390 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002391 // OpenMP [2.16, Nesting of Regions]
2392 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002393 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002394 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002395 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002396 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002397 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2398 // OpenMP [2.16, Nesting of Regions]
2399 // A critical region may not be nested (closely or otherwise) inside a
2400 // critical region with the same name. Note that this restriction is not
2401 // sufficient to prevent deadlock.
2402 SourceLocation PreviousCriticalLoc;
2403 bool DeadLock =
2404 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2405 OpenMPDirectiveKind K,
2406 const DeclarationNameInfo &DNI,
2407 SourceLocation Loc)
2408 ->bool {
2409 if (K == OMPD_critical &&
2410 DNI.getName() == CurrentName.getName()) {
2411 PreviousCriticalLoc = Loc;
2412 return true;
2413 } else
2414 return false;
2415 },
2416 false /* skip top directive */);
2417 if (DeadLock) {
2418 SemaRef.Diag(StartLoc,
2419 diag::err_omp_prohibited_region_critical_same_name)
2420 << CurrentName.getName();
2421 if (PreviousCriticalLoc.isValid())
2422 SemaRef.Diag(PreviousCriticalLoc,
2423 diag::note_omp_previous_critical_region);
2424 return true;
2425 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002426 } else if (CurrentRegion == OMPD_barrier) {
2427 // OpenMP [2.16, Nesting of Regions]
2428 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002429 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002430 NestingProhibited =
2431 isOpenMPWorksharingDirective(ParentRegion) ||
2432 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002433 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002434 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002435 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002436 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002437 // OpenMP [2.16, Nesting of Regions]
2438 // A worksharing region may not be closely nested inside a worksharing,
2439 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002440 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002441 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002442 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002443 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002444 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002445 Recommend = ShouldBeInParallelRegion;
2446 } else if (CurrentRegion == OMPD_ordered) {
2447 // OpenMP [2.16, Nesting of Regions]
2448 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002449 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002450 // An ordered region must be closely nested inside a loop region (or
2451 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002452 // OpenMP [2.8.1,simd Construct, Restrictions]
2453 // An ordered construct with the simd clause is the only OpenMP construct
2454 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002455 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002456 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002457 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002458 !(isOpenMPSimdDirective(ParentRegion) ||
2459 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002460 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002461 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2462 // OpenMP [2.16, Nesting of Regions]
2463 // If specified, a teams construct must be contained within a target
2464 // construct.
2465 NestingProhibited = ParentRegion != OMPD_target;
2466 Recommend = ShouldBeInTargetRegion;
2467 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2468 }
2469 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2470 // OpenMP [2.16, Nesting of Regions]
2471 // distribute, parallel, parallel sections, parallel workshare, and the
2472 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2473 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002474 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2475 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002476 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002477 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002478 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2479 // OpenMP 4.5 [2.17 Nesting of Regions]
2480 // The region associated with the distribute construct must be strictly
2481 // nested inside a teams region
2482 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2483 Recommend = ShouldBeInTeamsRegion;
2484 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002485 if (NestingProhibited) {
2486 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002487 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2488 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002489 return true;
2490 }
2491 }
2492 return false;
2493}
2494
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002495static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2496 ArrayRef<OMPClause *> Clauses,
2497 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2498 bool ErrorFound = false;
2499 unsigned NamedModifiersNumber = 0;
2500 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2501 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002502 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002503 for (const auto *C : Clauses) {
2504 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2505 // At most one if clause without a directive-name-modifier can appear on
2506 // the directive.
2507 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2508 if (FoundNameModifiers[CurNM]) {
2509 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2510 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2511 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2512 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002513 } else if (CurNM != OMPD_unknown) {
2514 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002515 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002516 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002517 FoundNameModifiers[CurNM] = IC;
2518 if (CurNM == OMPD_unknown)
2519 continue;
2520 // Check if the specified name modifier is allowed for the current
2521 // directive.
2522 // At most one if clause with the particular directive-name-modifier can
2523 // appear on the directive.
2524 bool MatchFound = false;
2525 for (auto NM : AllowedNameModifiers) {
2526 if (CurNM == NM) {
2527 MatchFound = true;
2528 break;
2529 }
2530 }
2531 if (!MatchFound) {
2532 S.Diag(IC->getNameModifierLoc(),
2533 diag::err_omp_wrong_if_directive_name_modifier)
2534 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2535 ErrorFound = true;
2536 }
2537 }
2538 }
2539 // If any if clause on the directive includes a directive-name-modifier then
2540 // all if clauses on the directive must include a directive-name-modifier.
2541 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2542 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2543 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2544 diag::err_omp_no_more_if_clause);
2545 } else {
2546 std::string Values;
2547 std::string Sep(", ");
2548 unsigned AllowedCnt = 0;
2549 unsigned TotalAllowedNum =
2550 AllowedNameModifiers.size() - NamedModifiersNumber;
2551 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2552 ++Cnt) {
2553 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2554 if (!FoundNameModifiers[NM]) {
2555 Values += "'";
2556 Values += getOpenMPDirectiveName(NM);
2557 Values += "'";
2558 if (AllowedCnt + 2 == TotalAllowedNum)
2559 Values += " or ";
2560 else if (AllowedCnt + 1 != TotalAllowedNum)
2561 Values += Sep;
2562 ++AllowedCnt;
2563 }
2564 }
2565 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2566 diag::err_omp_unnamed_if_clause)
2567 << (TotalAllowedNum > 1) << Values;
2568 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002569 for (auto Loc : NameModifierLoc) {
2570 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2571 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002572 ErrorFound = true;
2573 }
2574 return ErrorFound;
2575}
2576
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002577StmtResult Sema::ActOnOpenMPExecutableDirective(
2578 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2579 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2580 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002581 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002582 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2583 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002584 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002585
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002586 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002587 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002588 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002589 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002590 if (AStmt) {
2591 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2592
2593 // Check default data sharing attributes for referenced variables.
2594 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2595 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2596 if (DSAChecker.isErrorFound())
2597 return StmtError();
2598 // Generate list of implicitly defined firstprivate variables.
2599 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002600
2601 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2602 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2603 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2604 SourceLocation(), SourceLocation())) {
2605 ClausesWithImplicit.push_back(Implicit);
2606 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2607 DSAChecker.getImplicitFirstprivate().size();
2608 } else
2609 ErrorFound = true;
2610 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002611 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002612
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002613 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002614 switch (Kind) {
2615 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002616 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2617 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002618 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002619 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002620 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002621 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2622 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002623 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002624 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002625 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2626 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002627 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002628 case OMPD_for_simd:
2629 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2630 EndLoc, VarsWithInheritedDSA);
2631 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002632 case OMPD_sections:
2633 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2634 EndLoc);
2635 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002636 case OMPD_section:
2637 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002638 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002639 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2640 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002641 case OMPD_single:
2642 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2643 EndLoc);
2644 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002645 case OMPD_master:
2646 assert(ClausesWithImplicit.empty() &&
2647 "No clauses are allowed for 'omp master' directive");
2648 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2649 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002650 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002651 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2652 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002653 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002654 case OMPD_parallel_for:
2655 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2656 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002657 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002658 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002659 case OMPD_parallel_for_simd:
2660 Res = ActOnOpenMPParallelForSimdDirective(
2661 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002662 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002663 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002664 case OMPD_parallel_sections:
2665 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2666 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002667 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002668 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002669 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002670 Res =
2671 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002672 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002673 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002674 case OMPD_taskyield:
2675 assert(ClausesWithImplicit.empty() &&
2676 "No clauses are allowed for 'omp taskyield' directive");
2677 assert(AStmt == nullptr &&
2678 "No associated statement allowed for 'omp taskyield' directive");
2679 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2680 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002681 case OMPD_barrier:
2682 assert(ClausesWithImplicit.empty() &&
2683 "No clauses are allowed for 'omp barrier' directive");
2684 assert(AStmt == nullptr &&
2685 "No associated statement allowed for 'omp barrier' directive");
2686 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2687 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002688 case OMPD_taskwait:
2689 assert(ClausesWithImplicit.empty() &&
2690 "No clauses are allowed for 'omp taskwait' directive");
2691 assert(AStmt == nullptr &&
2692 "No associated statement allowed for 'omp taskwait' directive");
2693 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2694 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002695 case OMPD_taskgroup:
2696 assert(ClausesWithImplicit.empty() &&
2697 "No clauses are allowed for 'omp taskgroup' directive");
2698 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2699 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002700 case OMPD_flush:
2701 assert(AStmt == nullptr &&
2702 "No associated statement allowed for 'omp flush' directive");
2703 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2704 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002705 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002706 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2707 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002708 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002709 case OMPD_atomic:
2710 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2711 EndLoc);
2712 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002713 case OMPD_teams:
2714 Res =
2715 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2716 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002717 case OMPD_target:
2718 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2719 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002720 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002721 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002722 case OMPD_cancellation_point:
2723 assert(ClausesWithImplicit.empty() &&
2724 "No clauses are allowed for 'omp cancellation point' directive");
2725 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2726 "cancellation point' directive");
2727 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2728 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002729 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002730 assert(AStmt == nullptr &&
2731 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002732 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2733 CancelRegion);
2734 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002735 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002736 case OMPD_target_data:
2737 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2738 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002739 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002740 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002741 case OMPD_target_enter_data:
2742 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2743 EndLoc);
2744 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2745 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002746 case OMPD_taskloop:
2747 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2748 EndLoc, VarsWithInheritedDSA);
2749 AllowedNameModifiers.push_back(OMPD_taskloop);
2750 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002751 case OMPD_taskloop_simd:
2752 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2753 EndLoc, VarsWithInheritedDSA);
2754 AllowedNameModifiers.push_back(OMPD_taskloop);
2755 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002756 case OMPD_distribute:
2757 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2758 EndLoc, VarsWithInheritedDSA);
2759 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002760 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002761 llvm_unreachable("OpenMP Directive is not allowed");
2762 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002763 llvm_unreachable("Unknown OpenMP directive");
2764 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002765
Alexey Bataev4acb8592014-07-07 13:01:15 +00002766 for (auto P : VarsWithInheritedDSA) {
2767 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2768 << P.first << P.second->getSourceRange();
2769 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002770 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2771
2772 if (!AllowedNameModifiers.empty())
2773 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2774 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002775
Alexey Bataeved09d242014-05-28 05:53:51 +00002776 if (ErrorFound)
2777 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002778 return Res;
2779}
2780
2781StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2782 Stmt *AStmt,
2783 SourceLocation StartLoc,
2784 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002785 if (!AStmt)
2786 return StmtError();
2787
Alexey Bataev9959db52014-05-06 10:08:46 +00002788 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2789 // 1.2.2 OpenMP Language Terminology
2790 // Structured block - An executable statement with a single entry at the
2791 // top and a single exit at the bottom.
2792 // The point of exit cannot be a branch out of the structured block.
2793 // longjmp() and throw() must not violate the entry/exit criteria.
2794 CS->getCapturedDecl()->setNothrow();
2795
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002796 getCurFunction()->setHasBranchProtectedScope();
2797
Alexey Bataev25e5b442015-09-15 12:52:43 +00002798 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2799 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002800}
2801
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002802namespace {
2803/// \brief Helper class for checking canonical form of the OpenMP loops and
2804/// extracting iteration space of each loop in the loop nest, that will be used
2805/// for IR generation.
2806class OpenMPIterationSpaceChecker {
2807 /// \brief Reference to Sema.
2808 Sema &SemaRef;
2809 /// \brief A location for diagnostics (when there is no some better location).
2810 SourceLocation DefaultLoc;
2811 /// \brief A location for diagnostics (when increment is not compatible).
2812 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002813 /// \brief A source location for referring to loop init later.
2814 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002815 /// \brief A source location for referring to condition later.
2816 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002817 /// \brief A source location for referring to increment later.
2818 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002819 /// \brief Loop variable.
2820 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002821 /// \brief Reference to loop variable.
2822 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002823 /// \brief Lower bound (initializer for the var).
2824 Expr *LB;
2825 /// \brief Upper bound.
2826 Expr *UB;
2827 /// \brief Loop step (increment).
2828 Expr *Step;
2829 /// \brief This flag is true when condition is one of:
2830 /// Var < UB
2831 /// Var <= UB
2832 /// UB > Var
2833 /// UB >= Var
2834 bool TestIsLessOp;
2835 /// \brief This flag is true when condition is strict ( < or > ).
2836 bool TestIsStrictOp;
2837 /// \brief This flag is true when step is subtracted on each iteration.
2838 bool SubtractStep;
2839
2840public:
2841 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2842 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002843 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2844 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002845 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2846 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002847 /// \brief Check init-expr for canonical loop form and save loop counter
2848 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002849 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002850 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2851 /// for less/greater and for strict/non-strict comparison.
2852 bool CheckCond(Expr *S);
2853 /// \brief Check incr-expr for canonical loop form and return true if it
2854 /// does not conform, otherwise save loop step (#Step).
2855 bool CheckInc(Expr *S);
2856 /// \brief Return the loop counter variable.
2857 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002858 /// \brief Return the reference expression to loop counter variable.
2859 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002860 /// \brief Source range of the loop init.
2861 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2862 /// \brief Source range of the loop condition.
2863 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2864 /// \brief Source range of the loop increment.
2865 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2866 /// \brief True if the step should be subtracted.
2867 bool ShouldSubtractStep() const { return SubtractStep; }
2868 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002869 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002870 /// \brief Build the precondition expression for the loops.
2871 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002872 /// \brief Build reference expression to the counter be used for codegen.
2873 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002874 /// \brief Build reference expression to the private counter be used for
2875 /// codegen.
2876 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002877 /// \brief Build initization of the counter be used for codegen.
2878 Expr *BuildCounterInit() const;
2879 /// \brief Build step of the counter be used for codegen.
2880 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002881 /// \brief Return true if any expression is dependent.
2882 bool Dependent() const;
2883
2884private:
2885 /// \brief Check the right-hand side of an assignment in the increment
2886 /// expression.
2887 bool CheckIncRHS(Expr *RHS);
2888 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002889 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002890 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002891 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002892 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002893 /// \brief Helper to set loop increment.
2894 bool SetStep(Expr *NewStep, bool Subtract);
2895};
2896
2897bool OpenMPIterationSpaceChecker::Dependent() const {
2898 if (!Var) {
2899 assert(!LB && !UB && !Step);
2900 return false;
2901 }
2902 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2903 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2904}
2905
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002906template <typename T>
2907static T *getExprAsWritten(T *E) {
2908 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2909 E = ExprTemp->getSubExpr();
2910
2911 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2912 E = MTE->GetTemporaryExpr();
2913
2914 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2915 E = Binder->getSubExpr();
2916
2917 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2918 E = ICE->getSubExprAsWritten();
2919 return E->IgnoreParens();
2920}
2921
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002922bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2923 DeclRefExpr *NewVarRefExpr,
2924 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002925 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002926 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2927 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002928 if (!NewVar || !NewLB)
2929 return true;
2930 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002931 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002932 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2933 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002934 if ((Ctor->isCopyOrMoveConstructor() ||
2935 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2936 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002937 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002938 LB = NewLB;
2939 return false;
2940}
2941
2942bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002943 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002944 // State consistency checking to ensure correct usage.
2945 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2946 !TestIsLessOp && !TestIsStrictOp);
2947 if (!NewUB)
2948 return true;
2949 UB = NewUB;
2950 TestIsLessOp = LessOp;
2951 TestIsStrictOp = StrictOp;
2952 ConditionSrcRange = SR;
2953 ConditionLoc = SL;
2954 return false;
2955}
2956
2957bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2958 // State consistency checking to ensure correct usage.
2959 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2960 if (!NewStep)
2961 return true;
2962 if (!NewStep->isValueDependent()) {
2963 // Check that the step is integer expression.
2964 SourceLocation StepLoc = NewStep->getLocStart();
2965 ExprResult Val =
2966 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2967 if (Val.isInvalid())
2968 return true;
2969 NewStep = Val.get();
2970
2971 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2972 // If test-expr is of form var relational-op b and relational-op is < or
2973 // <= then incr-expr must cause var to increase on each iteration of the
2974 // loop. If test-expr is of form var relational-op b and relational-op is
2975 // > or >= then incr-expr must cause var to decrease on each iteration of
2976 // the loop.
2977 // If test-expr is of form b relational-op var and relational-op is < or
2978 // <= then incr-expr must cause var to decrease on each iteration of the
2979 // loop. If test-expr is of form b relational-op var and relational-op is
2980 // > or >= then incr-expr must cause var to increase on each iteration of
2981 // the loop.
2982 llvm::APSInt Result;
2983 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2984 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2985 bool IsConstNeg =
2986 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002987 bool IsConstPos =
2988 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002989 bool IsConstZero = IsConstant && !Result.getBoolValue();
2990 if (UB && (IsConstZero ||
2991 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002992 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002993 SemaRef.Diag(NewStep->getExprLoc(),
2994 diag::err_omp_loop_incr_not_compatible)
2995 << Var << TestIsLessOp << NewStep->getSourceRange();
2996 SemaRef.Diag(ConditionLoc,
2997 diag::note_omp_loop_cond_requres_compatible_incr)
2998 << TestIsLessOp << ConditionSrcRange;
2999 return true;
3000 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003001 if (TestIsLessOp == Subtract) {
3002 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3003 NewStep).get();
3004 Subtract = !Subtract;
3005 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003006 }
3007
3008 Step = NewStep;
3009 SubtractStep = Subtract;
3010 return false;
3011}
3012
Alexey Bataev9c821032015-04-30 04:23:23 +00003013bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003014 // Check init-expr for canonical loop form and save loop counter
3015 // variable - #Var and its initialization value - #LB.
3016 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3017 // var = lb
3018 // integer-type var = lb
3019 // random-access-iterator-type var = lb
3020 // pointer-type var = lb
3021 //
3022 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003023 if (EmitDiags) {
3024 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3025 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003026 return true;
3027 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003028 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003029 if (Expr *E = dyn_cast<Expr>(S))
3030 S = E->IgnoreParens();
3031 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3032 if (BO->getOpcode() == BO_Assign)
3033 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003034 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003035 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003036 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3037 if (DS->isSingleDecl()) {
3038 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003039 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003040 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003041 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003042 SemaRef.Diag(S->getLocStart(),
3043 diag::ext_omp_loop_not_canonical_init)
3044 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003045 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003046 }
3047 }
3048 }
3049 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3050 if (CE->getOperator() == OO_Equal)
3051 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003052 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3053 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003054
Alexey Bataev9c821032015-04-30 04:23:23 +00003055 if (EmitDiags) {
3056 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3057 << S->getSourceRange();
3058 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003059 return true;
3060}
3061
Alexey Bataev23b69422014-06-18 07:08:49 +00003062/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003063/// variable (which may be the loop variable) if possible.
3064static const VarDecl *GetInitVarDecl(const Expr *E) {
3065 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003066 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003067 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003068 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3069 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003070 if ((Ctor->isCopyOrMoveConstructor() ||
3071 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3072 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003073 E = CE->getArg(0)->IgnoreParenImpCasts();
3074 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3075 if (!DRE)
3076 return nullptr;
3077 return dyn_cast<VarDecl>(DRE->getDecl());
3078}
3079
3080bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3081 // Check test-expr for canonical form, save upper-bound UB, flags for
3082 // less/greater and for strict/non-strict comparison.
3083 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3084 // var relational-op b
3085 // b relational-op var
3086 //
3087 if (!S) {
3088 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3089 return true;
3090 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003091 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003092 SourceLocation CondLoc = S->getLocStart();
3093 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3094 if (BO->isRelationalOp()) {
3095 if (GetInitVarDecl(BO->getLHS()) == Var)
3096 return SetUB(BO->getRHS(),
3097 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3098 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3099 BO->getSourceRange(), BO->getOperatorLoc());
3100 if (GetInitVarDecl(BO->getRHS()) == Var)
3101 return SetUB(BO->getLHS(),
3102 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3103 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3104 BO->getSourceRange(), BO->getOperatorLoc());
3105 }
3106 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3107 if (CE->getNumArgs() == 2) {
3108 auto Op = CE->getOperator();
3109 switch (Op) {
3110 case OO_Greater:
3111 case OO_GreaterEqual:
3112 case OO_Less:
3113 case OO_LessEqual:
3114 if (GetInitVarDecl(CE->getArg(0)) == Var)
3115 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3116 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3117 CE->getOperatorLoc());
3118 if (GetInitVarDecl(CE->getArg(1)) == Var)
3119 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3120 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3121 CE->getOperatorLoc());
3122 break;
3123 default:
3124 break;
3125 }
3126 }
3127 }
3128 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3129 << S->getSourceRange() << Var;
3130 return true;
3131}
3132
3133bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3134 // RHS of canonical loop form increment can be:
3135 // var + incr
3136 // incr + var
3137 // var - incr
3138 //
3139 RHS = RHS->IgnoreParenImpCasts();
3140 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3141 if (BO->isAdditiveOp()) {
3142 bool IsAdd = BO->getOpcode() == BO_Add;
3143 if (GetInitVarDecl(BO->getLHS()) == Var)
3144 return SetStep(BO->getRHS(), !IsAdd);
3145 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3146 return SetStep(BO->getLHS(), false);
3147 }
3148 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3149 bool IsAdd = CE->getOperator() == OO_Plus;
3150 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3151 if (GetInitVarDecl(CE->getArg(0)) == Var)
3152 return SetStep(CE->getArg(1), !IsAdd);
3153 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3154 return SetStep(CE->getArg(0), false);
3155 }
3156 }
3157 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3158 << RHS->getSourceRange() << Var;
3159 return true;
3160}
3161
3162bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3163 // Check incr-expr for canonical loop form and return true if it
3164 // does not conform.
3165 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3166 // ++var
3167 // var++
3168 // --var
3169 // var--
3170 // var += incr
3171 // var -= incr
3172 // var = var + incr
3173 // var = incr + var
3174 // var = var - incr
3175 //
3176 if (!S) {
3177 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3178 return true;
3179 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003180 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003181 S = S->IgnoreParens();
3182 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3183 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3184 return SetStep(
3185 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3186 (UO->isDecrementOp() ? -1 : 1)).get(),
3187 false);
3188 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3189 switch (BO->getOpcode()) {
3190 case BO_AddAssign:
3191 case BO_SubAssign:
3192 if (GetInitVarDecl(BO->getLHS()) == Var)
3193 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3194 break;
3195 case BO_Assign:
3196 if (GetInitVarDecl(BO->getLHS()) == Var)
3197 return CheckIncRHS(BO->getRHS());
3198 break;
3199 default:
3200 break;
3201 }
3202 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3203 switch (CE->getOperator()) {
3204 case OO_PlusPlus:
3205 case OO_MinusMinus:
3206 if (GetInitVarDecl(CE->getArg(0)) == Var)
3207 return SetStep(
3208 SemaRef.ActOnIntegerConstant(
3209 CE->getLocStart(),
3210 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3211 false);
3212 break;
3213 case OO_PlusEqual:
3214 case OO_MinusEqual:
3215 if (GetInitVarDecl(CE->getArg(0)) == Var)
3216 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3217 break;
3218 case OO_Equal:
3219 if (GetInitVarDecl(CE->getArg(0)) == Var)
3220 return CheckIncRHS(CE->getArg(1));
3221 break;
3222 default:
3223 break;
3224 }
3225 }
3226 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3227 << S->getSourceRange() << Var;
3228 return true;
3229}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003230
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003231namespace {
3232// Transform variables declared in GNU statement expressions to new ones to
3233// avoid crash on codegen.
3234class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3235 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3236
3237public:
3238 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3239
3240 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3241 if (auto *VD = cast<VarDecl>(D))
3242 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3243 !isa<ImplicitParamDecl>(D)) {
3244 auto *NewVD = VarDecl::Create(
3245 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3246 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3247 VD->getTypeSourceInfo(), VD->getStorageClass());
3248 NewVD->setTSCSpec(VD->getTSCSpec());
3249 NewVD->setInit(VD->getInit());
3250 NewVD->setInitStyle(VD->getInitStyle());
3251 NewVD->setExceptionVariable(VD->isExceptionVariable());
3252 NewVD->setNRVOVariable(VD->isNRVOVariable());
3253 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3254 NewVD->setConstexpr(VD->isConstexpr());
3255 NewVD->setInitCapture(VD->isInitCapture());
3256 NewVD->setPreviousDeclInSameBlockScope(
3257 VD->isPreviousDeclInSameBlockScope());
3258 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003259 if (VD->hasAttrs())
3260 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003261 transformedLocalDecl(VD, NewVD);
3262 return NewVD;
3263 }
3264 return BaseTransform::TransformDefinition(Loc, D);
3265 }
3266
3267 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3268 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3269 if (E->getDecl() != NewD) {
3270 NewD->setReferenced();
3271 NewD->markUsed(SemaRef.Context);
3272 return DeclRefExpr::Create(
3273 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3274 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3275 E->getNameInfo(), E->getType(), E->getValueKind());
3276 }
3277 return BaseTransform::TransformDeclRefExpr(E);
3278 }
3279};
3280}
3281
Alexander Musmana5f070a2014-10-01 06:03:56 +00003282/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003283Expr *
3284OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3285 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003286 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003287 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003288 auto VarType = Var->getType().getNonReferenceType();
3289 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003290 SemaRef.getLangOpts().CPlusPlus) {
3291 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003292 auto *UBExpr = TestIsLessOp ? UB : LB;
3293 auto *LBExpr = TestIsLessOp ? LB : UB;
3294 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3295 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3296 if (!Upper || !Lower)
3297 return nullptr;
3298 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3299 Sema::AA_Converting,
3300 /*AllowExplicit=*/true)
3301 .get();
3302 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3303 Sema::AA_Converting,
3304 /*AllowExplicit=*/true)
3305 .get();
3306 if (!Upper || !Lower)
3307 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003308
3309 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3310
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003311 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003312 // BuildBinOp already emitted error, this one is to point user to upper
3313 // and lower bound, and to tell what is passed to 'operator-'.
3314 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3315 << Upper->getSourceRange() << Lower->getSourceRange();
3316 return nullptr;
3317 }
3318 }
3319
3320 if (!Diff.isUsable())
3321 return nullptr;
3322
3323 // Upper - Lower [- 1]
3324 if (TestIsStrictOp)
3325 Diff = SemaRef.BuildBinOp(
3326 S, DefaultLoc, BO_Sub, Diff.get(),
3327 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3328 if (!Diff.isUsable())
3329 return nullptr;
3330
3331 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003332 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3333 if (NewStep.isInvalid())
3334 return nullptr;
3335 NewStep = SemaRef.PerformImplicitConversion(
3336 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3337 /*AllowExplicit=*/true);
3338 if (NewStep.isInvalid())
3339 return nullptr;
3340 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003341 if (!Diff.isUsable())
3342 return nullptr;
3343
3344 // Parentheses (for dumping/debugging purposes only).
3345 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3346 if (!Diff.isUsable())
3347 return nullptr;
3348
3349 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003350 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3351 if (NewStep.isInvalid())
3352 return nullptr;
3353 NewStep = SemaRef.PerformImplicitConversion(
3354 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3355 /*AllowExplicit=*/true);
3356 if (NewStep.isInvalid())
3357 return nullptr;
3358 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003359 if (!Diff.isUsable())
3360 return nullptr;
3361
Alexander Musman174b3ca2014-10-06 11:16:29 +00003362 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003363 QualType Type = Diff.get()->getType();
3364 auto &C = SemaRef.Context;
3365 bool UseVarType = VarType->hasIntegerRepresentation() &&
3366 C.getTypeSize(Type) > C.getTypeSize(VarType);
3367 if (!Type->isIntegerType() || UseVarType) {
3368 unsigned NewSize =
3369 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3370 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3371 : Type->hasSignedIntegerRepresentation();
3372 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3373 Diff = SemaRef.PerformImplicitConversion(
3374 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3375 if (!Diff.isUsable())
3376 return nullptr;
3377 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003378 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003379 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3380 if (NewSize != C.getTypeSize(Type)) {
3381 if (NewSize < C.getTypeSize(Type)) {
3382 assert(NewSize == 64 && "incorrect loop var size");
3383 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3384 << InitSrcRange << ConditionSrcRange;
3385 }
3386 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003387 NewSize, Type->hasSignedIntegerRepresentation() ||
3388 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003389 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3390 Sema::AA_Converting, true);
3391 if (!Diff.isUsable())
3392 return nullptr;
3393 }
3394 }
3395
Alexander Musmana5f070a2014-10-01 06:03:56 +00003396 return Diff.get();
3397}
3398
Alexey Bataev62dbb972015-04-22 11:59:37 +00003399Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3400 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3401 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3402 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003403 TransformToNewDefs Transform(SemaRef);
3404
3405 auto NewLB = Transform.TransformExpr(LB);
3406 auto NewUB = Transform.TransformExpr(UB);
3407 if (NewLB.isInvalid() || NewUB.isInvalid())
3408 return Cond;
3409 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3410 Sema::AA_Converting,
3411 /*AllowExplicit=*/true);
3412 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3413 Sema::AA_Converting,
3414 /*AllowExplicit=*/true);
3415 if (NewLB.isInvalid() || NewUB.isInvalid())
3416 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003417 auto CondExpr = SemaRef.BuildBinOp(
3418 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3419 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003420 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003421 if (CondExpr.isUsable()) {
3422 CondExpr = SemaRef.PerformImplicitConversion(
3423 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3424 /*AllowExplicit=*/true);
3425 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003426 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3427 // Otherwise use original loop conditon and evaluate it in runtime.
3428 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3429}
3430
Alexander Musmana5f070a2014-10-01 06:03:56 +00003431/// \brief Build reference expression to the counter be used for codegen.
3432Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003433 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3434 DefaultLoc);
3435}
3436
3437Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3438 if (Var && !Var->isInvalidDecl()) {
3439 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003440 auto *PrivateVar =
3441 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3442 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003443 if (PrivateVar->isInvalidDecl())
3444 return nullptr;
3445 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3446 }
3447 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003448}
3449
3450/// \brief Build initization of the counter be used for codegen.
3451Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3452
3453/// \brief Build step of the counter be used for codegen.
3454Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3455
3456/// \brief Iteration space of a single for loop.
3457struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003458 /// \brief Condition of the loop.
3459 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003460 /// \brief This expression calculates the number of iterations in the loop.
3461 /// It is always possible to calculate it before starting the loop.
3462 Expr *NumIterations;
3463 /// \brief The loop counter variable.
3464 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003465 /// \brief Private loop counter variable.
3466 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003467 /// \brief This is initializer for the initial value of #CounterVar.
3468 Expr *CounterInit;
3469 /// \brief This is step for the #CounterVar used to generate its update:
3470 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3471 Expr *CounterStep;
3472 /// \brief Should step be subtracted?
3473 bool Subtract;
3474 /// \brief Source range of the loop init.
3475 SourceRange InitSrcRange;
3476 /// \brief Source range of the loop condition.
3477 SourceRange CondSrcRange;
3478 /// \brief Source range of the loop increment.
3479 SourceRange IncSrcRange;
3480};
3481
Alexey Bataev23b69422014-06-18 07:08:49 +00003482} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003483
Alexey Bataev9c821032015-04-30 04:23:23 +00003484void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3485 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3486 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003487 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3488 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003489 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3490 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003491 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003492 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003493 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003494 }
3495}
3496
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003497/// \brief Called on a for stmt to check and extract its iteration space
3498/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003499static bool CheckOpenMPIterationSpace(
3500 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3501 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003502 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003503 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3504 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003505 // OpenMP [2.6, Canonical Loop Form]
3506 // for (init-expr; test-expr; incr-expr) structured-block
3507 auto For = dyn_cast_or_null<ForStmt>(S);
3508 if (!For) {
3509 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003510 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3511 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3512 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3513 if (NestedLoopCount > 1) {
3514 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3515 SemaRef.Diag(DSA.getConstructLoc(),
3516 diag::note_omp_collapse_ordered_expr)
3517 << 2 << CollapseLoopCountExpr->getSourceRange()
3518 << OrderedLoopCountExpr->getSourceRange();
3519 else if (CollapseLoopCountExpr)
3520 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3521 diag::note_omp_collapse_ordered_expr)
3522 << 0 << CollapseLoopCountExpr->getSourceRange();
3523 else
3524 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3525 diag::note_omp_collapse_ordered_expr)
3526 << 1 << OrderedLoopCountExpr->getSourceRange();
3527 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003528 return true;
3529 }
3530 assert(For->getBody());
3531
3532 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3533
3534 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003535 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003536 if (ISC.CheckInit(Init)) {
3537 return true;
3538 }
3539
3540 bool HasErrors = false;
3541
3542 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003543 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003544
3545 // OpenMP [2.6, Canonical Loop Form]
3546 // Var is one of the following:
3547 // A variable of signed or unsigned integer type.
3548 // For C++, a variable of a random access iterator type.
3549 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003550 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003551 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3552 !VarType->isPointerType() &&
3553 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3554 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3555 << SemaRef.getLangOpts().CPlusPlus;
3556 HasErrors = true;
3557 }
3558
Alexey Bataev4acb8592014-07-07 13:01:15 +00003559 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3560 // Construct
3561 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3562 // parallel for construct is (are) private.
3563 // The loop iteration variable in the associated for-loop of a simd construct
3564 // with just one associated for-loop is linear with a constant-linear-step
3565 // that is the increment of the associated for-loop.
3566 // Exclude loop var from the list of variables with implicitly defined data
3567 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003568 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003569
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003570 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3571 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003572 // The loop iteration variable in the associated for-loop of a simd construct
3573 // with just one associated for-loop may be listed in a linear clause with a
3574 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003575 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3576 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003577 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003578 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3579 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3580 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003581 auto PredeterminedCKind =
3582 isOpenMPSimdDirective(DKind)
3583 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3584 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003585 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003586 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003587 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003588 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003589 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003590 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3591 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003592 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003593 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3594 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003595 if (DVar.RefExpr == nullptr)
3596 DVar.CKind = PredeterminedCKind;
3597 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003598 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003599 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003600 // Make the loop iteration variable private (for worksharing constructs),
3601 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003602 // lastprivate (for simd directives with several collapsed or ordered
3603 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003604 if (DVar.CKind == OMPC_unknown)
3605 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3606 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003607 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003608 }
3609
Alexey Bataev7ff55242014-06-19 09:13:45 +00003610 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003611
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003612 // Check test-expr.
3613 HasErrors |= ISC.CheckCond(For->getCond());
3614
3615 // Check incr-expr.
3616 HasErrors |= ISC.CheckInc(For->getInc());
3617
Alexander Musmana5f070a2014-10-01 06:03:56 +00003618 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003619 return HasErrors;
3620
Alexander Musmana5f070a2014-10-01 06:03:56 +00003621 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003622 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003623 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003624 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003625 isOpenMPTaskLoopDirective(DKind) ||
3626 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003627 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003628 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003629 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3630 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3631 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3632 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3633 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3634 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3635
Alexey Bataev62dbb972015-04-22 11:59:37 +00003636 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3637 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003638 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003639 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003640 ResultIterSpace.CounterInit == nullptr ||
3641 ResultIterSpace.CounterStep == nullptr);
3642
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003643 return HasErrors;
3644}
3645
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003646/// \brief Build 'VarRef = Start.
3647static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3648 ExprResult VarRef, ExprResult Start) {
3649 TransformToNewDefs Transform(SemaRef);
3650 // Build 'VarRef = Start.
3651 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3652 if (NewStart.isInvalid())
3653 return ExprError();
3654 NewStart = SemaRef.PerformImplicitConversion(
3655 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3656 Sema::AA_Converting,
3657 /*AllowExplicit=*/true);
3658 if (NewStart.isInvalid())
3659 return ExprError();
3660 NewStart = SemaRef.PerformImplicitConversion(
3661 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3662 /*AllowExplicit=*/true);
3663 if (!NewStart.isUsable())
3664 return ExprError();
3665
3666 auto Init =
3667 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3668 return Init;
3669}
3670
Alexander Musmana5f070a2014-10-01 06:03:56 +00003671/// \brief Build 'VarRef = Start + Iter * Step'.
3672static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3673 SourceLocation Loc, ExprResult VarRef,
3674 ExprResult Start, ExprResult Iter,
3675 ExprResult Step, bool Subtract) {
3676 // Add parentheses (for debugging purposes only).
3677 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3678 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3679 !Step.isUsable())
3680 return ExprError();
3681
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003682 TransformToNewDefs Transform(SemaRef);
3683 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3684 if (NewStep.isInvalid())
3685 return ExprError();
3686 NewStep = SemaRef.PerformImplicitConversion(
3687 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3688 Sema::AA_Converting,
3689 /*AllowExplicit=*/true);
3690 if (NewStep.isInvalid())
3691 return ExprError();
3692 ExprResult Update =
3693 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003694 if (!Update.isUsable())
3695 return ExprError();
3696
3697 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003698 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3699 if (NewStart.isInvalid())
3700 return ExprError();
3701 NewStart = SemaRef.PerformImplicitConversion(
3702 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3703 Sema::AA_Converting,
3704 /*AllowExplicit=*/true);
3705 if (NewStart.isInvalid())
3706 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003707 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003708 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003709 if (!Update.isUsable())
3710 return ExprError();
3711
3712 Update = SemaRef.PerformImplicitConversion(
3713 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3714 if (!Update.isUsable())
3715 return ExprError();
3716
3717 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3718 return Update;
3719}
3720
3721/// \brief Convert integer expression \a E to make it have at least \a Bits
3722/// bits.
3723static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3724 Sema &SemaRef) {
3725 if (E == nullptr)
3726 return ExprError();
3727 auto &C = SemaRef.Context;
3728 QualType OldType = E->getType();
3729 unsigned HasBits = C.getTypeSize(OldType);
3730 if (HasBits >= Bits)
3731 return ExprResult(E);
3732 // OK to convert to signed, because new type has more bits than old.
3733 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3734 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3735 true);
3736}
3737
3738/// \brief Check if the given expression \a E is a constant integer that fits
3739/// into \a Bits bits.
3740static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3741 if (E == nullptr)
3742 return false;
3743 llvm::APSInt Result;
3744 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3745 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3746 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003747}
3748
3749/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003750/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3751/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003752static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003753CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3754 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3755 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003756 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003757 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003758 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003759 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003760 // Found 'collapse' clause - calculate collapse number.
3761 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003762 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003763 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003764 }
3765 if (OrderedLoopCountExpr) {
3766 // Found 'ordered' clause - calculate collapse number.
3767 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003768 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3769 if (Result.getLimitedValue() < NestedLoopCount) {
3770 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3771 diag::err_omp_wrong_ordered_loop_count)
3772 << OrderedLoopCountExpr->getSourceRange();
3773 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3774 diag::note_collapse_loop_count)
3775 << CollapseLoopCountExpr->getSourceRange();
3776 }
3777 NestedLoopCount = Result.getLimitedValue();
3778 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003779 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003780 // This is helper routine for loop directives (e.g., 'for', 'simd',
3781 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003782 SmallVector<LoopIterationSpace, 4> IterSpaces;
3783 IterSpaces.resize(NestedLoopCount);
3784 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003785 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003786 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003787 NestedLoopCount, CollapseLoopCountExpr,
3788 OrderedLoopCountExpr, VarsWithImplicitDSA,
3789 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003790 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003791 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003792 // OpenMP [2.8.1, simd construct, Restrictions]
3793 // All loops associated with the construct must be perfectly nested; that
3794 // is, there must be no intervening code nor any OpenMP directive between
3795 // any two loops.
3796 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003797 }
3798
Alexander Musmana5f070a2014-10-01 06:03:56 +00003799 Built.clear(/* size */ NestedLoopCount);
3800
3801 if (SemaRef.CurContext->isDependentContext())
3802 return NestedLoopCount;
3803
3804 // An example of what is generated for the following code:
3805 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003806 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003807 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003808 // for (k = 0; k < NK; ++k)
3809 // for (j = J0; j < NJ; j+=2) {
3810 // <loop body>
3811 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003812 //
3813 // We generate the code below.
3814 // Note: the loop body may be outlined in CodeGen.
3815 // Note: some counters may be C++ classes, operator- is used to find number of
3816 // iterations and operator+= to calculate counter value.
3817 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3818 // or i64 is currently supported).
3819 //
3820 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3821 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3822 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3823 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3824 // // similar updates for vars in clauses (e.g. 'linear')
3825 // <loop body (using local i and j)>
3826 // }
3827 // i = NI; // assign final values of counters
3828 // j = NJ;
3829 //
3830
3831 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3832 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003833 // Precondition tests if there is at least one iteration (all conditions are
3834 // true).
3835 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003836 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003837 ExprResult LastIteration32 = WidenIterationCount(
3838 32 /* Bits */, SemaRef.PerformImplicitConversion(
3839 N0->IgnoreImpCasts(), N0->getType(),
3840 Sema::AA_Converting, /*AllowExplicit=*/true)
3841 .get(),
3842 SemaRef);
3843 ExprResult LastIteration64 = WidenIterationCount(
3844 64 /* Bits */, SemaRef.PerformImplicitConversion(
3845 N0->IgnoreImpCasts(), N0->getType(),
3846 Sema::AA_Converting, /*AllowExplicit=*/true)
3847 .get(),
3848 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003849
3850 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3851 return NestedLoopCount;
3852
3853 auto &C = SemaRef.Context;
3854 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3855
3856 Scope *CurScope = DSA.getCurScope();
3857 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003858 if (PreCond.isUsable()) {
3859 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3860 PreCond.get(), IterSpaces[Cnt].PreCond);
3861 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003862 auto N = IterSpaces[Cnt].NumIterations;
3863 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3864 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003865 LastIteration32 = SemaRef.BuildBinOp(
3866 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3867 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3868 Sema::AA_Converting,
3869 /*AllowExplicit=*/true)
3870 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003871 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003872 LastIteration64 = SemaRef.BuildBinOp(
3873 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3874 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3875 Sema::AA_Converting,
3876 /*AllowExplicit=*/true)
3877 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003878 }
3879
3880 // Choose either the 32-bit or 64-bit version.
3881 ExprResult LastIteration = LastIteration64;
3882 if (LastIteration32.isUsable() &&
3883 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3884 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3885 FitsInto(
3886 32 /* Bits */,
3887 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3888 LastIteration64.get(), SemaRef)))
3889 LastIteration = LastIteration32;
3890
3891 if (!LastIteration.isUsable())
3892 return 0;
3893
3894 // Save the number of iterations.
3895 ExprResult NumIterations = LastIteration;
3896 {
3897 LastIteration = SemaRef.BuildBinOp(
3898 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3899 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3900 if (!LastIteration.isUsable())
3901 return 0;
3902 }
3903
3904 // Calculate the last iteration number beforehand instead of doing this on
3905 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3906 llvm::APSInt Result;
3907 bool IsConstant =
3908 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3909 ExprResult CalcLastIteration;
3910 if (!IsConstant) {
3911 SourceLocation SaveLoc;
3912 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003913 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003914 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003915 ExprResult SaveRef = buildDeclRefExpr(
3916 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003917 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3918 SaveRef.get(), LastIteration.get());
3919 LastIteration = SaveRef;
3920
3921 // Prepare SaveRef + 1.
3922 NumIterations = SemaRef.BuildBinOp(
3923 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3924 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3925 if (!NumIterations.isUsable())
3926 return 0;
3927 }
3928
3929 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3930
Alexander Musmanc6388682014-12-15 07:07:06 +00003931 QualType VType = LastIteration.get()->getType();
3932 // Build variables passed into runtime, nesessary for worksharing directives.
3933 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003934 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3935 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003936 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003937 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3938 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003939 SemaRef.AddInitializerToDecl(
3940 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3941 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3942
3943 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003944 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3945 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003946 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3947 /*DirectInit*/ false,
3948 /*TypeMayContainAuto*/ false);
3949
3950 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3951 // This will be used to implement clause 'lastprivate'.
3952 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003953 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3954 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003955 SemaRef.AddInitializerToDecl(
3956 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3957 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3958
3959 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003960 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3961 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003962 SemaRef.AddInitializerToDecl(
3963 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3964 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3965
3966 // Build expression: UB = min(UB, LastIteration)
3967 // It is nesessary for CodeGen of directives with static scheduling.
3968 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3969 UB.get(), LastIteration.get());
3970 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3971 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3972 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3973 CondOp.get());
3974 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3975 }
3976
3977 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003978 ExprResult IV;
3979 ExprResult Init;
3980 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003981 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3982 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003983 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003984 isOpenMPTaskLoopDirective(DKind) ||
3985 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003986 ? LB.get()
3987 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3988 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3989 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003990 }
3991
Alexander Musmanc6388682014-12-15 07:07:06 +00003992 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003993 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003994 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003995 (isOpenMPWorksharingDirective(DKind) ||
3996 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003997 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3998 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3999 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004000
4001 // Loop increment (IV = IV + 1)
4002 SourceLocation IncLoc;
4003 ExprResult Inc =
4004 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4005 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4006 if (!Inc.isUsable())
4007 return 0;
4008 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004009 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4010 if (!Inc.isUsable())
4011 return 0;
4012
4013 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4014 // Used for directives with static scheduling.
4015 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004016 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4017 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004018 // LB + ST
4019 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4020 if (!NextLB.isUsable())
4021 return 0;
4022 // LB = LB + ST
4023 NextLB =
4024 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4025 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4026 if (!NextLB.isUsable())
4027 return 0;
4028 // UB + ST
4029 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4030 if (!NextUB.isUsable())
4031 return 0;
4032 // UB = UB + ST
4033 NextUB =
4034 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4035 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4036 if (!NextUB.isUsable())
4037 return 0;
4038 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004039
4040 // Build updates and final values of the loop counters.
4041 bool HasErrors = false;
4042 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004043 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004044 Built.Updates.resize(NestedLoopCount);
4045 Built.Finals.resize(NestedLoopCount);
4046 {
4047 ExprResult Div;
4048 // Go from inner nested loop to outer.
4049 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4050 LoopIterationSpace &IS = IterSpaces[Cnt];
4051 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4052 // Build: Iter = (IV / Div) % IS.NumIters
4053 // where Div is product of previous iterations' IS.NumIters.
4054 ExprResult Iter;
4055 if (Div.isUsable()) {
4056 Iter =
4057 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4058 } else {
4059 Iter = IV;
4060 assert((Cnt == (int)NestedLoopCount - 1) &&
4061 "unusable div expected on first iteration only");
4062 }
4063
4064 if (Cnt != 0 && Iter.isUsable())
4065 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4066 IS.NumIterations);
4067 if (!Iter.isUsable()) {
4068 HasErrors = true;
4069 break;
4070 }
4071
Alexey Bataev39f915b82015-05-08 10:41:21 +00004072 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4073 auto *CounterVar = buildDeclRefExpr(
4074 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4075 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4076 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004077 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4078 IS.CounterInit);
4079 if (!Init.isUsable()) {
4080 HasErrors = true;
4081 break;
4082 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004083 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004084 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004085 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4086 if (!Update.isUsable()) {
4087 HasErrors = true;
4088 break;
4089 }
4090
4091 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4092 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004093 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094 IS.NumIterations, IS.CounterStep, IS.Subtract);
4095 if (!Final.isUsable()) {
4096 HasErrors = true;
4097 break;
4098 }
4099
4100 // Build Div for the next iteration: Div <- Div * IS.NumIters
4101 if (Cnt != 0) {
4102 if (Div.isUnset())
4103 Div = IS.NumIterations;
4104 else
4105 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4106 IS.NumIterations);
4107
4108 // Add parentheses (for debugging purposes only).
4109 if (Div.isUsable())
4110 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4111 if (!Div.isUsable()) {
4112 HasErrors = true;
4113 break;
4114 }
4115 }
4116 if (!Update.isUsable() || !Final.isUsable()) {
4117 HasErrors = true;
4118 break;
4119 }
4120 // Save results
4121 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004122 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004123 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004124 Built.Updates[Cnt] = Update.get();
4125 Built.Finals[Cnt] = Final.get();
4126 }
4127 }
4128
4129 if (HasErrors)
4130 return 0;
4131
4132 // Save results
4133 Built.IterationVarRef = IV.get();
4134 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004135 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004136 Built.CalcLastIteration =
4137 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004138 Built.PreCond = PreCond.get();
4139 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004140 Built.Init = Init.get();
4141 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004142 Built.LB = LB.get();
4143 Built.UB = UB.get();
4144 Built.IL = IL.get();
4145 Built.ST = ST.get();
4146 Built.EUB = EUB.get();
4147 Built.NLB = NextLB.get();
4148 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004149
Alexey Bataevabfc0692014-06-25 06:52:00 +00004150 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004151}
4152
Alexey Bataev10e775f2015-07-30 11:36:16 +00004153static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004154 auto CollapseClauses =
4155 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4156 if (CollapseClauses.begin() != CollapseClauses.end())
4157 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004158 return nullptr;
4159}
4160
Alexey Bataev10e775f2015-07-30 11:36:16 +00004161static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004162 auto OrderedClauses =
4163 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4164 if (OrderedClauses.begin() != OrderedClauses.end())
4165 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004166 return nullptr;
4167}
4168
Alexey Bataev66b15b52015-08-21 11:14:16 +00004169static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4170 const Expr *Safelen) {
4171 llvm::APSInt SimdlenRes, SafelenRes;
4172 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4173 Simdlen->isInstantiationDependent() ||
4174 Simdlen->containsUnexpandedParameterPack())
4175 return false;
4176 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4177 Safelen->isInstantiationDependent() ||
4178 Safelen->containsUnexpandedParameterPack())
4179 return false;
4180 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4181 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4182 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4183 // If both simdlen and safelen clauses are specified, the value of the simdlen
4184 // parameter must be less than or equal to the value of the safelen parameter.
4185 if (SimdlenRes > SafelenRes) {
4186 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4187 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4188 return true;
4189 }
4190 return false;
4191}
4192
Alexey Bataev4acb8592014-07-07 13:01:15 +00004193StmtResult Sema::ActOnOpenMPSimdDirective(
4194 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4195 SourceLocation EndLoc,
4196 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004197 if (!AStmt)
4198 return StmtError();
4199
4200 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004201 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004202 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4203 // define the nested loops number.
4204 unsigned NestedLoopCount = CheckOpenMPLoop(
4205 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4206 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004207 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004208 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004209
Alexander Musmana5f070a2014-10-01 06:03:56 +00004210 assert((CurContext->isDependentContext() || B.builtAll()) &&
4211 "omp simd loop exprs were not built");
4212
Alexander Musman3276a272015-03-21 10:12:56 +00004213 if (!CurContext->isDependentContext()) {
4214 // Finalize the clauses that need pre-built expressions for CodeGen.
4215 for (auto C : Clauses) {
4216 if (auto LC = dyn_cast<OMPLinearClause>(C))
4217 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4218 B.NumIterations, *this, CurScope))
4219 return StmtError();
4220 }
4221 }
4222
Alexey Bataev66b15b52015-08-21 11:14:16 +00004223 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4224 // If both simdlen and safelen clauses are specified, the value of the simdlen
4225 // parameter must be less than or equal to the value of the safelen parameter.
4226 OMPSafelenClause *Safelen = nullptr;
4227 OMPSimdlenClause *Simdlen = nullptr;
4228 for (auto *Clause : Clauses) {
4229 if (Clause->getClauseKind() == OMPC_safelen)
4230 Safelen = cast<OMPSafelenClause>(Clause);
4231 else if (Clause->getClauseKind() == OMPC_simdlen)
4232 Simdlen = cast<OMPSimdlenClause>(Clause);
4233 if (Safelen && Simdlen)
4234 break;
4235 }
4236 if (Simdlen && Safelen &&
4237 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4238 Safelen->getSafelen()))
4239 return StmtError();
4240
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004241 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004242 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4243 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004244}
4245
Alexey Bataev4acb8592014-07-07 13:01:15 +00004246StmtResult Sema::ActOnOpenMPForDirective(
4247 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4248 SourceLocation EndLoc,
4249 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004250 if (!AStmt)
4251 return StmtError();
4252
4253 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004254 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004255 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4256 // define the nested loops number.
4257 unsigned NestedLoopCount = CheckOpenMPLoop(
4258 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4259 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004260 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004261 return StmtError();
4262
Alexander Musmana5f070a2014-10-01 06:03:56 +00004263 assert((CurContext->isDependentContext() || B.builtAll()) &&
4264 "omp for loop exprs were not built");
4265
Alexey Bataev54acd402015-08-04 11:18:19 +00004266 if (!CurContext->isDependentContext()) {
4267 // Finalize the clauses that need pre-built expressions for CodeGen.
4268 for (auto C : Clauses) {
4269 if (auto LC = dyn_cast<OMPLinearClause>(C))
4270 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4271 B.NumIterations, *this, CurScope))
4272 return StmtError();
4273 }
4274 }
4275
Alexey Bataevf29276e2014-06-18 04:14:57 +00004276 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004277 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004278 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004279}
4280
Alexander Musmanf82886e2014-09-18 05:12:34 +00004281StmtResult Sema::ActOnOpenMPForSimdDirective(
4282 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4283 SourceLocation EndLoc,
4284 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004285 if (!AStmt)
4286 return StmtError();
4287
4288 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004289 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004290 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4291 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004292 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004293 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4294 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4295 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004296 if (NestedLoopCount == 0)
4297 return StmtError();
4298
Alexander Musmanc6388682014-12-15 07:07:06 +00004299 assert((CurContext->isDependentContext() || B.builtAll()) &&
4300 "omp for simd loop exprs were not built");
4301
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004302 if (!CurContext->isDependentContext()) {
4303 // Finalize the clauses that need pre-built expressions for CodeGen.
4304 for (auto C : Clauses) {
4305 if (auto LC = dyn_cast<OMPLinearClause>(C))
4306 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4307 B.NumIterations, *this, CurScope))
4308 return StmtError();
4309 }
4310 }
4311
Alexey Bataev66b15b52015-08-21 11:14:16 +00004312 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4313 // If both simdlen and safelen clauses are specified, the value of the simdlen
4314 // parameter must be less than or equal to the value of the safelen parameter.
4315 OMPSafelenClause *Safelen = nullptr;
4316 OMPSimdlenClause *Simdlen = nullptr;
4317 for (auto *Clause : Clauses) {
4318 if (Clause->getClauseKind() == OMPC_safelen)
4319 Safelen = cast<OMPSafelenClause>(Clause);
4320 else if (Clause->getClauseKind() == OMPC_simdlen)
4321 Simdlen = cast<OMPSimdlenClause>(Clause);
4322 if (Safelen && Simdlen)
4323 break;
4324 }
4325 if (Simdlen && Safelen &&
4326 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4327 Safelen->getSafelen()))
4328 return StmtError();
4329
Alexander Musmanf82886e2014-09-18 05:12:34 +00004330 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004331 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4332 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004333}
4334
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004335StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4336 Stmt *AStmt,
4337 SourceLocation StartLoc,
4338 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004339 if (!AStmt)
4340 return StmtError();
4341
4342 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004343 auto BaseStmt = AStmt;
4344 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4345 BaseStmt = CS->getCapturedStmt();
4346 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4347 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004348 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004349 return StmtError();
4350 // All associated statements must be '#pragma omp section' except for
4351 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004352 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004353 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4354 if (SectionStmt)
4355 Diag(SectionStmt->getLocStart(),
4356 diag::err_omp_sections_substmt_not_section);
4357 return StmtError();
4358 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004359 cast<OMPSectionDirective>(SectionStmt)
4360 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004361 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004362 } else {
4363 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4364 return StmtError();
4365 }
4366
4367 getCurFunction()->setHasBranchProtectedScope();
4368
Alexey Bataev25e5b442015-09-15 12:52:43 +00004369 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4370 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004371}
4372
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004373StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4374 SourceLocation StartLoc,
4375 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004376 if (!AStmt)
4377 return StmtError();
4378
4379 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004380
4381 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004382 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004383
Alexey Bataev25e5b442015-09-15 12:52:43 +00004384 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4385 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004386}
4387
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004388StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4389 Stmt *AStmt,
4390 SourceLocation StartLoc,
4391 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004392 if (!AStmt)
4393 return StmtError();
4394
4395 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004396
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004397 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004398
Alexey Bataev3255bf32015-01-19 05:20:46 +00004399 // OpenMP [2.7.3, single Construct, Restrictions]
4400 // The copyprivate clause must not be used with the nowait clause.
4401 OMPClause *Nowait = nullptr;
4402 OMPClause *Copyprivate = nullptr;
4403 for (auto *Clause : Clauses) {
4404 if (Clause->getClauseKind() == OMPC_nowait)
4405 Nowait = Clause;
4406 else if (Clause->getClauseKind() == OMPC_copyprivate)
4407 Copyprivate = Clause;
4408 if (Copyprivate && Nowait) {
4409 Diag(Copyprivate->getLocStart(),
4410 diag::err_omp_single_copyprivate_with_nowait);
4411 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4412 return StmtError();
4413 }
4414 }
4415
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004416 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4417}
4418
Alexander Musman80c22892014-07-17 08:54:58 +00004419StmtResult Sema::ActOnOpenMPMasterDirective(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");
Alexander Musman80c22892014-07-17 08:54:58 +00004426
4427 getCurFunction()->setHasBranchProtectedScope();
4428
4429 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4430}
4431
Alexey Bataev28c75412015-12-15 08:19:24 +00004432StmtResult Sema::ActOnOpenMPCriticalDirective(
4433 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4434 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004435 if (!AStmt)
4436 return StmtError();
4437
4438 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004439
Alexey Bataev28c75412015-12-15 08:19:24 +00004440 bool ErrorFound = false;
4441 llvm::APSInt Hint;
4442 SourceLocation HintLoc;
4443 bool DependentHint = false;
4444 for (auto *C : Clauses) {
4445 if (C->getClauseKind() == OMPC_hint) {
4446 if (!DirName.getName()) {
4447 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4448 ErrorFound = true;
4449 }
4450 Expr *E = cast<OMPHintClause>(C)->getHint();
4451 if (E->isTypeDependent() || E->isValueDependent() ||
4452 E->isInstantiationDependent())
4453 DependentHint = true;
4454 else {
4455 Hint = E->EvaluateKnownConstInt(Context);
4456 HintLoc = C->getLocStart();
4457 }
4458 }
4459 }
4460 if (ErrorFound)
4461 return StmtError();
4462 auto Pair = DSAStack->getCriticalWithHint(DirName);
4463 if (Pair.first && DirName.getName() && !DependentHint) {
4464 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4465 Diag(StartLoc, diag::err_omp_critical_with_hint);
4466 if (HintLoc.isValid()) {
4467 Diag(HintLoc, diag::note_omp_critical_hint_here)
4468 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4469 } else
4470 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4471 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4472 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4473 << 1
4474 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4475 /*Radix=*/10, /*Signed=*/false);
4476 } else
4477 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4478 }
4479 }
4480
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004481 getCurFunction()->setHasBranchProtectedScope();
4482
Alexey Bataev28c75412015-12-15 08:19:24 +00004483 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4484 Clauses, AStmt);
4485 if (!Pair.first && DirName.getName() && !DependentHint)
4486 DSAStack->addCriticalWithHint(Dir, Hint);
4487 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004488}
4489
Alexey Bataev4acb8592014-07-07 13:01:15 +00004490StmtResult Sema::ActOnOpenMPParallelForDirective(
4491 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4492 SourceLocation EndLoc,
4493 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004494 if (!AStmt)
4495 return StmtError();
4496
Alexey Bataev4acb8592014-07-07 13:01:15 +00004497 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4498 // 1.2.2 OpenMP Language Terminology
4499 // Structured block - An executable statement with a single entry at the
4500 // top and a single exit at the bottom.
4501 // The point of exit cannot be a branch out of the structured block.
4502 // longjmp() and throw() must not violate the entry/exit criteria.
4503 CS->getCapturedDecl()->setNothrow();
4504
Alexander Musmanc6388682014-12-15 07:07:06 +00004505 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004506 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4507 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004508 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004509 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4510 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4511 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004512 if (NestedLoopCount == 0)
4513 return StmtError();
4514
Alexander Musmana5f070a2014-10-01 06:03:56 +00004515 assert((CurContext->isDependentContext() || B.builtAll()) &&
4516 "omp parallel for loop exprs were not built");
4517
Alexey Bataev54acd402015-08-04 11:18:19 +00004518 if (!CurContext->isDependentContext()) {
4519 // Finalize the clauses that need pre-built expressions for CodeGen.
4520 for (auto C : Clauses) {
4521 if (auto LC = dyn_cast<OMPLinearClause>(C))
4522 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4523 B.NumIterations, *this, CurScope))
4524 return StmtError();
4525 }
4526 }
4527
Alexey Bataev4acb8592014-07-07 13:01:15 +00004528 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004529 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004530 NestedLoopCount, Clauses, AStmt, B,
4531 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004532}
4533
Alexander Musmane4e893b2014-09-23 09:33:00 +00004534StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4535 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4536 SourceLocation EndLoc,
4537 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004538 if (!AStmt)
4539 return StmtError();
4540
Alexander Musmane4e893b2014-09-23 09:33:00 +00004541 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4542 // 1.2.2 OpenMP Language Terminology
4543 // Structured block - An executable statement with a single entry at the
4544 // top and a single exit at the bottom.
4545 // The point of exit cannot be a branch out of the structured block.
4546 // longjmp() and throw() must not violate the entry/exit criteria.
4547 CS->getCapturedDecl()->setNothrow();
4548
Alexander Musmanc6388682014-12-15 07:07:06 +00004549 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004550 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4551 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004552 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004553 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4554 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4555 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004556 if (NestedLoopCount == 0)
4557 return StmtError();
4558
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004559 if (!CurContext->isDependentContext()) {
4560 // Finalize the clauses that need pre-built expressions for CodeGen.
4561 for (auto C : Clauses) {
4562 if (auto LC = dyn_cast<OMPLinearClause>(C))
4563 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4564 B.NumIterations, *this, CurScope))
4565 return StmtError();
4566 }
4567 }
4568
Alexey Bataev66b15b52015-08-21 11:14:16 +00004569 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4570 // If both simdlen and safelen clauses are specified, the value of the simdlen
4571 // parameter must be less than or equal to the value of the safelen parameter.
4572 OMPSafelenClause *Safelen = nullptr;
4573 OMPSimdlenClause *Simdlen = nullptr;
4574 for (auto *Clause : Clauses) {
4575 if (Clause->getClauseKind() == OMPC_safelen)
4576 Safelen = cast<OMPSafelenClause>(Clause);
4577 else if (Clause->getClauseKind() == OMPC_simdlen)
4578 Simdlen = cast<OMPSimdlenClause>(Clause);
4579 if (Safelen && Simdlen)
4580 break;
4581 }
4582 if (Simdlen && Safelen &&
4583 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4584 Safelen->getSafelen()))
4585 return StmtError();
4586
Alexander Musmane4e893b2014-09-23 09:33:00 +00004587 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004588 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004589 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004590}
4591
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004592StmtResult
4593Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4594 Stmt *AStmt, SourceLocation StartLoc,
4595 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004596 if (!AStmt)
4597 return StmtError();
4598
4599 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004600 auto BaseStmt = AStmt;
4601 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4602 BaseStmt = CS->getCapturedStmt();
4603 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4604 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004605 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004606 return StmtError();
4607 // All associated statements must be '#pragma omp section' except for
4608 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004609 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004610 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4611 if (SectionStmt)
4612 Diag(SectionStmt->getLocStart(),
4613 diag::err_omp_parallel_sections_substmt_not_section);
4614 return StmtError();
4615 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004616 cast<OMPSectionDirective>(SectionStmt)
4617 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004618 }
4619 } else {
4620 Diag(AStmt->getLocStart(),
4621 diag::err_omp_parallel_sections_not_compound_stmt);
4622 return StmtError();
4623 }
4624
4625 getCurFunction()->setHasBranchProtectedScope();
4626
Alexey Bataev25e5b442015-09-15 12:52:43 +00004627 return OMPParallelSectionsDirective::Create(
4628 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004629}
4630
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004631StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4632 Stmt *AStmt, SourceLocation StartLoc,
4633 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004634 if (!AStmt)
4635 return StmtError();
4636
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004637 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4638 // 1.2.2 OpenMP Language Terminology
4639 // Structured block - An executable statement with a single entry at the
4640 // top and a single exit at the bottom.
4641 // The point of exit cannot be a branch out of the structured block.
4642 // longjmp() and throw() must not violate the entry/exit criteria.
4643 CS->getCapturedDecl()->setNothrow();
4644
4645 getCurFunction()->setHasBranchProtectedScope();
4646
Alexey Bataev25e5b442015-09-15 12:52:43 +00004647 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4648 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004649}
4650
Alexey Bataev68446b72014-07-18 07:47:19 +00004651StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4652 SourceLocation EndLoc) {
4653 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4654}
4655
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004656StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4657 SourceLocation EndLoc) {
4658 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4659}
4660
Alexey Bataev2df347a2014-07-18 10:17:07 +00004661StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4662 SourceLocation EndLoc) {
4663 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4664}
4665
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004666StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4667 SourceLocation StartLoc,
4668 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004669 if (!AStmt)
4670 return StmtError();
4671
4672 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004673
4674 getCurFunction()->setHasBranchProtectedScope();
4675
4676 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4677}
4678
Alexey Bataev6125da92014-07-21 11:26:11 +00004679StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4680 SourceLocation StartLoc,
4681 SourceLocation EndLoc) {
4682 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4683 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4684}
4685
Alexey Bataev346265e2015-09-25 10:37:12 +00004686StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4687 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004688 SourceLocation StartLoc,
4689 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004690 OMPClause *DependFound = nullptr;
4691 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004692 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004693 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004694 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004695 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004696 for (auto *C : Clauses) {
4697 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4698 DependFound = C;
4699 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4700 if (DependSourceClause) {
4701 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4702 << getOpenMPDirectiveName(OMPD_ordered)
4703 << getOpenMPClauseName(OMPC_depend) << 2;
4704 ErrorFound = true;
4705 } else
4706 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004707 if (DependSinkClause) {
4708 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4709 << 0;
4710 ErrorFound = true;
4711 }
4712 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4713 if (DependSourceClause) {
4714 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4715 << 1;
4716 ErrorFound = true;
4717 }
4718 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004719 }
4720 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004721 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004722 else if (C->getClauseKind() == OMPC_simd)
4723 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004724 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004725 if (!ErrorFound && !SC &&
4726 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004727 // OpenMP [2.8.1,simd Construct, Restrictions]
4728 // An ordered construct with the simd clause is the only OpenMP construct
4729 // that can appear in the simd region.
4730 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004731 ErrorFound = true;
4732 } else if (DependFound && (TC || SC)) {
4733 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4734 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4735 ErrorFound = true;
4736 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4737 Diag(DependFound->getLocStart(),
4738 diag::err_omp_ordered_directive_without_param);
4739 ErrorFound = true;
4740 } else if (TC || Clauses.empty()) {
4741 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4742 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4743 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4744 << (TC != nullptr);
4745 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4746 ErrorFound = true;
4747 }
4748 }
4749 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004750 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004751
4752 if (AStmt) {
4753 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4754
4755 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004756 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004757
4758 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004759}
4760
Alexey Bataev1d160b12015-03-13 12:27:31 +00004761namespace {
4762/// \brief Helper class for checking expression in 'omp atomic [update]'
4763/// construct.
4764class OpenMPAtomicUpdateChecker {
4765 /// \brief Error results for atomic update expressions.
4766 enum ExprAnalysisErrorCode {
4767 /// \brief A statement is not an expression statement.
4768 NotAnExpression,
4769 /// \brief Expression is not builtin binary or unary operation.
4770 NotABinaryOrUnaryExpression,
4771 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4772 NotAnUnaryIncDecExpression,
4773 /// \brief An expression is not of scalar type.
4774 NotAScalarType,
4775 /// \brief A binary operation is not an assignment operation.
4776 NotAnAssignmentOp,
4777 /// \brief RHS part of the binary operation is not a binary expression.
4778 NotABinaryExpression,
4779 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4780 /// expression.
4781 NotABinaryOperator,
4782 /// \brief RHS binary operation does not have reference to the updated LHS
4783 /// part.
4784 NotAnUpdateExpression,
4785 /// \brief No errors is found.
4786 NoError
4787 };
4788 /// \brief Reference to Sema.
4789 Sema &SemaRef;
4790 /// \brief A location for note diagnostics (when error is found).
4791 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004792 /// \brief 'x' lvalue part of the source atomic expression.
4793 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004794 /// \brief 'expr' rvalue part of the source atomic expression.
4795 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004796 /// \brief Helper expression of the form
4797 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4798 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4799 Expr *UpdateExpr;
4800 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4801 /// important for non-associative operations.
4802 bool IsXLHSInRHSPart;
4803 BinaryOperatorKind Op;
4804 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004805 /// \brief true if the source expression is a postfix unary operation, false
4806 /// if it is a prefix unary operation.
4807 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004808
4809public:
4810 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004811 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004812 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004813 /// \brief Check specified statement that it is suitable for 'atomic update'
4814 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004815 /// expression. If DiagId and NoteId == 0, then only check is performed
4816 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004817 /// \param DiagId Diagnostic which should be emitted if error is found.
4818 /// \param NoteId Diagnostic note for the main error message.
4819 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004820 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004821 /// \brief Return the 'x' lvalue part of the source atomic expression.
4822 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004823 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4824 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004825 /// \brief Return the update expression used in calculation of the updated
4826 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4827 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4828 Expr *getUpdateExpr() const { return UpdateExpr; }
4829 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4830 /// false otherwise.
4831 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4832
Alexey Bataevb78ca832015-04-01 03:33:17 +00004833 /// \brief true if the source expression is a postfix unary operation, false
4834 /// if it is a prefix unary operation.
4835 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4836
Alexey Bataev1d160b12015-03-13 12:27:31 +00004837private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004838 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4839 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004840};
4841} // namespace
4842
4843bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4844 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4845 ExprAnalysisErrorCode ErrorFound = NoError;
4846 SourceLocation ErrorLoc, NoteLoc;
4847 SourceRange ErrorRange, NoteRange;
4848 // Allowed constructs are:
4849 // x = x binop expr;
4850 // x = expr binop x;
4851 if (AtomicBinOp->getOpcode() == BO_Assign) {
4852 X = AtomicBinOp->getLHS();
4853 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4854 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4855 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4856 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4857 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004858 Op = AtomicInnerBinOp->getOpcode();
4859 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004860 auto *LHS = AtomicInnerBinOp->getLHS();
4861 auto *RHS = AtomicInnerBinOp->getRHS();
4862 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4863 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4864 /*Canonical=*/true);
4865 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4866 /*Canonical=*/true);
4867 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4868 /*Canonical=*/true);
4869 if (XId == LHSId) {
4870 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004871 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004872 } else if (XId == RHSId) {
4873 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004874 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004875 } else {
4876 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4877 ErrorRange = AtomicInnerBinOp->getSourceRange();
4878 NoteLoc = X->getExprLoc();
4879 NoteRange = X->getSourceRange();
4880 ErrorFound = NotAnUpdateExpression;
4881 }
4882 } else {
4883 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4884 ErrorRange = AtomicInnerBinOp->getSourceRange();
4885 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4886 NoteRange = SourceRange(NoteLoc, NoteLoc);
4887 ErrorFound = NotABinaryOperator;
4888 }
4889 } else {
4890 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4891 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4892 ErrorFound = NotABinaryExpression;
4893 }
4894 } else {
4895 ErrorLoc = AtomicBinOp->getExprLoc();
4896 ErrorRange = AtomicBinOp->getSourceRange();
4897 NoteLoc = AtomicBinOp->getOperatorLoc();
4898 NoteRange = SourceRange(NoteLoc, NoteLoc);
4899 ErrorFound = NotAnAssignmentOp;
4900 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004901 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004902 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4903 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4904 return true;
4905 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004906 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004907 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004908}
4909
4910bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4911 unsigned NoteId) {
4912 ExprAnalysisErrorCode ErrorFound = NoError;
4913 SourceLocation ErrorLoc, NoteLoc;
4914 SourceRange ErrorRange, NoteRange;
4915 // Allowed constructs are:
4916 // x++;
4917 // x--;
4918 // ++x;
4919 // --x;
4920 // x binop= expr;
4921 // x = x binop expr;
4922 // x = expr binop x;
4923 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4924 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4925 if (AtomicBody->getType()->isScalarType() ||
4926 AtomicBody->isInstantiationDependent()) {
4927 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4928 AtomicBody->IgnoreParenImpCasts())) {
4929 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004930 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004931 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004932 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004933 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004934 X = AtomicCompAssignOp->getLHS();
4935 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004936 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4937 AtomicBody->IgnoreParenImpCasts())) {
4938 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004939 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4940 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004941 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004942 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4943 // Check for Unary Operation
4944 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004945 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004946 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4947 OpLoc = AtomicUnaryOp->getOperatorLoc();
4948 X = AtomicUnaryOp->getSubExpr();
4949 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4950 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004951 } else {
4952 ErrorFound = NotAnUnaryIncDecExpression;
4953 ErrorLoc = AtomicUnaryOp->getExprLoc();
4954 ErrorRange = AtomicUnaryOp->getSourceRange();
4955 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4956 NoteRange = SourceRange(NoteLoc, NoteLoc);
4957 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004958 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004959 ErrorFound = NotABinaryOrUnaryExpression;
4960 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4961 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4962 }
4963 } else {
4964 ErrorFound = NotAScalarType;
4965 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4966 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4967 }
4968 } else {
4969 ErrorFound = NotAnExpression;
4970 NoteLoc = ErrorLoc = S->getLocStart();
4971 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4972 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004973 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004974 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4975 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4976 return true;
4977 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004978 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004979 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004980 // Build an update expression of form 'OpaqueValueExpr(x) binop
4981 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4982 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4983 auto *OVEX = new (SemaRef.getASTContext())
4984 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4985 auto *OVEExpr = new (SemaRef.getASTContext())
4986 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4987 auto Update =
4988 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4989 IsXLHSInRHSPart ? OVEExpr : OVEX);
4990 if (Update.isInvalid())
4991 return true;
4992 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4993 Sema::AA_Casting);
4994 if (Update.isInvalid())
4995 return true;
4996 UpdateExpr = Update.get();
4997 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004998 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004999}
5000
Alexey Bataev0162e452014-07-22 10:10:35 +00005001StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5002 Stmt *AStmt,
5003 SourceLocation StartLoc,
5004 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005005 if (!AStmt)
5006 return StmtError();
5007
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005008 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005009 // 1.2.2 OpenMP Language Terminology
5010 // Structured block - An executable statement with a single entry at the
5011 // top and a single exit at the bottom.
5012 // The point of exit cannot be a branch out of the structured block.
5013 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005014 OpenMPClauseKind AtomicKind = OMPC_unknown;
5015 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005016 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005017 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005018 C->getClauseKind() == OMPC_update ||
5019 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005020 if (AtomicKind != OMPC_unknown) {
5021 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5022 << SourceRange(C->getLocStart(), C->getLocEnd());
5023 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5024 << getOpenMPClauseName(AtomicKind);
5025 } else {
5026 AtomicKind = C->getClauseKind();
5027 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005028 }
5029 }
5030 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005031
Alexey Bataev459dec02014-07-24 06:46:57 +00005032 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005033 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5034 Body = EWC->getSubExpr();
5035
Alexey Bataev62cec442014-11-18 10:14:22 +00005036 Expr *X = nullptr;
5037 Expr *V = nullptr;
5038 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005039 Expr *UE = nullptr;
5040 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005041 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005042 // OpenMP [2.12.6, atomic Construct]
5043 // In the next expressions:
5044 // * x and v (as applicable) are both l-value expressions with scalar type.
5045 // * During the execution of an atomic region, multiple syntactic
5046 // occurrences of x must designate the same storage location.
5047 // * Neither of v and expr (as applicable) may access the storage location
5048 // designated by x.
5049 // * Neither of x and expr (as applicable) may access the storage location
5050 // designated by v.
5051 // * expr is an expression with scalar type.
5052 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5053 // * binop, binop=, ++, and -- are not overloaded operators.
5054 // * The expression x binop expr must be numerically equivalent to x binop
5055 // (expr). This requirement is satisfied if the operators in expr have
5056 // precedence greater than binop, or by using parentheses around expr or
5057 // subexpressions of expr.
5058 // * The expression expr binop x must be numerically equivalent to (expr)
5059 // binop x. This requirement is satisfied if the operators in expr have
5060 // precedence equal to or greater than binop, or by using parentheses around
5061 // expr or subexpressions of expr.
5062 // * For forms that allow multiple occurrences of x, the number of times
5063 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005064 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005065 enum {
5066 NotAnExpression,
5067 NotAnAssignmentOp,
5068 NotAScalarType,
5069 NotAnLValue,
5070 NoError
5071 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005072 SourceLocation ErrorLoc, NoteLoc;
5073 SourceRange ErrorRange, NoteRange;
5074 // If clause is read:
5075 // v = x;
5076 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5077 auto AtomicBinOp =
5078 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5079 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5080 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5081 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5082 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5083 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5084 if (!X->isLValue() || !V->isLValue()) {
5085 auto NotLValueExpr = X->isLValue() ? V : X;
5086 ErrorFound = NotAnLValue;
5087 ErrorLoc = AtomicBinOp->getExprLoc();
5088 ErrorRange = AtomicBinOp->getSourceRange();
5089 NoteLoc = NotLValueExpr->getExprLoc();
5090 NoteRange = NotLValueExpr->getSourceRange();
5091 }
5092 } else if (!X->isInstantiationDependent() ||
5093 !V->isInstantiationDependent()) {
5094 auto NotScalarExpr =
5095 (X->isInstantiationDependent() || X->getType()->isScalarType())
5096 ? V
5097 : X;
5098 ErrorFound = NotAScalarType;
5099 ErrorLoc = AtomicBinOp->getExprLoc();
5100 ErrorRange = AtomicBinOp->getSourceRange();
5101 NoteLoc = NotScalarExpr->getExprLoc();
5102 NoteRange = NotScalarExpr->getSourceRange();
5103 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005104 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005105 ErrorFound = NotAnAssignmentOp;
5106 ErrorLoc = AtomicBody->getExprLoc();
5107 ErrorRange = AtomicBody->getSourceRange();
5108 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5109 : AtomicBody->getExprLoc();
5110 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5111 : AtomicBody->getSourceRange();
5112 }
5113 } else {
5114 ErrorFound = NotAnExpression;
5115 NoteLoc = ErrorLoc = Body->getLocStart();
5116 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005117 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005118 if (ErrorFound != NoError) {
5119 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5120 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005121 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5122 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005123 return StmtError();
5124 } else if (CurContext->isDependentContext())
5125 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005126 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005127 enum {
5128 NotAnExpression,
5129 NotAnAssignmentOp,
5130 NotAScalarType,
5131 NotAnLValue,
5132 NoError
5133 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005134 SourceLocation ErrorLoc, NoteLoc;
5135 SourceRange ErrorRange, NoteRange;
5136 // If clause is write:
5137 // x = expr;
5138 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5139 auto AtomicBinOp =
5140 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5141 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005142 X = AtomicBinOp->getLHS();
5143 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005144 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5145 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5146 if (!X->isLValue()) {
5147 ErrorFound = NotAnLValue;
5148 ErrorLoc = AtomicBinOp->getExprLoc();
5149 ErrorRange = AtomicBinOp->getSourceRange();
5150 NoteLoc = X->getExprLoc();
5151 NoteRange = X->getSourceRange();
5152 }
5153 } else if (!X->isInstantiationDependent() ||
5154 !E->isInstantiationDependent()) {
5155 auto NotScalarExpr =
5156 (X->isInstantiationDependent() || X->getType()->isScalarType())
5157 ? E
5158 : X;
5159 ErrorFound = NotAScalarType;
5160 ErrorLoc = AtomicBinOp->getExprLoc();
5161 ErrorRange = AtomicBinOp->getSourceRange();
5162 NoteLoc = NotScalarExpr->getExprLoc();
5163 NoteRange = NotScalarExpr->getSourceRange();
5164 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005165 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005166 ErrorFound = NotAnAssignmentOp;
5167 ErrorLoc = AtomicBody->getExprLoc();
5168 ErrorRange = AtomicBody->getSourceRange();
5169 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5170 : AtomicBody->getExprLoc();
5171 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5172 : AtomicBody->getSourceRange();
5173 }
5174 } else {
5175 ErrorFound = NotAnExpression;
5176 NoteLoc = ErrorLoc = Body->getLocStart();
5177 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005178 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005179 if (ErrorFound != NoError) {
5180 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5181 << ErrorRange;
5182 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5183 << NoteRange;
5184 return StmtError();
5185 } else if (CurContext->isDependentContext())
5186 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005187 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005188 // If clause is update:
5189 // x++;
5190 // x--;
5191 // ++x;
5192 // --x;
5193 // x binop= expr;
5194 // x = x binop expr;
5195 // x = expr binop x;
5196 OpenMPAtomicUpdateChecker Checker(*this);
5197 if (Checker.checkStatement(
5198 Body, (AtomicKind == OMPC_update)
5199 ? diag::err_omp_atomic_update_not_expression_statement
5200 : diag::err_omp_atomic_not_expression_statement,
5201 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005202 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005203 if (!CurContext->isDependentContext()) {
5204 E = Checker.getExpr();
5205 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005206 UE = Checker.getUpdateExpr();
5207 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005208 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005209 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005210 enum {
5211 NotAnAssignmentOp,
5212 NotACompoundStatement,
5213 NotTwoSubstatements,
5214 NotASpecificExpression,
5215 NoError
5216 } ErrorFound = NoError;
5217 SourceLocation ErrorLoc, NoteLoc;
5218 SourceRange ErrorRange, NoteRange;
5219 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5220 // If clause is a capture:
5221 // v = x++;
5222 // v = x--;
5223 // v = ++x;
5224 // v = --x;
5225 // v = x binop= expr;
5226 // v = x = x binop expr;
5227 // v = x = expr binop x;
5228 auto *AtomicBinOp =
5229 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5230 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5231 V = AtomicBinOp->getLHS();
5232 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5233 OpenMPAtomicUpdateChecker Checker(*this);
5234 if (Checker.checkStatement(
5235 Body, diag::err_omp_atomic_capture_not_expression_statement,
5236 diag::note_omp_atomic_update))
5237 return StmtError();
5238 E = Checker.getExpr();
5239 X = Checker.getX();
5240 UE = Checker.getUpdateExpr();
5241 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5242 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005243 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005244 ErrorLoc = AtomicBody->getExprLoc();
5245 ErrorRange = AtomicBody->getSourceRange();
5246 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5247 : AtomicBody->getExprLoc();
5248 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5249 : AtomicBody->getSourceRange();
5250 ErrorFound = NotAnAssignmentOp;
5251 }
5252 if (ErrorFound != NoError) {
5253 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5254 << ErrorRange;
5255 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5256 return StmtError();
5257 } else if (CurContext->isDependentContext()) {
5258 UE = V = E = X = nullptr;
5259 }
5260 } else {
5261 // If clause is a capture:
5262 // { v = x; x = expr; }
5263 // { v = x; x++; }
5264 // { v = x; x--; }
5265 // { v = x; ++x; }
5266 // { v = x; --x; }
5267 // { v = x; x binop= expr; }
5268 // { v = x; x = x binop expr; }
5269 // { v = x; x = expr binop x; }
5270 // { x++; v = x; }
5271 // { x--; v = x; }
5272 // { ++x; v = x; }
5273 // { --x; v = x; }
5274 // { x binop= expr; v = x; }
5275 // { x = x binop expr; v = x; }
5276 // { x = expr binop x; v = x; }
5277 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5278 // Check that this is { expr1; expr2; }
5279 if (CS->size() == 2) {
5280 auto *First = CS->body_front();
5281 auto *Second = CS->body_back();
5282 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5283 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5284 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5285 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5286 // Need to find what subexpression is 'v' and what is 'x'.
5287 OpenMPAtomicUpdateChecker Checker(*this);
5288 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5289 BinaryOperator *BinOp = nullptr;
5290 if (IsUpdateExprFound) {
5291 BinOp = dyn_cast<BinaryOperator>(First);
5292 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5293 }
5294 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5295 // { v = x; x++; }
5296 // { v = x; x--; }
5297 // { v = x; ++x; }
5298 // { v = x; --x; }
5299 // { v = x; x binop= expr; }
5300 // { v = x; x = x binop expr; }
5301 // { v = x; x = expr binop x; }
5302 // Check that the first expression has form v = x.
5303 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5304 llvm::FoldingSetNodeID XId, PossibleXId;
5305 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5306 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5307 IsUpdateExprFound = XId == PossibleXId;
5308 if (IsUpdateExprFound) {
5309 V = BinOp->getLHS();
5310 X = Checker.getX();
5311 E = Checker.getExpr();
5312 UE = Checker.getUpdateExpr();
5313 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005314 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005315 }
5316 }
5317 if (!IsUpdateExprFound) {
5318 IsUpdateExprFound = !Checker.checkStatement(First);
5319 BinOp = nullptr;
5320 if (IsUpdateExprFound) {
5321 BinOp = dyn_cast<BinaryOperator>(Second);
5322 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5323 }
5324 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5325 // { x++; v = x; }
5326 // { x--; v = x; }
5327 // { ++x; v = x; }
5328 // { --x; v = x; }
5329 // { x binop= expr; v = x; }
5330 // { x = x binop expr; v = x; }
5331 // { x = expr binop x; v = x; }
5332 // Check that the second expression has form v = x.
5333 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5334 llvm::FoldingSetNodeID XId, PossibleXId;
5335 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5336 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5337 IsUpdateExprFound = XId == PossibleXId;
5338 if (IsUpdateExprFound) {
5339 V = BinOp->getLHS();
5340 X = Checker.getX();
5341 E = Checker.getExpr();
5342 UE = Checker.getUpdateExpr();
5343 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005344 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005345 }
5346 }
5347 }
5348 if (!IsUpdateExprFound) {
5349 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005350 auto *FirstExpr = dyn_cast<Expr>(First);
5351 auto *SecondExpr = dyn_cast<Expr>(Second);
5352 if (!FirstExpr || !SecondExpr ||
5353 !(FirstExpr->isInstantiationDependent() ||
5354 SecondExpr->isInstantiationDependent())) {
5355 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5356 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005357 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005358 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5359 : First->getLocStart();
5360 NoteRange = ErrorRange = FirstBinOp
5361 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005362 : SourceRange(ErrorLoc, ErrorLoc);
5363 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005364 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5365 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5366 ErrorFound = NotAnAssignmentOp;
5367 NoteLoc = ErrorLoc = SecondBinOp
5368 ? SecondBinOp->getOperatorLoc()
5369 : Second->getLocStart();
5370 NoteRange = ErrorRange =
5371 SecondBinOp ? SecondBinOp->getSourceRange()
5372 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005373 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005374 auto *PossibleXRHSInFirst =
5375 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5376 auto *PossibleXLHSInSecond =
5377 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5378 llvm::FoldingSetNodeID X1Id, X2Id;
5379 PossibleXRHSInFirst->Profile(X1Id, Context,
5380 /*Canonical=*/true);
5381 PossibleXLHSInSecond->Profile(X2Id, Context,
5382 /*Canonical=*/true);
5383 IsUpdateExprFound = X1Id == X2Id;
5384 if (IsUpdateExprFound) {
5385 V = FirstBinOp->getLHS();
5386 X = SecondBinOp->getLHS();
5387 E = SecondBinOp->getRHS();
5388 UE = nullptr;
5389 IsXLHSInRHSPart = false;
5390 IsPostfixUpdate = true;
5391 } else {
5392 ErrorFound = NotASpecificExpression;
5393 ErrorLoc = FirstBinOp->getExprLoc();
5394 ErrorRange = FirstBinOp->getSourceRange();
5395 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5396 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5397 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005398 }
5399 }
5400 }
5401 }
5402 } else {
5403 NoteLoc = ErrorLoc = Body->getLocStart();
5404 NoteRange = ErrorRange =
5405 SourceRange(Body->getLocStart(), Body->getLocStart());
5406 ErrorFound = NotTwoSubstatements;
5407 }
5408 } else {
5409 NoteLoc = ErrorLoc = Body->getLocStart();
5410 NoteRange = ErrorRange =
5411 SourceRange(Body->getLocStart(), Body->getLocStart());
5412 ErrorFound = NotACompoundStatement;
5413 }
5414 if (ErrorFound != NoError) {
5415 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5416 << ErrorRange;
5417 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5418 return StmtError();
5419 } else if (CurContext->isDependentContext()) {
5420 UE = V = E = X = nullptr;
5421 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005422 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005423 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005424
5425 getCurFunction()->setHasBranchProtectedScope();
5426
Alexey Bataev62cec442014-11-18 10:14:22 +00005427 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005428 X, V, E, UE, IsXLHSInRHSPart,
5429 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005430}
5431
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005432StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5433 Stmt *AStmt,
5434 SourceLocation StartLoc,
5435 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005436 if (!AStmt)
5437 return StmtError();
5438
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005439 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5440 // 1.2.2 OpenMP Language Terminology
5441 // Structured block - An executable statement with a single entry at the
5442 // top and a single exit at the bottom.
5443 // The point of exit cannot be a branch out of the structured block.
5444 // longjmp() and throw() must not violate the entry/exit criteria.
5445 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005446
Alexey Bataev13314bf2014-10-09 04:18:56 +00005447 // OpenMP [2.16, Nesting of Regions]
5448 // If specified, a teams construct must be contained within a target
5449 // construct. That target construct must contain no statements or directives
5450 // outside of the teams construct.
5451 if (DSAStack->hasInnerTeamsRegion()) {
5452 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5453 bool OMPTeamsFound = true;
5454 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5455 auto I = CS->body_begin();
5456 while (I != CS->body_end()) {
5457 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5458 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5459 OMPTeamsFound = false;
5460 break;
5461 }
5462 ++I;
5463 }
5464 assert(I != CS->body_end() && "Not found statement");
5465 S = *I;
5466 }
5467 if (!OMPTeamsFound) {
5468 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5469 Diag(DSAStack->getInnerTeamsRegionLoc(),
5470 diag::note_omp_nested_teams_construct_here);
5471 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5472 << isa<OMPExecutableDirective>(S);
5473 return StmtError();
5474 }
5475 }
5476
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005477 getCurFunction()->setHasBranchProtectedScope();
5478
5479 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5480}
5481
Samuel Antaodf67fc42016-01-19 19:15:56 +00005482/// \brief Check for existence of a map clause in the list of clauses.
5483static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5484 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5485 I != E; ++I) {
5486 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5487 return true;
5488 }
5489 }
5490
5491 return false;
5492}
5493
Michael Wong65f367f2015-07-21 13:44:28 +00005494StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5495 Stmt *AStmt,
5496 SourceLocation StartLoc,
5497 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005498 if (!AStmt)
5499 return StmtError();
5500
5501 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5502
Michael Wong65f367f2015-07-21 13:44:28 +00005503 getCurFunction()->setHasBranchProtectedScope();
5504
5505 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5506 AStmt);
5507}
5508
Samuel Antaodf67fc42016-01-19 19:15:56 +00005509StmtResult
5510Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5511 SourceLocation StartLoc,
5512 SourceLocation EndLoc) {
5513 // OpenMP [2.10.2, Restrictions, p. 99]
5514 // At least one map clause must appear on the directive.
5515 if (!HasMapClause(Clauses)) {
5516 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5517 << getOpenMPDirectiveName(OMPD_target_enter_data);
5518 return StmtError();
5519 }
5520
5521 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5522 Clauses);
5523}
5524
Alexey Bataev13314bf2014-10-09 04:18:56 +00005525StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5526 Stmt *AStmt, SourceLocation StartLoc,
5527 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005528 if (!AStmt)
5529 return StmtError();
5530
Alexey Bataev13314bf2014-10-09 04:18:56 +00005531 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5532 // 1.2.2 OpenMP Language Terminology
5533 // Structured block - An executable statement with a single entry at the
5534 // top and a single exit at the bottom.
5535 // The point of exit cannot be a branch out of the structured block.
5536 // longjmp() and throw() must not violate the entry/exit criteria.
5537 CS->getCapturedDecl()->setNothrow();
5538
5539 getCurFunction()->setHasBranchProtectedScope();
5540
5541 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5542}
5543
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005544StmtResult
5545Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5546 SourceLocation EndLoc,
5547 OpenMPDirectiveKind CancelRegion) {
5548 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5549 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5550 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5551 << getOpenMPDirectiveName(CancelRegion);
5552 return StmtError();
5553 }
5554 if (DSAStack->isParentNowaitRegion()) {
5555 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5556 return StmtError();
5557 }
5558 if (DSAStack->isParentOrderedRegion()) {
5559 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5560 return StmtError();
5561 }
5562 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5563 CancelRegion);
5564}
5565
Alexey Bataev87933c72015-09-18 08:07:34 +00005566StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5567 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005568 SourceLocation EndLoc,
5569 OpenMPDirectiveKind CancelRegion) {
5570 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5571 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5572 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5573 << getOpenMPDirectiveName(CancelRegion);
5574 return StmtError();
5575 }
5576 if (DSAStack->isParentNowaitRegion()) {
5577 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5578 return StmtError();
5579 }
5580 if (DSAStack->isParentOrderedRegion()) {
5581 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5582 return StmtError();
5583 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005584 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005585 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5586 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005587}
5588
Alexey Bataev382967a2015-12-08 12:06:20 +00005589static bool checkGrainsizeNumTasksClauses(Sema &S,
5590 ArrayRef<OMPClause *> Clauses) {
5591 OMPClause *PrevClause = nullptr;
5592 bool ErrorFound = false;
5593 for (auto *C : Clauses) {
5594 if (C->getClauseKind() == OMPC_grainsize ||
5595 C->getClauseKind() == OMPC_num_tasks) {
5596 if (!PrevClause)
5597 PrevClause = C;
5598 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5599 S.Diag(C->getLocStart(),
5600 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5601 << getOpenMPClauseName(C->getClauseKind())
5602 << getOpenMPClauseName(PrevClause->getClauseKind());
5603 S.Diag(PrevClause->getLocStart(),
5604 diag::note_omp_previous_grainsize_num_tasks)
5605 << getOpenMPClauseName(PrevClause->getClauseKind());
5606 ErrorFound = true;
5607 }
5608 }
5609 }
5610 return ErrorFound;
5611}
5612
Alexey Bataev49f6e782015-12-01 04:18:41 +00005613StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5614 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5615 SourceLocation EndLoc,
5616 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5617 if (!AStmt)
5618 return StmtError();
5619
5620 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5621 OMPLoopDirective::HelperExprs B;
5622 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5623 // define the nested loops number.
5624 unsigned NestedLoopCount =
5625 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005626 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005627 VarsWithImplicitDSA, B);
5628 if (NestedLoopCount == 0)
5629 return StmtError();
5630
5631 assert((CurContext->isDependentContext() || B.builtAll()) &&
5632 "omp for loop exprs were not built");
5633
Alexey Bataev382967a2015-12-08 12:06:20 +00005634 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5635 // The grainsize clause and num_tasks clause are mutually exclusive and may
5636 // not appear on the same taskloop directive.
5637 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5638 return StmtError();
5639
Alexey Bataev49f6e782015-12-01 04:18:41 +00005640 getCurFunction()->setHasBranchProtectedScope();
5641 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5642 NestedLoopCount, Clauses, AStmt, B);
5643}
5644
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005645StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5646 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5647 SourceLocation EndLoc,
5648 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5649 if (!AStmt)
5650 return StmtError();
5651
5652 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5653 OMPLoopDirective::HelperExprs B;
5654 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5655 // define the nested loops number.
5656 unsigned NestedLoopCount =
5657 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5658 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5659 VarsWithImplicitDSA, B);
5660 if (NestedLoopCount == 0)
5661 return StmtError();
5662
5663 assert((CurContext->isDependentContext() || B.builtAll()) &&
5664 "omp for loop exprs were not built");
5665
Alexey Bataev382967a2015-12-08 12:06:20 +00005666 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5667 // The grainsize clause and num_tasks clause are mutually exclusive and may
5668 // not appear on the same taskloop directive.
5669 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5670 return StmtError();
5671
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005672 getCurFunction()->setHasBranchProtectedScope();
5673 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5674 NestedLoopCount, Clauses, AStmt, B);
5675}
5676
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005677StmtResult Sema::ActOnOpenMPDistributeDirective(
5678 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5679 SourceLocation EndLoc,
5680 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5681 if (!AStmt)
5682 return StmtError();
5683
5684 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5685 OMPLoopDirective::HelperExprs B;
5686 // In presence of clause 'collapse' with number of loops, it will
5687 // define the nested loops number.
5688 unsigned NestedLoopCount =
5689 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5690 nullptr /*ordered not a clause on distribute*/, AStmt,
5691 *this, *DSAStack, VarsWithImplicitDSA, B);
5692 if (NestedLoopCount == 0)
5693 return StmtError();
5694
5695 assert((CurContext->isDependentContext() || B.builtAll()) &&
5696 "omp for loop exprs were not built");
5697
5698 getCurFunction()->setHasBranchProtectedScope();
5699 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5700 NestedLoopCount, Clauses, AStmt, B);
5701}
5702
Alexey Bataeved09d242014-05-28 05:53:51 +00005703OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005704 SourceLocation StartLoc,
5705 SourceLocation LParenLoc,
5706 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005707 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005708 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005709 case OMPC_final:
5710 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5711 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005712 case OMPC_num_threads:
5713 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5714 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005715 case OMPC_safelen:
5716 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5717 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005718 case OMPC_simdlen:
5719 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5720 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005721 case OMPC_collapse:
5722 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5723 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005724 case OMPC_ordered:
5725 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5726 break;
Michael Wonge710d542015-08-07 16:16:36 +00005727 case OMPC_device:
5728 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5729 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005730 case OMPC_num_teams:
5731 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5732 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005733 case OMPC_thread_limit:
5734 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5735 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005736 case OMPC_priority:
5737 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5738 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005739 case OMPC_grainsize:
5740 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5741 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005742 case OMPC_num_tasks:
5743 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5744 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005745 case OMPC_hint:
5746 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5747 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005748 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005749 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005750 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005751 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005752 case OMPC_private:
5753 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005754 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005755 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005756 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005757 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005758 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005759 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005760 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005761 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005762 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005763 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005764 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005765 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005766 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005767 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005768 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005769 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005770 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005771 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005772 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005773 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005774 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005775 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00005776 case OMPC_dist_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005777 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005778 llvm_unreachable("Clause is not allowed.");
5779 }
5780 return Res;
5781}
5782
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005783OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5784 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005785 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005786 SourceLocation NameModifierLoc,
5787 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005788 SourceLocation EndLoc) {
5789 Expr *ValExpr = Condition;
5790 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5791 !Condition->isInstantiationDependent() &&
5792 !Condition->containsUnexpandedParameterPack()) {
5793 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005794 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005795 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005796 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005797
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005798 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005799 }
5800
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005801 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5802 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005803}
5804
Alexey Bataev3778b602014-07-17 07:32:53 +00005805OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5806 SourceLocation StartLoc,
5807 SourceLocation LParenLoc,
5808 SourceLocation EndLoc) {
5809 Expr *ValExpr = Condition;
5810 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5811 !Condition->isInstantiationDependent() &&
5812 !Condition->containsUnexpandedParameterPack()) {
5813 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5814 Condition->getExprLoc(), Condition);
5815 if (Val.isInvalid())
5816 return nullptr;
5817
5818 ValExpr = Val.get();
5819 }
5820
5821 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5822}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005823ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5824 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005825 if (!Op)
5826 return ExprError();
5827
5828 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5829 public:
5830 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005831 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005832 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5833 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005834 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5835 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005836 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5837 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005838 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5839 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005840 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5841 QualType T,
5842 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005843 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5844 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005845 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5846 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005847 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005848 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005849 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005850 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5851 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005852 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5853 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005854 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5855 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005856 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005857 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005858 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005859 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5860 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005861 llvm_unreachable("conversion functions are permitted");
5862 }
5863 } ConvertDiagnoser;
5864 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5865}
5866
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005867static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005868 OpenMPClauseKind CKind,
5869 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005870 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5871 !ValExpr->isInstantiationDependent()) {
5872 SourceLocation Loc = ValExpr->getExprLoc();
5873 ExprResult Value =
5874 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5875 if (Value.isInvalid())
5876 return false;
5877
5878 ValExpr = Value.get();
5879 // The expression must evaluate to a non-negative integer value.
5880 llvm::APSInt Result;
5881 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005882 Result.isSigned() &&
5883 !((!StrictlyPositive && Result.isNonNegative()) ||
5884 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005885 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005886 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5887 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005888 return false;
5889 }
5890 }
5891 return true;
5892}
5893
Alexey Bataev568a8332014-03-06 06:15:19 +00005894OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5895 SourceLocation StartLoc,
5896 SourceLocation LParenLoc,
5897 SourceLocation EndLoc) {
5898 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005899
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005900 // OpenMP [2.5, Restrictions]
5901 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005902 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5903 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005904 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005905
Alexey Bataeved09d242014-05-28 05:53:51 +00005906 return new (Context)
5907 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005908}
5909
Alexey Bataev62c87d22014-03-21 04:51:18 +00005910ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005911 OpenMPClauseKind CKind,
5912 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005913 if (!E)
5914 return ExprError();
5915 if (E->isValueDependent() || E->isTypeDependent() ||
5916 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005917 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005918 llvm::APSInt Result;
5919 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5920 if (ICE.isInvalid())
5921 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005922 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
5923 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005924 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005925 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5926 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005927 return ExprError();
5928 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005929 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5930 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5931 << E->getSourceRange();
5932 return ExprError();
5933 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005934 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
5935 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005936 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005937 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005938 return ICE;
5939}
5940
5941OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5942 SourceLocation LParenLoc,
5943 SourceLocation EndLoc) {
5944 // OpenMP [2.8.1, simd construct, Description]
5945 // The parameter of the safelen clause must be a constant
5946 // positive integer expression.
5947 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5948 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005949 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005950 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005951 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005952}
5953
Alexey Bataev66b15b52015-08-21 11:14:16 +00005954OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5955 SourceLocation LParenLoc,
5956 SourceLocation EndLoc) {
5957 // OpenMP [2.8.1, simd construct, Description]
5958 // The parameter of the simdlen clause must be a constant
5959 // positive integer expression.
5960 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5961 if (Simdlen.isInvalid())
5962 return nullptr;
5963 return new (Context)
5964 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5965}
5966
Alexander Musman64d33f12014-06-04 07:53:32 +00005967OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5968 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005969 SourceLocation LParenLoc,
5970 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005971 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005972 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005973 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005974 // The parameter of the collapse clause must be a constant
5975 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005976 ExprResult NumForLoopsResult =
5977 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5978 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005979 return nullptr;
5980 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005981 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005982}
5983
Alexey Bataev10e775f2015-07-30 11:36:16 +00005984OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5985 SourceLocation EndLoc,
5986 SourceLocation LParenLoc,
5987 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005988 // OpenMP [2.7.1, loop construct, Description]
5989 // OpenMP [2.8.1, simd construct, Description]
5990 // OpenMP [2.9.6, distribute construct, Description]
5991 // The parameter of the ordered clause must be a constant
5992 // positive integer expression if any.
5993 if (NumForLoops && LParenLoc.isValid()) {
5994 ExprResult NumForLoopsResult =
5995 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5996 if (NumForLoopsResult.isInvalid())
5997 return nullptr;
5998 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005999 } else
6000 NumForLoops = nullptr;
6001 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006002 return new (Context)
6003 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6004}
6005
Alexey Bataeved09d242014-05-28 05:53:51 +00006006OMPClause *Sema::ActOnOpenMPSimpleClause(
6007 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6008 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006009 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006010 switch (Kind) {
6011 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006012 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006013 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6014 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006015 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006016 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006017 Res = ActOnOpenMPProcBindClause(
6018 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6019 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006020 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006021 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006022 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006023 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006024 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006025 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006026 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006027 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006028 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006029 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006030 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006031 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006032 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006033 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006034 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006035 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006036 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006037 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006038 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006039 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006040 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006041 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006042 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006043 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006044 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006045 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006046 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006047 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006048 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006049 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006050 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006051 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006052 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006053 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006054 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006055 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006056 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006057 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006058 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006059 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006060 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006061 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006062 llvm_unreachable("Clause is not allowed.");
6063 }
6064 return Res;
6065}
6066
Alexey Bataev6402bca2015-12-28 07:25:51 +00006067static std::string
6068getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6069 ArrayRef<unsigned> Exclude = llvm::None) {
6070 std::string Values;
6071 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6072 unsigned Skipped = Exclude.size();
6073 auto S = Exclude.begin(), E = Exclude.end();
6074 for (unsigned i = First; i < Last; ++i) {
6075 if (std::find(S, E, i) != E) {
6076 --Skipped;
6077 continue;
6078 }
6079 Values += "'";
6080 Values += getOpenMPSimpleClauseTypeName(K, i);
6081 Values += "'";
6082 if (i == Bound - Skipped)
6083 Values += " or ";
6084 else if (i != Bound + 1 - Skipped)
6085 Values += ", ";
6086 }
6087 return Values;
6088}
6089
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006090OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6091 SourceLocation KindKwLoc,
6092 SourceLocation StartLoc,
6093 SourceLocation LParenLoc,
6094 SourceLocation EndLoc) {
6095 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006096 static_assert(OMPC_DEFAULT_unknown > 0,
6097 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006098 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006099 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6100 /*Last=*/OMPC_DEFAULT_unknown)
6101 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006102 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006103 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006104 switch (Kind) {
6105 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006106 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006107 break;
6108 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006109 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006110 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006111 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006112 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006113 break;
6114 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006115 return new (Context)
6116 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006117}
6118
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006119OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6120 SourceLocation KindKwLoc,
6121 SourceLocation StartLoc,
6122 SourceLocation LParenLoc,
6123 SourceLocation EndLoc) {
6124 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006125 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006126 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6127 /*Last=*/OMPC_PROC_BIND_unknown)
6128 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006129 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006130 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006131 return new (Context)
6132 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006133}
6134
Alexey Bataev56dafe82014-06-20 07:16:17 +00006135OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006136 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006137 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006138 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006139 SourceLocation EndLoc) {
6140 OMPClause *Res = nullptr;
6141 switch (Kind) {
6142 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006143 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6144 assert(Argument.size() == NumberOfElements &&
6145 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006146 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006147 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6148 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6149 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6150 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6151 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006152 break;
6153 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006154 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6155 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6156 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6157 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006158 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006159 case OMPC_dist_schedule:
6160 Res = ActOnOpenMPDistScheduleClause(
6161 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6162 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6163 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006164 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006165 case OMPC_num_threads:
6166 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006167 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006168 case OMPC_collapse:
6169 case OMPC_default:
6170 case OMPC_proc_bind:
6171 case OMPC_private:
6172 case OMPC_firstprivate:
6173 case OMPC_lastprivate:
6174 case OMPC_shared:
6175 case OMPC_reduction:
6176 case OMPC_linear:
6177 case OMPC_aligned:
6178 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006179 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006180 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006181 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006182 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006183 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006184 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006185 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006186 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006187 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006188 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006189 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006190 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006191 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006192 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006193 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006194 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006195 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006196 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006197 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006198 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006199 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006200 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006201 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006202 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006203 case OMPC_unknown:
6204 llvm_unreachable("Clause is not allowed.");
6205 }
6206 return Res;
6207}
6208
Alexey Bataev6402bca2015-12-28 07:25:51 +00006209static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6210 OpenMPScheduleClauseModifier M2,
6211 SourceLocation M1Loc, SourceLocation M2Loc) {
6212 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6213 SmallVector<unsigned, 2> Excluded;
6214 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6215 Excluded.push_back(M2);
6216 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6217 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6218 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6219 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6220 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6221 << getListOfPossibleValues(OMPC_schedule,
6222 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6223 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6224 Excluded)
6225 << getOpenMPClauseName(OMPC_schedule);
6226 return true;
6227 }
6228 return false;
6229}
6230
Alexey Bataev56dafe82014-06-20 07:16:17 +00006231OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006232 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006233 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006234 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6235 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6236 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6237 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6238 return nullptr;
6239 // OpenMP, 2.7.1, Loop Construct, Restrictions
6240 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6241 // but not both.
6242 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6243 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6244 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6245 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6246 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6247 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6248 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6249 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6250 return nullptr;
6251 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006252 if (Kind == OMPC_SCHEDULE_unknown) {
6253 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006254 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6255 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6256 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6257 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6258 Exclude);
6259 } else {
6260 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6261 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006262 }
6263 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6264 << Values << getOpenMPClauseName(OMPC_schedule);
6265 return nullptr;
6266 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006267 // OpenMP, 2.7.1, Loop Construct, Restrictions
6268 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6269 // schedule(guided).
6270 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6271 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6272 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6273 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6274 diag::err_omp_schedule_nonmonotonic_static);
6275 return nullptr;
6276 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006277 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006278 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006279 if (ChunkSize) {
6280 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6281 !ChunkSize->isInstantiationDependent() &&
6282 !ChunkSize->containsUnexpandedParameterPack()) {
6283 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6284 ExprResult Val =
6285 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6286 if (Val.isInvalid())
6287 return nullptr;
6288
6289 ValExpr = Val.get();
6290
6291 // OpenMP [2.7.1, Restrictions]
6292 // chunk_size must be a loop invariant integer expression with a positive
6293 // value.
6294 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006295 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6296 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6297 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006298 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006299 return nullptr;
6300 }
6301 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6302 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6303 ChunkSize->getType(), ".chunk.");
6304 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6305 ChunkSize->getExprLoc(),
6306 /*RefersToCapture=*/true);
6307 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006308 }
6309 }
6310 }
6311
Alexey Bataev6402bca2015-12-28 07:25:51 +00006312 return new (Context)
6313 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6314 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006315}
6316
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006317OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6318 SourceLocation StartLoc,
6319 SourceLocation EndLoc) {
6320 OMPClause *Res = nullptr;
6321 switch (Kind) {
6322 case OMPC_ordered:
6323 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6324 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006325 case OMPC_nowait:
6326 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6327 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006328 case OMPC_untied:
6329 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6330 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006331 case OMPC_mergeable:
6332 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6333 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006334 case OMPC_read:
6335 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6336 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006337 case OMPC_write:
6338 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6339 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006340 case OMPC_update:
6341 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6342 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006343 case OMPC_capture:
6344 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6345 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006346 case OMPC_seq_cst:
6347 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6348 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006349 case OMPC_threads:
6350 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6351 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006352 case OMPC_simd:
6353 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6354 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006355 case OMPC_nogroup:
6356 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6357 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006358 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006359 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006360 case OMPC_num_threads:
6361 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006362 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006363 case OMPC_collapse:
6364 case OMPC_schedule:
6365 case OMPC_private:
6366 case OMPC_firstprivate:
6367 case OMPC_lastprivate:
6368 case OMPC_shared:
6369 case OMPC_reduction:
6370 case OMPC_linear:
6371 case OMPC_aligned:
6372 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006373 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006374 case OMPC_default:
6375 case OMPC_proc_bind:
6376 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006377 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006378 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006379 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006380 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006381 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006382 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006383 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006384 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006385 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006386 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006387 case OMPC_dist_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006388 case OMPC_unknown:
6389 llvm_unreachable("Clause is not allowed.");
6390 }
6391 return Res;
6392}
6393
Alexey Bataev236070f2014-06-20 11:19:47 +00006394OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6395 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006396 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006397 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6398}
6399
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006400OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6401 SourceLocation EndLoc) {
6402 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6403}
6404
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006405OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6406 SourceLocation EndLoc) {
6407 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6408}
6409
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006410OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6411 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006412 return new (Context) OMPReadClause(StartLoc, EndLoc);
6413}
6414
Alexey Bataevdea47612014-07-23 07:46:59 +00006415OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6416 SourceLocation EndLoc) {
6417 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6418}
6419
Alexey Bataev67a4f222014-07-23 10:25:33 +00006420OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6421 SourceLocation EndLoc) {
6422 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6423}
6424
Alexey Bataev459dec02014-07-24 06:46:57 +00006425OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6426 SourceLocation EndLoc) {
6427 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6428}
6429
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006430OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6431 SourceLocation EndLoc) {
6432 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6433}
6434
Alexey Bataev346265e2015-09-25 10:37:12 +00006435OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6436 SourceLocation EndLoc) {
6437 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6438}
6439
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006440OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6441 SourceLocation EndLoc) {
6442 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6443}
6444
Alexey Bataevb825de12015-12-07 10:51:44 +00006445OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6446 SourceLocation EndLoc) {
6447 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6448}
6449
Alexey Bataevc5e02582014-06-16 07:08:35 +00006450OMPClause *Sema::ActOnOpenMPVarListClause(
6451 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6452 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6453 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006454 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006455 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6456 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006457 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006458 switch (Kind) {
6459 case OMPC_private:
6460 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6461 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006462 case OMPC_firstprivate:
6463 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6464 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006465 case OMPC_lastprivate:
6466 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6467 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006468 case OMPC_shared:
6469 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6470 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006471 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006472 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6473 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006474 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006475 case OMPC_linear:
6476 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006477 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006478 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006479 case OMPC_aligned:
6480 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6481 ColonLoc, EndLoc);
6482 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006483 case OMPC_copyin:
6484 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6485 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006486 case OMPC_copyprivate:
6487 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6488 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006489 case OMPC_flush:
6490 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6491 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006492 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006493 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6494 StartLoc, LParenLoc, EndLoc);
6495 break;
6496 case OMPC_map:
6497 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6498 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006499 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006500 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006501 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006502 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006503 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006504 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006505 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006506 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006507 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006508 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006509 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006510 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006511 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006512 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006513 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006514 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006515 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006516 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006517 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006518 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006519 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006520 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006521 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006522 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006523 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006524 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006525 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006526 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006527 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006528 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006529 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006530 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006531 llvm_unreachable("Clause is not allowed.");
6532 }
6533 return Res;
6534}
6535
6536OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6537 SourceLocation StartLoc,
6538 SourceLocation LParenLoc,
6539 SourceLocation EndLoc) {
6540 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006541 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006542 for (auto &RefExpr : VarList) {
6543 assert(RefExpr && "NULL expr in OpenMP private clause.");
6544 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006545 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006546 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006547 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006548 continue;
6549 }
6550
Alexey Bataeved09d242014-05-28 05:53:51 +00006551 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006552 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006553 // A list item is a variable name.
6554 // OpenMP [2.9.3.3, Restrictions, p.1]
6555 // A variable that is part of another variable (as an array or
6556 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006557 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006558 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006559 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006560 continue;
6561 }
6562 Decl *D = DE->getDecl();
6563 VarDecl *VD = cast<VarDecl>(D);
6564
6565 QualType Type = VD->getType();
6566 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6567 // It will be analyzed later.
6568 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006569 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006570 continue;
6571 }
6572
6573 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6574 // A variable that appears in a private clause must not have an incomplete
6575 // type or a reference type.
6576 if (RequireCompleteType(ELoc, Type,
6577 diag::err_omp_private_incomplete_type)) {
6578 continue;
6579 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006580 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006581
Alexey Bataev758e55e2013-09-06 18:03:48 +00006582 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6583 // in a Construct]
6584 // Variables with the predetermined data-sharing attributes may not be
6585 // listed in data-sharing attributes clauses, except for the cases
6586 // listed below. For these exceptions only, listing a predetermined
6587 // variable in a data-sharing attribute clause is allowed and overrides
6588 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006589 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006590 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006591 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6592 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006593 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006594 continue;
6595 }
6596
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006597 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006598 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006599 DSAStack->getCurrentDirective() == OMPD_task) {
6600 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6601 << getOpenMPClauseName(OMPC_private) << Type
6602 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6603 bool IsDecl =
6604 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6605 Diag(VD->getLocation(),
6606 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6607 << VD;
6608 continue;
6609 }
6610
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006611 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6612 // A variable of class type (or array thereof) that appears in a private
6613 // clause requires an accessible, unambiguous default constructor for the
6614 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006615 // Generate helper private variable and initialize it with the default
6616 // value. The address of the original variable is replaced by the address of
6617 // the new private variable in CodeGen. This new variable is not added to
6618 // IdResolver, so the code in the OpenMP region uses original variable for
6619 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006620 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006621 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6622 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006623 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006624 if (VDPrivate->isInvalidDecl())
6625 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006626 auto VDPrivateRefExpr = buildDeclRefExpr(
6627 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006628
Alexey Bataev758e55e2013-09-06 18:03:48 +00006629 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006630 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006631 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006632 }
6633
Alexey Bataeved09d242014-05-28 05:53:51 +00006634 if (Vars.empty())
6635 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006636
Alexey Bataev03b340a2014-10-21 03:16:40 +00006637 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6638 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006639}
6640
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006641namespace {
6642class DiagsUninitializedSeveretyRAII {
6643private:
6644 DiagnosticsEngine &Diags;
6645 SourceLocation SavedLoc;
6646 bool IsIgnored;
6647
6648public:
6649 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6650 bool IsIgnored)
6651 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6652 if (!IsIgnored) {
6653 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6654 /*Map*/ diag::Severity::Ignored, Loc);
6655 }
6656 }
6657 ~DiagsUninitializedSeveretyRAII() {
6658 if (!IsIgnored)
6659 Diags.popMappings(SavedLoc);
6660 }
6661};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006662}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006663
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006664OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6665 SourceLocation StartLoc,
6666 SourceLocation LParenLoc,
6667 SourceLocation EndLoc) {
6668 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006669 SmallVector<Expr *, 8> PrivateCopies;
6670 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006671 bool IsImplicitClause =
6672 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6673 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6674
Alexey Bataeved09d242014-05-28 05:53:51 +00006675 for (auto &RefExpr : VarList) {
6676 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6677 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006678 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006679 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006680 PrivateCopies.push_back(nullptr);
6681 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006682 continue;
6683 }
6684
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006685 SourceLocation ELoc =
6686 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006687 // OpenMP [2.1, C/C++]
6688 // A list item is a variable name.
6689 // OpenMP [2.9.3.3, Restrictions, p.1]
6690 // A variable that is part of another variable (as an array or
6691 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006692 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006693 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006694 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006695 continue;
6696 }
6697 Decl *D = DE->getDecl();
6698 VarDecl *VD = cast<VarDecl>(D);
6699
6700 QualType Type = VD->getType();
6701 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6702 // It will be analyzed later.
6703 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006704 PrivateCopies.push_back(nullptr);
6705 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006706 continue;
6707 }
6708
6709 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6710 // A variable that appears in a private clause must not have an incomplete
6711 // type or a reference type.
6712 if (RequireCompleteType(ELoc, Type,
6713 diag::err_omp_firstprivate_incomplete_type)) {
6714 continue;
6715 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006716 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006717
6718 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6719 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006720 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006721 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006722 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006723
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006724 // If an implicit firstprivate variable found it was checked already.
6725 if (!IsImplicitClause) {
6726 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006727 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006728 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6729 // A list item that specifies a given variable may not appear in more
6730 // than one clause on the same directive, except that a variable may be
6731 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006732 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006733 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006734 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006735 << getOpenMPClauseName(DVar.CKind)
6736 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006737 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006738 continue;
6739 }
6740
6741 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6742 // in a Construct]
6743 // Variables with the predetermined data-sharing attributes may not be
6744 // listed in data-sharing attributes clauses, except for the cases
6745 // listed below. For these exceptions only, listing a predetermined
6746 // variable in a data-sharing attribute clause is allowed and overrides
6747 // the variable's predetermined data-sharing attributes.
6748 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6749 // in a Construct, C/C++, p.2]
6750 // Variables with const-qualified type having no mutable member may be
6751 // listed in a firstprivate clause, even if they are static data members.
6752 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6753 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6754 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006755 << getOpenMPClauseName(DVar.CKind)
6756 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006757 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006758 continue;
6759 }
6760
Alexey Bataevf29276e2014-06-18 04:14:57 +00006761 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006762 // OpenMP [2.9.3.4, Restrictions, p.2]
6763 // A list item that is private within a parallel region must not appear
6764 // in a firstprivate clause on a worksharing construct if any of the
6765 // worksharing regions arising from the worksharing construct ever bind
6766 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006767 if (isOpenMPWorksharingDirective(CurrDir) &&
6768 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006769 DVar = DSAStack->getImplicitDSA(VD, true);
6770 if (DVar.CKind != OMPC_shared &&
6771 (isOpenMPParallelDirective(DVar.DKind) ||
6772 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006773 Diag(ELoc, diag::err_omp_required_access)
6774 << getOpenMPClauseName(OMPC_firstprivate)
6775 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006776 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006777 continue;
6778 }
6779 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006780 // OpenMP [2.9.3.4, Restrictions, p.3]
6781 // A list item that appears in a reduction clause of a parallel construct
6782 // must not appear in a firstprivate clause on a worksharing or task
6783 // construct if any of the worksharing or task regions arising from the
6784 // worksharing or task construct ever bind to any of the parallel regions
6785 // arising from the parallel construct.
6786 // OpenMP [2.9.3.4, Restrictions, p.4]
6787 // A list item that appears in a reduction clause in worksharing
6788 // construct must not appear in a firstprivate clause in a task construct
6789 // encountered during execution of any of the worksharing regions arising
6790 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006791 if (CurrDir == OMPD_task) {
6792 DVar =
6793 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6794 [](OpenMPDirectiveKind K) -> bool {
6795 return isOpenMPParallelDirective(K) ||
6796 isOpenMPWorksharingDirective(K);
6797 },
6798 false);
6799 if (DVar.CKind == OMPC_reduction &&
6800 (isOpenMPParallelDirective(DVar.DKind) ||
6801 isOpenMPWorksharingDirective(DVar.DKind))) {
6802 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6803 << getOpenMPDirectiveName(DVar.DKind);
6804 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6805 continue;
6806 }
6807 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006808
6809 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6810 // A list item that is private within a teams region must not appear in a
6811 // firstprivate clause on a distribute construct if any of the distribute
6812 // regions arising from the distribute construct ever bind to any of the
6813 // teams regions arising from the teams construct.
6814 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6815 // A list item that appears in a reduction clause of a teams construct
6816 // must not appear in a firstprivate clause on a distribute construct if
6817 // any of the distribute regions arising from the distribute construct
6818 // ever bind to any of the teams regions arising from the teams construct.
6819 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6820 // A list item may appear in a firstprivate or lastprivate clause but not
6821 // both.
6822 if (CurrDir == OMPD_distribute) {
6823 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6824 [](OpenMPDirectiveKind K) -> bool {
6825 return isOpenMPTeamsDirective(K);
6826 },
6827 false);
6828 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6829 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6830 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6831 continue;
6832 }
6833 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6834 [](OpenMPDirectiveKind K) -> bool {
6835 return isOpenMPTeamsDirective(K);
6836 },
6837 false);
6838 if (DVar.CKind == OMPC_reduction &&
6839 isOpenMPTeamsDirective(DVar.DKind)) {
6840 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6841 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6842 continue;
6843 }
6844 DVar = DSAStack->getTopDSA(VD, false);
6845 if (DVar.CKind == OMPC_lastprivate) {
6846 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6847 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6848 continue;
6849 }
6850 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006851 }
6852
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006853 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006854 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006855 DSAStack->getCurrentDirective() == OMPD_task) {
6856 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6857 << getOpenMPClauseName(OMPC_firstprivate) << Type
6858 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6859 bool IsDecl =
6860 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6861 Diag(VD->getLocation(),
6862 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6863 << VD;
6864 continue;
6865 }
6866
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006867 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006868 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6869 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006870 // Generate helper private variable and initialize it with the value of the
6871 // original variable. The address of the original variable is replaced by
6872 // the address of the new private variable in the CodeGen. This new variable
6873 // is not added to IdResolver, so the code in the OpenMP region uses
6874 // original variable for proper diagnostics and variable capturing.
6875 Expr *VDInitRefExpr = nullptr;
6876 // For arrays generate initializer for single element and replace it by the
6877 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006878 if (Type->isArrayType()) {
6879 auto VDInit =
6880 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6881 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006882 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006883 ElemType = ElemType.getUnqualifiedType();
6884 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6885 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006886 InitializedEntity Entity =
6887 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006888 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6889
6890 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6891 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6892 if (Result.isInvalid())
6893 VDPrivate->setInvalidDecl();
6894 else
6895 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006896 // Remove temp variable declaration.
6897 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006898 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006899 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006900 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006901 VDInitRefExpr =
6902 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006903 AddInitializerToDecl(VDPrivate,
6904 DefaultLvalueConversion(VDInitRefExpr).get(),
6905 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006906 }
6907 if (VDPrivate->isInvalidDecl()) {
6908 if (IsImplicitClause) {
6909 Diag(DE->getExprLoc(),
6910 diag::note_omp_task_predetermined_firstprivate_here);
6911 }
6912 continue;
6913 }
6914 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006915 auto VDPrivateRefExpr = buildDeclRefExpr(
6916 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006917 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6918 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006919 PrivateCopies.push_back(VDPrivateRefExpr);
6920 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006921 }
6922
Alexey Bataeved09d242014-05-28 05:53:51 +00006923 if (Vars.empty())
6924 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006925
6926 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006927 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006928}
6929
Alexander Musman1bb328c2014-06-04 13:06:39 +00006930OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6931 SourceLocation StartLoc,
6932 SourceLocation LParenLoc,
6933 SourceLocation EndLoc) {
6934 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006935 SmallVector<Expr *, 8> SrcExprs;
6936 SmallVector<Expr *, 8> DstExprs;
6937 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006938 for (auto &RefExpr : VarList) {
6939 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6940 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6941 // It will be analyzed later.
6942 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006943 SrcExprs.push_back(nullptr);
6944 DstExprs.push_back(nullptr);
6945 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006946 continue;
6947 }
6948
6949 SourceLocation ELoc = RefExpr->getExprLoc();
6950 // OpenMP [2.1, C/C++]
6951 // A list item is a variable name.
6952 // OpenMP [2.14.3.5, Restrictions, p.1]
6953 // A variable that is part of another variable (as an array or structure
6954 // element) cannot appear in a lastprivate clause.
6955 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6956 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6957 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6958 continue;
6959 }
6960 Decl *D = DE->getDecl();
6961 VarDecl *VD = cast<VarDecl>(D);
6962
6963 QualType Type = VD->getType();
6964 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6965 // It will be analyzed later.
6966 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006967 SrcExprs.push_back(nullptr);
6968 DstExprs.push_back(nullptr);
6969 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006970 continue;
6971 }
6972
6973 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6974 // A variable that appears in a lastprivate clause must not have an
6975 // incomplete type or a reference type.
6976 if (RequireCompleteType(ELoc, Type,
6977 diag::err_omp_lastprivate_incomplete_type)) {
6978 continue;
6979 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006980 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006981
6982 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6983 // in a Construct]
6984 // Variables with the predetermined data-sharing attributes may not be
6985 // listed in data-sharing attributes clauses, except for the cases
6986 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006987 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006988 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6989 DVar.CKind != OMPC_firstprivate &&
6990 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6991 Diag(ELoc, diag::err_omp_wrong_dsa)
6992 << getOpenMPClauseName(DVar.CKind)
6993 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006994 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006995 continue;
6996 }
6997
Alexey Bataevf29276e2014-06-18 04:14:57 +00006998 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6999 // OpenMP [2.14.3.5, Restrictions, p.2]
7000 // A list item that is private within a parallel region, or that appears in
7001 // the reduction clause of a parallel construct, must not appear in a
7002 // lastprivate clause on a worksharing construct if any of the corresponding
7003 // worksharing regions ever binds to any of the corresponding parallel
7004 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007005 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007006 if (isOpenMPWorksharingDirective(CurrDir) &&
7007 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007008 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007009 if (DVar.CKind != OMPC_shared) {
7010 Diag(ELoc, diag::err_omp_required_access)
7011 << getOpenMPClauseName(OMPC_lastprivate)
7012 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007013 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007014 continue;
7015 }
7016 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007017 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007018 // A variable of class type (or array thereof) that appears in a
7019 // lastprivate clause requires an accessible, unambiguous default
7020 // constructor for the class type, unless the list item is also specified
7021 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007022 // A variable of class type (or array thereof) that appears in a
7023 // lastprivate clause requires an accessible, unambiguous copy assignment
7024 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007025 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007026 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007027 Type.getUnqualifiedType(), ".lastprivate.src",
7028 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007029 auto *PseudoSrcExpr = buildDeclRefExpr(
7030 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007031 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007032 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7033 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007034 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007035 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007036 // For arrays generate assignment operation for single element and replace
7037 // it by the original array element in CodeGen.
7038 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7039 PseudoDstExpr, PseudoSrcExpr);
7040 if (AssignmentOp.isInvalid())
7041 continue;
7042 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7043 /*DiscardedValue=*/true);
7044 if (AssignmentOp.isInvalid())
7045 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007046
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007047 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7048 // A list item may appear in a firstprivate or lastprivate clause but not
7049 // both.
7050 if (CurrDir == OMPD_distribute) {
7051 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7052 if (DVar.CKind == OMPC_firstprivate) {
7053 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7054 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7055 continue;
7056 }
7057 }
7058
Alexey Bataev39f915b82015-05-08 10:41:21 +00007059 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007060 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007061 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007062 SrcExprs.push_back(PseudoSrcExpr);
7063 DstExprs.push_back(PseudoDstExpr);
7064 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007065 }
7066
7067 if (Vars.empty())
7068 return nullptr;
7069
7070 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007071 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007072}
7073
Alexey Bataev758e55e2013-09-06 18:03:48 +00007074OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7075 SourceLocation StartLoc,
7076 SourceLocation LParenLoc,
7077 SourceLocation EndLoc) {
7078 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007079 for (auto &RefExpr : VarList) {
7080 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7081 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007082 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007083 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007084 continue;
7085 }
7086
Alexey Bataeved09d242014-05-28 05:53:51 +00007087 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007088 // OpenMP [2.1, C/C++]
7089 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007090 // OpenMP [2.14.3.2, Restrictions, p.1]
7091 // A variable that is part of another variable (as an array or structure
7092 // element) cannot appear in a shared unless it is a static data member
7093 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007094 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007095 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007096 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007097 continue;
7098 }
7099 Decl *D = DE->getDecl();
7100 VarDecl *VD = cast<VarDecl>(D);
7101
7102 QualType Type = VD->getType();
7103 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7104 // It will be analyzed later.
7105 Vars.push_back(DE);
7106 continue;
7107 }
7108
7109 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7110 // in a Construct]
7111 // Variables with the predetermined data-sharing attributes may not be
7112 // listed in data-sharing attributes clauses, except for the cases
7113 // listed below. For these exceptions only, listing a predetermined
7114 // variable in a data-sharing attribute clause is allowed and overrides
7115 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007116 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007117 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7118 DVar.RefExpr) {
7119 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7120 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007121 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007122 continue;
7123 }
7124
7125 DSAStack->addDSA(VD, DE, OMPC_shared);
7126 Vars.push_back(DE);
7127 }
7128
Alexey Bataeved09d242014-05-28 05:53:51 +00007129 if (Vars.empty())
7130 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007131
7132 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7133}
7134
Alexey Bataevc5e02582014-06-16 07:08:35 +00007135namespace {
7136class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7137 DSAStackTy *Stack;
7138
7139public:
7140 bool VisitDeclRefExpr(DeclRefExpr *E) {
7141 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007142 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007143 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7144 return false;
7145 if (DVar.CKind != OMPC_unknown)
7146 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007147 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007148 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007149 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007150 return true;
7151 return false;
7152 }
7153 return false;
7154 }
7155 bool VisitStmt(Stmt *S) {
7156 for (auto Child : S->children()) {
7157 if (Child && Visit(Child))
7158 return true;
7159 }
7160 return false;
7161 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007162 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007163};
Alexey Bataev23b69422014-06-18 07:08:49 +00007164} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007165
7166OMPClause *Sema::ActOnOpenMPReductionClause(
7167 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7168 SourceLocation ColonLoc, SourceLocation EndLoc,
7169 CXXScopeSpec &ReductionIdScopeSpec,
7170 const DeclarationNameInfo &ReductionId) {
7171 // TODO: Allow scope specification search when 'declare reduction' is
7172 // supported.
7173 assert(ReductionIdScopeSpec.isEmpty() &&
7174 "No support for scoped reduction identifiers yet.");
7175
7176 auto DN = ReductionId.getName();
7177 auto OOK = DN.getCXXOverloadedOperator();
7178 BinaryOperatorKind BOK = BO_Comma;
7179
7180 // OpenMP [2.14.3.6, reduction clause]
7181 // C
7182 // reduction-identifier is either an identifier or one of the following
7183 // operators: +, -, *, &, |, ^, && and ||
7184 // C++
7185 // reduction-identifier is either an id-expression or one of the following
7186 // operators: +, -, *, &, |, ^, && and ||
7187 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7188 switch (OOK) {
7189 case OO_Plus:
7190 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007191 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007192 break;
7193 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007194 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007195 break;
7196 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007197 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007198 break;
7199 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007200 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007201 break;
7202 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007203 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007204 break;
7205 case OO_AmpAmp:
7206 BOK = BO_LAnd;
7207 break;
7208 case OO_PipePipe:
7209 BOK = BO_LOr;
7210 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007211 case OO_New:
7212 case OO_Delete:
7213 case OO_Array_New:
7214 case OO_Array_Delete:
7215 case OO_Slash:
7216 case OO_Percent:
7217 case OO_Tilde:
7218 case OO_Exclaim:
7219 case OO_Equal:
7220 case OO_Less:
7221 case OO_Greater:
7222 case OO_LessEqual:
7223 case OO_GreaterEqual:
7224 case OO_PlusEqual:
7225 case OO_MinusEqual:
7226 case OO_StarEqual:
7227 case OO_SlashEqual:
7228 case OO_PercentEqual:
7229 case OO_CaretEqual:
7230 case OO_AmpEqual:
7231 case OO_PipeEqual:
7232 case OO_LessLess:
7233 case OO_GreaterGreater:
7234 case OO_LessLessEqual:
7235 case OO_GreaterGreaterEqual:
7236 case OO_EqualEqual:
7237 case OO_ExclaimEqual:
7238 case OO_PlusPlus:
7239 case OO_MinusMinus:
7240 case OO_Comma:
7241 case OO_ArrowStar:
7242 case OO_Arrow:
7243 case OO_Call:
7244 case OO_Subscript:
7245 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007246 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007247 case NUM_OVERLOADED_OPERATORS:
7248 llvm_unreachable("Unexpected reduction identifier");
7249 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007250 if (auto II = DN.getAsIdentifierInfo()) {
7251 if (II->isStr("max"))
7252 BOK = BO_GT;
7253 else if (II->isStr("min"))
7254 BOK = BO_LT;
7255 }
7256 break;
7257 }
7258 SourceRange ReductionIdRange;
7259 if (ReductionIdScopeSpec.isValid()) {
7260 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7261 }
7262 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7263 if (BOK == BO_Comma) {
7264 // Not allowed reduction identifier is found.
7265 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7266 << ReductionIdRange;
7267 return nullptr;
7268 }
7269
7270 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007271 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007272 SmallVector<Expr *, 8> LHSs;
7273 SmallVector<Expr *, 8> RHSs;
7274 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007275 for (auto RefExpr : VarList) {
7276 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7277 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7278 // It will be analyzed later.
7279 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007280 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007281 LHSs.push_back(nullptr);
7282 RHSs.push_back(nullptr);
7283 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007284 continue;
7285 }
7286
7287 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7288 RefExpr->isInstantiationDependent() ||
7289 RefExpr->containsUnexpandedParameterPack()) {
7290 // It will be analyzed later.
7291 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007292 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007293 LHSs.push_back(nullptr);
7294 RHSs.push_back(nullptr);
7295 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007296 continue;
7297 }
7298
7299 auto ELoc = RefExpr->getExprLoc();
7300 auto ERange = RefExpr->getSourceRange();
7301 // OpenMP [2.1, C/C++]
7302 // A list item is a variable or array section, subject to the restrictions
7303 // specified in Section 2.4 on page 42 and in each of the sections
7304 // describing clauses and directives for which a list appears.
7305 // OpenMP [2.14.3.3, Restrictions, p.1]
7306 // A variable that is part of another variable (as an array or
7307 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007308 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7309 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7310 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7311 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7312 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007313 continue;
7314 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007315 QualType Type;
7316 VarDecl *VD = nullptr;
7317 if (DE) {
7318 auto D = DE->getDecl();
7319 VD = cast<VarDecl>(D);
7320 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007321 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007322 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007323 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7324 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7325 Base = TempASE->getBase()->IgnoreParenImpCasts();
7326 DE = dyn_cast<DeclRefExpr>(Base);
7327 if (DE)
7328 VD = dyn_cast<VarDecl>(DE->getDecl());
7329 if (!VD) {
7330 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7331 << 0 << Base->getSourceRange();
7332 continue;
7333 }
7334 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007335 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7336 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7337 Type = ATy->getElementType();
7338 else
7339 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007340 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7341 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7342 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7343 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7344 Base = TempASE->getBase()->IgnoreParenImpCasts();
7345 DE = dyn_cast<DeclRefExpr>(Base);
7346 if (DE)
7347 VD = dyn_cast<VarDecl>(DE->getDecl());
7348 if (!VD) {
7349 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7350 << 1 << Base->getSourceRange();
7351 continue;
7352 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007353 }
7354
Alexey Bataevc5e02582014-06-16 07:08:35 +00007355 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7356 // A variable that appears in a private clause must not have an incomplete
7357 // type or a reference type.
7358 if (RequireCompleteType(ELoc, Type,
7359 diag::err_omp_reduction_incomplete_type))
7360 continue;
7361 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7362 // Arrays may not appear in a reduction clause.
7363 if (Type.getNonReferenceType()->isArrayType()) {
7364 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007365 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007366 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7367 VarDecl::DeclarationOnly;
7368 Diag(VD->getLocation(),
7369 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7370 << VD;
7371 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007372 continue;
7373 }
7374 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7375 // A list item that appears in a reduction clause must not be
7376 // const-qualified.
7377 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007378 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007379 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007380 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007381 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7382 VarDecl::DeclarationOnly;
7383 Diag(VD->getLocation(),
7384 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7385 << VD;
7386 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007387 continue;
7388 }
7389 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7390 // If a list-item is a reference type then it must bind to the same object
7391 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007392 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007393 VarDecl *VDDef = VD->getDefinition();
7394 if (Type->isReferenceType() && VDDef) {
7395 DSARefChecker Check(DSAStack);
7396 if (Check.Visit(VDDef->getInit())) {
7397 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7398 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7399 continue;
7400 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007401 }
7402 }
7403 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7404 // The type of a list item that appears in a reduction clause must be valid
7405 // for the reduction-identifier. For a max or min reduction in C, the type
7406 // of the list item must be an allowed arithmetic data type: char, int,
7407 // float, double, or _Bool, possibly modified with long, short, signed, or
7408 // unsigned. For a max or min reduction in C++, the type of the list item
7409 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7410 // double, or bool, possibly modified with long, short, signed, or unsigned.
7411 if ((BOK == BO_GT || BOK == BO_LT) &&
7412 !(Type->isScalarType() ||
7413 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7414 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7415 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007416 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007417 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7418 VarDecl::DeclarationOnly;
7419 Diag(VD->getLocation(),
7420 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7421 << VD;
7422 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007423 continue;
7424 }
7425 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7426 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7427 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
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 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007437 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7438 // in a Construct]
7439 // Variables with the predetermined data-sharing attributes may not be
7440 // listed in data-sharing attributes clauses, except for the cases
7441 // listed below. For these exceptions only, listing a predetermined
7442 // variable in a data-sharing attribute clause is allowed and overrides
7443 // the variable's predetermined data-sharing attributes.
7444 // OpenMP [2.14.3.6, Restrictions, p.3]
7445 // Any number of reduction clauses can be specified on the directive,
7446 // but a list item can appear only once in the reduction clauses for that
7447 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007448 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007449 DVar = DSAStack->getTopDSA(VD, false);
7450 if (DVar.CKind == OMPC_reduction) {
7451 Diag(ELoc, diag::err_omp_once_referenced)
7452 << getOpenMPClauseName(OMPC_reduction);
7453 if (DVar.RefExpr) {
7454 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007455 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007456 } else if (DVar.CKind != OMPC_unknown) {
7457 Diag(ELoc, diag::err_omp_wrong_dsa)
7458 << getOpenMPClauseName(DVar.CKind)
7459 << getOpenMPClauseName(OMPC_reduction);
7460 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7461 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007462 }
7463
7464 // OpenMP [2.14.3.6, Restrictions, p.1]
7465 // A list item that appears in a reduction clause of a worksharing
7466 // construct must be shared in the parallel regions to which any of the
7467 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007468 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7469 if (isOpenMPWorksharingDirective(CurrDir) &&
7470 !isOpenMPParallelDirective(CurrDir)) {
7471 DVar = DSAStack->getImplicitDSA(VD, true);
7472 if (DVar.CKind != OMPC_shared) {
7473 Diag(ELoc, diag::err_omp_required_access)
7474 << getOpenMPClauseName(OMPC_reduction)
7475 << getOpenMPClauseName(OMPC_shared);
7476 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7477 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007478 }
7479 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007480
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007481 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007482 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7483 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7484 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7485 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7486 auto PrivateTy = Type;
7487 if (OASE) {
7488 // For array sections only:
7489 // Create pseudo array type for private copy. The size for this array will
7490 // be generated during codegen.
7491 // For array subscripts or single variables Private Ty is the same as Type
7492 // (type of the variable or single array element).
7493 PrivateTy = Context.getVariableArrayType(
7494 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7495 Context.getSizeType(), VK_RValue),
7496 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7497 }
7498 // Private copy.
7499 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7500 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007501 // Add initializer for private variable.
7502 Expr *Init = nullptr;
7503 switch (BOK) {
7504 case BO_Add:
7505 case BO_Xor:
7506 case BO_Or:
7507 case BO_LOr:
7508 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7509 if (Type->isScalarType() || Type->isAnyComplexType()) {
7510 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007511 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007512 break;
7513 case BO_Mul:
7514 case BO_LAnd:
7515 if (Type->isScalarType() || Type->isAnyComplexType()) {
7516 // '*' and '&&' reduction ops - initializer is '1'.
7517 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7518 }
7519 break;
7520 case BO_And: {
7521 // '&' reduction op - initializer is '~0'.
7522 QualType OrigType = Type;
7523 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7524 Type = ComplexTy->getElementType();
7525 }
7526 if (Type->isRealFloatingType()) {
7527 llvm::APFloat InitValue =
7528 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7529 /*isIEEE=*/true);
7530 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7531 Type, ELoc);
7532 } else if (Type->isScalarType()) {
7533 auto Size = Context.getTypeSize(Type);
7534 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7535 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7536 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7537 }
7538 if (Init && OrigType->isAnyComplexType()) {
7539 // Init = 0xFFFF + 0xFFFFi;
7540 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7541 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7542 }
7543 Type = OrigType;
7544 break;
7545 }
7546 case BO_LT:
7547 case BO_GT: {
7548 // 'min' reduction op - initializer is 'Largest representable number in
7549 // the reduction list item type'.
7550 // 'max' reduction op - initializer is 'Least representable number in
7551 // the reduction list item type'.
7552 if (Type->isIntegerType() || Type->isPointerType()) {
7553 bool IsSigned = Type->hasSignedIntegerRepresentation();
7554 auto Size = Context.getTypeSize(Type);
7555 QualType IntTy =
7556 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7557 llvm::APInt InitValue =
7558 (BOK != BO_LT)
7559 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7560 : llvm::APInt::getMinValue(Size)
7561 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7562 : llvm::APInt::getMaxValue(Size);
7563 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7564 if (Type->isPointerType()) {
7565 // Cast to pointer type.
7566 auto CastExpr = BuildCStyleCastExpr(
7567 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7568 SourceLocation(), Init);
7569 if (CastExpr.isInvalid())
7570 continue;
7571 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007572 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007573 } else if (Type->isRealFloatingType()) {
7574 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7575 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7576 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7577 Type, ELoc);
7578 }
7579 break;
7580 }
7581 case BO_PtrMemD:
7582 case BO_PtrMemI:
7583 case BO_MulAssign:
7584 case BO_Div:
7585 case BO_Rem:
7586 case BO_Sub:
7587 case BO_Shl:
7588 case BO_Shr:
7589 case BO_LE:
7590 case BO_GE:
7591 case BO_EQ:
7592 case BO_NE:
7593 case BO_AndAssign:
7594 case BO_XorAssign:
7595 case BO_OrAssign:
7596 case BO_Assign:
7597 case BO_AddAssign:
7598 case BO_SubAssign:
7599 case BO_DivAssign:
7600 case BO_RemAssign:
7601 case BO_ShlAssign:
7602 case BO_ShrAssign:
7603 case BO_Comma:
7604 llvm_unreachable("Unexpected reduction operation");
7605 }
7606 if (Init) {
7607 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7608 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007609 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007610 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007611 if (!RHSVD->hasInit()) {
7612 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7613 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007614 if (VD) {
7615 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7616 VarDecl::DeclarationOnly;
7617 Diag(VD->getLocation(),
7618 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7619 << VD;
7620 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007621 continue;
7622 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007623 // Store initializer for single element in private copy. Will be used during
7624 // codegen.
7625 PrivateVD->setInit(RHSVD->getInit());
7626 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007627 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7628 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007629 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007630 ExprResult ReductionOp =
7631 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7632 LHSDRE, RHSDRE);
7633 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007634 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007635 ReductionOp =
7636 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7637 BO_Assign, LHSDRE, ReductionOp.get());
7638 } else {
7639 auto *ConditionalOp = new (Context) ConditionalOperator(
7640 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7641 RHSDRE, Type, VK_LValue, OK_Ordinary);
7642 ReductionOp =
7643 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7644 BO_Assign, LHSDRE, ConditionalOp);
7645 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007646 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007647 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007648 if (ReductionOp.isInvalid())
7649 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007650
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007651 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007652 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007653 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007654 LHSs.push_back(LHSDRE);
7655 RHSs.push_back(RHSDRE);
7656 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007657 }
7658
7659 if (Vars.empty())
7660 return nullptr;
7661
7662 return OMPReductionClause::Create(
7663 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007664 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7665 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007666}
7667
Alexey Bataev182227b2015-08-20 10:54:39 +00007668OMPClause *Sema::ActOnOpenMPLinearClause(
7669 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7670 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7671 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007672 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007673 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007674 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007675 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7676 LinKind == OMPC_LINEAR_unknown) {
7677 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7678 LinKind = OMPC_LINEAR_val;
7679 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007680 for (auto &RefExpr : VarList) {
7681 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7682 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007683 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007684 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007685 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007686 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007687 continue;
7688 }
7689
7690 // OpenMP [2.14.3.7, linear clause]
7691 // A list item that appears in a linear clause is subject to the private
7692 // clause semantics described in Section 2.14.3.3 on page 159 except as
7693 // noted. In addition, the value of the new list item on each iteration
7694 // of the associated loop(s) corresponds to the value of the original
7695 // list item before entering the construct plus the logical number of
7696 // the iteration times linear-step.
7697
Alexey Bataeved09d242014-05-28 05:53:51 +00007698 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007699 // OpenMP [2.1, C/C++]
7700 // A list item is a variable name.
7701 // OpenMP [2.14.3.3, Restrictions, p.1]
7702 // A variable that is part of another variable (as an array or
7703 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007704 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007705 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007706 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007707 continue;
7708 }
7709
7710 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7711
7712 // OpenMP [2.14.3.7, linear clause]
7713 // A list-item cannot appear in more than one linear clause.
7714 // A list-item that appears in a linear clause cannot appear in any
7715 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007716 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007717 if (DVar.RefExpr) {
7718 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7719 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007720 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007721 continue;
7722 }
7723
7724 QualType QType = VD->getType();
7725 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7726 // It will be analyzed later.
7727 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007728 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007729 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007730 continue;
7731 }
7732
7733 // A variable must not have an incomplete type or a reference type.
7734 if (RequireCompleteType(ELoc, QType,
7735 diag::err_omp_linear_incomplete_type)) {
7736 continue;
7737 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007738 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7739 !QType->isReferenceType()) {
7740 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7741 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7742 continue;
7743 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007744 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007745
7746 // A list item must not be const-qualified.
7747 if (QType.isConstant(Context)) {
7748 Diag(ELoc, diag::err_omp_const_variable)
7749 << getOpenMPClauseName(OMPC_linear);
7750 bool IsDecl =
7751 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7752 Diag(VD->getLocation(),
7753 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7754 << VD;
7755 continue;
7756 }
7757
7758 // A list item must be of integral or pointer type.
7759 QType = QType.getUnqualifiedType().getCanonicalType();
7760 const Type *Ty = QType.getTypePtrOrNull();
7761 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7762 !Ty->isPointerType())) {
7763 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7764 bool IsDecl =
7765 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7766 Diag(VD->getLocation(),
7767 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7768 << VD;
7769 continue;
7770 }
7771
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007772 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007773 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7774 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007775 auto *PrivateRef = buildDeclRefExpr(
7776 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007777 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007778 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007779 Expr *InitExpr;
7780 if (LinKind == OMPC_LINEAR_uval)
7781 InitExpr = VD->getInit();
7782 else
7783 InitExpr = DE;
7784 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007785 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007786 auto InitRef = buildDeclRefExpr(
7787 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007788 DSAStack->addDSA(VD, DE, OMPC_linear);
7789 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007790 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007791 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007792 }
7793
7794 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007795 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007796
7797 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007798 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007799 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7800 !Step->isInstantiationDependent() &&
7801 !Step->containsUnexpandedParameterPack()) {
7802 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007803 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007804 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007805 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007806 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007807
Alexander Musman3276a272015-03-21 10:12:56 +00007808 // Build var to save the step value.
7809 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007810 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007811 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007812 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007813 ExprResult CalcStep =
7814 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007815 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007816
Alexander Musman8dba6642014-04-22 13:09:42 +00007817 // Warn about zero linear step (it would be probably better specified as
7818 // making corresponding variables 'const').
7819 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007820 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7821 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007822 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7823 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007824 if (!IsConstant && CalcStep.isUsable()) {
7825 // Calculate the step beforehand instead of doing this on each iteration.
7826 // (This is not used if the number of iterations may be kfold-ed).
7827 CalcStepExpr = CalcStep.get();
7828 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007829 }
7830
Alexey Bataev182227b2015-08-20 10:54:39 +00007831 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7832 ColonLoc, EndLoc, Vars, Privates, Inits,
7833 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007834}
7835
7836static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7837 Expr *NumIterations, Sema &SemaRef,
7838 Scope *S) {
7839 // Walk the vars and build update/final expressions for the CodeGen.
7840 SmallVector<Expr *, 8> Updates;
7841 SmallVector<Expr *, 8> Finals;
7842 Expr *Step = Clause.getStep();
7843 Expr *CalcStep = Clause.getCalcStep();
7844 // OpenMP [2.14.3.7, linear clause]
7845 // If linear-step is not specified it is assumed to be 1.
7846 if (Step == nullptr)
7847 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7848 else if (CalcStep)
7849 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7850 bool HasErrors = false;
7851 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007852 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007853 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007854 for (auto &RefExpr : Clause.varlists()) {
7855 Expr *InitExpr = *CurInit;
7856
7857 // Build privatized reference to the current linear var.
7858 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007859 Expr *CapturedRef;
7860 if (LinKind == OMPC_LINEAR_uval)
7861 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7862 else
7863 CapturedRef =
7864 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7865 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7866 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007867
7868 // Build update: Var = InitExpr + IV * Step
7869 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007870 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007871 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007872 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7873 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007874
7875 // Build final: Var = InitExpr + NumIterations * Step
7876 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007877 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007878 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007879 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7880 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007881 if (!Update.isUsable() || !Final.isUsable()) {
7882 Updates.push_back(nullptr);
7883 Finals.push_back(nullptr);
7884 HasErrors = true;
7885 } else {
7886 Updates.push_back(Update.get());
7887 Finals.push_back(Final.get());
7888 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007889 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007890 }
7891 Clause.setUpdates(Updates);
7892 Clause.setFinals(Finals);
7893 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007894}
7895
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007896OMPClause *Sema::ActOnOpenMPAlignedClause(
7897 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7898 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7899
7900 SmallVector<Expr *, 8> Vars;
7901 for (auto &RefExpr : VarList) {
7902 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7903 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7904 // It will be analyzed later.
7905 Vars.push_back(RefExpr);
7906 continue;
7907 }
7908
7909 SourceLocation ELoc = RefExpr->getExprLoc();
7910 // OpenMP [2.1, C/C++]
7911 // A list item is a variable name.
7912 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7913 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7914 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7915 continue;
7916 }
7917
7918 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7919
7920 // OpenMP [2.8.1, simd construct, Restrictions]
7921 // The type of list items appearing in the aligned clause must be
7922 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007923 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007924 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007925 const Type *Ty = QType.getTypePtrOrNull();
7926 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7927 !Ty->isPointerType())) {
7928 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7929 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7930 bool IsDecl =
7931 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7932 Diag(VD->getLocation(),
7933 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7934 << VD;
7935 continue;
7936 }
7937
7938 // OpenMP [2.8.1, simd construct, Restrictions]
7939 // A list-item cannot appear in more than one aligned clause.
7940 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7941 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7942 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7943 << getOpenMPClauseName(OMPC_aligned);
7944 continue;
7945 }
7946
7947 Vars.push_back(DE);
7948 }
7949
7950 // OpenMP [2.8.1, simd construct, Description]
7951 // The parameter of the aligned clause, alignment, must be a constant
7952 // positive integer expression.
7953 // If no optional parameter is specified, implementation-defined default
7954 // alignments for SIMD instructions on the target platforms are assumed.
7955 if (Alignment != nullptr) {
7956 ExprResult AlignResult =
7957 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7958 if (AlignResult.isInvalid())
7959 return nullptr;
7960 Alignment = AlignResult.get();
7961 }
7962 if (Vars.empty())
7963 return nullptr;
7964
7965 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7966 EndLoc, Vars, Alignment);
7967}
7968
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007969OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7970 SourceLocation StartLoc,
7971 SourceLocation LParenLoc,
7972 SourceLocation EndLoc) {
7973 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007974 SmallVector<Expr *, 8> SrcExprs;
7975 SmallVector<Expr *, 8> DstExprs;
7976 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007977 for (auto &RefExpr : VarList) {
7978 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7979 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007980 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007981 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007982 SrcExprs.push_back(nullptr);
7983 DstExprs.push_back(nullptr);
7984 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007985 continue;
7986 }
7987
Alexey Bataeved09d242014-05-28 05:53:51 +00007988 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007989 // OpenMP [2.1, C/C++]
7990 // A list item is a variable name.
7991 // OpenMP [2.14.4.1, Restrictions, p.1]
7992 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007993 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007994 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007995 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007996 continue;
7997 }
7998
7999 Decl *D = DE->getDecl();
8000 VarDecl *VD = cast<VarDecl>(D);
8001
8002 QualType Type = VD->getType();
8003 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8004 // It will be analyzed later.
8005 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008006 SrcExprs.push_back(nullptr);
8007 DstExprs.push_back(nullptr);
8008 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008009 continue;
8010 }
8011
8012 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8013 // A list item that appears in a copyin clause must be threadprivate.
8014 if (!DSAStack->isThreadPrivate(VD)) {
8015 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008016 << getOpenMPClauseName(OMPC_copyin)
8017 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008018 continue;
8019 }
8020
8021 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8022 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008023 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008024 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008025 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008026 auto *SrcVD =
8027 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8028 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008029 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008030 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8031 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008032 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8033 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008034 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008035 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008036 // For arrays generate assignment operation for single element and replace
8037 // it by the original array element in CodeGen.
8038 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8039 PseudoDstExpr, PseudoSrcExpr);
8040 if (AssignmentOp.isInvalid())
8041 continue;
8042 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8043 /*DiscardedValue=*/true);
8044 if (AssignmentOp.isInvalid())
8045 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008046
8047 DSAStack->addDSA(VD, DE, OMPC_copyin);
8048 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008049 SrcExprs.push_back(PseudoSrcExpr);
8050 DstExprs.push_back(PseudoDstExpr);
8051 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008052 }
8053
Alexey Bataeved09d242014-05-28 05:53:51 +00008054 if (Vars.empty())
8055 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008056
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008057 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8058 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008059}
8060
Alexey Bataevbae9a792014-06-27 10:37:06 +00008061OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8062 SourceLocation StartLoc,
8063 SourceLocation LParenLoc,
8064 SourceLocation EndLoc) {
8065 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008066 SmallVector<Expr *, 8> SrcExprs;
8067 SmallVector<Expr *, 8> DstExprs;
8068 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008069 for (auto &RefExpr : VarList) {
8070 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8071 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8072 // It will be analyzed later.
8073 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008074 SrcExprs.push_back(nullptr);
8075 DstExprs.push_back(nullptr);
8076 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008077 continue;
8078 }
8079
8080 SourceLocation ELoc = RefExpr->getExprLoc();
8081 // OpenMP [2.1, C/C++]
8082 // A list item is a variable name.
8083 // OpenMP [2.14.4.1, Restrictions, p.1]
8084 // A list item that appears in a copyin clause must be threadprivate.
8085 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8086 if (!DE || !isa<VarDecl>(DE->getDecl())) {
8087 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
8088 continue;
8089 }
8090
8091 Decl *D = DE->getDecl();
8092 VarDecl *VD = cast<VarDecl>(D);
8093
8094 QualType Type = VD->getType();
8095 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8096 // It will be analyzed later.
8097 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008098 SrcExprs.push_back(nullptr);
8099 DstExprs.push_back(nullptr);
8100 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008101 continue;
8102 }
8103
8104 // OpenMP [2.14.4.2, Restrictions, p.2]
8105 // A list item that appears in a copyprivate clause may not appear in a
8106 // private or firstprivate clause on the single construct.
8107 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008108 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008109 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8110 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008111 Diag(ELoc, diag::err_omp_wrong_dsa)
8112 << getOpenMPClauseName(DVar.CKind)
8113 << getOpenMPClauseName(OMPC_copyprivate);
8114 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8115 continue;
8116 }
8117
8118 // OpenMP [2.11.4.2, Restrictions, p.1]
8119 // All list items that appear in a copyprivate clause must be either
8120 // threadprivate or private in the enclosing context.
8121 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008122 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008123 if (DVar.CKind == OMPC_shared) {
8124 Diag(ELoc, diag::err_omp_required_access)
8125 << getOpenMPClauseName(OMPC_copyprivate)
8126 << "threadprivate or private in the enclosing context";
8127 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8128 continue;
8129 }
8130 }
8131 }
8132
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008133 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008134 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008135 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008136 << getOpenMPClauseName(OMPC_copyprivate) << Type
8137 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008138 bool IsDecl =
8139 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8140 Diag(VD->getLocation(),
8141 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8142 << VD;
8143 continue;
8144 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008145
Alexey Bataevbae9a792014-06-27 10:37:06 +00008146 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8147 // A variable of class type (or array thereof) that appears in a
8148 // copyin clause requires an accessible, unambiguous copy assignment
8149 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008150 Type = Context.getBaseElementType(Type.getNonReferenceType())
8151 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008152 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008153 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8154 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008155 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008156 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008157 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008158 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8159 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008160 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008161 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008162 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8163 PseudoDstExpr, PseudoSrcExpr);
8164 if (AssignmentOp.isInvalid())
8165 continue;
8166 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8167 /*DiscardedValue=*/true);
8168 if (AssignmentOp.isInvalid())
8169 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008170
8171 // No need to mark vars as copyprivate, they are already threadprivate or
8172 // implicitly private.
8173 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008174 SrcExprs.push_back(PseudoSrcExpr);
8175 DstExprs.push_back(PseudoDstExpr);
8176 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008177 }
8178
8179 if (Vars.empty())
8180 return nullptr;
8181
Alexey Bataeva63048e2015-03-23 06:18:07 +00008182 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8183 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008184}
8185
Alexey Bataev6125da92014-07-21 11:26:11 +00008186OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8187 SourceLocation StartLoc,
8188 SourceLocation LParenLoc,
8189 SourceLocation EndLoc) {
8190 if (VarList.empty())
8191 return nullptr;
8192
8193 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8194}
Alexey Bataevdea47612014-07-23 07:46:59 +00008195
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008196OMPClause *
8197Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8198 SourceLocation DepLoc, SourceLocation ColonLoc,
8199 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8200 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008201 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008202 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008203 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008204 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008205 return nullptr;
8206 }
8207 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008208 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8209 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008210 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008211 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008212 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8213 /*Last=*/OMPC_DEPEND_unknown, Except)
8214 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008215 return nullptr;
8216 }
8217 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008218 llvm::APSInt DepCounter(/*BitWidth=*/32);
8219 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8220 if (DepKind == OMPC_DEPEND_sink) {
8221 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8222 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8223 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008224 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008225 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008226 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8227 DSAStack->getParentOrderedRegionParam()) {
8228 for (auto &RefExpr : VarList) {
8229 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8230 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8231 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8232 // It will be analyzed later.
8233 Vars.push_back(RefExpr);
8234 continue;
8235 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008236
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008237 SourceLocation ELoc = RefExpr->getExprLoc();
8238 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8239 if (DepKind == OMPC_DEPEND_sink) {
8240 if (DepCounter >= TotalDepCount) {
8241 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8242 continue;
8243 }
8244 ++DepCounter;
8245 // OpenMP [2.13.9, Summary]
8246 // depend(dependence-type : vec), where dependence-type is:
8247 // 'sink' and where vec is the iteration vector, which has the form:
8248 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8249 // where n is the value specified by the ordered clause in the loop
8250 // directive, xi denotes the loop iteration variable of the i-th nested
8251 // loop associated with the loop directive, and di is a constant
8252 // non-negative integer.
8253 SimpleExpr = SimpleExpr->IgnoreImplicit();
8254 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8255 if (!DE) {
8256 OverloadedOperatorKind OOK = OO_None;
8257 SourceLocation OOLoc;
8258 Expr *LHS, *RHS;
8259 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8260 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8261 OOLoc = BO->getOperatorLoc();
8262 LHS = BO->getLHS()->IgnoreParenImpCasts();
8263 RHS = BO->getRHS()->IgnoreParenImpCasts();
8264 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8265 OOK = OCE->getOperator();
8266 OOLoc = OCE->getOperatorLoc();
8267 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8268 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8269 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8270 OOK = MCE->getMethodDecl()
8271 ->getNameInfo()
8272 .getName()
8273 .getCXXOverloadedOperator();
8274 OOLoc = MCE->getCallee()->getExprLoc();
8275 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8276 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8277 } else {
8278 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8279 continue;
8280 }
8281 DE = dyn_cast<DeclRefExpr>(LHS);
8282 if (!DE) {
8283 Diag(LHS->getExprLoc(),
8284 diag::err_omp_depend_sink_expected_loop_iteration)
8285 << DSAStack->getParentLoopControlVariable(
8286 DepCounter.getZExtValue());
8287 continue;
8288 }
8289 if (OOK != OO_Plus && OOK != OO_Minus) {
8290 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8291 continue;
8292 }
8293 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8294 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8295 if (Res.isInvalid())
8296 continue;
8297 }
8298 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8299 if (!CurContext->isDependentContext() &&
8300 DSAStack->getParentOrderedRegionParam() &&
8301 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8302 Diag(DE->getExprLoc(),
8303 diag::err_omp_depend_sink_expected_loop_iteration)
8304 << DSAStack->getParentLoopControlVariable(
8305 DepCounter.getZExtValue());
8306 continue;
8307 }
8308 } else {
8309 // OpenMP [2.11.1.1, Restrictions, p.3]
8310 // A variable that is part of another variable (such as a field of a
8311 // structure) but is not an array element or an array section cannot
8312 // appear in a depend clause.
8313 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8314 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8315 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8316 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8317 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8318 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8319 !ASE->getBase()->getType()->isArrayType())) {
8320 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8321 << RefExpr->getSourceRange();
8322 continue;
8323 }
8324 }
8325
8326 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8327 }
8328
8329 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8330 TotalDepCount > VarList.size() &&
8331 DSAStack->getParentOrderedRegionParam()) {
8332 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8333 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8334 }
8335 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8336 Vars.empty())
8337 return nullptr;
8338 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008339
8340 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8341 DepLoc, ColonLoc, Vars);
8342}
Michael Wonge710d542015-08-07 16:16:36 +00008343
8344OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8345 SourceLocation LParenLoc,
8346 SourceLocation EndLoc) {
8347 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008348
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008349 // OpenMP [2.9.1, Restrictions]
8350 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008351 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8352 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008353 return nullptr;
8354
Michael Wonge710d542015-08-07 16:16:36 +00008355 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8356}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008357
8358static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8359 DSAStackTy *Stack, CXXRecordDecl *RD) {
8360 if (!RD || RD->isInvalidDecl())
8361 return true;
8362
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008363 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8364 if (auto *CTD = CTSD->getSpecializedTemplate())
8365 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008366 auto QTy = SemaRef.Context.getRecordType(RD);
8367 if (RD->isDynamicClass()) {
8368 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8369 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8370 return false;
8371 }
8372 auto *DC = RD;
8373 bool IsCorrect = true;
8374 for (auto *I : DC->decls()) {
8375 if (I) {
8376 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8377 if (MD->isStatic()) {
8378 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8379 SemaRef.Diag(MD->getLocation(),
8380 diag::note_omp_static_member_in_target);
8381 IsCorrect = false;
8382 }
8383 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8384 if (VD->isStaticDataMember()) {
8385 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8386 SemaRef.Diag(VD->getLocation(),
8387 diag::note_omp_static_member_in_target);
8388 IsCorrect = false;
8389 }
8390 }
8391 }
8392 }
8393
8394 for (auto &I : RD->bases()) {
8395 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8396 I.getType()->getAsCXXRecordDecl()))
8397 IsCorrect = false;
8398 }
8399 return IsCorrect;
8400}
8401
8402static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8403 DSAStackTy *Stack, QualType QTy) {
8404 NamedDecl *ND;
8405 if (QTy->isIncompleteType(&ND)) {
8406 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8407 return false;
8408 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8409 if (!RD->isInvalidDecl() &&
8410 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8411 return false;
8412 }
8413 return true;
8414}
8415
8416OMPClause *Sema::ActOnOpenMPMapClause(
8417 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8418 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8419 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8420 SmallVector<Expr *, 4> Vars;
8421
8422 for (auto &RE : VarList) {
8423 assert(RE && "Null expr in omp map");
8424 if (isa<DependentScopeDeclRefExpr>(RE)) {
8425 // It will be analyzed later.
8426 Vars.push_back(RE);
8427 continue;
8428 }
8429 SourceLocation ELoc = RE->getExprLoc();
8430
8431 // OpenMP [2.14.5, Restrictions]
8432 // A variable that is part of another variable (such as field of a
8433 // structure) but is not an array element or an array section cannot appear
8434 // in a map clause.
8435 auto *VE = RE->IgnoreParenLValueCasts();
8436
8437 if (VE->isValueDependent() || VE->isTypeDependent() ||
8438 VE->isInstantiationDependent() ||
8439 VE->containsUnexpandedParameterPack()) {
8440 // It will be analyzed later.
8441 Vars.push_back(RE);
8442 continue;
8443 }
8444
8445 auto *SimpleExpr = RE->IgnoreParenCasts();
8446 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8447 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8448 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8449
8450 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8451 (!OASE && !ASE && !DE) ||
8452 (DE && !isa<VarDecl>(DE->getDecl())) ||
8453 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8454 !ASE->getBase()->getType()->isArrayType())) {
8455 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8456 << RE->getSourceRange();
8457 continue;
8458 }
8459
8460 Decl *D = nullptr;
8461 if (DE) {
8462 D = DE->getDecl();
8463 } else if (ASE) {
8464 auto *B = ASE->getBase()->IgnoreParenCasts();
8465 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8466 } else if (OASE) {
8467 auto *B = OASE->getBase();
8468 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8469 }
8470 assert(D && "Null decl on map clause.");
8471 auto *VD = cast<VarDecl>(D);
8472
8473 // OpenMP [2.14.5, Restrictions, p.8]
8474 // threadprivate variables cannot appear in a map clause.
8475 if (DSAStack->isThreadPrivate(VD)) {
8476 auto DVar = DSAStack->getTopDSA(VD, false);
8477 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8478 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8479 continue;
8480 }
8481
8482 // OpenMP [2.14.5, Restrictions, p.2]
8483 // At most one list item can be an array item derived from a given variable
8484 // in map clauses of the same construct.
8485 // OpenMP [2.14.5, Restrictions, p.3]
8486 // List items of map clauses in the same construct must not share original
8487 // storage.
8488 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8489 // A variable for which the type is pointer, reference to array, or
8490 // reference to pointer and an array section derived from that variable
8491 // must not appear as list items of map clauses of the same construct.
8492 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8493 if (MI.RefExpr) {
8494 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8495 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8496 << MI.RefExpr->getSourceRange();
8497 continue;
8498 }
8499
8500 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8501 // A variable for which the type is pointer, reference to array, or
8502 // reference to pointer must not appear as a list item if the enclosing
8503 // device data environment already contains an array section derived from
8504 // that variable.
8505 // An array section derived from a variable for which the type is pointer,
8506 // reference to array, or reference to pointer must not appear as a list
8507 // item if the enclosing device data environment already contains that
8508 // variable.
8509 QualType Type = VD->getType();
8510 MI = DSAStack->getMapInfoForVar(VD);
8511 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8512 isa<DeclRefExpr>(VE)) &&
8513 (Type->isPointerType() || Type->isReferenceType())) {
8514 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8515 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8516 << MI.RefExpr->getSourceRange();
8517 continue;
8518 }
8519
8520 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8521 // A list item must have a mappable type.
8522 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8523 DSAStack, Type))
8524 continue;
8525
Samuel Antaodf67fc42016-01-19 19:15:56 +00008526 // target enter data
8527 // OpenMP [2.10.2, Restrictions, p. 99]
8528 // A map-type must be specified in all map clauses and must be either
8529 // to or alloc.
8530 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8531 if (DKind == OMPD_target_enter_data &&
8532 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
8533 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
8534 <<
8535 // TODO: Need to determine if map type is implicitly determined
8536 0 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
8537 << getOpenMPDirectiveName(DKind);
8538 // Proceed to add the variable in a map clause anyway, to prevent
8539 // further spurious messages
8540 }
8541
Kelvin Li0bff7af2015-11-23 05:32:03 +00008542 Vars.push_back(RE);
8543 MI.RefExpr = RE;
8544 DSAStack->addMapInfoForVar(VD, MI);
8545 }
8546 if (Vars.empty())
8547 return nullptr;
8548
8549 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8550 MapTypeModifier, MapType, MapLoc);
8551}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008552
8553OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8554 SourceLocation StartLoc,
8555 SourceLocation LParenLoc,
8556 SourceLocation EndLoc) {
8557 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008558
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008559 // OpenMP [teams Constrcut, Restrictions]
8560 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008561 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8562 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008563 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008564
8565 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8566}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008567
8568OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8569 SourceLocation StartLoc,
8570 SourceLocation LParenLoc,
8571 SourceLocation EndLoc) {
8572 Expr *ValExpr = ThreadLimit;
8573
8574 // OpenMP [teams Constrcut, Restrictions]
8575 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008576 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8577 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008578 return nullptr;
8579
8580 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8581 EndLoc);
8582}
Alexey Bataeva0569352015-12-01 10:17:31 +00008583
8584OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8585 SourceLocation StartLoc,
8586 SourceLocation LParenLoc,
8587 SourceLocation EndLoc) {
8588 Expr *ValExpr = Priority;
8589
8590 // OpenMP [2.9.1, task Constrcut]
8591 // The priority-value is a non-negative numerical scalar expression.
8592 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8593 /*StrictlyPositive=*/false))
8594 return nullptr;
8595
8596 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8597}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008598
8599OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8600 SourceLocation StartLoc,
8601 SourceLocation LParenLoc,
8602 SourceLocation EndLoc) {
8603 Expr *ValExpr = Grainsize;
8604
8605 // OpenMP [2.9.2, taskloop Constrcut]
8606 // The parameter of the grainsize clause must be a positive integer
8607 // expression.
8608 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8609 /*StrictlyPositive=*/true))
8610 return nullptr;
8611
8612 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8613}
Alexey Bataev382967a2015-12-08 12:06:20 +00008614
8615OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8616 SourceLocation StartLoc,
8617 SourceLocation LParenLoc,
8618 SourceLocation EndLoc) {
8619 Expr *ValExpr = NumTasks;
8620
8621 // OpenMP [2.9.2, taskloop Constrcut]
8622 // The parameter of the num_tasks clause must be a positive integer
8623 // expression.
8624 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8625 /*StrictlyPositive=*/true))
8626 return nullptr;
8627
8628 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8629}
8630
Alexey Bataev28c75412015-12-15 08:19:24 +00008631OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8632 SourceLocation LParenLoc,
8633 SourceLocation EndLoc) {
8634 // OpenMP [2.13.2, critical construct, Description]
8635 // ... where hint-expression is an integer constant expression that evaluates
8636 // to a valid lock hint.
8637 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8638 if (HintExpr.isInvalid())
8639 return nullptr;
8640 return new (Context)
8641 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8642}
8643
Carlo Bertollib4adf552016-01-15 18:50:31 +00008644OMPClause *Sema::ActOnOpenMPDistScheduleClause(
8645 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8646 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
8647 SourceLocation EndLoc) {
8648 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
8649 std::string Values;
8650 Values += "'";
8651 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
8652 Values += "'";
8653 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8654 << Values << getOpenMPClauseName(OMPC_dist_schedule);
8655 return nullptr;
8656 }
8657 Expr *ValExpr = ChunkSize;
8658 Expr *HelperValExpr = nullptr;
8659 if (ChunkSize) {
8660 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8661 !ChunkSize->isInstantiationDependent() &&
8662 !ChunkSize->containsUnexpandedParameterPack()) {
8663 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8664 ExprResult Val =
8665 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8666 if (Val.isInvalid())
8667 return nullptr;
8668
8669 ValExpr = Val.get();
8670
8671 // OpenMP [2.7.1, Restrictions]
8672 // chunk_size must be a loop invariant integer expression with a positive
8673 // value.
8674 llvm::APSInt Result;
8675 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8676 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8677 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8678 << "dist_schedule" << ChunkSize->getSourceRange();
8679 return nullptr;
8680 }
8681 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
8682 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
8683 ChunkSize->getType(), ".chunk.");
8684 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
8685 ChunkSize->getExprLoc(),
8686 /*RefersToCapture=*/true);
8687 HelperValExpr = ImpVarRef;
8688 }
8689 }
8690 }
8691
8692 return new (Context)
8693 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
8694 Kind, ValExpr, HelperValExpr);
8695}