blob: c42718d576bc831b1de592e1b3b46c4eeb327241 [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 }
1144 return DeclGroupPtrTy();
1145}
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 Bataev040d5402015-05-12 08:35:28 +00001625 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001626 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001627 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001628 Clause->getClauseKind() == OMPC_copyprivate ||
1629 (getLangOpts().OpenMPUseTLS &&
1630 getASTContext().getTargetInfo().isTLSSupported() &&
1631 Clause->getClauseKind() == OMPC_copyin)) {
1632 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001633 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001634 for (auto *VarRef : Clause->children()) {
1635 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001636 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001637 }
1638 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001639 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001640 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1641 Clause->getClauseKind() == OMPC_schedule) {
1642 // Mark all variables in private list clauses as used in inner region.
1643 // Required for proper codegen of combined directives.
1644 // TODO: add processing for other clauses.
1645 if (auto *E = cast_or_null<Expr>(
1646 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1647 MarkDeclarationsReferencedInExpr(E);
1648 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001649 }
1650 }
1651 return ActOnCapturedRegionEnd(S.get());
1652}
1653
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001654static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1655 OpenMPDirectiveKind CurrentRegion,
1656 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001657 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001658 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001659 // Allowed nesting of constructs
1660 // +------------------+-----------------+------------------------------------+
1661 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1662 // +------------------+-----------------+------------------------------------+
1663 // | parallel | parallel | * |
1664 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001665 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001666 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001667 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001668 // | parallel | simd | * |
1669 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001670 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001671 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001672 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001673 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001674 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001675 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001676 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001677 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001678 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001679 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001680 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001681 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001682 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001683 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001684 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001685 // | parallel | cancellation | |
1686 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001687 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001688 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001689 // | parallel | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001690 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001691 // +------------------+-----------------+------------------------------------+
1692 // | for | parallel | * |
1693 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001694 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001695 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001696 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001697 // | for | simd | * |
1698 // | for | sections | + |
1699 // | for | section | + |
1700 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001701 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001702 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001703 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001704 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001705 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001706 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001707 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001708 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001709 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001710 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001711 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001712 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001713 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001714 // | for | cancellation | |
1715 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001716 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001717 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001718 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001719 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001720 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001721 // | master | parallel | * |
1722 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001723 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001724 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001725 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001726 // | master | simd | * |
1727 // | master | sections | + |
1728 // | master | section | + |
1729 // | master | single | + |
1730 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001731 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001732 // | master |parallel sections| * |
1733 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001734 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001735 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001736 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001737 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001738 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001739 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001740 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001741 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001742 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001743 // | master | cancellation | |
1744 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001745 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001746 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001747 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001748 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001749 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001750 // | critical | parallel | * |
1751 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001752 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001753 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001754 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001755 // | critical | simd | * |
1756 // | critical | sections | + |
1757 // | critical | section | + |
1758 // | critical | single | + |
1759 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001760 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001761 // | critical |parallel sections| * |
1762 // | critical | task | * |
1763 // | critical | taskyield | * |
1764 // | critical | barrier | + |
1765 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001766 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001767 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001768 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001769 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001770 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001771 // | critical | cancellation | |
1772 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001773 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001774 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001775 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001776 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001777 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001778 // | simd | parallel | |
1779 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001780 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001781 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001782 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001783 // | simd | simd | |
1784 // | simd | sections | |
1785 // | simd | section | |
1786 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001787 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001788 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001789 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001790 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001791 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001792 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001793 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001794 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001795 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001796 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001797 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001798 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001799 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001800 // | simd | cancellation | |
1801 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001802 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001803 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001804 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001805 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001806 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001807 // | for simd | parallel | |
1808 // | for simd | for | |
1809 // | for simd | for simd | |
1810 // | for simd | master | |
1811 // | for simd | critical | |
1812 // | for simd | simd | |
1813 // | for simd | sections | |
1814 // | for simd | section | |
1815 // | for simd | single | |
1816 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001817 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001818 // | for simd |parallel sections| |
1819 // | for simd | task | |
1820 // | for simd | taskyield | |
1821 // | for simd | barrier | |
1822 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001823 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001824 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001825 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001826 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001827 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001828 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001829 // | for simd | cancellation | |
1830 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001831 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001832 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001833 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001834 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001835 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001836 // | parallel for simd| parallel | |
1837 // | parallel for simd| for | |
1838 // | parallel for simd| for simd | |
1839 // | parallel for simd| master | |
1840 // | parallel for simd| critical | |
1841 // | parallel for simd| simd | |
1842 // | parallel for simd| sections | |
1843 // | parallel for simd| section | |
1844 // | parallel for simd| single | |
1845 // | parallel for simd| parallel for | |
1846 // | parallel for simd|parallel for simd| |
1847 // | parallel for simd|parallel sections| |
1848 // | parallel for simd| task | |
1849 // | parallel for simd| taskyield | |
1850 // | parallel for simd| barrier | |
1851 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001852 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001853 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001854 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001855 // | parallel for simd| atomic | |
1856 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001857 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001858 // | parallel for simd| cancellation | |
1859 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001860 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001861 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001862 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001863 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001864 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001865 // | sections | parallel | * |
1866 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001867 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001868 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001869 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001870 // | sections | simd | * |
1871 // | sections | sections | + |
1872 // | sections | section | * |
1873 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001874 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001875 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001876 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001877 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001878 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001879 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001880 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001881 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001882 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001883 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001884 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001885 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001886 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001887 // | sections | cancellation | |
1888 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001889 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001890 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001891 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001892 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001893 // +------------------+-----------------+------------------------------------+
1894 // | section | parallel | * |
1895 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001896 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001897 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001898 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001899 // | section | simd | * |
1900 // | section | sections | + |
1901 // | section | section | + |
1902 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001903 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001904 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001905 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001906 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001907 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001908 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001909 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001910 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001911 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001912 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001913 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001914 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001915 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001916 // | section | cancellation | |
1917 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001918 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001919 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001920 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001921 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001922 // +------------------+-----------------+------------------------------------+
1923 // | single | parallel | * |
1924 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001925 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001926 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001927 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001928 // | single | simd | * |
1929 // | single | sections | + |
1930 // | single | section | + |
1931 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001932 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001933 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001935 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001936 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001937 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001938 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001939 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001940 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001941 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001942 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001943 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001944 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001945 // | single | cancellation | |
1946 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001947 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001948 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001949 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001950 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001951 // +------------------+-----------------+------------------------------------+
1952 // | parallel for | parallel | * |
1953 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001954 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001955 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001956 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001957 // | parallel for | simd | * |
1958 // | parallel for | sections | + |
1959 // | parallel for | section | + |
1960 // | parallel for | single | + |
1961 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001962 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001963 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001964 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001965 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001966 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001967 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001968 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001969 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001970 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001971 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001972 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001973 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001974 // | parallel for | cancellation | |
1975 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001976 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001977 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001978 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001979 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001980 // +------------------+-----------------+------------------------------------+
1981 // | parallel sections| parallel | * |
1982 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001983 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001984 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001985 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001986 // | parallel sections| simd | * |
1987 // | parallel sections| sections | + |
1988 // | parallel sections| section | * |
1989 // | parallel sections| single | + |
1990 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001991 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001992 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001993 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001994 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001995 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001996 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001997 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001998 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001999 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002000 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002001 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002002 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002003 // | parallel sections| cancellation | |
2004 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002005 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002006 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002007 // | parallel sections| taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002008 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002009 // +------------------+-----------------+------------------------------------+
2010 // | task | parallel | * |
2011 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002012 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002014 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002015 // | task | simd | * |
2016 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002017 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002018 // | task | single | + |
2019 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002020 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002021 // | task |parallel sections| * |
2022 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002023 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002024 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002025 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002026 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002027 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002028 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002029 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002030 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002031 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002032 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002033 // | | point | ! |
2034 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002035 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002036 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002037 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002038 // +------------------+-----------------+------------------------------------+
2039 // | ordered | parallel | * |
2040 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002041 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002042 // | ordered | master | * |
2043 // | ordered | critical | * |
2044 // | ordered | simd | * |
2045 // | ordered | sections | + |
2046 // | ordered | section | + |
2047 // | ordered | single | + |
2048 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002049 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002050 // | ordered |parallel sections| * |
2051 // | ordered | task | * |
2052 // | ordered | taskyield | * |
2053 // | ordered | barrier | + |
2054 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002055 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002056 // | ordered | flush | * |
2057 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002058 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002059 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002060 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002061 // | ordered | cancellation | |
2062 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002063 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002064 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002065 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002066 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002067 // +------------------+-----------------+------------------------------------+
2068 // | atomic | parallel | |
2069 // | atomic | for | |
2070 // | atomic | for simd | |
2071 // | atomic | master | |
2072 // | atomic | critical | |
2073 // | atomic | simd | |
2074 // | atomic | sections | |
2075 // | atomic | section | |
2076 // | atomic | single | |
2077 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002078 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002079 // | atomic |parallel sections| |
2080 // | atomic | task | |
2081 // | atomic | taskyield | |
2082 // | atomic | barrier | |
2083 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002084 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002085 // | atomic | flush | |
2086 // | atomic | ordered | |
2087 // | atomic | atomic | |
2088 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002089 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002090 // | atomic | cancellation | |
2091 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002092 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002093 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002094 // | atomic | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002095 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002096 // +------------------+-----------------+------------------------------------+
2097 // | target | parallel | * |
2098 // | target | for | * |
2099 // | target | for simd | * |
2100 // | target | master | * |
2101 // | target | critical | * |
2102 // | target | simd | * |
2103 // | target | sections | * |
2104 // | target | section | * |
2105 // | target | single | * |
2106 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002107 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002108 // | target |parallel sections| * |
2109 // | target | task | * |
2110 // | target | taskyield | * |
2111 // | target | barrier | * |
2112 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002113 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002114 // | target | flush | * |
2115 // | target | ordered | * |
2116 // | target | atomic | * |
2117 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002118 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002119 // | target | cancellation | |
2120 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002121 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002122 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002123 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002124 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002125 // +------------------+-----------------+------------------------------------+
2126 // | teams | parallel | * |
2127 // | teams | for | + |
2128 // | teams | for simd | + |
2129 // | teams | master | + |
2130 // | teams | critical | + |
2131 // | teams | simd | + |
2132 // | teams | sections | + |
2133 // | teams | section | + |
2134 // | teams | single | + |
2135 // | teams | parallel for | * |
2136 // | teams |parallel for simd| * |
2137 // | teams |parallel sections| * |
2138 // | teams | task | + |
2139 // | teams | taskyield | + |
2140 // | teams | barrier | + |
2141 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002142 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002143 // | teams | flush | + |
2144 // | teams | ordered | + |
2145 // | teams | atomic | + |
2146 // | teams | target | + |
2147 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002148 // | teams | cancellation | |
2149 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002150 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002151 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002152 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002153 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002154 // +------------------+-----------------+------------------------------------+
2155 // | taskloop | parallel | * |
2156 // | taskloop | for | + |
2157 // | taskloop | for simd | + |
2158 // | taskloop | master | + |
2159 // | taskloop | critical | * |
2160 // | taskloop | simd | * |
2161 // | taskloop | sections | + |
2162 // | taskloop | section | + |
2163 // | taskloop | single | + |
2164 // | taskloop | parallel for | * |
2165 // | taskloop |parallel for simd| * |
2166 // | taskloop |parallel sections| * |
2167 // | taskloop | task | * |
2168 // | taskloop | taskyield | * |
2169 // | taskloop | barrier | + |
2170 // | taskloop | taskwait | * |
2171 // | taskloop | taskgroup | * |
2172 // | taskloop | flush | * |
2173 // | taskloop | ordered | + |
2174 // | taskloop | atomic | * |
2175 // | taskloop | target | * |
2176 // | taskloop | teams | + |
2177 // | taskloop | cancellation | |
2178 // | | point | |
2179 // | taskloop | cancel | |
2180 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002181 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002182 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002183 // | taskloop simd | parallel | |
2184 // | taskloop simd | for | |
2185 // | taskloop simd | for simd | |
2186 // | taskloop simd | master | |
2187 // | taskloop simd | critical | |
2188 // | taskloop simd | simd | |
2189 // | taskloop simd | sections | |
2190 // | taskloop simd | section | |
2191 // | taskloop simd | single | |
2192 // | taskloop simd | parallel for | |
2193 // | taskloop simd |parallel for simd| |
2194 // | taskloop simd |parallel sections| |
2195 // | taskloop simd | task | |
2196 // | taskloop simd | taskyield | |
2197 // | taskloop simd | barrier | |
2198 // | taskloop simd | taskwait | |
2199 // | taskloop simd | taskgroup | |
2200 // | taskloop simd | flush | |
2201 // | taskloop simd | ordered | + (with simd clause) |
2202 // | taskloop simd | atomic | |
2203 // | taskloop simd | target | |
2204 // | taskloop simd | teams | |
2205 // | taskloop simd | cancellation | |
2206 // | | point | |
2207 // | taskloop simd | cancel | |
2208 // | taskloop simd | taskloop | |
2209 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002210 // | taskloop simd | distribute | |
2211 // +------------------+-----------------+------------------------------------+
2212 // | distribute | parallel | * |
2213 // | distribute | for | * |
2214 // | distribute | for simd | * |
2215 // | distribute | master | * |
2216 // | distribute | critical | * |
2217 // | distribute | simd | * |
2218 // | distribute | sections | * |
2219 // | distribute | section | * |
2220 // | distribute | single | * |
2221 // | distribute | parallel for | * |
2222 // | distribute |parallel for simd| * |
2223 // | distribute |parallel sections| * |
2224 // | distribute | task | * |
2225 // | distribute | taskyield | * |
2226 // | distribute | barrier | * |
2227 // | distribute | taskwait | * |
2228 // | distribute | taskgroup | * |
2229 // | distribute | flush | * |
2230 // | distribute | ordered | + |
2231 // | distribute | atomic | * |
2232 // | distribute | target | |
2233 // | distribute | teams | |
2234 // | distribute | cancellation | + |
2235 // | | point | |
2236 // | distribute | cancel | + |
2237 // | distribute | taskloop | * |
2238 // | distribute | taskloop simd | * |
2239 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002240 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002241 if (Stack->getCurScope()) {
2242 auto ParentRegion = Stack->getParentDirective();
2243 bool NestingProhibited = false;
2244 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002245 enum {
2246 NoRecommend,
2247 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002248 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002249 ShouldBeInTargetRegion,
2250 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002251 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002252 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002253 // OpenMP [2.16, Nesting of Regions]
2254 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002255 // OpenMP [2.8.1,simd Construct, Restrictions]
2256 // An ordered construct with the simd clause is the only OpenMP construct
2257 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002258 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2259 return true;
2260 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002261 if (ParentRegion == OMPD_atomic) {
2262 // OpenMP [2.16, Nesting of Regions]
2263 // OpenMP constructs may not be nested inside an atomic region.
2264 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2265 return true;
2266 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002267 if (CurrentRegion == OMPD_section) {
2268 // OpenMP [2.7.2, sections Construct, Restrictions]
2269 // Orphaned section directives are prohibited. That is, the section
2270 // directives must appear within the sections construct and must not be
2271 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002272 if (ParentRegion != OMPD_sections &&
2273 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002274 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2275 << (ParentRegion != OMPD_unknown)
2276 << getOpenMPDirectiveName(ParentRegion);
2277 return true;
2278 }
2279 return false;
2280 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002281 // Allow some constructs to be orphaned (they could be used in functions,
2282 // called from OpenMP regions with the required preconditions).
2283 if (ParentRegion == OMPD_unknown)
2284 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002285 if (CurrentRegion == OMPD_cancellation_point ||
2286 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002287 // OpenMP [2.16, Nesting of Regions]
2288 // A cancellation point construct for which construct-type-clause is
2289 // taskgroup must be nested inside a task construct. A cancellation
2290 // point construct for which construct-type-clause is not taskgroup must
2291 // be closely nested inside an OpenMP construct that matches the type
2292 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002293 // A cancel construct for which construct-type-clause is taskgroup must be
2294 // nested inside a task construct. A cancel construct for which
2295 // construct-type-clause is not taskgroup must be closely nested inside an
2296 // OpenMP construct that matches the type specified in
2297 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002298 NestingProhibited =
2299 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002300 (CancelRegion == OMPD_for &&
2301 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002302 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2303 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002304 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2305 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002306 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002307 // OpenMP [2.16, Nesting of Regions]
2308 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002309 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002310 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002311 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002312 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002313 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2314 // OpenMP [2.16, Nesting of Regions]
2315 // A critical region may not be nested (closely or otherwise) inside a
2316 // critical region with the same name. Note that this restriction is not
2317 // sufficient to prevent deadlock.
2318 SourceLocation PreviousCriticalLoc;
2319 bool DeadLock =
2320 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2321 OpenMPDirectiveKind K,
2322 const DeclarationNameInfo &DNI,
2323 SourceLocation Loc)
2324 ->bool {
2325 if (K == OMPD_critical &&
2326 DNI.getName() == CurrentName.getName()) {
2327 PreviousCriticalLoc = Loc;
2328 return true;
2329 } else
2330 return false;
2331 },
2332 false /* skip top directive */);
2333 if (DeadLock) {
2334 SemaRef.Diag(StartLoc,
2335 diag::err_omp_prohibited_region_critical_same_name)
2336 << CurrentName.getName();
2337 if (PreviousCriticalLoc.isValid())
2338 SemaRef.Diag(PreviousCriticalLoc,
2339 diag::note_omp_previous_critical_region);
2340 return true;
2341 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002342 } else if (CurrentRegion == OMPD_barrier) {
2343 // OpenMP [2.16, Nesting of Regions]
2344 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002345 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002346 NestingProhibited =
2347 isOpenMPWorksharingDirective(ParentRegion) ||
2348 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002349 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002350 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002351 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002352 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002353 // OpenMP [2.16, Nesting of Regions]
2354 // A worksharing region may not be closely nested inside a worksharing,
2355 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002356 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002357 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002358 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002359 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002360 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002361 Recommend = ShouldBeInParallelRegion;
2362 } else if (CurrentRegion == OMPD_ordered) {
2363 // OpenMP [2.16, Nesting of Regions]
2364 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002365 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002366 // An ordered region must be closely nested inside a loop region (or
2367 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002368 // OpenMP [2.8.1,simd Construct, Restrictions]
2369 // An ordered construct with the simd clause is the only OpenMP construct
2370 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002371 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002372 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002373 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002374 !(isOpenMPSimdDirective(ParentRegion) ||
2375 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002376 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002377 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2378 // OpenMP [2.16, Nesting of Regions]
2379 // If specified, a teams construct must be contained within a target
2380 // construct.
2381 NestingProhibited = ParentRegion != OMPD_target;
2382 Recommend = ShouldBeInTargetRegion;
2383 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2384 }
2385 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2386 // OpenMP [2.16, Nesting of Regions]
2387 // distribute, parallel, parallel sections, parallel workshare, and the
2388 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2389 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002390 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2391 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002392 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002393 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002394 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2395 // OpenMP 4.5 [2.17 Nesting of Regions]
2396 // The region associated with the distribute construct must be strictly
2397 // nested inside a teams region
2398 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2399 Recommend = ShouldBeInTeamsRegion;
2400 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002401 if (NestingProhibited) {
2402 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002403 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2404 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002405 return true;
2406 }
2407 }
2408 return false;
2409}
2410
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002411static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2412 ArrayRef<OMPClause *> Clauses,
2413 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2414 bool ErrorFound = false;
2415 unsigned NamedModifiersNumber = 0;
2416 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2417 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002418 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002419 for (const auto *C : Clauses) {
2420 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2421 // At most one if clause without a directive-name-modifier can appear on
2422 // the directive.
2423 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2424 if (FoundNameModifiers[CurNM]) {
2425 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2426 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2427 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2428 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002429 } else if (CurNM != OMPD_unknown) {
2430 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002431 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002432 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002433 FoundNameModifiers[CurNM] = IC;
2434 if (CurNM == OMPD_unknown)
2435 continue;
2436 // Check if the specified name modifier is allowed for the current
2437 // directive.
2438 // At most one if clause with the particular directive-name-modifier can
2439 // appear on the directive.
2440 bool MatchFound = false;
2441 for (auto NM : AllowedNameModifiers) {
2442 if (CurNM == NM) {
2443 MatchFound = true;
2444 break;
2445 }
2446 }
2447 if (!MatchFound) {
2448 S.Diag(IC->getNameModifierLoc(),
2449 diag::err_omp_wrong_if_directive_name_modifier)
2450 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2451 ErrorFound = true;
2452 }
2453 }
2454 }
2455 // If any if clause on the directive includes a directive-name-modifier then
2456 // all if clauses on the directive must include a directive-name-modifier.
2457 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2458 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2459 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2460 diag::err_omp_no_more_if_clause);
2461 } else {
2462 std::string Values;
2463 std::string Sep(", ");
2464 unsigned AllowedCnt = 0;
2465 unsigned TotalAllowedNum =
2466 AllowedNameModifiers.size() - NamedModifiersNumber;
2467 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2468 ++Cnt) {
2469 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2470 if (!FoundNameModifiers[NM]) {
2471 Values += "'";
2472 Values += getOpenMPDirectiveName(NM);
2473 Values += "'";
2474 if (AllowedCnt + 2 == TotalAllowedNum)
2475 Values += " or ";
2476 else if (AllowedCnt + 1 != TotalAllowedNum)
2477 Values += Sep;
2478 ++AllowedCnt;
2479 }
2480 }
2481 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2482 diag::err_omp_unnamed_if_clause)
2483 << (TotalAllowedNum > 1) << Values;
2484 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002485 for (auto Loc : NameModifierLoc) {
2486 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2487 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002488 ErrorFound = true;
2489 }
2490 return ErrorFound;
2491}
2492
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002493StmtResult Sema::ActOnOpenMPExecutableDirective(
2494 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2495 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2496 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002497 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002498 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2499 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002500 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002501
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002502 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002503 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002504 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002505 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002506 if (AStmt) {
2507 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2508
2509 // Check default data sharing attributes for referenced variables.
2510 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2511 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2512 if (DSAChecker.isErrorFound())
2513 return StmtError();
2514 // Generate list of implicitly defined firstprivate variables.
2515 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002516
2517 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2518 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2519 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2520 SourceLocation(), SourceLocation())) {
2521 ClausesWithImplicit.push_back(Implicit);
2522 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2523 DSAChecker.getImplicitFirstprivate().size();
2524 } else
2525 ErrorFound = true;
2526 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002527 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002528
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002529 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002530 switch (Kind) {
2531 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002532 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2533 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002534 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002535 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002536 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002537 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2538 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002539 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002540 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002541 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2542 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002543 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002544 case OMPD_for_simd:
2545 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2546 EndLoc, VarsWithInheritedDSA);
2547 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002548 case OMPD_sections:
2549 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2550 EndLoc);
2551 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002552 case OMPD_section:
2553 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002554 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002555 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2556 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002557 case OMPD_single:
2558 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2559 EndLoc);
2560 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002561 case OMPD_master:
2562 assert(ClausesWithImplicit.empty() &&
2563 "No clauses are allowed for 'omp master' directive");
2564 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2565 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002566 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002567 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2568 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002569 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002570 case OMPD_parallel_for:
2571 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2572 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002573 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002574 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002575 case OMPD_parallel_for_simd:
2576 Res = ActOnOpenMPParallelForSimdDirective(
2577 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002578 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002579 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002580 case OMPD_parallel_sections:
2581 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2582 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002583 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002584 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002585 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002586 Res =
2587 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002588 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002589 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002590 case OMPD_taskyield:
2591 assert(ClausesWithImplicit.empty() &&
2592 "No clauses are allowed for 'omp taskyield' directive");
2593 assert(AStmt == nullptr &&
2594 "No associated statement allowed for 'omp taskyield' directive");
2595 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2596 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002597 case OMPD_barrier:
2598 assert(ClausesWithImplicit.empty() &&
2599 "No clauses are allowed for 'omp barrier' directive");
2600 assert(AStmt == nullptr &&
2601 "No associated statement allowed for 'omp barrier' directive");
2602 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2603 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002604 case OMPD_taskwait:
2605 assert(ClausesWithImplicit.empty() &&
2606 "No clauses are allowed for 'omp taskwait' directive");
2607 assert(AStmt == nullptr &&
2608 "No associated statement allowed for 'omp taskwait' directive");
2609 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2610 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002611 case OMPD_taskgroup:
2612 assert(ClausesWithImplicit.empty() &&
2613 "No clauses are allowed for 'omp taskgroup' directive");
2614 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2615 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002616 case OMPD_flush:
2617 assert(AStmt == nullptr &&
2618 "No associated statement allowed for 'omp flush' directive");
2619 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2620 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002621 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002622 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2623 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002624 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002625 case OMPD_atomic:
2626 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2627 EndLoc);
2628 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002629 case OMPD_teams:
2630 Res =
2631 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2632 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002633 case OMPD_target:
2634 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2635 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002636 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002637 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002638 case OMPD_cancellation_point:
2639 assert(ClausesWithImplicit.empty() &&
2640 "No clauses are allowed for 'omp cancellation point' directive");
2641 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2642 "cancellation point' directive");
2643 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2644 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002645 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002646 assert(AStmt == nullptr &&
2647 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002648 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2649 CancelRegion);
2650 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002651 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002652 case OMPD_target_data:
2653 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2654 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002655 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002656 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002657 case OMPD_taskloop:
2658 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2659 EndLoc, VarsWithInheritedDSA);
2660 AllowedNameModifiers.push_back(OMPD_taskloop);
2661 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002662 case OMPD_taskloop_simd:
2663 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2664 EndLoc, VarsWithInheritedDSA);
2665 AllowedNameModifiers.push_back(OMPD_taskloop);
2666 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002667 case OMPD_distribute:
2668 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2669 EndLoc, VarsWithInheritedDSA);
2670 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002671 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002672 llvm_unreachable("OpenMP Directive is not allowed");
2673 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002674 llvm_unreachable("Unknown OpenMP directive");
2675 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002676
Alexey Bataev4acb8592014-07-07 13:01:15 +00002677 for (auto P : VarsWithInheritedDSA) {
2678 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2679 << P.first << P.second->getSourceRange();
2680 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002681 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2682
2683 if (!AllowedNameModifiers.empty())
2684 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2685 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002686
Alexey Bataeved09d242014-05-28 05:53:51 +00002687 if (ErrorFound)
2688 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002689 return Res;
2690}
2691
2692StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2693 Stmt *AStmt,
2694 SourceLocation StartLoc,
2695 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002696 if (!AStmt)
2697 return StmtError();
2698
Alexey Bataev9959db52014-05-06 10:08:46 +00002699 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2700 // 1.2.2 OpenMP Language Terminology
2701 // Structured block - An executable statement with a single entry at the
2702 // top and a single exit at the bottom.
2703 // The point of exit cannot be a branch out of the structured block.
2704 // longjmp() and throw() must not violate the entry/exit criteria.
2705 CS->getCapturedDecl()->setNothrow();
2706
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002707 getCurFunction()->setHasBranchProtectedScope();
2708
Alexey Bataev25e5b442015-09-15 12:52:43 +00002709 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2710 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002711}
2712
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002713namespace {
2714/// \brief Helper class for checking canonical form of the OpenMP loops and
2715/// extracting iteration space of each loop in the loop nest, that will be used
2716/// for IR generation.
2717class OpenMPIterationSpaceChecker {
2718 /// \brief Reference to Sema.
2719 Sema &SemaRef;
2720 /// \brief A location for diagnostics (when there is no some better location).
2721 SourceLocation DefaultLoc;
2722 /// \brief A location for diagnostics (when increment is not compatible).
2723 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002724 /// \brief A source location for referring to loop init later.
2725 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002726 /// \brief A source location for referring to condition later.
2727 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002728 /// \brief A source location for referring to increment later.
2729 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002730 /// \brief Loop variable.
2731 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002732 /// \brief Reference to loop variable.
2733 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002734 /// \brief Lower bound (initializer for the var).
2735 Expr *LB;
2736 /// \brief Upper bound.
2737 Expr *UB;
2738 /// \brief Loop step (increment).
2739 Expr *Step;
2740 /// \brief This flag is true when condition is one of:
2741 /// Var < UB
2742 /// Var <= UB
2743 /// UB > Var
2744 /// UB >= Var
2745 bool TestIsLessOp;
2746 /// \brief This flag is true when condition is strict ( < or > ).
2747 bool TestIsStrictOp;
2748 /// \brief This flag is true when step is subtracted on each iteration.
2749 bool SubtractStep;
2750
2751public:
2752 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2753 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002754 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2755 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002756 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2757 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002758 /// \brief Check init-expr for canonical loop form and save loop counter
2759 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002760 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002761 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2762 /// for less/greater and for strict/non-strict comparison.
2763 bool CheckCond(Expr *S);
2764 /// \brief Check incr-expr for canonical loop form and return true if it
2765 /// does not conform, otherwise save loop step (#Step).
2766 bool CheckInc(Expr *S);
2767 /// \brief Return the loop counter variable.
2768 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002769 /// \brief Return the reference expression to loop counter variable.
2770 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002771 /// \brief Source range of the loop init.
2772 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2773 /// \brief Source range of the loop condition.
2774 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2775 /// \brief Source range of the loop increment.
2776 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2777 /// \brief True if the step should be subtracted.
2778 bool ShouldSubtractStep() const { return SubtractStep; }
2779 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002780 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002781 /// \brief Build the precondition expression for the loops.
2782 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002783 /// \brief Build reference expression to the counter be used for codegen.
2784 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002785 /// \brief Build reference expression to the private counter be used for
2786 /// codegen.
2787 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002788 /// \brief Build initization of the counter be used for codegen.
2789 Expr *BuildCounterInit() const;
2790 /// \brief Build step of the counter be used for codegen.
2791 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002792 /// \brief Return true if any expression is dependent.
2793 bool Dependent() const;
2794
2795private:
2796 /// \brief Check the right-hand side of an assignment in the increment
2797 /// expression.
2798 bool CheckIncRHS(Expr *RHS);
2799 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002800 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002801 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002802 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002803 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002804 /// \brief Helper to set loop increment.
2805 bool SetStep(Expr *NewStep, bool Subtract);
2806};
2807
2808bool OpenMPIterationSpaceChecker::Dependent() const {
2809 if (!Var) {
2810 assert(!LB && !UB && !Step);
2811 return false;
2812 }
2813 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2814 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2815}
2816
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002817template <typename T>
2818static T *getExprAsWritten(T *E) {
2819 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2820 E = ExprTemp->getSubExpr();
2821
2822 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2823 E = MTE->GetTemporaryExpr();
2824
2825 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2826 E = Binder->getSubExpr();
2827
2828 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2829 E = ICE->getSubExprAsWritten();
2830 return E->IgnoreParens();
2831}
2832
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002833bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2834 DeclRefExpr *NewVarRefExpr,
2835 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002836 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002837 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2838 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002839 if (!NewVar || !NewLB)
2840 return true;
2841 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002842 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002843 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2844 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002845 if ((Ctor->isCopyOrMoveConstructor() ||
2846 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2847 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002848 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002849 LB = NewLB;
2850 return false;
2851}
2852
2853bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002854 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002855 // State consistency checking to ensure correct usage.
2856 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2857 !TestIsLessOp && !TestIsStrictOp);
2858 if (!NewUB)
2859 return true;
2860 UB = NewUB;
2861 TestIsLessOp = LessOp;
2862 TestIsStrictOp = StrictOp;
2863 ConditionSrcRange = SR;
2864 ConditionLoc = SL;
2865 return false;
2866}
2867
2868bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2869 // State consistency checking to ensure correct usage.
2870 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2871 if (!NewStep)
2872 return true;
2873 if (!NewStep->isValueDependent()) {
2874 // Check that the step is integer expression.
2875 SourceLocation StepLoc = NewStep->getLocStart();
2876 ExprResult Val =
2877 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2878 if (Val.isInvalid())
2879 return true;
2880 NewStep = Val.get();
2881
2882 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2883 // If test-expr is of form var relational-op b and relational-op is < or
2884 // <= then incr-expr must cause var to increase on each iteration of the
2885 // loop. If test-expr is of form var relational-op b and relational-op is
2886 // > or >= then incr-expr must cause var to decrease on each iteration of
2887 // the loop.
2888 // If test-expr is of form b relational-op var and relational-op is < or
2889 // <= then incr-expr must cause var to decrease on each iteration of the
2890 // loop. If test-expr is of form b relational-op var and relational-op is
2891 // > or >= then incr-expr must cause var to increase on each iteration of
2892 // the loop.
2893 llvm::APSInt Result;
2894 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2895 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2896 bool IsConstNeg =
2897 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002898 bool IsConstPos =
2899 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002900 bool IsConstZero = IsConstant && !Result.getBoolValue();
2901 if (UB && (IsConstZero ||
2902 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002903 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002904 SemaRef.Diag(NewStep->getExprLoc(),
2905 diag::err_omp_loop_incr_not_compatible)
2906 << Var << TestIsLessOp << NewStep->getSourceRange();
2907 SemaRef.Diag(ConditionLoc,
2908 diag::note_omp_loop_cond_requres_compatible_incr)
2909 << TestIsLessOp << ConditionSrcRange;
2910 return true;
2911 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002912 if (TestIsLessOp == Subtract) {
2913 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2914 NewStep).get();
2915 Subtract = !Subtract;
2916 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002917 }
2918
2919 Step = NewStep;
2920 SubtractStep = Subtract;
2921 return false;
2922}
2923
Alexey Bataev9c821032015-04-30 04:23:23 +00002924bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002925 // Check init-expr for canonical loop form and save loop counter
2926 // variable - #Var and its initialization value - #LB.
2927 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2928 // var = lb
2929 // integer-type var = lb
2930 // random-access-iterator-type var = lb
2931 // pointer-type var = lb
2932 //
2933 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002934 if (EmitDiags) {
2935 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2936 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002937 return true;
2938 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002939 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002940 if (Expr *E = dyn_cast<Expr>(S))
2941 S = E->IgnoreParens();
2942 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2943 if (BO->getOpcode() == BO_Assign)
2944 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002945 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002946 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002947 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2948 if (DS->isSingleDecl()) {
2949 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002950 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002951 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002952 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002953 SemaRef.Diag(S->getLocStart(),
2954 diag::ext_omp_loop_not_canonical_init)
2955 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002956 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002957 }
2958 }
2959 }
2960 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2961 if (CE->getOperator() == OO_Equal)
2962 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002963 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2964 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002965
Alexey Bataev9c821032015-04-30 04:23:23 +00002966 if (EmitDiags) {
2967 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2968 << S->getSourceRange();
2969 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002970 return true;
2971}
2972
Alexey Bataev23b69422014-06-18 07:08:49 +00002973/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002974/// variable (which may be the loop variable) if possible.
2975static const VarDecl *GetInitVarDecl(const Expr *E) {
2976 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002977 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002978 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002979 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2980 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002981 if ((Ctor->isCopyOrMoveConstructor() ||
2982 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2983 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002984 E = CE->getArg(0)->IgnoreParenImpCasts();
2985 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2986 if (!DRE)
2987 return nullptr;
2988 return dyn_cast<VarDecl>(DRE->getDecl());
2989}
2990
2991bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2992 // Check test-expr for canonical form, save upper-bound UB, flags for
2993 // less/greater and for strict/non-strict comparison.
2994 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2995 // var relational-op b
2996 // b relational-op var
2997 //
2998 if (!S) {
2999 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3000 return true;
3001 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003002 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003003 SourceLocation CondLoc = S->getLocStart();
3004 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3005 if (BO->isRelationalOp()) {
3006 if (GetInitVarDecl(BO->getLHS()) == Var)
3007 return SetUB(BO->getRHS(),
3008 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3009 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3010 BO->getSourceRange(), BO->getOperatorLoc());
3011 if (GetInitVarDecl(BO->getRHS()) == Var)
3012 return SetUB(BO->getLHS(),
3013 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3014 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3015 BO->getSourceRange(), BO->getOperatorLoc());
3016 }
3017 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3018 if (CE->getNumArgs() == 2) {
3019 auto Op = CE->getOperator();
3020 switch (Op) {
3021 case OO_Greater:
3022 case OO_GreaterEqual:
3023 case OO_Less:
3024 case OO_LessEqual:
3025 if (GetInitVarDecl(CE->getArg(0)) == Var)
3026 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3027 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3028 CE->getOperatorLoc());
3029 if (GetInitVarDecl(CE->getArg(1)) == Var)
3030 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3031 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3032 CE->getOperatorLoc());
3033 break;
3034 default:
3035 break;
3036 }
3037 }
3038 }
3039 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3040 << S->getSourceRange() << Var;
3041 return true;
3042}
3043
3044bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3045 // RHS of canonical loop form increment can be:
3046 // var + incr
3047 // incr + var
3048 // var - incr
3049 //
3050 RHS = RHS->IgnoreParenImpCasts();
3051 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3052 if (BO->isAdditiveOp()) {
3053 bool IsAdd = BO->getOpcode() == BO_Add;
3054 if (GetInitVarDecl(BO->getLHS()) == Var)
3055 return SetStep(BO->getRHS(), !IsAdd);
3056 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3057 return SetStep(BO->getLHS(), false);
3058 }
3059 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3060 bool IsAdd = CE->getOperator() == OO_Plus;
3061 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3062 if (GetInitVarDecl(CE->getArg(0)) == Var)
3063 return SetStep(CE->getArg(1), !IsAdd);
3064 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3065 return SetStep(CE->getArg(0), false);
3066 }
3067 }
3068 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3069 << RHS->getSourceRange() << Var;
3070 return true;
3071}
3072
3073bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3074 // Check incr-expr for canonical loop form and return true if it
3075 // does not conform.
3076 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3077 // ++var
3078 // var++
3079 // --var
3080 // var--
3081 // var += incr
3082 // var -= incr
3083 // var = var + incr
3084 // var = incr + var
3085 // var = var - incr
3086 //
3087 if (!S) {
3088 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3089 return true;
3090 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003092 S = S->IgnoreParens();
3093 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3094 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3095 return SetStep(
3096 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3097 (UO->isDecrementOp() ? -1 : 1)).get(),
3098 false);
3099 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3100 switch (BO->getOpcode()) {
3101 case BO_AddAssign:
3102 case BO_SubAssign:
3103 if (GetInitVarDecl(BO->getLHS()) == Var)
3104 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3105 break;
3106 case BO_Assign:
3107 if (GetInitVarDecl(BO->getLHS()) == Var)
3108 return CheckIncRHS(BO->getRHS());
3109 break;
3110 default:
3111 break;
3112 }
3113 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3114 switch (CE->getOperator()) {
3115 case OO_PlusPlus:
3116 case OO_MinusMinus:
3117 if (GetInitVarDecl(CE->getArg(0)) == Var)
3118 return SetStep(
3119 SemaRef.ActOnIntegerConstant(
3120 CE->getLocStart(),
3121 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3122 false);
3123 break;
3124 case OO_PlusEqual:
3125 case OO_MinusEqual:
3126 if (GetInitVarDecl(CE->getArg(0)) == Var)
3127 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3128 break;
3129 case OO_Equal:
3130 if (GetInitVarDecl(CE->getArg(0)) == Var)
3131 return CheckIncRHS(CE->getArg(1));
3132 break;
3133 default:
3134 break;
3135 }
3136 }
3137 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3138 << S->getSourceRange() << Var;
3139 return true;
3140}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003141
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003142namespace {
3143// Transform variables declared in GNU statement expressions to new ones to
3144// avoid crash on codegen.
3145class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3146 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3147
3148public:
3149 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3150
3151 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3152 if (auto *VD = cast<VarDecl>(D))
3153 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3154 !isa<ImplicitParamDecl>(D)) {
3155 auto *NewVD = VarDecl::Create(
3156 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3157 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3158 VD->getTypeSourceInfo(), VD->getStorageClass());
3159 NewVD->setTSCSpec(VD->getTSCSpec());
3160 NewVD->setInit(VD->getInit());
3161 NewVD->setInitStyle(VD->getInitStyle());
3162 NewVD->setExceptionVariable(VD->isExceptionVariable());
3163 NewVD->setNRVOVariable(VD->isNRVOVariable());
3164 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3165 NewVD->setConstexpr(VD->isConstexpr());
3166 NewVD->setInitCapture(VD->isInitCapture());
3167 NewVD->setPreviousDeclInSameBlockScope(
3168 VD->isPreviousDeclInSameBlockScope());
3169 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003170 if (VD->hasAttrs())
3171 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003172 transformedLocalDecl(VD, NewVD);
3173 return NewVD;
3174 }
3175 return BaseTransform::TransformDefinition(Loc, D);
3176 }
3177
3178 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3179 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3180 if (E->getDecl() != NewD) {
3181 NewD->setReferenced();
3182 NewD->markUsed(SemaRef.Context);
3183 return DeclRefExpr::Create(
3184 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3185 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3186 E->getNameInfo(), E->getType(), E->getValueKind());
3187 }
3188 return BaseTransform::TransformDeclRefExpr(E);
3189 }
3190};
3191}
3192
Alexander Musmana5f070a2014-10-01 06:03:56 +00003193/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003194Expr *
3195OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3196 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003197 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003198 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003199 auto VarType = Var->getType().getNonReferenceType();
3200 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003201 SemaRef.getLangOpts().CPlusPlus) {
3202 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003203 auto *UBExpr = TestIsLessOp ? UB : LB;
3204 auto *LBExpr = TestIsLessOp ? LB : UB;
3205 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3206 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3207 if (!Upper || !Lower)
3208 return nullptr;
3209 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3210 Sema::AA_Converting,
3211 /*AllowExplicit=*/true)
3212 .get();
3213 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3214 Sema::AA_Converting,
3215 /*AllowExplicit=*/true)
3216 .get();
3217 if (!Upper || !Lower)
3218 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003219
3220 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3221
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003222 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003223 // BuildBinOp already emitted error, this one is to point user to upper
3224 // and lower bound, and to tell what is passed to 'operator-'.
3225 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3226 << Upper->getSourceRange() << Lower->getSourceRange();
3227 return nullptr;
3228 }
3229 }
3230
3231 if (!Diff.isUsable())
3232 return nullptr;
3233
3234 // Upper - Lower [- 1]
3235 if (TestIsStrictOp)
3236 Diff = SemaRef.BuildBinOp(
3237 S, DefaultLoc, BO_Sub, Diff.get(),
3238 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3239 if (!Diff.isUsable())
3240 return nullptr;
3241
3242 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003243 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3244 if (NewStep.isInvalid())
3245 return nullptr;
3246 NewStep = SemaRef.PerformImplicitConversion(
3247 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3248 /*AllowExplicit=*/true);
3249 if (NewStep.isInvalid())
3250 return nullptr;
3251 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003252 if (!Diff.isUsable())
3253 return nullptr;
3254
3255 // Parentheses (for dumping/debugging purposes only).
3256 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3257 if (!Diff.isUsable())
3258 return nullptr;
3259
3260 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003261 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3262 if (NewStep.isInvalid())
3263 return nullptr;
3264 NewStep = SemaRef.PerformImplicitConversion(
3265 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3266 /*AllowExplicit=*/true);
3267 if (NewStep.isInvalid())
3268 return nullptr;
3269 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003270 if (!Diff.isUsable())
3271 return nullptr;
3272
Alexander Musman174b3ca2014-10-06 11:16:29 +00003273 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003274 QualType Type = Diff.get()->getType();
3275 auto &C = SemaRef.Context;
3276 bool UseVarType = VarType->hasIntegerRepresentation() &&
3277 C.getTypeSize(Type) > C.getTypeSize(VarType);
3278 if (!Type->isIntegerType() || UseVarType) {
3279 unsigned NewSize =
3280 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3281 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3282 : Type->hasSignedIntegerRepresentation();
3283 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3284 Diff = SemaRef.PerformImplicitConversion(
3285 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3286 if (!Diff.isUsable())
3287 return nullptr;
3288 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003289 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003290 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3291 if (NewSize != C.getTypeSize(Type)) {
3292 if (NewSize < C.getTypeSize(Type)) {
3293 assert(NewSize == 64 && "incorrect loop var size");
3294 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3295 << InitSrcRange << ConditionSrcRange;
3296 }
3297 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003298 NewSize, Type->hasSignedIntegerRepresentation() ||
3299 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003300 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3301 Sema::AA_Converting, true);
3302 if (!Diff.isUsable())
3303 return nullptr;
3304 }
3305 }
3306
Alexander Musmana5f070a2014-10-01 06:03:56 +00003307 return Diff.get();
3308}
3309
Alexey Bataev62dbb972015-04-22 11:59:37 +00003310Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3311 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3312 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3313 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003314 TransformToNewDefs Transform(SemaRef);
3315
3316 auto NewLB = Transform.TransformExpr(LB);
3317 auto NewUB = Transform.TransformExpr(UB);
3318 if (NewLB.isInvalid() || NewUB.isInvalid())
3319 return Cond;
3320 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3321 Sema::AA_Converting,
3322 /*AllowExplicit=*/true);
3323 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3324 Sema::AA_Converting,
3325 /*AllowExplicit=*/true);
3326 if (NewLB.isInvalid() || NewUB.isInvalid())
3327 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003328 auto CondExpr = SemaRef.BuildBinOp(
3329 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3330 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003331 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003332 if (CondExpr.isUsable()) {
3333 CondExpr = SemaRef.PerformImplicitConversion(
3334 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3335 /*AllowExplicit=*/true);
3336 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003337 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3338 // Otherwise use original loop conditon and evaluate it in runtime.
3339 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3340}
3341
Alexander Musmana5f070a2014-10-01 06:03:56 +00003342/// \brief Build reference expression to the counter be used for codegen.
3343Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003344 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3345 DefaultLoc);
3346}
3347
3348Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3349 if (Var && !Var->isInvalidDecl()) {
3350 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003351 auto *PrivateVar =
3352 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3353 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003354 if (PrivateVar->isInvalidDecl())
3355 return nullptr;
3356 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3357 }
3358 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003359}
3360
3361/// \brief Build initization of the counter be used for codegen.
3362Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3363
3364/// \brief Build step of the counter be used for codegen.
3365Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3366
3367/// \brief Iteration space of a single for loop.
3368struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003369 /// \brief Condition of the loop.
3370 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003371 /// \brief This expression calculates the number of iterations in the loop.
3372 /// It is always possible to calculate it before starting the loop.
3373 Expr *NumIterations;
3374 /// \brief The loop counter variable.
3375 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003376 /// \brief Private loop counter variable.
3377 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003378 /// \brief This is initializer for the initial value of #CounterVar.
3379 Expr *CounterInit;
3380 /// \brief This is step for the #CounterVar used to generate its update:
3381 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3382 Expr *CounterStep;
3383 /// \brief Should step be subtracted?
3384 bool Subtract;
3385 /// \brief Source range of the loop init.
3386 SourceRange InitSrcRange;
3387 /// \brief Source range of the loop condition.
3388 SourceRange CondSrcRange;
3389 /// \brief Source range of the loop increment.
3390 SourceRange IncSrcRange;
3391};
3392
Alexey Bataev23b69422014-06-18 07:08:49 +00003393} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003394
Alexey Bataev9c821032015-04-30 04:23:23 +00003395void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3396 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3397 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003398 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3399 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003400 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3401 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003402 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003403 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003404 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003405 }
3406}
3407
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003408/// \brief Called on a for stmt to check and extract its iteration space
3409/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003410static bool CheckOpenMPIterationSpace(
3411 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3412 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003413 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003414 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3415 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003416 // OpenMP [2.6, Canonical Loop Form]
3417 // for (init-expr; test-expr; incr-expr) structured-block
3418 auto For = dyn_cast_or_null<ForStmt>(S);
3419 if (!For) {
3420 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003421 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3422 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3423 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3424 if (NestedLoopCount > 1) {
3425 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3426 SemaRef.Diag(DSA.getConstructLoc(),
3427 diag::note_omp_collapse_ordered_expr)
3428 << 2 << CollapseLoopCountExpr->getSourceRange()
3429 << OrderedLoopCountExpr->getSourceRange();
3430 else if (CollapseLoopCountExpr)
3431 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3432 diag::note_omp_collapse_ordered_expr)
3433 << 0 << CollapseLoopCountExpr->getSourceRange();
3434 else
3435 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3436 diag::note_omp_collapse_ordered_expr)
3437 << 1 << OrderedLoopCountExpr->getSourceRange();
3438 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003439 return true;
3440 }
3441 assert(For->getBody());
3442
3443 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3444
3445 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003446 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003447 if (ISC.CheckInit(Init)) {
3448 return true;
3449 }
3450
3451 bool HasErrors = false;
3452
3453 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003454 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003455
3456 // OpenMP [2.6, Canonical Loop Form]
3457 // Var is one of the following:
3458 // A variable of signed or unsigned integer type.
3459 // For C++, a variable of a random access iterator type.
3460 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003461 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003462 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3463 !VarType->isPointerType() &&
3464 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3465 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3466 << SemaRef.getLangOpts().CPlusPlus;
3467 HasErrors = true;
3468 }
3469
Alexey Bataev4acb8592014-07-07 13:01:15 +00003470 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3471 // Construct
3472 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3473 // parallel for construct is (are) private.
3474 // The loop iteration variable in the associated for-loop of a simd construct
3475 // with just one associated for-loop is linear with a constant-linear-step
3476 // that is the increment of the associated for-loop.
3477 // Exclude loop var from the list of variables with implicitly defined data
3478 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003479 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003480
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003481 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3482 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003483 // The loop iteration variable in the associated for-loop of a simd construct
3484 // with just one associated for-loop may be listed in a linear clause with a
3485 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003486 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3487 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003488 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003489 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3490 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3491 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003492 auto PredeterminedCKind =
3493 isOpenMPSimdDirective(DKind)
3494 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3495 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003496 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003497 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003498 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003499 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003500 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003501 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3502 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003503 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003504 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3505 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003506 if (DVar.RefExpr == nullptr)
3507 DVar.CKind = PredeterminedCKind;
3508 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003509 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003510 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003511 // Make the loop iteration variable private (for worksharing constructs),
3512 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003513 // lastprivate (for simd directives with several collapsed or ordered
3514 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003515 if (DVar.CKind == OMPC_unknown)
3516 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3517 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003518 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003519 }
3520
Alexey Bataev7ff55242014-06-19 09:13:45 +00003521 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003522
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003523 // Check test-expr.
3524 HasErrors |= ISC.CheckCond(For->getCond());
3525
3526 // Check incr-expr.
3527 HasErrors |= ISC.CheckInc(For->getInc());
3528
Alexander Musmana5f070a2014-10-01 06:03:56 +00003529 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003530 return HasErrors;
3531
Alexander Musmana5f070a2014-10-01 06:03:56 +00003532 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003533 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003534 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003535 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003536 isOpenMPTaskLoopDirective(DKind) ||
3537 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003538 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003539 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003540 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3541 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3542 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3543 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3544 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3545 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3546
Alexey Bataev62dbb972015-04-22 11:59:37 +00003547 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3548 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003549 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003550 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003551 ResultIterSpace.CounterInit == nullptr ||
3552 ResultIterSpace.CounterStep == nullptr);
3553
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003554 return HasErrors;
3555}
3556
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003557/// \brief Build 'VarRef = Start.
3558static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3559 ExprResult VarRef, ExprResult Start) {
3560 TransformToNewDefs Transform(SemaRef);
3561 // Build 'VarRef = Start.
3562 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3563 if (NewStart.isInvalid())
3564 return ExprError();
3565 NewStart = SemaRef.PerformImplicitConversion(
3566 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3567 Sema::AA_Converting,
3568 /*AllowExplicit=*/true);
3569 if (NewStart.isInvalid())
3570 return ExprError();
3571 NewStart = SemaRef.PerformImplicitConversion(
3572 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3573 /*AllowExplicit=*/true);
3574 if (!NewStart.isUsable())
3575 return ExprError();
3576
3577 auto Init =
3578 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3579 return Init;
3580}
3581
Alexander Musmana5f070a2014-10-01 06:03:56 +00003582/// \brief Build 'VarRef = Start + Iter * Step'.
3583static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3584 SourceLocation Loc, ExprResult VarRef,
3585 ExprResult Start, ExprResult Iter,
3586 ExprResult Step, bool Subtract) {
3587 // Add parentheses (for debugging purposes only).
3588 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3589 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3590 !Step.isUsable())
3591 return ExprError();
3592
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003593 TransformToNewDefs Transform(SemaRef);
3594 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3595 if (NewStep.isInvalid())
3596 return ExprError();
3597 NewStep = SemaRef.PerformImplicitConversion(
3598 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3599 Sema::AA_Converting,
3600 /*AllowExplicit=*/true);
3601 if (NewStep.isInvalid())
3602 return ExprError();
3603 ExprResult Update =
3604 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003605 if (!Update.isUsable())
3606 return ExprError();
3607
3608 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003609 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3610 if (NewStart.isInvalid())
3611 return ExprError();
3612 NewStart = SemaRef.PerformImplicitConversion(
3613 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3614 Sema::AA_Converting,
3615 /*AllowExplicit=*/true);
3616 if (NewStart.isInvalid())
3617 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003618 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003619 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003620 if (!Update.isUsable())
3621 return ExprError();
3622
3623 Update = SemaRef.PerformImplicitConversion(
3624 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3625 if (!Update.isUsable())
3626 return ExprError();
3627
3628 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3629 return Update;
3630}
3631
3632/// \brief Convert integer expression \a E to make it have at least \a Bits
3633/// bits.
3634static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3635 Sema &SemaRef) {
3636 if (E == nullptr)
3637 return ExprError();
3638 auto &C = SemaRef.Context;
3639 QualType OldType = E->getType();
3640 unsigned HasBits = C.getTypeSize(OldType);
3641 if (HasBits >= Bits)
3642 return ExprResult(E);
3643 // OK to convert to signed, because new type has more bits than old.
3644 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3645 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3646 true);
3647}
3648
3649/// \brief Check if the given expression \a E is a constant integer that fits
3650/// into \a Bits bits.
3651static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3652 if (E == nullptr)
3653 return false;
3654 llvm::APSInt Result;
3655 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3656 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3657 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003658}
3659
3660/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003661/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3662/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003663static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003664CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3665 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3666 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003667 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003668 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003669 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003670 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003671 // Found 'collapse' clause - calculate collapse number.
3672 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003673 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003674 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003675 }
3676 if (OrderedLoopCountExpr) {
3677 // Found 'ordered' clause - calculate collapse number.
3678 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003679 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3680 if (Result.getLimitedValue() < NestedLoopCount) {
3681 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3682 diag::err_omp_wrong_ordered_loop_count)
3683 << OrderedLoopCountExpr->getSourceRange();
3684 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3685 diag::note_collapse_loop_count)
3686 << CollapseLoopCountExpr->getSourceRange();
3687 }
3688 NestedLoopCount = Result.getLimitedValue();
3689 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003690 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003691 // This is helper routine for loop directives (e.g., 'for', 'simd',
3692 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003693 SmallVector<LoopIterationSpace, 4> IterSpaces;
3694 IterSpaces.resize(NestedLoopCount);
3695 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003696 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003697 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003698 NestedLoopCount, CollapseLoopCountExpr,
3699 OrderedLoopCountExpr, VarsWithImplicitDSA,
3700 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003701 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003702 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003703 // OpenMP [2.8.1, simd construct, Restrictions]
3704 // All loops associated with the construct must be perfectly nested; that
3705 // is, there must be no intervening code nor any OpenMP directive between
3706 // any two loops.
3707 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003708 }
3709
Alexander Musmana5f070a2014-10-01 06:03:56 +00003710 Built.clear(/* size */ NestedLoopCount);
3711
3712 if (SemaRef.CurContext->isDependentContext())
3713 return NestedLoopCount;
3714
3715 // An example of what is generated for the following code:
3716 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003717 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003718 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003719 // for (k = 0; k < NK; ++k)
3720 // for (j = J0; j < NJ; j+=2) {
3721 // <loop body>
3722 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003723 //
3724 // We generate the code below.
3725 // Note: the loop body may be outlined in CodeGen.
3726 // Note: some counters may be C++ classes, operator- is used to find number of
3727 // iterations and operator+= to calculate counter value.
3728 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3729 // or i64 is currently supported).
3730 //
3731 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3732 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3733 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3734 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3735 // // similar updates for vars in clauses (e.g. 'linear')
3736 // <loop body (using local i and j)>
3737 // }
3738 // i = NI; // assign final values of counters
3739 // j = NJ;
3740 //
3741
3742 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3743 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003744 // Precondition tests if there is at least one iteration (all conditions are
3745 // true).
3746 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003747 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003748 ExprResult LastIteration32 = WidenIterationCount(
3749 32 /* Bits */, SemaRef.PerformImplicitConversion(
3750 N0->IgnoreImpCasts(), N0->getType(),
3751 Sema::AA_Converting, /*AllowExplicit=*/true)
3752 .get(),
3753 SemaRef);
3754 ExprResult LastIteration64 = WidenIterationCount(
3755 64 /* Bits */, SemaRef.PerformImplicitConversion(
3756 N0->IgnoreImpCasts(), N0->getType(),
3757 Sema::AA_Converting, /*AllowExplicit=*/true)
3758 .get(),
3759 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003760
3761 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3762 return NestedLoopCount;
3763
3764 auto &C = SemaRef.Context;
3765 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3766
3767 Scope *CurScope = DSA.getCurScope();
3768 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003769 if (PreCond.isUsable()) {
3770 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3771 PreCond.get(), IterSpaces[Cnt].PreCond);
3772 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003773 auto N = IterSpaces[Cnt].NumIterations;
3774 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3775 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003776 LastIteration32 = SemaRef.BuildBinOp(
3777 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3778 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3779 Sema::AA_Converting,
3780 /*AllowExplicit=*/true)
3781 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003782 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003783 LastIteration64 = SemaRef.BuildBinOp(
3784 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3785 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3786 Sema::AA_Converting,
3787 /*AllowExplicit=*/true)
3788 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003789 }
3790
3791 // Choose either the 32-bit or 64-bit version.
3792 ExprResult LastIteration = LastIteration64;
3793 if (LastIteration32.isUsable() &&
3794 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3795 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3796 FitsInto(
3797 32 /* Bits */,
3798 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3799 LastIteration64.get(), SemaRef)))
3800 LastIteration = LastIteration32;
3801
3802 if (!LastIteration.isUsable())
3803 return 0;
3804
3805 // Save the number of iterations.
3806 ExprResult NumIterations = LastIteration;
3807 {
3808 LastIteration = SemaRef.BuildBinOp(
3809 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3810 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3811 if (!LastIteration.isUsable())
3812 return 0;
3813 }
3814
3815 // Calculate the last iteration number beforehand instead of doing this on
3816 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3817 llvm::APSInt Result;
3818 bool IsConstant =
3819 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3820 ExprResult CalcLastIteration;
3821 if (!IsConstant) {
3822 SourceLocation SaveLoc;
3823 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003824 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003825 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003826 ExprResult SaveRef = buildDeclRefExpr(
3827 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003828 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3829 SaveRef.get(), LastIteration.get());
3830 LastIteration = SaveRef;
3831
3832 // Prepare SaveRef + 1.
3833 NumIterations = SemaRef.BuildBinOp(
3834 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3835 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3836 if (!NumIterations.isUsable())
3837 return 0;
3838 }
3839
3840 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3841
Alexander Musmanc6388682014-12-15 07:07:06 +00003842 QualType VType = LastIteration.get()->getType();
3843 // Build variables passed into runtime, nesessary for worksharing directives.
3844 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003845 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3846 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003847 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003848 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3849 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003850 SemaRef.AddInitializerToDecl(
3851 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3852 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3853
3854 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003855 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3856 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003857 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3858 /*DirectInit*/ false,
3859 /*TypeMayContainAuto*/ false);
3860
3861 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3862 // This will be used to implement clause 'lastprivate'.
3863 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003864 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3865 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003866 SemaRef.AddInitializerToDecl(
3867 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3868 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3869
3870 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003871 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3872 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003873 SemaRef.AddInitializerToDecl(
3874 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3875 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3876
3877 // Build expression: UB = min(UB, LastIteration)
3878 // It is nesessary for CodeGen of directives with static scheduling.
3879 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3880 UB.get(), LastIteration.get());
3881 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3882 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3883 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3884 CondOp.get());
3885 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3886 }
3887
3888 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003889 ExprResult IV;
3890 ExprResult Init;
3891 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003892 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3893 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003894 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003895 isOpenMPTaskLoopDirective(DKind) ||
3896 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003897 ? LB.get()
3898 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3899 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3900 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003901 }
3902
Alexander Musmanc6388682014-12-15 07:07:06 +00003903 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003904 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003905 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003906 (isOpenMPWorksharingDirective(DKind) ||
3907 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003908 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3909 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3910 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003911
3912 // Loop increment (IV = IV + 1)
3913 SourceLocation IncLoc;
3914 ExprResult Inc =
3915 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3916 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3917 if (!Inc.isUsable())
3918 return 0;
3919 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003920 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3921 if (!Inc.isUsable())
3922 return 0;
3923
3924 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3925 // Used for directives with static scheduling.
3926 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003927 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3928 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003929 // LB + ST
3930 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3931 if (!NextLB.isUsable())
3932 return 0;
3933 // LB = LB + ST
3934 NextLB =
3935 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3936 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3937 if (!NextLB.isUsable())
3938 return 0;
3939 // UB + ST
3940 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3941 if (!NextUB.isUsable())
3942 return 0;
3943 // UB = UB + ST
3944 NextUB =
3945 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3946 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3947 if (!NextUB.isUsable())
3948 return 0;
3949 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003950
3951 // Build updates and final values of the loop counters.
3952 bool HasErrors = false;
3953 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003954 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003955 Built.Updates.resize(NestedLoopCount);
3956 Built.Finals.resize(NestedLoopCount);
3957 {
3958 ExprResult Div;
3959 // Go from inner nested loop to outer.
3960 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3961 LoopIterationSpace &IS = IterSpaces[Cnt];
3962 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3963 // Build: Iter = (IV / Div) % IS.NumIters
3964 // where Div is product of previous iterations' IS.NumIters.
3965 ExprResult Iter;
3966 if (Div.isUsable()) {
3967 Iter =
3968 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3969 } else {
3970 Iter = IV;
3971 assert((Cnt == (int)NestedLoopCount - 1) &&
3972 "unusable div expected on first iteration only");
3973 }
3974
3975 if (Cnt != 0 && Iter.isUsable())
3976 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3977 IS.NumIterations);
3978 if (!Iter.isUsable()) {
3979 HasErrors = true;
3980 break;
3981 }
3982
Alexey Bataev39f915b82015-05-08 10:41:21 +00003983 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3984 auto *CounterVar = buildDeclRefExpr(
3985 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3986 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3987 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003988 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3989 IS.CounterInit);
3990 if (!Init.isUsable()) {
3991 HasErrors = true;
3992 break;
3993 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003994 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003995 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003996 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3997 if (!Update.isUsable()) {
3998 HasErrors = true;
3999 break;
4000 }
4001
4002 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4003 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004004 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004005 IS.NumIterations, IS.CounterStep, IS.Subtract);
4006 if (!Final.isUsable()) {
4007 HasErrors = true;
4008 break;
4009 }
4010
4011 // Build Div for the next iteration: Div <- Div * IS.NumIters
4012 if (Cnt != 0) {
4013 if (Div.isUnset())
4014 Div = IS.NumIterations;
4015 else
4016 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4017 IS.NumIterations);
4018
4019 // Add parentheses (for debugging purposes only).
4020 if (Div.isUsable())
4021 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4022 if (!Div.isUsable()) {
4023 HasErrors = true;
4024 break;
4025 }
4026 }
4027 if (!Update.isUsable() || !Final.isUsable()) {
4028 HasErrors = true;
4029 break;
4030 }
4031 // Save results
4032 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004033 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004034 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004035 Built.Updates[Cnt] = Update.get();
4036 Built.Finals[Cnt] = Final.get();
4037 }
4038 }
4039
4040 if (HasErrors)
4041 return 0;
4042
4043 // Save results
4044 Built.IterationVarRef = IV.get();
4045 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004046 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004047 Built.CalcLastIteration =
4048 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004049 Built.PreCond = PreCond.get();
4050 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004051 Built.Init = Init.get();
4052 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004053 Built.LB = LB.get();
4054 Built.UB = UB.get();
4055 Built.IL = IL.get();
4056 Built.ST = ST.get();
4057 Built.EUB = EUB.get();
4058 Built.NLB = NextLB.get();
4059 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004060
Alexey Bataevabfc0692014-06-25 06:52:00 +00004061 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004062}
4063
Alexey Bataev10e775f2015-07-30 11:36:16 +00004064static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004065 auto CollapseClauses =
4066 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4067 if (CollapseClauses.begin() != CollapseClauses.end())
4068 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004069 return nullptr;
4070}
4071
Alexey Bataev10e775f2015-07-30 11:36:16 +00004072static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004073 auto OrderedClauses =
4074 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4075 if (OrderedClauses.begin() != OrderedClauses.end())
4076 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004077 return nullptr;
4078}
4079
Alexey Bataev66b15b52015-08-21 11:14:16 +00004080static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4081 const Expr *Safelen) {
4082 llvm::APSInt SimdlenRes, SafelenRes;
4083 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4084 Simdlen->isInstantiationDependent() ||
4085 Simdlen->containsUnexpandedParameterPack())
4086 return false;
4087 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4088 Safelen->isInstantiationDependent() ||
4089 Safelen->containsUnexpandedParameterPack())
4090 return false;
4091 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4092 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4093 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4094 // If both simdlen and safelen clauses are specified, the value of the simdlen
4095 // parameter must be less than or equal to the value of the safelen parameter.
4096 if (SimdlenRes > SafelenRes) {
4097 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4098 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4099 return true;
4100 }
4101 return false;
4102}
4103
Alexey Bataev4acb8592014-07-07 13:01:15 +00004104StmtResult Sema::ActOnOpenMPSimdDirective(
4105 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4106 SourceLocation EndLoc,
4107 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004108 if (!AStmt)
4109 return StmtError();
4110
4111 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004112 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004113 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4114 // define the nested loops number.
4115 unsigned NestedLoopCount = CheckOpenMPLoop(
4116 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4117 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004118 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004119 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004120
Alexander Musmana5f070a2014-10-01 06:03:56 +00004121 assert((CurContext->isDependentContext() || B.builtAll()) &&
4122 "omp simd loop exprs were not built");
4123
Alexander Musman3276a272015-03-21 10:12:56 +00004124 if (!CurContext->isDependentContext()) {
4125 // Finalize the clauses that need pre-built expressions for CodeGen.
4126 for (auto C : Clauses) {
4127 if (auto LC = dyn_cast<OMPLinearClause>(C))
4128 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4129 B.NumIterations, *this, CurScope))
4130 return StmtError();
4131 }
4132 }
4133
Alexey Bataev66b15b52015-08-21 11:14:16 +00004134 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4135 // If both simdlen and safelen clauses are specified, the value of the simdlen
4136 // parameter must be less than or equal to the value of the safelen parameter.
4137 OMPSafelenClause *Safelen = nullptr;
4138 OMPSimdlenClause *Simdlen = nullptr;
4139 for (auto *Clause : Clauses) {
4140 if (Clause->getClauseKind() == OMPC_safelen)
4141 Safelen = cast<OMPSafelenClause>(Clause);
4142 else if (Clause->getClauseKind() == OMPC_simdlen)
4143 Simdlen = cast<OMPSimdlenClause>(Clause);
4144 if (Safelen && Simdlen)
4145 break;
4146 }
4147 if (Simdlen && Safelen &&
4148 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4149 Safelen->getSafelen()))
4150 return StmtError();
4151
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004152 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004153 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4154 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004155}
4156
Alexey Bataev4acb8592014-07-07 13:01:15 +00004157StmtResult Sema::ActOnOpenMPForDirective(
4158 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4159 SourceLocation EndLoc,
4160 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004161 if (!AStmt)
4162 return StmtError();
4163
4164 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004165 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004166 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4167 // define the nested loops number.
4168 unsigned NestedLoopCount = CheckOpenMPLoop(
4169 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4170 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004171 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004172 return StmtError();
4173
Alexander Musmana5f070a2014-10-01 06:03:56 +00004174 assert((CurContext->isDependentContext() || B.builtAll()) &&
4175 "omp for loop exprs were not built");
4176
Alexey Bataev54acd402015-08-04 11:18:19 +00004177 if (!CurContext->isDependentContext()) {
4178 // Finalize the clauses that need pre-built expressions for CodeGen.
4179 for (auto C : Clauses) {
4180 if (auto LC = dyn_cast<OMPLinearClause>(C))
4181 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4182 B.NumIterations, *this, CurScope))
4183 return StmtError();
4184 }
4185 }
4186
Alexey Bataevf29276e2014-06-18 04:14:57 +00004187 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004188 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004189 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004190}
4191
Alexander Musmanf82886e2014-09-18 05:12:34 +00004192StmtResult Sema::ActOnOpenMPForSimdDirective(
4193 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4194 SourceLocation EndLoc,
4195 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004196 if (!AStmt)
4197 return StmtError();
4198
4199 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004200 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004201 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4202 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004203 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004204 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4205 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4206 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004207 if (NestedLoopCount == 0)
4208 return StmtError();
4209
Alexander Musmanc6388682014-12-15 07:07:06 +00004210 assert((CurContext->isDependentContext() || B.builtAll()) &&
4211 "omp for simd loop exprs were not built");
4212
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004213 if (!CurContext->isDependentContext()) {
4214 // Finalize the clauses that need pre-built expressions for CodeGen.
4215 for (auto C : Clauses) {
4216 if (auto LC = dyn_cast<OMPLinearClause>(C))
4217 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4218 B.NumIterations, *this, CurScope))
4219 return StmtError();
4220 }
4221 }
4222
Alexey Bataev66b15b52015-08-21 11:14:16 +00004223 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4224 // If both simdlen and safelen clauses are specified, the value of the simdlen
4225 // parameter must be less than or equal to the value of the safelen parameter.
4226 OMPSafelenClause *Safelen = nullptr;
4227 OMPSimdlenClause *Simdlen = nullptr;
4228 for (auto *Clause : Clauses) {
4229 if (Clause->getClauseKind() == OMPC_safelen)
4230 Safelen = cast<OMPSafelenClause>(Clause);
4231 else if (Clause->getClauseKind() == OMPC_simdlen)
4232 Simdlen = cast<OMPSimdlenClause>(Clause);
4233 if (Safelen && Simdlen)
4234 break;
4235 }
4236 if (Simdlen && Safelen &&
4237 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4238 Safelen->getSafelen()))
4239 return StmtError();
4240
Alexander Musmanf82886e2014-09-18 05:12:34 +00004241 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004242 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4243 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004244}
4245
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004246StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4247 Stmt *AStmt,
4248 SourceLocation StartLoc,
4249 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004250 if (!AStmt)
4251 return StmtError();
4252
4253 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004254 auto BaseStmt = AStmt;
4255 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4256 BaseStmt = CS->getCapturedStmt();
4257 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4258 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004259 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004260 return StmtError();
4261 // All associated statements must be '#pragma omp section' except for
4262 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004263 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004264 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4265 if (SectionStmt)
4266 Diag(SectionStmt->getLocStart(),
4267 diag::err_omp_sections_substmt_not_section);
4268 return StmtError();
4269 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004270 cast<OMPSectionDirective>(SectionStmt)
4271 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004272 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004273 } else {
4274 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4275 return StmtError();
4276 }
4277
4278 getCurFunction()->setHasBranchProtectedScope();
4279
Alexey Bataev25e5b442015-09-15 12:52:43 +00004280 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4281 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004282}
4283
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004284StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4285 SourceLocation StartLoc,
4286 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004287 if (!AStmt)
4288 return StmtError();
4289
4290 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004291
4292 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004293 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004294
Alexey Bataev25e5b442015-09-15 12:52:43 +00004295 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4296 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004297}
4298
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004299StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4300 Stmt *AStmt,
4301 SourceLocation StartLoc,
4302 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004303 if (!AStmt)
4304 return StmtError();
4305
4306 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004307
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004308 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004309
Alexey Bataev3255bf32015-01-19 05:20:46 +00004310 // OpenMP [2.7.3, single Construct, Restrictions]
4311 // The copyprivate clause must not be used with the nowait clause.
4312 OMPClause *Nowait = nullptr;
4313 OMPClause *Copyprivate = nullptr;
4314 for (auto *Clause : Clauses) {
4315 if (Clause->getClauseKind() == OMPC_nowait)
4316 Nowait = Clause;
4317 else if (Clause->getClauseKind() == OMPC_copyprivate)
4318 Copyprivate = Clause;
4319 if (Copyprivate && Nowait) {
4320 Diag(Copyprivate->getLocStart(),
4321 diag::err_omp_single_copyprivate_with_nowait);
4322 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4323 return StmtError();
4324 }
4325 }
4326
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004327 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4328}
4329
Alexander Musman80c22892014-07-17 08:54:58 +00004330StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4331 SourceLocation StartLoc,
4332 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004333 if (!AStmt)
4334 return StmtError();
4335
4336 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004337
4338 getCurFunction()->setHasBranchProtectedScope();
4339
4340 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4341}
4342
Alexey Bataev28c75412015-12-15 08:19:24 +00004343StmtResult Sema::ActOnOpenMPCriticalDirective(
4344 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4345 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004346 if (!AStmt)
4347 return StmtError();
4348
4349 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004350
Alexey Bataev28c75412015-12-15 08:19:24 +00004351 bool ErrorFound = false;
4352 llvm::APSInt Hint;
4353 SourceLocation HintLoc;
4354 bool DependentHint = false;
4355 for (auto *C : Clauses) {
4356 if (C->getClauseKind() == OMPC_hint) {
4357 if (!DirName.getName()) {
4358 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4359 ErrorFound = true;
4360 }
4361 Expr *E = cast<OMPHintClause>(C)->getHint();
4362 if (E->isTypeDependent() || E->isValueDependent() ||
4363 E->isInstantiationDependent())
4364 DependentHint = true;
4365 else {
4366 Hint = E->EvaluateKnownConstInt(Context);
4367 HintLoc = C->getLocStart();
4368 }
4369 }
4370 }
4371 if (ErrorFound)
4372 return StmtError();
4373 auto Pair = DSAStack->getCriticalWithHint(DirName);
4374 if (Pair.first && DirName.getName() && !DependentHint) {
4375 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4376 Diag(StartLoc, diag::err_omp_critical_with_hint);
4377 if (HintLoc.isValid()) {
4378 Diag(HintLoc, diag::note_omp_critical_hint_here)
4379 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4380 } else
4381 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4382 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4383 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4384 << 1
4385 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4386 /*Radix=*/10, /*Signed=*/false);
4387 } else
4388 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4389 }
4390 }
4391
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004392 getCurFunction()->setHasBranchProtectedScope();
4393
Alexey Bataev28c75412015-12-15 08:19:24 +00004394 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4395 Clauses, AStmt);
4396 if (!Pair.first && DirName.getName() && !DependentHint)
4397 DSAStack->addCriticalWithHint(Dir, Hint);
4398 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004399}
4400
Alexey Bataev4acb8592014-07-07 13:01:15 +00004401StmtResult Sema::ActOnOpenMPParallelForDirective(
4402 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4403 SourceLocation EndLoc,
4404 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004405 if (!AStmt)
4406 return StmtError();
4407
Alexey Bataev4acb8592014-07-07 13:01:15 +00004408 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4409 // 1.2.2 OpenMP Language Terminology
4410 // Structured block - An executable statement with a single entry at the
4411 // top and a single exit at the bottom.
4412 // The point of exit cannot be a branch out of the structured block.
4413 // longjmp() and throw() must not violate the entry/exit criteria.
4414 CS->getCapturedDecl()->setNothrow();
4415
Alexander Musmanc6388682014-12-15 07:07:06 +00004416 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004417 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4418 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004419 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004420 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4421 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4422 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004423 if (NestedLoopCount == 0)
4424 return StmtError();
4425
Alexander Musmana5f070a2014-10-01 06:03:56 +00004426 assert((CurContext->isDependentContext() || B.builtAll()) &&
4427 "omp parallel for loop exprs were not built");
4428
Alexey Bataev54acd402015-08-04 11:18:19 +00004429 if (!CurContext->isDependentContext()) {
4430 // Finalize the clauses that need pre-built expressions for CodeGen.
4431 for (auto C : Clauses) {
4432 if (auto LC = dyn_cast<OMPLinearClause>(C))
4433 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4434 B.NumIterations, *this, CurScope))
4435 return StmtError();
4436 }
4437 }
4438
Alexey Bataev4acb8592014-07-07 13:01:15 +00004439 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004440 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004441 NestedLoopCount, Clauses, AStmt, B,
4442 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004443}
4444
Alexander Musmane4e893b2014-09-23 09:33:00 +00004445StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4446 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4447 SourceLocation EndLoc,
4448 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004449 if (!AStmt)
4450 return StmtError();
4451
Alexander Musmane4e893b2014-09-23 09:33:00 +00004452 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4453 // 1.2.2 OpenMP Language Terminology
4454 // Structured block - An executable statement with a single entry at the
4455 // top and a single exit at the bottom.
4456 // The point of exit cannot be a branch out of the structured block.
4457 // longjmp() and throw() must not violate the entry/exit criteria.
4458 CS->getCapturedDecl()->setNothrow();
4459
Alexander Musmanc6388682014-12-15 07:07:06 +00004460 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004461 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4462 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004463 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004464 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4465 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4466 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004467 if (NestedLoopCount == 0)
4468 return StmtError();
4469
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004470 if (!CurContext->isDependentContext()) {
4471 // Finalize the clauses that need pre-built expressions for CodeGen.
4472 for (auto C : Clauses) {
4473 if (auto LC = dyn_cast<OMPLinearClause>(C))
4474 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4475 B.NumIterations, *this, CurScope))
4476 return StmtError();
4477 }
4478 }
4479
Alexey Bataev66b15b52015-08-21 11:14:16 +00004480 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4481 // If both simdlen and safelen clauses are specified, the value of the simdlen
4482 // parameter must be less than or equal to the value of the safelen parameter.
4483 OMPSafelenClause *Safelen = nullptr;
4484 OMPSimdlenClause *Simdlen = nullptr;
4485 for (auto *Clause : Clauses) {
4486 if (Clause->getClauseKind() == OMPC_safelen)
4487 Safelen = cast<OMPSafelenClause>(Clause);
4488 else if (Clause->getClauseKind() == OMPC_simdlen)
4489 Simdlen = cast<OMPSimdlenClause>(Clause);
4490 if (Safelen && Simdlen)
4491 break;
4492 }
4493 if (Simdlen && Safelen &&
4494 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4495 Safelen->getSafelen()))
4496 return StmtError();
4497
Alexander Musmane4e893b2014-09-23 09:33:00 +00004498 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004499 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004500 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004501}
4502
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004503StmtResult
4504Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4505 Stmt *AStmt, SourceLocation StartLoc,
4506 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004507 if (!AStmt)
4508 return StmtError();
4509
4510 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004511 auto BaseStmt = AStmt;
4512 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4513 BaseStmt = CS->getCapturedStmt();
4514 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4515 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004516 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004517 return StmtError();
4518 // All associated statements must be '#pragma omp section' except for
4519 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004520 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004521 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4522 if (SectionStmt)
4523 Diag(SectionStmt->getLocStart(),
4524 diag::err_omp_parallel_sections_substmt_not_section);
4525 return StmtError();
4526 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004527 cast<OMPSectionDirective>(SectionStmt)
4528 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004529 }
4530 } else {
4531 Diag(AStmt->getLocStart(),
4532 diag::err_omp_parallel_sections_not_compound_stmt);
4533 return StmtError();
4534 }
4535
4536 getCurFunction()->setHasBranchProtectedScope();
4537
Alexey Bataev25e5b442015-09-15 12:52:43 +00004538 return OMPParallelSectionsDirective::Create(
4539 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004540}
4541
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004542StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4543 Stmt *AStmt, SourceLocation StartLoc,
4544 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004545 if (!AStmt)
4546 return StmtError();
4547
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004548 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4549 // 1.2.2 OpenMP Language Terminology
4550 // Structured block - An executable statement with a single entry at the
4551 // top and a single exit at the bottom.
4552 // The point of exit cannot be a branch out of the structured block.
4553 // longjmp() and throw() must not violate the entry/exit criteria.
4554 CS->getCapturedDecl()->setNothrow();
4555
4556 getCurFunction()->setHasBranchProtectedScope();
4557
Alexey Bataev25e5b442015-09-15 12:52:43 +00004558 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4559 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004560}
4561
Alexey Bataev68446b72014-07-18 07:47:19 +00004562StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4563 SourceLocation EndLoc) {
4564 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4565}
4566
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004567StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4568 SourceLocation EndLoc) {
4569 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4570}
4571
Alexey Bataev2df347a2014-07-18 10:17:07 +00004572StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4573 SourceLocation EndLoc) {
4574 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4575}
4576
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004577StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4578 SourceLocation StartLoc,
4579 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004580 if (!AStmt)
4581 return StmtError();
4582
4583 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004584
4585 getCurFunction()->setHasBranchProtectedScope();
4586
4587 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4588}
4589
Alexey Bataev6125da92014-07-21 11:26:11 +00004590StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4591 SourceLocation StartLoc,
4592 SourceLocation EndLoc) {
4593 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4594 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4595}
4596
Alexey Bataev346265e2015-09-25 10:37:12 +00004597StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4598 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004599 SourceLocation StartLoc,
4600 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004601 OMPClause *DependFound = nullptr;
4602 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004603 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004604 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004605 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004606 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004607 for (auto *C : Clauses) {
4608 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4609 DependFound = C;
4610 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4611 if (DependSourceClause) {
4612 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4613 << getOpenMPDirectiveName(OMPD_ordered)
4614 << getOpenMPClauseName(OMPC_depend) << 2;
4615 ErrorFound = true;
4616 } else
4617 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004618 if (DependSinkClause) {
4619 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4620 << 0;
4621 ErrorFound = true;
4622 }
4623 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4624 if (DependSourceClause) {
4625 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4626 << 1;
4627 ErrorFound = true;
4628 }
4629 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004630 }
4631 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004632 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004633 else if (C->getClauseKind() == OMPC_simd)
4634 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004635 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004636 if (!ErrorFound && !SC &&
4637 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004638 // OpenMP [2.8.1,simd Construct, Restrictions]
4639 // An ordered construct with the simd clause is the only OpenMP construct
4640 // that can appear in the simd region.
4641 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004642 ErrorFound = true;
4643 } else if (DependFound && (TC || SC)) {
4644 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4645 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4646 ErrorFound = true;
4647 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4648 Diag(DependFound->getLocStart(),
4649 diag::err_omp_ordered_directive_without_param);
4650 ErrorFound = true;
4651 } else if (TC || Clauses.empty()) {
4652 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4653 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4654 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4655 << (TC != nullptr);
4656 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4657 ErrorFound = true;
4658 }
4659 }
4660 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004661 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004662
4663 if (AStmt) {
4664 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4665
4666 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004667 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004668
4669 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004670}
4671
Alexey Bataev1d160b12015-03-13 12:27:31 +00004672namespace {
4673/// \brief Helper class for checking expression in 'omp atomic [update]'
4674/// construct.
4675class OpenMPAtomicUpdateChecker {
4676 /// \brief Error results for atomic update expressions.
4677 enum ExprAnalysisErrorCode {
4678 /// \brief A statement is not an expression statement.
4679 NotAnExpression,
4680 /// \brief Expression is not builtin binary or unary operation.
4681 NotABinaryOrUnaryExpression,
4682 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4683 NotAnUnaryIncDecExpression,
4684 /// \brief An expression is not of scalar type.
4685 NotAScalarType,
4686 /// \brief A binary operation is not an assignment operation.
4687 NotAnAssignmentOp,
4688 /// \brief RHS part of the binary operation is not a binary expression.
4689 NotABinaryExpression,
4690 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4691 /// expression.
4692 NotABinaryOperator,
4693 /// \brief RHS binary operation does not have reference to the updated LHS
4694 /// part.
4695 NotAnUpdateExpression,
4696 /// \brief No errors is found.
4697 NoError
4698 };
4699 /// \brief Reference to Sema.
4700 Sema &SemaRef;
4701 /// \brief A location for note diagnostics (when error is found).
4702 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004703 /// \brief 'x' lvalue part of the source atomic expression.
4704 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004705 /// \brief 'expr' rvalue part of the source atomic expression.
4706 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004707 /// \brief Helper expression of the form
4708 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4709 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4710 Expr *UpdateExpr;
4711 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4712 /// important for non-associative operations.
4713 bool IsXLHSInRHSPart;
4714 BinaryOperatorKind Op;
4715 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004716 /// \brief true if the source expression is a postfix unary operation, false
4717 /// if it is a prefix unary operation.
4718 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004719
4720public:
4721 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004722 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004723 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004724 /// \brief Check specified statement that it is suitable for 'atomic update'
4725 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004726 /// expression. If DiagId and NoteId == 0, then only check is performed
4727 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004728 /// \param DiagId Diagnostic which should be emitted if error is found.
4729 /// \param NoteId Diagnostic note for the main error message.
4730 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004731 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004732 /// \brief Return the 'x' lvalue part of the source atomic expression.
4733 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004734 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4735 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004736 /// \brief Return the update expression used in calculation of the updated
4737 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4738 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4739 Expr *getUpdateExpr() const { return UpdateExpr; }
4740 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4741 /// false otherwise.
4742 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4743
Alexey Bataevb78ca832015-04-01 03:33:17 +00004744 /// \brief true if the source expression is a postfix unary operation, false
4745 /// if it is a prefix unary operation.
4746 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4747
Alexey Bataev1d160b12015-03-13 12:27:31 +00004748private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004749 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4750 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004751};
4752} // namespace
4753
4754bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4755 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4756 ExprAnalysisErrorCode ErrorFound = NoError;
4757 SourceLocation ErrorLoc, NoteLoc;
4758 SourceRange ErrorRange, NoteRange;
4759 // Allowed constructs are:
4760 // x = x binop expr;
4761 // x = expr binop x;
4762 if (AtomicBinOp->getOpcode() == BO_Assign) {
4763 X = AtomicBinOp->getLHS();
4764 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4765 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4766 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4767 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4768 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004769 Op = AtomicInnerBinOp->getOpcode();
4770 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004771 auto *LHS = AtomicInnerBinOp->getLHS();
4772 auto *RHS = AtomicInnerBinOp->getRHS();
4773 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4774 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4775 /*Canonical=*/true);
4776 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4777 /*Canonical=*/true);
4778 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4779 /*Canonical=*/true);
4780 if (XId == LHSId) {
4781 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004782 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004783 } else if (XId == RHSId) {
4784 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004785 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004786 } else {
4787 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4788 ErrorRange = AtomicInnerBinOp->getSourceRange();
4789 NoteLoc = X->getExprLoc();
4790 NoteRange = X->getSourceRange();
4791 ErrorFound = NotAnUpdateExpression;
4792 }
4793 } else {
4794 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4795 ErrorRange = AtomicInnerBinOp->getSourceRange();
4796 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4797 NoteRange = SourceRange(NoteLoc, NoteLoc);
4798 ErrorFound = NotABinaryOperator;
4799 }
4800 } else {
4801 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4802 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4803 ErrorFound = NotABinaryExpression;
4804 }
4805 } else {
4806 ErrorLoc = AtomicBinOp->getExprLoc();
4807 ErrorRange = AtomicBinOp->getSourceRange();
4808 NoteLoc = AtomicBinOp->getOperatorLoc();
4809 NoteRange = SourceRange(NoteLoc, NoteLoc);
4810 ErrorFound = NotAnAssignmentOp;
4811 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004812 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004813 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4814 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4815 return true;
4816 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004817 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004818 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004819}
4820
4821bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4822 unsigned NoteId) {
4823 ExprAnalysisErrorCode ErrorFound = NoError;
4824 SourceLocation ErrorLoc, NoteLoc;
4825 SourceRange ErrorRange, NoteRange;
4826 // Allowed constructs are:
4827 // x++;
4828 // x--;
4829 // ++x;
4830 // --x;
4831 // x binop= expr;
4832 // x = x binop expr;
4833 // x = expr binop x;
4834 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4835 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4836 if (AtomicBody->getType()->isScalarType() ||
4837 AtomicBody->isInstantiationDependent()) {
4838 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4839 AtomicBody->IgnoreParenImpCasts())) {
4840 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004841 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004842 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004843 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004844 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004845 X = AtomicCompAssignOp->getLHS();
4846 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004847 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4848 AtomicBody->IgnoreParenImpCasts())) {
4849 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004850 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4851 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004852 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004853 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4854 // Check for Unary Operation
4855 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004856 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004857 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4858 OpLoc = AtomicUnaryOp->getOperatorLoc();
4859 X = AtomicUnaryOp->getSubExpr();
4860 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4861 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004862 } else {
4863 ErrorFound = NotAnUnaryIncDecExpression;
4864 ErrorLoc = AtomicUnaryOp->getExprLoc();
4865 ErrorRange = AtomicUnaryOp->getSourceRange();
4866 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4867 NoteRange = SourceRange(NoteLoc, NoteLoc);
4868 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004869 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004870 ErrorFound = NotABinaryOrUnaryExpression;
4871 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4872 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4873 }
4874 } else {
4875 ErrorFound = NotAScalarType;
4876 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4877 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4878 }
4879 } else {
4880 ErrorFound = NotAnExpression;
4881 NoteLoc = ErrorLoc = S->getLocStart();
4882 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4883 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004884 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004885 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4886 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4887 return true;
4888 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004889 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004890 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004891 // Build an update expression of form 'OpaqueValueExpr(x) binop
4892 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4893 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4894 auto *OVEX = new (SemaRef.getASTContext())
4895 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4896 auto *OVEExpr = new (SemaRef.getASTContext())
4897 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4898 auto Update =
4899 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4900 IsXLHSInRHSPart ? OVEExpr : OVEX);
4901 if (Update.isInvalid())
4902 return true;
4903 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4904 Sema::AA_Casting);
4905 if (Update.isInvalid())
4906 return true;
4907 UpdateExpr = Update.get();
4908 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004909 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004910}
4911
Alexey Bataev0162e452014-07-22 10:10:35 +00004912StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4913 Stmt *AStmt,
4914 SourceLocation StartLoc,
4915 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004916 if (!AStmt)
4917 return StmtError();
4918
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004919 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004920 // 1.2.2 OpenMP Language Terminology
4921 // Structured block - An executable statement with a single entry at the
4922 // top and a single exit at the bottom.
4923 // The point of exit cannot be a branch out of the structured block.
4924 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004925 OpenMPClauseKind AtomicKind = OMPC_unknown;
4926 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004927 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004928 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004929 C->getClauseKind() == OMPC_update ||
4930 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004931 if (AtomicKind != OMPC_unknown) {
4932 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4933 << SourceRange(C->getLocStart(), C->getLocEnd());
4934 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4935 << getOpenMPClauseName(AtomicKind);
4936 } else {
4937 AtomicKind = C->getClauseKind();
4938 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004939 }
4940 }
4941 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004942
Alexey Bataev459dec02014-07-24 06:46:57 +00004943 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004944 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4945 Body = EWC->getSubExpr();
4946
Alexey Bataev62cec442014-11-18 10:14:22 +00004947 Expr *X = nullptr;
4948 Expr *V = nullptr;
4949 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004950 Expr *UE = nullptr;
4951 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004952 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004953 // OpenMP [2.12.6, atomic Construct]
4954 // In the next expressions:
4955 // * x and v (as applicable) are both l-value expressions with scalar type.
4956 // * During the execution of an atomic region, multiple syntactic
4957 // occurrences of x must designate the same storage location.
4958 // * Neither of v and expr (as applicable) may access the storage location
4959 // designated by x.
4960 // * Neither of x and expr (as applicable) may access the storage location
4961 // designated by v.
4962 // * expr is an expression with scalar type.
4963 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4964 // * binop, binop=, ++, and -- are not overloaded operators.
4965 // * The expression x binop expr must be numerically equivalent to x binop
4966 // (expr). This requirement is satisfied if the operators in expr have
4967 // precedence greater than binop, or by using parentheses around expr or
4968 // subexpressions of expr.
4969 // * The expression expr binop x must be numerically equivalent to (expr)
4970 // binop x. This requirement is satisfied if the operators in expr have
4971 // precedence equal to or greater than binop, or by using parentheses around
4972 // expr or subexpressions of expr.
4973 // * For forms that allow multiple occurrences of x, the number of times
4974 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004975 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004976 enum {
4977 NotAnExpression,
4978 NotAnAssignmentOp,
4979 NotAScalarType,
4980 NotAnLValue,
4981 NoError
4982 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004983 SourceLocation ErrorLoc, NoteLoc;
4984 SourceRange ErrorRange, NoteRange;
4985 // If clause is read:
4986 // v = x;
4987 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4988 auto AtomicBinOp =
4989 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4990 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4991 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4992 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4993 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4994 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4995 if (!X->isLValue() || !V->isLValue()) {
4996 auto NotLValueExpr = X->isLValue() ? V : X;
4997 ErrorFound = NotAnLValue;
4998 ErrorLoc = AtomicBinOp->getExprLoc();
4999 ErrorRange = AtomicBinOp->getSourceRange();
5000 NoteLoc = NotLValueExpr->getExprLoc();
5001 NoteRange = NotLValueExpr->getSourceRange();
5002 }
5003 } else if (!X->isInstantiationDependent() ||
5004 !V->isInstantiationDependent()) {
5005 auto NotScalarExpr =
5006 (X->isInstantiationDependent() || X->getType()->isScalarType())
5007 ? V
5008 : X;
5009 ErrorFound = NotAScalarType;
5010 ErrorLoc = AtomicBinOp->getExprLoc();
5011 ErrorRange = AtomicBinOp->getSourceRange();
5012 NoteLoc = NotScalarExpr->getExprLoc();
5013 NoteRange = NotScalarExpr->getSourceRange();
5014 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005015 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005016 ErrorFound = NotAnAssignmentOp;
5017 ErrorLoc = AtomicBody->getExprLoc();
5018 ErrorRange = AtomicBody->getSourceRange();
5019 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5020 : AtomicBody->getExprLoc();
5021 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5022 : AtomicBody->getSourceRange();
5023 }
5024 } else {
5025 ErrorFound = NotAnExpression;
5026 NoteLoc = ErrorLoc = Body->getLocStart();
5027 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005028 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005029 if (ErrorFound != NoError) {
5030 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5031 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005032 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5033 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005034 return StmtError();
5035 } else if (CurContext->isDependentContext())
5036 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005037 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005038 enum {
5039 NotAnExpression,
5040 NotAnAssignmentOp,
5041 NotAScalarType,
5042 NotAnLValue,
5043 NoError
5044 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005045 SourceLocation ErrorLoc, NoteLoc;
5046 SourceRange ErrorRange, NoteRange;
5047 // If clause is write:
5048 // x = expr;
5049 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5050 auto AtomicBinOp =
5051 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5052 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005053 X = AtomicBinOp->getLHS();
5054 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005055 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5056 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5057 if (!X->isLValue()) {
5058 ErrorFound = NotAnLValue;
5059 ErrorLoc = AtomicBinOp->getExprLoc();
5060 ErrorRange = AtomicBinOp->getSourceRange();
5061 NoteLoc = X->getExprLoc();
5062 NoteRange = X->getSourceRange();
5063 }
5064 } else if (!X->isInstantiationDependent() ||
5065 !E->isInstantiationDependent()) {
5066 auto NotScalarExpr =
5067 (X->isInstantiationDependent() || X->getType()->isScalarType())
5068 ? E
5069 : X;
5070 ErrorFound = NotAScalarType;
5071 ErrorLoc = AtomicBinOp->getExprLoc();
5072 ErrorRange = AtomicBinOp->getSourceRange();
5073 NoteLoc = NotScalarExpr->getExprLoc();
5074 NoteRange = NotScalarExpr->getSourceRange();
5075 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005076 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005077 ErrorFound = NotAnAssignmentOp;
5078 ErrorLoc = AtomicBody->getExprLoc();
5079 ErrorRange = AtomicBody->getSourceRange();
5080 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5081 : AtomicBody->getExprLoc();
5082 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5083 : AtomicBody->getSourceRange();
5084 }
5085 } else {
5086 ErrorFound = NotAnExpression;
5087 NoteLoc = ErrorLoc = Body->getLocStart();
5088 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005089 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005090 if (ErrorFound != NoError) {
5091 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5092 << ErrorRange;
5093 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5094 << NoteRange;
5095 return StmtError();
5096 } else if (CurContext->isDependentContext())
5097 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005098 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005099 // If clause is update:
5100 // x++;
5101 // x--;
5102 // ++x;
5103 // --x;
5104 // x binop= expr;
5105 // x = x binop expr;
5106 // x = expr binop x;
5107 OpenMPAtomicUpdateChecker Checker(*this);
5108 if (Checker.checkStatement(
5109 Body, (AtomicKind == OMPC_update)
5110 ? diag::err_omp_atomic_update_not_expression_statement
5111 : diag::err_omp_atomic_not_expression_statement,
5112 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005113 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005114 if (!CurContext->isDependentContext()) {
5115 E = Checker.getExpr();
5116 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005117 UE = Checker.getUpdateExpr();
5118 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005119 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005120 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005121 enum {
5122 NotAnAssignmentOp,
5123 NotACompoundStatement,
5124 NotTwoSubstatements,
5125 NotASpecificExpression,
5126 NoError
5127 } ErrorFound = NoError;
5128 SourceLocation ErrorLoc, NoteLoc;
5129 SourceRange ErrorRange, NoteRange;
5130 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5131 // If clause is a capture:
5132 // v = x++;
5133 // v = x--;
5134 // v = ++x;
5135 // v = --x;
5136 // v = x binop= expr;
5137 // v = x = x binop expr;
5138 // v = x = expr binop x;
5139 auto *AtomicBinOp =
5140 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5141 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5142 V = AtomicBinOp->getLHS();
5143 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5144 OpenMPAtomicUpdateChecker Checker(*this);
5145 if (Checker.checkStatement(
5146 Body, diag::err_omp_atomic_capture_not_expression_statement,
5147 diag::note_omp_atomic_update))
5148 return StmtError();
5149 E = Checker.getExpr();
5150 X = Checker.getX();
5151 UE = Checker.getUpdateExpr();
5152 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5153 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005154 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005155 ErrorLoc = AtomicBody->getExprLoc();
5156 ErrorRange = AtomicBody->getSourceRange();
5157 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5158 : AtomicBody->getExprLoc();
5159 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5160 : AtomicBody->getSourceRange();
5161 ErrorFound = NotAnAssignmentOp;
5162 }
5163 if (ErrorFound != NoError) {
5164 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5165 << ErrorRange;
5166 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5167 return StmtError();
5168 } else if (CurContext->isDependentContext()) {
5169 UE = V = E = X = nullptr;
5170 }
5171 } else {
5172 // If clause is a capture:
5173 // { v = x; x = expr; }
5174 // { v = x; x++; }
5175 // { v = x; x--; }
5176 // { v = x; ++x; }
5177 // { v = x; --x; }
5178 // { v = x; x binop= expr; }
5179 // { v = x; x = x binop expr; }
5180 // { v = x; x = expr binop x; }
5181 // { x++; v = x; }
5182 // { x--; v = x; }
5183 // { ++x; v = x; }
5184 // { --x; v = x; }
5185 // { x binop= expr; v = x; }
5186 // { x = x binop expr; v = x; }
5187 // { x = expr binop x; v = x; }
5188 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5189 // Check that this is { expr1; expr2; }
5190 if (CS->size() == 2) {
5191 auto *First = CS->body_front();
5192 auto *Second = CS->body_back();
5193 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5194 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5195 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5196 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5197 // Need to find what subexpression is 'v' and what is 'x'.
5198 OpenMPAtomicUpdateChecker Checker(*this);
5199 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5200 BinaryOperator *BinOp = nullptr;
5201 if (IsUpdateExprFound) {
5202 BinOp = dyn_cast<BinaryOperator>(First);
5203 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5204 }
5205 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5206 // { v = x; x++; }
5207 // { v = x; x--; }
5208 // { v = x; ++x; }
5209 // { v = x; --x; }
5210 // { v = x; x binop= expr; }
5211 // { v = x; x = x binop expr; }
5212 // { v = x; x = expr binop x; }
5213 // Check that the first expression has form v = x.
5214 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5215 llvm::FoldingSetNodeID XId, PossibleXId;
5216 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5217 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5218 IsUpdateExprFound = XId == PossibleXId;
5219 if (IsUpdateExprFound) {
5220 V = BinOp->getLHS();
5221 X = Checker.getX();
5222 E = Checker.getExpr();
5223 UE = Checker.getUpdateExpr();
5224 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005225 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005226 }
5227 }
5228 if (!IsUpdateExprFound) {
5229 IsUpdateExprFound = !Checker.checkStatement(First);
5230 BinOp = nullptr;
5231 if (IsUpdateExprFound) {
5232 BinOp = dyn_cast<BinaryOperator>(Second);
5233 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5234 }
5235 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5236 // { x++; v = x; }
5237 // { x--; v = x; }
5238 // { ++x; v = x; }
5239 // { --x; v = x; }
5240 // { x binop= expr; v = x; }
5241 // { x = x binop expr; v = x; }
5242 // { x = expr binop x; v = x; }
5243 // Check that the second expression has form v = x.
5244 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5245 llvm::FoldingSetNodeID XId, PossibleXId;
5246 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5247 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5248 IsUpdateExprFound = XId == PossibleXId;
5249 if (IsUpdateExprFound) {
5250 V = BinOp->getLHS();
5251 X = Checker.getX();
5252 E = Checker.getExpr();
5253 UE = Checker.getUpdateExpr();
5254 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005255 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005256 }
5257 }
5258 }
5259 if (!IsUpdateExprFound) {
5260 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005261 auto *FirstExpr = dyn_cast<Expr>(First);
5262 auto *SecondExpr = dyn_cast<Expr>(Second);
5263 if (!FirstExpr || !SecondExpr ||
5264 !(FirstExpr->isInstantiationDependent() ||
5265 SecondExpr->isInstantiationDependent())) {
5266 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5267 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005268 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005269 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5270 : First->getLocStart();
5271 NoteRange = ErrorRange = FirstBinOp
5272 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005273 : SourceRange(ErrorLoc, ErrorLoc);
5274 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005275 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5276 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5277 ErrorFound = NotAnAssignmentOp;
5278 NoteLoc = ErrorLoc = SecondBinOp
5279 ? SecondBinOp->getOperatorLoc()
5280 : Second->getLocStart();
5281 NoteRange = ErrorRange =
5282 SecondBinOp ? SecondBinOp->getSourceRange()
5283 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005284 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005285 auto *PossibleXRHSInFirst =
5286 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5287 auto *PossibleXLHSInSecond =
5288 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5289 llvm::FoldingSetNodeID X1Id, X2Id;
5290 PossibleXRHSInFirst->Profile(X1Id, Context,
5291 /*Canonical=*/true);
5292 PossibleXLHSInSecond->Profile(X2Id, Context,
5293 /*Canonical=*/true);
5294 IsUpdateExprFound = X1Id == X2Id;
5295 if (IsUpdateExprFound) {
5296 V = FirstBinOp->getLHS();
5297 X = SecondBinOp->getLHS();
5298 E = SecondBinOp->getRHS();
5299 UE = nullptr;
5300 IsXLHSInRHSPart = false;
5301 IsPostfixUpdate = true;
5302 } else {
5303 ErrorFound = NotASpecificExpression;
5304 ErrorLoc = FirstBinOp->getExprLoc();
5305 ErrorRange = FirstBinOp->getSourceRange();
5306 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5307 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5308 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005309 }
5310 }
5311 }
5312 }
5313 } else {
5314 NoteLoc = ErrorLoc = Body->getLocStart();
5315 NoteRange = ErrorRange =
5316 SourceRange(Body->getLocStart(), Body->getLocStart());
5317 ErrorFound = NotTwoSubstatements;
5318 }
5319 } else {
5320 NoteLoc = ErrorLoc = Body->getLocStart();
5321 NoteRange = ErrorRange =
5322 SourceRange(Body->getLocStart(), Body->getLocStart());
5323 ErrorFound = NotACompoundStatement;
5324 }
5325 if (ErrorFound != NoError) {
5326 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5327 << ErrorRange;
5328 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5329 return StmtError();
5330 } else if (CurContext->isDependentContext()) {
5331 UE = V = E = X = nullptr;
5332 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005333 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005334 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005335
5336 getCurFunction()->setHasBranchProtectedScope();
5337
Alexey Bataev62cec442014-11-18 10:14:22 +00005338 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005339 X, V, E, UE, IsXLHSInRHSPart,
5340 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005341}
5342
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005343StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5344 Stmt *AStmt,
5345 SourceLocation StartLoc,
5346 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005347 if (!AStmt)
5348 return StmtError();
5349
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005350 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5351 // 1.2.2 OpenMP Language Terminology
5352 // Structured block - An executable statement with a single entry at the
5353 // top and a single exit at the bottom.
5354 // The point of exit cannot be a branch out of the structured block.
5355 // longjmp() and throw() must not violate the entry/exit criteria.
5356 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005357
Alexey Bataev13314bf2014-10-09 04:18:56 +00005358 // OpenMP [2.16, Nesting of Regions]
5359 // If specified, a teams construct must be contained within a target
5360 // construct. That target construct must contain no statements or directives
5361 // outside of the teams construct.
5362 if (DSAStack->hasInnerTeamsRegion()) {
5363 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5364 bool OMPTeamsFound = true;
5365 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5366 auto I = CS->body_begin();
5367 while (I != CS->body_end()) {
5368 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5369 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5370 OMPTeamsFound = false;
5371 break;
5372 }
5373 ++I;
5374 }
5375 assert(I != CS->body_end() && "Not found statement");
5376 S = *I;
5377 }
5378 if (!OMPTeamsFound) {
5379 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5380 Diag(DSAStack->getInnerTeamsRegionLoc(),
5381 diag::note_omp_nested_teams_construct_here);
5382 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5383 << isa<OMPExecutableDirective>(S);
5384 return StmtError();
5385 }
5386 }
5387
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005388 getCurFunction()->setHasBranchProtectedScope();
5389
5390 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5391}
5392
Michael Wong65f367f2015-07-21 13:44:28 +00005393StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5394 Stmt *AStmt,
5395 SourceLocation StartLoc,
5396 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005397 if (!AStmt)
5398 return StmtError();
5399
5400 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5401
Michael Wong65f367f2015-07-21 13:44:28 +00005402 getCurFunction()->setHasBranchProtectedScope();
5403
5404 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5405 AStmt);
5406}
5407
Alexey Bataev13314bf2014-10-09 04:18:56 +00005408StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5409 Stmt *AStmt, SourceLocation StartLoc,
5410 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005411 if (!AStmt)
5412 return StmtError();
5413
Alexey Bataev13314bf2014-10-09 04:18:56 +00005414 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5415 // 1.2.2 OpenMP Language Terminology
5416 // Structured block - An executable statement with a single entry at the
5417 // top and a single exit at the bottom.
5418 // The point of exit cannot be a branch out of the structured block.
5419 // longjmp() and throw() must not violate the entry/exit criteria.
5420 CS->getCapturedDecl()->setNothrow();
5421
5422 getCurFunction()->setHasBranchProtectedScope();
5423
5424 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5425}
5426
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005427StmtResult
5428Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5429 SourceLocation EndLoc,
5430 OpenMPDirectiveKind CancelRegion) {
5431 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5432 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5433 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5434 << getOpenMPDirectiveName(CancelRegion);
5435 return StmtError();
5436 }
5437 if (DSAStack->isParentNowaitRegion()) {
5438 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5439 return StmtError();
5440 }
5441 if (DSAStack->isParentOrderedRegion()) {
5442 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5443 return StmtError();
5444 }
5445 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5446 CancelRegion);
5447}
5448
Alexey Bataev87933c72015-09-18 08:07:34 +00005449StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5450 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005451 SourceLocation EndLoc,
5452 OpenMPDirectiveKind CancelRegion) {
5453 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5454 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5455 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5456 << getOpenMPDirectiveName(CancelRegion);
5457 return StmtError();
5458 }
5459 if (DSAStack->isParentNowaitRegion()) {
5460 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5461 return StmtError();
5462 }
5463 if (DSAStack->isParentOrderedRegion()) {
5464 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5465 return StmtError();
5466 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005467 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005468 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5469 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005470}
5471
Alexey Bataev382967a2015-12-08 12:06:20 +00005472static bool checkGrainsizeNumTasksClauses(Sema &S,
5473 ArrayRef<OMPClause *> Clauses) {
5474 OMPClause *PrevClause = nullptr;
5475 bool ErrorFound = false;
5476 for (auto *C : Clauses) {
5477 if (C->getClauseKind() == OMPC_grainsize ||
5478 C->getClauseKind() == OMPC_num_tasks) {
5479 if (!PrevClause)
5480 PrevClause = C;
5481 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5482 S.Diag(C->getLocStart(),
5483 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5484 << getOpenMPClauseName(C->getClauseKind())
5485 << getOpenMPClauseName(PrevClause->getClauseKind());
5486 S.Diag(PrevClause->getLocStart(),
5487 diag::note_omp_previous_grainsize_num_tasks)
5488 << getOpenMPClauseName(PrevClause->getClauseKind());
5489 ErrorFound = true;
5490 }
5491 }
5492 }
5493 return ErrorFound;
5494}
5495
Alexey Bataev49f6e782015-12-01 04:18:41 +00005496StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5497 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5498 SourceLocation EndLoc,
5499 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5500 if (!AStmt)
5501 return StmtError();
5502
5503 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5504 OMPLoopDirective::HelperExprs B;
5505 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5506 // define the nested loops number.
5507 unsigned NestedLoopCount =
5508 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005509 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005510 VarsWithImplicitDSA, B);
5511 if (NestedLoopCount == 0)
5512 return StmtError();
5513
5514 assert((CurContext->isDependentContext() || B.builtAll()) &&
5515 "omp for loop exprs were not built");
5516
Alexey Bataev382967a2015-12-08 12:06:20 +00005517 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5518 // The grainsize clause and num_tasks clause are mutually exclusive and may
5519 // not appear on the same taskloop directive.
5520 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5521 return StmtError();
5522
Alexey Bataev49f6e782015-12-01 04:18:41 +00005523 getCurFunction()->setHasBranchProtectedScope();
5524 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5525 NestedLoopCount, Clauses, AStmt, B);
5526}
5527
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005528StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5529 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5530 SourceLocation EndLoc,
5531 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5532 if (!AStmt)
5533 return StmtError();
5534
5535 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5536 OMPLoopDirective::HelperExprs B;
5537 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5538 // define the nested loops number.
5539 unsigned NestedLoopCount =
5540 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5541 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5542 VarsWithImplicitDSA, B);
5543 if (NestedLoopCount == 0)
5544 return StmtError();
5545
5546 assert((CurContext->isDependentContext() || B.builtAll()) &&
5547 "omp for loop exprs were not built");
5548
Alexey Bataev382967a2015-12-08 12:06:20 +00005549 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5550 // The grainsize clause and num_tasks clause are mutually exclusive and may
5551 // not appear on the same taskloop directive.
5552 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5553 return StmtError();
5554
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005555 getCurFunction()->setHasBranchProtectedScope();
5556 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5557 NestedLoopCount, Clauses, AStmt, B);
5558}
5559
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005560StmtResult Sema::ActOnOpenMPDistributeDirective(
5561 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5562 SourceLocation EndLoc,
5563 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5564 if (!AStmt)
5565 return StmtError();
5566
5567 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5568 OMPLoopDirective::HelperExprs B;
5569 // In presence of clause 'collapse' with number of loops, it will
5570 // define the nested loops number.
5571 unsigned NestedLoopCount =
5572 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5573 nullptr /*ordered not a clause on distribute*/, AStmt,
5574 *this, *DSAStack, VarsWithImplicitDSA, B);
5575 if (NestedLoopCount == 0)
5576 return StmtError();
5577
5578 assert((CurContext->isDependentContext() || B.builtAll()) &&
5579 "omp for loop exprs were not built");
5580
5581 getCurFunction()->setHasBranchProtectedScope();
5582 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5583 NestedLoopCount, Clauses, AStmt, B);
5584}
5585
Alexey Bataeved09d242014-05-28 05:53:51 +00005586OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005587 SourceLocation StartLoc,
5588 SourceLocation LParenLoc,
5589 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005590 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005591 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005592 case OMPC_final:
5593 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5594 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005595 case OMPC_num_threads:
5596 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5597 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005598 case OMPC_safelen:
5599 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5600 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005601 case OMPC_simdlen:
5602 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5603 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005604 case OMPC_collapse:
5605 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5606 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005607 case OMPC_ordered:
5608 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5609 break;
Michael Wonge710d542015-08-07 16:16:36 +00005610 case OMPC_device:
5611 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5612 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005613 case OMPC_num_teams:
5614 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5615 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005616 case OMPC_thread_limit:
5617 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5618 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005619 case OMPC_priority:
5620 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5621 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005622 case OMPC_grainsize:
5623 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5624 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005625 case OMPC_num_tasks:
5626 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5627 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005628 case OMPC_hint:
5629 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5630 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005631 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005632 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005633 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005634 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005635 case OMPC_private:
5636 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005637 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005638 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005639 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005640 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005641 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005642 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005643 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005644 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005645 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005646 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005647 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005648 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005649 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005650 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005651 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005652 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005653 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005654 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005655 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005656 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005657 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005658 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005659 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005660 llvm_unreachable("Clause is not allowed.");
5661 }
5662 return Res;
5663}
5664
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005665OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5666 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005667 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005668 SourceLocation NameModifierLoc,
5669 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005670 SourceLocation EndLoc) {
5671 Expr *ValExpr = Condition;
5672 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5673 !Condition->isInstantiationDependent() &&
5674 !Condition->containsUnexpandedParameterPack()) {
5675 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005676 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005677 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005678 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005679
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005680 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005681 }
5682
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005683 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5684 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005685}
5686
Alexey Bataev3778b602014-07-17 07:32:53 +00005687OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5688 SourceLocation StartLoc,
5689 SourceLocation LParenLoc,
5690 SourceLocation EndLoc) {
5691 Expr *ValExpr = Condition;
5692 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5693 !Condition->isInstantiationDependent() &&
5694 !Condition->containsUnexpandedParameterPack()) {
5695 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5696 Condition->getExprLoc(), Condition);
5697 if (Val.isInvalid())
5698 return nullptr;
5699
5700 ValExpr = Val.get();
5701 }
5702
5703 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5704}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005705ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5706 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005707 if (!Op)
5708 return ExprError();
5709
5710 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5711 public:
5712 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005713 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005714 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5715 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005716 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5717 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005718 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5719 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005720 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5721 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005722 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5723 QualType T,
5724 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005725 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5726 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005727 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5728 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005729 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005730 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005731 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005732 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5733 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005734 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5735 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005736 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5737 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005738 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005739 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005740 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005741 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5742 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005743 llvm_unreachable("conversion functions are permitted");
5744 }
5745 } ConvertDiagnoser;
5746 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5747}
5748
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005749static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005750 OpenMPClauseKind CKind,
5751 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005752 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5753 !ValExpr->isInstantiationDependent()) {
5754 SourceLocation Loc = ValExpr->getExprLoc();
5755 ExprResult Value =
5756 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5757 if (Value.isInvalid())
5758 return false;
5759
5760 ValExpr = Value.get();
5761 // The expression must evaluate to a non-negative integer value.
5762 llvm::APSInt Result;
5763 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005764 Result.isSigned() &&
5765 !((!StrictlyPositive && Result.isNonNegative()) ||
5766 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005767 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005768 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5769 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005770 return false;
5771 }
5772 }
5773 return true;
5774}
5775
Alexey Bataev568a8332014-03-06 06:15:19 +00005776OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5777 SourceLocation StartLoc,
5778 SourceLocation LParenLoc,
5779 SourceLocation EndLoc) {
5780 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005781
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005782 // OpenMP [2.5, Restrictions]
5783 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005784 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5785 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005786 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005787
Alexey Bataeved09d242014-05-28 05:53:51 +00005788 return new (Context)
5789 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005790}
5791
Alexey Bataev62c87d22014-03-21 04:51:18 +00005792ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005793 OpenMPClauseKind CKind,
5794 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005795 if (!E)
5796 return ExprError();
5797 if (E->isValueDependent() || E->isTypeDependent() ||
5798 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005799 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005800 llvm::APSInt Result;
5801 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5802 if (ICE.isInvalid())
5803 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005804 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
5805 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00005806 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005807 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5808 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005809 return ExprError();
5810 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005811 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5812 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5813 << E->getSourceRange();
5814 return ExprError();
5815 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005816 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
5817 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005818 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005819 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005820 return ICE;
5821}
5822
5823OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5824 SourceLocation LParenLoc,
5825 SourceLocation EndLoc) {
5826 // OpenMP [2.8.1, simd construct, Description]
5827 // The parameter of the safelen clause must be a constant
5828 // positive integer expression.
5829 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5830 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005831 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005832 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005833 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005834}
5835
Alexey Bataev66b15b52015-08-21 11:14:16 +00005836OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5837 SourceLocation LParenLoc,
5838 SourceLocation EndLoc) {
5839 // OpenMP [2.8.1, simd construct, Description]
5840 // The parameter of the simdlen clause must be a constant
5841 // positive integer expression.
5842 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5843 if (Simdlen.isInvalid())
5844 return nullptr;
5845 return new (Context)
5846 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5847}
5848
Alexander Musman64d33f12014-06-04 07:53:32 +00005849OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5850 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005851 SourceLocation LParenLoc,
5852 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005853 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005854 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005855 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005856 // The parameter of the collapse clause must be a constant
5857 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005858 ExprResult NumForLoopsResult =
5859 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5860 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005861 return nullptr;
5862 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005863 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005864}
5865
Alexey Bataev10e775f2015-07-30 11:36:16 +00005866OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5867 SourceLocation EndLoc,
5868 SourceLocation LParenLoc,
5869 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005870 // OpenMP [2.7.1, loop construct, Description]
5871 // OpenMP [2.8.1, simd construct, Description]
5872 // OpenMP [2.9.6, distribute construct, Description]
5873 // The parameter of the ordered clause must be a constant
5874 // positive integer expression if any.
5875 if (NumForLoops && LParenLoc.isValid()) {
5876 ExprResult NumForLoopsResult =
5877 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5878 if (NumForLoopsResult.isInvalid())
5879 return nullptr;
5880 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005881 } else
5882 NumForLoops = nullptr;
5883 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005884 return new (Context)
5885 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5886}
5887
Alexey Bataeved09d242014-05-28 05:53:51 +00005888OMPClause *Sema::ActOnOpenMPSimpleClause(
5889 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5890 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005891 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005892 switch (Kind) {
5893 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005894 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005895 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5896 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005897 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005898 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005899 Res = ActOnOpenMPProcBindClause(
5900 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5901 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005902 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005903 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005904 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005905 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005906 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005907 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005908 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005909 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005910 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005911 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005912 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005913 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005914 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005915 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005916 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005917 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005918 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005919 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005920 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005921 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005922 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005923 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005924 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005925 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005926 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005927 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005928 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005929 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005930 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005931 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005932 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005933 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005934 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005935 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005936 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005937 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005938 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005939 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005940 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00005941 case OMPC_hint:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005942 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005943 llvm_unreachable("Clause is not allowed.");
5944 }
5945 return Res;
5946}
5947
5948OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5949 SourceLocation KindKwLoc,
5950 SourceLocation StartLoc,
5951 SourceLocation LParenLoc,
5952 SourceLocation EndLoc) {
5953 if (Kind == OMPC_DEFAULT_unknown) {
5954 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005955 static_assert(OMPC_DEFAULT_unknown > 0,
5956 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005957 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005958 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005959 Values += "'";
5960 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5961 Values += "'";
5962 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005963 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005964 Values += " or ";
5965 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005966 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005967 break;
5968 default:
5969 Values += Sep;
5970 break;
5971 }
5972 }
5973 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005974 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005975 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005976 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005977 switch (Kind) {
5978 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005979 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005980 break;
5981 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005982 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005983 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005984 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005985 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005986 break;
5987 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005988 return new (Context)
5989 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005990}
5991
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005992OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5993 SourceLocation KindKwLoc,
5994 SourceLocation StartLoc,
5995 SourceLocation LParenLoc,
5996 SourceLocation EndLoc) {
5997 if (Kind == OMPC_PROC_BIND_unknown) {
5998 std::string Values;
5999 std::string Sep(", ");
6000 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
6001 Values += "'";
6002 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
6003 Values += "'";
6004 switch (i) {
6005 case OMPC_PROC_BIND_unknown - 2:
6006 Values += " or ";
6007 break;
6008 case OMPC_PROC_BIND_unknown - 1:
6009 break;
6010 default:
6011 Values += Sep;
6012 break;
6013 }
6014 }
6015 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00006016 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006017 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006018 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006019 return new (Context)
6020 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006021}
6022
Alexey Bataev56dafe82014-06-20 07:16:17 +00006023OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
6024 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
6025 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006026 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006027 SourceLocation EndLoc) {
6028 OMPClause *Res = nullptr;
6029 switch (Kind) {
6030 case OMPC_schedule:
6031 Res = ActOnOpenMPScheduleClause(
6032 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006033 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006034 break;
6035 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006036 Res =
6037 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
6038 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
6039 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006040 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006041 case OMPC_num_threads:
6042 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006043 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006044 case OMPC_collapse:
6045 case OMPC_default:
6046 case OMPC_proc_bind:
6047 case OMPC_private:
6048 case OMPC_firstprivate:
6049 case OMPC_lastprivate:
6050 case OMPC_shared:
6051 case OMPC_reduction:
6052 case OMPC_linear:
6053 case OMPC_aligned:
6054 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006055 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006056 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006057 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006058 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006059 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006060 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006061 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006062 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006063 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006064 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006065 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006066 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006067 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006068 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006069 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006070 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006071 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006072 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006073 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006074 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006075 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006076 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006077 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006078 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006079 case OMPC_unknown:
6080 llvm_unreachable("Clause is not allowed.");
6081 }
6082 return Res;
6083}
6084
6085OMPClause *Sema::ActOnOpenMPScheduleClause(
6086 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
6087 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
6088 SourceLocation EndLoc) {
6089 if (Kind == OMPC_SCHEDULE_unknown) {
6090 std::string Values;
6091 std::string Sep(", ");
6092 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
6093 Values += "'";
6094 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
6095 Values += "'";
6096 switch (i) {
6097 case OMPC_SCHEDULE_unknown - 2:
6098 Values += " or ";
6099 break;
6100 case OMPC_SCHEDULE_unknown - 1:
6101 break;
6102 default:
6103 Values += Sep;
6104 break;
6105 }
6106 }
6107 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6108 << Values << getOpenMPClauseName(OMPC_schedule);
6109 return nullptr;
6110 }
6111 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006112 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006113 if (ChunkSize) {
6114 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6115 !ChunkSize->isInstantiationDependent() &&
6116 !ChunkSize->containsUnexpandedParameterPack()) {
6117 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6118 ExprResult Val =
6119 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6120 if (Val.isInvalid())
6121 return nullptr;
6122
6123 ValExpr = Val.get();
6124
6125 // OpenMP [2.7.1, Restrictions]
6126 // chunk_size must be a loop invariant integer expression with a positive
6127 // value.
6128 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006129 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6130 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6131 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006132 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006133 return nullptr;
6134 }
6135 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6136 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6137 ChunkSize->getType(), ".chunk.");
6138 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6139 ChunkSize->getExprLoc(),
6140 /*RefersToCapture=*/true);
6141 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006142 }
6143 }
6144 }
6145
6146 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00006147 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006148}
6149
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006150OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6151 SourceLocation StartLoc,
6152 SourceLocation EndLoc) {
6153 OMPClause *Res = nullptr;
6154 switch (Kind) {
6155 case OMPC_ordered:
6156 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6157 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006158 case OMPC_nowait:
6159 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6160 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006161 case OMPC_untied:
6162 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6163 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006164 case OMPC_mergeable:
6165 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6166 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006167 case OMPC_read:
6168 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6169 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006170 case OMPC_write:
6171 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6172 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006173 case OMPC_update:
6174 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6175 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006176 case OMPC_capture:
6177 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6178 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006179 case OMPC_seq_cst:
6180 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6181 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006182 case OMPC_threads:
6183 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6184 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006185 case OMPC_simd:
6186 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6187 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006188 case OMPC_nogroup:
6189 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6190 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006191 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006192 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006193 case OMPC_num_threads:
6194 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006195 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006196 case OMPC_collapse:
6197 case OMPC_schedule:
6198 case OMPC_private:
6199 case OMPC_firstprivate:
6200 case OMPC_lastprivate:
6201 case OMPC_shared:
6202 case OMPC_reduction:
6203 case OMPC_linear:
6204 case OMPC_aligned:
6205 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006206 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006207 case OMPC_default:
6208 case OMPC_proc_bind:
6209 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006210 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006211 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006212 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006213 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006214 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006215 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006216 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006217 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006218 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006219 case OMPC_hint:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006220 case OMPC_unknown:
6221 llvm_unreachable("Clause is not allowed.");
6222 }
6223 return Res;
6224}
6225
Alexey Bataev236070f2014-06-20 11:19:47 +00006226OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6227 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006228 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006229 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6230}
6231
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006232OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6233 SourceLocation EndLoc) {
6234 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6235}
6236
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006237OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6238 SourceLocation EndLoc) {
6239 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6240}
6241
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006242OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6243 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006244 return new (Context) OMPReadClause(StartLoc, EndLoc);
6245}
6246
Alexey Bataevdea47612014-07-23 07:46:59 +00006247OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6248 SourceLocation EndLoc) {
6249 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6250}
6251
Alexey Bataev67a4f222014-07-23 10:25:33 +00006252OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6253 SourceLocation EndLoc) {
6254 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6255}
6256
Alexey Bataev459dec02014-07-24 06:46:57 +00006257OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6258 SourceLocation EndLoc) {
6259 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6260}
6261
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006262OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6263 SourceLocation EndLoc) {
6264 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6265}
6266
Alexey Bataev346265e2015-09-25 10:37:12 +00006267OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6268 SourceLocation EndLoc) {
6269 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6270}
6271
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006272OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6273 SourceLocation EndLoc) {
6274 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6275}
6276
Alexey Bataevb825de12015-12-07 10:51:44 +00006277OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6278 SourceLocation EndLoc) {
6279 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6280}
6281
Alexey Bataevc5e02582014-06-16 07:08:35 +00006282OMPClause *Sema::ActOnOpenMPVarListClause(
6283 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6284 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6285 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006286 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006287 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6288 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006289 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006290 switch (Kind) {
6291 case OMPC_private:
6292 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6293 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006294 case OMPC_firstprivate:
6295 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6296 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006297 case OMPC_lastprivate:
6298 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6299 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006300 case OMPC_shared:
6301 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6302 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006303 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006304 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6305 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006306 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006307 case OMPC_linear:
6308 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006309 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006310 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006311 case OMPC_aligned:
6312 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6313 ColonLoc, EndLoc);
6314 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006315 case OMPC_copyin:
6316 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6317 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006318 case OMPC_copyprivate:
6319 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6320 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006321 case OMPC_flush:
6322 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6323 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006324 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006325 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6326 StartLoc, LParenLoc, EndLoc);
6327 break;
6328 case OMPC_map:
6329 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6330 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006331 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006332 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006333 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006334 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006335 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006336 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006337 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006338 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006339 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006340 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006341 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006342 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006343 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006344 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006345 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006346 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006347 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006348 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006349 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006350 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006351 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006352 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006353 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006354 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006355 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006356 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006357 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006358 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006359 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006360 case OMPC_hint:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006361 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006362 llvm_unreachable("Clause is not allowed.");
6363 }
6364 return Res;
6365}
6366
6367OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6368 SourceLocation StartLoc,
6369 SourceLocation LParenLoc,
6370 SourceLocation EndLoc) {
6371 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006372 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006373 for (auto &RefExpr : VarList) {
6374 assert(RefExpr && "NULL expr in OpenMP private clause.");
6375 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006376 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006377 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006378 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006379 continue;
6380 }
6381
Alexey Bataeved09d242014-05-28 05:53:51 +00006382 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006383 // OpenMP [2.1, C/C++]
6384 // A list item is a variable name.
6385 // OpenMP [2.9.3.3, Restrictions, p.1]
6386 // A variable that is part of another variable (as an array or
6387 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006388 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006389 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006390 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006391 continue;
6392 }
6393 Decl *D = DE->getDecl();
6394 VarDecl *VD = cast<VarDecl>(D);
6395
6396 QualType Type = VD->getType();
6397 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6398 // It will be analyzed later.
6399 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006400 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006401 continue;
6402 }
6403
6404 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6405 // A variable that appears in a private clause must not have an incomplete
6406 // type or a reference type.
6407 if (RequireCompleteType(ELoc, Type,
6408 diag::err_omp_private_incomplete_type)) {
6409 continue;
6410 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006411 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006412
Alexey Bataev758e55e2013-09-06 18:03:48 +00006413 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6414 // in a Construct]
6415 // Variables with the predetermined data-sharing attributes may not be
6416 // listed in data-sharing attributes clauses, except for the cases
6417 // listed below. For these exceptions only, listing a predetermined
6418 // variable in a data-sharing attribute clause is allowed and overrides
6419 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006420 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006421 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006422 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6423 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006424 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006425 continue;
6426 }
6427
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006428 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006429 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006430 DSAStack->getCurrentDirective() == OMPD_task) {
6431 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6432 << getOpenMPClauseName(OMPC_private) << Type
6433 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6434 bool IsDecl =
6435 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6436 Diag(VD->getLocation(),
6437 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6438 << VD;
6439 continue;
6440 }
6441
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006442 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6443 // A variable of class type (or array thereof) that appears in a private
6444 // clause requires an accessible, unambiguous default constructor for the
6445 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006446 // Generate helper private variable and initialize it with the default
6447 // value. The address of the original variable is replaced by the address of
6448 // the new private variable in CodeGen. This new variable is not added to
6449 // IdResolver, so the code in the OpenMP region uses original variable for
6450 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006451 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006452 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6453 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006454 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006455 if (VDPrivate->isInvalidDecl())
6456 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006457 auto VDPrivateRefExpr = buildDeclRefExpr(
6458 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006459
Alexey Bataev758e55e2013-09-06 18:03:48 +00006460 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006461 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006462 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006463 }
6464
Alexey Bataeved09d242014-05-28 05:53:51 +00006465 if (Vars.empty())
6466 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006467
Alexey Bataev03b340a2014-10-21 03:16:40 +00006468 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6469 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006470}
6471
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006472namespace {
6473class DiagsUninitializedSeveretyRAII {
6474private:
6475 DiagnosticsEngine &Diags;
6476 SourceLocation SavedLoc;
6477 bool IsIgnored;
6478
6479public:
6480 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6481 bool IsIgnored)
6482 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6483 if (!IsIgnored) {
6484 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6485 /*Map*/ diag::Severity::Ignored, Loc);
6486 }
6487 }
6488 ~DiagsUninitializedSeveretyRAII() {
6489 if (!IsIgnored)
6490 Diags.popMappings(SavedLoc);
6491 }
6492};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006493}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006494
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006495OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6496 SourceLocation StartLoc,
6497 SourceLocation LParenLoc,
6498 SourceLocation EndLoc) {
6499 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006500 SmallVector<Expr *, 8> PrivateCopies;
6501 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006502 bool IsImplicitClause =
6503 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6504 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6505
Alexey Bataeved09d242014-05-28 05:53:51 +00006506 for (auto &RefExpr : VarList) {
6507 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6508 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006509 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006510 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006511 PrivateCopies.push_back(nullptr);
6512 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006513 continue;
6514 }
6515
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006516 SourceLocation ELoc =
6517 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006518 // OpenMP [2.1, C/C++]
6519 // A list item is a variable name.
6520 // OpenMP [2.9.3.3, Restrictions, p.1]
6521 // A variable that is part of another variable (as an array or
6522 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006523 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006524 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006525 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006526 continue;
6527 }
6528 Decl *D = DE->getDecl();
6529 VarDecl *VD = cast<VarDecl>(D);
6530
6531 QualType Type = VD->getType();
6532 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6533 // It will be analyzed later.
6534 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006535 PrivateCopies.push_back(nullptr);
6536 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006537 continue;
6538 }
6539
6540 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6541 // A variable that appears in a private clause must not have an incomplete
6542 // type or a reference type.
6543 if (RequireCompleteType(ELoc, Type,
6544 diag::err_omp_firstprivate_incomplete_type)) {
6545 continue;
6546 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006547 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006548
6549 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6550 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006551 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006552 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006553 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006554
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006555 // If an implicit firstprivate variable found it was checked already.
6556 if (!IsImplicitClause) {
6557 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006558 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006559 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6560 // A list item that specifies a given variable may not appear in more
6561 // than one clause on the same directive, except that a variable may be
6562 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006563 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006564 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006565 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006566 << getOpenMPClauseName(DVar.CKind)
6567 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006568 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006569 continue;
6570 }
6571
6572 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6573 // in a Construct]
6574 // Variables with the predetermined data-sharing attributes may not be
6575 // listed in data-sharing attributes clauses, except for the cases
6576 // listed below. For these exceptions only, listing a predetermined
6577 // variable in a data-sharing attribute clause is allowed and overrides
6578 // the variable's predetermined data-sharing attributes.
6579 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6580 // in a Construct, C/C++, p.2]
6581 // Variables with const-qualified type having no mutable member may be
6582 // listed in a firstprivate clause, even if they are static data members.
6583 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6584 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6585 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006586 << getOpenMPClauseName(DVar.CKind)
6587 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006588 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006589 continue;
6590 }
6591
Alexey Bataevf29276e2014-06-18 04:14:57 +00006592 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006593 // OpenMP [2.9.3.4, Restrictions, p.2]
6594 // A list item that is private within a parallel region must not appear
6595 // in a firstprivate clause on a worksharing construct if any of the
6596 // worksharing regions arising from the worksharing construct ever bind
6597 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006598 if (isOpenMPWorksharingDirective(CurrDir) &&
6599 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006600 DVar = DSAStack->getImplicitDSA(VD, true);
6601 if (DVar.CKind != OMPC_shared &&
6602 (isOpenMPParallelDirective(DVar.DKind) ||
6603 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006604 Diag(ELoc, diag::err_omp_required_access)
6605 << getOpenMPClauseName(OMPC_firstprivate)
6606 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006607 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006608 continue;
6609 }
6610 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006611 // OpenMP [2.9.3.4, Restrictions, p.3]
6612 // A list item that appears in a reduction clause of a parallel construct
6613 // must not appear in a firstprivate clause on a worksharing or task
6614 // construct if any of the worksharing or task regions arising from the
6615 // worksharing or task construct ever bind to any of the parallel regions
6616 // arising from the parallel construct.
6617 // OpenMP [2.9.3.4, Restrictions, p.4]
6618 // A list item that appears in a reduction clause in worksharing
6619 // construct must not appear in a firstprivate clause in a task construct
6620 // encountered during execution of any of the worksharing regions arising
6621 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006622 if (CurrDir == OMPD_task) {
6623 DVar =
6624 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6625 [](OpenMPDirectiveKind K) -> bool {
6626 return isOpenMPParallelDirective(K) ||
6627 isOpenMPWorksharingDirective(K);
6628 },
6629 false);
6630 if (DVar.CKind == OMPC_reduction &&
6631 (isOpenMPParallelDirective(DVar.DKind) ||
6632 isOpenMPWorksharingDirective(DVar.DKind))) {
6633 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6634 << getOpenMPDirectiveName(DVar.DKind);
6635 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6636 continue;
6637 }
6638 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006639
6640 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6641 // A list item that is private within a teams region must not appear in a
6642 // firstprivate clause on a distribute construct if any of the distribute
6643 // regions arising from the distribute construct ever bind to any of the
6644 // teams regions arising from the teams construct.
6645 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6646 // A list item that appears in a reduction clause of a teams construct
6647 // must not appear in a firstprivate clause on a distribute construct if
6648 // any of the distribute regions arising from the distribute construct
6649 // ever bind to any of the teams regions arising from the teams construct.
6650 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6651 // A list item may appear in a firstprivate or lastprivate clause but not
6652 // both.
6653 if (CurrDir == OMPD_distribute) {
6654 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6655 [](OpenMPDirectiveKind K) -> bool {
6656 return isOpenMPTeamsDirective(K);
6657 },
6658 false);
6659 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6660 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6661 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6662 continue;
6663 }
6664 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6665 [](OpenMPDirectiveKind K) -> bool {
6666 return isOpenMPTeamsDirective(K);
6667 },
6668 false);
6669 if (DVar.CKind == OMPC_reduction &&
6670 isOpenMPTeamsDirective(DVar.DKind)) {
6671 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6672 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6673 continue;
6674 }
6675 DVar = DSAStack->getTopDSA(VD, false);
6676 if (DVar.CKind == OMPC_lastprivate) {
6677 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6678 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6679 continue;
6680 }
6681 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006682 }
6683
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006684 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006685 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006686 DSAStack->getCurrentDirective() == OMPD_task) {
6687 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6688 << getOpenMPClauseName(OMPC_firstprivate) << Type
6689 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6690 bool IsDecl =
6691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6692 Diag(VD->getLocation(),
6693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6694 << VD;
6695 continue;
6696 }
6697
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006698 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006699 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6700 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006701 // Generate helper private variable and initialize it with the value of the
6702 // original variable. The address of the original variable is replaced by
6703 // the address of the new private variable in the CodeGen. This new variable
6704 // is not added to IdResolver, so the code in the OpenMP region uses
6705 // original variable for proper diagnostics and variable capturing.
6706 Expr *VDInitRefExpr = nullptr;
6707 // For arrays generate initializer for single element and replace it by the
6708 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006709 if (Type->isArrayType()) {
6710 auto VDInit =
6711 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6712 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006713 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006714 ElemType = ElemType.getUnqualifiedType();
6715 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6716 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006717 InitializedEntity Entity =
6718 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006719 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6720
6721 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6722 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6723 if (Result.isInvalid())
6724 VDPrivate->setInvalidDecl();
6725 else
6726 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006727 // Remove temp variable declaration.
6728 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006729 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006730 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006731 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006732 VDInitRefExpr =
6733 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006734 AddInitializerToDecl(VDPrivate,
6735 DefaultLvalueConversion(VDInitRefExpr).get(),
6736 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006737 }
6738 if (VDPrivate->isInvalidDecl()) {
6739 if (IsImplicitClause) {
6740 Diag(DE->getExprLoc(),
6741 diag::note_omp_task_predetermined_firstprivate_here);
6742 }
6743 continue;
6744 }
6745 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006746 auto VDPrivateRefExpr = buildDeclRefExpr(
6747 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006748 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6749 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006750 PrivateCopies.push_back(VDPrivateRefExpr);
6751 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006752 }
6753
Alexey Bataeved09d242014-05-28 05:53:51 +00006754 if (Vars.empty())
6755 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006756
6757 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006758 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006759}
6760
Alexander Musman1bb328c2014-06-04 13:06:39 +00006761OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6762 SourceLocation StartLoc,
6763 SourceLocation LParenLoc,
6764 SourceLocation EndLoc) {
6765 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006766 SmallVector<Expr *, 8> SrcExprs;
6767 SmallVector<Expr *, 8> DstExprs;
6768 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006769 for (auto &RefExpr : VarList) {
6770 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6771 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6772 // It will be analyzed later.
6773 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006774 SrcExprs.push_back(nullptr);
6775 DstExprs.push_back(nullptr);
6776 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006777 continue;
6778 }
6779
6780 SourceLocation ELoc = RefExpr->getExprLoc();
6781 // OpenMP [2.1, C/C++]
6782 // A list item is a variable name.
6783 // OpenMP [2.14.3.5, Restrictions, p.1]
6784 // A variable that is part of another variable (as an array or structure
6785 // element) cannot appear in a lastprivate clause.
6786 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6787 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6788 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6789 continue;
6790 }
6791 Decl *D = DE->getDecl();
6792 VarDecl *VD = cast<VarDecl>(D);
6793
6794 QualType Type = VD->getType();
6795 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6796 // It will be analyzed later.
6797 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006798 SrcExprs.push_back(nullptr);
6799 DstExprs.push_back(nullptr);
6800 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006801 continue;
6802 }
6803
6804 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6805 // A variable that appears in a lastprivate clause must not have an
6806 // incomplete type or a reference type.
6807 if (RequireCompleteType(ELoc, Type,
6808 diag::err_omp_lastprivate_incomplete_type)) {
6809 continue;
6810 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006811 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006812
6813 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6814 // in a Construct]
6815 // Variables with the predetermined data-sharing attributes may not be
6816 // listed in data-sharing attributes clauses, except for the cases
6817 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006818 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006819 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6820 DVar.CKind != OMPC_firstprivate &&
6821 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6822 Diag(ELoc, diag::err_omp_wrong_dsa)
6823 << getOpenMPClauseName(DVar.CKind)
6824 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006825 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006826 continue;
6827 }
6828
Alexey Bataevf29276e2014-06-18 04:14:57 +00006829 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6830 // OpenMP [2.14.3.5, Restrictions, p.2]
6831 // A list item that is private within a parallel region, or that appears in
6832 // the reduction clause of a parallel construct, must not appear in a
6833 // lastprivate clause on a worksharing construct if any of the corresponding
6834 // worksharing regions ever binds to any of the corresponding parallel
6835 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006836 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006837 if (isOpenMPWorksharingDirective(CurrDir) &&
6838 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006839 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006840 if (DVar.CKind != OMPC_shared) {
6841 Diag(ELoc, diag::err_omp_required_access)
6842 << getOpenMPClauseName(OMPC_lastprivate)
6843 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006844 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006845 continue;
6846 }
6847 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006848 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006849 // A variable of class type (or array thereof) that appears in a
6850 // lastprivate clause requires an accessible, unambiguous default
6851 // constructor for the class type, unless the list item is also specified
6852 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006853 // A variable of class type (or array thereof) that appears in a
6854 // lastprivate clause requires an accessible, unambiguous copy assignment
6855 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006856 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006857 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006858 Type.getUnqualifiedType(), ".lastprivate.src",
6859 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006860 auto *PseudoSrcExpr = buildDeclRefExpr(
6861 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006862 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006863 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6864 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006865 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006866 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006867 // For arrays generate assignment operation for single element and replace
6868 // it by the original array element in CodeGen.
6869 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6870 PseudoDstExpr, PseudoSrcExpr);
6871 if (AssignmentOp.isInvalid())
6872 continue;
6873 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6874 /*DiscardedValue=*/true);
6875 if (AssignmentOp.isInvalid())
6876 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006877
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006878 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6879 // A list item may appear in a firstprivate or lastprivate clause but not
6880 // both.
6881 if (CurrDir == OMPD_distribute) {
6882 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6883 if (DVar.CKind == OMPC_firstprivate) {
6884 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6885 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6886 continue;
6887 }
6888 }
6889
Alexey Bataev39f915b82015-05-08 10:41:21 +00006890 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006891 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006892 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006893 SrcExprs.push_back(PseudoSrcExpr);
6894 DstExprs.push_back(PseudoDstExpr);
6895 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006896 }
6897
6898 if (Vars.empty())
6899 return nullptr;
6900
6901 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006902 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006903}
6904
Alexey Bataev758e55e2013-09-06 18:03:48 +00006905OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6906 SourceLocation StartLoc,
6907 SourceLocation LParenLoc,
6908 SourceLocation EndLoc) {
6909 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006910 for (auto &RefExpr : VarList) {
6911 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6912 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006913 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006914 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006915 continue;
6916 }
6917
Alexey Bataeved09d242014-05-28 05:53:51 +00006918 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006919 // OpenMP [2.1, C/C++]
6920 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006921 // OpenMP [2.14.3.2, Restrictions, p.1]
6922 // A variable that is part of another variable (as an array or structure
6923 // element) cannot appear in a shared unless it is a static data member
6924 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006925 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006926 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006927 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006928 continue;
6929 }
6930 Decl *D = DE->getDecl();
6931 VarDecl *VD = cast<VarDecl>(D);
6932
6933 QualType Type = VD->getType();
6934 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6935 // It will be analyzed later.
6936 Vars.push_back(DE);
6937 continue;
6938 }
6939
6940 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6941 // in a Construct]
6942 // Variables with the predetermined data-sharing attributes may not be
6943 // listed in data-sharing attributes clauses, except for the cases
6944 // listed below. For these exceptions only, listing a predetermined
6945 // variable in a data-sharing attribute clause is allowed and overrides
6946 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006947 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006948 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6949 DVar.RefExpr) {
6950 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6951 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006952 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006953 continue;
6954 }
6955
6956 DSAStack->addDSA(VD, DE, OMPC_shared);
6957 Vars.push_back(DE);
6958 }
6959
Alexey Bataeved09d242014-05-28 05:53:51 +00006960 if (Vars.empty())
6961 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006962
6963 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6964}
6965
Alexey Bataevc5e02582014-06-16 07:08:35 +00006966namespace {
6967class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6968 DSAStackTy *Stack;
6969
6970public:
6971 bool VisitDeclRefExpr(DeclRefExpr *E) {
6972 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006973 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006974 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6975 return false;
6976 if (DVar.CKind != OMPC_unknown)
6977 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006978 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006979 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006980 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006981 return true;
6982 return false;
6983 }
6984 return false;
6985 }
6986 bool VisitStmt(Stmt *S) {
6987 for (auto Child : S->children()) {
6988 if (Child && Visit(Child))
6989 return true;
6990 }
6991 return false;
6992 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006993 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006994};
Alexey Bataev23b69422014-06-18 07:08:49 +00006995} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006996
6997OMPClause *Sema::ActOnOpenMPReductionClause(
6998 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6999 SourceLocation ColonLoc, SourceLocation EndLoc,
7000 CXXScopeSpec &ReductionIdScopeSpec,
7001 const DeclarationNameInfo &ReductionId) {
7002 // TODO: Allow scope specification search when 'declare reduction' is
7003 // supported.
7004 assert(ReductionIdScopeSpec.isEmpty() &&
7005 "No support for scoped reduction identifiers yet.");
7006
7007 auto DN = ReductionId.getName();
7008 auto OOK = DN.getCXXOverloadedOperator();
7009 BinaryOperatorKind BOK = BO_Comma;
7010
7011 // OpenMP [2.14.3.6, reduction clause]
7012 // C
7013 // reduction-identifier is either an identifier or one of the following
7014 // operators: +, -, *, &, |, ^, && and ||
7015 // C++
7016 // reduction-identifier is either an id-expression or one of the following
7017 // operators: +, -, *, &, |, ^, && and ||
7018 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7019 switch (OOK) {
7020 case OO_Plus:
7021 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007022 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007023 break;
7024 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007025 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007026 break;
7027 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007028 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007029 break;
7030 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007031 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007032 break;
7033 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007034 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007035 break;
7036 case OO_AmpAmp:
7037 BOK = BO_LAnd;
7038 break;
7039 case OO_PipePipe:
7040 BOK = BO_LOr;
7041 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007042 case OO_New:
7043 case OO_Delete:
7044 case OO_Array_New:
7045 case OO_Array_Delete:
7046 case OO_Slash:
7047 case OO_Percent:
7048 case OO_Tilde:
7049 case OO_Exclaim:
7050 case OO_Equal:
7051 case OO_Less:
7052 case OO_Greater:
7053 case OO_LessEqual:
7054 case OO_GreaterEqual:
7055 case OO_PlusEqual:
7056 case OO_MinusEqual:
7057 case OO_StarEqual:
7058 case OO_SlashEqual:
7059 case OO_PercentEqual:
7060 case OO_CaretEqual:
7061 case OO_AmpEqual:
7062 case OO_PipeEqual:
7063 case OO_LessLess:
7064 case OO_GreaterGreater:
7065 case OO_LessLessEqual:
7066 case OO_GreaterGreaterEqual:
7067 case OO_EqualEqual:
7068 case OO_ExclaimEqual:
7069 case OO_PlusPlus:
7070 case OO_MinusMinus:
7071 case OO_Comma:
7072 case OO_ArrowStar:
7073 case OO_Arrow:
7074 case OO_Call:
7075 case OO_Subscript:
7076 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007077 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007078 case NUM_OVERLOADED_OPERATORS:
7079 llvm_unreachable("Unexpected reduction identifier");
7080 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007081 if (auto II = DN.getAsIdentifierInfo()) {
7082 if (II->isStr("max"))
7083 BOK = BO_GT;
7084 else if (II->isStr("min"))
7085 BOK = BO_LT;
7086 }
7087 break;
7088 }
7089 SourceRange ReductionIdRange;
7090 if (ReductionIdScopeSpec.isValid()) {
7091 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7092 }
7093 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7094 if (BOK == BO_Comma) {
7095 // Not allowed reduction identifier is found.
7096 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7097 << ReductionIdRange;
7098 return nullptr;
7099 }
7100
7101 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007102 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007103 SmallVector<Expr *, 8> LHSs;
7104 SmallVector<Expr *, 8> RHSs;
7105 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007106 for (auto RefExpr : VarList) {
7107 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7108 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7109 // It will be analyzed later.
7110 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007111 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007112 LHSs.push_back(nullptr);
7113 RHSs.push_back(nullptr);
7114 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007115 continue;
7116 }
7117
7118 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7119 RefExpr->isInstantiationDependent() ||
7120 RefExpr->containsUnexpandedParameterPack()) {
7121 // It will be analyzed later.
7122 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007123 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007124 LHSs.push_back(nullptr);
7125 RHSs.push_back(nullptr);
7126 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007127 continue;
7128 }
7129
7130 auto ELoc = RefExpr->getExprLoc();
7131 auto ERange = RefExpr->getSourceRange();
7132 // OpenMP [2.1, C/C++]
7133 // A list item is a variable or array section, subject to the restrictions
7134 // specified in Section 2.4 on page 42 and in each of the sections
7135 // describing clauses and directives for which a list appears.
7136 // OpenMP [2.14.3.3, Restrictions, p.1]
7137 // A variable that is part of another variable (as an array or
7138 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007139 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7140 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7141 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7142 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7143 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007144 continue;
7145 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007146 QualType Type;
7147 VarDecl *VD = nullptr;
7148 if (DE) {
7149 auto D = DE->getDecl();
7150 VD = cast<VarDecl>(D);
7151 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007152 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007153 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007154 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7155 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7156 Base = TempASE->getBase()->IgnoreParenImpCasts();
7157 DE = dyn_cast<DeclRefExpr>(Base);
7158 if (DE)
7159 VD = dyn_cast<VarDecl>(DE->getDecl());
7160 if (!VD) {
7161 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7162 << 0 << Base->getSourceRange();
7163 continue;
7164 }
7165 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007166 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7167 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7168 Type = ATy->getElementType();
7169 else
7170 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007171 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7172 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7173 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7174 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7175 Base = TempASE->getBase()->IgnoreParenImpCasts();
7176 DE = dyn_cast<DeclRefExpr>(Base);
7177 if (DE)
7178 VD = dyn_cast<VarDecl>(DE->getDecl());
7179 if (!VD) {
7180 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7181 << 1 << Base->getSourceRange();
7182 continue;
7183 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007184 }
7185
Alexey Bataevc5e02582014-06-16 07:08:35 +00007186 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7187 // A variable that appears in a private clause must not have an incomplete
7188 // type or a reference type.
7189 if (RequireCompleteType(ELoc, Type,
7190 diag::err_omp_reduction_incomplete_type))
7191 continue;
7192 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7193 // Arrays may not appear in a reduction clause.
7194 if (Type.getNonReferenceType()->isArrayType()) {
7195 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007196 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007197 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7198 VarDecl::DeclarationOnly;
7199 Diag(VD->getLocation(),
7200 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7201 << VD;
7202 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007203 continue;
7204 }
7205 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7206 // A list item that appears in a reduction clause must not be
7207 // const-qualified.
7208 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007209 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007210 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007211 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007212 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7213 VarDecl::DeclarationOnly;
7214 Diag(VD->getLocation(),
7215 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7216 << VD;
7217 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007218 continue;
7219 }
7220 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7221 // If a list-item is a reference type then it must bind to the same object
7222 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007223 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007224 VarDecl *VDDef = VD->getDefinition();
7225 if (Type->isReferenceType() && VDDef) {
7226 DSARefChecker Check(DSAStack);
7227 if (Check.Visit(VDDef->getInit())) {
7228 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7229 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7230 continue;
7231 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007232 }
7233 }
7234 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7235 // The type of a list item that appears in a reduction clause must be valid
7236 // for the reduction-identifier. For a max or min reduction in C, the type
7237 // of the list item must be an allowed arithmetic data type: char, int,
7238 // float, double, or _Bool, possibly modified with long, short, signed, or
7239 // unsigned. For a max or min reduction in C++, the type of the list item
7240 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7241 // double, or bool, possibly modified with long, short, signed, or unsigned.
7242 if ((BOK == BO_GT || BOK == BO_LT) &&
7243 !(Type->isScalarType() ||
7244 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7245 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7246 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007247 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007248 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7249 VarDecl::DeclarationOnly;
7250 Diag(VD->getLocation(),
7251 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7252 << VD;
7253 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007254 continue;
7255 }
7256 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7257 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7258 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007259 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007260 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7261 VarDecl::DeclarationOnly;
7262 Diag(VD->getLocation(),
7263 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7264 << VD;
7265 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007266 continue;
7267 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007268 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7269 // in a Construct]
7270 // Variables with the predetermined data-sharing attributes may not be
7271 // listed in data-sharing attributes clauses, except for the cases
7272 // listed below. For these exceptions only, listing a predetermined
7273 // variable in a data-sharing attribute clause is allowed and overrides
7274 // the variable's predetermined data-sharing attributes.
7275 // OpenMP [2.14.3.6, Restrictions, p.3]
7276 // Any number of reduction clauses can be specified on the directive,
7277 // but a list item can appear only once in the reduction clauses for that
7278 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007279 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007280 DVar = DSAStack->getTopDSA(VD, false);
7281 if (DVar.CKind == OMPC_reduction) {
7282 Diag(ELoc, diag::err_omp_once_referenced)
7283 << getOpenMPClauseName(OMPC_reduction);
7284 if (DVar.RefExpr) {
7285 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007286 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007287 } else if (DVar.CKind != OMPC_unknown) {
7288 Diag(ELoc, diag::err_omp_wrong_dsa)
7289 << getOpenMPClauseName(DVar.CKind)
7290 << getOpenMPClauseName(OMPC_reduction);
7291 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7292 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007293 }
7294
7295 // OpenMP [2.14.3.6, Restrictions, p.1]
7296 // A list item that appears in a reduction clause of a worksharing
7297 // construct must be shared in the parallel regions to which any of the
7298 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007299 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7300 if (isOpenMPWorksharingDirective(CurrDir) &&
7301 !isOpenMPParallelDirective(CurrDir)) {
7302 DVar = DSAStack->getImplicitDSA(VD, true);
7303 if (DVar.CKind != OMPC_shared) {
7304 Diag(ELoc, diag::err_omp_required_access)
7305 << getOpenMPClauseName(OMPC_reduction)
7306 << getOpenMPClauseName(OMPC_shared);
7307 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7308 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007309 }
7310 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007311
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007312 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007313 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7314 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7315 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7316 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7317 auto PrivateTy = Type;
7318 if (OASE) {
7319 // For array sections only:
7320 // Create pseudo array type for private copy. The size for this array will
7321 // be generated during codegen.
7322 // For array subscripts or single variables Private Ty is the same as Type
7323 // (type of the variable or single array element).
7324 PrivateTy = Context.getVariableArrayType(
7325 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7326 Context.getSizeType(), VK_RValue),
7327 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7328 }
7329 // Private copy.
7330 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7331 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007332 // Add initializer for private variable.
7333 Expr *Init = nullptr;
7334 switch (BOK) {
7335 case BO_Add:
7336 case BO_Xor:
7337 case BO_Or:
7338 case BO_LOr:
7339 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7340 if (Type->isScalarType() || Type->isAnyComplexType()) {
7341 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007342 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007343 break;
7344 case BO_Mul:
7345 case BO_LAnd:
7346 if (Type->isScalarType() || Type->isAnyComplexType()) {
7347 // '*' and '&&' reduction ops - initializer is '1'.
7348 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7349 }
7350 break;
7351 case BO_And: {
7352 // '&' reduction op - initializer is '~0'.
7353 QualType OrigType = Type;
7354 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7355 Type = ComplexTy->getElementType();
7356 }
7357 if (Type->isRealFloatingType()) {
7358 llvm::APFloat InitValue =
7359 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7360 /*isIEEE=*/true);
7361 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7362 Type, ELoc);
7363 } else if (Type->isScalarType()) {
7364 auto Size = Context.getTypeSize(Type);
7365 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7366 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7367 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7368 }
7369 if (Init && OrigType->isAnyComplexType()) {
7370 // Init = 0xFFFF + 0xFFFFi;
7371 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7372 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7373 }
7374 Type = OrigType;
7375 break;
7376 }
7377 case BO_LT:
7378 case BO_GT: {
7379 // 'min' reduction op - initializer is 'Largest representable number in
7380 // the reduction list item type'.
7381 // 'max' reduction op - initializer is 'Least representable number in
7382 // the reduction list item type'.
7383 if (Type->isIntegerType() || Type->isPointerType()) {
7384 bool IsSigned = Type->hasSignedIntegerRepresentation();
7385 auto Size = Context.getTypeSize(Type);
7386 QualType IntTy =
7387 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7388 llvm::APInt InitValue =
7389 (BOK != BO_LT)
7390 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7391 : llvm::APInt::getMinValue(Size)
7392 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7393 : llvm::APInt::getMaxValue(Size);
7394 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7395 if (Type->isPointerType()) {
7396 // Cast to pointer type.
7397 auto CastExpr = BuildCStyleCastExpr(
7398 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7399 SourceLocation(), Init);
7400 if (CastExpr.isInvalid())
7401 continue;
7402 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007403 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007404 } else if (Type->isRealFloatingType()) {
7405 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7406 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7407 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7408 Type, ELoc);
7409 }
7410 break;
7411 }
7412 case BO_PtrMemD:
7413 case BO_PtrMemI:
7414 case BO_MulAssign:
7415 case BO_Div:
7416 case BO_Rem:
7417 case BO_Sub:
7418 case BO_Shl:
7419 case BO_Shr:
7420 case BO_LE:
7421 case BO_GE:
7422 case BO_EQ:
7423 case BO_NE:
7424 case BO_AndAssign:
7425 case BO_XorAssign:
7426 case BO_OrAssign:
7427 case BO_Assign:
7428 case BO_AddAssign:
7429 case BO_SubAssign:
7430 case BO_DivAssign:
7431 case BO_RemAssign:
7432 case BO_ShlAssign:
7433 case BO_ShrAssign:
7434 case BO_Comma:
7435 llvm_unreachable("Unexpected reduction operation");
7436 }
7437 if (Init) {
7438 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7439 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007440 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007441 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007442 if (!RHSVD->hasInit()) {
7443 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7444 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007445 if (VD) {
7446 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7447 VarDecl::DeclarationOnly;
7448 Diag(VD->getLocation(),
7449 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7450 << VD;
7451 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007452 continue;
7453 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007454 // Store initializer for single element in private copy. Will be used during
7455 // codegen.
7456 PrivateVD->setInit(RHSVD->getInit());
7457 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007458 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7459 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007460 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007461 ExprResult ReductionOp =
7462 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7463 LHSDRE, RHSDRE);
7464 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007465 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007466 ReductionOp =
7467 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7468 BO_Assign, LHSDRE, ReductionOp.get());
7469 } else {
7470 auto *ConditionalOp = new (Context) ConditionalOperator(
7471 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7472 RHSDRE, Type, VK_LValue, OK_Ordinary);
7473 ReductionOp =
7474 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7475 BO_Assign, LHSDRE, ConditionalOp);
7476 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007477 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007478 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007479 if (ReductionOp.isInvalid())
7480 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007481
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007482 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007483 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007484 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007485 LHSs.push_back(LHSDRE);
7486 RHSs.push_back(RHSDRE);
7487 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007488 }
7489
7490 if (Vars.empty())
7491 return nullptr;
7492
7493 return OMPReductionClause::Create(
7494 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007495 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7496 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007497}
7498
Alexey Bataev182227b2015-08-20 10:54:39 +00007499OMPClause *Sema::ActOnOpenMPLinearClause(
7500 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7501 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7502 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007503 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007504 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007505 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007506 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7507 LinKind == OMPC_LINEAR_unknown) {
7508 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7509 LinKind = OMPC_LINEAR_val;
7510 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007511 for (auto &RefExpr : VarList) {
7512 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7513 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007514 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007515 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007516 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007517 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007518 continue;
7519 }
7520
7521 // OpenMP [2.14.3.7, linear clause]
7522 // A list item that appears in a linear clause is subject to the private
7523 // clause semantics described in Section 2.14.3.3 on page 159 except as
7524 // noted. In addition, the value of the new list item on each iteration
7525 // of the associated loop(s) corresponds to the value of the original
7526 // list item before entering the construct plus the logical number of
7527 // the iteration times linear-step.
7528
Alexey Bataeved09d242014-05-28 05:53:51 +00007529 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007530 // OpenMP [2.1, C/C++]
7531 // A list item is a variable name.
7532 // OpenMP [2.14.3.3, Restrictions, p.1]
7533 // A variable that is part of another variable (as an array or
7534 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007535 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007536 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007537 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007538 continue;
7539 }
7540
7541 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7542
7543 // OpenMP [2.14.3.7, linear clause]
7544 // A list-item cannot appear in more than one linear clause.
7545 // A list-item that appears in a linear clause cannot appear in any
7546 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007547 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007548 if (DVar.RefExpr) {
7549 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7550 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007551 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007552 continue;
7553 }
7554
7555 QualType QType = VD->getType();
7556 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7557 // It will be analyzed later.
7558 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007559 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007560 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007561 continue;
7562 }
7563
7564 // A variable must not have an incomplete type or a reference type.
7565 if (RequireCompleteType(ELoc, QType,
7566 diag::err_omp_linear_incomplete_type)) {
7567 continue;
7568 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007569 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7570 !QType->isReferenceType()) {
7571 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7572 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7573 continue;
7574 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007575 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007576
7577 // A list item must not be const-qualified.
7578 if (QType.isConstant(Context)) {
7579 Diag(ELoc, diag::err_omp_const_variable)
7580 << getOpenMPClauseName(OMPC_linear);
7581 bool IsDecl =
7582 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7583 Diag(VD->getLocation(),
7584 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7585 << VD;
7586 continue;
7587 }
7588
7589 // A list item must be of integral or pointer type.
7590 QType = QType.getUnqualifiedType().getCanonicalType();
7591 const Type *Ty = QType.getTypePtrOrNull();
7592 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7593 !Ty->isPointerType())) {
7594 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7595 bool IsDecl =
7596 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7597 Diag(VD->getLocation(),
7598 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7599 << VD;
7600 continue;
7601 }
7602
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007603 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007604 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7605 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007606 auto *PrivateRef = buildDeclRefExpr(
7607 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007608 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007609 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007610 Expr *InitExpr;
7611 if (LinKind == OMPC_LINEAR_uval)
7612 InitExpr = VD->getInit();
7613 else
7614 InitExpr = DE;
7615 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007616 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007617 auto InitRef = buildDeclRefExpr(
7618 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007619 DSAStack->addDSA(VD, DE, OMPC_linear);
7620 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007621 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007622 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007623 }
7624
7625 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007626 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007627
7628 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007629 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007630 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7631 !Step->isInstantiationDependent() &&
7632 !Step->containsUnexpandedParameterPack()) {
7633 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007634 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007635 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007636 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007637 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007638
Alexander Musman3276a272015-03-21 10:12:56 +00007639 // Build var to save the step value.
7640 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007641 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007642 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007643 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007644 ExprResult CalcStep =
7645 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007646 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007647
Alexander Musman8dba6642014-04-22 13:09:42 +00007648 // Warn about zero linear step (it would be probably better specified as
7649 // making corresponding variables 'const').
7650 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007651 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7652 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007653 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7654 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007655 if (!IsConstant && CalcStep.isUsable()) {
7656 // Calculate the step beforehand instead of doing this on each iteration.
7657 // (This is not used if the number of iterations may be kfold-ed).
7658 CalcStepExpr = CalcStep.get();
7659 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007660 }
7661
Alexey Bataev182227b2015-08-20 10:54:39 +00007662 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7663 ColonLoc, EndLoc, Vars, Privates, Inits,
7664 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007665}
7666
7667static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7668 Expr *NumIterations, Sema &SemaRef,
7669 Scope *S) {
7670 // Walk the vars and build update/final expressions for the CodeGen.
7671 SmallVector<Expr *, 8> Updates;
7672 SmallVector<Expr *, 8> Finals;
7673 Expr *Step = Clause.getStep();
7674 Expr *CalcStep = Clause.getCalcStep();
7675 // OpenMP [2.14.3.7, linear clause]
7676 // If linear-step is not specified it is assumed to be 1.
7677 if (Step == nullptr)
7678 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7679 else if (CalcStep)
7680 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7681 bool HasErrors = false;
7682 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007683 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007684 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007685 for (auto &RefExpr : Clause.varlists()) {
7686 Expr *InitExpr = *CurInit;
7687
7688 // Build privatized reference to the current linear var.
7689 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007690 Expr *CapturedRef;
7691 if (LinKind == OMPC_LINEAR_uval)
7692 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7693 else
7694 CapturedRef =
7695 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7696 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7697 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007698
7699 // Build update: Var = InitExpr + IV * Step
7700 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007701 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007702 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007703 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7704 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007705
7706 // Build final: Var = InitExpr + NumIterations * Step
7707 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007708 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007709 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007710 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7711 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007712 if (!Update.isUsable() || !Final.isUsable()) {
7713 Updates.push_back(nullptr);
7714 Finals.push_back(nullptr);
7715 HasErrors = true;
7716 } else {
7717 Updates.push_back(Update.get());
7718 Finals.push_back(Final.get());
7719 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007720 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007721 }
7722 Clause.setUpdates(Updates);
7723 Clause.setFinals(Finals);
7724 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007725}
7726
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007727OMPClause *Sema::ActOnOpenMPAlignedClause(
7728 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7729 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7730
7731 SmallVector<Expr *, 8> Vars;
7732 for (auto &RefExpr : VarList) {
7733 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7734 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7735 // It will be analyzed later.
7736 Vars.push_back(RefExpr);
7737 continue;
7738 }
7739
7740 SourceLocation ELoc = RefExpr->getExprLoc();
7741 // OpenMP [2.1, C/C++]
7742 // A list item is a variable name.
7743 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7744 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7745 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7746 continue;
7747 }
7748
7749 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7750
7751 // OpenMP [2.8.1, simd construct, Restrictions]
7752 // The type of list items appearing in the aligned clause must be
7753 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007754 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007755 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007756 const Type *Ty = QType.getTypePtrOrNull();
7757 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7758 !Ty->isPointerType())) {
7759 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7760 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7761 bool IsDecl =
7762 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7763 Diag(VD->getLocation(),
7764 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7765 << VD;
7766 continue;
7767 }
7768
7769 // OpenMP [2.8.1, simd construct, Restrictions]
7770 // A list-item cannot appear in more than one aligned clause.
7771 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7772 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7773 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7774 << getOpenMPClauseName(OMPC_aligned);
7775 continue;
7776 }
7777
7778 Vars.push_back(DE);
7779 }
7780
7781 // OpenMP [2.8.1, simd construct, Description]
7782 // The parameter of the aligned clause, alignment, must be a constant
7783 // positive integer expression.
7784 // If no optional parameter is specified, implementation-defined default
7785 // alignments for SIMD instructions on the target platforms are assumed.
7786 if (Alignment != nullptr) {
7787 ExprResult AlignResult =
7788 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7789 if (AlignResult.isInvalid())
7790 return nullptr;
7791 Alignment = AlignResult.get();
7792 }
7793 if (Vars.empty())
7794 return nullptr;
7795
7796 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7797 EndLoc, Vars, Alignment);
7798}
7799
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007800OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7801 SourceLocation StartLoc,
7802 SourceLocation LParenLoc,
7803 SourceLocation EndLoc) {
7804 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007805 SmallVector<Expr *, 8> SrcExprs;
7806 SmallVector<Expr *, 8> DstExprs;
7807 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007808 for (auto &RefExpr : VarList) {
7809 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7810 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007811 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007812 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007813 SrcExprs.push_back(nullptr);
7814 DstExprs.push_back(nullptr);
7815 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007816 continue;
7817 }
7818
Alexey Bataeved09d242014-05-28 05:53:51 +00007819 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007820 // OpenMP [2.1, C/C++]
7821 // A list item is a variable name.
7822 // OpenMP [2.14.4.1, Restrictions, p.1]
7823 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007824 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007825 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007826 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007827 continue;
7828 }
7829
7830 Decl *D = DE->getDecl();
7831 VarDecl *VD = cast<VarDecl>(D);
7832
7833 QualType Type = VD->getType();
7834 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7835 // It will be analyzed later.
7836 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007837 SrcExprs.push_back(nullptr);
7838 DstExprs.push_back(nullptr);
7839 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007840 continue;
7841 }
7842
7843 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7844 // A list item that appears in a copyin clause must be threadprivate.
7845 if (!DSAStack->isThreadPrivate(VD)) {
7846 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007847 << getOpenMPClauseName(OMPC_copyin)
7848 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007849 continue;
7850 }
7851
7852 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7853 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007854 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007855 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007856 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007857 auto *SrcVD =
7858 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7859 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007860 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007861 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7862 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007863 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7864 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007865 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007866 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007867 // For arrays generate assignment operation for single element and replace
7868 // it by the original array element in CodeGen.
7869 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7870 PseudoDstExpr, PseudoSrcExpr);
7871 if (AssignmentOp.isInvalid())
7872 continue;
7873 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7874 /*DiscardedValue=*/true);
7875 if (AssignmentOp.isInvalid())
7876 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007877
7878 DSAStack->addDSA(VD, DE, OMPC_copyin);
7879 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007880 SrcExprs.push_back(PseudoSrcExpr);
7881 DstExprs.push_back(PseudoDstExpr);
7882 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007883 }
7884
Alexey Bataeved09d242014-05-28 05:53:51 +00007885 if (Vars.empty())
7886 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007887
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007888 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7889 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007890}
7891
Alexey Bataevbae9a792014-06-27 10:37:06 +00007892OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7893 SourceLocation StartLoc,
7894 SourceLocation LParenLoc,
7895 SourceLocation EndLoc) {
7896 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007897 SmallVector<Expr *, 8> SrcExprs;
7898 SmallVector<Expr *, 8> DstExprs;
7899 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007900 for (auto &RefExpr : VarList) {
7901 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7902 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7903 // It will be analyzed later.
7904 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007905 SrcExprs.push_back(nullptr);
7906 DstExprs.push_back(nullptr);
7907 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007908 continue;
7909 }
7910
7911 SourceLocation ELoc = RefExpr->getExprLoc();
7912 // OpenMP [2.1, C/C++]
7913 // A list item is a variable name.
7914 // OpenMP [2.14.4.1, Restrictions, p.1]
7915 // A list item that appears in a copyin clause must be threadprivate.
7916 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7917 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7918 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7919 continue;
7920 }
7921
7922 Decl *D = DE->getDecl();
7923 VarDecl *VD = cast<VarDecl>(D);
7924
7925 QualType Type = VD->getType();
7926 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7927 // It will be analyzed later.
7928 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007929 SrcExprs.push_back(nullptr);
7930 DstExprs.push_back(nullptr);
7931 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007932 continue;
7933 }
7934
7935 // OpenMP [2.14.4.2, Restrictions, p.2]
7936 // A list item that appears in a copyprivate clause may not appear in a
7937 // private or firstprivate clause on the single construct.
7938 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007939 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007940 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7941 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007942 Diag(ELoc, diag::err_omp_wrong_dsa)
7943 << getOpenMPClauseName(DVar.CKind)
7944 << getOpenMPClauseName(OMPC_copyprivate);
7945 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7946 continue;
7947 }
7948
7949 // OpenMP [2.11.4.2, Restrictions, p.1]
7950 // All list items that appear in a copyprivate clause must be either
7951 // threadprivate or private in the enclosing context.
7952 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007953 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007954 if (DVar.CKind == OMPC_shared) {
7955 Diag(ELoc, diag::err_omp_required_access)
7956 << getOpenMPClauseName(OMPC_copyprivate)
7957 << "threadprivate or private in the enclosing context";
7958 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7959 continue;
7960 }
7961 }
7962 }
7963
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007964 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007965 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007966 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007967 << getOpenMPClauseName(OMPC_copyprivate) << Type
7968 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007969 bool IsDecl =
7970 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7971 Diag(VD->getLocation(),
7972 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7973 << VD;
7974 continue;
7975 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007976
Alexey Bataevbae9a792014-06-27 10:37:06 +00007977 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7978 // A variable of class type (or array thereof) that appears in a
7979 // copyin clause requires an accessible, unambiguous copy assignment
7980 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007981 Type = Context.getBaseElementType(Type.getNonReferenceType())
7982 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007983 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007984 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7985 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007986 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007987 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007988 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007989 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7990 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007991 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007992 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007993 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7994 PseudoDstExpr, PseudoSrcExpr);
7995 if (AssignmentOp.isInvalid())
7996 continue;
7997 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7998 /*DiscardedValue=*/true);
7999 if (AssignmentOp.isInvalid())
8000 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008001
8002 // No need to mark vars as copyprivate, they are already threadprivate or
8003 // implicitly private.
8004 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008005 SrcExprs.push_back(PseudoSrcExpr);
8006 DstExprs.push_back(PseudoDstExpr);
8007 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008008 }
8009
8010 if (Vars.empty())
8011 return nullptr;
8012
Alexey Bataeva63048e2015-03-23 06:18:07 +00008013 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8014 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008015}
8016
Alexey Bataev6125da92014-07-21 11:26:11 +00008017OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8018 SourceLocation StartLoc,
8019 SourceLocation LParenLoc,
8020 SourceLocation EndLoc) {
8021 if (VarList.empty())
8022 return nullptr;
8023
8024 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8025}
Alexey Bataevdea47612014-07-23 07:46:59 +00008026
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008027OMPClause *
8028Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8029 SourceLocation DepLoc, SourceLocation ColonLoc,
8030 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8031 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008032 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008033 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008034 std::string Values = "'";
8035 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, OMPC_DEPEND_source);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008036 Values += "' or '";
8037 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, OMPC_DEPEND_sink);
Alexey Bataeveb482352015-12-18 05:05:56 +00008038 Values += "'";
8039 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
8040 << Values << getOpenMPClauseName(OMPC_depend);
8041 return nullptr;
8042 }
8043 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008044 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8045 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008046 std::string Values;
8047 std::string Sep(", ");
8048 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008049 if (i == OMPC_DEPEND_source || i == OMPC_DEPEND_sink)
Alexey Bataeveb482352015-12-18 05:05:56 +00008050 continue;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008051 Values += "'";
8052 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
8053 Values += "'";
8054 switch (i) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008055 case OMPC_DEPEND_unknown - 4:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008056 Values += " or ";
8057 break;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008058 case OMPC_DEPEND_unknown - 3:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008059 break;
8060 default:
8061 Values += Sep;
8062 break;
8063 }
8064 }
8065 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
8066 << Values << getOpenMPClauseName(OMPC_depend);
8067 return nullptr;
8068 }
8069 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008070 llvm::APSInt DepCounter(/*BitWidth=*/32);
8071 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8072 if (DepKind == OMPC_DEPEND_sink) {
8073 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8074 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8075 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008076 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008077 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008078 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8079 DSAStack->getParentOrderedRegionParam()) {
8080 for (auto &RefExpr : VarList) {
8081 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8082 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8083 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8084 // It will be analyzed later.
8085 Vars.push_back(RefExpr);
8086 continue;
8087 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008088
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008089 SourceLocation ELoc = RefExpr->getExprLoc();
8090 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8091 if (DepKind == OMPC_DEPEND_sink) {
8092 if (DepCounter >= TotalDepCount) {
8093 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8094 continue;
8095 }
8096 ++DepCounter;
8097 // OpenMP [2.13.9, Summary]
8098 // depend(dependence-type : vec), where dependence-type is:
8099 // 'sink' and where vec is the iteration vector, which has the form:
8100 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8101 // where n is the value specified by the ordered clause in the loop
8102 // directive, xi denotes the loop iteration variable of the i-th nested
8103 // loop associated with the loop directive, and di is a constant
8104 // non-negative integer.
8105 SimpleExpr = SimpleExpr->IgnoreImplicit();
8106 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8107 if (!DE) {
8108 OverloadedOperatorKind OOK = OO_None;
8109 SourceLocation OOLoc;
8110 Expr *LHS, *RHS;
8111 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8112 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8113 OOLoc = BO->getOperatorLoc();
8114 LHS = BO->getLHS()->IgnoreParenImpCasts();
8115 RHS = BO->getRHS()->IgnoreParenImpCasts();
8116 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8117 OOK = OCE->getOperator();
8118 OOLoc = OCE->getOperatorLoc();
8119 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8120 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8121 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8122 OOK = MCE->getMethodDecl()
8123 ->getNameInfo()
8124 .getName()
8125 .getCXXOverloadedOperator();
8126 OOLoc = MCE->getCallee()->getExprLoc();
8127 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8128 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8129 } else {
8130 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8131 continue;
8132 }
8133 DE = dyn_cast<DeclRefExpr>(LHS);
8134 if (!DE) {
8135 Diag(LHS->getExprLoc(),
8136 diag::err_omp_depend_sink_expected_loop_iteration)
8137 << DSAStack->getParentLoopControlVariable(
8138 DepCounter.getZExtValue());
8139 continue;
8140 }
8141 if (OOK != OO_Plus && OOK != OO_Minus) {
8142 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8143 continue;
8144 }
8145 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8146 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8147 if (Res.isInvalid())
8148 continue;
8149 }
8150 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8151 if (!CurContext->isDependentContext() &&
8152 DSAStack->getParentOrderedRegionParam() &&
8153 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8154 Diag(DE->getExprLoc(),
8155 diag::err_omp_depend_sink_expected_loop_iteration)
8156 << DSAStack->getParentLoopControlVariable(
8157 DepCounter.getZExtValue());
8158 continue;
8159 }
8160 } else {
8161 // OpenMP [2.11.1.1, Restrictions, p.3]
8162 // A variable that is part of another variable (such as a field of a
8163 // structure) but is not an array element or an array section cannot
8164 // appear in a depend clause.
8165 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8166 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8167 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8168 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8169 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8170 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8171 !ASE->getBase()->getType()->isArrayType())) {
8172 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8173 << RefExpr->getSourceRange();
8174 continue;
8175 }
8176 }
8177
8178 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8179 }
8180
8181 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8182 TotalDepCount > VarList.size() &&
8183 DSAStack->getParentOrderedRegionParam()) {
8184 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8185 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8186 }
8187 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8188 Vars.empty())
8189 return nullptr;
8190 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008191
8192 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8193 DepLoc, ColonLoc, Vars);
8194}
Michael Wonge710d542015-08-07 16:16:36 +00008195
8196OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8197 SourceLocation LParenLoc,
8198 SourceLocation EndLoc) {
8199 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008200
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008201 // OpenMP [2.9.1, Restrictions]
8202 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008203 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8204 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008205 return nullptr;
8206
Michael Wonge710d542015-08-07 16:16:36 +00008207 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8208}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008209
8210static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8211 DSAStackTy *Stack, CXXRecordDecl *RD) {
8212 if (!RD || RD->isInvalidDecl())
8213 return true;
8214
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008215 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8216 if (auto *CTD = CTSD->getSpecializedTemplate())
8217 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008218 auto QTy = SemaRef.Context.getRecordType(RD);
8219 if (RD->isDynamicClass()) {
8220 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8221 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8222 return false;
8223 }
8224 auto *DC = RD;
8225 bool IsCorrect = true;
8226 for (auto *I : DC->decls()) {
8227 if (I) {
8228 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8229 if (MD->isStatic()) {
8230 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8231 SemaRef.Diag(MD->getLocation(),
8232 diag::note_omp_static_member_in_target);
8233 IsCorrect = false;
8234 }
8235 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8236 if (VD->isStaticDataMember()) {
8237 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8238 SemaRef.Diag(VD->getLocation(),
8239 diag::note_omp_static_member_in_target);
8240 IsCorrect = false;
8241 }
8242 }
8243 }
8244 }
8245
8246 for (auto &I : RD->bases()) {
8247 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8248 I.getType()->getAsCXXRecordDecl()))
8249 IsCorrect = false;
8250 }
8251 return IsCorrect;
8252}
8253
8254static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8255 DSAStackTy *Stack, QualType QTy) {
8256 NamedDecl *ND;
8257 if (QTy->isIncompleteType(&ND)) {
8258 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8259 return false;
8260 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8261 if (!RD->isInvalidDecl() &&
8262 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8263 return false;
8264 }
8265 return true;
8266}
8267
8268OMPClause *Sema::ActOnOpenMPMapClause(
8269 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8270 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8271 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8272 SmallVector<Expr *, 4> Vars;
8273
8274 for (auto &RE : VarList) {
8275 assert(RE && "Null expr in omp map");
8276 if (isa<DependentScopeDeclRefExpr>(RE)) {
8277 // It will be analyzed later.
8278 Vars.push_back(RE);
8279 continue;
8280 }
8281 SourceLocation ELoc = RE->getExprLoc();
8282
8283 // OpenMP [2.14.5, Restrictions]
8284 // A variable that is part of another variable (such as field of a
8285 // structure) but is not an array element or an array section cannot appear
8286 // in a map clause.
8287 auto *VE = RE->IgnoreParenLValueCasts();
8288
8289 if (VE->isValueDependent() || VE->isTypeDependent() ||
8290 VE->isInstantiationDependent() ||
8291 VE->containsUnexpandedParameterPack()) {
8292 // It will be analyzed later.
8293 Vars.push_back(RE);
8294 continue;
8295 }
8296
8297 auto *SimpleExpr = RE->IgnoreParenCasts();
8298 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8299 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8300 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8301
8302 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8303 (!OASE && !ASE && !DE) ||
8304 (DE && !isa<VarDecl>(DE->getDecl())) ||
8305 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8306 !ASE->getBase()->getType()->isArrayType())) {
8307 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8308 << RE->getSourceRange();
8309 continue;
8310 }
8311
8312 Decl *D = nullptr;
8313 if (DE) {
8314 D = DE->getDecl();
8315 } else if (ASE) {
8316 auto *B = ASE->getBase()->IgnoreParenCasts();
8317 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8318 } else if (OASE) {
8319 auto *B = OASE->getBase();
8320 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8321 }
8322 assert(D && "Null decl on map clause.");
8323 auto *VD = cast<VarDecl>(D);
8324
8325 // OpenMP [2.14.5, Restrictions, p.8]
8326 // threadprivate variables cannot appear in a map clause.
8327 if (DSAStack->isThreadPrivate(VD)) {
8328 auto DVar = DSAStack->getTopDSA(VD, false);
8329 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8330 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8331 continue;
8332 }
8333
8334 // OpenMP [2.14.5, Restrictions, p.2]
8335 // At most one list item can be an array item derived from a given variable
8336 // in map clauses of the same construct.
8337 // OpenMP [2.14.5, Restrictions, p.3]
8338 // List items of map clauses in the same construct must not share original
8339 // storage.
8340 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8341 // A variable for which the type is pointer, reference to array, or
8342 // reference to pointer and an array section derived from that variable
8343 // must not appear as list items of map clauses of the same construct.
8344 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8345 if (MI.RefExpr) {
8346 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8347 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8348 << MI.RefExpr->getSourceRange();
8349 continue;
8350 }
8351
8352 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8353 // A variable for which the type is pointer, reference to array, or
8354 // reference to pointer must not appear as a list item if the enclosing
8355 // device data environment already contains an array section derived from
8356 // that variable.
8357 // An array section derived from a variable for which the type is pointer,
8358 // reference to array, or reference to pointer must not appear as a list
8359 // item if the enclosing device data environment already contains that
8360 // variable.
8361 QualType Type = VD->getType();
8362 MI = DSAStack->getMapInfoForVar(VD);
8363 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8364 isa<DeclRefExpr>(VE)) &&
8365 (Type->isPointerType() || Type->isReferenceType())) {
8366 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8367 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8368 << MI.RefExpr->getSourceRange();
8369 continue;
8370 }
8371
8372 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8373 // A list item must have a mappable type.
8374 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8375 DSAStack, Type))
8376 continue;
8377
8378 Vars.push_back(RE);
8379 MI.RefExpr = RE;
8380 DSAStack->addMapInfoForVar(VD, MI);
8381 }
8382 if (Vars.empty())
8383 return nullptr;
8384
8385 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8386 MapTypeModifier, MapType, MapLoc);
8387}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008388
8389OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8390 SourceLocation StartLoc,
8391 SourceLocation LParenLoc,
8392 SourceLocation EndLoc) {
8393 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008394
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008395 // OpenMP [teams Constrcut, Restrictions]
8396 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008397 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8398 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008399 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008400
8401 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8402}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008403
8404OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8405 SourceLocation StartLoc,
8406 SourceLocation LParenLoc,
8407 SourceLocation EndLoc) {
8408 Expr *ValExpr = ThreadLimit;
8409
8410 // OpenMP [teams Constrcut, Restrictions]
8411 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008412 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8413 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008414 return nullptr;
8415
8416 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8417 EndLoc);
8418}
Alexey Bataeva0569352015-12-01 10:17:31 +00008419
8420OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8421 SourceLocation StartLoc,
8422 SourceLocation LParenLoc,
8423 SourceLocation EndLoc) {
8424 Expr *ValExpr = Priority;
8425
8426 // OpenMP [2.9.1, task Constrcut]
8427 // The priority-value is a non-negative numerical scalar expression.
8428 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8429 /*StrictlyPositive=*/false))
8430 return nullptr;
8431
8432 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8433}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008434
8435OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8436 SourceLocation StartLoc,
8437 SourceLocation LParenLoc,
8438 SourceLocation EndLoc) {
8439 Expr *ValExpr = Grainsize;
8440
8441 // OpenMP [2.9.2, taskloop Constrcut]
8442 // The parameter of the grainsize clause must be a positive integer
8443 // expression.
8444 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8445 /*StrictlyPositive=*/true))
8446 return nullptr;
8447
8448 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8449}
Alexey Bataev382967a2015-12-08 12:06:20 +00008450
8451OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8452 SourceLocation StartLoc,
8453 SourceLocation LParenLoc,
8454 SourceLocation EndLoc) {
8455 Expr *ValExpr = NumTasks;
8456
8457 // OpenMP [2.9.2, taskloop Constrcut]
8458 // The parameter of the num_tasks clause must be a positive integer
8459 // expression.
8460 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8461 /*StrictlyPositive=*/true))
8462 return nullptr;
8463
8464 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8465}
8466
Alexey Bataev28c75412015-12-15 08:19:24 +00008467OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8468 SourceLocation LParenLoc,
8469 SourceLocation EndLoc) {
8470 // OpenMP [2.13.2, critical construct, Description]
8471 // ... where hint-expression is an integer constant expression that evaluates
8472 // to a valid lock hint.
8473 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8474 if (HintExpr.isInvalid())
8475 return nullptr;
8476 return new (Context)
8477 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8478}
8479