blob: 4d0d3137bc003990333beb3537b7e6777f2f5d4e [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:
Alexey Bataev9959db52014-05-06 10:08:46 +00001613 llvm_unreachable("OpenMP Directive is not allowed");
1614 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001615 llvm_unreachable("Unknown OpenMP directive");
1616 }
1617}
1618
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001619StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1620 ArrayRef<OMPClause *> Clauses) {
1621 if (!S.isUsable()) {
1622 ActOnCapturedRegionError();
1623 return StmtError();
1624 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001625
1626 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001627 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001628 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001629 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001630 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001631 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001632 Clause->getClauseKind() == OMPC_copyprivate ||
1633 (getLangOpts().OpenMPUseTLS &&
1634 getASTContext().getTargetInfo().isTLSSupported() &&
1635 Clause->getClauseKind() == OMPC_copyin)) {
1636 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001637 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001638 for (auto *VarRef : Clause->children()) {
1639 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001640 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001641 }
1642 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001643 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001644 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1645 Clause->getClauseKind() == OMPC_schedule) {
1646 // Mark all variables in private list clauses as used in inner region.
1647 // Required for proper codegen of combined directives.
1648 // TODO: add processing for other clauses.
1649 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001650 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1651 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001652 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001653 if (Clause->getClauseKind() == OMPC_schedule)
1654 SC = cast<OMPScheduleClause>(Clause);
1655 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001656 OC = cast<OMPOrderedClause>(Clause);
1657 else if (Clause->getClauseKind() == OMPC_linear)
1658 LCs.push_back(cast<OMPLinearClause>(Clause));
1659 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001660 bool ErrorFound = false;
1661 // OpenMP, 2.7.1 Loop Construct, Restrictions
1662 // The nonmonotonic modifier cannot be specified if an ordered clause is
1663 // specified.
1664 if (SC &&
1665 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1666 SC->getSecondScheduleModifier() ==
1667 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1668 OC) {
1669 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1670 ? SC->getFirstScheduleModifierLoc()
1671 : SC->getSecondScheduleModifierLoc(),
1672 diag::err_omp_schedule_nonmonotonic_ordered)
1673 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1674 ErrorFound = true;
1675 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001676 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1677 for (auto *C : LCs) {
1678 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1679 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1680 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001681 ErrorFound = true;
1682 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001683 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1684 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1685 OC->getNumForLoops()) {
1686 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1687 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1688 ErrorFound = true;
1689 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001690 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001691 ActOnCapturedRegionError();
1692 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001693 }
1694 return ActOnCapturedRegionEnd(S.get());
1695}
1696
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001697static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1698 OpenMPDirectiveKind CurrentRegion,
1699 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001700 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001701 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001702 // Allowed nesting of constructs
1703 // +------------------+-----------------+------------------------------------+
1704 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1705 // +------------------+-----------------+------------------------------------+
1706 // | parallel | parallel | * |
1707 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001708 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001709 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001710 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001711 // | parallel | simd | * |
1712 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001713 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001714 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001715 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001716 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001717 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001718 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001719 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001720 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001721 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001722 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001723 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001724 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001725 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001726 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001727 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001728 // | parallel | cancellation | |
1729 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001730 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001731 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001732 // | parallel | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001733 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001734 // +------------------+-----------------+------------------------------------+
1735 // | for | parallel | * |
1736 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001737 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001738 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001739 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001740 // | for | simd | * |
1741 // | for | sections | + |
1742 // | for | section | + |
1743 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001744 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001745 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001746 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001747 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001748 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001749 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001750 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001751 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001752 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001753 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001754 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001755 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001756 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001757 // | for | cancellation | |
1758 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001759 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001760 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001761 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001762 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001763 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001764 // | master | parallel | * |
1765 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001766 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001767 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001768 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001769 // | master | simd | * |
1770 // | master | sections | + |
1771 // | master | section | + |
1772 // | master | single | + |
1773 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001774 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001775 // | master |parallel sections| * |
1776 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001777 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001778 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001779 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001780 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001781 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001782 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001783 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001784 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001785 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001786 // | master | cancellation | |
1787 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001788 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001789 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001790 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001791 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001792 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001793 // | critical | parallel | * |
1794 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001795 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001796 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001797 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001798 // | critical | simd | * |
1799 // | critical | sections | + |
1800 // | critical | section | + |
1801 // | critical | single | + |
1802 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001803 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001804 // | critical |parallel sections| * |
1805 // | critical | task | * |
1806 // | critical | taskyield | * |
1807 // | critical | barrier | + |
1808 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001809 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001810 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001811 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001812 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001813 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001814 // | critical | cancellation | |
1815 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001816 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001817 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001818 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001819 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001820 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001821 // | simd | parallel | |
1822 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001823 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001824 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001825 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001826 // | simd | simd | |
1827 // | simd | sections | |
1828 // | simd | section | |
1829 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001830 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001831 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001832 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001833 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001834 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001835 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001836 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001837 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001838 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001839 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001840 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001841 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001842 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001843 // | simd | cancellation | |
1844 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001845 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001846 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001847 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001848 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001849 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001850 // | for simd | parallel | |
1851 // | for simd | for | |
1852 // | for simd | for simd | |
1853 // | for simd | master | |
1854 // | for simd | critical | |
1855 // | for simd | simd | |
1856 // | for simd | sections | |
1857 // | for simd | section | |
1858 // | for simd | single | |
1859 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001860 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001861 // | for simd |parallel sections| |
1862 // | for simd | task | |
1863 // | for simd | taskyield | |
1864 // | for simd | barrier | |
1865 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001866 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001867 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001868 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001869 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001870 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001871 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001872 // | for simd | cancellation | |
1873 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001874 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001875 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001876 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001877 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001878 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001879 // | parallel for simd| parallel | |
1880 // | parallel for simd| for | |
1881 // | parallel for simd| for simd | |
1882 // | parallel for simd| master | |
1883 // | parallel for simd| critical | |
1884 // | parallel for simd| simd | |
1885 // | parallel for simd| sections | |
1886 // | parallel for simd| section | |
1887 // | parallel for simd| single | |
1888 // | parallel for simd| parallel for | |
1889 // | parallel for simd|parallel for simd| |
1890 // | parallel for simd|parallel sections| |
1891 // | parallel for simd| task | |
1892 // | parallel for simd| taskyield | |
1893 // | parallel for simd| barrier | |
1894 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001895 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001896 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001897 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001898 // | parallel for simd| atomic | |
1899 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001900 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001901 // | parallel for simd| cancellation | |
1902 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001903 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001904 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001905 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001906 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001907 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001908 // | sections | parallel | * |
1909 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001910 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001911 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001912 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001913 // | sections | simd | * |
1914 // | sections | sections | + |
1915 // | sections | section | * |
1916 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001917 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001918 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001919 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001920 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001921 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001922 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001923 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001924 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001925 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001926 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001927 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001928 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001929 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001930 // | sections | cancellation | |
1931 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001932 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001933 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001934 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001935 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001936 // +------------------+-----------------+------------------------------------+
1937 // | section | parallel | * |
1938 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001939 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001940 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001941 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001942 // | section | simd | * |
1943 // | section | sections | + |
1944 // | section | section | + |
1945 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001946 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001947 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001948 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001949 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001950 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001951 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001952 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001953 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001954 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001955 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001956 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001957 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001958 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001959 // | section | cancellation | |
1960 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001961 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001962 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001963 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001964 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001965 // +------------------+-----------------+------------------------------------+
1966 // | single | parallel | * |
1967 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001968 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001969 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001970 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001971 // | single | simd | * |
1972 // | single | sections | + |
1973 // | single | section | + |
1974 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001975 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001976 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001977 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001978 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001979 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001980 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001981 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001982 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001983 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001984 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001985 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001986 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001987 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001988 // | single | cancellation | |
1989 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001990 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001991 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001992 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001993 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001994 // +------------------+-----------------+------------------------------------+
1995 // | parallel for | parallel | * |
1996 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001997 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001998 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001999 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002000 // | parallel for | simd | * |
2001 // | parallel for | sections | + |
2002 // | parallel for | section | + |
2003 // | parallel for | single | + |
2004 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002005 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002006 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002007 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002008 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002009 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002010 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002011 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002012 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002013 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002014 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002015 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002016 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002017 // | parallel for | cancellation | |
2018 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002019 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002020 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002021 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002022 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002023 // +------------------+-----------------+------------------------------------+
2024 // | parallel sections| parallel | * |
2025 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002026 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002027 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002028 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002029 // | parallel sections| simd | * |
2030 // | parallel sections| sections | + |
2031 // | parallel sections| section | * |
2032 // | parallel sections| single | + |
2033 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002034 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002035 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002036 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002037 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002038 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002039 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002040 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002041 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002042 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002043 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002044 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002045 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002046 // | parallel sections| cancellation | |
2047 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002048 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002049 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002050 // | parallel sections| taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002051 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002052 // +------------------+-----------------+------------------------------------+
2053 // | task | parallel | * |
2054 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002055 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002056 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002057 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002058 // | task | simd | * |
2059 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002060 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002061 // | task | single | + |
2062 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002063 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002064 // | task |parallel sections| * |
2065 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002066 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002067 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002068 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002069 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002070 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002071 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002072 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002073 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002074 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002075 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002076 // | | point | ! |
2077 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002078 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002079 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002080 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002081 // +------------------+-----------------+------------------------------------+
2082 // | ordered | parallel | * |
2083 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002084 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002085 // | ordered | master | * |
2086 // | ordered | critical | * |
2087 // | ordered | simd | * |
2088 // | ordered | sections | + |
2089 // | ordered | section | + |
2090 // | ordered | single | + |
2091 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002092 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002093 // | ordered |parallel sections| * |
2094 // | ordered | task | * |
2095 // | ordered | taskyield | * |
2096 // | ordered | barrier | + |
2097 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002098 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002099 // | ordered | flush | * |
2100 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002101 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002102 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002103 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002104 // | ordered | cancellation | |
2105 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002106 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002107 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002108 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002109 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002110 // +------------------+-----------------+------------------------------------+
2111 // | atomic | parallel | |
2112 // | atomic | for | |
2113 // | atomic | for simd | |
2114 // | atomic | master | |
2115 // | atomic | critical | |
2116 // | atomic | simd | |
2117 // | atomic | sections | |
2118 // | atomic | section | |
2119 // | atomic | single | |
2120 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002121 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002122 // | atomic |parallel sections| |
2123 // | atomic | task | |
2124 // | atomic | taskyield | |
2125 // | atomic | barrier | |
2126 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002127 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002128 // | atomic | flush | |
2129 // | atomic | ordered | |
2130 // | atomic | atomic | |
2131 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002132 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002133 // | atomic | cancellation | |
2134 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002135 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002136 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002137 // | atomic | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002138 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002139 // +------------------+-----------------+------------------------------------+
2140 // | target | parallel | * |
2141 // | target | for | * |
2142 // | target | for simd | * |
2143 // | target | master | * |
2144 // | target | critical | * |
2145 // | target | simd | * |
2146 // | target | sections | * |
2147 // | target | section | * |
2148 // | target | single | * |
2149 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002150 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002151 // | target |parallel sections| * |
2152 // | target | task | * |
2153 // | target | taskyield | * |
2154 // | target | barrier | * |
2155 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002156 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002157 // | target | flush | * |
2158 // | target | ordered | * |
2159 // | target | atomic | * |
2160 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002161 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002162 // | target | cancellation | |
2163 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002164 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002165 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002166 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002167 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002168 // +------------------+-----------------+------------------------------------+
2169 // | teams | parallel | * |
2170 // | teams | for | + |
2171 // | teams | for simd | + |
2172 // | teams | master | + |
2173 // | teams | critical | + |
2174 // | teams | simd | + |
2175 // | teams | sections | + |
2176 // | teams | section | + |
2177 // | teams | single | + |
2178 // | teams | parallel for | * |
2179 // | teams |parallel for simd| * |
2180 // | teams |parallel sections| * |
2181 // | teams | task | + |
2182 // | teams | taskyield | + |
2183 // | teams | barrier | + |
2184 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002185 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002186 // | teams | flush | + |
2187 // | teams | ordered | + |
2188 // | teams | atomic | + |
2189 // | teams | target | + |
2190 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002191 // | teams | cancellation | |
2192 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002193 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002194 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002195 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002196 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002197 // +------------------+-----------------+------------------------------------+
2198 // | taskloop | parallel | * |
2199 // | taskloop | for | + |
2200 // | taskloop | for simd | + |
2201 // | taskloop | master | + |
2202 // | taskloop | critical | * |
2203 // | taskloop | simd | * |
2204 // | taskloop | sections | + |
2205 // | taskloop | section | + |
2206 // | taskloop | single | + |
2207 // | taskloop | parallel for | * |
2208 // | taskloop |parallel for simd| * |
2209 // | taskloop |parallel sections| * |
2210 // | taskloop | task | * |
2211 // | taskloop | taskyield | * |
2212 // | taskloop | barrier | + |
2213 // | taskloop | taskwait | * |
2214 // | taskloop | taskgroup | * |
2215 // | taskloop | flush | * |
2216 // | taskloop | ordered | + |
2217 // | taskloop | atomic | * |
2218 // | taskloop | target | * |
2219 // | taskloop | teams | + |
2220 // | taskloop | cancellation | |
2221 // | | point | |
2222 // | taskloop | cancel | |
2223 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002224 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002225 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002226 // | taskloop simd | parallel | |
2227 // | taskloop simd | for | |
2228 // | taskloop simd | for simd | |
2229 // | taskloop simd | master | |
2230 // | taskloop simd | critical | |
2231 // | taskloop simd | simd | |
2232 // | taskloop simd | sections | |
2233 // | taskloop simd | section | |
2234 // | taskloop simd | single | |
2235 // | taskloop simd | parallel for | |
2236 // | taskloop simd |parallel for simd| |
2237 // | taskloop simd |parallel sections| |
2238 // | taskloop simd | task | |
2239 // | taskloop simd | taskyield | |
2240 // | taskloop simd | barrier | |
2241 // | taskloop simd | taskwait | |
2242 // | taskloop simd | taskgroup | |
2243 // | taskloop simd | flush | |
2244 // | taskloop simd | ordered | + (with simd clause) |
2245 // | taskloop simd | atomic | |
2246 // | taskloop simd | target | |
2247 // | taskloop simd | teams | |
2248 // | taskloop simd | cancellation | |
2249 // | | point | |
2250 // | taskloop simd | cancel | |
2251 // | taskloop simd | taskloop | |
2252 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002253 // | taskloop simd | distribute | |
2254 // +------------------+-----------------+------------------------------------+
2255 // | distribute | parallel | * |
2256 // | distribute | for | * |
2257 // | distribute | for simd | * |
2258 // | distribute | master | * |
2259 // | distribute | critical | * |
2260 // | distribute | simd | * |
2261 // | distribute | sections | * |
2262 // | distribute | section | * |
2263 // | distribute | single | * |
2264 // | distribute | parallel for | * |
2265 // | distribute |parallel for simd| * |
2266 // | distribute |parallel sections| * |
2267 // | distribute | task | * |
2268 // | distribute | taskyield | * |
2269 // | distribute | barrier | * |
2270 // | distribute | taskwait | * |
2271 // | distribute | taskgroup | * |
2272 // | distribute | flush | * |
2273 // | distribute | ordered | + |
2274 // | distribute | atomic | * |
2275 // | distribute | target | |
2276 // | distribute | teams | |
2277 // | distribute | cancellation | + |
2278 // | | point | |
2279 // | distribute | cancel | + |
2280 // | distribute | taskloop | * |
2281 // | distribute | taskloop simd | * |
2282 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002283 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002284 if (Stack->getCurScope()) {
2285 auto ParentRegion = Stack->getParentDirective();
2286 bool NestingProhibited = false;
2287 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002288 enum {
2289 NoRecommend,
2290 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002291 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002292 ShouldBeInTargetRegion,
2293 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002294 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002295 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002296 // OpenMP [2.16, Nesting of Regions]
2297 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002298 // OpenMP [2.8.1,simd Construct, Restrictions]
2299 // An ordered construct with the simd clause is the only OpenMP construct
2300 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002301 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2302 return true;
2303 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002304 if (ParentRegion == OMPD_atomic) {
2305 // OpenMP [2.16, Nesting of Regions]
2306 // OpenMP constructs may not be nested inside an atomic region.
2307 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2308 return true;
2309 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002310 if (CurrentRegion == OMPD_section) {
2311 // OpenMP [2.7.2, sections Construct, Restrictions]
2312 // Orphaned section directives are prohibited. That is, the section
2313 // directives must appear within the sections construct and must not be
2314 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002315 if (ParentRegion != OMPD_sections &&
2316 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002317 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2318 << (ParentRegion != OMPD_unknown)
2319 << getOpenMPDirectiveName(ParentRegion);
2320 return true;
2321 }
2322 return false;
2323 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002324 // Allow some constructs to be orphaned (they could be used in functions,
2325 // called from OpenMP regions with the required preconditions).
2326 if (ParentRegion == OMPD_unknown)
2327 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002328 if (CurrentRegion == OMPD_cancellation_point ||
2329 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002330 // OpenMP [2.16, Nesting of Regions]
2331 // A cancellation point construct for which construct-type-clause is
2332 // taskgroup must be nested inside a task construct. A cancellation
2333 // point construct for which construct-type-clause is not taskgroup must
2334 // be closely nested inside an OpenMP construct that matches the type
2335 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002336 // A cancel construct for which construct-type-clause is taskgroup must be
2337 // nested inside a task construct. A cancel construct for which
2338 // construct-type-clause is not taskgroup must be closely nested inside an
2339 // OpenMP construct that matches the type specified in
2340 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002341 NestingProhibited =
2342 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002343 (CancelRegion == OMPD_for &&
2344 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002345 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2346 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002347 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2348 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002349 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002350 // OpenMP [2.16, Nesting of Regions]
2351 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002352 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002353 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002354 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002355 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002356 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2357 // OpenMP [2.16, Nesting of Regions]
2358 // A critical region may not be nested (closely or otherwise) inside a
2359 // critical region with the same name. Note that this restriction is not
2360 // sufficient to prevent deadlock.
2361 SourceLocation PreviousCriticalLoc;
2362 bool DeadLock =
2363 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2364 OpenMPDirectiveKind K,
2365 const DeclarationNameInfo &DNI,
2366 SourceLocation Loc)
2367 ->bool {
2368 if (K == OMPD_critical &&
2369 DNI.getName() == CurrentName.getName()) {
2370 PreviousCriticalLoc = Loc;
2371 return true;
2372 } else
2373 return false;
2374 },
2375 false /* skip top directive */);
2376 if (DeadLock) {
2377 SemaRef.Diag(StartLoc,
2378 diag::err_omp_prohibited_region_critical_same_name)
2379 << CurrentName.getName();
2380 if (PreviousCriticalLoc.isValid())
2381 SemaRef.Diag(PreviousCriticalLoc,
2382 diag::note_omp_previous_critical_region);
2383 return true;
2384 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002385 } else if (CurrentRegion == OMPD_barrier) {
2386 // OpenMP [2.16, Nesting of Regions]
2387 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002388 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002389 NestingProhibited =
2390 isOpenMPWorksharingDirective(ParentRegion) ||
2391 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002392 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002393 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002394 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002395 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002396 // OpenMP [2.16, Nesting of Regions]
2397 // A worksharing region may not be closely nested inside a worksharing,
2398 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002399 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002400 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002401 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002402 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002403 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002404 Recommend = ShouldBeInParallelRegion;
2405 } else if (CurrentRegion == OMPD_ordered) {
2406 // OpenMP [2.16, Nesting of Regions]
2407 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002408 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002409 // An ordered region must be closely nested inside a loop region (or
2410 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002411 // OpenMP [2.8.1,simd Construct, Restrictions]
2412 // An ordered construct with the simd clause is the only OpenMP construct
2413 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002414 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002415 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002416 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002417 !(isOpenMPSimdDirective(ParentRegion) ||
2418 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002419 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002420 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2421 // OpenMP [2.16, Nesting of Regions]
2422 // If specified, a teams construct must be contained within a target
2423 // construct.
2424 NestingProhibited = ParentRegion != OMPD_target;
2425 Recommend = ShouldBeInTargetRegion;
2426 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2427 }
2428 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2429 // OpenMP [2.16, Nesting of Regions]
2430 // distribute, parallel, parallel sections, parallel workshare, and the
2431 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2432 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002433 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2434 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002435 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002436 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002437 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2438 // OpenMP 4.5 [2.17 Nesting of Regions]
2439 // The region associated with the distribute construct must be strictly
2440 // nested inside a teams region
2441 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2442 Recommend = ShouldBeInTeamsRegion;
2443 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002444 if (NestingProhibited) {
2445 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002446 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2447 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002448 return true;
2449 }
2450 }
2451 return false;
2452}
2453
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002454static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2455 ArrayRef<OMPClause *> Clauses,
2456 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2457 bool ErrorFound = false;
2458 unsigned NamedModifiersNumber = 0;
2459 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2460 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002461 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002462 for (const auto *C : Clauses) {
2463 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2464 // At most one if clause without a directive-name-modifier can appear on
2465 // the directive.
2466 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2467 if (FoundNameModifiers[CurNM]) {
2468 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2469 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2470 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2471 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002472 } else if (CurNM != OMPD_unknown) {
2473 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002474 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002475 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002476 FoundNameModifiers[CurNM] = IC;
2477 if (CurNM == OMPD_unknown)
2478 continue;
2479 // Check if the specified name modifier is allowed for the current
2480 // directive.
2481 // At most one if clause with the particular directive-name-modifier can
2482 // appear on the directive.
2483 bool MatchFound = false;
2484 for (auto NM : AllowedNameModifiers) {
2485 if (CurNM == NM) {
2486 MatchFound = true;
2487 break;
2488 }
2489 }
2490 if (!MatchFound) {
2491 S.Diag(IC->getNameModifierLoc(),
2492 diag::err_omp_wrong_if_directive_name_modifier)
2493 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2494 ErrorFound = true;
2495 }
2496 }
2497 }
2498 // If any if clause on the directive includes a directive-name-modifier then
2499 // all if clauses on the directive must include a directive-name-modifier.
2500 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2501 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2502 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2503 diag::err_omp_no_more_if_clause);
2504 } else {
2505 std::string Values;
2506 std::string Sep(", ");
2507 unsigned AllowedCnt = 0;
2508 unsigned TotalAllowedNum =
2509 AllowedNameModifiers.size() - NamedModifiersNumber;
2510 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2511 ++Cnt) {
2512 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2513 if (!FoundNameModifiers[NM]) {
2514 Values += "'";
2515 Values += getOpenMPDirectiveName(NM);
2516 Values += "'";
2517 if (AllowedCnt + 2 == TotalAllowedNum)
2518 Values += " or ";
2519 else if (AllowedCnt + 1 != TotalAllowedNum)
2520 Values += Sep;
2521 ++AllowedCnt;
2522 }
2523 }
2524 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2525 diag::err_omp_unnamed_if_clause)
2526 << (TotalAllowedNum > 1) << Values;
2527 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002528 for (auto Loc : NameModifierLoc) {
2529 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2530 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002531 ErrorFound = true;
2532 }
2533 return ErrorFound;
2534}
2535
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002536StmtResult Sema::ActOnOpenMPExecutableDirective(
2537 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2538 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2539 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002540 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002541 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2542 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002543 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002544
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002545 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002546 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002547 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002548 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002549 if (AStmt) {
2550 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2551
2552 // Check default data sharing attributes for referenced variables.
2553 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2554 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2555 if (DSAChecker.isErrorFound())
2556 return StmtError();
2557 // Generate list of implicitly defined firstprivate variables.
2558 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002559
2560 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2561 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2562 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2563 SourceLocation(), SourceLocation())) {
2564 ClausesWithImplicit.push_back(Implicit);
2565 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2566 DSAChecker.getImplicitFirstprivate().size();
2567 } else
2568 ErrorFound = true;
2569 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002570 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002571
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002572 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002573 switch (Kind) {
2574 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002575 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2576 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002577 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002578 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002579 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002580 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2581 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002582 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002583 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002584 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2585 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002586 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002587 case OMPD_for_simd:
2588 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2589 EndLoc, VarsWithInheritedDSA);
2590 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002591 case OMPD_sections:
2592 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2593 EndLoc);
2594 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002595 case OMPD_section:
2596 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002597 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002598 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2599 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002600 case OMPD_single:
2601 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2602 EndLoc);
2603 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002604 case OMPD_master:
2605 assert(ClausesWithImplicit.empty() &&
2606 "No clauses are allowed for 'omp master' directive");
2607 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2608 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002609 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002610 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2611 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002612 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002613 case OMPD_parallel_for:
2614 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2615 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002616 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002617 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002618 case OMPD_parallel_for_simd:
2619 Res = ActOnOpenMPParallelForSimdDirective(
2620 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002621 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002622 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002623 case OMPD_parallel_sections:
2624 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2625 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002626 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002627 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002628 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002629 Res =
2630 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002631 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002632 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002633 case OMPD_taskyield:
2634 assert(ClausesWithImplicit.empty() &&
2635 "No clauses are allowed for 'omp taskyield' directive");
2636 assert(AStmt == nullptr &&
2637 "No associated statement allowed for 'omp taskyield' directive");
2638 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2639 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002640 case OMPD_barrier:
2641 assert(ClausesWithImplicit.empty() &&
2642 "No clauses are allowed for 'omp barrier' directive");
2643 assert(AStmt == nullptr &&
2644 "No associated statement allowed for 'omp barrier' directive");
2645 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2646 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002647 case OMPD_taskwait:
2648 assert(ClausesWithImplicit.empty() &&
2649 "No clauses are allowed for 'omp taskwait' directive");
2650 assert(AStmt == nullptr &&
2651 "No associated statement allowed for 'omp taskwait' directive");
2652 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2653 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002654 case OMPD_taskgroup:
2655 assert(ClausesWithImplicit.empty() &&
2656 "No clauses are allowed for 'omp taskgroup' directive");
2657 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2658 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002659 case OMPD_flush:
2660 assert(AStmt == nullptr &&
2661 "No associated statement allowed for 'omp flush' directive");
2662 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2663 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002664 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002665 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2666 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002667 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002668 case OMPD_atomic:
2669 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2670 EndLoc);
2671 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002672 case OMPD_teams:
2673 Res =
2674 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2675 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002676 case OMPD_target:
2677 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2678 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002679 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002680 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002681 case OMPD_cancellation_point:
2682 assert(ClausesWithImplicit.empty() &&
2683 "No clauses are allowed for 'omp cancellation point' directive");
2684 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2685 "cancellation point' directive");
2686 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2687 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002688 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002689 assert(AStmt == nullptr &&
2690 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002691 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2692 CancelRegion);
2693 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002694 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002695 case OMPD_target_data:
2696 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2697 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002698 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002699 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002700 case OMPD_taskloop:
2701 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2702 EndLoc, VarsWithInheritedDSA);
2703 AllowedNameModifiers.push_back(OMPD_taskloop);
2704 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002705 case OMPD_taskloop_simd:
2706 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2707 EndLoc, VarsWithInheritedDSA);
2708 AllowedNameModifiers.push_back(OMPD_taskloop);
2709 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002710 case OMPD_distribute:
2711 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2712 EndLoc, VarsWithInheritedDSA);
2713 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002714 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002715 llvm_unreachable("OpenMP Directive is not allowed");
2716 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002717 llvm_unreachable("Unknown OpenMP directive");
2718 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002719
Alexey Bataev4acb8592014-07-07 13:01:15 +00002720 for (auto P : VarsWithInheritedDSA) {
2721 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2722 << P.first << P.second->getSourceRange();
2723 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002724 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2725
2726 if (!AllowedNameModifiers.empty())
2727 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2728 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002729
Alexey Bataeved09d242014-05-28 05:53:51 +00002730 if (ErrorFound)
2731 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002732 return Res;
2733}
2734
2735StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2736 Stmt *AStmt,
2737 SourceLocation StartLoc,
2738 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002739 if (!AStmt)
2740 return StmtError();
2741
Alexey Bataev9959db52014-05-06 10:08:46 +00002742 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2743 // 1.2.2 OpenMP Language Terminology
2744 // Structured block - An executable statement with a single entry at the
2745 // top and a single exit at the bottom.
2746 // The point of exit cannot be a branch out of the structured block.
2747 // longjmp() and throw() must not violate the entry/exit criteria.
2748 CS->getCapturedDecl()->setNothrow();
2749
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002750 getCurFunction()->setHasBranchProtectedScope();
2751
Alexey Bataev25e5b442015-09-15 12:52:43 +00002752 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2753 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002754}
2755
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002756namespace {
2757/// \brief Helper class for checking canonical form of the OpenMP loops and
2758/// extracting iteration space of each loop in the loop nest, that will be used
2759/// for IR generation.
2760class OpenMPIterationSpaceChecker {
2761 /// \brief Reference to Sema.
2762 Sema &SemaRef;
2763 /// \brief A location for diagnostics (when there is no some better location).
2764 SourceLocation DefaultLoc;
2765 /// \brief A location for diagnostics (when increment is not compatible).
2766 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002767 /// \brief A source location for referring to loop init later.
2768 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002769 /// \brief A source location for referring to condition later.
2770 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002771 /// \brief A source location for referring to increment later.
2772 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002773 /// \brief Loop variable.
2774 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002775 /// \brief Reference to loop variable.
2776 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002777 /// \brief Lower bound (initializer for the var).
2778 Expr *LB;
2779 /// \brief Upper bound.
2780 Expr *UB;
2781 /// \brief Loop step (increment).
2782 Expr *Step;
2783 /// \brief This flag is true when condition is one of:
2784 /// Var < UB
2785 /// Var <= UB
2786 /// UB > Var
2787 /// UB >= Var
2788 bool TestIsLessOp;
2789 /// \brief This flag is true when condition is strict ( < or > ).
2790 bool TestIsStrictOp;
2791 /// \brief This flag is true when step is subtracted on each iteration.
2792 bool SubtractStep;
2793
2794public:
2795 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2796 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002797 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2798 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002799 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2800 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002801 /// \brief Check init-expr for canonical loop form and save loop counter
2802 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002803 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002804 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2805 /// for less/greater and for strict/non-strict comparison.
2806 bool CheckCond(Expr *S);
2807 /// \brief Check incr-expr for canonical loop form and return true if it
2808 /// does not conform, otherwise save loop step (#Step).
2809 bool CheckInc(Expr *S);
2810 /// \brief Return the loop counter variable.
2811 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002812 /// \brief Return the reference expression to loop counter variable.
2813 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002814 /// \brief Source range of the loop init.
2815 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2816 /// \brief Source range of the loop condition.
2817 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2818 /// \brief Source range of the loop increment.
2819 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2820 /// \brief True if the step should be subtracted.
2821 bool ShouldSubtractStep() const { return SubtractStep; }
2822 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002823 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002824 /// \brief Build the precondition expression for the loops.
2825 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002826 /// \brief Build reference expression to the counter be used for codegen.
2827 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002828 /// \brief Build reference expression to the private counter be used for
2829 /// codegen.
2830 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002831 /// \brief Build initization of the counter be used for codegen.
2832 Expr *BuildCounterInit() const;
2833 /// \brief Build step of the counter be used for codegen.
2834 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002835 /// \brief Return true if any expression is dependent.
2836 bool Dependent() const;
2837
2838private:
2839 /// \brief Check the right-hand side of an assignment in the increment
2840 /// expression.
2841 bool CheckIncRHS(Expr *RHS);
2842 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002843 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002844 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002845 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002846 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002847 /// \brief Helper to set loop increment.
2848 bool SetStep(Expr *NewStep, bool Subtract);
2849};
2850
2851bool OpenMPIterationSpaceChecker::Dependent() const {
2852 if (!Var) {
2853 assert(!LB && !UB && !Step);
2854 return false;
2855 }
2856 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2857 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2858}
2859
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002860template <typename T>
2861static T *getExprAsWritten(T *E) {
2862 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2863 E = ExprTemp->getSubExpr();
2864
2865 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2866 E = MTE->GetTemporaryExpr();
2867
2868 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2869 E = Binder->getSubExpr();
2870
2871 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2872 E = ICE->getSubExprAsWritten();
2873 return E->IgnoreParens();
2874}
2875
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002876bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2877 DeclRefExpr *NewVarRefExpr,
2878 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002879 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002880 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2881 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002882 if (!NewVar || !NewLB)
2883 return true;
2884 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002885 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002886 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2887 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002888 if ((Ctor->isCopyOrMoveConstructor() ||
2889 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2890 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002891 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002892 LB = NewLB;
2893 return false;
2894}
2895
2896bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002897 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002898 // State consistency checking to ensure correct usage.
2899 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2900 !TestIsLessOp && !TestIsStrictOp);
2901 if (!NewUB)
2902 return true;
2903 UB = NewUB;
2904 TestIsLessOp = LessOp;
2905 TestIsStrictOp = StrictOp;
2906 ConditionSrcRange = SR;
2907 ConditionLoc = SL;
2908 return false;
2909}
2910
2911bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2912 // State consistency checking to ensure correct usage.
2913 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2914 if (!NewStep)
2915 return true;
2916 if (!NewStep->isValueDependent()) {
2917 // Check that the step is integer expression.
2918 SourceLocation StepLoc = NewStep->getLocStart();
2919 ExprResult Val =
2920 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2921 if (Val.isInvalid())
2922 return true;
2923 NewStep = Val.get();
2924
2925 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2926 // If test-expr is of form var relational-op b and relational-op is < or
2927 // <= then incr-expr must cause var to increase on each iteration of the
2928 // loop. If test-expr is of form var relational-op b and relational-op is
2929 // > or >= then incr-expr must cause var to decrease on each iteration of
2930 // the loop.
2931 // If test-expr is of form b relational-op var and relational-op is < or
2932 // <= then incr-expr must cause var to decrease on each iteration of the
2933 // loop. If test-expr is of form b relational-op var and relational-op is
2934 // > or >= then incr-expr must cause var to increase on each iteration of
2935 // the loop.
2936 llvm::APSInt Result;
2937 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2938 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2939 bool IsConstNeg =
2940 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002941 bool IsConstPos =
2942 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002943 bool IsConstZero = IsConstant && !Result.getBoolValue();
2944 if (UB && (IsConstZero ||
2945 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002946 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002947 SemaRef.Diag(NewStep->getExprLoc(),
2948 diag::err_omp_loop_incr_not_compatible)
2949 << Var << TestIsLessOp << NewStep->getSourceRange();
2950 SemaRef.Diag(ConditionLoc,
2951 diag::note_omp_loop_cond_requres_compatible_incr)
2952 << TestIsLessOp << ConditionSrcRange;
2953 return true;
2954 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002955 if (TestIsLessOp == Subtract) {
2956 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2957 NewStep).get();
2958 Subtract = !Subtract;
2959 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002960 }
2961
2962 Step = NewStep;
2963 SubtractStep = Subtract;
2964 return false;
2965}
2966
Alexey Bataev9c821032015-04-30 04:23:23 +00002967bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002968 // Check init-expr for canonical loop form and save loop counter
2969 // variable - #Var and its initialization value - #LB.
2970 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2971 // var = lb
2972 // integer-type var = lb
2973 // random-access-iterator-type var = lb
2974 // pointer-type var = lb
2975 //
2976 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002977 if (EmitDiags) {
2978 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2979 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002980 return true;
2981 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002982 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002983 if (Expr *E = dyn_cast<Expr>(S))
2984 S = E->IgnoreParens();
2985 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2986 if (BO->getOpcode() == BO_Assign)
2987 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002988 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002989 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002990 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2991 if (DS->isSingleDecl()) {
2992 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002993 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002994 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002995 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002996 SemaRef.Diag(S->getLocStart(),
2997 diag::ext_omp_loop_not_canonical_init)
2998 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002999 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003000 }
3001 }
3002 }
3003 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3004 if (CE->getOperator() == OO_Equal)
3005 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003006 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3007 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003008
Alexey Bataev9c821032015-04-30 04:23:23 +00003009 if (EmitDiags) {
3010 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3011 << S->getSourceRange();
3012 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003013 return true;
3014}
3015
Alexey Bataev23b69422014-06-18 07:08:49 +00003016/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003017/// variable (which may be the loop variable) if possible.
3018static const VarDecl *GetInitVarDecl(const Expr *E) {
3019 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003020 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003021 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003022 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3023 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003024 if ((Ctor->isCopyOrMoveConstructor() ||
3025 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3026 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003027 E = CE->getArg(0)->IgnoreParenImpCasts();
3028 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3029 if (!DRE)
3030 return nullptr;
3031 return dyn_cast<VarDecl>(DRE->getDecl());
3032}
3033
3034bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3035 // Check test-expr for canonical form, save upper-bound UB, flags for
3036 // less/greater and for strict/non-strict comparison.
3037 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3038 // var relational-op b
3039 // b relational-op var
3040 //
3041 if (!S) {
3042 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3043 return true;
3044 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003045 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003046 SourceLocation CondLoc = S->getLocStart();
3047 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3048 if (BO->isRelationalOp()) {
3049 if (GetInitVarDecl(BO->getLHS()) == Var)
3050 return SetUB(BO->getRHS(),
3051 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3052 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3053 BO->getSourceRange(), BO->getOperatorLoc());
3054 if (GetInitVarDecl(BO->getRHS()) == Var)
3055 return SetUB(BO->getLHS(),
3056 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3057 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3058 BO->getSourceRange(), BO->getOperatorLoc());
3059 }
3060 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3061 if (CE->getNumArgs() == 2) {
3062 auto Op = CE->getOperator();
3063 switch (Op) {
3064 case OO_Greater:
3065 case OO_GreaterEqual:
3066 case OO_Less:
3067 case OO_LessEqual:
3068 if (GetInitVarDecl(CE->getArg(0)) == Var)
3069 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3070 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3071 CE->getOperatorLoc());
3072 if (GetInitVarDecl(CE->getArg(1)) == Var)
3073 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3074 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3075 CE->getOperatorLoc());
3076 break;
3077 default:
3078 break;
3079 }
3080 }
3081 }
3082 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3083 << S->getSourceRange() << Var;
3084 return true;
3085}
3086
3087bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3088 // RHS of canonical loop form increment can be:
3089 // var + incr
3090 // incr + var
3091 // var - incr
3092 //
3093 RHS = RHS->IgnoreParenImpCasts();
3094 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3095 if (BO->isAdditiveOp()) {
3096 bool IsAdd = BO->getOpcode() == BO_Add;
3097 if (GetInitVarDecl(BO->getLHS()) == Var)
3098 return SetStep(BO->getRHS(), !IsAdd);
3099 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3100 return SetStep(BO->getLHS(), false);
3101 }
3102 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3103 bool IsAdd = CE->getOperator() == OO_Plus;
3104 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3105 if (GetInitVarDecl(CE->getArg(0)) == Var)
3106 return SetStep(CE->getArg(1), !IsAdd);
3107 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3108 return SetStep(CE->getArg(0), false);
3109 }
3110 }
3111 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3112 << RHS->getSourceRange() << Var;
3113 return true;
3114}
3115
3116bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3117 // Check incr-expr for canonical loop form and return true if it
3118 // does not conform.
3119 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3120 // ++var
3121 // var++
3122 // --var
3123 // var--
3124 // var += incr
3125 // var -= incr
3126 // var = var + incr
3127 // var = incr + var
3128 // var = var - incr
3129 //
3130 if (!S) {
3131 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3132 return true;
3133 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003134 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003135 S = S->IgnoreParens();
3136 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3137 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3138 return SetStep(
3139 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3140 (UO->isDecrementOp() ? -1 : 1)).get(),
3141 false);
3142 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3143 switch (BO->getOpcode()) {
3144 case BO_AddAssign:
3145 case BO_SubAssign:
3146 if (GetInitVarDecl(BO->getLHS()) == Var)
3147 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3148 break;
3149 case BO_Assign:
3150 if (GetInitVarDecl(BO->getLHS()) == Var)
3151 return CheckIncRHS(BO->getRHS());
3152 break;
3153 default:
3154 break;
3155 }
3156 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3157 switch (CE->getOperator()) {
3158 case OO_PlusPlus:
3159 case OO_MinusMinus:
3160 if (GetInitVarDecl(CE->getArg(0)) == Var)
3161 return SetStep(
3162 SemaRef.ActOnIntegerConstant(
3163 CE->getLocStart(),
3164 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3165 false);
3166 break;
3167 case OO_PlusEqual:
3168 case OO_MinusEqual:
3169 if (GetInitVarDecl(CE->getArg(0)) == Var)
3170 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3171 break;
3172 case OO_Equal:
3173 if (GetInitVarDecl(CE->getArg(0)) == Var)
3174 return CheckIncRHS(CE->getArg(1));
3175 break;
3176 default:
3177 break;
3178 }
3179 }
3180 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3181 << S->getSourceRange() << Var;
3182 return true;
3183}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003184
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003185namespace {
3186// Transform variables declared in GNU statement expressions to new ones to
3187// avoid crash on codegen.
3188class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3189 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3190
3191public:
3192 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3193
3194 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3195 if (auto *VD = cast<VarDecl>(D))
3196 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3197 !isa<ImplicitParamDecl>(D)) {
3198 auto *NewVD = VarDecl::Create(
3199 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3200 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3201 VD->getTypeSourceInfo(), VD->getStorageClass());
3202 NewVD->setTSCSpec(VD->getTSCSpec());
3203 NewVD->setInit(VD->getInit());
3204 NewVD->setInitStyle(VD->getInitStyle());
3205 NewVD->setExceptionVariable(VD->isExceptionVariable());
3206 NewVD->setNRVOVariable(VD->isNRVOVariable());
3207 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3208 NewVD->setConstexpr(VD->isConstexpr());
3209 NewVD->setInitCapture(VD->isInitCapture());
3210 NewVD->setPreviousDeclInSameBlockScope(
3211 VD->isPreviousDeclInSameBlockScope());
3212 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003213 if (VD->hasAttrs())
3214 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003215 transformedLocalDecl(VD, NewVD);
3216 return NewVD;
3217 }
3218 return BaseTransform::TransformDefinition(Loc, D);
3219 }
3220
3221 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3222 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3223 if (E->getDecl() != NewD) {
3224 NewD->setReferenced();
3225 NewD->markUsed(SemaRef.Context);
3226 return DeclRefExpr::Create(
3227 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3228 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3229 E->getNameInfo(), E->getType(), E->getValueKind());
3230 }
3231 return BaseTransform::TransformDeclRefExpr(E);
3232 }
3233};
3234}
3235
Alexander Musmana5f070a2014-10-01 06:03:56 +00003236/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003237Expr *
3238OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3239 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003240 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003241 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003242 auto VarType = Var->getType().getNonReferenceType();
3243 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003244 SemaRef.getLangOpts().CPlusPlus) {
3245 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003246 auto *UBExpr = TestIsLessOp ? UB : LB;
3247 auto *LBExpr = TestIsLessOp ? LB : UB;
3248 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3249 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3250 if (!Upper || !Lower)
3251 return nullptr;
3252 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3253 Sema::AA_Converting,
3254 /*AllowExplicit=*/true)
3255 .get();
3256 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3257 Sema::AA_Converting,
3258 /*AllowExplicit=*/true)
3259 .get();
3260 if (!Upper || !Lower)
3261 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003262
3263 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3264
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003265 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003266 // BuildBinOp already emitted error, this one is to point user to upper
3267 // and lower bound, and to tell what is passed to 'operator-'.
3268 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3269 << Upper->getSourceRange() << Lower->getSourceRange();
3270 return nullptr;
3271 }
3272 }
3273
3274 if (!Diff.isUsable())
3275 return nullptr;
3276
3277 // Upper - Lower [- 1]
3278 if (TestIsStrictOp)
3279 Diff = SemaRef.BuildBinOp(
3280 S, DefaultLoc, BO_Sub, Diff.get(),
3281 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3282 if (!Diff.isUsable())
3283 return nullptr;
3284
3285 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003286 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3287 if (NewStep.isInvalid())
3288 return nullptr;
3289 NewStep = SemaRef.PerformImplicitConversion(
3290 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3291 /*AllowExplicit=*/true);
3292 if (NewStep.isInvalid())
3293 return nullptr;
3294 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003295 if (!Diff.isUsable())
3296 return nullptr;
3297
3298 // Parentheses (for dumping/debugging purposes only).
3299 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3300 if (!Diff.isUsable())
3301 return nullptr;
3302
3303 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003304 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3305 if (NewStep.isInvalid())
3306 return nullptr;
3307 NewStep = SemaRef.PerformImplicitConversion(
3308 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3309 /*AllowExplicit=*/true);
3310 if (NewStep.isInvalid())
3311 return nullptr;
3312 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003313 if (!Diff.isUsable())
3314 return nullptr;
3315
Alexander Musman174b3ca2014-10-06 11:16:29 +00003316 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003317 QualType Type = Diff.get()->getType();
3318 auto &C = SemaRef.Context;
3319 bool UseVarType = VarType->hasIntegerRepresentation() &&
3320 C.getTypeSize(Type) > C.getTypeSize(VarType);
3321 if (!Type->isIntegerType() || UseVarType) {
3322 unsigned NewSize =
3323 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3324 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3325 : Type->hasSignedIntegerRepresentation();
3326 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3327 Diff = SemaRef.PerformImplicitConversion(
3328 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3329 if (!Diff.isUsable())
3330 return nullptr;
3331 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003332 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003333 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3334 if (NewSize != C.getTypeSize(Type)) {
3335 if (NewSize < C.getTypeSize(Type)) {
3336 assert(NewSize == 64 && "incorrect loop var size");
3337 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3338 << InitSrcRange << ConditionSrcRange;
3339 }
3340 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003341 NewSize, Type->hasSignedIntegerRepresentation() ||
3342 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003343 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3344 Sema::AA_Converting, true);
3345 if (!Diff.isUsable())
3346 return nullptr;
3347 }
3348 }
3349
Alexander Musmana5f070a2014-10-01 06:03:56 +00003350 return Diff.get();
3351}
3352
Alexey Bataev62dbb972015-04-22 11:59:37 +00003353Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3354 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3355 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3356 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003357 TransformToNewDefs Transform(SemaRef);
3358
3359 auto NewLB = Transform.TransformExpr(LB);
3360 auto NewUB = Transform.TransformExpr(UB);
3361 if (NewLB.isInvalid() || NewUB.isInvalid())
3362 return Cond;
3363 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3364 Sema::AA_Converting,
3365 /*AllowExplicit=*/true);
3366 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3367 Sema::AA_Converting,
3368 /*AllowExplicit=*/true);
3369 if (NewLB.isInvalid() || NewUB.isInvalid())
3370 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003371 auto CondExpr = SemaRef.BuildBinOp(
3372 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3373 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003374 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003375 if (CondExpr.isUsable()) {
3376 CondExpr = SemaRef.PerformImplicitConversion(
3377 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3378 /*AllowExplicit=*/true);
3379 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003380 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3381 // Otherwise use original loop conditon and evaluate it in runtime.
3382 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3383}
3384
Alexander Musmana5f070a2014-10-01 06:03:56 +00003385/// \brief Build reference expression to the counter be used for codegen.
3386Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003387 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3388 DefaultLoc);
3389}
3390
3391Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3392 if (Var && !Var->isInvalidDecl()) {
3393 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003394 auto *PrivateVar =
3395 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3396 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003397 if (PrivateVar->isInvalidDecl())
3398 return nullptr;
3399 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3400 }
3401 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003402}
3403
3404/// \brief Build initization of the counter be used for codegen.
3405Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3406
3407/// \brief Build step of the counter be used for codegen.
3408Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3409
3410/// \brief Iteration space of a single for loop.
3411struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003412 /// \brief Condition of the loop.
3413 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003414 /// \brief This expression calculates the number of iterations in the loop.
3415 /// It is always possible to calculate it before starting the loop.
3416 Expr *NumIterations;
3417 /// \brief The loop counter variable.
3418 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003419 /// \brief Private loop counter variable.
3420 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003421 /// \brief This is initializer for the initial value of #CounterVar.
3422 Expr *CounterInit;
3423 /// \brief This is step for the #CounterVar used to generate its update:
3424 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3425 Expr *CounterStep;
3426 /// \brief Should step be subtracted?
3427 bool Subtract;
3428 /// \brief Source range of the loop init.
3429 SourceRange InitSrcRange;
3430 /// \brief Source range of the loop condition.
3431 SourceRange CondSrcRange;
3432 /// \brief Source range of the loop increment.
3433 SourceRange IncSrcRange;
3434};
3435
Alexey Bataev23b69422014-06-18 07:08:49 +00003436} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003437
Alexey Bataev9c821032015-04-30 04:23:23 +00003438void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3439 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3440 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003441 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3442 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003443 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3444 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003445 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003446 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003447 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003448 }
3449}
3450
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003451/// \brief Called on a for stmt to check and extract its iteration space
3452/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003453static bool CheckOpenMPIterationSpace(
3454 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3455 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003456 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003457 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3458 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003459 // OpenMP [2.6, Canonical Loop Form]
3460 // for (init-expr; test-expr; incr-expr) structured-block
3461 auto For = dyn_cast_or_null<ForStmt>(S);
3462 if (!For) {
3463 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003464 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3465 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3466 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3467 if (NestedLoopCount > 1) {
3468 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3469 SemaRef.Diag(DSA.getConstructLoc(),
3470 diag::note_omp_collapse_ordered_expr)
3471 << 2 << CollapseLoopCountExpr->getSourceRange()
3472 << OrderedLoopCountExpr->getSourceRange();
3473 else if (CollapseLoopCountExpr)
3474 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3475 diag::note_omp_collapse_ordered_expr)
3476 << 0 << CollapseLoopCountExpr->getSourceRange();
3477 else
3478 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3479 diag::note_omp_collapse_ordered_expr)
3480 << 1 << OrderedLoopCountExpr->getSourceRange();
3481 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003482 return true;
3483 }
3484 assert(For->getBody());
3485
3486 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3487
3488 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003489 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003490 if (ISC.CheckInit(Init)) {
3491 return true;
3492 }
3493
3494 bool HasErrors = false;
3495
3496 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003497 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003498
3499 // OpenMP [2.6, Canonical Loop Form]
3500 // Var is one of the following:
3501 // A variable of signed or unsigned integer type.
3502 // For C++, a variable of a random access iterator type.
3503 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003504 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003505 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3506 !VarType->isPointerType() &&
3507 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3508 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3509 << SemaRef.getLangOpts().CPlusPlus;
3510 HasErrors = true;
3511 }
3512
Alexey Bataev4acb8592014-07-07 13:01:15 +00003513 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3514 // Construct
3515 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3516 // parallel for construct is (are) private.
3517 // The loop iteration variable in the associated for-loop of a simd construct
3518 // with just one associated for-loop is linear with a constant-linear-step
3519 // that is the increment of the associated for-loop.
3520 // Exclude loop var from the list of variables with implicitly defined data
3521 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003522 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003523
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003524 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3525 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003526 // The loop iteration variable in the associated for-loop of a simd construct
3527 // with just one associated for-loop may be listed in a linear clause with a
3528 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003529 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3530 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003531 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003532 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3533 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3534 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003535 auto PredeterminedCKind =
3536 isOpenMPSimdDirective(DKind)
3537 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3538 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003539 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003540 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003541 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003542 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003543 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003544 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3545 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003546 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003547 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3548 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003549 if (DVar.RefExpr == nullptr)
3550 DVar.CKind = PredeterminedCKind;
3551 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003552 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003553 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003554 // Make the loop iteration variable private (for worksharing constructs),
3555 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003556 // lastprivate (for simd directives with several collapsed or ordered
3557 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003558 if (DVar.CKind == OMPC_unknown)
3559 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3560 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003561 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003562 }
3563
Alexey Bataev7ff55242014-06-19 09:13:45 +00003564 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003565
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003566 // Check test-expr.
3567 HasErrors |= ISC.CheckCond(For->getCond());
3568
3569 // Check incr-expr.
3570 HasErrors |= ISC.CheckInc(For->getInc());
3571
Alexander Musmana5f070a2014-10-01 06:03:56 +00003572 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003573 return HasErrors;
3574
Alexander Musmana5f070a2014-10-01 06:03:56 +00003575 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003576 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003577 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003578 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003579 isOpenMPTaskLoopDirective(DKind) ||
3580 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003581 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003582 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003583 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3584 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3585 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3586 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3587 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3588 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3589
Alexey Bataev62dbb972015-04-22 11:59:37 +00003590 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3591 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003592 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003593 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003594 ResultIterSpace.CounterInit == nullptr ||
3595 ResultIterSpace.CounterStep == nullptr);
3596
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003597 return HasErrors;
3598}
3599
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003600/// \brief Build 'VarRef = Start.
3601static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3602 ExprResult VarRef, ExprResult Start) {
3603 TransformToNewDefs Transform(SemaRef);
3604 // Build 'VarRef = Start.
3605 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3606 if (NewStart.isInvalid())
3607 return ExprError();
3608 NewStart = SemaRef.PerformImplicitConversion(
3609 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3610 Sema::AA_Converting,
3611 /*AllowExplicit=*/true);
3612 if (NewStart.isInvalid())
3613 return ExprError();
3614 NewStart = SemaRef.PerformImplicitConversion(
3615 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3616 /*AllowExplicit=*/true);
3617 if (!NewStart.isUsable())
3618 return ExprError();
3619
3620 auto Init =
3621 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3622 return Init;
3623}
3624
Alexander Musmana5f070a2014-10-01 06:03:56 +00003625/// \brief Build 'VarRef = Start + Iter * Step'.
3626static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3627 SourceLocation Loc, ExprResult VarRef,
3628 ExprResult Start, ExprResult Iter,
3629 ExprResult Step, bool Subtract) {
3630 // Add parentheses (for debugging purposes only).
3631 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3632 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3633 !Step.isUsable())
3634 return ExprError();
3635
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003636 TransformToNewDefs Transform(SemaRef);
3637 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3638 if (NewStep.isInvalid())
3639 return ExprError();
3640 NewStep = SemaRef.PerformImplicitConversion(
3641 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3642 Sema::AA_Converting,
3643 /*AllowExplicit=*/true);
3644 if (NewStep.isInvalid())
3645 return ExprError();
3646 ExprResult Update =
3647 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003648 if (!Update.isUsable())
3649 return ExprError();
3650
3651 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003652 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3653 if (NewStart.isInvalid())
3654 return ExprError();
3655 NewStart = SemaRef.PerformImplicitConversion(
3656 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3657 Sema::AA_Converting,
3658 /*AllowExplicit=*/true);
3659 if (NewStart.isInvalid())
3660 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003661 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003662 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003663 if (!Update.isUsable())
3664 return ExprError();
3665
3666 Update = SemaRef.PerformImplicitConversion(
3667 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3668 if (!Update.isUsable())
3669 return ExprError();
3670
3671 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3672 return Update;
3673}
3674
3675/// \brief Convert integer expression \a E to make it have at least \a Bits
3676/// bits.
3677static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3678 Sema &SemaRef) {
3679 if (E == nullptr)
3680 return ExprError();
3681 auto &C = SemaRef.Context;
3682 QualType OldType = E->getType();
3683 unsigned HasBits = C.getTypeSize(OldType);
3684 if (HasBits >= Bits)
3685 return ExprResult(E);
3686 // OK to convert to signed, because new type has more bits than old.
3687 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3688 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3689 true);
3690}
3691
3692/// \brief Check if the given expression \a E is a constant integer that fits
3693/// into \a Bits bits.
3694static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3695 if (E == nullptr)
3696 return false;
3697 llvm::APSInt Result;
3698 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3699 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3700 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003701}
3702
3703/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003704/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3705/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003706static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003707CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3708 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3709 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003710 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003711 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003712 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003713 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003714 // Found 'collapse' clause - calculate collapse number.
3715 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003716 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003717 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003718 }
3719 if (OrderedLoopCountExpr) {
3720 // Found 'ordered' clause - calculate collapse number.
3721 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003722 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3723 if (Result.getLimitedValue() < NestedLoopCount) {
3724 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3725 diag::err_omp_wrong_ordered_loop_count)
3726 << OrderedLoopCountExpr->getSourceRange();
3727 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3728 diag::note_collapse_loop_count)
3729 << CollapseLoopCountExpr->getSourceRange();
3730 }
3731 NestedLoopCount = Result.getLimitedValue();
3732 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003733 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003734 // This is helper routine for loop directives (e.g., 'for', 'simd',
3735 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003736 SmallVector<LoopIterationSpace, 4> IterSpaces;
3737 IterSpaces.resize(NestedLoopCount);
3738 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003739 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003740 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003741 NestedLoopCount, CollapseLoopCountExpr,
3742 OrderedLoopCountExpr, VarsWithImplicitDSA,
3743 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003744 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003745 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003746 // OpenMP [2.8.1, simd construct, Restrictions]
3747 // All loops associated with the construct must be perfectly nested; that
3748 // is, there must be no intervening code nor any OpenMP directive between
3749 // any two loops.
3750 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003751 }
3752
Alexander Musmana5f070a2014-10-01 06:03:56 +00003753 Built.clear(/* size */ NestedLoopCount);
3754
3755 if (SemaRef.CurContext->isDependentContext())
3756 return NestedLoopCount;
3757
3758 // An example of what is generated for the following code:
3759 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003760 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003761 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003762 // for (k = 0; k < NK; ++k)
3763 // for (j = J0; j < NJ; j+=2) {
3764 // <loop body>
3765 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003766 //
3767 // We generate the code below.
3768 // Note: the loop body may be outlined in CodeGen.
3769 // Note: some counters may be C++ classes, operator- is used to find number of
3770 // iterations and operator+= to calculate counter value.
3771 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3772 // or i64 is currently supported).
3773 //
3774 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3775 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3776 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3777 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3778 // // similar updates for vars in clauses (e.g. 'linear')
3779 // <loop body (using local i and j)>
3780 // }
3781 // i = NI; // assign final values of counters
3782 // j = NJ;
3783 //
3784
3785 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3786 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003787 // Precondition tests if there is at least one iteration (all conditions are
3788 // true).
3789 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003790 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003791 ExprResult LastIteration32 = WidenIterationCount(
3792 32 /* Bits */, SemaRef.PerformImplicitConversion(
3793 N0->IgnoreImpCasts(), N0->getType(),
3794 Sema::AA_Converting, /*AllowExplicit=*/true)
3795 .get(),
3796 SemaRef);
3797 ExprResult LastIteration64 = WidenIterationCount(
3798 64 /* Bits */, SemaRef.PerformImplicitConversion(
3799 N0->IgnoreImpCasts(), N0->getType(),
3800 Sema::AA_Converting, /*AllowExplicit=*/true)
3801 .get(),
3802 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003803
3804 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3805 return NestedLoopCount;
3806
3807 auto &C = SemaRef.Context;
3808 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3809
3810 Scope *CurScope = DSA.getCurScope();
3811 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003812 if (PreCond.isUsable()) {
3813 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3814 PreCond.get(), IterSpaces[Cnt].PreCond);
3815 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003816 auto N = IterSpaces[Cnt].NumIterations;
3817 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3818 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003819 LastIteration32 = SemaRef.BuildBinOp(
3820 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3821 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3822 Sema::AA_Converting,
3823 /*AllowExplicit=*/true)
3824 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003825 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003826 LastIteration64 = SemaRef.BuildBinOp(
3827 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3828 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3829 Sema::AA_Converting,
3830 /*AllowExplicit=*/true)
3831 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003832 }
3833
3834 // Choose either the 32-bit or 64-bit version.
3835 ExprResult LastIteration = LastIteration64;
3836 if (LastIteration32.isUsable() &&
3837 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3838 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3839 FitsInto(
3840 32 /* Bits */,
3841 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3842 LastIteration64.get(), SemaRef)))
3843 LastIteration = LastIteration32;
3844
3845 if (!LastIteration.isUsable())
3846 return 0;
3847
3848 // Save the number of iterations.
3849 ExprResult NumIterations = LastIteration;
3850 {
3851 LastIteration = SemaRef.BuildBinOp(
3852 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3853 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3854 if (!LastIteration.isUsable())
3855 return 0;
3856 }
3857
3858 // Calculate the last iteration number beforehand instead of doing this on
3859 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3860 llvm::APSInt Result;
3861 bool IsConstant =
3862 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3863 ExprResult CalcLastIteration;
3864 if (!IsConstant) {
3865 SourceLocation SaveLoc;
3866 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003867 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003868 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003869 ExprResult SaveRef = buildDeclRefExpr(
3870 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003871 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3872 SaveRef.get(), LastIteration.get());
3873 LastIteration = SaveRef;
3874
3875 // Prepare SaveRef + 1.
3876 NumIterations = SemaRef.BuildBinOp(
3877 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3878 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3879 if (!NumIterations.isUsable())
3880 return 0;
3881 }
3882
3883 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3884
Alexander Musmanc6388682014-12-15 07:07:06 +00003885 QualType VType = LastIteration.get()->getType();
3886 // Build variables passed into runtime, nesessary for worksharing directives.
3887 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003888 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3889 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003890 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003891 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3892 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003893 SemaRef.AddInitializerToDecl(
3894 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3895 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3896
3897 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003898 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3899 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003900 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3901 /*DirectInit*/ false,
3902 /*TypeMayContainAuto*/ false);
3903
3904 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3905 // This will be used to implement clause 'lastprivate'.
3906 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003907 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3908 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003909 SemaRef.AddInitializerToDecl(
3910 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3911 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3912
3913 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003914 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3915 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003916 SemaRef.AddInitializerToDecl(
3917 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3918 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3919
3920 // Build expression: UB = min(UB, LastIteration)
3921 // It is nesessary for CodeGen of directives with static scheduling.
3922 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3923 UB.get(), LastIteration.get());
3924 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3925 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3926 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3927 CondOp.get());
3928 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3929 }
3930
3931 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003932 ExprResult IV;
3933 ExprResult Init;
3934 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003935 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3936 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003937 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003938 isOpenMPTaskLoopDirective(DKind) ||
3939 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003940 ? LB.get()
3941 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3942 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3943 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003944 }
3945
Alexander Musmanc6388682014-12-15 07:07:06 +00003946 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003947 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003948 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003949 (isOpenMPWorksharingDirective(DKind) ||
3950 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003951 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3952 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3953 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003954
3955 // Loop increment (IV = IV + 1)
3956 SourceLocation IncLoc;
3957 ExprResult Inc =
3958 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3959 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3960 if (!Inc.isUsable())
3961 return 0;
3962 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003963 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3964 if (!Inc.isUsable())
3965 return 0;
3966
3967 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3968 // Used for directives with static scheduling.
3969 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003970 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3971 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003972 // LB + ST
3973 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3974 if (!NextLB.isUsable())
3975 return 0;
3976 // LB = LB + ST
3977 NextLB =
3978 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3979 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3980 if (!NextLB.isUsable())
3981 return 0;
3982 // UB + ST
3983 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3984 if (!NextUB.isUsable())
3985 return 0;
3986 // UB = UB + ST
3987 NextUB =
3988 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3989 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3990 if (!NextUB.isUsable())
3991 return 0;
3992 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003993
3994 // Build updates and final values of the loop counters.
3995 bool HasErrors = false;
3996 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003997 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003998 Built.Updates.resize(NestedLoopCount);
3999 Built.Finals.resize(NestedLoopCount);
4000 {
4001 ExprResult Div;
4002 // Go from inner nested loop to outer.
4003 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4004 LoopIterationSpace &IS = IterSpaces[Cnt];
4005 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4006 // Build: Iter = (IV / Div) % IS.NumIters
4007 // where Div is product of previous iterations' IS.NumIters.
4008 ExprResult Iter;
4009 if (Div.isUsable()) {
4010 Iter =
4011 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4012 } else {
4013 Iter = IV;
4014 assert((Cnt == (int)NestedLoopCount - 1) &&
4015 "unusable div expected on first iteration only");
4016 }
4017
4018 if (Cnt != 0 && Iter.isUsable())
4019 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4020 IS.NumIterations);
4021 if (!Iter.isUsable()) {
4022 HasErrors = true;
4023 break;
4024 }
4025
Alexey Bataev39f915b82015-05-08 10:41:21 +00004026 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4027 auto *CounterVar = buildDeclRefExpr(
4028 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4029 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4030 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004031 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4032 IS.CounterInit);
4033 if (!Init.isUsable()) {
4034 HasErrors = true;
4035 break;
4036 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004037 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004038 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004039 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4040 if (!Update.isUsable()) {
4041 HasErrors = true;
4042 break;
4043 }
4044
4045 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4046 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004047 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 IS.NumIterations, IS.CounterStep, IS.Subtract);
4049 if (!Final.isUsable()) {
4050 HasErrors = true;
4051 break;
4052 }
4053
4054 // Build Div for the next iteration: Div <- Div * IS.NumIters
4055 if (Cnt != 0) {
4056 if (Div.isUnset())
4057 Div = IS.NumIterations;
4058 else
4059 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4060 IS.NumIterations);
4061
4062 // Add parentheses (for debugging purposes only).
4063 if (Div.isUsable())
4064 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4065 if (!Div.isUsable()) {
4066 HasErrors = true;
4067 break;
4068 }
4069 }
4070 if (!Update.isUsable() || !Final.isUsable()) {
4071 HasErrors = true;
4072 break;
4073 }
4074 // Save results
4075 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004076 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004077 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004078 Built.Updates[Cnt] = Update.get();
4079 Built.Finals[Cnt] = Final.get();
4080 }
4081 }
4082
4083 if (HasErrors)
4084 return 0;
4085
4086 // Save results
4087 Built.IterationVarRef = IV.get();
4088 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004089 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004090 Built.CalcLastIteration =
4091 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004092 Built.PreCond = PreCond.get();
4093 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094 Built.Init = Init.get();
4095 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004096 Built.LB = LB.get();
4097 Built.UB = UB.get();
4098 Built.IL = IL.get();
4099 Built.ST = ST.get();
4100 Built.EUB = EUB.get();
4101 Built.NLB = NextLB.get();
4102 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004103
Alexey Bataevabfc0692014-06-25 06:52:00 +00004104 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004105}
4106
Alexey Bataev10e775f2015-07-30 11:36:16 +00004107static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004108 auto CollapseClauses =
4109 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4110 if (CollapseClauses.begin() != CollapseClauses.end())
4111 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004112 return nullptr;
4113}
4114
Alexey Bataev10e775f2015-07-30 11:36:16 +00004115static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004116 auto OrderedClauses =
4117 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4118 if (OrderedClauses.begin() != OrderedClauses.end())
4119 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004120 return nullptr;
4121}
4122
Alexey Bataev66b15b52015-08-21 11:14:16 +00004123static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4124 const Expr *Safelen) {
4125 llvm::APSInt SimdlenRes, SafelenRes;
4126 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4127 Simdlen->isInstantiationDependent() ||
4128 Simdlen->containsUnexpandedParameterPack())
4129 return false;
4130 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4131 Safelen->isInstantiationDependent() ||
4132 Safelen->containsUnexpandedParameterPack())
4133 return false;
4134 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4135 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4136 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4137 // If both simdlen and safelen clauses are specified, the value of the simdlen
4138 // parameter must be less than or equal to the value of the safelen parameter.
4139 if (SimdlenRes > SafelenRes) {
4140 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4141 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4142 return true;
4143 }
4144 return false;
4145}
4146
Alexey Bataev4acb8592014-07-07 13:01:15 +00004147StmtResult Sema::ActOnOpenMPSimdDirective(
4148 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4149 SourceLocation EndLoc,
4150 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004151 if (!AStmt)
4152 return StmtError();
4153
4154 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004155 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004156 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4157 // define the nested loops number.
4158 unsigned NestedLoopCount = CheckOpenMPLoop(
4159 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4160 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004161 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004162 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004163
Alexander Musmana5f070a2014-10-01 06:03:56 +00004164 assert((CurContext->isDependentContext() || B.builtAll()) &&
4165 "omp simd loop exprs were not built");
4166
Alexander Musman3276a272015-03-21 10:12:56 +00004167 if (!CurContext->isDependentContext()) {
4168 // Finalize the clauses that need pre-built expressions for CodeGen.
4169 for (auto C : Clauses) {
4170 if (auto LC = dyn_cast<OMPLinearClause>(C))
4171 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4172 B.NumIterations, *this, CurScope))
4173 return StmtError();
4174 }
4175 }
4176
Alexey Bataev66b15b52015-08-21 11:14:16 +00004177 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4178 // If both simdlen and safelen clauses are specified, the value of the simdlen
4179 // parameter must be less than or equal to the value of the safelen parameter.
4180 OMPSafelenClause *Safelen = nullptr;
4181 OMPSimdlenClause *Simdlen = nullptr;
4182 for (auto *Clause : Clauses) {
4183 if (Clause->getClauseKind() == OMPC_safelen)
4184 Safelen = cast<OMPSafelenClause>(Clause);
4185 else if (Clause->getClauseKind() == OMPC_simdlen)
4186 Simdlen = cast<OMPSimdlenClause>(Clause);
4187 if (Safelen && Simdlen)
4188 break;
4189 }
4190 if (Simdlen && Safelen &&
4191 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4192 Safelen->getSafelen()))
4193 return StmtError();
4194
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004195 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004196 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4197 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004198}
4199
Alexey Bataev4acb8592014-07-07 13:01:15 +00004200StmtResult Sema::ActOnOpenMPForDirective(
4201 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4202 SourceLocation EndLoc,
4203 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004204 if (!AStmt)
4205 return StmtError();
4206
4207 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004208 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004209 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4210 // define the nested loops number.
4211 unsigned NestedLoopCount = CheckOpenMPLoop(
4212 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4213 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004214 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004215 return StmtError();
4216
Alexander Musmana5f070a2014-10-01 06:03:56 +00004217 assert((CurContext->isDependentContext() || B.builtAll()) &&
4218 "omp for loop exprs were not built");
4219
Alexey Bataev54acd402015-08-04 11:18:19 +00004220 if (!CurContext->isDependentContext()) {
4221 // Finalize the clauses that need pre-built expressions for CodeGen.
4222 for (auto C : Clauses) {
4223 if (auto LC = dyn_cast<OMPLinearClause>(C))
4224 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4225 B.NumIterations, *this, CurScope))
4226 return StmtError();
4227 }
4228 }
4229
Alexey Bataevf29276e2014-06-18 04:14:57 +00004230 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004231 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004232 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004233}
4234
Alexander Musmanf82886e2014-09-18 05:12:34 +00004235StmtResult Sema::ActOnOpenMPForSimdDirective(
4236 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4237 SourceLocation EndLoc,
4238 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004239 if (!AStmt)
4240 return StmtError();
4241
4242 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004243 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004244 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4245 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004246 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004247 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4248 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4249 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004250 if (NestedLoopCount == 0)
4251 return StmtError();
4252
Alexander Musmanc6388682014-12-15 07:07:06 +00004253 assert((CurContext->isDependentContext() || B.builtAll()) &&
4254 "omp for simd loop exprs were not built");
4255
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004256 if (!CurContext->isDependentContext()) {
4257 // Finalize the clauses that need pre-built expressions for CodeGen.
4258 for (auto C : Clauses) {
4259 if (auto LC = dyn_cast<OMPLinearClause>(C))
4260 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4261 B.NumIterations, *this, CurScope))
4262 return StmtError();
4263 }
4264 }
4265
Alexey Bataev66b15b52015-08-21 11:14:16 +00004266 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4267 // If both simdlen and safelen clauses are specified, the value of the simdlen
4268 // parameter must be less than or equal to the value of the safelen parameter.
4269 OMPSafelenClause *Safelen = nullptr;
4270 OMPSimdlenClause *Simdlen = nullptr;
4271 for (auto *Clause : Clauses) {
4272 if (Clause->getClauseKind() == OMPC_safelen)
4273 Safelen = cast<OMPSafelenClause>(Clause);
4274 else if (Clause->getClauseKind() == OMPC_simdlen)
4275 Simdlen = cast<OMPSimdlenClause>(Clause);
4276 if (Safelen && Simdlen)
4277 break;
4278 }
4279 if (Simdlen && Safelen &&
4280 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4281 Safelen->getSafelen()))
4282 return StmtError();
4283
Alexander Musmanf82886e2014-09-18 05:12:34 +00004284 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004285 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4286 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004287}
4288
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004289StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4290 Stmt *AStmt,
4291 SourceLocation StartLoc,
4292 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004293 if (!AStmt)
4294 return StmtError();
4295
4296 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004297 auto BaseStmt = AStmt;
4298 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4299 BaseStmt = CS->getCapturedStmt();
4300 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4301 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004302 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004303 return StmtError();
4304 // All associated statements must be '#pragma omp section' except for
4305 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004306 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004307 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4308 if (SectionStmt)
4309 Diag(SectionStmt->getLocStart(),
4310 diag::err_omp_sections_substmt_not_section);
4311 return StmtError();
4312 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004313 cast<OMPSectionDirective>(SectionStmt)
4314 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004315 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004316 } else {
4317 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4318 return StmtError();
4319 }
4320
4321 getCurFunction()->setHasBranchProtectedScope();
4322
Alexey Bataev25e5b442015-09-15 12:52:43 +00004323 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4324 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004325}
4326
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004327StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4328 SourceLocation StartLoc,
4329 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004330 if (!AStmt)
4331 return StmtError();
4332
4333 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004334
4335 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004336 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004337
Alexey Bataev25e5b442015-09-15 12:52:43 +00004338 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4339 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004340}
4341
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004342StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4343 Stmt *AStmt,
4344 SourceLocation StartLoc,
4345 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004346 if (!AStmt)
4347 return StmtError();
4348
4349 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004350
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004351 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004352
Alexey Bataev3255bf32015-01-19 05:20:46 +00004353 // OpenMP [2.7.3, single Construct, Restrictions]
4354 // The copyprivate clause must not be used with the nowait clause.
4355 OMPClause *Nowait = nullptr;
4356 OMPClause *Copyprivate = nullptr;
4357 for (auto *Clause : Clauses) {
4358 if (Clause->getClauseKind() == OMPC_nowait)
4359 Nowait = Clause;
4360 else if (Clause->getClauseKind() == OMPC_copyprivate)
4361 Copyprivate = Clause;
4362 if (Copyprivate && Nowait) {
4363 Diag(Copyprivate->getLocStart(),
4364 diag::err_omp_single_copyprivate_with_nowait);
4365 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4366 return StmtError();
4367 }
4368 }
4369
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004370 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4371}
4372
Alexander Musman80c22892014-07-17 08:54:58 +00004373StmtResult Sema::ActOnOpenMPMasterDirective(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");
Alexander Musman80c22892014-07-17 08:54:58 +00004380
4381 getCurFunction()->setHasBranchProtectedScope();
4382
4383 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4384}
4385
Alexey Bataev28c75412015-12-15 08:19:24 +00004386StmtResult Sema::ActOnOpenMPCriticalDirective(
4387 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4388 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004389 if (!AStmt)
4390 return StmtError();
4391
4392 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004393
Alexey Bataev28c75412015-12-15 08:19:24 +00004394 bool ErrorFound = false;
4395 llvm::APSInt Hint;
4396 SourceLocation HintLoc;
4397 bool DependentHint = false;
4398 for (auto *C : Clauses) {
4399 if (C->getClauseKind() == OMPC_hint) {
4400 if (!DirName.getName()) {
4401 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4402 ErrorFound = true;
4403 }
4404 Expr *E = cast<OMPHintClause>(C)->getHint();
4405 if (E->isTypeDependent() || E->isValueDependent() ||
4406 E->isInstantiationDependent())
4407 DependentHint = true;
4408 else {
4409 Hint = E->EvaluateKnownConstInt(Context);
4410 HintLoc = C->getLocStart();
4411 }
4412 }
4413 }
4414 if (ErrorFound)
4415 return StmtError();
4416 auto Pair = DSAStack->getCriticalWithHint(DirName);
4417 if (Pair.first && DirName.getName() && !DependentHint) {
4418 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4419 Diag(StartLoc, diag::err_omp_critical_with_hint);
4420 if (HintLoc.isValid()) {
4421 Diag(HintLoc, diag::note_omp_critical_hint_here)
4422 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4423 } else
4424 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4425 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4426 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4427 << 1
4428 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4429 /*Radix=*/10, /*Signed=*/false);
4430 } else
4431 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4432 }
4433 }
4434
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004435 getCurFunction()->setHasBranchProtectedScope();
4436
Alexey Bataev28c75412015-12-15 08:19:24 +00004437 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4438 Clauses, AStmt);
4439 if (!Pair.first && DirName.getName() && !DependentHint)
4440 DSAStack->addCriticalWithHint(Dir, Hint);
4441 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004442}
4443
Alexey Bataev4acb8592014-07-07 13:01:15 +00004444StmtResult Sema::ActOnOpenMPParallelForDirective(
4445 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4446 SourceLocation EndLoc,
4447 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004448 if (!AStmt)
4449 return StmtError();
4450
Alexey Bataev4acb8592014-07-07 13:01:15 +00004451 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4452 // 1.2.2 OpenMP Language Terminology
4453 // Structured block - An executable statement with a single entry at the
4454 // top and a single exit at the bottom.
4455 // The point of exit cannot be a branch out of the structured block.
4456 // longjmp() and throw() must not violate the entry/exit criteria.
4457 CS->getCapturedDecl()->setNothrow();
4458
Alexander Musmanc6388682014-12-15 07:07:06 +00004459 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004460 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4461 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004462 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004463 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4464 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4465 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004466 if (NestedLoopCount == 0)
4467 return StmtError();
4468
Alexander Musmana5f070a2014-10-01 06:03:56 +00004469 assert((CurContext->isDependentContext() || B.builtAll()) &&
4470 "omp parallel for loop exprs were not built");
4471
Alexey Bataev54acd402015-08-04 11:18:19 +00004472 if (!CurContext->isDependentContext()) {
4473 // Finalize the clauses that need pre-built expressions for CodeGen.
4474 for (auto C : Clauses) {
4475 if (auto LC = dyn_cast<OMPLinearClause>(C))
4476 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4477 B.NumIterations, *this, CurScope))
4478 return StmtError();
4479 }
4480 }
4481
Alexey Bataev4acb8592014-07-07 13:01:15 +00004482 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004483 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004484 NestedLoopCount, Clauses, AStmt, B,
4485 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004486}
4487
Alexander Musmane4e893b2014-09-23 09:33:00 +00004488StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4489 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4490 SourceLocation EndLoc,
4491 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004492 if (!AStmt)
4493 return StmtError();
4494
Alexander Musmane4e893b2014-09-23 09:33:00 +00004495 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4496 // 1.2.2 OpenMP Language Terminology
4497 // Structured block - An executable statement with a single entry at the
4498 // top and a single exit at the bottom.
4499 // The point of exit cannot be a branch out of the structured block.
4500 // longjmp() and throw() must not violate the entry/exit criteria.
4501 CS->getCapturedDecl()->setNothrow();
4502
Alexander Musmanc6388682014-12-15 07:07:06 +00004503 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004504 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4505 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004506 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004507 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4508 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4509 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004510 if (NestedLoopCount == 0)
4511 return StmtError();
4512
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004513 if (!CurContext->isDependentContext()) {
4514 // Finalize the clauses that need pre-built expressions for CodeGen.
4515 for (auto C : Clauses) {
4516 if (auto LC = dyn_cast<OMPLinearClause>(C))
4517 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4518 B.NumIterations, *this, CurScope))
4519 return StmtError();
4520 }
4521 }
4522
Alexey Bataev66b15b52015-08-21 11:14:16 +00004523 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4524 // If both simdlen and safelen clauses are specified, the value of the simdlen
4525 // parameter must be less than or equal to the value of the safelen parameter.
4526 OMPSafelenClause *Safelen = nullptr;
4527 OMPSimdlenClause *Simdlen = nullptr;
4528 for (auto *Clause : Clauses) {
4529 if (Clause->getClauseKind() == OMPC_safelen)
4530 Safelen = cast<OMPSafelenClause>(Clause);
4531 else if (Clause->getClauseKind() == OMPC_simdlen)
4532 Simdlen = cast<OMPSimdlenClause>(Clause);
4533 if (Safelen && Simdlen)
4534 break;
4535 }
4536 if (Simdlen && Safelen &&
4537 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4538 Safelen->getSafelen()))
4539 return StmtError();
4540
Alexander Musmane4e893b2014-09-23 09:33:00 +00004541 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004542 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004543 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004544}
4545
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004546StmtResult
4547Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4548 Stmt *AStmt, SourceLocation StartLoc,
4549 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004550 if (!AStmt)
4551 return StmtError();
4552
4553 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004554 auto BaseStmt = AStmt;
4555 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4556 BaseStmt = CS->getCapturedStmt();
4557 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4558 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004559 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004560 return StmtError();
4561 // All associated statements must be '#pragma omp section' except for
4562 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004563 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004564 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4565 if (SectionStmt)
4566 Diag(SectionStmt->getLocStart(),
4567 diag::err_omp_parallel_sections_substmt_not_section);
4568 return StmtError();
4569 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004570 cast<OMPSectionDirective>(SectionStmt)
4571 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004572 }
4573 } else {
4574 Diag(AStmt->getLocStart(),
4575 diag::err_omp_parallel_sections_not_compound_stmt);
4576 return StmtError();
4577 }
4578
4579 getCurFunction()->setHasBranchProtectedScope();
4580
Alexey Bataev25e5b442015-09-15 12:52:43 +00004581 return OMPParallelSectionsDirective::Create(
4582 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004583}
4584
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004585StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4586 Stmt *AStmt, SourceLocation StartLoc,
4587 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004588 if (!AStmt)
4589 return StmtError();
4590
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004591 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4592 // 1.2.2 OpenMP Language Terminology
4593 // Structured block - An executable statement with a single entry at the
4594 // top and a single exit at the bottom.
4595 // The point of exit cannot be a branch out of the structured block.
4596 // longjmp() and throw() must not violate the entry/exit criteria.
4597 CS->getCapturedDecl()->setNothrow();
4598
4599 getCurFunction()->setHasBranchProtectedScope();
4600
Alexey Bataev25e5b442015-09-15 12:52:43 +00004601 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4602 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004603}
4604
Alexey Bataev68446b72014-07-18 07:47:19 +00004605StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4606 SourceLocation EndLoc) {
4607 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4608}
4609
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004610StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4611 SourceLocation EndLoc) {
4612 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4613}
4614
Alexey Bataev2df347a2014-07-18 10:17:07 +00004615StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4616 SourceLocation EndLoc) {
4617 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4618}
4619
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004620StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4621 SourceLocation StartLoc,
4622 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004623 if (!AStmt)
4624 return StmtError();
4625
4626 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004627
4628 getCurFunction()->setHasBranchProtectedScope();
4629
4630 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4631}
4632
Alexey Bataev6125da92014-07-21 11:26:11 +00004633StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4634 SourceLocation StartLoc,
4635 SourceLocation EndLoc) {
4636 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4637 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4638}
4639
Alexey Bataev346265e2015-09-25 10:37:12 +00004640StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4641 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004642 SourceLocation StartLoc,
4643 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004644 OMPClause *DependFound = nullptr;
4645 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004646 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004647 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004648 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004649 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004650 for (auto *C : Clauses) {
4651 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4652 DependFound = C;
4653 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4654 if (DependSourceClause) {
4655 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4656 << getOpenMPDirectiveName(OMPD_ordered)
4657 << getOpenMPClauseName(OMPC_depend) << 2;
4658 ErrorFound = true;
4659 } else
4660 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004661 if (DependSinkClause) {
4662 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4663 << 0;
4664 ErrorFound = true;
4665 }
4666 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4667 if (DependSourceClause) {
4668 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4669 << 1;
4670 ErrorFound = true;
4671 }
4672 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004673 }
4674 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004675 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004676 else if (C->getClauseKind() == OMPC_simd)
4677 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004678 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004679 if (!ErrorFound && !SC &&
4680 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004681 // OpenMP [2.8.1,simd Construct, Restrictions]
4682 // An ordered construct with the simd clause is the only OpenMP construct
4683 // that can appear in the simd region.
4684 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004685 ErrorFound = true;
4686 } else if (DependFound && (TC || SC)) {
4687 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4688 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4689 ErrorFound = true;
4690 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4691 Diag(DependFound->getLocStart(),
4692 diag::err_omp_ordered_directive_without_param);
4693 ErrorFound = true;
4694 } else if (TC || Clauses.empty()) {
4695 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4696 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4697 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4698 << (TC != nullptr);
4699 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4700 ErrorFound = true;
4701 }
4702 }
4703 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004704 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004705
4706 if (AStmt) {
4707 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4708
4709 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004710 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004711
4712 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004713}
4714
Alexey Bataev1d160b12015-03-13 12:27:31 +00004715namespace {
4716/// \brief Helper class for checking expression in 'omp atomic [update]'
4717/// construct.
4718class OpenMPAtomicUpdateChecker {
4719 /// \brief Error results for atomic update expressions.
4720 enum ExprAnalysisErrorCode {
4721 /// \brief A statement is not an expression statement.
4722 NotAnExpression,
4723 /// \brief Expression is not builtin binary or unary operation.
4724 NotABinaryOrUnaryExpression,
4725 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4726 NotAnUnaryIncDecExpression,
4727 /// \brief An expression is not of scalar type.
4728 NotAScalarType,
4729 /// \brief A binary operation is not an assignment operation.
4730 NotAnAssignmentOp,
4731 /// \brief RHS part of the binary operation is not a binary expression.
4732 NotABinaryExpression,
4733 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4734 /// expression.
4735 NotABinaryOperator,
4736 /// \brief RHS binary operation does not have reference to the updated LHS
4737 /// part.
4738 NotAnUpdateExpression,
4739 /// \brief No errors is found.
4740 NoError
4741 };
4742 /// \brief Reference to Sema.
4743 Sema &SemaRef;
4744 /// \brief A location for note diagnostics (when error is found).
4745 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004746 /// \brief 'x' lvalue part of the source atomic expression.
4747 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004748 /// \brief 'expr' rvalue part of the source atomic expression.
4749 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004750 /// \brief Helper expression of the form
4751 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4752 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4753 Expr *UpdateExpr;
4754 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4755 /// important for non-associative operations.
4756 bool IsXLHSInRHSPart;
4757 BinaryOperatorKind Op;
4758 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004759 /// \brief true if the source expression is a postfix unary operation, false
4760 /// if it is a prefix unary operation.
4761 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004762
4763public:
4764 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004765 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004766 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004767 /// \brief Check specified statement that it is suitable for 'atomic update'
4768 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004769 /// expression. If DiagId and NoteId == 0, then only check is performed
4770 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004771 /// \param DiagId Diagnostic which should be emitted if error is found.
4772 /// \param NoteId Diagnostic note for the main error message.
4773 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004774 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004775 /// \brief Return the 'x' lvalue part of the source atomic expression.
4776 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004777 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4778 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004779 /// \brief Return the update expression used in calculation of the updated
4780 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4781 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4782 Expr *getUpdateExpr() const { return UpdateExpr; }
4783 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4784 /// false otherwise.
4785 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4786
Alexey Bataevb78ca832015-04-01 03:33:17 +00004787 /// \brief true if the source expression is a postfix unary operation, false
4788 /// if it is a prefix unary operation.
4789 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4790
Alexey Bataev1d160b12015-03-13 12:27:31 +00004791private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004792 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4793 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004794};
4795} // namespace
4796
4797bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4798 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4799 ExprAnalysisErrorCode ErrorFound = NoError;
4800 SourceLocation ErrorLoc, NoteLoc;
4801 SourceRange ErrorRange, NoteRange;
4802 // Allowed constructs are:
4803 // x = x binop expr;
4804 // x = expr binop x;
4805 if (AtomicBinOp->getOpcode() == BO_Assign) {
4806 X = AtomicBinOp->getLHS();
4807 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4808 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4809 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4810 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4811 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004812 Op = AtomicInnerBinOp->getOpcode();
4813 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004814 auto *LHS = AtomicInnerBinOp->getLHS();
4815 auto *RHS = AtomicInnerBinOp->getRHS();
4816 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4817 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4818 /*Canonical=*/true);
4819 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4820 /*Canonical=*/true);
4821 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4822 /*Canonical=*/true);
4823 if (XId == LHSId) {
4824 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004825 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004826 } else if (XId == RHSId) {
4827 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004828 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004829 } else {
4830 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4831 ErrorRange = AtomicInnerBinOp->getSourceRange();
4832 NoteLoc = X->getExprLoc();
4833 NoteRange = X->getSourceRange();
4834 ErrorFound = NotAnUpdateExpression;
4835 }
4836 } else {
4837 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4838 ErrorRange = AtomicInnerBinOp->getSourceRange();
4839 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4840 NoteRange = SourceRange(NoteLoc, NoteLoc);
4841 ErrorFound = NotABinaryOperator;
4842 }
4843 } else {
4844 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4845 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4846 ErrorFound = NotABinaryExpression;
4847 }
4848 } else {
4849 ErrorLoc = AtomicBinOp->getExprLoc();
4850 ErrorRange = AtomicBinOp->getSourceRange();
4851 NoteLoc = AtomicBinOp->getOperatorLoc();
4852 NoteRange = SourceRange(NoteLoc, NoteLoc);
4853 ErrorFound = NotAnAssignmentOp;
4854 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004855 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004856 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4857 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4858 return true;
4859 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004860 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004861 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004862}
4863
4864bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4865 unsigned NoteId) {
4866 ExprAnalysisErrorCode ErrorFound = NoError;
4867 SourceLocation ErrorLoc, NoteLoc;
4868 SourceRange ErrorRange, NoteRange;
4869 // Allowed constructs are:
4870 // x++;
4871 // x--;
4872 // ++x;
4873 // --x;
4874 // x binop= expr;
4875 // x = x binop expr;
4876 // x = expr binop x;
4877 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4878 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4879 if (AtomicBody->getType()->isScalarType() ||
4880 AtomicBody->isInstantiationDependent()) {
4881 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4882 AtomicBody->IgnoreParenImpCasts())) {
4883 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004884 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004885 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004886 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004887 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004888 X = AtomicCompAssignOp->getLHS();
4889 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004890 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4891 AtomicBody->IgnoreParenImpCasts())) {
4892 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004893 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4894 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004895 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004896 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4897 // Check for Unary Operation
4898 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004899 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004900 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4901 OpLoc = AtomicUnaryOp->getOperatorLoc();
4902 X = AtomicUnaryOp->getSubExpr();
4903 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4904 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004905 } else {
4906 ErrorFound = NotAnUnaryIncDecExpression;
4907 ErrorLoc = AtomicUnaryOp->getExprLoc();
4908 ErrorRange = AtomicUnaryOp->getSourceRange();
4909 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4910 NoteRange = SourceRange(NoteLoc, NoteLoc);
4911 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004912 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004913 ErrorFound = NotABinaryOrUnaryExpression;
4914 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4915 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4916 }
4917 } else {
4918 ErrorFound = NotAScalarType;
4919 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4920 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4921 }
4922 } else {
4923 ErrorFound = NotAnExpression;
4924 NoteLoc = ErrorLoc = S->getLocStart();
4925 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4926 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004927 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004928 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4929 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4930 return true;
4931 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004932 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004933 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004934 // Build an update expression of form 'OpaqueValueExpr(x) binop
4935 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4936 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4937 auto *OVEX = new (SemaRef.getASTContext())
4938 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4939 auto *OVEExpr = new (SemaRef.getASTContext())
4940 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4941 auto Update =
4942 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4943 IsXLHSInRHSPart ? OVEExpr : OVEX);
4944 if (Update.isInvalid())
4945 return true;
4946 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4947 Sema::AA_Casting);
4948 if (Update.isInvalid())
4949 return true;
4950 UpdateExpr = Update.get();
4951 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004952 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004953}
4954
Alexey Bataev0162e452014-07-22 10:10:35 +00004955StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4956 Stmt *AStmt,
4957 SourceLocation StartLoc,
4958 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004959 if (!AStmt)
4960 return StmtError();
4961
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004962 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004963 // 1.2.2 OpenMP Language Terminology
4964 // Structured block - An executable statement with a single entry at the
4965 // top and a single exit at the bottom.
4966 // The point of exit cannot be a branch out of the structured block.
4967 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004968 OpenMPClauseKind AtomicKind = OMPC_unknown;
4969 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004970 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004971 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004972 C->getClauseKind() == OMPC_update ||
4973 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004974 if (AtomicKind != OMPC_unknown) {
4975 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4976 << SourceRange(C->getLocStart(), C->getLocEnd());
4977 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4978 << getOpenMPClauseName(AtomicKind);
4979 } else {
4980 AtomicKind = C->getClauseKind();
4981 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004982 }
4983 }
4984 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004985
Alexey Bataev459dec02014-07-24 06:46:57 +00004986 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004987 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4988 Body = EWC->getSubExpr();
4989
Alexey Bataev62cec442014-11-18 10:14:22 +00004990 Expr *X = nullptr;
4991 Expr *V = nullptr;
4992 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004993 Expr *UE = nullptr;
4994 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004995 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004996 // OpenMP [2.12.6, atomic Construct]
4997 // In the next expressions:
4998 // * x and v (as applicable) are both l-value expressions with scalar type.
4999 // * During the execution of an atomic region, multiple syntactic
5000 // occurrences of x must designate the same storage location.
5001 // * Neither of v and expr (as applicable) may access the storage location
5002 // designated by x.
5003 // * Neither of x and expr (as applicable) may access the storage location
5004 // designated by v.
5005 // * expr is an expression with scalar type.
5006 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5007 // * binop, binop=, ++, and -- are not overloaded operators.
5008 // * The expression x binop expr must be numerically equivalent to x binop
5009 // (expr). This requirement is satisfied if the operators in expr have
5010 // precedence greater than binop, or by using parentheses around expr or
5011 // subexpressions of expr.
5012 // * The expression expr binop x must be numerically equivalent to (expr)
5013 // binop x. This requirement is satisfied if the operators in expr have
5014 // precedence equal to or greater than binop, or by using parentheses around
5015 // expr or subexpressions of expr.
5016 // * For forms that allow multiple occurrences of x, the number of times
5017 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005018 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005019 enum {
5020 NotAnExpression,
5021 NotAnAssignmentOp,
5022 NotAScalarType,
5023 NotAnLValue,
5024 NoError
5025 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005026 SourceLocation ErrorLoc, NoteLoc;
5027 SourceRange ErrorRange, NoteRange;
5028 // If clause is read:
5029 // v = x;
5030 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5031 auto AtomicBinOp =
5032 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5033 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5034 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5035 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5036 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5037 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5038 if (!X->isLValue() || !V->isLValue()) {
5039 auto NotLValueExpr = X->isLValue() ? V : X;
5040 ErrorFound = NotAnLValue;
5041 ErrorLoc = AtomicBinOp->getExprLoc();
5042 ErrorRange = AtomicBinOp->getSourceRange();
5043 NoteLoc = NotLValueExpr->getExprLoc();
5044 NoteRange = NotLValueExpr->getSourceRange();
5045 }
5046 } else if (!X->isInstantiationDependent() ||
5047 !V->isInstantiationDependent()) {
5048 auto NotScalarExpr =
5049 (X->isInstantiationDependent() || X->getType()->isScalarType())
5050 ? V
5051 : X;
5052 ErrorFound = NotAScalarType;
5053 ErrorLoc = AtomicBinOp->getExprLoc();
5054 ErrorRange = AtomicBinOp->getSourceRange();
5055 NoteLoc = NotScalarExpr->getExprLoc();
5056 NoteRange = NotScalarExpr->getSourceRange();
5057 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005058 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005059 ErrorFound = NotAnAssignmentOp;
5060 ErrorLoc = AtomicBody->getExprLoc();
5061 ErrorRange = AtomicBody->getSourceRange();
5062 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5063 : AtomicBody->getExprLoc();
5064 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5065 : AtomicBody->getSourceRange();
5066 }
5067 } else {
5068 ErrorFound = NotAnExpression;
5069 NoteLoc = ErrorLoc = Body->getLocStart();
5070 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005071 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005072 if (ErrorFound != NoError) {
5073 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5074 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005075 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5076 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005077 return StmtError();
5078 } else if (CurContext->isDependentContext())
5079 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005080 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005081 enum {
5082 NotAnExpression,
5083 NotAnAssignmentOp,
5084 NotAScalarType,
5085 NotAnLValue,
5086 NoError
5087 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005088 SourceLocation ErrorLoc, NoteLoc;
5089 SourceRange ErrorRange, NoteRange;
5090 // If clause is write:
5091 // x = expr;
5092 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5093 auto AtomicBinOp =
5094 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5095 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005096 X = AtomicBinOp->getLHS();
5097 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005098 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5099 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5100 if (!X->isLValue()) {
5101 ErrorFound = NotAnLValue;
5102 ErrorLoc = AtomicBinOp->getExprLoc();
5103 ErrorRange = AtomicBinOp->getSourceRange();
5104 NoteLoc = X->getExprLoc();
5105 NoteRange = X->getSourceRange();
5106 }
5107 } else if (!X->isInstantiationDependent() ||
5108 !E->isInstantiationDependent()) {
5109 auto NotScalarExpr =
5110 (X->isInstantiationDependent() || X->getType()->isScalarType())
5111 ? E
5112 : X;
5113 ErrorFound = NotAScalarType;
5114 ErrorLoc = AtomicBinOp->getExprLoc();
5115 ErrorRange = AtomicBinOp->getSourceRange();
5116 NoteLoc = NotScalarExpr->getExprLoc();
5117 NoteRange = NotScalarExpr->getSourceRange();
5118 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005119 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005120 ErrorFound = NotAnAssignmentOp;
5121 ErrorLoc = AtomicBody->getExprLoc();
5122 ErrorRange = AtomicBody->getSourceRange();
5123 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5124 : AtomicBody->getExprLoc();
5125 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5126 : AtomicBody->getSourceRange();
5127 }
5128 } else {
5129 ErrorFound = NotAnExpression;
5130 NoteLoc = ErrorLoc = Body->getLocStart();
5131 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005132 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005133 if (ErrorFound != NoError) {
5134 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5135 << ErrorRange;
5136 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5137 << NoteRange;
5138 return StmtError();
5139 } else if (CurContext->isDependentContext())
5140 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005141 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005142 // If clause is update:
5143 // x++;
5144 // x--;
5145 // ++x;
5146 // --x;
5147 // x binop= expr;
5148 // x = x binop expr;
5149 // x = expr binop x;
5150 OpenMPAtomicUpdateChecker Checker(*this);
5151 if (Checker.checkStatement(
5152 Body, (AtomicKind == OMPC_update)
5153 ? diag::err_omp_atomic_update_not_expression_statement
5154 : diag::err_omp_atomic_not_expression_statement,
5155 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005156 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005157 if (!CurContext->isDependentContext()) {
5158 E = Checker.getExpr();
5159 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005160 UE = Checker.getUpdateExpr();
5161 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005162 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005163 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005164 enum {
5165 NotAnAssignmentOp,
5166 NotACompoundStatement,
5167 NotTwoSubstatements,
5168 NotASpecificExpression,
5169 NoError
5170 } ErrorFound = NoError;
5171 SourceLocation ErrorLoc, NoteLoc;
5172 SourceRange ErrorRange, NoteRange;
5173 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5174 // If clause is a capture:
5175 // v = x++;
5176 // v = x--;
5177 // v = ++x;
5178 // v = --x;
5179 // v = x binop= expr;
5180 // v = x = x binop expr;
5181 // v = x = expr binop x;
5182 auto *AtomicBinOp =
5183 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5184 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5185 V = AtomicBinOp->getLHS();
5186 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5187 OpenMPAtomicUpdateChecker Checker(*this);
5188 if (Checker.checkStatement(
5189 Body, diag::err_omp_atomic_capture_not_expression_statement,
5190 diag::note_omp_atomic_update))
5191 return StmtError();
5192 E = Checker.getExpr();
5193 X = Checker.getX();
5194 UE = Checker.getUpdateExpr();
5195 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5196 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005197 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005198 ErrorLoc = AtomicBody->getExprLoc();
5199 ErrorRange = AtomicBody->getSourceRange();
5200 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5201 : AtomicBody->getExprLoc();
5202 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5203 : AtomicBody->getSourceRange();
5204 ErrorFound = NotAnAssignmentOp;
5205 }
5206 if (ErrorFound != NoError) {
5207 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5208 << ErrorRange;
5209 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5210 return StmtError();
5211 } else if (CurContext->isDependentContext()) {
5212 UE = V = E = X = nullptr;
5213 }
5214 } else {
5215 // If clause is a capture:
5216 // { v = x; x = expr; }
5217 // { v = x; x++; }
5218 // { v = x; x--; }
5219 // { v = x; ++x; }
5220 // { v = x; --x; }
5221 // { v = x; x binop= expr; }
5222 // { v = x; x = x binop expr; }
5223 // { v = x; x = expr binop x; }
5224 // { x++; v = x; }
5225 // { x--; v = x; }
5226 // { ++x; v = x; }
5227 // { --x; v = x; }
5228 // { x binop= expr; v = x; }
5229 // { x = x binop expr; v = x; }
5230 // { x = expr binop x; v = x; }
5231 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5232 // Check that this is { expr1; expr2; }
5233 if (CS->size() == 2) {
5234 auto *First = CS->body_front();
5235 auto *Second = CS->body_back();
5236 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5237 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5238 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5239 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5240 // Need to find what subexpression is 'v' and what is 'x'.
5241 OpenMPAtomicUpdateChecker Checker(*this);
5242 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5243 BinaryOperator *BinOp = nullptr;
5244 if (IsUpdateExprFound) {
5245 BinOp = dyn_cast<BinaryOperator>(First);
5246 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5247 }
5248 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5249 // { v = x; x++; }
5250 // { v = x; x--; }
5251 // { v = x; ++x; }
5252 // { v = x; --x; }
5253 // { v = x; x binop= expr; }
5254 // { v = x; x = x binop expr; }
5255 // { v = x; x = expr binop x; }
5256 // Check that the first expression has form v = x.
5257 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5258 llvm::FoldingSetNodeID XId, PossibleXId;
5259 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5260 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5261 IsUpdateExprFound = XId == PossibleXId;
5262 if (IsUpdateExprFound) {
5263 V = BinOp->getLHS();
5264 X = Checker.getX();
5265 E = Checker.getExpr();
5266 UE = Checker.getUpdateExpr();
5267 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005268 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005269 }
5270 }
5271 if (!IsUpdateExprFound) {
5272 IsUpdateExprFound = !Checker.checkStatement(First);
5273 BinOp = nullptr;
5274 if (IsUpdateExprFound) {
5275 BinOp = dyn_cast<BinaryOperator>(Second);
5276 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5277 }
5278 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5279 // { x++; v = x; }
5280 // { x--; v = x; }
5281 // { ++x; v = x; }
5282 // { --x; v = x; }
5283 // { x binop= expr; v = x; }
5284 // { x = x binop expr; v = x; }
5285 // { x = expr binop x; v = x; }
5286 // Check that the second expression has form v = x.
5287 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5288 llvm::FoldingSetNodeID XId, PossibleXId;
5289 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5290 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5291 IsUpdateExprFound = XId == PossibleXId;
5292 if (IsUpdateExprFound) {
5293 V = BinOp->getLHS();
5294 X = Checker.getX();
5295 E = Checker.getExpr();
5296 UE = Checker.getUpdateExpr();
5297 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005298 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005299 }
5300 }
5301 }
5302 if (!IsUpdateExprFound) {
5303 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005304 auto *FirstExpr = dyn_cast<Expr>(First);
5305 auto *SecondExpr = dyn_cast<Expr>(Second);
5306 if (!FirstExpr || !SecondExpr ||
5307 !(FirstExpr->isInstantiationDependent() ||
5308 SecondExpr->isInstantiationDependent())) {
5309 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5310 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005311 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005312 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5313 : First->getLocStart();
5314 NoteRange = ErrorRange = FirstBinOp
5315 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005316 : SourceRange(ErrorLoc, ErrorLoc);
5317 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005318 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5319 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5320 ErrorFound = NotAnAssignmentOp;
5321 NoteLoc = ErrorLoc = SecondBinOp
5322 ? SecondBinOp->getOperatorLoc()
5323 : Second->getLocStart();
5324 NoteRange = ErrorRange =
5325 SecondBinOp ? SecondBinOp->getSourceRange()
5326 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005327 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005328 auto *PossibleXRHSInFirst =
5329 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5330 auto *PossibleXLHSInSecond =
5331 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5332 llvm::FoldingSetNodeID X1Id, X2Id;
5333 PossibleXRHSInFirst->Profile(X1Id, Context,
5334 /*Canonical=*/true);
5335 PossibleXLHSInSecond->Profile(X2Id, Context,
5336 /*Canonical=*/true);
5337 IsUpdateExprFound = X1Id == X2Id;
5338 if (IsUpdateExprFound) {
5339 V = FirstBinOp->getLHS();
5340 X = SecondBinOp->getLHS();
5341 E = SecondBinOp->getRHS();
5342 UE = nullptr;
5343 IsXLHSInRHSPart = false;
5344 IsPostfixUpdate = true;
5345 } else {
5346 ErrorFound = NotASpecificExpression;
5347 ErrorLoc = FirstBinOp->getExprLoc();
5348 ErrorRange = FirstBinOp->getSourceRange();
5349 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5350 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5351 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005352 }
5353 }
5354 }
5355 }
5356 } else {
5357 NoteLoc = ErrorLoc = Body->getLocStart();
5358 NoteRange = ErrorRange =
5359 SourceRange(Body->getLocStart(), Body->getLocStart());
5360 ErrorFound = NotTwoSubstatements;
5361 }
5362 } else {
5363 NoteLoc = ErrorLoc = Body->getLocStart();
5364 NoteRange = ErrorRange =
5365 SourceRange(Body->getLocStart(), Body->getLocStart());
5366 ErrorFound = NotACompoundStatement;
5367 }
5368 if (ErrorFound != NoError) {
5369 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5370 << ErrorRange;
5371 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5372 return StmtError();
5373 } else if (CurContext->isDependentContext()) {
5374 UE = V = E = X = nullptr;
5375 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005376 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005377 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005378
5379 getCurFunction()->setHasBranchProtectedScope();
5380
Alexey Bataev62cec442014-11-18 10:14:22 +00005381 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005382 X, V, E, UE, IsXLHSInRHSPart,
5383 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005384}
5385
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005386StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5387 Stmt *AStmt,
5388 SourceLocation StartLoc,
5389 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005390 if (!AStmt)
5391 return StmtError();
5392
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005393 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5394 // 1.2.2 OpenMP Language Terminology
5395 // Structured block - An executable statement with a single entry at the
5396 // top and a single exit at the bottom.
5397 // The point of exit cannot be a branch out of the structured block.
5398 // longjmp() and throw() must not violate the entry/exit criteria.
5399 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005400
Alexey Bataev13314bf2014-10-09 04:18:56 +00005401 // OpenMP [2.16, Nesting of Regions]
5402 // If specified, a teams construct must be contained within a target
5403 // construct. That target construct must contain no statements or directives
5404 // outside of the teams construct.
5405 if (DSAStack->hasInnerTeamsRegion()) {
5406 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5407 bool OMPTeamsFound = true;
5408 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5409 auto I = CS->body_begin();
5410 while (I != CS->body_end()) {
5411 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5412 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5413 OMPTeamsFound = false;
5414 break;
5415 }
5416 ++I;
5417 }
5418 assert(I != CS->body_end() && "Not found statement");
5419 S = *I;
5420 }
5421 if (!OMPTeamsFound) {
5422 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5423 Diag(DSAStack->getInnerTeamsRegionLoc(),
5424 diag::note_omp_nested_teams_construct_here);
5425 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5426 << isa<OMPExecutableDirective>(S);
5427 return StmtError();
5428 }
5429 }
5430
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005431 getCurFunction()->setHasBranchProtectedScope();
5432
5433 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5434}
5435
Michael Wong65f367f2015-07-21 13:44:28 +00005436StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5437 Stmt *AStmt,
5438 SourceLocation StartLoc,
5439 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005440 if (!AStmt)
5441 return StmtError();
5442
5443 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5444
Michael Wong65f367f2015-07-21 13:44:28 +00005445 getCurFunction()->setHasBranchProtectedScope();
5446
5447 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5448 AStmt);
5449}
5450
Alexey Bataev13314bf2014-10-09 04:18:56 +00005451StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5452 Stmt *AStmt, SourceLocation StartLoc,
5453 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005454 if (!AStmt)
5455 return StmtError();
5456
Alexey Bataev13314bf2014-10-09 04:18:56 +00005457 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5458 // 1.2.2 OpenMP Language Terminology
5459 // Structured block - An executable statement with a single entry at the
5460 // top and a single exit at the bottom.
5461 // The point of exit cannot be a branch out of the structured block.
5462 // longjmp() and throw() must not violate the entry/exit criteria.
5463 CS->getCapturedDecl()->setNothrow();
5464
5465 getCurFunction()->setHasBranchProtectedScope();
5466
5467 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5468}
5469
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005470StmtResult
5471Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5472 SourceLocation EndLoc,
5473 OpenMPDirectiveKind CancelRegion) {
5474 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5475 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5476 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5477 << getOpenMPDirectiveName(CancelRegion);
5478 return StmtError();
5479 }
5480 if (DSAStack->isParentNowaitRegion()) {
5481 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5482 return StmtError();
5483 }
5484 if (DSAStack->isParentOrderedRegion()) {
5485 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5486 return StmtError();
5487 }
5488 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5489 CancelRegion);
5490}
5491
Alexey Bataev87933c72015-09-18 08:07:34 +00005492StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5493 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005494 SourceLocation EndLoc,
5495 OpenMPDirectiveKind CancelRegion) {
5496 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5497 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5498 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5499 << getOpenMPDirectiveName(CancelRegion);
5500 return StmtError();
5501 }
5502 if (DSAStack->isParentNowaitRegion()) {
5503 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5504 return StmtError();
5505 }
5506 if (DSAStack->isParentOrderedRegion()) {
5507 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5508 return StmtError();
5509 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005510 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005511 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5512 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005513}
5514
Alexey Bataev382967a2015-12-08 12:06:20 +00005515static bool checkGrainsizeNumTasksClauses(Sema &S,
5516 ArrayRef<OMPClause *> Clauses) {
5517 OMPClause *PrevClause = nullptr;
5518 bool ErrorFound = false;
5519 for (auto *C : Clauses) {
5520 if (C->getClauseKind() == OMPC_grainsize ||
5521 C->getClauseKind() == OMPC_num_tasks) {
5522 if (!PrevClause)
5523 PrevClause = C;
5524 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5525 S.Diag(C->getLocStart(),
5526 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5527 << getOpenMPClauseName(C->getClauseKind())
5528 << getOpenMPClauseName(PrevClause->getClauseKind());
5529 S.Diag(PrevClause->getLocStart(),
5530 diag::note_omp_previous_grainsize_num_tasks)
5531 << getOpenMPClauseName(PrevClause->getClauseKind());
5532 ErrorFound = true;
5533 }
5534 }
5535 }
5536 return ErrorFound;
5537}
5538
Alexey Bataev49f6e782015-12-01 04:18:41 +00005539StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5540 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5541 SourceLocation EndLoc,
5542 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5543 if (!AStmt)
5544 return StmtError();
5545
5546 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5547 OMPLoopDirective::HelperExprs B;
5548 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5549 // define the nested loops number.
5550 unsigned NestedLoopCount =
5551 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005552 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005553 VarsWithImplicitDSA, B);
5554 if (NestedLoopCount == 0)
5555 return StmtError();
5556
5557 assert((CurContext->isDependentContext() || B.builtAll()) &&
5558 "omp for loop exprs were not built");
5559
Alexey Bataev382967a2015-12-08 12:06:20 +00005560 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5561 // The grainsize clause and num_tasks clause are mutually exclusive and may
5562 // not appear on the same taskloop directive.
5563 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5564 return StmtError();
5565
Alexey Bataev49f6e782015-12-01 04:18:41 +00005566 getCurFunction()->setHasBranchProtectedScope();
5567 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5568 NestedLoopCount, Clauses, AStmt, B);
5569}
5570
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005571StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5572 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5573 SourceLocation EndLoc,
5574 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5575 if (!AStmt)
5576 return StmtError();
5577
5578 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5579 OMPLoopDirective::HelperExprs B;
5580 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5581 // define the nested loops number.
5582 unsigned NestedLoopCount =
5583 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5584 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5585 VarsWithImplicitDSA, B);
5586 if (NestedLoopCount == 0)
5587 return StmtError();
5588
5589 assert((CurContext->isDependentContext() || B.builtAll()) &&
5590 "omp for loop exprs were not built");
5591
Alexey Bataev382967a2015-12-08 12:06:20 +00005592 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5593 // The grainsize clause and num_tasks clause are mutually exclusive and may
5594 // not appear on the same taskloop directive.
5595 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5596 return StmtError();
5597
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005598 getCurFunction()->setHasBranchProtectedScope();
5599 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5600 NestedLoopCount, Clauses, AStmt, B);
5601}
5602
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005603StmtResult Sema::ActOnOpenMPDistributeDirective(
5604 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5605 SourceLocation EndLoc,
5606 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5607 if (!AStmt)
5608 return StmtError();
5609
5610 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5611 OMPLoopDirective::HelperExprs B;
5612 // In presence of clause 'collapse' with number of loops, it will
5613 // define the nested loops number.
5614 unsigned NestedLoopCount =
5615 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5616 nullptr /*ordered not a clause on distribute*/, AStmt,
5617 *this, *DSAStack, VarsWithImplicitDSA, B);
5618 if (NestedLoopCount == 0)
5619 return StmtError();
5620
5621 assert((CurContext->isDependentContext() || B.builtAll()) &&
5622 "omp for loop exprs were not built");
5623
5624 getCurFunction()->setHasBranchProtectedScope();
5625 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5626 NestedLoopCount, Clauses, AStmt, B);
5627}
5628
Alexey Bataeved09d242014-05-28 05:53:51 +00005629OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005630 SourceLocation StartLoc,
5631 SourceLocation LParenLoc,
5632 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005633 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005634 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005635 case OMPC_final:
5636 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5637 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005638 case OMPC_num_threads:
5639 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5640 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005641 case OMPC_safelen:
5642 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5643 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005644 case OMPC_simdlen:
5645 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5646 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005647 case OMPC_collapse:
5648 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5649 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005650 case OMPC_ordered:
5651 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5652 break;
Michael Wonge710d542015-08-07 16:16:36 +00005653 case OMPC_device:
5654 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5655 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005656 case OMPC_num_teams:
5657 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5658 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005659 case OMPC_thread_limit:
5660 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5661 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005662 case OMPC_priority:
5663 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5664 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005665 case OMPC_grainsize:
5666 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5667 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005668 case OMPC_num_tasks:
5669 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5670 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005671 case OMPC_hint:
5672 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5673 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005674 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005675 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005676 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005677 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005678 case OMPC_private:
5679 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005680 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005681 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005682 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005683 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005684 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005685 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005686 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005687 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005688 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005689 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005690 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005691 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005692 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005693 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005694 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005695 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005696 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005697 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005698 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005699 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005700 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005701 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00005702 case OMPC_dist_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005703 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005704 llvm_unreachable("Clause is not allowed.");
5705 }
5706 return Res;
5707}
5708
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005709OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5710 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005711 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005712 SourceLocation NameModifierLoc,
5713 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005714 SourceLocation EndLoc) {
5715 Expr *ValExpr = Condition;
5716 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5717 !Condition->isInstantiationDependent() &&
5718 !Condition->containsUnexpandedParameterPack()) {
5719 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005720 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005721 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005722 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005723
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005724 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005725 }
5726
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005727 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5728 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005729}
5730
Alexey Bataev3778b602014-07-17 07:32:53 +00005731OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5732 SourceLocation StartLoc,
5733 SourceLocation LParenLoc,
5734 SourceLocation EndLoc) {
5735 Expr *ValExpr = Condition;
5736 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5737 !Condition->isInstantiationDependent() &&
5738 !Condition->containsUnexpandedParameterPack()) {
5739 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5740 Condition->getExprLoc(), Condition);
5741 if (Val.isInvalid())
5742 return nullptr;
5743
5744 ValExpr = Val.get();
5745 }
5746
5747 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5748}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005749ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5750 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005751 if (!Op)
5752 return ExprError();
5753
5754 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5755 public:
5756 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005757 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005758 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5759 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005760 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5761 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005762 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5763 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005764 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5765 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005766 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5767 QualType T,
5768 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005769 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5770 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005771 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5772 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005773 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005774 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005775 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005776 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5777 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005778 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5779 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005780 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5781 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005782 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005783 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005784 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005785 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5786 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005787 llvm_unreachable("conversion functions are permitted");
5788 }
5789 } ConvertDiagnoser;
5790 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5791}
5792
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005793static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005794 OpenMPClauseKind CKind,
5795 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005796 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5797 !ValExpr->isInstantiationDependent()) {
5798 SourceLocation Loc = ValExpr->getExprLoc();
5799 ExprResult Value =
5800 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5801 if (Value.isInvalid())
5802 return false;
5803
5804 ValExpr = Value.get();
5805 // The expression must evaluate to a non-negative integer value.
5806 llvm::APSInt Result;
5807 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005808 Result.isSigned() &&
5809 !((!StrictlyPositive && Result.isNonNegative()) ||
5810 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005811 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005812 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5813 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005814 return false;
5815 }
5816 }
5817 return true;
5818}
5819
Alexey Bataev568a8332014-03-06 06:15:19 +00005820OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5821 SourceLocation StartLoc,
5822 SourceLocation LParenLoc,
5823 SourceLocation EndLoc) {
5824 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005825
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005826 // OpenMP [2.5, Restrictions]
5827 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005828 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5829 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005830 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005831
Alexey Bataeved09d242014-05-28 05:53:51 +00005832 return new (Context)
5833 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005834}
5835
Alexey Bataev62c87d22014-03-21 04:51:18 +00005836ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005837 OpenMPClauseKind CKind,
5838 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005839 if (!E)
5840 return ExprError();
5841 if (E->isValueDependent() || E->isTypeDependent() ||
5842 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005843 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005844 llvm::APSInt Result;
5845 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5846 if (ICE.isInvalid())
5847 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005848 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
5849 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005850 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005851 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5852 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005853 return ExprError();
5854 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005855 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5856 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5857 << E->getSourceRange();
5858 return ExprError();
5859 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005860 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
5861 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005862 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005863 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005864 return ICE;
5865}
5866
5867OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5868 SourceLocation LParenLoc,
5869 SourceLocation EndLoc) {
5870 // OpenMP [2.8.1, simd construct, Description]
5871 // The parameter of the safelen clause must be a constant
5872 // positive integer expression.
5873 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5874 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005875 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005876 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005877 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005878}
5879
Alexey Bataev66b15b52015-08-21 11:14:16 +00005880OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5881 SourceLocation LParenLoc,
5882 SourceLocation EndLoc) {
5883 // OpenMP [2.8.1, simd construct, Description]
5884 // The parameter of the simdlen clause must be a constant
5885 // positive integer expression.
5886 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5887 if (Simdlen.isInvalid())
5888 return nullptr;
5889 return new (Context)
5890 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5891}
5892
Alexander Musman64d33f12014-06-04 07:53:32 +00005893OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5894 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005895 SourceLocation LParenLoc,
5896 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005897 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005898 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005899 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005900 // The parameter of the collapse clause must be a constant
5901 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005902 ExprResult NumForLoopsResult =
5903 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5904 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005905 return nullptr;
5906 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005907 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005908}
5909
Alexey Bataev10e775f2015-07-30 11:36:16 +00005910OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5911 SourceLocation EndLoc,
5912 SourceLocation LParenLoc,
5913 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005914 // OpenMP [2.7.1, loop construct, Description]
5915 // OpenMP [2.8.1, simd construct, Description]
5916 // OpenMP [2.9.6, distribute construct, Description]
5917 // The parameter of the ordered clause must be a constant
5918 // positive integer expression if any.
5919 if (NumForLoops && LParenLoc.isValid()) {
5920 ExprResult NumForLoopsResult =
5921 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5922 if (NumForLoopsResult.isInvalid())
5923 return nullptr;
5924 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005925 } else
5926 NumForLoops = nullptr;
5927 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005928 return new (Context)
5929 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5930}
5931
Alexey Bataeved09d242014-05-28 05:53:51 +00005932OMPClause *Sema::ActOnOpenMPSimpleClause(
5933 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5934 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005935 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005936 switch (Kind) {
5937 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005938 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005939 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5940 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005941 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005942 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005943 Res = ActOnOpenMPProcBindClause(
5944 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5945 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005946 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005947 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005948 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005949 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005950 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005951 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005952 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005953 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005954 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005955 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005956 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005957 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005958 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005959 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005960 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005961 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005962 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005966 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005967 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005968 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005969 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005970 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005971 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005972 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005973 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005974 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005975 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005976 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005977 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005978 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005979 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005980 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005981 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005982 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005983 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005984 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00005985 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00005986 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005987 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005988 llvm_unreachable("Clause is not allowed.");
5989 }
5990 return Res;
5991}
5992
Alexey Bataev6402bca2015-12-28 07:25:51 +00005993static std::string
5994getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
5995 ArrayRef<unsigned> Exclude = llvm::None) {
5996 std::string Values;
5997 unsigned Bound = Last >= 2 ? Last - 2 : 0;
5998 unsigned Skipped = Exclude.size();
5999 auto S = Exclude.begin(), E = Exclude.end();
6000 for (unsigned i = First; i < Last; ++i) {
6001 if (std::find(S, E, i) != E) {
6002 --Skipped;
6003 continue;
6004 }
6005 Values += "'";
6006 Values += getOpenMPSimpleClauseTypeName(K, i);
6007 Values += "'";
6008 if (i == Bound - Skipped)
6009 Values += " or ";
6010 else if (i != Bound + 1 - Skipped)
6011 Values += ", ";
6012 }
6013 return Values;
6014}
6015
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006016OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6017 SourceLocation KindKwLoc,
6018 SourceLocation StartLoc,
6019 SourceLocation LParenLoc,
6020 SourceLocation EndLoc) {
6021 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006022 static_assert(OMPC_DEFAULT_unknown > 0,
6023 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006024 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006025 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6026 /*Last=*/OMPC_DEFAULT_unknown)
6027 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006028 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006029 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006030 switch (Kind) {
6031 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006032 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006033 break;
6034 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006035 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006036 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006037 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006038 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006039 break;
6040 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006041 return new (Context)
6042 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006043}
6044
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006045OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6046 SourceLocation KindKwLoc,
6047 SourceLocation StartLoc,
6048 SourceLocation LParenLoc,
6049 SourceLocation EndLoc) {
6050 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006051 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006052 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6053 /*Last=*/OMPC_PROC_BIND_unknown)
6054 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006055 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006056 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006057 return new (Context)
6058 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006059}
6060
Alexey Bataev56dafe82014-06-20 07:16:17 +00006061OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006062 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006063 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006064 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006065 SourceLocation EndLoc) {
6066 OMPClause *Res = nullptr;
6067 switch (Kind) {
6068 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006069 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6070 assert(Argument.size() == NumberOfElements &&
6071 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006072 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006073 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6074 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6075 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6076 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6077 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006078 break;
6079 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006080 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6081 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6082 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6083 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006084 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006085 case OMPC_dist_schedule:
6086 Res = ActOnOpenMPDistScheduleClause(
6087 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6088 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6089 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006090 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006091 case OMPC_num_threads:
6092 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006093 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006094 case OMPC_collapse:
6095 case OMPC_default:
6096 case OMPC_proc_bind:
6097 case OMPC_private:
6098 case OMPC_firstprivate:
6099 case OMPC_lastprivate:
6100 case OMPC_shared:
6101 case OMPC_reduction:
6102 case OMPC_linear:
6103 case OMPC_aligned:
6104 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006105 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006106 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006107 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006108 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006109 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006110 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006111 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006112 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006113 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006114 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006115 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006116 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006117 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006118 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006119 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006120 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006121 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006122 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006123 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006124 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006125 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006126 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006127 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006128 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006129 case OMPC_unknown:
6130 llvm_unreachable("Clause is not allowed.");
6131 }
6132 return Res;
6133}
6134
Alexey Bataev6402bca2015-12-28 07:25:51 +00006135static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6136 OpenMPScheduleClauseModifier M2,
6137 SourceLocation M1Loc, SourceLocation M2Loc) {
6138 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6139 SmallVector<unsigned, 2> Excluded;
6140 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6141 Excluded.push_back(M2);
6142 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6143 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6144 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6145 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6146 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6147 << getListOfPossibleValues(OMPC_schedule,
6148 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6149 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6150 Excluded)
6151 << getOpenMPClauseName(OMPC_schedule);
6152 return true;
6153 }
6154 return false;
6155}
6156
Alexey Bataev56dafe82014-06-20 07:16:17 +00006157OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006158 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006159 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006160 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6161 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6162 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6163 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6164 return nullptr;
6165 // OpenMP, 2.7.1, Loop Construct, Restrictions
6166 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6167 // but not both.
6168 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6169 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6170 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6171 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6172 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6173 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6174 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6175 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6176 return nullptr;
6177 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006178 if (Kind == OMPC_SCHEDULE_unknown) {
6179 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006180 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6181 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6182 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6183 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6184 Exclude);
6185 } else {
6186 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6187 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006188 }
6189 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6190 << Values << getOpenMPClauseName(OMPC_schedule);
6191 return nullptr;
6192 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006193 // OpenMP, 2.7.1, Loop Construct, Restrictions
6194 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6195 // schedule(guided).
6196 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6197 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6198 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6199 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6200 diag::err_omp_schedule_nonmonotonic_static);
6201 return nullptr;
6202 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006203 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006204 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006205 if (ChunkSize) {
6206 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6207 !ChunkSize->isInstantiationDependent() &&
6208 !ChunkSize->containsUnexpandedParameterPack()) {
6209 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6210 ExprResult Val =
6211 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6212 if (Val.isInvalid())
6213 return nullptr;
6214
6215 ValExpr = Val.get();
6216
6217 // OpenMP [2.7.1, Restrictions]
6218 // chunk_size must be a loop invariant integer expression with a positive
6219 // value.
6220 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006221 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6222 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6223 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006224 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006225 return nullptr;
6226 }
6227 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6228 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6229 ChunkSize->getType(), ".chunk.");
6230 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6231 ChunkSize->getExprLoc(),
6232 /*RefersToCapture=*/true);
6233 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006234 }
6235 }
6236 }
6237
Alexey Bataev6402bca2015-12-28 07:25:51 +00006238 return new (Context)
6239 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6240 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006241}
6242
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006243OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6244 SourceLocation StartLoc,
6245 SourceLocation EndLoc) {
6246 OMPClause *Res = nullptr;
6247 switch (Kind) {
6248 case OMPC_ordered:
6249 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6250 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006251 case OMPC_nowait:
6252 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6253 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006254 case OMPC_untied:
6255 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6256 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006257 case OMPC_mergeable:
6258 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6259 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006260 case OMPC_read:
6261 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6262 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006263 case OMPC_write:
6264 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6265 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006266 case OMPC_update:
6267 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6268 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006269 case OMPC_capture:
6270 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6271 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006272 case OMPC_seq_cst:
6273 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6274 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006275 case OMPC_threads:
6276 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6277 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006278 case OMPC_simd:
6279 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6280 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006281 case OMPC_nogroup:
6282 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6283 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006284 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006285 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006286 case OMPC_num_threads:
6287 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006288 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006289 case OMPC_collapse:
6290 case OMPC_schedule:
6291 case OMPC_private:
6292 case OMPC_firstprivate:
6293 case OMPC_lastprivate:
6294 case OMPC_shared:
6295 case OMPC_reduction:
6296 case OMPC_linear:
6297 case OMPC_aligned:
6298 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006299 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006300 case OMPC_default:
6301 case OMPC_proc_bind:
6302 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006303 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006304 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006305 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006306 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006307 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006308 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006309 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006310 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006311 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006312 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006313 case OMPC_dist_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006314 case OMPC_unknown:
6315 llvm_unreachable("Clause is not allowed.");
6316 }
6317 return Res;
6318}
6319
Alexey Bataev236070f2014-06-20 11:19:47 +00006320OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6321 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006322 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006323 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6324}
6325
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006326OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6327 SourceLocation EndLoc) {
6328 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6329}
6330
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006331OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6332 SourceLocation EndLoc) {
6333 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6334}
6335
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006336OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6337 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006338 return new (Context) OMPReadClause(StartLoc, EndLoc);
6339}
6340
Alexey Bataevdea47612014-07-23 07:46:59 +00006341OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6342 SourceLocation EndLoc) {
6343 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6344}
6345
Alexey Bataev67a4f222014-07-23 10:25:33 +00006346OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6347 SourceLocation EndLoc) {
6348 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6349}
6350
Alexey Bataev459dec02014-07-24 06:46:57 +00006351OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6352 SourceLocation EndLoc) {
6353 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6354}
6355
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006356OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6357 SourceLocation EndLoc) {
6358 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6359}
6360
Alexey Bataev346265e2015-09-25 10:37:12 +00006361OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6362 SourceLocation EndLoc) {
6363 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6364}
6365
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006366OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6367 SourceLocation EndLoc) {
6368 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6369}
6370
Alexey Bataevb825de12015-12-07 10:51:44 +00006371OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6372 SourceLocation EndLoc) {
6373 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6374}
6375
Alexey Bataevc5e02582014-06-16 07:08:35 +00006376OMPClause *Sema::ActOnOpenMPVarListClause(
6377 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6378 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6379 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006380 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006381 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6382 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006383 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006384 switch (Kind) {
6385 case OMPC_private:
6386 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6387 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006388 case OMPC_firstprivate:
6389 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6390 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006391 case OMPC_lastprivate:
6392 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6393 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006394 case OMPC_shared:
6395 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6396 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006397 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006398 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6399 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006400 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006401 case OMPC_linear:
6402 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006403 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006404 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006405 case OMPC_aligned:
6406 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6407 ColonLoc, EndLoc);
6408 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006409 case OMPC_copyin:
6410 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6411 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006412 case OMPC_copyprivate:
6413 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6414 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006415 case OMPC_flush:
6416 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6417 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006418 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006419 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6420 StartLoc, LParenLoc, EndLoc);
6421 break;
6422 case OMPC_map:
6423 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6424 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006425 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006426 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006427 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006428 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006429 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006430 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006431 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006432 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006433 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006434 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006435 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006436 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006437 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006438 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006439 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006440 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006441 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006442 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006443 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006444 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006445 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006446 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006447 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006448 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006449 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006450 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006451 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006452 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006453 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006454 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006455 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006456 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006457 llvm_unreachable("Clause is not allowed.");
6458 }
6459 return Res;
6460}
6461
6462OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6463 SourceLocation StartLoc,
6464 SourceLocation LParenLoc,
6465 SourceLocation EndLoc) {
6466 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006467 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006468 for (auto &RefExpr : VarList) {
6469 assert(RefExpr && "NULL expr in OpenMP private clause.");
6470 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006471 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006472 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006473 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006474 continue;
6475 }
6476
Alexey Bataeved09d242014-05-28 05:53:51 +00006477 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006478 // OpenMP [2.1, C/C++]
6479 // A list item is a variable name.
6480 // OpenMP [2.9.3.3, Restrictions, p.1]
6481 // A variable that is part of another variable (as an array or
6482 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006483 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006484 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006485 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006486 continue;
6487 }
6488 Decl *D = DE->getDecl();
6489 VarDecl *VD = cast<VarDecl>(D);
6490
6491 QualType Type = VD->getType();
6492 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6493 // It will be analyzed later.
6494 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006495 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006496 continue;
6497 }
6498
6499 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6500 // A variable that appears in a private clause must not have an incomplete
6501 // type or a reference type.
6502 if (RequireCompleteType(ELoc, Type,
6503 diag::err_omp_private_incomplete_type)) {
6504 continue;
6505 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006506 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006507
Alexey Bataev758e55e2013-09-06 18:03:48 +00006508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6509 // in a Construct]
6510 // Variables with the predetermined data-sharing attributes may not be
6511 // listed in data-sharing attributes clauses, except for the cases
6512 // listed below. For these exceptions only, listing a predetermined
6513 // variable in a data-sharing attribute clause is allowed and overrides
6514 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006515 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006516 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006517 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6518 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006519 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006520 continue;
6521 }
6522
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006523 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006524 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006525 DSAStack->getCurrentDirective() == OMPD_task) {
6526 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6527 << getOpenMPClauseName(OMPC_private) << Type
6528 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6529 bool IsDecl =
6530 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6531 Diag(VD->getLocation(),
6532 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6533 << VD;
6534 continue;
6535 }
6536
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006537 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6538 // A variable of class type (or array thereof) that appears in a private
6539 // clause requires an accessible, unambiguous default constructor for the
6540 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006541 // Generate helper private variable and initialize it with the default
6542 // value. The address of the original variable is replaced by the address of
6543 // the new private variable in CodeGen. This new variable is not added to
6544 // IdResolver, so the code in the OpenMP region uses original variable for
6545 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006546 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006547 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6548 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006549 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006550 if (VDPrivate->isInvalidDecl())
6551 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006552 auto VDPrivateRefExpr = buildDeclRefExpr(
6553 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006554
Alexey Bataev758e55e2013-09-06 18:03:48 +00006555 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006556 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006557 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006558 }
6559
Alexey Bataeved09d242014-05-28 05:53:51 +00006560 if (Vars.empty())
6561 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006562
Alexey Bataev03b340a2014-10-21 03:16:40 +00006563 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6564 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006565}
6566
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006567namespace {
6568class DiagsUninitializedSeveretyRAII {
6569private:
6570 DiagnosticsEngine &Diags;
6571 SourceLocation SavedLoc;
6572 bool IsIgnored;
6573
6574public:
6575 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6576 bool IsIgnored)
6577 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6578 if (!IsIgnored) {
6579 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6580 /*Map*/ diag::Severity::Ignored, Loc);
6581 }
6582 }
6583 ~DiagsUninitializedSeveretyRAII() {
6584 if (!IsIgnored)
6585 Diags.popMappings(SavedLoc);
6586 }
6587};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006588}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006589
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006590OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6591 SourceLocation StartLoc,
6592 SourceLocation LParenLoc,
6593 SourceLocation EndLoc) {
6594 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006595 SmallVector<Expr *, 8> PrivateCopies;
6596 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006597 bool IsImplicitClause =
6598 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6599 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6600
Alexey Bataeved09d242014-05-28 05:53:51 +00006601 for (auto &RefExpr : VarList) {
6602 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6603 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006604 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006605 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006606 PrivateCopies.push_back(nullptr);
6607 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006608 continue;
6609 }
6610
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006611 SourceLocation ELoc =
6612 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006613 // OpenMP [2.1, C/C++]
6614 // A list item is a variable name.
6615 // OpenMP [2.9.3.3, Restrictions, p.1]
6616 // A variable that is part of another variable (as an array or
6617 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006618 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006619 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006620 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006621 continue;
6622 }
6623 Decl *D = DE->getDecl();
6624 VarDecl *VD = cast<VarDecl>(D);
6625
6626 QualType Type = VD->getType();
6627 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6628 // It will be analyzed later.
6629 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006630 PrivateCopies.push_back(nullptr);
6631 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006632 continue;
6633 }
6634
6635 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6636 // A variable that appears in a private clause must not have an incomplete
6637 // type or a reference type.
6638 if (RequireCompleteType(ELoc, Type,
6639 diag::err_omp_firstprivate_incomplete_type)) {
6640 continue;
6641 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006642 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006643
6644 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6645 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006646 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006647 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006648 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006649
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006650 // If an implicit firstprivate variable found it was checked already.
6651 if (!IsImplicitClause) {
6652 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006653 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006654 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6655 // A list item that specifies a given variable may not appear in more
6656 // than one clause on the same directive, except that a variable may be
6657 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006658 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006659 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006660 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006661 << getOpenMPClauseName(DVar.CKind)
6662 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006663 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006664 continue;
6665 }
6666
6667 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6668 // in a Construct]
6669 // Variables with the predetermined data-sharing attributes may not be
6670 // listed in data-sharing attributes clauses, except for the cases
6671 // listed below. For these exceptions only, listing a predetermined
6672 // variable in a data-sharing attribute clause is allowed and overrides
6673 // the variable's predetermined data-sharing attributes.
6674 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6675 // in a Construct, C/C++, p.2]
6676 // Variables with const-qualified type having no mutable member may be
6677 // listed in a firstprivate clause, even if they are static data members.
6678 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6679 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6680 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006681 << getOpenMPClauseName(DVar.CKind)
6682 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006683 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006684 continue;
6685 }
6686
Alexey Bataevf29276e2014-06-18 04:14:57 +00006687 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006688 // OpenMP [2.9.3.4, Restrictions, p.2]
6689 // A list item that is private within a parallel region must not appear
6690 // in a firstprivate clause on a worksharing construct if any of the
6691 // worksharing regions arising from the worksharing construct ever bind
6692 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006693 if (isOpenMPWorksharingDirective(CurrDir) &&
6694 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006695 DVar = DSAStack->getImplicitDSA(VD, true);
6696 if (DVar.CKind != OMPC_shared &&
6697 (isOpenMPParallelDirective(DVar.DKind) ||
6698 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006699 Diag(ELoc, diag::err_omp_required_access)
6700 << getOpenMPClauseName(OMPC_firstprivate)
6701 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006702 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006703 continue;
6704 }
6705 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006706 // OpenMP [2.9.3.4, Restrictions, p.3]
6707 // A list item that appears in a reduction clause of a parallel construct
6708 // must not appear in a firstprivate clause on a worksharing or task
6709 // construct if any of the worksharing or task regions arising from the
6710 // worksharing or task construct ever bind to any of the parallel regions
6711 // arising from the parallel construct.
6712 // OpenMP [2.9.3.4, Restrictions, p.4]
6713 // A list item that appears in a reduction clause in worksharing
6714 // construct must not appear in a firstprivate clause in a task construct
6715 // encountered during execution of any of the worksharing regions arising
6716 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006717 if (CurrDir == OMPD_task) {
6718 DVar =
6719 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6720 [](OpenMPDirectiveKind K) -> bool {
6721 return isOpenMPParallelDirective(K) ||
6722 isOpenMPWorksharingDirective(K);
6723 },
6724 false);
6725 if (DVar.CKind == OMPC_reduction &&
6726 (isOpenMPParallelDirective(DVar.DKind) ||
6727 isOpenMPWorksharingDirective(DVar.DKind))) {
6728 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6729 << getOpenMPDirectiveName(DVar.DKind);
6730 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6731 continue;
6732 }
6733 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006734
6735 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6736 // A list item that is private within a teams region must not appear in a
6737 // firstprivate clause on a distribute construct if any of the distribute
6738 // regions arising from the distribute construct ever bind to any of the
6739 // teams regions arising from the teams construct.
6740 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6741 // A list item that appears in a reduction clause of a teams construct
6742 // must not appear in a firstprivate clause on a distribute construct if
6743 // any of the distribute regions arising from the distribute construct
6744 // ever bind to any of the teams regions arising from the teams construct.
6745 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6746 // A list item may appear in a firstprivate or lastprivate clause but not
6747 // both.
6748 if (CurrDir == OMPD_distribute) {
6749 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6750 [](OpenMPDirectiveKind K) -> bool {
6751 return isOpenMPTeamsDirective(K);
6752 },
6753 false);
6754 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6755 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6756 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6757 continue;
6758 }
6759 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6760 [](OpenMPDirectiveKind K) -> bool {
6761 return isOpenMPTeamsDirective(K);
6762 },
6763 false);
6764 if (DVar.CKind == OMPC_reduction &&
6765 isOpenMPTeamsDirective(DVar.DKind)) {
6766 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6767 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6768 continue;
6769 }
6770 DVar = DSAStack->getTopDSA(VD, false);
6771 if (DVar.CKind == OMPC_lastprivate) {
6772 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6773 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6774 continue;
6775 }
6776 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006777 }
6778
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006779 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006780 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006781 DSAStack->getCurrentDirective() == OMPD_task) {
6782 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6783 << getOpenMPClauseName(OMPC_firstprivate) << Type
6784 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6785 bool IsDecl =
6786 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6787 Diag(VD->getLocation(),
6788 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6789 << VD;
6790 continue;
6791 }
6792
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006793 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006794 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6795 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006796 // Generate helper private variable and initialize it with the value of the
6797 // original variable. The address of the original variable is replaced by
6798 // the address of the new private variable in the CodeGen. This new variable
6799 // is not added to IdResolver, so the code in the OpenMP region uses
6800 // original variable for proper diagnostics and variable capturing.
6801 Expr *VDInitRefExpr = nullptr;
6802 // For arrays generate initializer for single element and replace it by the
6803 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006804 if (Type->isArrayType()) {
6805 auto VDInit =
6806 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6807 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006808 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006809 ElemType = ElemType.getUnqualifiedType();
6810 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6811 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006812 InitializedEntity Entity =
6813 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006814 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6815
6816 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6817 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6818 if (Result.isInvalid())
6819 VDPrivate->setInvalidDecl();
6820 else
6821 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006822 // Remove temp variable declaration.
6823 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006824 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006825 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006826 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006827 VDInitRefExpr =
6828 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006829 AddInitializerToDecl(VDPrivate,
6830 DefaultLvalueConversion(VDInitRefExpr).get(),
6831 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006832 }
6833 if (VDPrivate->isInvalidDecl()) {
6834 if (IsImplicitClause) {
6835 Diag(DE->getExprLoc(),
6836 diag::note_omp_task_predetermined_firstprivate_here);
6837 }
6838 continue;
6839 }
6840 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006841 auto VDPrivateRefExpr = buildDeclRefExpr(
6842 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006843 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6844 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006845 PrivateCopies.push_back(VDPrivateRefExpr);
6846 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006847 }
6848
Alexey Bataeved09d242014-05-28 05:53:51 +00006849 if (Vars.empty())
6850 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006851
6852 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006853 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006854}
6855
Alexander Musman1bb328c2014-06-04 13:06:39 +00006856OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6857 SourceLocation StartLoc,
6858 SourceLocation LParenLoc,
6859 SourceLocation EndLoc) {
6860 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006861 SmallVector<Expr *, 8> SrcExprs;
6862 SmallVector<Expr *, 8> DstExprs;
6863 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006864 for (auto &RefExpr : VarList) {
6865 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6866 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6867 // It will be analyzed later.
6868 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006869 SrcExprs.push_back(nullptr);
6870 DstExprs.push_back(nullptr);
6871 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006872 continue;
6873 }
6874
6875 SourceLocation ELoc = RefExpr->getExprLoc();
6876 // OpenMP [2.1, C/C++]
6877 // A list item is a variable name.
6878 // OpenMP [2.14.3.5, Restrictions, p.1]
6879 // A variable that is part of another variable (as an array or structure
6880 // element) cannot appear in a lastprivate clause.
6881 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6882 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6883 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6884 continue;
6885 }
6886 Decl *D = DE->getDecl();
6887 VarDecl *VD = cast<VarDecl>(D);
6888
6889 QualType Type = VD->getType();
6890 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6891 // It will be analyzed later.
6892 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006893 SrcExprs.push_back(nullptr);
6894 DstExprs.push_back(nullptr);
6895 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006896 continue;
6897 }
6898
6899 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6900 // A variable that appears in a lastprivate clause must not have an
6901 // incomplete type or a reference type.
6902 if (RequireCompleteType(ELoc, Type,
6903 diag::err_omp_lastprivate_incomplete_type)) {
6904 continue;
6905 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006906 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006907
6908 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6909 // in a Construct]
6910 // Variables with the predetermined data-sharing attributes may not be
6911 // listed in data-sharing attributes clauses, except for the cases
6912 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006913 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006914 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6915 DVar.CKind != OMPC_firstprivate &&
6916 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6917 Diag(ELoc, diag::err_omp_wrong_dsa)
6918 << getOpenMPClauseName(DVar.CKind)
6919 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006920 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006921 continue;
6922 }
6923
Alexey Bataevf29276e2014-06-18 04:14:57 +00006924 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6925 // OpenMP [2.14.3.5, Restrictions, p.2]
6926 // A list item that is private within a parallel region, or that appears in
6927 // the reduction clause of a parallel construct, must not appear in a
6928 // lastprivate clause on a worksharing construct if any of the corresponding
6929 // worksharing regions ever binds to any of the corresponding parallel
6930 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006931 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006932 if (isOpenMPWorksharingDirective(CurrDir) &&
6933 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006934 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006935 if (DVar.CKind != OMPC_shared) {
6936 Diag(ELoc, diag::err_omp_required_access)
6937 << getOpenMPClauseName(OMPC_lastprivate)
6938 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006939 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006940 continue;
6941 }
6942 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006943 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006944 // A variable of class type (or array thereof) that appears in a
6945 // lastprivate clause requires an accessible, unambiguous default
6946 // constructor for the class type, unless the list item is also specified
6947 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006948 // A variable of class type (or array thereof) that appears in a
6949 // lastprivate clause requires an accessible, unambiguous copy assignment
6950 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006951 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006952 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006953 Type.getUnqualifiedType(), ".lastprivate.src",
6954 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006955 auto *PseudoSrcExpr = buildDeclRefExpr(
6956 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006957 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006958 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6959 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006960 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006961 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006962 // For arrays generate assignment operation for single element and replace
6963 // it by the original array element in CodeGen.
6964 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6965 PseudoDstExpr, PseudoSrcExpr);
6966 if (AssignmentOp.isInvalid())
6967 continue;
6968 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6969 /*DiscardedValue=*/true);
6970 if (AssignmentOp.isInvalid())
6971 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006972
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006973 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6974 // A list item may appear in a firstprivate or lastprivate clause but not
6975 // both.
6976 if (CurrDir == OMPD_distribute) {
6977 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6978 if (DVar.CKind == OMPC_firstprivate) {
6979 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6980 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6981 continue;
6982 }
6983 }
6984
Alexey Bataev39f915b82015-05-08 10:41:21 +00006985 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006986 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006987 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006988 SrcExprs.push_back(PseudoSrcExpr);
6989 DstExprs.push_back(PseudoDstExpr);
6990 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006991 }
6992
6993 if (Vars.empty())
6994 return nullptr;
6995
6996 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006997 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006998}
6999
Alexey Bataev758e55e2013-09-06 18:03:48 +00007000OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7001 SourceLocation StartLoc,
7002 SourceLocation LParenLoc,
7003 SourceLocation EndLoc) {
7004 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007005 for (auto &RefExpr : VarList) {
7006 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7007 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007008 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007009 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007010 continue;
7011 }
7012
Alexey Bataeved09d242014-05-28 05:53:51 +00007013 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007014 // OpenMP [2.1, C/C++]
7015 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007016 // OpenMP [2.14.3.2, Restrictions, p.1]
7017 // A variable that is part of another variable (as an array or structure
7018 // element) cannot appear in a shared unless it is a static data member
7019 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007020 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007021 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007022 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007023 continue;
7024 }
7025 Decl *D = DE->getDecl();
7026 VarDecl *VD = cast<VarDecl>(D);
7027
7028 QualType Type = VD->getType();
7029 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7030 // It will be analyzed later.
7031 Vars.push_back(DE);
7032 continue;
7033 }
7034
7035 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7036 // in a Construct]
7037 // Variables with the predetermined data-sharing attributes may not be
7038 // listed in data-sharing attributes clauses, except for the cases
7039 // listed below. For these exceptions only, listing a predetermined
7040 // variable in a data-sharing attribute clause is allowed and overrides
7041 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007042 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007043 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7044 DVar.RefExpr) {
7045 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7046 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007047 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007048 continue;
7049 }
7050
7051 DSAStack->addDSA(VD, DE, OMPC_shared);
7052 Vars.push_back(DE);
7053 }
7054
Alexey Bataeved09d242014-05-28 05:53:51 +00007055 if (Vars.empty())
7056 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007057
7058 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7059}
7060
Alexey Bataevc5e02582014-06-16 07:08:35 +00007061namespace {
7062class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7063 DSAStackTy *Stack;
7064
7065public:
7066 bool VisitDeclRefExpr(DeclRefExpr *E) {
7067 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007068 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007069 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7070 return false;
7071 if (DVar.CKind != OMPC_unknown)
7072 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007073 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007074 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007075 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007076 return true;
7077 return false;
7078 }
7079 return false;
7080 }
7081 bool VisitStmt(Stmt *S) {
7082 for (auto Child : S->children()) {
7083 if (Child && Visit(Child))
7084 return true;
7085 }
7086 return false;
7087 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007088 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007089};
Alexey Bataev23b69422014-06-18 07:08:49 +00007090} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007091
7092OMPClause *Sema::ActOnOpenMPReductionClause(
7093 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7094 SourceLocation ColonLoc, SourceLocation EndLoc,
7095 CXXScopeSpec &ReductionIdScopeSpec,
7096 const DeclarationNameInfo &ReductionId) {
7097 // TODO: Allow scope specification search when 'declare reduction' is
7098 // supported.
7099 assert(ReductionIdScopeSpec.isEmpty() &&
7100 "No support for scoped reduction identifiers yet.");
7101
7102 auto DN = ReductionId.getName();
7103 auto OOK = DN.getCXXOverloadedOperator();
7104 BinaryOperatorKind BOK = BO_Comma;
7105
7106 // OpenMP [2.14.3.6, reduction clause]
7107 // C
7108 // reduction-identifier is either an identifier or one of the following
7109 // operators: +, -, *, &, |, ^, && and ||
7110 // C++
7111 // reduction-identifier is either an id-expression or one of the following
7112 // operators: +, -, *, &, |, ^, && and ||
7113 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7114 switch (OOK) {
7115 case OO_Plus:
7116 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007117 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007118 break;
7119 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007120 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007121 break;
7122 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007123 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007124 break;
7125 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007126 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007127 break;
7128 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007129 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007130 break;
7131 case OO_AmpAmp:
7132 BOK = BO_LAnd;
7133 break;
7134 case OO_PipePipe:
7135 BOK = BO_LOr;
7136 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007137 case OO_New:
7138 case OO_Delete:
7139 case OO_Array_New:
7140 case OO_Array_Delete:
7141 case OO_Slash:
7142 case OO_Percent:
7143 case OO_Tilde:
7144 case OO_Exclaim:
7145 case OO_Equal:
7146 case OO_Less:
7147 case OO_Greater:
7148 case OO_LessEqual:
7149 case OO_GreaterEqual:
7150 case OO_PlusEqual:
7151 case OO_MinusEqual:
7152 case OO_StarEqual:
7153 case OO_SlashEqual:
7154 case OO_PercentEqual:
7155 case OO_CaretEqual:
7156 case OO_AmpEqual:
7157 case OO_PipeEqual:
7158 case OO_LessLess:
7159 case OO_GreaterGreater:
7160 case OO_LessLessEqual:
7161 case OO_GreaterGreaterEqual:
7162 case OO_EqualEqual:
7163 case OO_ExclaimEqual:
7164 case OO_PlusPlus:
7165 case OO_MinusMinus:
7166 case OO_Comma:
7167 case OO_ArrowStar:
7168 case OO_Arrow:
7169 case OO_Call:
7170 case OO_Subscript:
7171 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007172 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007173 case NUM_OVERLOADED_OPERATORS:
7174 llvm_unreachable("Unexpected reduction identifier");
7175 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007176 if (auto II = DN.getAsIdentifierInfo()) {
7177 if (II->isStr("max"))
7178 BOK = BO_GT;
7179 else if (II->isStr("min"))
7180 BOK = BO_LT;
7181 }
7182 break;
7183 }
7184 SourceRange ReductionIdRange;
7185 if (ReductionIdScopeSpec.isValid()) {
7186 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7187 }
7188 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7189 if (BOK == BO_Comma) {
7190 // Not allowed reduction identifier is found.
7191 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7192 << ReductionIdRange;
7193 return nullptr;
7194 }
7195
7196 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007197 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007198 SmallVector<Expr *, 8> LHSs;
7199 SmallVector<Expr *, 8> RHSs;
7200 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007201 for (auto RefExpr : VarList) {
7202 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7203 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7204 // It will be analyzed later.
7205 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007206 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007207 LHSs.push_back(nullptr);
7208 RHSs.push_back(nullptr);
7209 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007210 continue;
7211 }
7212
7213 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7214 RefExpr->isInstantiationDependent() ||
7215 RefExpr->containsUnexpandedParameterPack()) {
7216 // It will be analyzed later.
7217 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007218 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007219 LHSs.push_back(nullptr);
7220 RHSs.push_back(nullptr);
7221 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007222 continue;
7223 }
7224
7225 auto ELoc = RefExpr->getExprLoc();
7226 auto ERange = RefExpr->getSourceRange();
7227 // OpenMP [2.1, C/C++]
7228 // A list item is a variable or array section, subject to the restrictions
7229 // specified in Section 2.4 on page 42 and in each of the sections
7230 // describing clauses and directives for which a list appears.
7231 // OpenMP [2.14.3.3, Restrictions, p.1]
7232 // A variable that is part of another variable (as an array or
7233 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007234 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7235 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7236 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7237 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7238 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007239 continue;
7240 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007241 QualType Type;
7242 VarDecl *VD = nullptr;
7243 if (DE) {
7244 auto D = DE->getDecl();
7245 VD = cast<VarDecl>(D);
7246 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007247 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007248 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007249 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7250 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7251 Base = TempASE->getBase()->IgnoreParenImpCasts();
7252 DE = dyn_cast<DeclRefExpr>(Base);
7253 if (DE)
7254 VD = dyn_cast<VarDecl>(DE->getDecl());
7255 if (!VD) {
7256 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7257 << 0 << Base->getSourceRange();
7258 continue;
7259 }
7260 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007261 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7262 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7263 Type = ATy->getElementType();
7264 else
7265 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007266 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7267 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7268 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7269 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7270 Base = TempASE->getBase()->IgnoreParenImpCasts();
7271 DE = dyn_cast<DeclRefExpr>(Base);
7272 if (DE)
7273 VD = dyn_cast<VarDecl>(DE->getDecl());
7274 if (!VD) {
7275 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7276 << 1 << Base->getSourceRange();
7277 continue;
7278 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007279 }
7280
Alexey Bataevc5e02582014-06-16 07:08:35 +00007281 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7282 // A variable that appears in a private clause must not have an incomplete
7283 // type or a reference type.
7284 if (RequireCompleteType(ELoc, Type,
7285 diag::err_omp_reduction_incomplete_type))
7286 continue;
7287 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7288 // Arrays may not appear in a reduction clause.
7289 if (Type.getNonReferenceType()->isArrayType()) {
7290 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007291 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007292 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7293 VarDecl::DeclarationOnly;
7294 Diag(VD->getLocation(),
7295 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7296 << VD;
7297 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007298 continue;
7299 }
7300 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7301 // A list item that appears in a reduction clause must not be
7302 // const-qualified.
7303 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007304 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007305 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007306 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007307 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7308 VarDecl::DeclarationOnly;
7309 Diag(VD->getLocation(),
7310 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7311 << VD;
7312 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007313 continue;
7314 }
7315 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7316 // If a list-item is a reference type then it must bind to the same object
7317 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007318 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007319 VarDecl *VDDef = VD->getDefinition();
7320 if (Type->isReferenceType() && VDDef) {
7321 DSARefChecker Check(DSAStack);
7322 if (Check.Visit(VDDef->getInit())) {
7323 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7324 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7325 continue;
7326 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007327 }
7328 }
7329 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7330 // The type of a list item that appears in a reduction clause must be valid
7331 // for the reduction-identifier. For a max or min reduction in C, the type
7332 // of the list item must be an allowed arithmetic data type: char, int,
7333 // float, double, or _Bool, possibly modified with long, short, signed, or
7334 // unsigned. For a max or min reduction in C++, the type of the list item
7335 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7336 // double, or bool, possibly modified with long, short, signed, or unsigned.
7337 if ((BOK == BO_GT || BOK == BO_LT) &&
7338 !(Type->isScalarType() ||
7339 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7340 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7341 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007342 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007343 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7344 VarDecl::DeclarationOnly;
7345 Diag(VD->getLocation(),
7346 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7347 << VD;
7348 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007349 continue;
7350 }
7351 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7352 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7353 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007354 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007355 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7356 VarDecl::DeclarationOnly;
7357 Diag(VD->getLocation(),
7358 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7359 << VD;
7360 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007361 continue;
7362 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007363 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7364 // in a Construct]
7365 // Variables with the predetermined data-sharing attributes may not be
7366 // listed in data-sharing attributes clauses, except for the cases
7367 // listed below. For these exceptions only, listing a predetermined
7368 // variable in a data-sharing attribute clause is allowed and overrides
7369 // the variable's predetermined data-sharing attributes.
7370 // OpenMP [2.14.3.6, Restrictions, p.3]
7371 // Any number of reduction clauses can be specified on the directive,
7372 // but a list item can appear only once in the reduction clauses for that
7373 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007374 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007375 DVar = DSAStack->getTopDSA(VD, false);
7376 if (DVar.CKind == OMPC_reduction) {
7377 Diag(ELoc, diag::err_omp_once_referenced)
7378 << getOpenMPClauseName(OMPC_reduction);
7379 if (DVar.RefExpr) {
7380 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007381 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007382 } else if (DVar.CKind != OMPC_unknown) {
7383 Diag(ELoc, diag::err_omp_wrong_dsa)
7384 << getOpenMPClauseName(DVar.CKind)
7385 << getOpenMPClauseName(OMPC_reduction);
7386 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7387 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007388 }
7389
7390 // OpenMP [2.14.3.6, Restrictions, p.1]
7391 // A list item that appears in a reduction clause of a worksharing
7392 // construct must be shared in the parallel regions to which any of the
7393 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007394 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7395 if (isOpenMPWorksharingDirective(CurrDir) &&
7396 !isOpenMPParallelDirective(CurrDir)) {
7397 DVar = DSAStack->getImplicitDSA(VD, true);
7398 if (DVar.CKind != OMPC_shared) {
7399 Diag(ELoc, diag::err_omp_required_access)
7400 << getOpenMPClauseName(OMPC_reduction)
7401 << getOpenMPClauseName(OMPC_shared);
7402 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7403 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007404 }
7405 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007406
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007407 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007408 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7409 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7410 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7411 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7412 auto PrivateTy = Type;
7413 if (OASE) {
7414 // For array sections only:
7415 // Create pseudo array type for private copy. The size for this array will
7416 // be generated during codegen.
7417 // For array subscripts or single variables Private Ty is the same as Type
7418 // (type of the variable or single array element).
7419 PrivateTy = Context.getVariableArrayType(
7420 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7421 Context.getSizeType(), VK_RValue),
7422 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7423 }
7424 // Private copy.
7425 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7426 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007427 // Add initializer for private variable.
7428 Expr *Init = nullptr;
7429 switch (BOK) {
7430 case BO_Add:
7431 case BO_Xor:
7432 case BO_Or:
7433 case BO_LOr:
7434 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7435 if (Type->isScalarType() || Type->isAnyComplexType()) {
7436 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007437 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007438 break;
7439 case BO_Mul:
7440 case BO_LAnd:
7441 if (Type->isScalarType() || Type->isAnyComplexType()) {
7442 // '*' and '&&' reduction ops - initializer is '1'.
7443 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7444 }
7445 break;
7446 case BO_And: {
7447 // '&' reduction op - initializer is '~0'.
7448 QualType OrigType = Type;
7449 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7450 Type = ComplexTy->getElementType();
7451 }
7452 if (Type->isRealFloatingType()) {
7453 llvm::APFloat InitValue =
7454 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7455 /*isIEEE=*/true);
7456 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7457 Type, ELoc);
7458 } else if (Type->isScalarType()) {
7459 auto Size = Context.getTypeSize(Type);
7460 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7461 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7462 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7463 }
7464 if (Init && OrigType->isAnyComplexType()) {
7465 // Init = 0xFFFF + 0xFFFFi;
7466 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7467 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7468 }
7469 Type = OrigType;
7470 break;
7471 }
7472 case BO_LT:
7473 case BO_GT: {
7474 // 'min' reduction op - initializer is 'Largest representable number in
7475 // the reduction list item type'.
7476 // 'max' reduction op - initializer is 'Least representable number in
7477 // the reduction list item type'.
7478 if (Type->isIntegerType() || Type->isPointerType()) {
7479 bool IsSigned = Type->hasSignedIntegerRepresentation();
7480 auto Size = Context.getTypeSize(Type);
7481 QualType IntTy =
7482 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7483 llvm::APInt InitValue =
7484 (BOK != BO_LT)
7485 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7486 : llvm::APInt::getMinValue(Size)
7487 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7488 : llvm::APInt::getMaxValue(Size);
7489 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7490 if (Type->isPointerType()) {
7491 // Cast to pointer type.
7492 auto CastExpr = BuildCStyleCastExpr(
7493 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7494 SourceLocation(), Init);
7495 if (CastExpr.isInvalid())
7496 continue;
7497 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007498 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007499 } else if (Type->isRealFloatingType()) {
7500 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7501 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7502 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7503 Type, ELoc);
7504 }
7505 break;
7506 }
7507 case BO_PtrMemD:
7508 case BO_PtrMemI:
7509 case BO_MulAssign:
7510 case BO_Div:
7511 case BO_Rem:
7512 case BO_Sub:
7513 case BO_Shl:
7514 case BO_Shr:
7515 case BO_LE:
7516 case BO_GE:
7517 case BO_EQ:
7518 case BO_NE:
7519 case BO_AndAssign:
7520 case BO_XorAssign:
7521 case BO_OrAssign:
7522 case BO_Assign:
7523 case BO_AddAssign:
7524 case BO_SubAssign:
7525 case BO_DivAssign:
7526 case BO_RemAssign:
7527 case BO_ShlAssign:
7528 case BO_ShrAssign:
7529 case BO_Comma:
7530 llvm_unreachable("Unexpected reduction operation");
7531 }
7532 if (Init) {
7533 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7534 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007535 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007536 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007537 if (!RHSVD->hasInit()) {
7538 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7539 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007540 if (VD) {
7541 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7542 VarDecl::DeclarationOnly;
7543 Diag(VD->getLocation(),
7544 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7545 << VD;
7546 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007547 continue;
7548 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007549 // Store initializer for single element in private copy. Will be used during
7550 // codegen.
7551 PrivateVD->setInit(RHSVD->getInit());
7552 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007553 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7554 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007555 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007556 ExprResult ReductionOp =
7557 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7558 LHSDRE, RHSDRE);
7559 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007560 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007561 ReductionOp =
7562 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7563 BO_Assign, LHSDRE, ReductionOp.get());
7564 } else {
7565 auto *ConditionalOp = new (Context) ConditionalOperator(
7566 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7567 RHSDRE, Type, VK_LValue, OK_Ordinary);
7568 ReductionOp =
7569 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7570 BO_Assign, LHSDRE, ConditionalOp);
7571 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007572 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007573 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007574 if (ReductionOp.isInvalid())
7575 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007576
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007577 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007578 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007579 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007580 LHSs.push_back(LHSDRE);
7581 RHSs.push_back(RHSDRE);
7582 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007583 }
7584
7585 if (Vars.empty())
7586 return nullptr;
7587
7588 return OMPReductionClause::Create(
7589 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007590 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7591 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007592}
7593
Alexey Bataev182227b2015-08-20 10:54:39 +00007594OMPClause *Sema::ActOnOpenMPLinearClause(
7595 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7596 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7597 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007598 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007599 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007600 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007601 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7602 LinKind == OMPC_LINEAR_unknown) {
7603 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7604 LinKind = OMPC_LINEAR_val;
7605 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007606 for (auto &RefExpr : VarList) {
7607 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7608 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007609 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007610 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007611 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007612 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007613 continue;
7614 }
7615
7616 // OpenMP [2.14.3.7, linear clause]
7617 // A list item that appears in a linear clause is subject to the private
7618 // clause semantics described in Section 2.14.3.3 on page 159 except as
7619 // noted. In addition, the value of the new list item on each iteration
7620 // of the associated loop(s) corresponds to the value of the original
7621 // list item before entering the construct plus the logical number of
7622 // the iteration times linear-step.
7623
Alexey Bataeved09d242014-05-28 05:53:51 +00007624 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007625 // OpenMP [2.1, C/C++]
7626 // A list item is a variable name.
7627 // OpenMP [2.14.3.3, Restrictions, p.1]
7628 // A variable that is part of another variable (as an array or
7629 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007630 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007631 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007632 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007633 continue;
7634 }
7635
7636 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7637
7638 // OpenMP [2.14.3.7, linear clause]
7639 // A list-item cannot appear in more than one linear clause.
7640 // A list-item that appears in a linear clause cannot appear in any
7641 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007642 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007643 if (DVar.RefExpr) {
7644 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7645 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007646 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007647 continue;
7648 }
7649
7650 QualType QType = VD->getType();
7651 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7652 // It will be analyzed later.
7653 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007654 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007655 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007656 continue;
7657 }
7658
7659 // A variable must not have an incomplete type or a reference type.
7660 if (RequireCompleteType(ELoc, QType,
7661 diag::err_omp_linear_incomplete_type)) {
7662 continue;
7663 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007664 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7665 !QType->isReferenceType()) {
7666 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7667 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7668 continue;
7669 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007670 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007671
7672 // A list item must not be const-qualified.
7673 if (QType.isConstant(Context)) {
7674 Diag(ELoc, diag::err_omp_const_variable)
7675 << getOpenMPClauseName(OMPC_linear);
7676 bool IsDecl =
7677 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7678 Diag(VD->getLocation(),
7679 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7680 << VD;
7681 continue;
7682 }
7683
7684 // A list item must be of integral or pointer type.
7685 QType = QType.getUnqualifiedType().getCanonicalType();
7686 const Type *Ty = QType.getTypePtrOrNull();
7687 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7688 !Ty->isPointerType())) {
7689 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7690 bool IsDecl =
7691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7692 Diag(VD->getLocation(),
7693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7694 << VD;
7695 continue;
7696 }
7697
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007698 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007699 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7700 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007701 auto *PrivateRef = buildDeclRefExpr(
7702 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007703 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007704 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007705 Expr *InitExpr;
7706 if (LinKind == OMPC_LINEAR_uval)
7707 InitExpr = VD->getInit();
7708 else
7709 InitExpr = DE;
7710 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007711 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007712 auto InitRef = buildDeclRefExpr(
7713 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007714 DSAStack->addDSA(VD, DE, OMPC_linear);
7715 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007716 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007717 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007718 }
7719
7720 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007721 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007722
7723 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007724 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007725 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7726 !Step->isInstantiationDependent() &&
7727 !Step->containsUnexpandedParameterPack()) {
7728 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007729 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007730 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007731 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007732 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007733
Alexander Musman3276a272015-03-21 10:12:56 +00007734 // Build var to save the step value.
7735 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007736 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007737 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007738 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007739 ExprResult CalcStep =
7740 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007741 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007742
Alexander Musman8dba6642014-04-22 13:09:42 +00007743 // Warn about zero linear step (it would be probably better specified as
7744 // making corresponding variables 'const').
7745 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007746 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7747 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007748 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7749 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007750 if (!IsConstant && CalcStep.isUsable()) {
7751 // Calculate the step beforehand instead of doing this on each iteration.
7752 // (This is not used if the number of iterations may be kfold-ed).
7753 CalcStepExpr = CalcStep.get();
7754 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007755 }
7756
Alexey Bataev182227b2015-08-20 10:54:39 +00007757 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7758 ColonLoc, EndLoc, Vars, Privates, Inits,
7759 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007760}
7761
7762static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7763 Expr *NumIterations, Sema &SemaRef,
7764 Scope *S) {
7765 // Walk the vars and build update/final expressions for the CodeGen.
7766 SmallVector<Expr *, 8> Updates;
7767 SmallVector<Expr *, 8> Finals;
7768 Expr *Step = Clause.getStep();
7769 Expr *CalcStep = Clause.getCalcStep();
7770 // OpenMP [2.14.3.7, linear clause]
7771 // If linear-step is not specified it is assumed to be 1.
7772 if (Step == nullptr)
7773 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7774 else if (CalcStep)
7775 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7776 bool HasErrors = false;
7777 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007778 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007779 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007780 for (auto &RefExpr : Clause.varlists()) {
7781 Expr *InitExpr = *CurInit;
7782
7783 // Build privatized reference to the current linear var.
7784 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007785 Expr *CapturedRef;
7786 if (LinKind == OMPC_LINEAR_uval)
7787 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7788 else
7789 CapturedRef =
7790 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7791 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7792 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007793
7794 // Build update: Var = InitExpr + IV * Step
7795 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007796 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007797 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007798 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7799 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007800
7801 // Build final: Var = InitExpr + NumIterations * Step
7802 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007803 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007804 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007805 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7806 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007807 if (!Update.isUsable() || !Final.isUsable()) {
7808 Updates.push_back(nullptr);
7809 Finals.push_back(nullptr);
7810 HasErrors = true;
7811 } else {
7812 Updates.push_back(Update.get());
7813 Finals.push_back(Final.get());
7814 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007815 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007816 }
7817 Clause.setUpdates(Updates);
7818 Clause.setFinals(Finals);
7819 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007820}
7821
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007822OMPClause *Sema::ActOnOpenMPAlignedClause(
7823 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7824 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7825
7826 SmallVector<Expr *, 8> Vars;
7827 for (auto &RefExpr : VarList) {
7828 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7829 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7830 // It will be analyzed later.
7831 Vars.push_back(RefExpr);
7832 continue;
7833 }
7834
7835 SourceLocation ELoc = RefExpr->getExprLoc();
7836 // OpenMP [2.1, C/C++]
7837 // A list item is a variable name.
7838 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7839 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7840 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7841 continue;
7842 }
7843
7844 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7845
7846 // OpenMP [2.8.1, simd construct, Restrictions]
7847 // The type of list items appearing in the aligned clause must be
7848 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007849 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007850 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007851 const Type *Ty = QType.getTypePtrOrNull();
7852 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7853 !Ty->isPointerType())) {
7854 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7855 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7856 bool IsDecl =
7857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7858 Diag(VD->getLocation(),
7859 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7860 << VD;
7861 continue;
7862 }
7863
7864 // OpenMP [2.8.1, simd construct, Restrictions]
7865 // A list-item cannot appear in more than one aligned clause.
7866 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7867 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7868 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7869 << getOpenMPClauseName(OMPC_aligned);
7870 continue;
7871 }
7872
7873 Vars.push_back(DE);
7874 }
7875
7876 // OpenMP [2.8.1, simd construct, Description]
7877 // The parameter of the aligned clause, alignment, must be a constant
7878 // positive integer expression.
7879 // If no optional parameter is specified, implementation-defined default
7880 // alignments for SIMD instructions on the target platforms are assumed.
7881 if (Alignment != nullptr) {
7882 ExprResult AlignResult =
7883 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7884 if (AlignResult.isInvalid())
7885 return nullptr;
7886 Alignment = AlignResult.get();
7887 }
7888 if (Vars.empty())
7889 return nullptr;
7890
7891 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7892 EndLoc, Vars, Alignment);
7893}
7894
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007895OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7896 SourceLocation StartLoc,
7897 SourceLocation LParenLoc,
7898 SourceLocation EndLoc) {
7899 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007900 SmallVector<Expr *, 8> SrcExprs;
7901 SmallVector<Expr *, 8> DstExprs;
7902 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007903 for (auto &RefExpr : VarList) {
7904 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7905 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007906 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007907 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007908 SrcExprs.push_back(nullptr);
7909 DstExprs.push_back(nullptr);
7910 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007911 continue;
7912 }
7913
Alexey Bataeved09d242014-05-28 05:53:51 +00007914 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007915 // OpenMP [2.1, C/C++]
7916 // A list item is a variable name.
7917 // OpenMP [2.14.4.1, Restrictions, p.1]
7918 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007919 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007920 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007921 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007922 continue;
7923 }
7924
7925 Decl *D = DE->getDecl();
7926 VarDecl *VD = cast<VarDecl>(D);
7927
7928 QualType Type = VD->getType();
7929 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7930 // It will be analyzed later.
7931 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007932 SrcExprs.push_back(nullptr);
7933 DstExprs.push_back(nullptr);
7934 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007935 continue;
7936 }
7937
7938 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7939 // A list item that appears in a copyin clause must be threadprivate.
7940 if (!DSAStack->isThreadPrivate(VD)) {
7941 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007942 << getOpenMPClauseName(OMPC_copyin)
7943 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007944 continue;
7945 }
7946
7947 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7948 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007949 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007950 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007951 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007952 auto *SrcVD =
7953 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7954 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007955 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007956 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7957 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007958 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7959 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007960 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007961 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007962 // For arrays generate assignment operation for single element and replace
7963 // it by the original array element in CodeGen.
7964 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7965 PseudoDstExpr, PseudoSrcExpr);
7966 if (AssignmentOp.isInvalid())
7967 continue;
7968 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7969 /*DiscardedValue=*/true);
7970 if (AssignmentOp.isInvalid())
7971 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007972
7973 DSAStack->addDSA(VD, DE, OMPC_copyin);
7974 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007975 SrcExprs.push_back(PseudoSrcExpr);
7976 DstExprs.push_back(PseudoDstExpr);
7977 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007978 }
7979
Alexey Bataeved09d242014-05-28 05:53:51 +00007980 if (Vars.empty())
7981 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007982
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007983 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7984 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007985}
7986
Alexey Bataevbae9a792014-06-27 10:37:06 +00007987OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7988 SourceLocation StartLoc,
7989 SourceLocation LParenLoc,
7990 SourceLocation EndLoc) {
7991 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007992 SmallVector<Expr *, 8> SrcExprs;
7993 SmallVector<Expr *, 8> DstExprs;
7994 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007995 for (auto &RefExpr : VarList) {
7996 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7997 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7998 // It will be analyzed later.
7999 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008000 SrcExprs.push_back(nullptr);
8001 DstExprs.push_back(nullptr);
8002 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008003 continue;
8004 }
8005
8006 SourceLocation ELoc = RefExpr->getExprLoc();
8007 // OpenMP [2.1, C/C++]
8008 // A list item is a variable name.
8009 // OpenMP [2.14.4.1, Restrictions, p.1]
8010 // A list item that appears in a copyin clause must be threadprivate.
8011 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8012 if (!DE || !isa<VarDecl>(DE->getDecl())) {
8013 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
8014 continue;
8015 }
8016
8017 Decl *D = DE->getDecl();
8018 VarDecl *VD = cast<VarDecl>(D);
8019
8020 QualType Type = VD->getType();
8021 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8022 // It will be analyzed later.
8023 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008024 SrcExprs.push_back(nullptr);
8025 DstExprs.push_back(nullptr);
8026 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008027 continue;
8028 }
8029
8030 // OpenMP [2.14.4.2, Restrictions, p.2]
8031 // A list item that appears in a copyprivate clause may not appear in a
8032 // private or firstprivate clause on the single construct.
8033 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008034 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008035 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8036 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008037 Diag(ELoc, diag::err_omp_wrong_dsa)
8038 << getOpenMPClauseName(DVar.CKind)
8039 << getOpenMPClauseName(OMPC_copyprivate);
8040 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8041 continue;
8042 }
8043
8044 // OpenMP [2.11.4.2, Restrictions, p.1]
8045 // All list items that appear in a copyprivate clause must be either
8046 // threadprivate or private in the enclosing context.
8047 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008048 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008049 if (DVar.CKind == OMPC_shared) {
8050 Diag(ELoc, diag::err_omp_required_access)
8051 << getOpenMPClauseName(OMPC_copyprivate)
8052 << "threadprivate or private in the enclosing context";
8053 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8054 continue;
8055 }
8056 }
8057 }
8058
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008059 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008060 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008061 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008062 << getOpenMPClauseName(OMPC_copyprivate) << Type
8063 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008064 bool IsDecl =
8065 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8066 Diag(VD->getLocation(),
8067 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8068 << VD;
8069 continue;
8070 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008071
Alexey Bataevbae9a792014-06-27 10:37:06 +00008072 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8073 // A variable of class type (or array thereof) that appears in a
8074 // copyin clause requires an accessible, unambiguous copy assignment
8075 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008076 Type = Context.getBaseElementType(Type.getNonReferenceType())
8077 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008078 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008079 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8080 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008081 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008082 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008083 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008084 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8085 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008086 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008087 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008088 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8089 PseudoDstExpr, PseudoSrcExpr);
8090 if (AssignmentOp.isInvalid())
8091 continue;
8092 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8093 /*DiscardedValue=*/true);
8094 if (AssignmentOp.isInvalid())
8095 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008096
8097 // No need to mark vars as copyprivate, they are already threadprivate or
8098 // implicitly private.
8099 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008100 SrcExprs.push_back(PseudoSrcExpr);
8101 DstExprs.push_back(PseudoDstExpr);
8102 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008103 }
8104
8105 if (Vars.empty())
8106 return nullptr;
8107
Alexey Bataeva63048e2015-03-23 06:18:07 +00008108 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8109 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008110}
8111
Alexey Bataev6125da92014-07-21 11:26:11 +00008112OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8113 SourceLocation StartLoc,
8114 SourceLocation LParenLoc,
8115 SourceLocation EndLoc) {
8116 if (VarList.empty())
8117 return nullptr;
8118
8119 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8120}
Alexey Bataevdea47612014-07-23 07:46:59 +00008121
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008122OMPClause *
8123Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8124 SourceLocation DepLoc, SourceLocation ColonLoc,
8125 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8126 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008127 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008128 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008129 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008130 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008131 return nullptr;
8132 }
8133 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008134 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8135 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008136 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008137 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008138 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8139 /*Last=*/OMPC_DEPEND_unknown, Except)
8140 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008141 return nullptr;
8142 }
8143 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008144 llvm::APSInt DepCounter(/*BitWidth=*/32);
8145 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8146 if (DepKind == OMPC_DEPEND_sink) {
8147 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8148 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8149 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008150 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008151 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008152 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8153 DSAStack->getParentOrderedRegionParam()) {
8154 for (auto &RefExpr : VarList) {
8155 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8156 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8157 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8158 // It will be analyzed later.
8159 Vars.push_back(RefExpr);
8160 continue;
8161 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008162
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008163 SourceLocation ELoc = RefExpr->getExprLoc();
8164 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8165 if (DepKind == OMPC_DEPEND_sink) {
8166 if (DepCounter >= TotalDepCount) {
8167 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8168 continue;
8169 }
8170 ++DepCounter;
8171 // OpenMP [2.13.9, Summary]
8172 // depend(dependence-type : vec), where dependence-type is:
8173 // 'sink' and where vec is the iteration vector, which has the form:
8174 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8175 // where n is the value specified by the ordered clause in the loop
8176 // directive, xi denotes the loop iteration variable of the i-th nested
8177 // loop associated with the loop directive, and di is a constant
8178 // non-negative integer.
8179 SimpleExpr = SimpleExpr->IgnoreImplicit();
8180 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8181 if (!DE) {
8182 OverloadedOperatorKind OOK = OO_None;
8183 SourceLocation OOLoc;
8184 Expr *LHS, *RHS;
8185 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8186 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8187 OOLoc = BO->getOperatorLoc();
8188 LHS = BO->getLHS()->IgnoreParenImpCasts();
8189 RHS = BO->getRHS()->IgnoreParenImpCasts();
8190 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8191 OOK = OCE->getOperator();
8192 OOLoc = OCE->getOperatorLoc();
8193 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8194 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8195 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8196 OOK = MCE->getMethodDecl()
8197 ->getNameInfo()
8198 .getName()
8199 .getCXXOverloadedOperator();
8200 OOLoc = MCE->getCallee()->getExprLoc();
8201 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8202 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8203 } else {
8204 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8205 continue;
8206 }
8207 DE = dyn_cast<DeclRefExpr>(LHS);
8208 if (!DE) {
8209 Diag(LHS->getExprLoc(),
8210 diag::err_omp_depend_sink_expected_loop_iteration)
8211 << DSAStack->getParentLoopControlVariable(
8212 DepCounter.getZExtValue());
8213 continue;
8214 }
8215 if (OOK != OO_Plus && OOK != OO_Minus) {
8216 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8217 continue;
8218 }
8219 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8220 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8221 if (Res.isInvalid())
8222 continue;
8223 }
8224 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8225 if (!CurContext->isDependentContext() &&
8226 DSAStack->getParentOrderedRegionParam() &&
8227 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8228 Diag(DE->getExprLoc(),
8229 diag::err_omp_depend_sink_expected_loop_iteration)
8230 << DSAStack->getParentLoopControlVariable(
8231 DepCounter.getZExtValue());
8232 continue;
8233 }
8234 } else {
8235 // OpenMP [2.11.1.1, Restrictions, p.3]
8236 // A variable that is part of another variable (such as a field of a
8237 // structure) but is not an array element or an array section cannot
8238 // appear in a depend clause.
8239 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8240 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8241 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8242 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8243 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8244 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8245 !ASE->getBase()->getType()->isArrayType())) {
8246 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8247 << RefExpr->getSourceRange();
8248 continue;
8249 }
8250 }
8251
8252 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8253 }
8254
8255 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8256 TotalDepCount > VarList.size() &&
8257 DSAStack->getParentOrderedRegionParam()) {
8258 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8259 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8260 }
8261 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8262 Vars.empty())
8263 return nullptr;
8264 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008265
8266 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8267 DepLoc, ColonLoc, Vars);
8268}
Michael Wonge710d542015-08-07 16:16:36 +00008269
8270OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8271 SourceLocation LParenLoc,
8272 SourceLocation EndLoc) {
8273 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008274
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008275 // OpenMP [2.9.1, Restrictions]
8276 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008277 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8278 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008279 return nullptr;
8280
Michael Wonge710d542015-08-07 16:16:36 +00008281 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8282}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008283
8284static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8285 DSAStackTy *Stack, CXXRecordDecl *RD) {
8286 if (!RD || RD->isInvalidDecl())
8287 return true;
8288
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008289 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8290 if (auto *CTD = CTSD->getSpecializedTemplate())
8291 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008292 auto QTy = SemaRef.Context.getRecordType(RD);
8293 if (RD->isDynamicClass()) {
8294 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8295 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8296 return false;
8297 }
8298 auto *DC = RD;
8299 bool IsCorrect = true;
8300 for (auto *I : DC->decls()) {
8301 if (I) {
8302 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8303 if (MD->isStatic()) {
8304 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8305 SemaRef.Diag(MD->getLocation(),
8306 diag::note_omp_static_member_in_target);
8307 IsCorrect = false;
8308 }
8309 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8310 if (VD->isStaticDataMember()) {
8311 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8312 SemaRef.Diag(VD->getLocation(),
8313 diag::note_omp_static_member_in_target);
8314 IsCorrect = false;
8315 }
8316 }
8317 }
8318 }
8319
8320 for (auto &I : RD->bases()) {
8321 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8322 I.getType()->getAsCXXRecordDecl()))
8323 IsCorrect = false;
8324 }
8325 return IsCorrect;
8326}
8327
8328static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8329 DSAStackTy *Stack, QualType QTy) {
8330 NamedDecl *ND;
8331 if (QTy->isIncompleteType(&ND)) {
8332 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8333 return false;
8334 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8335 if (!RD->isInvalidDecl() &&
8336 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8337 return false;
8338 }
8339 return true;
8340}
8341
8342OMPClause *Sema::ActOnOpenMPMapClause(
8343 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8344 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8345 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8346 SmallVector<Expr *, 4> Vars;
8347
8348 for (auto &RE : VarList) {
8349 assert(RE && "Null expr in omp map");
8350 if (isa<DependentScopeDeclRefExpr>(RE)) {
8351 // It will be analyzed later.
8352 Vars.push_back(RE);
8353 continue;
8354 }
8355 SourceLocation ELoc = RE->getExprLoc();
8356
8357 // OpenMP [2.14.5, Restrictions]
8358 // A variable that is part of another variable (such as field of a
8359 // structure) but is not an array element or an array section cannot appear
8360 // in a map clause.
8361 auto *VE = RE->IgnoreParenLValueCasts();
8362
8363 if (VE->isValueDependent() || VE->isTypeDependent() ||
8364 VE->isInstantiationDependent() ||
8365 VE->containsUnexpandedParameterPack()) {
8366 // It will be analyzed later.
8367 Vars.push_back(RE);
8368 continue;
8369 }
8370
8371 auto *SimpleExpr = RE->IgnoreParenCasts();
8372 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8373 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8374 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8375
8376 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8377 (!OASE && !ASE && !DE) ||
8378 (DE && !isa<VarDecl>(DE->getDecl())) ||
8379 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8380 !ASE->getBase()->getType()->isArrayType())) {
8381 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8382 << RE->getSourceRange();
8383 continue;
8384 }
8385
8386 Decl *D = nullptr;
8387 if (DE) {
8388 D = DE->getDecl();
8389 } else if (ASE) {
8390 auto *B = ASE->getBase()->IgnoreParenCasts();
8391 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8392 } else if (OASE) {
8393 auto *B = OASE->getBase();
8394 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8395 }
8396 assert(D && "Null decl on map clause.");
8397 auto *VD = cast<VarDecl>(D);
8398
8399 // OpenMP [2.14.5, Restrictions, p.8]
8400 // threadprivate variables cannot appear in a map clause.
8401 if (DSAStack->isThreadPrivate(VD)) {
8402 auto DVar = DSAStack->getTopDSA(VD, false);
8403 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8404 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8405 continue;
8406 }
8407
8408 // OpenMP [2.14.5, Restrictions, p.2]
8409 // At most one list item can be an array item derived from a given variable
8410 // in map clauses of the same construct.
8411 // OpenMP [2.14.5, Restrictions, p.3]
8412 // List items of map clauses in the same construct must not share original
8413 // storage.
8414 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8415 // A variable for which the type is pointer, reference to array, or
8416 // reference to pointer and an array section derived from that variable
8417 // must not appear as list items of map clauses of the same construct.
8418 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8419 if (MI.RefExpr) {
8420 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8421 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8422 << MI.RefExpr->getSourceRange();
8423 continue;
8424 }
8425
8426 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8427 // A variable for which the type is pointer, reference to array, or
8428 // reference to pointer must not appear as a list item if the enclosing
8429 // device data environment already contains an array section derived from
8430 // that variable.
8431 // An array section derived from a variable for which the type is pointer,
8432 // reference to array, or reference to pointer must not appear as a list
8433 // item if the enclosing device data environment already contains that
8434 // variable.
8435 QualType Type = VD->getType();
8436 MI = DSAStack->getMapInfoForVar(VD);
8437 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8438 isa<DeclRefExpr>(VE)) &&
8439 (Type->isPointerType() || Type->isReferenceType())) {
8440 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8441 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8442 << MI.RefExpr->getSourceRange();
8443 continue;
8444 }
8445
8446 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8447 // A list item must have a mappable type.
8448 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8449 DSAStack, Type))
8450 continue;
8451
8452 Vars.push_back(RE);
8453 MI.RefExpr = RE;
8454 DSAStack->addMapInfoForVar(VD, MI);
8455 }
8456 if (Vars.empty())
8457 return nullptr;
8458
8459 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8460 MapTypeModifier, MapType, MapLoc);
8461}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008462
8463OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8464 SourceLocation StartLoc,
8465 SourceLocation LParenLoc,
8466 SourceLocation EndLoc) {
8467 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008468
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008469 // OpenMP [teams Constrcut, Restrictions]
8470 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008471 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8472 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008473 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008474
8475 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8476}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008477
8478OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8479 SourceLocation StartLoc,
8480 SourceLocation LParenLoc,
8481 SourceLocation EndLoc) {
8482 Expr *ValExpr = ThreadLimit;
8483
8484 // OpenMP [teams Constrcut, Restrictions]
8485 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008486 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8487 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008488 return nullptr;
8489
8490 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8491 EndLoc);
8492}
Alexey Bataeva0569352015-12-01 10:17:31 +00008493
8494OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8495 SourceLocation StartLoc,
8496 SourceLocation LParenLoc,
8497 SourceLocation EndLoc) {
8498 Expr *ValExpr = Priority;
8499
8500 // OpenMP [2.9.1, task Constrcut]
8501 // The priority-value is a non-negative numerical scalar expression.
8502 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8503 /*StrictlyPositive=*/false))
8504 return nullptr;
8505
8506 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8507}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008508
8509OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8510 SourceLocation StartLoc,
8511 SourceLocation LParenLoc,
8512 SourceLocation EndLoc) {
8513 Expr *ValExpr = Grainsize;
8514
8515 // OpenMP [2.9.2, taskloop Constrcut]
8516 // The parameter of the grainsize clause must be a positive integer
8517 // expression.
8518 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8519 /*StrictlyPositive=*/true))
8520 return nullptr;
8521
8522 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8523}
Alexey Bataev382967a2015-12-08 12:06:20 +00008524
8525OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8526 SourceLocation StartLoc,
8527 SourceLocation LParenLoc,
8528 SourceLocation EndLoc) {
8529 Expr *ValExpr = NumTasks;
8530
8531 // OpenMP [2.9.2, taskloop Constrcut]
8532 // The parameter of the num_tasks clause must be a positive integer
8533 // expression.
8534 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8535 /*StrictlyPositive=*/true))
8536 return nullptr;
8537
8538 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8539}
8540
Alexey Bataev28c75412015-12-15 08:19:24 +00008541OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8542 SourceLocation LParenLoc,
8543 SourceLocation EndLoc) {
8544 // OpenMP [2.13.2, critical construct, Description]
8545 // ... where hint-expression is an integer constant expression that evaluates
8546 // to a valid lock hint.
8547 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8548 if (HintExpr.isInvalid())
8549 return nullptr;
8550 return new (Context)
8551 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8552}
8553
Carlo Bertollib4adf552016-01-15 18:50:31 +00008554OMPClause *Sema::ActOnOpenMPDistScheduleClause(
8555 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8556 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
8557 SourceLocation EndLoc) {
8558 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
8559 std::string Values;
8560 Values += "'";
8561 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
8562 Values += "'";
8563 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8564 << Values << getOpenMPClauseName(OMPC_dist_schedule);
8565 return nullptr;
8566 }
8567 Expr *ValExpr = ChunkSize;
8568 Expr *HelperValExpr = nullptr;
8569 if (ChunkSize) {
8570 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8571 !ChunkSize->isInstantiationDependent() &&
8572 !ChunkSize->containsUnexpandedParameterPack()) {
8573 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8574 ExprResult Val =
8575 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8576 if (Val.isInvalid())
8577 return nullptr;
8578
8579 ValExpr = Val.get();
8580
8581 // OpenMP [2.7.1, Restrictions]
8582 // chunk_size must be a loop invariant integer expression with a positive
8583 // value.
8584 llvm::APSInt Result;
8585 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8586 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8587 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8588 << "dist_schedule" << ChunkSize->getSourceRange();
8589 return nullptr;
8590 }
8591 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
8592 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
8593 ChunkSize->getType(), ".chunk.");
8594 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
8595 ChunkSize->getExprLoc(),
8596 /*RefersToCapture=*/true);
8597 HelperValExpr = ImpVarRef;
8598 }
8599 }
8600 }
8601
8602 return new (Context)
8603 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
8604 Kind, ValExpr, HelperValExpr);
8605}