blob: 904556cbc638e7328f62fc9c659507f0a8efc7a8 [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 {
Alexey Bataevf29276e2014-06-18 04:14:57 +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
Alexey Bataev758e55e2013-09-06 18:03:48 +000080private:
81 struct DSAInfo {
82 OpenMPClauseKind Attributes;
83 DeclRefExpr *RefExpr;
84 };
85 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000086 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000087 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088
89 struct SharingMapTy {
90 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000092 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000094 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095 OpenMPDirectiveKind Directive;
96 DeclarationNameInfo DirectiveName;
97 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000099 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000100 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000101 bool CancelRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000102 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000103 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000104 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000106 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000108 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000109 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000110 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000111 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000112 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000113 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000114 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000115 };
116
117 typedef SmallVector<SharingMapTy, 64> StackTy;
118
119 /// \brief Stack of used declaration and their data-sharing attributes.
120 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000121 /// \brief true, if check for DSA must be from parent directive, false, if
122 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000123 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000124 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000125 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126
127 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
128
129 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000130
131 /// \brief Checks if the variable is a local for OpenMP region.
132 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000133
Alexey Bataev758e55e2013-09-06 18:03:48 +0000134public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000135 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000136 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
137 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000138
Alexey Bataevaac108a2015-06-23 04:51:00 +0000139 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
140 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000142 bool isForceVarCapturing() const { return ForceCapturing; }
143 void setForceVarCapturing(bool V) { ForceCapturing = V; }
144
Alexey Bataev758e55e2013-09-06 18:03:48 +0000145 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000146 Scope *CurScope, SourceLocation Loc) {
147 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
148 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000149 }
150
151 void pop() {
152 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
153 Stack.pop_back();
154 }
155
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000156 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000157 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000158 /// for diagnostics.
159 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
160
Alexey Bataev9c821032015-04-30 04:23:23 +0000161 /// \brief Register specified variable as loop control variable.
162 void addLoopControlVariable(VarDecl *D);
163 /// \brief Check if the specified variable is a loop control variable for
164 /// current region.
165 bool isLoopControlVariable(VarDecl *D);
166
Alexey Bataev758e55e2013-09-06 18:03:48 +0000167 /// \brief Adds explicit data sharing attribute to the specified declaration.
168 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
169
Alexey Bataev758e55e2013-09-06 18:03:48 +0000170 /// \brief Returns data sharing attributes from top of the stack for the
171 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000172 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000174 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000175 /// \brief Checks if the specified variables has data-sharing attributes which
176 /// match specified \a CPred predicate in any directive which matches \a DPred
177 /// predicate.
178 template <class ClausesPredicate, class DirectivesPredicate>
179 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000180 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000181 /// \brief Checks if the specified variables has data-sharing attributes which
182 /// match specified \a CPred predicate in any innermost directive which
183 /// matches \a DPred predicate.
184 template <class ClausesPredicate, class DirectivesPredicate>
185 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000186 DirectivesPredicate DPred,
187 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000188 /// \brief Checks if the specified variables has explicit data-sharing
189 /// attributes which match specified \a CPred predicate at the specified
190 /// OpenMP region.
191 bool hasExplicitDSA(VarDecl *D,
192 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
193 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000194 /// \brief Finds a directive which matches specified \a DPred predicate.
195 template <class NamedDirectivesPredicate>
196 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000197
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198 /// \brief Returns currently analyzed directive.
199 OpenMPDirectiveKind getCurrentDirective() const {
200 return Stack.back().Directive;
201 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000202 /// \brief Returns parent directive.
203 OpenMPDirectiveKind getParentDirective() const {
204 if (Stack.size() > 2)
205 return Stack[Stack.size() - 2].Directive;
206 return OMPD_unknown;
207 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000208
209 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000210 void setDefaultDSANone(SourceLocation Loc) {
211 Stack.back().DefaultAttr = DSA_none;
212 Stack.back().DefaultAttrLoc = Loc;
213 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000214 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000215 void setDefaultDSAShared(SourceLocation Loc) {
216 Stack.back().DefaultAttr = DSA_shared;
217 Stack.back().DefaultAttrLoc = Loc;
218 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219
220 DefaultDataSharingAttributes getDefaultDSA() const {
221 return Stack.back().DefaultAttr;
222 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000223 SourceLocation getDefaultDSALocation() const {
224 return Stack.back().DefaultAttrLoc;
225 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000226
Alexey Bataevf29276e2014-06-18 04:14:57 +0000227 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000228 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000229 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000230 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000231 }
232
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000233 /// \brief Marks current region as ordered (it has an 'ordered' clause).
234 void setOrderedRegion(bool IsOrdered = true) {
235 Stack.back().OrderedRegion = IsOrdered;
236 }
237 /// \brief Returns true, if parent region is ordered (has associated
238 /// 'ordered' clause), false - otherwise.
239 bool isParentOrderedRegion() const {
240 if (Stack.size() > 2)
241 return Stack[Stack.size() - 2].OrderedRegion;
242 return false;
243 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000244 /// \brief Marks current region as nowait (it has a 'nowait' clause).
245 void setNowaitRegion(bool IsNowait = true) {
246 Stack.back().NowaitRegion = IsNowait;
247 }
248 /// \brief Returns true, if parent region is nowait (has associated
249 /// 'nowait' clause), false - otherwise.
250 bool isParentNowaitRegion() const {
251 if (Stack.size() > 2)
252 return Stack[Stack.size() - 2].NowaitRegion;
253 return false;
254 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000255 /// \brief Marks parent region as cancel region.
256 void setParentCancelRegion(bool Cancel = true) {
257 if (Stack.size() > 2)
258 Stack[Stack.size() - 2].CancelRegion =
259 Stack[Stack.size() - 2].CancelRegion || Cancel;
260 }
261 /// \brief Return true if current region has inner cancel construct.
262 bool isCancelRegion() const {
263 return Stack.back().CancelRegion;
264 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000265
Alexey Bataev9c821032015-04-30 04:23:23 +0000266 /// \brief Set collapse value for the region.
267 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
268 /// \brief Return collapse value for region.
269 unsigned getCollapseNumber() const {
270 return Stack.back().CollapseNumber;
271 }
272
Alexey Bataev13314bf2014-10-09 04:18:56 +0000273 /// \brief Marks current target region as one with closely nested teams
274 /// region.
275 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
276 if (Stack.size() > 2)
277 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
278 }
279 /// \brief Returns true, if current region has closely nested teams region.
280 bool hasInnerTeamsRegion() const {
281 return getInnerTeamsRegionLoc().isValid();
282 }
283 /// \brief Returns location of the nested teams region (if any).
284 SourceLocation getInnerTeamsRegionLoc() const {
285 if (Stack.size() > 1)
286 return Stack.back().InnerTeamsRegionLoc;
287 return SourceLocation();
288 }
289
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000290 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000291 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000292 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000294bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
295 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000296 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000297}
Alexey Bataeved09d242014-05-28 05:53:51 +0000298} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000299
300DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
301 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000302 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000303 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000304 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000305 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
306 // in a region but not in construct]
307 // File-scope or namespace-scope variables referenced in called routines
308 // in the region are shared unless they appear in a threadprivate
309 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000310 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000311 DVar.CKind = OMPC_shared;
312
313 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
314 // in a region but not in construct]
315 // Variables with static storage duration that are declared in called
316 // routines in the region are shared.
317 if (D->hasGlobalStorage())
318 DVar.CKind = OMPC_shared;
319
Alexey Bataev758e55e2013-09-06 18:03:48 +0000320 return DVar;
321 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000322
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
325 // in a Construct, C/C++, predetermined, p.1]
326 // Variables with automatic storage duration that are declared in a scope
327 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000328 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
329 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
330 DVar.CKind = OMPC_private;
331 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000332 }
333
Alexey Bataev758e55e2013-09-06 18:03:48 +0000334 // Explicitly specified attributes and local variables with predetermined
335 // attributes.
336 if (Iter->SharingMap.count(D)) {
337 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
338 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000339 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 return DVar;
341 }
342
343 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
344 // in a Construct, C/C++, implicitly determined, p.1]
345 // In a parallel or task construct, the data-sharing attributes of these
346 // variables are determined by the default clause, if present.
347 switch (Iter->DefaultAttr) {
348 case DSA_shared:
349 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000350 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000351 return DVar;
352 case DSA_none:
353 return DVar;
354 case DSA_unspecified:
355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
356 // in a Construct, implicitly determined, p.2]
357 // In a parallel construct, if no default clause is present, these
358 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000359 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000360 if (isOpenMPParallelDirective(DVar.DKind) ||
361 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000362 DVar.CKind = OMPC_shared;
363 return DVar;
364 }
365
366 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
367 // in a Construct, implicitly determined, p.4]
368 // In a task construct, if no default clause is present, a variable that in
369 // the enclosing context is determined to be shared by all implicit tasks
370 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000371 if (DVar.DKind == OMPD_task) {
372 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000373 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000375 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
376 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377 // in a Construct, implicitly determined, p.6]
378 // In a task construct, if no default clause is present, a variable
379 // whose data-sharing attribute is not determined by the rules above is
380 // firstprivate.
381 DVarTemp = getDSA(I, D);
382 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000383 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000384 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000385 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 return DVar;
387 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000388 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000389 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000390 }
391 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000393 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000394 return DVar;
395 }
396 }
397 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
398 // in a Construct, implicitly determined, p.3]
399 // For constructs other than task, if no default clause is present, these
400 // variables inherit their data-sharing attributes from the enclosing
401 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000402 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403}
404
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000405DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
406 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000407 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000408 auto It = Stack.back().AlignedMap.find(D);
409 if (It == Stack.back().AlignedMap.end()) {
410 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
411 Stack.back().AlignedMap[D] = NewDE;
412 return nullptr;
413 } else {
414 assert(It->second && "Unexpected nullptr expr in the aligned map");
415 return It->second;
416 }
417 return nullptr;
418}
419
Alexey Bataev9c821032015-04-30 04:23:23 +0000420void DSAStackTy::addLoopControlVariable(VarDecl *D) {
421 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
422 D = D->getCanonicalDecl();
423 Stack.back().LCVSet.insert(D);
424}
425
426bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
427 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
428 D = D->getCanonicalDecl();
429 return Stack.back().LCVSet.count(D) > 0;
430}
431
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000433 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000434 if (A == OMPC_threadprivate) {
435 Stack[0].SharingMap[D].Attributes = A;
436 Stack[0].SharingMap[D].RefExpr = E;
437 } else {
438 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
439 Stack.back().SharingMap[D].Attributes = A;
440 Stack.back().SharingMap[D].RefExpr = E;
441 }
442}
443
Alexey Bataeved09d242014-05-28 05:53:51 +0000444bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000445 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000446 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000447 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000448 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000449 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000450 ++I;
451 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000452 if (I == E)
453 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000454 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000455 Scope *CurScope = getCurScope();
456 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000457 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000458 }
459 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000460 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000461 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000462}
463
Alexey Bataev39f915b82015-05-08 10:41:21 +0000464/// \brief Build a variable declaration for OpenMP loop iteration variable.
465static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000466 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000467 DeclContext *DC = SemaRef.CurContext;
468 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
469 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
470 VarDecl *Decl =
471 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000472 if (Attrs) {
473 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
474 I != E; ++I)
475 Decl->addAttr(*I);
476 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000477 Decl->setImplicit();
478 return Decl;
479}
480
481static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
482 SourceLocation Loc,
483 bool RefersToCapture = false) {
484 D->setReferenced();
485 D->markUsed(S.Context);
486 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
487 SourceLocation(), D, RefersToCapture, Loc, Ty,
488 VK_LValue);
489}
490
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000492 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 DSAVarData DVar;
494
495 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
496 // in a Construct, C/C++, predetermined, p.1]
497 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000498 if ((D->getTLSKind() != VarDecl::TLS_None &&
499 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
500 SemaRef.getLangOpts().OpenMPUseTLS &&
501 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000502 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
503 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000504 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
505 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000506 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000507 }
508 if (Stack[0].SharingMap.count(D)) {
509 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
510 DVar.CKind = OMPC_threadprivate;
511 return DVar;
512 }
513
514 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
515 // in a Construct, C/C++, predetermined, p.1]
516 // Variables with automatic storage duration that are declared in a scope
517 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000518 OpenMPDirectiveKind Kind =
519 FromParent ? getParentDirective() : getCurrentDirective();
520 auto StartI = std::next(Stack.rbegin());
521 auto EndI = std::prev(Stack.rend());
522 if (FromParent && StartI != EndI) {
523 StartI = std::next(StartI);
524 }
525 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000526 if (isOpenMPLocal(D, StartI) &&
527 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
528 D->getStorageClass() == SC_None)) ||
529 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000530 DVar.CKind = OMPC_private;
531 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000532 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000533
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000534 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
535 // in a Construct, C/C++, predetermined, p.4]
536 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000537 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
538 // in a Construct, C/C++, predetermined, p.7]
539 // Variables with static storage duration that are declared in a scope
540 // inside the construct are shared.
Kelvin Li4eea8c62015-09-15 18:56:58 +0000541 if (D->isStaticDataMember()) {
Alexey Bataev42971a32015-01-20 07:03:46 +0000542 DSAVarData DVarTemp =
543 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
544 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
545 return DVar;
546
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000547 DVar.CKind = OMPC_shared;
548 return DVar;
549 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000550 }
551
552 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000553 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
554 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
556 // in a Construct, C/C++, predetermined, p.6]
557 // Variables with const qualified type having no mutable member are
558 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000559 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000560 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000561 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000562 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000563 // Variables with const-qualified type having no mutable member may be
564 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000565 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
566 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000567 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
568 return DVar;
569
Alexey Bataev758e55e2013-09-06 18:03:48 +0000570 DVar.CKind = OMPC_shared;
571 return DVar;
572 }
573
Alexey Bataev758e55e2013-09-06 18:03:48 +0000574 // Explicitly specified attributes and local variables with predetermined
575 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 auto I = std::prev(StartI);
577 if (I->SharingMap.count(D)) {
578 DVar.RefExpr = I->SharingMap[D].RefExpr;
579 DVar.CKind = I->SharingMap[D].Attributes;
580 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000581 }
582
583 return DVar;
584}
585
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000586DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000587 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000588 auto StartI = Stack.rbegin();
589 auto EndI = std::prev(Stack.rend());
590 if (FromParent && StartI != EndI) {
591 StartI = std::next(StartI);
592 }
593 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594}
595
Alexey Bataevf29276e2014-06-18 04:14:57 +0000596template <class ClausesPredicate, class DirectivesPredicate>
597DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000598 DirectivesPredicate DPred,
599 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000600 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000601 auto StartI = std::next(Stack.rbegin());
602 auto EndI = std::prev(Stack.rend());
603 if (FromParent && StartI != EndI) {
604 StartI = std::next(StartI);
605 }
606 for (auto I = StartI, EE = EndI; I != EE; ++I) {
607 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000608 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000609 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000610 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000611 return DVar;
612 }
613 return DSAVarData();
614}
615
Alexey Bataevf29276e2014-06-18 04:14:57 +0000616template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000617DSAStackTy::DSAVarData
618DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
619 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000620 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000621 auto StartI = std::next(Stack.rbegin());
622 auto EndI = std::prev(Stack.rend());
623 if (FromParent && StartI != EndI) {
624 StartI = std::next(StartI);
625 }
626 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000627 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000628 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000629 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000630 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000631 return DVar;
632 return DSAVarData();
633 }
634 return DSAVarData();
635}
636
Alexey Bataevaac108a2015-06-23 04:51:00 +0000637bool DSAStackTy::hasExplicitDSA(
638 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
639 unsigned Level) {
640 if (CPred(ClauseKindMode))
641 return true;
642 if (isClauseParsingMode())
643 ++Level;
644 D = D->getCanonicalDecl();
645 auto StartI = Stack.rbegin();
646 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000647 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000648 return false;
649 std::advance(StartI, Level);
650 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
651 CPred(StartI->SharingMap[D].Attributes);
652}
653
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000654template <class NamedDirectivesPredicate>
655bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
656 auto StartI = std::next(Stack.rbegin());
657 auto EndI = std::prev(Stack.rend());
658 if (FromParent && StartI != EndI) {
659 StartI = std::next(StartI);
660 }
661 for (auto I = StartI, EE = EndI; I != EE; ++I) {
662 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
663 return true;
664 }
665 return false;
666}
667
Alexey Bataev758e55e2013-09-06 18:03:48 +0000668void Sema::InitDataSharingAttributesStack() {
669 VarDataSharingAttributesStack = new DSAStackTy(*this);
670}
671
672#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
673
Alexey Bataevf841bd92014-12-16 07:00:22 +0000674bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
675 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000676 VD = VD->getCanonicalDecl();
Alexey Bataev48977c32015-08-04 08:10:48 +0000677 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
678 (!DSAStack->isClauseParsingMode() ||
679 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000680 if (DSAStack->isLoopControlVariable(VD) ||
681 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000682 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
683 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000684 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000685 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000686 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
687 return true;
688 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000689 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000690 return DVarPrivate.CKind != OMPC_unknown;
691 }
692 return false;
693}
694
Alexey Bataevaac108a2015-06-23 04:51:00 +0000695bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
696 assert(LangOpts.OpenMP && "OpenMP is not allowed");
697 return DSAStack->hasExplicitDSA(
698 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
699}
700
Alexey Bataeved09d242014-05-28 05:53:51 +0000701void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702
703void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
704 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000705 Scope *CurScope, SourceLocation Loc) {
706 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000707 PushExpressionEvaluationContext(PotentiallyEvaluated);
708}
709
Alexey Bataevaac108a2015-06-23 04:51:00 +0000710void Sema::StartOpenMPClause(OpenMPClauseKind K) {
711 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000712}
713
Alexey Bataevaac108a2015-06-23 04:51:00 +0000714void Sema::EndOpenMPClause() {
715 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000716}
717
Alexey Bataev758e55e2013-09-06 18:03:48 +0000718void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000719 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
720 // A variable of class type (or array thereof) that appears in a lastprivate
721 // clause requires an accessible, unambiguous default constructor for the
722 // class type, unless the list item is also specified in a firstprivate
723 // clause.
724 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000725 for (auto *C : D->clauses()) {
726 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
727 SmallVector<Expr *, 8> PrivateCopies;
728 for (auto *DE : Clause->varlists()) {
729 if (DE->isValueDependent() || DE->isTypeDependent()) {
730 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000731 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000732 }
733 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000734 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000736 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000737 // Generate helper private variable and initialize it with the
738 // default value. The address of the original variable is replaced
739 // by the address of the new private variable in CodeGen. This new
740 // variable is not added to IdResolver, so the code in the OpenMP
741 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000742 auto *VDPrivate = buildVarDecl(
743 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
744 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000745 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
746 if (VDPrivate->isInvalidDecl())
747 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000748 PrivateCopies.push_back(buildDeclRefExpr(
749 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000750 } else {
751 // The variable is also a firstprivate, so initialization sequence
752 // for private copy is generated already.
753 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000754 }
755 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000756 // Set initializers to private copies if no errors were found.
757 if (PrivateCopies.size() == Clause->varlist_size()) {
758 Clause->setPrivateCopies(PrivateCopies);
759 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000760 }
761 }
762 }
763
Alexey Bataev758e55e2013-09-06 18:03:48 +0000764 DSAStack->pop();
765 DiscardCleanupsInEvaluationContext();
766 PopExpressionEvaluationContext();
767}
768
Alexander Musman3276a272015-03-21 10:12:56 +0000769static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
770 Expr *NumIterations, Sema &SemaRef,
771 Scope *S);
772
Alexey Bataeva769e072013-03-22 06:34:35 +0000773namespace {
774
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000775class VarDeclFilterCCC : public CorrectionCandidateCallback {
776private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000777 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000778
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000779public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000780 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000781 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000782 NamedDecl *ND = Candidate.getCorrectionDecl();
783 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
784 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000785 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
786 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000787 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000788 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000789 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000790};
Alexey Bataeved09d242014-05-28 05:53:51 +0000791} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000792
793ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
794 CXXScopeSpec &ScopeSpec,
795 const DeclarationNameInfo &Id) {
796 LookupResult Lookup(*this, Id, LookupOrdinaryName);
797 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
798
799 if (Lookup.isAmbiguous())
800 return ExprError();
801
802 VarDecl *VD;
803 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000804 if (TypoCorrection Corrected = CorrectTypo(
805 Id, LookupOrdinaryName, CurScope, nullptr,
806 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000807 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000808 PDiag(Lookup.empty()
809 ? diag::err_undeclared_var_use_suggest
810 : diag::err_omp_expected_var_arg_suggest)
811 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000812 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000813 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000814 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
815 : diag::err_omp_expected_var_arg)
816 << Id.getName();
817 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000818 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000819 } else {
820 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000821 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000822 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
823 return ExprError();
824 }
825 }
826 Lookup.suppressDiagnostics();
827
828 // OpenMP [2.9.2, Syntax, C/C++]
829 // Variables must be file-scope, namespace-scope, or static block-scope.
830 if (!VD->hasGlobalStorage()) {
831 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000832 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
833 bool IsDecl =
834 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000835 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000836 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
837 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000838 return ExprError();
839 }
840
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000841 VarDecl *CanonicalVD = VD->getCanonicalDecl();
842 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000843 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
844 // A threadprivate directive for file-scope variables must appear outside
845 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000846 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
847 !getCurLexicalContext()->isTranslationUnit()) {
848 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000849 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
850 bool IsDecl =
851 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
852 Diag(VD->getLocation(),
853 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
854 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000855 return ExprError();
856 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000857 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
858 // A threadprivate directive for static class member variables must appear
859 // in the class definition, in the same scope in which the member
860 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000861 if (CanonicalVD->isStaticDataMember() &&
862 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
863 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000864 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
865 bool IsDecl =
866 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
867 Diag(VD->getLocation(),
868 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
869 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000870 return ExprError();
871 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000872 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
873 // A threadprivate directive for namespace-scope variables must appear
874 // outside any definition or declaration other than the namespace
875 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000876 if (CanonicalVD->getDeclContext()->isNamespace() &&
877 (!getCurLexicalContext()->isFileContext() ||
878 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
879 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000880 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
881 bool IsDecl =
882 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
883 Diag(VD->getLocation(),
884 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
885 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000886 return ExprError();
887 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000888 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
889 // A threadprivate directive for static block-scope variables must appear
890 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000891 if (CanonicalVD->isStaticLocal() && CurScope &&
892 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000893 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000894 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
895 bool IsDecl =
896 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
897 Diag(VD->getLocation(),
898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
899 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000900 return ExprError();
901 }
902
903 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
904 // A threadprivate directive must lexically precede all references to any
905 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000906 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000907 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000908 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000909 return ExprError();
910 }
911
912 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000913 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000914 return DE;
915}
916
Alexey Bataeved09d242014-05-28 05:53:51 +0000917Sema::DeclGroupPtrTy
918Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
919 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000920 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000921 CurContext->addDecl(D);
922 return DeclGroupPtrTy::make(DeclGroupRef(D));
923 }
924 return DeclGroupPtrTy();
925}
926
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000927namespace {
928class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
929 Sema &SemaRef;
930
931public:
932 bool VisitDeclRefExpr(const DeclRefExpr *E) {
933 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
934 if (VD->hasLocalStorage()) {
935 SemaRef.Diag(E->getLocStart(),
936 diag::err_omp_local_var_in_threadprivate_init)
937 << E->getSourceRange();
938 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
939 << VD << VD->getSourceRange();
940 return true;
941 }
942 }
943 return false;
944 }
945 bool VisitStmt(const Stmt *S) {
946 for (auto Child : S->children()) {
947 if (Child && Visit(Child))
948 return true;
949 }
950 return false;
951 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000952 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000953};
954} // namespace
955
Alexey Bataeved09d242014-05-28 05:53:51 +0000956OMPThreadPrivateDecl *
957Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000958 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000959 for (auto &RefExpr : VarList) {
960 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000961 VarDecl *VD = cast<VarDecl>(DE->getDecl());
962 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000963
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000964 QualType QType = VD->getType();
965 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
966 // It will be analyzed later.
967 Vars.push_back(DE);
968 continue;
969 }
970
Alexey Bataeva769e072013-03-22 06:34:35 +0000971 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
972 // A threadprivate variable must not have an incomplete type.
973 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000974 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000975 continue;
976 }
977
978 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
979 // A threadprivate variable must not have a reference type.
980 if (VD->getType()->isReferenceType()) {
981 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000982 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
983 bool IsDecl =
984 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
985 Diag(VD->getLocation(),
986 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
987 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000988 continue;
989 }
990
Samuel Antaof8b50122015-07-13 22:54:53 +0000991 // Check if this is a TLS variable. If TLS is not being supported, produce
992 // the corresponding diagnostic.
993 if ((VD->getTLSKind() != VarDecl::TLS_None &&
994 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
995 getLangOpts().OpenMPUseTLS &&
996 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000997 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
998 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000999 Diag(ILoc, diag::err_omp_var_thread_local)
1000 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001001 bool IsDecl =
1002 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1003 Diag(VD->getLocation(),
1004 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1005 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001006 continue;
1007 }
1008
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001009 // Check if initial value of threadprivate variable reference variable with
1010 // local storage (it is not supported by runtime).
1011 if (auto Init = VD->getAnyInitializer()) {
1012 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001013 if (Checker.Visit(Init))
1014 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001015 }
1016
Alexey Bataeved09d242014-05-28 05:53:51 +00001017 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001018 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001019 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1020 Context, SourceRange(Loc, Loc)));
1021 if (auto *ML = Context.getASTMutationListener())
1022 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001023 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001024 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001025 if (!Vars.empty()) {
1026 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1027 Vars);
1028 D->setAccess(AS_public);
1029 }
1030 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001031}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001032
Alexey Bataev7ff55242014-06-19 09:13:45 +00001033static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1034 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1035 bool IsLoopIterVar = false) {
1036 if (DVar.RefExpr) {
1037 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1038 << getOpenMPClauseName(DVar.CKind);
1039 return;
1040 }
1041 enum {
1042 PDSA_StaticMemberShared,
1043 PDSA_StaticLocalVarShared,
1044 PDSA_LoopIterVarPrivate,
1045 PDSA_LoopIterVarLinear,
1046 PDSA_LoopIterVarLastprivate,
1047 PDSA_ConstVarShared,
1048 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001049 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001050 PDSA_LocalVarPrivate,
1051 PDSA_Implicit
1052 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001053 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001054 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001055 if (IsLoopIterVar) {
1056 if (DVar.CKind == OMPC_private)
1057 Reason = PDSA_LoopIterVarPrivate;
1058 else if (DVar.CKind == OMPC_lastprivate)
1059 Reason = PDSA_LoopIterVarLastprivate;
1060 else
1061 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001062 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1063 Reason = PDSA_TaskVarFirstprivate;
1064 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001065 } else if (VD->isStaticLocal())
1066 Reason = PDSA_StaticLocalVarShared;
1067 else if (VD->isStaticDataMember())
1068 Reason = PDSA_StaticMemberShared;
1069 else if (VD->isFileVarDecl())
1070 Reason = PDSA_GlobalVarShared;
1071 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1072 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001073 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001074 ReportHint = true;
1075 Reason = PDSA_LocalVarPrivate;
1076 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001077 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001078 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001079 << Reason << ReportHint
1080 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1081 } else if (DVar.ImplicitDSALoc.isValid()) {
1082 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1083 << getOpenMPClauseName(DVar.CKind);
1084 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001085}
1086
Alexey Bataev758e55e2013-09-06 18:03:48 +00001087namespace {
1088class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1089 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001090 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001091 bool ErrorFound;
1092 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001093 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001094 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001095
Alexey Bataev758e55e2013-09-06 18:03:48 +00001096public:
1097 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001098 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001099 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001100 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1101 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001102
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001103 auto DVar = Stack->getTopDSA(VD, false);
1104 // Check if the variable has explicit DSA set and stop analysis if it so.
1105 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001106
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001107 auto ELoc = E->getExprLoc();
1108 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001109 // The default(none) clause requires that each variable that is referenced
1110 // in the construct, and does not have a predetermined data-sharing
1111 // attribute, must have its data-sharing attribute explicitly determined
1112 // by being listed in a data-sharing attribute clause.
1113 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001114 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001115 VarsWithInheritedDSA.count(VD) == 0) {
1116 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001117 return;
1118 }
1119
1120 // OpenMP [2.9.3.6, Restrictions, p.2]
1121 // A list item that appears in a reduction clause of the innermost
1122 // enclosing worksharing or parallel construct may not be accessed in an
1123 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001124 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001125 [](OpenMPDirectiveKind K) -> bool {
1126 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001127 isOpenMPWorksharingDirective(K) ||
1128 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001129 },
1130 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001131 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1132 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001133 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1134 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001135 return;
1136 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001137
1138 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001139 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001140 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001141 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001142 }
1143 }
1144 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001145 for (auto *C : S->clauses()) {
1146 // Skip analysis of arguments of implicitly defined firstprivate clause
1147 // for task directives.
1148 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1149 for (auto *CC : C->children()) {
1150 if (CC)
1151 Visit(CC);
1152 }
1153 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001154 }
1155 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001156 for (auto *C : S->children()) {
1157 if (C && !isa<OMPExecutableDirective>(C))
1158 Visit(C);
1159 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001160 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001161
1162 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001163 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001164 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1165 return VarsWithInheritedDSA;
1166 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001167
Alexey Bataev7ff55242014-06-19 09:13:45 +00001168 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1169 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001170};
Alexey Bataeved09d242014-05-28 05:53:51 +00001171} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001172
Alexey Bataevbae9a792014-06-27 10:37:06 +00001173void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001174 switch (DKind) {
1175 case OMPD_parallel: {
1176 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001177 QualType KmpInt32PtrTy =
1178 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001179 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001180 std::make_pair(".global_tid.", KmpInt32PtrTy),
1181 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1182 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001183 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001184 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1185 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001186 break;
1187 }
1188 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001189 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001190 std::make_pair(StringRef(), QualType()) // __context with shared vars
1191 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001192 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1193 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001194 break;
1195 }
1196 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001197 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001198 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001199 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001200 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1201 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001202 break;
1203 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001204 case OMPD_for_simd: {
1205 Sema::CapturedParamNameType Params[] = {
1206 std::make_pair(StringRef(), QualType()) // __context with shared vars
1207 };
1208 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1209 Params);
1210 break;
1211 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001212 case OMPD_sections: {
1213 Sema::CapturedParamNameType Params[] = {
1214 std::make_pair(StringRef(), QualType()) // __context with shared vars
1215 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001216 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1217 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001218 break;
1219 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001220 case OMPD_section: {
1221 Sema::CapturedParamNameType Params[] = {
1222 std::make_pair(StringRef(), QualType()) // __context with shared vars
1223 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001224 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1225 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001226 break;
1227 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001228 case OMPD_single: {
1229 Sema::CapturedParamNameType Params[] = {
1230 std::make_pair(StringRef(), QualType()) // __context with shared vars
1231 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001232 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1233 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001234 break;
1235 }
Alexander Musman80c22892014-07-17 08:54:58 +00001236 case OMPD_master: {
1237 Sema::CapturedParamNameType Params[] = {
1238 std::make_pair(StringRef(), QualType()) // __context with shared vars
1239 };
1240 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1241 Params);
1242 break;
1243 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001244 case OMPD_critical: {
1245 Sema::CapturedParamNameType Params[] = {
1246 std::make_pair(StringRef(), QualType()) // __context with shared vars
1247 };
1248 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1249 Params);
1250 break;
1251 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001252 case OMPD_parallel_for: {
1253 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001254 QualType KmpInt32PtrTy =
1255 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001256 Sema::CapturedParamNameType Params[] = {
1257 std::make_pair(".global_tid.", KmpInt32PtrTy),
1258 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1259 std::make_pair(StringRef(), QualType()) // __context with shared vars
1260 };
1261 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1262 Params);
1263 break;
1264 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001265 case OMPD_parallel_for_simd: {
1266 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001267 QualType KmpInt32PtrTy =
1268 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001269 Sema::CapturedParamNameType Params[] = {
1270 std::make_pair(".global_tid.", KmpInt32PtrTy),
1271 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1272 std::make_pair(StringRef(), QualType()) // __context with shared vars
1273 };
1274 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1275 Params);
1276 break;
1277 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001278 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001279 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001280 QualType KmpInt32PtrTy =
1281 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001282 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001283 std::make_pair(".global_tid.", KmpInt32PtrTy),
1284 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001285 std::make_pair(StringRef(), QualType()) // __context with shared vars
1286 };
1287 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1288 Params);
1289 break;
1290 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001292 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001293 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1294 FunctionProtoType::ExtProtoInfo EPI;
1295 EPI.Variadic = true;
1296 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001297 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001298 std::make_pair(".global_tid.", KmpInt32Ty),
1299 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001300 std::make_pair(".privates.",
1301 Context.VoidPtrTy.withConst().withRestrict()),
1302 std::make_pair(
1303 ".copy_fn.",
1304 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001305 std::make_pair(StringRef(), QualType()) // __context with shared vars
1306 };
1307 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1308 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001309 // Mark this captured region as inlined, because we don't use outlined
1310 // function directly.
1311 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1312 AlwaysInlineAttr::CreateImplicit(
1313 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001314 break;
1315 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001316 case OMPD_ordered: {
1317 Sema::CapturedParamNameType Params[] = {
1318 std::make_pair(StringRef(), QualType()) // __context with shared vars
1319 };
1320 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1321 Params);
1322 break;
1323 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001324 case OMPD_atomic: {
1325 Sema::CapturedParamNameType Params[] = {
1326 std::make_pair(StringRef(), QualType()) // __context with shared vars
1327 };
1328 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1329 Params);
1330 break;
1331 }
Michael Wong65f367f2015-07-21 13:44:28 +00001332 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001333 case OMPD_target: {
1334 Sema::CapturedParamNameType Params[] = {
1335 std::make_pair(StringRef(), QualType()) // __context with shared vars
1336 };
1337 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1338 Params);
1339 break;
1340 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001341 case OMPD_teams: {
1342 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001343 QualType KmpInt32PtrTy =
1344 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001345 Sema::CapturedParamNameType Params[] = {
1346 std::make_pair(".global_tid.", KmpInt32PtrTy),
1347 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1348 std::make_pair(StringRef(), QualType()) // __context with shared vars
1349 };
1350 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1351 Params);
1352 break;
1353 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001354 case OMPD_taskgroup: {
1355 Sema::CapturedParamNameType Params[] = {
1356 std::make_pair(StringRef(), QualType()) // __context with shared vars
1357 };
1358 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1359 Params);
1360 break;
1361 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001362 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001363 case OMPD_taskyield:
1364 case OMPD_barrier:
1365 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001366 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001367 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001368 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001369 llvm_unreachable("OpenMP Directive is not allowed");
1370 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001371 llvm_unreachable("Unknown OpenMP directive");
1372 }
1373}
1374
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001375StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1376 ArrayRef<OMPClause *> Clauses) {
1377 if (!S.isUsable()) {
1378 ActOnCapturedRegionError();
1379 return StmtError();
1380 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001381 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001382 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001383 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001384 Clause->getClauseKind() == OMPC_copyprivate ||
1385 (getLangOpts().OpenMPUseTLS &&
1386 getASTContext().getTargetInfo().isTLSSupported() &&
1387 Clause->getClauseKind() == OMPC_copyin)) {
1388 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001389 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001390 for (auto *VarRef : Clause->children()) {
1391 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001392 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001393 }
1394 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001395 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001396 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1397 Clause->getClauseKind() == OMPC_schedule) {
1398 // Mark all variables in private list clauses as used in inner region.
1399 // Required for proper codegen of combined directives.
1400 // TODO: add processing for other clauses.
1401 if (auto *E = cast_or_null<Expr>(
1402 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1403 MarkDeclarationsReferencedInExpr(E);
1404 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001405 }
1406 }
1407 return ActOnCapturedRegionEnd(S.get());
1408}
1409
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001410static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1411 OpenMPDirectiveKind CurrentRegion,
1412 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001413 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001414 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001415 // Allowed nesting of constructs
1416 // +------------------+-----------------+------------------------------------+
1417 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1418 // +------------------+-----------------+------------------------------------+
1419 // | parallel | parallel | * |
1420 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001421 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001422 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001423 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001424 // | parallel | simd | * |
1425 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001426 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001427 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001428 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001429 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001430 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001431 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001432 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001433 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001434 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001435 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001436 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001437 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001438 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001439 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001440 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001441 // | parallel | cancellation | |
1442 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001443 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001444 // +------------------+-----------------+------------------------------------+
1445 // | for | parallel | * |
1446 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001447 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001448 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001449 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001450 // | for | simd | * |
1451 // | for | sections | + |
1452 // | for | section | + |
1453 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001454 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001455 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001456 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001457 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001458 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001459 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001460 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001461 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001462 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001463 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001464 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001465 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001466 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001467 // | for | cancellation | |
1468 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001469 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001470 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001471 // | master | parallel | * |
1472 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001473 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001474 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001475 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001476 // | master | simd | * |
1477 // | master | sections | + |
1478 // | master | section | + |
1479 // | master | single | + |
1480 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001481 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001482 // | master |parallel sections| * |
1483 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001484 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001485 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001486 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001487 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001488 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001489 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001490 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001491 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001492 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001493 // | master | cancellation | |
1494 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001495 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001496 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001497 // | critical | parallel | * |
1498 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001499 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001500 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001501 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001502 // | critical | simd | * |
1503 // | critical | sections | + |
1504 // | critical | section | + |
1505 // | critical | single | + |
1506 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001507 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001508 // | critical |parallel sections| * |
1509 // | critical | task | * |
1510 // | critical | taskyield | * |
1511 // | critical | barrier | + |
1512 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001513 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001514 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001515 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001516 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001517 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001518 // | critical | cancellation | |
1519 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001520 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001521 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001522 // | simd | parallel | |
1523 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001524 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001525 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001526 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001527 // | simd | simd | |
1528 // | simd | sections | |
1529 // | simd | section | |
1530 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001531 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001532 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001533 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001534 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001535 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001536 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001537 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001538 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001539 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001540 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001541 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001542 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001543 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001544 // | simd | cancellation | |
1545 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001546 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001547 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001548 // | for simd | parallel | |
1549 // | for simd | for | |
1550 // | for simd | for simd | |
1551 // | for simd | master | |
1552 // | for simd | critical | |
1553 // | for simd | simd | |
1554 // | for simd | sections | |
1555 // | for simd | section | |
1556 // | for simd | single | |
1557 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001558 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001559 // | for simd |parallel sections| |
1560 // | for simd | task | |
1561 // | for simd | taskyield | |
1562 // | for simd | barrier | |
1563 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001564 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001565 // | for simd | flush | |
1566 // | for simd | ordered | |
1567 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001568 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001569 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001570 // | for simd | cancellation | |
1571 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001572 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001573 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001574 // | parallel for simd| parallel | |
1575 // | parallel for simd| for | |
1576 // | parallel for simd| for simd | |
1577 // | parallel for simd| master | |
1578 // | parallel for simd| critical | |
1579 // | parallel for simd| simd | |
1580 // | parallel for simd| sections | |
1581 // | parallel for simd| section | |
1582 // | parallel for simd| single | |
1583 // | parallel for simd| parallel for | |
1584 // | parallel for simd|parallel for simd| |
1585 // | parallel for simd|parallel sections| |
1586 // | parallel for simd| task | |
1587 // | parallel for simd| taskyield | |
1588 // | parallel for simd| barrier | |
1589 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001590 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001591 // | parallel for simd| flush | |
1592 // | parallel for simd| ordered | |
1593 // | parallel for simd| atomic | |
1594 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001595 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001596 // | parallel for simd| cancellation | |
1597 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001598 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001599 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001600 // | sections | parallel | * |
1601 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001602 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001603 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001604 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001605 // | sections | simd | * |
1606 // | sections | sections | + |
1607 // | sections | section | * |
1608 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001609 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001610 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001611 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001612 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001613 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001614 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001615 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001616 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001617 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001618 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001619 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001620 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001621 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001622 // | sections | cancellation | |
1623 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001624 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001625 // +------------------+-----------------+------------------------------------+
1626 // | section | parallel | * |
1627 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001628 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001629 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001630 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001631 // | section | simd | * |
1632 // | section | sections | + |
1633 // | section | section | + |
1634 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001635 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001636 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001637 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001638 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001639 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001640 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001641 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001642 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001643 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001644 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001645 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001646 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001647 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001648 // | section | cancellation | |
1649 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001650 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001651 // +------------------+-----------------+------------------------------------+
1652 // | single | parallel | * |
1653 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001654 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001655 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001656 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001657 // | single | simd | * |
1658 // | single | sections | + |
1659 // | single | section | + |
1660 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001661 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001662 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001663 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001664 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001665 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001666 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001667 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001668 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001669 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001670 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001671 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001672 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001673 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001674 // | single | cancellation | |
1675 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001676 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001677 // +------------------+-----------------+------------------------------------+
1678 // | parallel for | parallel | * |
1679 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001680 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001681 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001682 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001683 // | parallel for | simd | * |
1684 // | parallel for | sections | + |
1685 // | parallel for | section | + |
1686 // | parallel for | single | + |
1687 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001688 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001689 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001690 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001691 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001692 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001693 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001694 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001695 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001696 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001697 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001698 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001699 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001700 // | parallel for | cancellation | |
1701 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001702 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001703 // +------------------+-----------------+------------------------------------+
1704 // | parallel sections| parallel | * |
1705 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001706 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001707 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001708 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001709 // | parallel sections| simd | * |
1710 // | parallel sections| sections | + |
1711 // | parallel sections| section | * |
1712 // | parallel sections| single | + |
1713 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001714 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001715 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001716 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001717 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001718 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001719 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001720 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001721 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001722 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001723 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001724 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001725 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001726 // | parallel sections| cancellation | |
1727 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001728 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001729 // +------------------+-----------------+------------------------------------+
1730 // | task | parallel | * |
1731 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001732 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001733 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001734 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001735 // | task | simd | * |
1736 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001737 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001738 // | task | single | + |
1739 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001740 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001741 // | task |parallel sections| * |
1742 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001743 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001744 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001745 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001746 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001747 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001748 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001749 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001750 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001751 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001752 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001753 // | | point | ! |
1754 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001755 // +------------------+-----------------+------------------------------------+
1756 // | ordered | parallel | * |
1757 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001758 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001759 // | ordered | master | * |
1760 // | ordered | critical | * |
1761 // | ordered | simd | * |
1762 // | ordered | sections | + |
1763 // | ordered | section | + |
1764 // | ordered | single | + |
1765 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001766 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001767 // | ordered |parallel sections| * |
1768 // | ordered | task | * |
1769 // | ordered | taskyield | * |
1770 // | ordered | barrier | + |
1771 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001772 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001773 // | ordered | flush | * |
1774 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001775 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001776 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001777 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001778 // | ordered | cancellation | |
1779 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001780 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001781 // +------------------+-----------------+------------------------------------+
1782 // | atomic | parallel | |
1783 // | atomic | for | |
1784 // | atomic | for simd | |
1785 // | atomic | master | |
1786 // | atomic | critical | |
1787 // | atomic | simd | |
1788 // | atomic | sections | |
1789 // | atomic | section | |
1790 // | atomic | single | |
1791 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001792 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001793 // | atomic |parallel sections| |
1794 // | atomic | task | |
1795 // | atomic | taskyield | |
1796 // | atomic | barrier | |
1797 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001798 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001799 // | atomic | flush | |
1800 // | atomic | ordered | |
1801 // | atomic | atomic | |
1802 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001803 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001804 // | atomic | cancellation | |
1805 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001806 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001807 // +------------------+-----------------+------------------------------------+
1808 // | target | parallel | * |
1809 // | target | for | * |
1810 // | target | for simd | * |
1811 // | target | master | * |
1812 // | target | critical | * |
1813 // | target | simd | * |
1814 // | target | sections | * |
1815 // | target | section | * |
1816 // | target | single | * |
1817 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001818 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001819 // | target |parallel sections| * |
1820 // | target | task | * |
1821 // | target | taskyield | * |
1822 // | target | barrier | * |
1823 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001824 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001825 // | target | flush | * |
1826 // | target | ordered | * |
1827 // | target | atomic | * |
1828 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001829 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001830 // | target | cancellation | |
1831 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001832 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001833 // +------------------+-----------------+------------------------------------+
1834 // | teams | parallel | * |
1835 // | teams | for | + |
1836 // | teams | for simd | + |
1837 // | teams | master | + |
1838 // | teams | critical | + |
1839 // | teams | simd | + |
1840 // | teams | sections | + |
1841 // | teams | section | + |
1842 // | teams | single | + |
1843 // | teams | parallel for | * |
1844 // | teams |parallel for simd| * |
1845 // | teams |parallel sections| * |
1846 // | teams | task | + |
1847 // | teams | taskyield | + |
1848 // | teams | barrier | + |
1849 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001850 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001851 // | teams | flush | + |
1852 // | teams | ordered | + |
1853 // | teams | atomic | + |
1854 // | teams | target | + |
1855 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001856 // | teams | cancellation | |
1857 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001858 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001859 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001860 if (Stack->getCurScope()) {
1861 auto ParentRegion = Stack->getParentDirective();
1862 bool NestingProhibited = false;
1863 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001864 enum {
1865 NoRecommend,
1866 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001867 ShouldBeInOrderedRegion,
1868 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001869 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001870 if (isOpenMPSimdDirective(ParentRegion)) {
1871 // OpenMP [2.16, Nesting of Regions]
1872 // OpenMP constructs may not be nested inside a simd region.
1873 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1874 return true;
1875 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001876 if (ParentRegion == OMPD_atomic) {
1877 // OpenMP [2.16, Nesting of Regions]
1878 // OpenMP constructs may not be nested inside an atomic region.
1879 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1880 return true;
1881 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001882 if (CurrentRegion == OMPD_section) {
1883 // OpenMP [2.7.2, sections Construct, Restrictions]
1884 // Orphaned section directives are prohibited. That is, the section
1885 // directives must appear within the sections construct and must not be
1886 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001887 if (ParentRegion != OMPD_sections &&
1888 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001889 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1890 << (ParentRegion != OMPD_unknown)
1891 << getOpenMPDirectiveName(ParentRegion);
1892 return true;
1893 }
1894 return false;
1895 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001896 // Allow some constructs to be orphaned (they could be used in functions,
1897 // called from OpenMP regions with the required preconditions).
1898 if (ParentRegion == OMPD_unknown)
1899 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001900 if (CurrentRegion == OMPD_cancellation_point ||
1901 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001902 // OpenMP [2.16, Nesting of Regions]
1903 // A cancellation point construct for which construct-type-clause is
1904 // taskgroup must be nested inside a task construct. A cancellation
1905 // point construct for which construct-type-clause is not taskgroup must
1906 // be closely nested inside an OpenMP construct that matches the type
1907 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001908 // A cancel construct for which construct-type-clause is taskgroup must be
1909 // nested inside a task construct. A cancel construct for which
1910 // construct-type-clause is not taskgroup must be closely nested inside an
1911 // OpenMP construct that matches the type specified in
1912 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001913 NestingProhibited =
1914 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00001915 (CancelRegion == OMPD_for &&
1916 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001917 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1918 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00001919 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
1920 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001921 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001922 // OpenMP [2.16, Nesting of Regions]
1923 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001924 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001925 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1926 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001927 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1928 // OpenMP [2.16, Nesting of Regions]
1929 // A critical region may not be nested (closely or otherwise) inside a
1930 // critical region with the same name. Note that this restriction is not
1931 // sufficient to prevent deadlock.
1932 SourceLocation PreviousCriticalLoc;
1933 bool DeadLock =
1934 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1935 OpenMPDirectiveKind K,
1936 const DeclarationNameInfo &DNI,
1937 SourceLocation Loc)
1938 ->bool {
1939 if (K == OMPD_critical &&
1940 DNI.getName() == CurrentName.getName()) {
1941 PreviousCriticalLoc = Loc;
1942 return true;
1943 } else
1944 return false;
1945 },
1946 false /* skip top directive */);
1947 if (DeadLock) {
1948 SemaRef.Diag(StartLoc,
1949 diag::err_omp_prohibited_region_critical_same_name)
1950 << CurrentName.getName();
1951 if (PreviousCriticalLoc.isValid())
1952 SemaRef.Diag(PreviousCriticalLoc,
1953 diag::note_omp_previous_critical_region);
1954 return true;
1955 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001956 } else if (CurrentRegion == OMPD_barrier) {
1957 // OpenMP [2.16, Nesting of Regions]
1958 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001959 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001960 NestingProhibited =
1961 isOpenMPWorksharingDirective(ParentRegion) ||
1962 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1963 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001964 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001965 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001966 // OpenMP [2.16, Nesting of Regions]
1967 // A worksharing region may not be closely nested inside a worksharing,
1968 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001969 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001970 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001971 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1972 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1973 Recommend = ShouldBeInParallelRegion;
1974 } else if (CurrentRegion == OMPD_ordered) {
1975 // OpenMP [2.16, Nesting of Regions]
1976 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001977 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001978 // An ordered region must be closely nested inside a loop region (or
1979 // parallel loop region) with an ordered clause.
1980 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001981 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001982 !Stack->isParentOrderedRegion();
1983 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001984 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1985 // OpenMP [2.16, Nesting of Regions]
1986 // If specified, a teams construct must be contained within a target
1987 // construct.
1988 NestingProhibited = ParentRegion != OMPD_target;
1989 Recommend = ShouldBeInTargetRegion;
1990 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1991 }
1992 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1993 // OpenMP [2.16, Nesting of Regions]
1994 // distribute, parallel, parallel sections, parallel workshare, and the
1995 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1996 // constructs that can be closely nested in the teams region.
1997 // TODO: add distribute directive.
1998 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1999 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002000 }
2001 if (NestingProhibited) {
2002 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002003 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2004 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002005 return true;
2006 }
2007 }
2008 return false;
2009}
2010
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002011static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2012 ArrayRef<OMPClause *> Clauses,
2013 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2014 bool ErrorFound = false;
2015 unsigned NamedModifiersNumber = 0;
2016 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2017 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002018 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002019 for (const auto *C : Clauses) {
2020 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2021 // At most one if clause without a directive-name-modifier can appear on
2022 // the directive.
2023 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2024 if (FoundNameModifiers[CurNM]) {
2025 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2026 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2027 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2028 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002029 } else if (CurNM != OMPD_unknown) {
2030 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002031 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002032 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002033 FoundNameModifiers[CurNM] = IC;
2034 if (CurNM == OMPD_unknown)
2035 continue;
2036 // Check if the specified name modifier is allowed for the current
2037 // directive.
2038 // At most one if clause with the particular directive-name-modifier can
2039 // appear on the directive.
2040 bool MatchFound = false;
2041 for (auto NM : AllowedNameModifiers) {
2042 if (CurNM == NM) {
2043 MatchFound = true;
2044 break;
2045 }
2046 }
2047 if (!MatchFound) {
2048 S.Diag(IC->getNameModifierLoc(),
2049 diag::err_omp_wrong_if_directive_name_modifier)
2050 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2051 ErrorFound = true;
2052 }
2053 }
2054 }
2055 // If any if clause on the directive includes a directive-name-modifier then
2056 // all if clauses on the directive must include a directive-name-modifier.
2057 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2058 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2059 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2060 diag::err_omp_no_more_if_clause);
2061 } else {
2062 std::string Values;
2063 std::string Sep(", ");
2064 unsigned AllowedCnt = 0;
2065 unsigned TotalAllowedNum =
2066 AllowedNameModifiers.size() - NamedModifiersNumber;
2067 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2068 ++Cnt) {
2069 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2070 if (!FoundNameModifiers[NM]) {
2071 Values += "'";
2072 Values += getOpenMPDirectiveName(NM);
2073 Values += "'";
2074 if (AllowedCnt + 2 == TotalAllowedNum)
2075 Values += " or ";
2076 else if (AllowedCnt + 1 != TotalAllowedNum)
2077 Values += Sep;
2078 ++AllowedCnt;
2079 }
2080 }
2081 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2082 diag::err_omp_unnamed_if_clause)
2083 << (TotalAllowedNum > 1) << Values;
2084 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002085 for (auto Loc : NameModifierLoc) {
2086 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2087 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002088 ErrorFound = true;
2089 }
2090 return ErrorFound;
2091}
2092
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002093StmtResult Sema::ActOnOpenMPExecutableDirective(
2094 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2095 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2096 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002097 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002098 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2099 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002100 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002101
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002102 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002103 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002104 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002105 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002106 if (AStmt) {
2107 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2108
2109 // Check default data sharing attributes for referenced variables.
2110 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2111 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2112 if (DSAChecker.isErrorFound())
2113 return StmtError();
2114 // Generate list of implicitly defined firstprivate variables.
2115 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002116
2117 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2118 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2119 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2120 SourceLocation(), SourceLocation())) {
2121 ClausesWithImplicit.push_back(Implicit);
2122 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2123 DSAChecker.getImplicitFirstprivate().size();
2124 } else
2125 ErrorFound = true;
2126 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002127 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002128
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002129 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002130 switch (Kind) {
2131 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002132 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2133 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002134 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002135 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002136 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002137 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2138 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002139 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002140 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002141 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2142 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002143 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002144 case OMPD_for_simd:
2145 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2146 EndLoc, VarsWithInheritedDSA);
2147 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002148 case OMPD_sections:
2149 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2150 EndLoc);
2151 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002152 case OMPD_section:
2153 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002154 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002155 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2156 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002157 case OMPD_single:
2158 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2159 EndLoc);
2160 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002161 case OMPD_master:
2162 assert(ClausesWithImplicit.empty() &&
2163 "No clauses are allowed for 'omp master' directive");
2164 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2165 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002166 case OMPD_critical:
2167 assert(ClausesWithImplicit.empty() &&
2168 "No clauses are allowed for 'omp critical' directive");
2169 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2170 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002171 case OMPD_parallel_for:
2172 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2173 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002174 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002175 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002176 case OMPD_parallel_for_simd:
2177 Res = ActOnOpenMPParallelForSimdDirective(
2178 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002179 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002180 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002181 case OMPD_parallel_sections:
2182 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2183 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002184 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002185 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002186 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002187 Res =
2188 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002189 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002190 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002191 case OMPD_taskyield:
2192 assert(ClausesWithImplicit.empty() &&
2193 "No clauses are allowed for 'omp taskyield' directive");
2194 assert(AStmt == nullptr &&
2195 "No associated statement allowed for 'omp taskyield' directive");
2196 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2197 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002198 case OMPD_barrier:
2199 assert(ClausesWithImplicit.empty() &&
2200 "No clauses are allowed for 'omp barrier' directive");
2201 assert(AStmt == nullptr &&
2202 "No associated statement allowed for 'omp barrier' directive");
2203 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2204 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002205 case OMPD_taskwait:
2206 assert(ClausesWithImplicit.empty() &&
2207 "No clauses are allowed for 'omp taskwait' directive");
2208 assert(AStmt == nullptr &&
2209 "No associated statement allowed for 'omp taskwait' directive");
2210 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2211 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002212 case OMPD_taskgroup:
2213 assert(ClausesWithImplicit.empty() &&
2214 "No clauses are allowed for 'omp taskgroup' directive");
2215 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2216 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002217 case OMPD_flush:
2218 assert(AStmt == nullptr &&
2219 "No associated statement allowed for 'omp flush' directive");
2220 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2221 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002222 case OMPD_ordered:
2223 assert(ClausesWithImplicit.empty() &&
2224 "No clauses are allowed for 'omp ordered' directive");
2225 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2226 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002227 case OMPD_atomic:
2228 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2229 EndLoc);
2230 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002231 case OMPD_teams:
2232 Res =
2233 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2234 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002235 case OMPD_target:
2236 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2237 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002238 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002239 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002240 case OMPD_cancellation_point:
2241 assert(ClausesWithImplicit.empty() &&
2242 "No clauses are allowed for 'omp cancellation point' directive");
2243 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2244 "cancellation point' directive");
2245 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2246 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002247 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002248 assert(AStmt == nullptr &&
2249 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002250 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2251 CancelRegion);
2252 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002253 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002254 case OMPD_target_data:
2255 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2256 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002257 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002258 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002259 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002260 llvm_unreachable("OpenMP Directive is not allowed");
2261 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002262 llvm_unreachable("Unknown OpenMP directive");
2263 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002264
Alexey Bataev4acb8592014-07-07 13:01:15 +00002265 for (auto P : VarsWithInheritedDSA) {
2266 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2267 << P.first << P.second->getSourceRange();
2268 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002269 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2270
2271 if (!AllowedNameModifiers.empty())
2272 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2273 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002274
Alexey Bataeved09d242014-05-28 05:53:51 +00002275 if (ErrorFound)
2276 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002277 return Res;
2278}
2279
2280StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2281 Stmt *AStmt,
2282 SourceLocation StartLoc,
2283 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002284 if (!AStmt)
2285 return StmtError();
2286
Alexey Bataev9959db52014-05-06 10:08:46 +00002287 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2288 // 1.2.2 OpenMP Language Terminology
2289 // Structured block - An executable statement with a single entry at the
2290 // top and a single exit at the bottom.
2291 // The point of exit cannot be a branch out of the structured block.
2292 // longjmp() and throw() must not violate the entry/exit criteria.
2293 CS->getCapturedDecl()->setNothrow();
2294
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002295 getCurFunction()->setHasBranchProtectedScope();
2296
Alexey Bataev25e5b442015-09-15 12:52:43 +00002297 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2298 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002299}
2300
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002301namespace {
2302/// \brief Helper class for checking canonical form of the OpenMP loops and
2303/// extracting iteration space of each loop in the loop nest, that will be used
2304/// for IR generation.
2305class OpenMPIterationSpaceChecker {
2306 /// \brief Reference to Sema.
2307 Sema &SemaRef;
2308 /// \brief A location for diagnostics (when there is no some better location).
2309 SourceLocation DefaultLoc;
2310 /// \brief A location for diagnostics (when increment is not compatible).
2311 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002312 /// \brief A source location for referring to loop init later.
2313 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002314 /// \brief A source location for referring to condition later.
2315 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002316 /// \brief A source location for referring to increment later.
2317 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002318 /// \brief Loop variable.
2319 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002320 /// \brief Reference to loop variable.
2321 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002322 /// \brief Lower bound (initializer for the var).
2323 Expr *LB;
2324 /// \brief Upper bound.
2325 Expr *UB;
2326 /// \brief Loop step (increment).
2327 Expr *Step;
2328 /// \brief This flag is true when condition is one of:
2329 /// Var < UB
2330 /// Var <= UB
2331 /// UB > Var
2332 /// UB >= Var
2333 bool TestIsLessOp;
2334 /// \brief This flag is true when condition is strict ( < or > ).
2335 bool TestIsStrictOp;
2336 /// \brief This flag is true when step is subtracted on each iteration.
2337 bool SubtractStep;
2338
2339public:
2340 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2341 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002342 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2343 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002344 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2345 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002346 /// \brief Check init-expr for canonical loop form and save loop counter
2347 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002348 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002349 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2350 /// for less/greater and for strict/non-strict comparison.
2351 bool CheckCond(Expr *S);
2352 /// \brief Check incr-expr for canonical loop form and return true if it
2353 /// does not conform, otherwise save loop step (#Step).
2354 bool CheckInc(Expr *S);
2355 /// \brief Return the loop counter variable.
2356 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002357 /// \brief Return the reference expression to loop counter variable.
2358 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002359 /// \brief Source range of the loop init.
2360 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2361 /// \brief Source range of the loop condition.
2362 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2363 /// \brief Source range of the loop increment.
2364 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2365 /// \brief True if the step should be subtracted.
2366 bool ShouldSubtractStep() const { return SubtractStep; }
2367 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002368 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002369 /// \brief Build the precondition expression for the loops.
2370 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002371 /// \brief Build reference expression to the counter be used for codegen.
2372 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002373 /// \brief Build reference expression to the private counter be used for
2374 /// codegen.
2375 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002376 /// \brief Build initization of the counter be used for codegen.
2377 Expr *BuildCounterInit() const;
2378 /// \brief Build step of the counter be used for codegen.
2379 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002380 /// \brief Return true if any expression is dependent.
2381 bool Dependent() const;
2382
2383private:
2384 /// \brief Check the right-hand side of an assignment in the increment
2385 /// expression.
2386 bool CheckIncRHS(Expr *RHS);
2387 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002388 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002389 /// \brief Helper to set upper bound.
2390 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002391 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002392 /// \brief Helper to set loop increment.
2393 bool SetStep(Expr *NewStep, bool Subtract);
2394};
2395
2396bool OpenMPIterationSpaceChecker::Dependent() const {
2397 if (!Var) {
2398 assert(!LB && !UB && !Step);
2399 return false;
2400 }
2401 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2402 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2403}
2404
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002405template <typename T>
2406static T *getExprAsWritten(T *E) {
2407 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2408 E = ExprTemp->getSubExpr();
2409
2410 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2411 E = MTE->GetTemporaryExpr();
2412
2413 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2414 E = Binder->getSubExpr();
2415
2416 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2417 E = ICE->getSubExprAsWritten();
2418 return E->IgnoreParens();
2419}
2420
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002421bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2422 DeclRefExpr *NewVarRefExpr,
2423 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002424 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002425 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2426 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002427 if (!NewVar || !NewLB)
2428 return true;
2429 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002430 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002431 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2432 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002433 if ((Ctor->isCopyOrMoveConstructor() ||
2434 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2435 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002436 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002437 LB = NewLB;
2438 return false;
2439}
2440
2441bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2442 const SourceRange &SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002443 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002444 // State consistency checking to ensure correct usage.
2445 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2446 !TestIsLessOp && !TestIsStrictOp);
2447 if (!NewUB)
2448 return true;
2449 UB = NewUB;
2450 TestIsLessOp = LessOp;
2451 TestIsStrictOp = StrictOp;
2452 ConditionSrcRange = SR;
2453 ConditionLoc = SL;
2454 return false;
2455}
2456
2457bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2458 // State consistency checking to ensure correct usage.
2459 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2460 if (!NewStep)
2461 return true;
2462 if (!NewStep->isValueDependent()) {
2463 // Check that the step is integer expression.
2464 SourceLocation StepLoc = NewStep->getLocStart();
2465 ExprResult Val =
2466 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2467 if (Val.isInvalid())
2468 return true;
2469 NewStep = Val.get();
2470
2471 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2472 // If test-expr is of form var relational-op b and relational-op is < or
2473 // <= then incr-expr must cause var to increase on each iteration of the
2474 // loop. If test-expr is of form var relational-op b and relational-op is
2475 // > or >= then incr-expr must cause var to decrease on each iteration of
2476 // the loop.
2477 // If test-expr is of form b relational-op var and relational-op is < or
2478 // <= then incr-expr must cause var to decrease on each iteration of the
2479 // loop. If test-expr is of form b relational-op var and relational-op is
2480 // > or >= then incr-expr must cause var to increase on each iteration of
2481 // the loop.
2482 llvm::APSInt Result;
2483 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2484 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2485 bool IsConstNeg =
2486 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002487 bool IsConstPos =
2488 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002489 bool IsConstZero = IsConstant && !Result.getBoolValue();
2490 if (UB && (IsConstZero ||
2491 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002492 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002493 SemaRef.Diag(NewStep->getExprLoc(),
2494 diag::err_omp_loop_incr_not_compatible)
2495 << Var << TestIsLessOp << NewStep->getSourceRange();
2496 SemaRef.Diag(ConditionLoc,
2497 diag::note_omp_loop_cond_requres_compatible_incr)
2498 << TestIsLessOp << ConditionSrcRange;
2499 return true;
2500 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002501 if (TestIsLessOp == Subtract) {
2502 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2503 NewStep).get();
2504 Subtract = !Subtract;
2505 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002506 }
2507
2508 Step = NewStep;
2509 SubtractStep = Subtract;
2510 return false;
2511}
2512
Alexey Bataev9c821032015-04-30 04:23:23 +00002513bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002514 // Check init-expr for canonical loop form and save loop counter
2515 // variable - #Var and its initialization value - #LB.
2516 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2517 // var = lb
2518 // integer-type var = lb
2519 // random-access-iterator-type var = lb
2520 // pointer-type var = lb
2521 //
2522 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002523 if (EmitDiags) {
2524 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2525 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002526 return true;
2527 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002528 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002529 if (Expr *E = dyn_cast<Expr>(S))
2530 S = E->IgnoreParens();
2531 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2532 if (BO->getOpcode() == BO_Assign)
2533 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002534 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002535 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002536 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2537 if (DS->isSingleDecl()) {
2538 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002539 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002540 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002541 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002542 SemaRef.Diag(S->getLocStart(),
2543 diag::ext_omp_loop_not_canonical_init)
2544 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002545 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002546 }
2547 }
2548 }
2549 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2550 if (CE->getOperator() == OO_Equal)
2551 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002552 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2553 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002554
Alexey Bataev9c821032015-04-30 04:23:23 +00002555 if (EmitDiags) {
2556 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2557 << S->getSourceRange();
2558 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002559 return true;
2560}
2561
Alexey Bataev23b69422014-06-18 07:08:49 +00002562/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002563/// variable (which may be the loop variable) if possible.
2564static const VarDecl *GetInitVarDecl(const Expr *E) {
2565 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002566 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002567 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002568 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2569 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002570 if ((Ctor->isCopyOrMoveConstructor() ||
2571 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2572 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002573 E = CE->getArg(0)->IgnoreParenImpCasts();
2574 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2575 if (!DRE)
2576 return nullptr;
2577 return dyn_cast<VarDecl>(DRE->getDecl());
2578}
2579
2580bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2581 // Check test-expr for canonical form, save upper-bound UB, flags for
2582 // less/greater and for strict/non-strict comparison.
2583 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2584 // var relational-op b
2585 // b relational-op var
2586 //
2587 if (!S) {
2588 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2589 return true;
2590 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002591 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002592 SourceLocation CondLoc = S->getLocStart();
2593 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2594 if (BO->isRelationalOp()) {
2595 if (GetInitVarDecl(BO->getLHS()) == Var)
2596 return SetUB(BO->getRHS(),
2597 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2598 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2599 BO->getSourceRange(), BO->getOperatorLoc());
2600 if (GetInitVarDecl(BO->getRHS()) == Var)
2601 return SetUB(BO->getLHS(),
2602 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2603 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2604 BO->getSourceRange(), BO->getOperatorLoc());
2605 }
2606 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2607 if (CE->getNumArgs() == 2) {
2608 auto Op = CE->getOperator();
2609 switch (Op) {
2610 case OO_Greater:
2611 case OO_GreaterEqual:
2612 case OO_Less:
2613 case OO_LessEqual:
2614 if (GetInitVarDecl(CE->getArg(0)) == Var)
2615 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2616 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2617 CE->getOperatorLoc());
2618 if (GetInitVarDecl(CE->getArg(1)) == Var)
2619 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2620 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2621 CE->getOperatorLoc());
2622 break;
2623 default:
2624 break;
2625 }
2626 }
2627 }
2628 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2629 << S->getSourceRange() << Var;
2630 return true;
2631}
2632
2633bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2634 // RHS of canonical loop form increment can be:
2635 // var + incr
2636 // incr + var
2637 // var - incr
2638 //
2639 RHS = RHS->IgnoreParenImpCasts();
2640 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2641 if (BO->isAdditiveOp()) {
2642 bool IsAdd = BO->getOpcode() == BO_Add;
2643 if (GetInitVarDecl(BO->getLHS()) == Var)
2644 return SetStep(BO->getRHS(), !IsAdd);
2645 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2646 return SetStep(BO->getLHS(), false);
2647 }
2648 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2649 bool IsAdd = CE->getOperator() == OO_Plus;
2650 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2651 if (GetInitVarDecl(CE->getArg(0)) == Var)
2652 return SetStep(CE->getArg(1), !IsAdd);
2653 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2654 return SetStep(CE->getArg(0), false);
2655 }
2656 }
2657 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2658 << RHS->getSourceRange() << Var;
2659 return true;
2660}
2661
2662bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2663 // Check incr-expr for canonical loop form and return true if it
2664 // does not conform.
2665 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2666 // ++var
2667 // var++
2668 // --var
2669 // var--
2670 // var += incr
2671 // var -= incr
2672 // var = var + incr
2673 // var = incr + var
2674 // var = var - incr
2675 //
2676 if (!S) {
2677 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2678 return true;
2679 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002680 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002681 S = S->IgnoreParens();
2682 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2683 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2684 return SetStep(
2685 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2686 (UO->isDecrementOp() ? -1 : 1)).get(),
2687 false);
2688 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2689 switch (BO->getOpcode()) {
2690 case BO_AddAssign:
2691 case BO_SubAssign:
2692 if (GetInitVarDecl(BO->getLHS()) == Var)
2693 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2694 break;
2695 case BO_Assign:
2696 if (GetInitVarDecl(BO->getLHS()) == Var)
2697 return CheckIncRHS(BO->getRHS());
2698 break;
2699 default:
2700 break;
2701 }
2702 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2703 switch (CE->getOperator()) {
2704 case OO_PlusPlus:
2705 case OO_MinusMinus:
2706 if (GetInitVarDecl(CE->getArg(0)) == Var)
2707 return SetStep(
2708 SemaRef.ActOnIntegerConstant(
2709 CE->getLocStart(),
2710 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2711 false);
2712 break;
2713 case OO_PlusEqual:
2714 case OO_MinusEqual:
2715 if (GetInitVarDecl(CE->getArg(0)) == Var)
2716 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2717 break;
2718 case OO_Equal:
2719 if (GetInitVarDecl(CE->getArg(0)) == Var)
2720 return CheckIncRHS(CE->getArg(1));
2721 break;
2722 default:
2723 break;
2724 }
2725 }
2726 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2727 << S->getSourceRange() << Var;
2728 return true;
2729}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002730
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002731namespace {
2732// Transform variables declared in GNU statement expressions to new ones to
2733// avoid crash on codegen.
2734class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2735 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2736
2737public:
2738 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2739
2740 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
2741 if (auto *VD = cast<VarDecl>(D))
2742 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
2743 !isa<ImplicitParamDecl>(D)) {
2744 auto *NewVD = VarDecl::Create(
2745 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
2746 VD->getLocation(), VD->getIdentifier(), VD->getType(),
2747 VD->getTypeSourceInfo(), VD->getStorageClass());
2748 NewVD->setTSCSpec(VD->getTSCSpec());
2749 NewVD->setInit(VD->getInit());
2750 NewVD->setInitStyle(VD->getInitStyle());
2751 NewVD->setExceptionVariable(VD->isExceptionVariable());
2752 NewVD->setNRVOVariable(VD->isNRVOVariable());
2753 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
2754 NewVD->setConstexpr(VD->isConstexpr());
2755 NewVD->setInitCapture(VD->isInitCapture());
2756 NewVD->setPreviousDeclInSameBlockScope(
2757 VD->isPreviousDeclInSameBlockScope());
2758 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002759 if (VD->hasAttrs())
2760 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002761 transformedLocalDecl(VD, NewVD);
2762 return NewVD;
2763 }
2764 return BaseTransform::TransformDefinition(Loc, D);
2765 }
2766
2767 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
2768 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
2769 if (E->getDecl() != NewD) {
2770 NewD->setReferenced();
2771 NewD->markUsed(SemaRef.Context);
2772 return DeclRefExpr::Create(
2773 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
2774 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
2775 E->getNameInfo(), E->getType(), E->getValueKind());
2776 }
2777 return BaseTransform::TransformDeclRefExpr(E);
2778 }
2779};
2780}
2781
Alexander Musmana5f070a2014-10-01 06:03:56 +00002782/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002783Expr *
2784OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2785 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002786 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002787 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002788 auto VarType = Var->getType().getNonReferenceType();
2789 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002790 SemaRef.getLangOpts().CPlusPlus) {
2791 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002792 auto *UBExpr = TestIsLessOp ? UB : LB;
2793 auto *LBExpr = TestIsLessOp ? LB : UB;
2794 Expr *Upper = Transform.TransformExpr(UBExpr).get();
2795 Expr *Lower = Transform.TransformExpr(LBExpr).get();
2796 if (!Upper || !Lower)
2797 return nullptr;
2798 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
2799 Sema::AA_Converting,
2800 /*AllowExplicit=*/true)
2801 .get();
2802 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
2803 Sema::AA_Converting,
2804 /*AllowExplicit=*/true)
2805 .get();
2806 if (!Upper || !Lower)
2807 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002808
2809 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2810
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002811 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002812 // BuildBinOp already emitted error, this one is to point user to upper
2813 // and lower bound, and to tell what is passed to 'operator-'.
2814 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2815 << Upper->getSourceRange() << Lower->getSourceRange();
2816 return nullptr;
2817 }
2818 }
2819
2820 if (!Diff.isUsable())
2821 return nullptr;
2822
2823 // Upper - Lower [- 1]
2824 if (TestIsStrictOp)
2825 Diff = SemaRef.BuildBinOp(
2826 S, DefaultLoc, BO_Sub, Diff.get(),
2827 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2828 if (!Diff.isUsable())
2829 return nullptr;
2830
2831 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002832 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2833 if (NewStep.isInvalid())
2834 return nullptr;
2835 NewStep = SemaRef.PerformImplicitConversion(
2836 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2837 /*AllowExplicit=*/true);
2838 if (NewStep.isInvalid())
2839 return nullptr;
2840 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002841 if (!Diff.isUsable())
2842 return nullptr;
2843
2844 // Parentheses (for dumping/debugging purposes only).
2845 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2846 if (!Diff.isUsable())
2847 return nullptr;
2848
2849 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002850 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2851 if (NewStep.isInvalid())
2852 return nullptr;
2853 NewStep = SemaRef.PerformImplicitConversion(
2854 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2855 /*AllowExplicit=*/true);
2856 if (NewStep.isInvalid())
2857 return nullptr;
2858 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002859 if (!Diff.isUsable())
2860 return nullptr;
2861
Alexander Musman174b3ca2014-10-06 11:16:29 +00002862 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002863 QualType Type = Diff.get()->getType();
2864 auto &C = SemaRef.Context;
2865 bool UseVarType = VarType->hasIntegerRepresentation() &&
2866 C.getTypeSize(Type) > C.getTypeSize(VarType);
2867 if (!Type->isIntegerType() || UseVarType) {
2868 unsigned NewSize =
2869 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
2870 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
2871 : Type->hasSignedIntegerRepresentation();
2872 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
2873 Diff = SemaRef.PerformImplicitConversion(
2874 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
2875 if (!Diff.isUsable())
2876 return nullptr;
2877 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00002878 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00002879 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2880 if (NewSize != C.getTypeSize(Type)) {
2881 if (NewSize < C.getTypeSize(Type)) {
2882 assert(NewSize == 64 && "incorrect loop var size");
2883 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2884 << InitSrcRange << ConditionSrcRange;
2885 }
2886 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002887 NewSize, Type->hasSignedIntegerRepresentation() ||
2888 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00002889 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2890 Sema::AA_Converting, true);
2891 if (!Diff.isUsable())
2892 return nullptr;
2893 }
2894 }
2895
Alexander Musmana5f070a2014-10-01 06:03:56 +00002896 return Diff.get();
2897}
2898
Alexey Bataev62dbb972015-04-22 11:59:37 +00002899Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2900 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2901 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2902 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002903 TransformToNewDefs Transform(SemaRef);
2904
2905 auto NewLB = Transform.TransformExpr(LB);
2906 auto NewUB = Transform.TransformExpr(UB);
2907 if (NewLB.isInvalid() || NewUB.isInvalid())
2908 return Cond;
2909 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
2910 Sema::AA_Converting,
2911 /*AllowExplicit=*/true);
2912 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
2913 Sema::AA_Converting,
2914 /*AllowExplicit=*/true);
2915 if (NewLB.isInvalid() || NewUB.isInvalid())
2916 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002917 auto CondExpr = SemaRef.BuildBinOp(
2918 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2919 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002920 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002921 if (CondExpr.isUsable()) {
2922 CondExpr = SemaRef.PerformImplicitConversion(
2923 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2924 /*AllowExplicit=*/true);
2925 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002926 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2927 // Otherwise use original loop conditon and evaluate it in runtime.
2928 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2929}
2930
Alexander Musmana5f070a2014-10-01 06:03:56 +00002931/// \brief Build reference expression to the counter be used for codegen.
2932Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00002933 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
2934 DefaultLoc);
2935}
2936
2937Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
2938 if (Var && !Var->isInvalidDecl()) {
2939 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002940 auto *PrivateVar =
2941 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
2942 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00002943 if (PrivateVar->isInvalidDecl())
2944 return nullptr;
2945 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
2946 }
2947 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002948}
2949
2950/// \brief Build initization of the counter be used for codegen.
2951Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2952
2953/// \brief Build step of the counter be used for codegen.
2954Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2955
2956/// \brief Iteration space of a single for loop.
2957struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002958 /// \brief Condition of the loop.
2959 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002960 /// \brief This expression calculates the number of iterations in the loop.
2961 /// It is always possible to calculate it before starting the loop.
2962 Expr *NumIterations;
2963 /// \brief The loop counter variable.
2964 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00002965 /// \brief Private loop counter variable.
2966 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002967 /// \brief This is initializer for the initial value of #CounterVar.
2968 Expr *CounterInit;
2969 /// \brief This is step for the #CounterVar used to generate its update:
2970 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2971 Expr *CounterStep;
2972 /// \brief Should step be subtracted?
2973 bool Subtract;
2974 /// \brief Source range of the loop init.
2975 SourceRange InitSrcRange;
2976 /// \brief Source range of the loop condition.
2977 SourceRange CondSrcRange;
2978 /// \brief Source range of the loop increment.
2979 SourceRange IncSrcRange;
2980};
2981
Alexey Bataev23b69422014-06-18 07:08:49 +00002982} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002983
Alexey Bataev9c821032015-04-30 04:23:23 +00002984void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2985 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2986 assert(Init && "Expected loop in canonical form.");
2987 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2988 if (CollapseIteration > 0 &&
2989 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2990 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2991 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2992 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2993 }
2994 DSAStack->setCollapseNumber(CollapseIteration - 1);
2995 }
2996}
2997
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002998/// \brief Called on a for stmt to check and extract its iteration space
2999/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003000static bool CheckOpenMPIterationSpace(
3001 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3002 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003003 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003004 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3005 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003006 // OpenMP [2.6, Canonical Loop Form]
3007 // for (init-expr; test-expr; incr-expr) structured-block
3008 auto For = dyn_cast_or_null<ForStmt>(S);
3009 if (!For) {
3010 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003011 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3012 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3013 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3014 if (NestedLoopCount > 1) {
3015 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3016 SemaRef.Diag(DSA.getConstructLoc(),
3017 diag::note_omp_collapse_ordered_expr)
3018 << 2 << CollapseLoopCountExpr->getSourceRange()
3019 << OrderedLoopCountExpr->getSourceRange();
3020 else if (CollapseLoopCountExpr)
3021 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3022 diag::note_omp_collapse_ordered_expr)
3023 << 0 << CollapseLoopCountExpr->getSourceRange();
3024 else
3025 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3026 diag::note_omp_collapse_ordered_expr)
3027 << 1 << OrderedLoopCountExpr->getSourceRange();
3028 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003029 return true;
3030 }
3031 assert(For->getBody());
3032
3033 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3034
3035 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003036 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003037 if (ISC.CheckInit(Init)) {
3038 return true;
3039 }
3040
3041 bool HasErrors = false;
3042
3043 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003044 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003045
3046 // OpenMP [2.6, Canonical Loop Form]
3047 // Var is one of the following:
3048 // A variable of signed or unsigned integer type.
3049 // For C++, a variable of a random access iterator type.
3050 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003051 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003052 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3053 !VarType->isPointerType() &&
3054 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3055 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3056 << SemaRef.getLangOpts().CPlusPlus;
3057 HasErrors = true;
3058 }
3059
Alexey Bataev4acb8592014-07-07 13:01:15 +00003060 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3061 // Construct
3062 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3063 // parallel for construct is (are) private.
3064 // The loop iteration variable in the associated for-loop of a simd construct
3065 // with just one associated for-loop is linear with a constant-linear-step
3066 // that is the increment of the associated for-loop.
3067 // Exclude loop var from the list of variables with implicitly defined data
3068 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003069 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003070
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003071 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3072 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003073 // The loop iteration variable in the associated for-loop of a simd construct
3074 // with just one associated for-loop may be listed in a linear clause with a
3075 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003076 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3077 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003078 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003079 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3080 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3081 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003082 auto PredeterminedCKind =
3083 isOpenMPSimdDirective(DKind)
3084 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3085 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003086 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003087 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00003088 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
3089 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003090 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
3091 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3092 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003093 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003094 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3095 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003096 if (DVar.RefExpr == nullptr)
3097 DVar.CKind = PredeterminedCKind;
3098 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003099 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003100 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003101 // Make the loop iteration variable private (for worksharing constructs),
3102 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003103 // lastprivate (for simd directives with several collapsed or ordered
3104 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003105 if (DVar.CKind == OMPC_unknown)
3106 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3107 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003108 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003109 }
3110
Alexey Bataev7ff55242014-06-19 09:13:45 +00003111 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003112
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003113 // Check test-expr.
3114 HasErrors |= ISC.CheckCond(For->getCond());
3115
3116 // Check incr-expr.
3117 HasErrors |= ISC.CheckInc(For->getInc());
3118
Alexander Musmana5f070a2014-10-01 06:03:56 +00003119 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003120 return HasErrors;
3121
Alexander Musmana5f070a2014-10-01 06:03:56 +00003122 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003123 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003124 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3125 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003126 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003127 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003128 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3129 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3130 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3131 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3132 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3133 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3134
Alexey Bataev62dbb972015-04-22 11:59:37 +00003135 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3136 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003137 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003138 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003139 ResultIterSpace.CounterInit == nullptr ||
3140 ResultIterSpace.CounterStep == nullptr);
3141
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003142 return HasErrors;
3143}
3144
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003145/// \brief Build 'VarRef = Start.
3146static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3147 ExprResult VarRef, ExprResult Start) {
3148 TransformToNewDefs Transform(SemaRef);
3149 // Build 'VarRef = Start.
3150 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3151 if (NewStart.isInvalid())
3152 return ExprError();
3153 NewStart = SemaRef.PerformImplicitConversion(
3154 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3155 Sema::AA_Converting,
3156 /*AllowExplicit=*/true);
3157 if (NewStart.isInvalid())
3158 return ExprError();
3159 NewStart = SemaRef.PerformImplicitConversion(
3160 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3161 /*AllowExplicit=*/true);
3162 if (!NewStart.isUsable())
3163 return ExprError();
3164
3165 auto Init =
3166 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3167 return Init;
3168}
3169
Alexander Musmana5f070a2014-10-01 06:03:56 +00003170/// \brief Build 'VarRef = Start + Iter * Step'.
3171static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3172 SourceLocation Loc, ExprResult VarRef,
3173 ExprResult Start, ExprResult Iter,
3174 ExprResult Step, bool Subtract) {
3175 // Add parentheses (for debugging purposes only).
3176 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3177 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3178 !Step.isUsable())
3179 return ExprError();
3180
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003181 TransformToNewDefs Transform(SemaRef);
3182 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3183 if (NewStep.isInvalid())
3184 return ExprError();
3185 NewStep = SemaRef.PerformImplicitConversion(
3186 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3187 Sema::AA_Converting,
3188 /*AllowExplicit=*/true);
3189 if (NewStep.isInvalid())
3190 return ExprError();
3191 ExprResult Update =
3192 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003193 if (!Update.isUsable())
3194 return ExprError();
3195
3196 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003197 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3198 if (NewStart.isInvalid())
3199 return ExprError();
3200 NewStart = SemaRef.PerformImplicitConversion(
3201 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3202 Sema::AA_Converting,
3203 /*AllowExplicit=*/true);
3204 if (NewStart.isInvalid())
3205 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003206 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003207 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003208 if (!Update.isUsable())
3209 return ExprError();
3210
3211 Update = SemaRef.PerformImplicitConversion(
3212 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3213 if (!Update.isUsable())
3214 return ExprError();
3215
3216 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3217 return Update;
3218}
3219
3220/// \brief Convert integer expression \a E to make it have at least \a Bits
3221/// bits.
3222static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3223 Sema &SemaRef) {
3224 if (E == nullptr)
3225 return ExprError();
3226 auto &C = SemaRef.Context;
3227 QualType OldType = E->getType();
3228 unsigned HasBits = C.getTypeSize(OldType);
3229 if (HasBits >= Bits)
3230 return ExprResult(E);
3231 // OK to convert to signed, because new type has more bits than old.
3232 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3233 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3234 true);
3235}
3236
3237/// \brief Check if the given expression \a E is a constant integer that fits
3238/// into \a Bits bits.
3239static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3240 if (E == nullptr)
3241 return false;
3242 llvm::APSInt Result;
3243 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3244 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3245 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003246}
3247
3248/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003249/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3250/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003251static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003252CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3253 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3254 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003255 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003256 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003257 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003258 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003259 // Found 'collapse' clause - calculate collapse number.
3260 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003261 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3262 NestedLoopCount += Result.getLimitedValue() - 1;
3263 }
3264 if (OrderedLoopCountExpr) {
3265 // Found 'ordered' clause - calculate collapse number.
3266 llvm::APSInt Result;
3267 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3268 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003269 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003270 // This is helper routine for loop directives (e.g., 'for', 'simd',
3271 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003272 SmallVector<LoopIterationSpace, 4> IterSpaces;
3273 IterSpaces.resize(NestedLoopCount);
3274 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003275 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003276 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003277 NestedLoopCount, CollapseLoopCountExpr,
3278 OrderedLoopCountExpr, VarsWithImplicitDSA,
3279 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003280 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003281 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003282 // OpenMP [2.8.1, simd construct, Restrictions]
3283 // All loops associated with the construct must be perfectly nested; that
3284 // is, there must be no intervening code nor any OpenMP directive between
3285 // any two loops.
3286 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003287 }
3288
Alexander Musmana5f070a2014-10-01 06:03:56 +00003289 Built.clear(/* size */ NestedLoopCount);
3290
3291 if (SemaRef.CurContext->isDependentContext())
3292 return NestedLoopCount;
3293
3294 // An example of what is generated for the following code:
3295 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003296 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003297 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003298 // for (k = 0; k < NK; ++k)
3299 // for (j = J0; j < NJ; j+=2) {
3300 // <loop body>
3301 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003302 //
3303 // We generate the code below.
3304 // Note: the loop body may be outlined in CodeGen.
3305 // Note: some counters may be C++ classes, operator- is used to find number of
3306 // iterations and operator+= to calculate counter value.
3307 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3308 // or i64 is currently supported).
3309 //
3310 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3311 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3312 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3313 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3314 // // similar updates for vars in clauses (e.g. 'linear')
3315 // <loop body (using local i and j)>
3316 // }
3317 // i = NI; // assign final values of counters
3318 // j = NJ;
3319 //
3320
3321 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3322 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003323 // Precondition tests if there is at least one iteration (all conditions are
3324 // true).
3325 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003326 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003327 ExprResult LastIteration32 = WidenIterationCount(
3328 32 /* Bits */, SemaRef.PerformImplicitConversion(
3329 N0->IgnoreImpCasts(), N0->getType(),
3330 Sema::AA_Converting, /*AllowExplicit=*/true)
3331 .get(),
3332 SemaRef);
3333 ExprResult LastIteration64 = WidenIterationCount(
3334 64 /* Bits */, SemaRef.PerformImplicitConversion(
3335 N0->IgnoreImpCasts(), N0->getType(),
3336 Sema::AA_Converting, /*AllowExplicit=*/true)
3337 .get(),
3338 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003339
3340 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3341 return NestedLoopCount;
3342
3343 auto &C = SemaRef.Context;
3344 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3345
3346 Scope *CurScope = DSA.getCurScope();
3347 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003348 if (PreCond.isUsable()) {
3349 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3350 PreCond.get(), IterSpaces[Cnt].PreCond);
3351 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003352 auto N = IterSpaces[Cnt].NumIterations;
3353 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3354 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003355 LastIteration32 = SemaRef.BuildBinOp(
3356 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3357 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3358 Sema::AA_Converting,
3359 /*AllowExplicit=*/true)
3360 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003361 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003362 LastIteration64 = SemaRef.BuildBinOp(
3363 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3364 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3365 Sema::AA_Converting,
3366 /*AllowExplicit=*/true)
3367 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003368 }
3369
3370 // Choose either the 32-bit or 64-bit version.
3371 ExprResult LastIteration = LastIteration64;
3372 if (LastIteration32.isUsable() &&
3373 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3374 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3375 FitsInto(
3376 32 /* Bits */,
3377 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3378 LastIteration64.get(), SemaRef)))
3379 LastIteration = LastIteration32;
3380
3381 if (!LastIteration.isUsable())
3382 return 0;
3383
3384 // Save the number of iterations.
3385 ExprResult NumIterations = LastIteration;
3386 {
3387 LastIteration = SemaRef.BuildBinOp(
3388 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3389 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3390 if (!LastIteration.isUsable())
3391 return 0;
3392 }
3393
3394 // Calculate the last iteration number beforehand instead of doing this on
3395 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3396 llvm::APSInt Result;
3397 bool IsConstant =
3398 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3399 ExprResult CalcLastIteration;
3400 if (!IsConstant) {
3401 SourceLocation SaveLoc;
3402 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003403 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003404 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003405 ExprResult SaveRef = buildDeclRefExpr(
3406 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003407 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3408 SaveRef.get(), LastIteration.get());
3409 LastIteration = SaveRef;
3410
3411 // Prepare SaveRef + 1.
3412 NumIterations = SemaRef.BuildBinOp(
3413 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3414 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3415 if (!NumIterations.isUsable())
3416 return 0;
3417 }
3418
3419 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3420
Alexander Musmanc6388682014-12-15 07:07:06 +00003421 QualType VType = LastIteration.get()->getType();
3422 // Build variables passed into runtime, nesessary for worksharing directives.
3423 ExprResult LB, UB, IL, ST, EUB;
3424 if (isOpenMPWorksharingDirective(DKind)) {
3425 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003426 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3427 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003428 SemaRef.AddInitializerToDecl(
3429 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3430 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3431
3432 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003433 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3434 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003435 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3436 /*DirectInit*/ false,
3437 /*TypeMayContainAuto*/ false);
3438
3439 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3440 // This will be used to implement clause 'lastprivate'.
3441 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003442 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3443 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003444 SemaRef.AddInitializerToDecl(
3445 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3446 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3447
3448 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003449 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3450 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003451 SemaRef.AddInitializerToDecl(
3452 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3453 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3454
3455 // Build expression: UB = min(UB, LastIteration)
3456 // It is nesessary for CodeGen of directives with static scheduling.
3457 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3458 UB.get(), LastIteration.get());
3459 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3460 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3461 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3462 CondOp.get());
3463 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3464 }
3465
3466 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003467 ExprResult IV;
3468 ExprResult Init;
3469 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003470 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3471 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003472 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3473 ? LB.get()
3474 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3475 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3476 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003477 }
3478
Alexander Musmanc6388682014-12-15 07:07:06 +00003479 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003480 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003481 ExprResult Cond =
3482 isOpenMPWorksharingDirective(DKind)
3483 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3484 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3485 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003486
3487 // Loop increment (IV = IV + 1)
3488 SourceLocation IncLoc;
3489 ExprResult Inc =
3490 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3491 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3492 if (!Inc.isUsable())
3493 return 0;
3494 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003495 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3496 if (!Inc.isUsable())
3497 return 0;
3498
3499 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3500 // Used for directives with static scheduling.
3501 ExprResult NextLB, NextUB;
3502 if (isOpenMPWorksharingDirective(DKind)) {
3503 // LB + ST
3504 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3505 if (!NextLB.isUsable())
3506 return 0;
3507 // LB = LB + ST
3508 NextLB =
3509 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3510 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3511 if (!NextLB.isUsable())
3512 return 0;
3513 // UB + ST
3514 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3515 if (!NextUB.isUsable())
3516 return 0;
3517 // UB = UB + ST
3518 NextUB =
3519 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3520 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3521 if (!NextUB.isUsable())
3522 return 0;
3523 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003524
3525 // Build updates and final values of the loop counters.
3526 bool HasErrors = false;
3527 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003528 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003529 Built.Updates.resize(NestedLoopCount);
3530 Built.Finals.resize(NestedLoopCount);
3531 {
3532 ExprResult Div;
3533 // Go from inner nested loop to outer.
3534 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3535 LoopIterationSpace &IS = IterSpaces[Cnt];
3536 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3537 // Build: Iter = (IV / Div) % IS.NumIters
3538 // where Div is product of previous iterations' IS.NumIters.
3539 ExprResult Iter;
3540 if (Div.isUsable()) {
3541 Iter =
3542 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3543 } else {
3544 Iter = IV;
3545 assert((Cnt == (int)NestedLoopCount - 1) &&
3546 "unusable div expected on first iteration only");
3547 }
3548
3549 if (Cnt != 0 && Iter.isUsable())
3550 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3551 IS.NumIterations);
3552 if (!Iter.isUsable()) {
3553 HasErrors = true;
3554 break;
3555 }
3556
Alexey Bataev39f915b82015-05-08 10:41:21 +00003557 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3558 auto *CounterVar = buildDeclRefExpr(
3559 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3560 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3561 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003562 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3563 IS.CounterInit);
3564 if (!Init.isUsable()) {
3565 HasErrors = true;
3566 break;
3567 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003568 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003569 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003570 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3571 if (!Update.isUsable()) {
3572 HasErrors = true;
3573 break;
3574 }
3575
3576 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3577 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003578 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003579 IS.NumIterations, IS.CounterStep, IS.Subtract);
3580 if (!Final.isUsable()) {
3581 HasErrors = true;
3582 break;
3583 }
3584
3585 // Build Div for the next iteration: Div <- Div * IS.NumIters
3586 if (Cnt != 0) {
3587 if (Div.isUnset())
3588 Div = IS.NumIterations;
3589 else
3590 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3591 IS.NumIterations);
3592
3593 // Add parentheses (for debugging purposes only).
3594 if (Div.isUsable())
3595 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3596 if (!Div.isUsable()) {
3597 HasErrors = true;
3598 break;
3599 }
3600 }
3601 if (!Update.isUsable() || !Final.isUsable()) {
3602 HasErrors = true;
3603 break;
3604 }
3605 // Save results
3606 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003607 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003608 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003609 Built.Updates[Cnt] = Update.get();
3610 Built.Finals[Cnt] = Final.get();
3611 }
3612 }
3613
3614 if (HasErrors)
3615 return 0;
3616
3617 // Save results
3618 Built.IterationVarRef = IV.get();
3619 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003620 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003621 Built.CalcLastIteration =
3622 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003623 Built.PreCond = PreCond.get();
3624 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003625 Built.Init = Init.get();
3626 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003627 Built.LB = LB.get();
3628 Built.UB = UB.get();
3629 Built.IL = IL.get();
3630 Built.ST = ST.get();
3631 Built.EUB = EUB.get();
3632 Built.NLB = NextLB.get();
3633 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003634
Alexey Bataevabfc0692014-06-25 06:52:00 +00003635 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003636}
3637
Alexey Bataev10e775f2015-07-30 11:36:16 +00003638static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003639 auto CollapseClauses =
3640 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3641 if (CollapseClauses.begin() != CollapseClauses.end())
3642 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003643 return nullptr;
3644}
3645
Alexey Bataev10e775f2015-07-30 11:36:16 +00003646static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003647 auto OrderedClauses =
3648 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3649 if (OrderedClauses.begin() != OrderedClauses.end())
3650 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003651 return nullptr;
3652}
3653
Alexey Bataev66b15b52015-08-21 11:14:16 +00003654static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3655 const Expr *Safelen) {
3656 llvm::APSInt SimdlenRes, SafelenRes;
3657 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3658 Simdlen->isInstantiationDependent() ||
3659 Simdlen->containsUnexpandedParameterPack())
3660 return false;
3661 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3662 Safelen->isInstantiationDependent() ||
3663 Safelen->containsUnexpandedParameterPack())
3664 return false;
3665 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3666 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3667 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3668 // If both simdlen and safelen clauses are specified, the value of the simdlen
3669 // parameter must be less than or equal to the value of the safelen parameter.
3670 if (SimdlenRes > SafelenRes) {
3671 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3672 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3673 return true;
3674 }
3675 return false;
3676}
3677
Alexey Bataev4acb8592014-07-07 13:01:15 +00003678StmtResult Sema::ActOnOpenMPSimdDirective(
3679 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3680 SourceLocation EndLoc,
3681 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003682 if (!AStmt)
3683 return StmtError();
3684
3685 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003686 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003687 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3688 // define the nested loops number.
3689 unsigned NestedLoopCount = CheckOpenMPLoop(
3690 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3691 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003692 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003693 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003694
Alexander Musmana5f070a2014-10-01 06:03:56 +00003695 assert((CurContext->isDependentContext() || B.builtAll()) &&
3696 "omp simd loop exprs were not built");
3697
Alexander Musman3276a272015-03-21 10:12:56 +00003698 if (!CurContext->isDependentContext()) {
3699 // Finalize the clauses that need pre-built expressions for CodeGen.
3700 for (auto C : Clauses) {
3701 if (auto LC = dyn_cast<OMPLinearClause>(C))
3702 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3703 B.NumIterations, *this, CurScope))
3704 return StmtError();
3705 }
3706 }
3707
Alexey Bataev66b15b52015-08-21 11:14:16 +00003708 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3709 // If both simdlen and safelen clauses are specified, the value of the simdlen
3710 // parameter must be less than or equal to the value of the safelen parameter.
3711 OMPSafelenClause *Safelen = nullptr;
3712 OMPSimdlenClause *Simdlen = nullptr;
3713 for (auto *Clause : Clauses) {
3714 if (Clause->getClauseKind() == OMPC_safelen)
3715 Safelen = cast<OMPSafelenClause>(Clause);
3716 else if (Clause->getClauseKind() == OMPC_simdlen)
3717 Simdlen = cast<OMPSimdlenClause>(Clause);
3718 if (Safelen && Simdlen)
3719 break;
3720 }
3721 if (Simdlen && Safelen &&
3722 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3723 Safelen->getSafelen()))
3724 return StmtError();
3725
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003726 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003727 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3728 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003729}
3730
Alexey Bataev4acb8592014-07-07 13:01:15 +00003731StmtResult Sema::ActOnOpenMPForDirective(
3732 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3733 SourceLocation EndLoc,
3734 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003735 if (!AStmt)
3736 return StmtError();
3737
3738 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003739 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003740 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3741 // define the nested loops number.
3742 unsigned NestedLoopCount = CheckOpenMPLoop(
3743 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3744 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003745 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003746 return StmtError();
3747
Alexander Musmana5f070a2014-10-01 06:03:56 +00003748 assert((CurContext->isDependentContext() || B.builtAll()) &&
3749 "omp for loop exprs were not built");
3750
Alexey Bataev54acd402015-08-04 11:18:19 +00003751 if (!CurContext->isDependentContext()) {
3752 // Finalize the clauses that need pre-built expressions for CodeGen.
3753 for (auto C : Clauses) {
3754 if (auto LC = dyn_cast<OMPLinearClause>(C))
3755 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3756 B.NumIterations, *this, CurScope))
3757 return StmtError();
3758 }
3759 }
3760
Alexey Bataevf29276e2014-06-18 04:14:57 +00003761 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003762 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00003763 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003764}
3765
Alexander Musmanf82886e2014-09-18 05:12:34 +00003766StmtResult Sema::ActOnOpenMPForSimdDirective(
3767 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3768 SourceLocation EndLoc,
3769 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003770 if (!AStmt)
3771 return StmtError();
3772
3773 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003774 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003775 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3776 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003777 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003778 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3779 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3780 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003781 if (NestedLoopCount == 0)
3782 return StmtError();
3783
Alexander Musmanc6388682014-12-15 07:07:06 +00003784 assert((CurContext->isDependentContext() || B.builtAll()) &&
3785 "omp for simd loop exprs were not built");
3786
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003787 if (!CurContext->isDependentContext()) {
3788 // Finalize the clauses that need pre-built expressions for CodeGen.
3789 for (auto C : Clauses) {
3790 if (auto LC = dyn_cast<OMPLinearClause>(C))
3791 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3792 B.NumIterations, *this, CurScope))
3793 return StmtError();
3794 }
3795 }
3796
Alexey Bataev66b15b52015-08-21 11:14:16 +00003797 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3798 // If both simdlen and safelen clauses are specified, the value of the simdlen
3799 // parameter must be less than or equal to the value of the safelen parameter.
3800 OMPSafelenClause *Safelen = nullptr;
3801 OMPSimdlenClause *Simdlen = nullptr;
3802 for (auto *Clause : Clauses) {
3803 if (Clause->getClauseKind() == OMPC_safelen)
3804 Safelen = cast<OMPSafelenClause>(Clause);
3805 else if (Clause->getClauseKind() == OMPC_simdlen)
3806 Simdlen = cast<OMPSimdlenClause>(Clause);
3807 if (Safelen && Simdlen)
3808 break;
3809 }
3810 if (Simdlen && Safelen &&
3811 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3812 Safelen->getSafelen()))
3813 return StmtError();
3814
Alexander Musmanf82886e2014-09-18 05:12:34 +00003815 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003816 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3817 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003818}
3819
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003820StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3821 Stmt *AStmt,
3822 SourceLocation StartLoc,
3823 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003824 if (!AStmt)
3825 return StmtError();
3826
3827 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003828 auto BaseStmt = AStmt;
3829 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3830 BaseStmt = CS->getCapturedStmt();
3831 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3832 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003833 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003834 return StmtError();
3835 // All associated statements must be '#pragma omp section' except for
3836 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003837 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003838 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3839 if (SectionStmt)
3840 Diag(SectionStmt->getLocStart(),
3841 diag::err_omp_sections_substmt_not_section);
3842 return StmtError();
3843 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00003844 cast<OMPSectionDirective>(SectionStmt)
3845 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003846 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003847 } else {
3848 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3849 return StmtError();
3850 }
3851
3852 getCurFunction()->setHasBranchProtectedScope();
3853
Alexey Bataev25e5b442015-09-15 12:52:43 +00003854 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3855 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003856}
3857
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003858StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3859 SourceLocation StartLoc,
3860 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003861 if (!AStmt)
3862 return StmtError();
3863
3864 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003865
3866 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00003867 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003868
Alexey Bataev25e5b442015-09-15 12:52:43 +00003869 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
3870 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003871}
3872
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003873StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3874 Stmt *AStmt,
3875 SourceLocation StartLoc,
3876 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003877 if (!AStmt)
3878 return StmtError();
3879
3880 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00003881
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003882 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003883
Alexey Bataev3255bf32015-01-19 05:20:46 +00003884 // OpenMP [2.7.3, single Construct, Restrictions]
3885 // The copyprivate clause must not be used with the nowait clause.
3886 OMPClause *Nowait = nullptr;
3887 OMPClause *Copyprivate = nullptr;
3888 for (auto *Clause : Clauses) {
3889 if (Clause->getClauseKind() == OMPC_nowait)
3890 Nowait = Clause;
3891 else if (Clause->getClauseKind() == OMPC_copyprivate)
3892 Copyprivate = Clause;
3893 if (Copyprivate && Nowait) {
3894 Diag(Copyprivate->getLocStart(),
3895 diag::err_omp_single_copyprivate_with_nowait);
3896 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3897 return StmtError();
3898 }
3899 }
3900
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003901 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3902}
3903
Alexander Musman80c22892014-07-17 08:54:58 +00003904StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3905 SourceLocation StartLoc,
3906 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003907 if (!AStmt)
3908 return StmtError();
3909
3910 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00003911
3912 getCurFunction()->setHasBranchProtectedScope();
3913
3914 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3915}
3916
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003917StmtResult
3918Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3919 Stmt *AStmt, SourceLocation StartLoc,
3920 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003921 if (!AStmt)
3922 return StmtError();
3923
3924 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003925
3926 getCurFunction()->setHasBranchProtectedScope();
3927
3928 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3929 AStmt);
3930}
3931
Alexey Bataev4acb8592014-07-07 13:01:15 +00003932StmtResult Sema::ActOnOpenMPParallelForDirective(
3933 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3934 SourceLocation EndLoc,
3935 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003936 if (!AStmt)
3937 return StmtError();
3938
Alexey Bataev4acb8592014-07-07 13:01:15 +00003939 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3940 // 1.2.2 OpenMP Language Terminology
3941 // Structured block - An executable statement with a single entry at the
3942 // top and a single exit at the bottom.
3943 // The point of exit cannot be a branch out of the structured block.
3944 // longjmp() and throw() must not violate the entry/exit criteria.
3945 CS->getCapturedDecl()->setNothrow();
3946
Alexander Musmanc6388682014-12-15 07:07:06 +00003947 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003948 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3949 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003950 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003951 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3952 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3953 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003954 if (NestedLoopCount == 0)
3955 return StmtError();
3956
Alexander Musmana5f070a2014-10-01 06:03:56 +00003957 assert((CurContext->isDependentContext() || B.builtAll()) &&
3958 "omp parallel for loop exprs were not built");
3959
Alexey Bataev54acd402015-08-04 11:18:19 +00003960 if (!CurContext->isDependentContext()) {
3961 // Finalize the clauses that need pre-built expressions for CodeGen.
3962 for (auto C : Clauses) {
3963 if (auto LC = dyn_cast<OMPLinearClause>(C))
3964 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3965 B.NumIterations, *this, CurScope))
3966 return StmtError();
3967 }
3968 }
3969
Alexey Bataev4acb8592014-07-07 13:01:15 +00003970 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003971 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00003972 NestedLoopCount, Clauses, AStmt, B,
3973 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00003974}
3975
Alexander Musmane4e893b2014-09-23 09:33:00 +00003976StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3977 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3978 SourceLocation EndLoc,
3979 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003980 if (!AStmt)
3981 return StmtError();
3982
Alexander Musmane4e893b2014-09-23 09:33:00 +00003983 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3984 // 1.2.2 OpenMP Language Terminology
3985 // Structured block - An executable statement with a single entry at the
3986 // top and a single exit at the bottom.
3987 // The point of exit cannot be a branch out of the structured block.
3988 // longjmp() and throw() must not violate the entry/exit criteria.
3989 CS->getCapturedDecl()->setNothrow();
3990
Alexander Musmanc6388682014-12-15 07:07:06 +00003991 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003992 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3993 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003994 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003995 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3996 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3997 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003998 if (NestedLoopCount == 0)
3999 return StmtError();
4000
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004001 if (!CurContext->isDependentContext()) {
4002 // Finalize the clauses that need pre-built expressions for CodeGen.
4003 for (auto C : Clauses) {
4004 if (auto LC = dyn_cast<OMPLinearClause>(C))
4005 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4006 B.NumIterations, *this, CurScope))
4007 return StmtError();
4008 }
4009 }
4010
Alexey Bataev66b15b52015-08-21 11:14:16 +00004011 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4012 // If both simdlen and safelen clauses are specified, the value of the simdlen
4013 // parameter must be less than or equal to the value of the safelen parameter.
4014 OMPSafelenClause *Safelen = nullptr;
4015 OMPSimdlenClause *Simdlen = nullptr;
4016 for (auto *Clause : Clauses) {
4017 if (Clause->getClauseKind() == OMPC_safelen)
4018 Safelen = cast<OMPSafelenClause>(Clause);
4019 else if (Clause->getClauseKind() == OMPC_simdlen)
4020 Simdlen = cast<OMPSimdlenClause>(Clause);
4021 if (Safelen && Simdlen)
4022 break;
4023 }
4024 if (Simdlen && Safelen &&
4025 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4026 Safelen->getSafelen()))
4027 return StmtError();
4028
Alexander Musmane4e893b2014-09-23 09:33:00 +00004029 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004030 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004031 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004032}
4033
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004034StmtResult
4035Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4036 Stmt *AStmt, SourceLocation StartLoc,
4037 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004038 if (!AStmt)
4039 return StmtError();
4040
4041 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004042 auto BaseStmt = AStmt;
4043 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4044 BaseStmt = CS->getCapturedStmt();
4045 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4046 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004047 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004048 return StmtError();
4049 // All associated statements must be '#pragma omp section' except for
4050 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004051 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004052 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4053 if (SectionStmt)
4054 Diag(SectionStmt->getLocStart(),
4055 diag::err_omp_parallel_sections_substmt_not_section);
4056 return StmtError();
4057 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004058 cast<OMPSectionDirective>(SectionStmt)
4059 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004060 }
4061 } else {
4062 Diag(AStmt->getLocStart(),
4063 diag::err_omp_parallel_sections_not_compound_stmt);
4064 return StmtError();
4065 }
4066
4067 getCurFunction()->setHasBranchProtectedScope();
4068
Alexey Bataev25e5b442015-09-15 12:52:43 +00004069 return OMPParallelSectionsDirective::Create(
4070 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004071}
4072
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004073StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4074 Stmt *AStmt, SourceLocation StartLoc,
4075 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004076 if (!AStmt)
4077 return StmtError();
4078
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004079 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4080 // 1.2.2 OpenMP Language Terminology
4081 // Structured block - An executable statement with a single entry at the
4082 // top and a single exit at the bottom.
4083 // The point of exit cannot be a branch out of the structured block.
4084 // longjmp() and throw() must not violate the entry/exit criteria.
4085 CS->getCapturedDecl()->setNothrow();
4086
4087 getCurFunction()->setHasBranchProtectedScope();
4088
Alexey Bataev25e5b442015-09-15 12:52:43 +00004089 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4090 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004091}
4092
Alexey Bataev68446b72014-07-18 07:47:19 +00004093StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4094 SourceLocation EndLoc) {
4095 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4096}
4097
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004098StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4099 SourceLocation EndLoc) {
4100 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4101}
4102
Alexey Bataev2df347a2014-07-18 10:17:07 +00004103StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4104 SourceLocation EndLoc) {
4105 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4106}
4107
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004108StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4109 SourceLocation StartLoc,
4110 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004111 if (!AStmt)
4112 return StmtError();
4113
4114 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004115
4116 getCurFunction()->setHasBranchProtectedScope();
4117
4118 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4119}
4120
Alexey Bataev6125da92014-07-21 11:26:11 +00004121StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4122 SourceLocation StartLoc,
4123 SourceLocation EndLoc) {
4124 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4125 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4126}
4127
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004128StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
4129 SourceLocation StartLoc,
4130 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004131 if (!AStmt)
4132 return StmtError();
4133
4134 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004135
4136 getCurFunction()->setHasBranchProtectedScope();
4137
4138 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
4139}
4140
Alexey Bataev1d160b12015-03-13 12:27:31 +00004141namespace {
4142/// \brief Helper class for checking expression in 'omp atomic [update]'
4143/// construct.
4144class OpenMPAtomicUpdateChecker {
4145 /// \brief Error results for atomic update expressions.
4146 enum ExprAnalysisErrorCode {
4147 /// \brief A statement is not an expression statement.
4148 NotAnExpression,
4149 /// \brief Expression is not builtin binary or unary operation.
4150 NotABinaryOrUnaryExpression,
4151 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4152 NotAnUnaryIncDecExpression,
4153 /// \brief An expression is not of scalar type.
4154 NotAScalarType,
4155 /// \brief A binary operation is not an assignment operation.
4156 NotAnAssignmentOp,
4157 /// \brief RHS part of the binary operation is not a binary expression.
4158 NotABinaryExpression,
4159 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4160 /// expression.
4161 NotABinaryOperator,
4162 /// \brief RHS binary operation does not have reference to the updated LHS
4163 /// part.
4164 NotAnUpdateExpression,
4165 /// \brief No errors is found.
4166 NoError
4167 };
4168 /// \brief Reference to Sema.
4169 Sema &SemaRef;
4170 /// \brief A location for note diagnostics (when error is found).
4171 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004172 /// \brief 'x' lvalue part of the source atomic expression.
4173 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004174 /// \brief 'expr' rvalue part of the source atomic expression.
4175 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004176 /// \brief Helper expression of the form
4177 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4178 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4179 Expr *UpdateExpr;
4180 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4181 /// important for non-associative operations.
4182 bool IsXLHSInRHSPart;
4183 BinaryOperatorKind Op;
4184 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004185 /// \brief true if the source expression is a postfix unary operation, false
4186 /// if it is a prefix unary operation.
4187 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004188
4189public:
4190 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004191 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004192 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004193 /// \brief Check specified statement that it is suitable for 'atomic update'
4194 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004195 /// expression. If DiagId and NoteId == 0, then only check is performed
4196 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004197 /// \param DiagId Diagnostic which should be emitted if error is found.
4198 /// \param NoteId Diagnostic note for the main error message.
4199 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004200 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004201 /// \brief Return the 'x' lvalue part of the source atomic expression.
4202 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004203 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4204 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004205 /// \brief Return the update expression used in calculation of the updated
4206 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4207 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4208 Expr *getUpdateExpr() const { return UpdateExpr; }
4209 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4210 /// false otherwise.
4211 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4212
Alexey Bataevb78ca832015-04-01 03:33:17 +00004213 /// \brief true if the source expression is a postfix unary operation, false
4214 /// if it is a prefix unary operation.
4215 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4216
Alexey Bataev1d160b12015-03-13 12:27:31 +00004217private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004218 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4219 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004220};
4221} // namespace
4222
4223bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4224 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4225 ExprAnalysisErrorCode ErrorFound = NoError;
4226 SourceLocation ErrorLoc, NoteLoc;
4227 SourceRange ErrorRange, NoteRange;
4228 // Allowed constructs are:
4229 // x = x binop expr;
4230 // x = expr binop x;
4231 if (AtomicBinOp->getOpcode() == BO_Assign) {
4232 X = AtomicBinOp->getLHS();
4233 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4234 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4235 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4236 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4237 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004238 Op = AtomicInnerBinOp->getOpcode();
4239 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004240 auto *LHS = AtomicInnerBinOp->getLHS();
4241 auto *RHS = AtomicInnerBinOp->getRHS();
4242 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4243 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4244 /*Canonical=*/true);
4245 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4246 /*Canonical=*/true);
4247 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4248 /*Canonical=*/true);
4249 if (XId == LHSId) {
4250 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004251 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004252 } else if (XId == RHSId) {
4253 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004254 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004255 } else {
4256 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4257 ErrorRange = AtomicInnerBinOp->getSourceRange();
4258 NoteLoc = X->getExprLoc();
4259 NoteRange = X->getSourceRange();
4260 ErrorFound = NotAnUpdateExpression;
4261 }
4262 } else {
4263 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4264 ErrorRange = AtomicInnerBinOp->getSourceRange();
4265 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4266 NoteRange = SourceRange(NoteLoc, NoteLoc);
4267 ErrorFound = NotABinaryOperator;
4268 }
4269 } else {
4270 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4271 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4272 ErrorFound = NotABinaryExpression;
4273 }
4274 } else {
4275 ErrorLoc = AtomicBinOp->getExprLoc();
4276 ErrorRange = AtomicBinOp->getSourceRange();
4277 NoteLoc = AtomicBinOp->getOperatorLoc();
4278 NoteRange = SourceRange(NoteLoc, NoteLoc);
4279 ErrorFound = NotAnAssignmentOp;
4280 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004281 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004282 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4283 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4284 return true;
4285 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004286 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004287 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004288}
4289
4290bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4291 unsigned NoteId) {
4292 ExprAnalysisErrorCode ErrorFound = NoError;
4293 SourceLocation ErrorLoc, NoteLoc;
4294 SourceRange ErrorRange, NoteRange;
4295 // Allowed constructs are:
4296 // x++;
4297 // x--;
4298 // ++x;
4299 // --x;
4300 // x binop= expr;
4301 // x = x binop expr;
4302 // x = expr binop x;
4303 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4304 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4305 if (AtomicBody->getType()->isScalarType() ||
4306 AtomicBody->isInstantiationDependent()) {
4307 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4308 AtomicBody->IgnoreParenImpCasts())) {
4309 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004310 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004311 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004312 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004313 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004314 X = AtomicCompAssignOp->getLHS();
4315 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004316 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4317 AtomicBody->IgnoreParenImpCasts())) {
4318 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004319 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4320 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004321 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004322 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4323 // Check for Unary Operation
4324 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004325 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004326 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4327 OpLoc = AtomicUnaryOp->getOperatorLoc();
4328 X = AtomicUnaryOp->getSubExpr();
4329 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4330 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004331 } else {
4332 ErrorFound = NotAnUnaryIncDecExpression;
4333 ErrorLoc = AtomicUnaryOp->getExprLoc();
4334 ErrorRange = AtomicUnaryOp->getSourceRange();
4335 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4336 NoteRange = SourceRange(NoteLoc, NoteLoc);
4337 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004338 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004339 ErrorFound = NotABinaryOrUnaryExpression;
4340 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4341 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4342 }
4343 } else {
4344 ErrorFound = NotAScalarType;
4345 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4346 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4347 }
4348 } else {
4349 ErrorFound = NotAnExpression;
4350 NoteLoc = ErrorLoc = S->getLocStart();
4351 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4352 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004353 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004354 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4355 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4356 return true;
4357 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004358 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004359 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004360 // Build an update expression of form 'OpaqueValueExpr(x) binop
4361 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4362 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4363 auto *OVEX = new (SemaRef.getASTContext())
4364 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4365 auto *OVEExpr = new (SemaRef.getASTContext())
4366 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4367 auto Update =
4368 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4369 IsXLHSInRHSPart ? OVEExpr : OVEX);
4370 if (Update.isInvalid())
4371 return true;
4372 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4373 Sema::AA_Casting);
4374 if (Update.isInvalid())
4375 return true;
4376 UpdateExpr = Update.get();
4377 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004378 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004379}
4380
Alexey Bataev0162e452014-07-22 10:10:35 +00004381StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4382 Stmt *AStmt,
4383 SourceLocation StartLoc,
4384 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004385 if (!AStmt)
4386 return StmtError();
4387
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004388 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004389 // 1.2.2 OpenMP Language Terminology
4390 // Structured block - An executable statement with a single entry at the
4391 // top and a single exit at the bottom.
4392 // The point of exit cannot be a branch out of the structured block.
4393 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004394 OpenMPClauseKind AtomicKind = OMPC_unknown;
4395 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004396 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004397 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004398 C->getClauseKind() == OMPC_update ||
4399 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004400 if (AtomicKind != OMPC_unknown) {
4401 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4402 << SourceRange(C->getLocStart(), C->getLocEnd());
4403 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4404 << getOpenMPClauseName(AtomicKind);
4405 } else {
4406 AtomicKind = C->getClauseKind();
4407 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004408 }
4409 }
4410 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004411
Alexey Bataev459dec02014-07-24 06:46:57 +00004412 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004413 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4414 Body = EWC->getSubExpr();
4415
Alexey Bataev62cec442014-11-18 10:14:22 +00004416 Expr *X = nullptr;
4417 Expr *V = nullptr;
4418 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004419 Expr *UE = nullptr;
4420 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004421 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004422 // OpenMP [2.12.6, atomic Construct]
4423 // In the next expressions:
4424 // * x and v (as applicable) are both l-value expressions with scalar type.
4425 // * During the execution of an atomic region, multiple syntactic
4426 // occurrences of x must designate the same storage location.
4427 // * Neither of v and expr (as applicable) may access the storage location
4428 // designated by x.
4429 // * Neither of x and expr (as applicable) may access the storage location
4430 // designated by v.
4431 // * expr is an expression with scalar type.
4432 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4433 // * binop, binop=, ++, and -- are not overloaded operators.
4434 // * The expression x binop expr must be numerically equivalent to x binop
4435 // (expr). This requirement is satisfied if the operators in expr have
4436 // precedence greater than binop, or by using parentheses around expr or
4437 // subexpressions of expr.
4438 // * The expression expr binop x must be numerically equivalent to (expr)
4439 // binop x. This requirement is satisfied if the operators in expr have
4440 // precedence equal to or greater than binop, or by using parentheses around
4441 // expr or subexpressions of expr.
4442 // * For forms that allow multiple occurrences of x, the number of times
4443 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004444 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004445 enum {
4446 NotAnExpression,
4447 NotAnAssignmentOp,
4448 NotAScalarType,
4449 NotAnLValue,
4450 NoError
4451 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004452 SourceLocation ErrorLoc, NoteLoc;
4453 SourceRange ErrorRange, NoteRange;
4454 // If clause is read:
4455 // v = x;
4456 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4457 auto AtomicBinOp =
4458 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4459 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4460 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4461 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4462 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4463 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4464 if (!X->isLValue() || !V->isLValue()) {
4465 auto NotLValueExpr = X->isLValue() ? V : X;
4466 ErrorFound = NotAnLValue;
4467 ErrorLoc = AtomicBinOp->getExprLoc();
4468 ErrorRange = AtomicBinOp->getSourceRange();
4469 NoteLoc = NotLValueExpr->getExprLoc();
4470 NoteRange = NotLValueExpr->getSourceRange();
4471 }
4472 } else if (!X->isInstantiationDependent() ||
4473 !V->isInstantiationDependent()) {
4474 auto NotScalarExpr =
4475 (X->isInstantiationDependent() || X->getType()->isScalarType())
4476 ? V
4477 : X;
4478 ErrorFound = NotAScalarType;
4479 ErrorLoc = AtomicBinOp->getExprLoc();
4480 ErrorRange = AtomicBinOp->getSourceRange();
4481 NoteLoc = NotScalarExpr->getExprLoc();
4482 NoteRange = NotScalarExpr->getSourceRange();
4483 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004484 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004485 ErrorFound = NotAnAssignmentOp;
4486 ErrorLoc = AtomicBody->getExprLoc();
4487 ErrorRange = AtomicBody->getSourceRange();
4488 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4489 : AtomicBody->getExprLoc();
4490 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4491 : AtomicBody->getSourceRange();
4492 }
4493 } else {
4494 ErrorFound = NotAnExpression;
4495 NoteLoc = ErrorLoc = Body->getLocStart();
4496 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004497 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004498 if (ErrorFound != NoError) {
4499 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4500 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004501 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4502 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004503 return StmtError();
4504 } else if (CurContext->isDependentContext())
4505 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004506 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004507 enum {
4508 NotAnExpression,
4509 NotAnAssignmentOp,
4510 NotAScalarType,
4511 NotAnLValue,
4512 NoError
4513 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004514 SourceLocation ErrorLoc, NoteLoc;
4515 SourceRange ErrorRange, NoteRange;
4516 // If clause is write:
4517 // x = expr;
4518 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4519 auto AtomicBinOp =
4520 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4521 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004522 X = AtomicBinOp->getLHS();
4523 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004524 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4525 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4526 if (!X->isLValue()) {
4527 ErrorFound = NotAnLValue;
4528 ErrorLoc = AtomicBinOp->getExprLoc();
4529 ErrorRange = AtomicBinOp->getSourceRange();
4530 NoteLoc = X->getExprLoc();
4531 NoteRange = X->getSourceRange();
4532 }
4533 } else if (!X->isInstantiationDependent() ||
4534 !E->isInstantiationDependent()) {
4535 auto NotScalarExpr =
4536 (X->isInstantiationDependent() || X->getType()->isScalarType())
4537 ? E
4538 : X;
4539 ErrorFound = NotAScalarType;
4540 ErrorLoc = AtomicBinOp->getExprLoc();
4541 ErrorRange = AtomicBinOp->getSourceRange();
4542 NoteLoc = NotScalarExpr->getExprLoc();
4543 NoteRange = NotScalarExpr->getSourceRange();
4544 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004545 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004546 ErrorFound = NotAnAssignmentOp;
4547 ErrorLoc = AtomicBody->getExprLoc();
4548 ErrorRange = AtomicBody->getSourceRange();
4549 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4550 : AtomicBody->getExprLoc();
4551 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4552 : AtomicBody->getSourceRange();
4553 }
4554 } else {
4555 ErrorFound = NotAnExpression;
4556 NoteLoc = ErrorLoc = Body->getLocStart();
4557 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004558 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004559 if (ErrorFound != NoError) {
4560 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4561 << ErrorRange;
4562 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4563 << NoteRange;
4564 return StmtError();
4565 } else if (CurContext->isDependentContext())
4566 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004567 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004568 // If clause is update:
4569 // x++;
4570 // x--;
4571 // ++x;
4572 // --x;
4573 // x binop= expr;
4574 // x = x binop expr;
4575 // x = expr binop x;
4576 OpenMPAtomicUpdateChecker Checker(*this);
4577 if (Checker.checkStatement(
4578 Body, (AtomicKind == OMPC_update)
4579 ? diag::err_omp_atomic_update_not_expression_statement
4580 : diag::err_omp_atomic_not_expression_statement,
4581 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004582 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004583 if (!CurContext->isDependentContext()) {
4584 E = Checker.getExpr();
4585 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004586 UE = Checker.getUpdateExpr();
4587 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004588 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004589 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004590 enum {
4591 NotAnAssignmentOp,
4592 NotACompoundStatement,
4593 NotTwoSubstatements,
4594 NotASpecificExpression,
4595 NoError
4596 } ErrorFound = NoError;
4597 SourceLocation ErrorLoc, NoteLoc;
4598 SourceRange ErrorRange, NoteRange;
4599 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4600 // If clause is a capture:
4601 // v = x++;
4602 // v = x--;
4603 // v = ++x;
4604 // v = --x;
4605 // v = x binop= expr;
4606 // v = x = x binop expr;
4607 // v = x = expr binop x;
4608 auto *AtomicBinOp =
4609 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4610 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4611 V = AtomicBinOp->getLHS();
4612 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4613 OpenMPAtomicUpdateChecker Checker(*this);
4614 if (Checker.checkStatement(
4615 Body, diag::err_omp_atomic_capture_not_expression_statement,
4616 diag::note_omp_atomic_update))
4617 return StmtError();
4618 E = Checker.getExpr();
4619 X = Checker.getX();
4620 UE = Checker.getUpdateExpr();
4621 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4622 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004623 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004624 ErrorLoc = AtomicBody->getExprLoc();
4625 ErrorRange = AtomicBody->getSourceRange();
4626 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4627 : AtomicBody->getExprLoc();
4628 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4629 : AtomicBody->getSourceRange();
4630 ErrorFound = NotAnAssignmentOp;
4631 }
4632 if (ErrorFound != NoError) {
4633 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4634 << ErrorRange;
4635 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4636 return StmtError();
4637 } else if (CurContext->isDependentContext()) {
4638 UE = V = E = X = nullptr;
4639 }
4640 } else {
4641 // If clause is a capture:
4642 // { v = x; x = expr; }
4643 // { v = x; x++; }
4644 // { v = x; x--; }
4645 // { v = x; ++x; }
4646 // { v = x; --x; }
4647 // { v = x; x binop= expr; }
4648 // { v = x; x = x binop expr; }
4649 // { v = x; x = expr binop x; }
4650 // { x++; v = x; }
4651 // { x--; v = x; }
4652 // { ++x; v = x; }
4653 // { --x; v = x; }
4654 // { x binop= expr; v = x; }
4655 // { x = x binop expr; v = x; }
4656 // { x = expr binop x; v = x; }
4657 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4658 // Check that this is { expr1; expr2; }
4659 if (CS->size() == 2) {
4660 auto *First = CS->body_front();
4661 auto *Second = CS->body_back();
4662 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4663 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4664 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4665 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4666 // Need to find what subexpression is 'v' and what is 'x'.
4667 OpenMPAtomicUpdateChecker Checker(*this);
4668 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4669 BinaryOperator *BinOp = nullptr;
4670 if (IsUpdateExprFound) {
4671 BinOp = dyn_cast<BinaryOperator>(First);
4672 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4673 }
4674 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4675 // { v = x; x++; }
4676 // { v = x; x--; }
4677 // { v = x; ++x; }
4678 // { v = x; --x; }
4679 // { v = x; x binop= expr; }
4680 // { v = x; x = x binop expr; }
4681 // { v = x; x = expr binop x; }
4682 // Check that the first expression has form v = x.
4683 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4684 llvm::FoldingSetNodeID XId, PossibleXId;
4685 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4686 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4687 IsUpdateExprFound = XId == PossibleXId;
4688 if (IsUpdateExprFound) {
4689 V = BinOp->getLHS();
4690 X = Checker.getX();
4691 E = Checker.getExpr();
4692 UE = Checker.getUpdateExpr();
4693 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004694 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004695 }
4696 }
4697 if (!IsUpdateExprFound) {
4698 IsUpdateExprFound = !Checker.checkStatement(First);
4699 BinOp = nullptr;
4700 if (IsUpdateExprFound) {
4701 BinOp = dyn_cast<BinaryOperator>(Second);
4702 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4703 }
4704 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4705 // { x++; v = x; }
4706 // { x--; v = x; }
4707 // { ++x; v = x; }
4708 // { --x; v = x; }
4709 // { x binop= expr; v = x; }
4710 // { x = x binop expr; v = x; }
4711 // { x = expr binop x; v = x; }
4712 // Check that the second expression has form v = x.
4713 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4714 llvm::FoldingSetNodeID XId, PossibleXId;
4715 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4716 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4717 IsUpdateExprFound = XId == PossibleXId;
4718 if (IsUpdateExprFound) {
4719 V = BinOp->getLHS();
4720 X = Checker.getX();
4721 E = Checker.getExpr();
4722 UE = Checker.getUpdateExpr();
4723 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004724 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004725 }
4726 }
4727 }
4728 if (!IsUpdateExprFound) {
4729 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00004730 auto *FirstExpr = dyn_cast<Expr>(First);
4731 auto *SecondExpr = dyn_cast<Expr>(Second);
4732 if (!FirstExpr || !SecondExpr ||
4733 !(FirstExpr->isInstantiationDependent() ||
4734 SecondExpr->isInstantiationDependent())) {
4735 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4736 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004737 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00004738 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4739 : First->getLocStart();
4740 NoteRange = ErrorRange = FirstBinOp
4741 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00004742 : SourceRange(ErrorLoc, ErrorLoc);
4743 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004744 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4745 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4746 ErrorFound = NotAnAssignmentOp;
4747 NoteLoc = ErrorLoc = SecondBinOp
4748 ? SecondBinOp->getOperatorLoc()
4749 : Second->getLocStart();
4750 NoteRange = ErrorRange =
4751 SecondBinOp ? SecondBinOp->getSourceRange()
4752 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00004753 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004754 auto *PossibleXRHSInFirst =
4755 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4756 auto *PossibleXLHSInSecond =
4757 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4758 llvm::FoldingSetNodeID X1Id, X2Id;
4759 PossibleXRHSInFirst->Profile(X1Id, Context,
4760 /*Canonical=*/true);
4761 PossibleXLHSInSecond->Profile(X2Id, Context,
4762 /*Canonical=*/true);
4763 IsUpdateExprFound = X1Id == X2Id;
4764 if (IsUpdateExprFound) {
4765 V = FirstBinOp->getLHS();
4766 X = SecondBinOp->getLHS();
4767 E = SecondBinOp->getRHS();
4768 UE = nullptr;
4769 IsXLHSInRHSPart = false;
4770 IsPostfixUpdate = true;
4771 } else {
4772 ErrorFound = NotASpecificExpression;
4773 ErrorLoc = FirstBinOp->getExprLoc();
4774 ErrorRange = FirstBinOp->getSourceRange();
4775 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4776 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4777 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004778 }
4779 }
4780 }
4781 }
4782 } else {
4783 NoteLoc = ErrorLoc = Body->getLocStart();
4784 NoteRange = ErrorRange =
4785 SourceRange(Body->getLocStart(), Body->getLocStart());
4786 ErrorFound = NotTwoSubstatements;
4787 }
4788 } else {
4789 NoteLoc = ErrorLoc = Body->getLocStart();
4790 NoteRange = ErrorRange =
4791 SourceRange(Body->getLocStart(), Body->getLocStart());
4792 ErrorFound = NotACompoundStatement;
4793 }
4794 if (ErrorFound != NoError) {
4795 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4796 << ErrorRange;
4797 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4798 return StmtError();
4799 } else if (CurContext->isDependentContext()) {
4800 UE = V = E = X = nullptr;
4801 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004802 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004803 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004804
4805 getCurFunction()->setHasBranchProtectedScope();
4806
Alexey Bataev62cec442014-11-18 10:14:22 +00004807 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004808 X, V, E, UE, IsXLHSInRHSPart,
4809 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004810}
4811
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004812StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4813 Stmt *AStmt,
4814 SourceLocation StartLoc,
4815 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004816 if (!AStmt)
4817 return StmtError();
4818
4819 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004820
Alexey Bataev13314bf2014-10-09 04:18:56 +00004821 // OpenMP [2.16, Nesting of Regions]
4822 // If specified, a teams construct must be contained within a target
4823 // construct. That target construct must contain no statements or directives
4824 // outside of the teams construct.
4825 if (DSAStack->hasInnerTeamsRegion()) {
4826 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4827 bool OMPTeamsFound = true;
4828 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4829 auto I = CS->body_begin();
4830 while (I != CS->body_end()) {
4831 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4832 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4833 OMPTeamsFound = false;
4834 break;
4835 }
4836 ++I;
4837 }
4838 assert(I != CS->body_end() && "Not found statement");
4839 S = *I;
4840 }
4841 if (!OMPTeamsFound) {
4842 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4843 Diag(DSAStack->getInnerTeamsRegionLoc(),
4844 diag::note_omp_nested_teams_construct_here);
4845 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4846 << isa<OMPExecutableDirective>(S);
4847 return StmtError();
4848 }
4849 }
4850
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004851 getCurFunction()->setHasBranchProtectedScope();
4852
4853 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4854}
4855
Michael Wong65f367f2015-07-21 13:44:28 +00004856StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4857 Stmt *AStmt,
4858 SourceLocation StartLoc,
4859 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004860 if (!AStmt)
4861 return StmtError();
4862
4863 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4864
Michael Wong65f367f2015-07-21 13:44:28 +00004865 getCurFunction()->setHasBranchProtectedScope();
4866
4867 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4868 AStmt);
4869}
4870
Alexey Bataev13314bf2014-10-09 04:18:56 +00004871StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4872 Stmt *AStmt, SourceLocation StartLoc,
4873 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004874 if (!AStmt)
4875 return StmtError();
4876
Alexey Bataev13314bf2014-10-09 04:18:56 +00004877 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4878 // 1.2.2 OpenMP Language Terminology
4879 // Structured block - An executable statement with a single entry at the
4880 // top and a single exit at the bottom.
4881 // The point of exit cannot be a branch out of the structured block.
4882 // longjmp() and throw() must not violate the entry/exit criteria.
4883 CS->getCapturedDecl()->setNothrow();
4884
4885 getCurFunction()->setHasBranchProtectedScope();
4886
4887 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4888}
4889
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004890StmtResult
4891Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4892 SourceLocation EndLoc,
4893 OpenMPDirectiveKind CancelRegion) {
4894 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4895 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4896 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4897 << getOpenMPDirectiveName(CancelRegion);
4898 return StmtError();
4899 }
4900 if (DSAStack->isParentNowaitRegion()) {
4901 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4902 return StmtError();
4903 }
4904 if (DSAStack->isParentOrderedRegion()) {
4905 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4906 return StmtError();
4907 }
4908 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4909 CancelRegion);
4910}
4911
Alexey Bataev87933c72015-09-18 08:07:34 +00004912StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
4913 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00004914 SourceLocation EndLoc,
4915 OpenMPDirectiveKind CancelRegion) {
4916 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4917 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4918 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4919 << getOpenMPDirectiveName(CancelRegion);
4920 return StmtError();
4921 }
4922 if (DSAStack->isParentNowaitRegion()) {
4923 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4924 return StmtError();
4925 }
4926 if (DSAStack->isParentOrderedRegion()) {
4927 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4928 return StmtError();
4929 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004930 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00004931 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
4932 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00004933}
4934
Alexey Bataeved09d242014-05-28 05:53:51 +00004935OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004936 SourceLocation StartLoc,
4937 SourceLocation LParenLoc,
4938 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004939 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004940 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00004941 case OMPC_final:
4942 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4943 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004944 case OMPC_num_threads:
4945 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4946 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004947 case OMPC_safelen:
4948 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4949 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00004950 case OMPC_simdlen:
4951 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
4952 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004953 case OMPC_collapse:
4954 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4955 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004956 case OMPC_ordered:
4957 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4958 break;
Michael Wonge710d542015-08-07 16:16:36 +00004959 case OMPC_device:
4960 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
4961 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004962 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004963 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004964 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004965 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004966 case OMPC_private:
4967 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004968 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004969 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004970 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004971 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004972 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004973 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004974 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004975 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004976 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004977 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004978 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004979 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004980 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004981 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004982 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004983 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004984 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004985 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004986 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004987 llvm_unreachable("Clause is not allowed.");
4988 }
4989 return Res;
4990}
4991
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004992OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
4993 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004994 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004995 SourceLocation NameModifierLoc,
4996 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004997 SourceLocation EndLoc) {
4998 Expr *ValExpr = Condition;
4999 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5000 !Condition->isInstantiationDependent() &&
5001 !Condition->containsUnexpandedParameterPack()) {
5002 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005003 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005004 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005005 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005006
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005007 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005008 }
5009
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005010 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5011 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005012}
5013
Alexey Bataev3778b602014-07-17 07:32:53 +00005014OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5015 SourceLocation StartLoc,
5016 SourceLocation LParenLoc,
5017 SourceLocation EndLoc) {
5018 Expr *ValExpr = Condition;
5019 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5020 !Condition->isInstantiationDependent() &&
5021 !Condition->containsUnexpandedParameterPack()) {
5022 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5023 Condition->getExprLoc(), Condition);
5024 if (Val.isInvalid())
5025 return nullptr;
5026
5027 ValExpr = Val.get();
5028 }
5029
5030 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5031}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005032ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5033 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005034 if (!Op)
5035 return ExprError();
5036
5037 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5038 public:
5039 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005040 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005041 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5042 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005043 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5044 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005045 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5046 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005047 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5048 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005049 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5050 QualType T,
5051 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005052 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5053 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005054 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5055 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005056 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005057 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005058 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005059 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5060 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005061 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5062 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005063 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5064 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005065 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005066 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005067 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005068 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5069 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005070 llvm_unreachable("conversion functions are permitted");
5071 }
5072 } ConvertDiagnoser;
5073 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5074}
5075
5076OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5077 SourceLocation StartLoc,
5078 SourceLocation LParenLoc,
5079 SourceLocation EndLoc) {
5080 Expr *ValExpr = NumThreads;
5081 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00005082 !NumThreads->containsUnexpandedParameterPack()) {
5083 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
5084 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005085 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00005086 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005087 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005088
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005089 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00005090
5091 // OpenMP [2.5, Restrictions]
5092 // The num_threads expression must evaluate to a positive integer value.
5093 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00005094 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
5095 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005096 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
5097 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005098 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005099 }
5100 }
5101
Alexey Bataeved09d242014-05-28 05:53:51 +00005102 return new (Context)
5103 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005104}
5105
Alexey Bataev62c87d22014-03-21 04:51:18 +00005106ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5107 OpenMPClauseKind CKind) {
5108 if (!E)
5109 return ExprError();
5110 if (E->isValueDependent() || E->isTypeDependent() ||
5111 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005112 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005113 llvm::APSInt Result;
5114 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5115 if (ICE.isInvalid())
5116 return ExprError();
5117 if (!Result.isStrictlyPositive()) {
5118 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
5119 << getOpenMPClauseName(CKind) << E->getSourceRange();
5120 return ExprError();
5121 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005122 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5123 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5124 << E->getSourceRange();
5125 return ExprError();
5126 }
Alexey Bataev9c821032015-04-30 04:23:23 +00005127 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005128 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5129 Result.getExtValue());
5130 } else if (CKind == OMPC_ordered) {
5131 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5132 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00005133 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00005134 return ICE;
5135}
5136
5137OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5138 SourceLocation LParenLoc,
5139 SourceLocation EndLoc) {
5140 // OpenMP [2.8.1, simd construct, Description]
5141 // The parameter of the safelen clause must be a constant
5142 // positive integer expression.
5143 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5144 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005145 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005146 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005147 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005148}
5149
Alexey Bataev66b15b52015-08-21 11:14:16 +00005150OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5151 SourceLocation LParenLoc,
5152 SourceLocation EndLoc) {
5153 // OpenMP [2.8.1, simd construct, Description]
5154 // The parameter of the simdlen clause must be a constant
5155 // positive integer expression.
5156 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5157 if (Simdlen.isInvalid())
5158 return nullptr;
5159 return new (Context)
5160 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5161}
5162
Alexander Musman64d33f12014-06-04 07:53:32 +00005163OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5164 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005165 SourceLocation LParenLoc,
5166 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005167 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005168 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005169 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005170 // The parameter of the collapse clause must be a constant
5171 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005172 ExprResult NumForLoopsResult =
5173 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5174 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005175 return nullptr;
5176 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005177 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005178}
5179
Alexey Bataev10e775f2015-07-30 11:36:16 +00005180OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5181 SourceLocation EndLoc,
5182 SourceLocation LParenLoc,
5183 Expr *NumForLoops) {
5184 DSAStack->setOrderedRegion();
5185 // OpenMP [2.7.1, loop construct, Description]
5186 // OpenMP [2.8.1, simd construct, Description]
5187 // OpenMP [2.9.6, distribute construct, Description]
5188 // The parameter of the ordered clause must be a constant
5189 // positive integer expression if any.
5190 if (NumForLoops && LParenLoc.isValid()) {
5191 ExprResult NumForLoopsResult =
5192 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5193 if (NumForLoopsResult.isInvalid())
5194 return nullptr;
5195 NumForLoops = NumForLoopsResult.get();
5196 }
5197 return new (Context)
5198 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5199}
5200
Alexey Bataeved09d242014-05-28 05:53:51 +00005201OMPClause *Sema::ActOnOpenMPSimpleClause(
5202 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5203 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005204 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005205 switch (Kind) {
5206 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005207 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005208 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5209 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005210 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005211 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005212 Res = ActOnOpenMPProcBindClause(
5213 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5214 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005215 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005216 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005217 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005218 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005219 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005220 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005221 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005222 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005223 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005224 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005225 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005226 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005227 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005228 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005229 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005230 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005231 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005232 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005233 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005234 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005235 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005236 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005237 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005238 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005239 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005240 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005241 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005242 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005243 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005244 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005245 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005246 llvm_unreachable("Clause is not allowed.");
5247 }
5248 return Res;
5249}
5250
5251OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5252 SourceLocation KindKwLoc,
5253 SourceLocation StartLoc,
5254 SourceLocation LParenLoc,
5255 SourceLocation EndLoc) {
5256 if (Kind == OMPC_DEFAULT_unknown) {
5257 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005258 static_assert(OMPC_DEFAULT_unknown > 0,
5259 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005260 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005261 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005262 Values += "'";
5263 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5264 Values += "'";
5265 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005266 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005267 Values += " or ";
5268 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005269 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005270 break;
5271 default:
5272 Values += Sep;
5273 break;
5274 }
5275 }
5276 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005277 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005278 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005279 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005280 switch (Kind) {
5281 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005282 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005283 break;
5284 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005285 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005286 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005287 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005288 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005289 break;
5290 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005291 return new (Context)
5292 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005293}
5294
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005295OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5296 SourceLocation KindKwLoc,
5297 SourceLocation StartLoc,
5298 SourceLocation LParenLoc,
5299 SourceLocation EndLoc) {
5300 if (Kind == OMPC_PROC_BIND_unknown) {
5301 std::string Values;
5302 std::string Sep(", ");
5303 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5304 Values += "'";
5305 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5306 Values += "'";
5307 switch (i) {
5308 case OMPC_PROC_BIND_unknown - 2:
5309 Values += " or ";
5310 break;
5311 case OMPC_PROC_BIND_unknown - 1:
5312 break;
5313 default:
5314 Values += Sep;
5315 break;
5316 }
5317 }
5318 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005319 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005320 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005321 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005322 return new (Context)
5323 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005324}
5325
Alexey Bataev56dafe82014-06-20 07:16:17 +00005326OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5327 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5328 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005329 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005330 SourceLocation EndLoc) {
5331 OMPClause *Res = nullptr;
5332 switch (Kind) {
5333 case OMPC_schedule:
5334 Res = ActOnOpenMPScheduleClause(
5335 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005336 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005337 break;
5338 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005339 Res =
5340 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5341 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5342 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005343 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005344 case OMPC_num_threads:
5345 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005346 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005347 case OMPC_collapse:
5348 case OMPC_default:
5349 case OMPC_proc_bind:
5350 case OMPC_private:
5351 case OMPC_firstprivate:
5352 case OMPC_lastprivate:
5353 case OMPC_shared:
5354 case OMPC_reduction:
5355 case OMPC_linear:
5356 case OMPC_aligned:
5357 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005358 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005359 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005360 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005361 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005362 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005363 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005364 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005365 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005366 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005367 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005368 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005369 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005370 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005371 case OMPC_device:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005372 case OMPC_unknown:
5373 llvm_unreachable("Clause is not allowed.");
5374 }
5375 return Res;
5376}
5377
5378OMPClause *Sema::ActOnOpenMPScheduleClause(
5379 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5380 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5381 SourceLocation EndLoc) {
5382 if (Kind == OMPC_SCHEDULE_unknown) {
5383 std::string Values;
5384 std::string Sep(", ");
5385 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5386 Values += "'";
5387 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5388 Values += "'";
5389 switch (i) {
5390 case OMPC_SCHEDULE_unknown - 2:
5391 Values += " or ";
5392 break;
5393 case OMPC_SCHEDULE_unknown - 1:
5394 break;
5395 default:
5396 Values += Sep;
5397 break;
5398 }
5399 }
5400 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5401 << Values << getOpenMPClauseName(OMPC_schedule);
5402 return nullptr;
5403 }
5404 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005405 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005406 if (ChunkSize) {
5407 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5408 !ChunkSize->isInstantiationDependent() &&
5409 !ChunkSize->containsUnexpandedParameterPack()) {
5410 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5411 ExprResult Val =
5412 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5413 if (Val.isInvalid())
5414 return nullptr;
5415
5416 ValExpr = Val.get();
5417
5418 // OpenMP [2.7.1, Restrictions]
5419 // chunk_size must be a loop invariant integer expression with a positive
5420 // value.
5421 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005422 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5423 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5424 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
5425 << "schedule" << ChunkSize->getSourceRange();
5426 return nullptr;
5427 }
5428 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5429 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5430 ChunkSize->getType(), ".chunk.");
5431 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5432 ChunkSize->getExprLoc(),
5433 /*RefersToCapture=*/true);
5434 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005435 }
5436 }
5437 }
5438
5439 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005440 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005441}
5442
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005443OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5444 SourceLocation StartLoc,
5445 SourceLocation EndLoc) {
5446 OMPClause *Res = nullptr;
5447 switch (Kind) {
5448 case OMPC_ordered:
5449 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5450 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005451 case OMPC_nowait:
5452 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5453 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005454 case OMPC_untied:
5455 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5456 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005457 case OMPC_mergeable:
5458 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5459 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005460 case OMPC_read:
5461 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5462 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005463 case OMPC_write:
5464 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5465 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005466 case OMPC_update:
5467 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5468 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005469 case OMPC_capture:
5470 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5471 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005472 case OMPC_seq_cst:
5473 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5474 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005475 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005476 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005477 case OMPC_num_threads:
5478 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005479 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005480 case OMPC_collapse:
5481 case OMPC_schedule:
5482 case OMPC_private:
5483 case OMPC_firstprivate:
5484 case OMPC_lastprivate:
5485 case OMPC_shared:
5486 case OMPC_reduction:
5487 case OMPC_linear:
5488 case OMPC_aligned:
5489 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005490 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005491 case OMPC_default:
5492 case OMPC_proc_bind:
5493 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005494 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005495 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005496 case OMPC_device:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005497 case OMPC_unknown:
5498 llvm_unreachable("Clause is not allowed.");
5499 }
5500 return Res;
5501}
5502
Alexey Bataev236070f2014-06-20 11:19:47 +00005503OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5504 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005505 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005506 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5507}
5508
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005509OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5510 SourceLocation EndLoc) {
5511 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5512}
5513
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005514OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5515 SourceLocation EndLoc) {
5516 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5517}
5518
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005519OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5520 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005521 return new (Context) OMPReadClause(StartLoc, EndLoc);
5522}
5523
Alexey Bataevdea47612014-07-23 07:46:59 +00005524OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5525 SourceLocation EndLoc) {
5526 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5527}
5528
Alexey Bataev67a4f222014-07-23 10:25:33 +00005529OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5530 SourceLocation EndLoc) {
5531 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5532}
5533
Alexey Bataev459dec02014-07-24 06:46:57 +00005534OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5535 SourceLocation EndLoc) {
5536 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5537}
5538
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005539OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5540 SourceLocation EndLoc) {
5541 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5542}
5543
Alexey Bataevc5e02582014-06-16 07:08:35 +00005544OMPClause *Sema::ActOnOpenMPVarListClause(
5545 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5546 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5547 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005548 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Alexey Bataev182227b2015-08-20 10:54:39 +00005549 OpenMPLinearClauseKind LinKind, SourceLocation DepLinLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005550 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005551 switch (Kind) {
5552 case OMPC_private:
5553 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5554 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005555 case OMPC_firstprivate:
5556 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5557 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005558 case OMPC_lastprivate:
5559 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5560 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005561 case OMPC_shared:
5562 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5563 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005564 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005565 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5566 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005567 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005568 case OMPC_linear:
5569 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Alexey Bataev182227b2015-08-20 10:54:39 +00005570 LinKind, DepLinLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005571 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005572 case OMPC_aligned:
5573 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5574 ColonLoc, EndLoc);
5575 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005576 case OMPC_copyin:
5577 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5578 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005579 case OMPC_copyprivate:
5580 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5581 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005582 case OMPC_flush:
5583 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5584 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005585 case OMPC_depend:
Alexey Bataev182227b2015-08-20 10:54:39 +00005586 Res = ActOnOpenMPDependClause(DepKind, DepLinLoc, ColonLoc, VarList, StartLoc,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005587 LParenLoc, EndLoc);
5588 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005589 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005590 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005591 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005592 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005593 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005594 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005595 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005596 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005597 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005598 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005599 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005600 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005601 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005602 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005603 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005604 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005605 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005606 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005607 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005608 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005609 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005610 llvm_unreachable("Clause is not allowed.");
5611 }
5612 return Res;
5613}
5614
5615OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5616 SourceLocation StartLoc,
5617 SourceLocation LParenLoc,
5618 SourceLocation EndLoc) {
5619 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005620 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005621 for (auto &RefExpr : VarList) {
5622 assert(RefExpr && "NULL expr in OpenMP private clause.");
5623 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005624 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005625 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005626 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005627 continue;
5628 }
5629
Alexey Bataeved09d242014-05-28 05:53:51 +00005630 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005631 // OpenMP [2.1, C/C++]
5632 // A list item is a variable name.
5633 // OpenMP [2.9.3.3, Restrictions, p.1]
5634 // A variable that is part of another variable (as an array or
5635 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005636 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005637 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005638 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005639 continue;
5640 }
5641 Decl *D = DE->getDecl();
5642 VarDecl *VD = cast<VarDecl>(D);
5643
5644 QualType Type = VD->getType();
5645 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5646 // It will be analyzed later.
5647 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005648 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005649 continue;
5650 }
5651
5652 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5653 // A variable that appears in a private clause must not have an incomplete
5654 // type or a reference type.
5655 if (RequireCompleteType(ELoc, Type,
5656 diag::err_omp_private_incomplete_type)) {
5657 continue;
5658 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005659 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005660
Alexey Bataev758e55e2013-09-06 18:03:48 +00005661 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5662 // in a Construct]
5663 // Variables with the predetermined data-sharing attributes may not be
5664 // listed in data-sharing attributes clauses, except for the cases
5665 // listed below. For these exceptions only, listing a predetermined
5666 // variable in a data-sharing attribute clause is allowed and overrides
5667 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005668 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005669 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005670 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5671 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005672 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005673 continue;
5674 }
5675
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005676 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005677 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005678 DSAStack->getCurrentDirective() == OMPD_task) {
5679 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5680 << getOpenMPClauseName(OMPC_private) << Type
5681 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5682 bool IsDecl =
5683 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5684 Diag(VD->getLocation(),
5685 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5686 << VD;
5687 continue;
5688 }
5689
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005690 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5691 // A variable of class type (or array thereof) that appears in a private
5692 // clause requires an accessible, unambiguous default constructor for the
5693 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005694 // Generate helper private variable and initialize it with the default
5695 // value. The address of the original variable is replaced by the address of
5696 // the new private variable in CodeGen. This new variable is not added to
5697 // IdResolver, so the code in the OpenMP region uses original variable for
5698 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005699 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00005700 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
5701 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005702 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005703 if (VDPrivate->isInvalidDecl())
5704 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005705 auto VDPrivateRefExpr = buildDeclRefExpr(
5706 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005707
Alexey Bataev758e55e2013-09-06 18:03:48 +00005708 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005709 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005710 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005711 }
5712
Alexey Bataeved09d242014-05-28 05:53:51 +00005713 if (Vars.empty())
5714 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005715
Alexey Bataev03b340a2014-10-21 03:16:40 +00005716 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5717 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005718}
5719
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005720namespace {
5721class DiagsUninitializedSeveretyRAII {
5722private:
5723 DiagnosticsEngine &Diags;
5724 SourceLocation SavedLoc;
5725 bool IsIgnored;
5726
5727public:
5728 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5729 bool IsIgnored)
5730 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5731 if (!IsIgnored) {
5732 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5733 /*Map*/ diag::Severity::Ignored, Loc);
5734 }
5735 }
5736 ~DiagsUninitializedSeveretyRAII() {
5737 if (!IsIgnored)
5738 Diags.popMappings(SavedLoc);
5739 }
5740};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005741}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005742
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005743OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5744 SourceLocation StartLoc,
5745 SourceLocation LParenLoc,
5746 SourceLocation EndLoc) {
5747 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005748 SmallVector<Expr *, 8> PrivateCopies;
5749 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005750 bool IsImplicitClause =
5751 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5752 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5753
Alexey Bataeved09d242014-05-28 05:53:51 +00005754 for (auto &RefExpr : VarList) {
5755 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5756 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005757 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005758 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005759 PrivateCopies.push_back(nullptr);
5760 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005761 continue;
5762 }
5763
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005764 SourceLocation ELoc =
5765 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005766 // OpenMP [2.1, C/C++]
5767 // A list item is a variable name.
5768 // OpenMP [2.9.3.3, Restrictions, p.1]
5769 // A variable that is part of another variable (as an array or
5770 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005771 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005772 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005773 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005774 continue;
5775 }
5776 Decl *D = DE->getDecl();
5777 VarDecl *VD = cast<VarDecl>(D);
5778
5779 QualType Type = VD->getType();
5780 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5781 // It will be analyzed later.
5782 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005783 PrivateCopies.push_back(nullptr);
5784 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005785 continue;
5786 }
5787
5788 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5789 // A variable that appears in a private clause must not have an incomplete
5790 // type or a reference type.
5791 if (RequireCompleteType(ELoc, Type,
5792 diag::err_omp_firstprivate_incomplete_type)) {
5793 continue;
5794 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005795 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005796
5797 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5798 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005799 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005800 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005801 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005802
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005803 // If an implicit firstprivate variable found it was checked already.
5804 if (!IsImplicitClause) {
5805 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005806 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005807 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5808 // A list item that specifies a given variable may not appear in more
5809 // than one clause on the same directive, except that a variable may be
5810 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005811 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005812 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005813 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005814 << getOpenMPClauseName(DVar.CKind)
5815 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005816 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005817 continue;
5818 }
5819
5820 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5821 // in a Construct]
5822 // Variables with the predetermined data-sharing attributes may not be
5823 // listed in data-sharing attributes clauses, except for the cases
5824 // listed below. For these exceptions only, listing a predetermined
5825 // variable in a data-sharing attribute clause is allowed and overrides
5826 // the variable's predetermined data-sharing attributes.
5827 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5828 // in a Construct, C/C++, p.2]
5829 // Variables with const-qualified type having no mutable member may be
5830 // listed in a firstprivate clause, even if they are static data members.
5831 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5832 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5833 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005834 << getOpenMPClauseName(DVar.CKind)
5835 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005836 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005837 continue;
5838 }
5839
Alexey Bataevf29276e2014-06-18 04:14:57 +00005840 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005841 // OpenMP [2.9.3.4, Restrictions, p.2]
5842 // A list item that is private within a parallel region must not appear
5843 // in a firstprivate clause on a worksharing construct if any of the
5844 // worksharing regions arising from the worksharing construct ever bind
5845 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005846 if (isOpenMPWorksharingDirective(CurrDir) &&
5847 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005848 DVar = DSAStack->getImplicitDSA(VD, true);
5849 if (DVar.CKind != OMPC_shared &&
5850 (isOpenMPParallelDirective(DVar.DKind) ||
5851 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005852 Diag(ELoc, diag::err_omp_required_access)
5853 << getOpenMPClauseName(OMPC_firstprivate)
5854 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005855 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005856 continue;
5857 }
5858 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005859 // OpenMP [2.9.3.4, Restrictions, p.3]
5860 // A list item that appears in a reduction clause of a parallel construct
5861 // must not appear in a firstprivate clause on a worksharing or task
5862 // construct if any of the worksharing or task regions arising from the
5863 // worksharing or task construct ever bind to any of the parallel regions
5864 // arising from the parallel construct.
5865 // OpenMP [2.9.3.4, Restrictions, p.4]
5866 // A list item that appears in a reduction clause in worksharing
5867 // construct must not appear in a firstprivate clause in a task construct
5868 // encountered during execution of any of the worksharing regions arising
5869 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005870 if (CurrDir == OMPD_task) {
5871 DVar =
5872 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5873 [](OpenMPDirectiveKind K) -> bool {
5874 return isOpenMPParallelDirective(K) ||
5875 isOpenMPWorksharingDirective(K);
5876 },
5877 false);
5878 if (DVar.CKind == OMPC_reduction &&
5879 (isOpenMPParallelDirective(DVar.DKind) ||
5880 isOpenMPWorksharingDirective(DVar.DKind))) {
5881 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5882 << getOpenMPDirectiveName(DVar.DKind);
5883 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5884 continue;
5885 }
5886 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005887 }
5888
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005889 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005890 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005891 DSAStack->getCurrentDirective() == OMPD_task) {
5892 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5893 << getOpenMPClauseName(OMPC_firstprivate) << Type
5894 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5895 bool IsDecl =
5896 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5897 Diag(VD->getLocation(),
5898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5899 << VD;
5900 continue;
5901 }
5902
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005903 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00005904 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
5905 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005906 // Generate helper private variable and initialize it with the value of the
5907 // original variable. The address of the original variable is replaced by
5908 // the address of the new private variable in the CodeGen. This new variable
5909 // is not added to IdResolver, so the code in the OpenMP region uses
5910 // original variable for proper diagnostics and variable capturing.
5911 Expr *VDInitRefExpr = nullptr;
5912 // For arrays generate initializer for single element and replace it by the
5913 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005914 if (Type->isArrayType()) {
5915 auto VDInit =
5916 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5917 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005918 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005919 ElemType = ElemType.getUnqualifiedType();
5920 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5921 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005922 InitializedEntity Entity =
5923 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005924 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5925
5926 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5927 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5928 if (Result.isInvalid())
5929 VDPrivate->setInvalidDecl();
5930 else
5931 VDPrivate->setInit(Result.getAs<Expr>());
5932 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005933 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005934 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005935 VDInitRefExpr =
5936 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005937 AddInitializerToDecl(VDPrivate,
5938 DefaultLvalueConversion(VDInitRefExpr).get(),
5939 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005940 }
5941 if (VDPrivate->isInvalidDecl()) {
5942 if (IsImplicitClause) {
5943 Diag(DE->getExprLoc(),
5944 diag::note_omp_task_predetermined_firstprivate_here);
5945 }
5946 continue;
5947 }
5948 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005949 auto VDPrivateRefExpr = buildDeclRefExpr(
5950 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005951 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5952 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005953 PrivateCopies.push_back(VDPrivateRefExpr);
5954 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005955 }
5956
Alexey Bataeved09d242014-05-28 05:53:51 +00005957 if (Vars.empty())
5958 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005959
5960 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005961 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005962}
5963
Alexander Musman1bb328c2014-06-04 13:06:39 +00005964OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5965 SourceLocation StartLoc,
5966 SourceLocation LParenLoc,
5967 SourceLocation EndLoc) {
5968 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005969 SmallVector<Expr *, 8> SrcExprs;
5970 SmallVector<Expr *, 8> DstExprs;
5971 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005972 for (auto &RefExpr : VarList) {
5973 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5974 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5975 // It will be analyzed later.
5976 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005977 SrcExprs.push_back(nullptr);
5978 DstExprs.push_back(nullptr);
5979 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005980 continue;
5981 }
5982
5983 SourceLocation ELoc = RefExpr->getExprLoc();
5984 // OpenMP [2.1, C/C++]
5985 // A list item is a variable name.
5986 // OpenMP [2.14.3.5, Restrictions, p.1]
5987 // A variable that is part of another variable (as an array or structure
5988 // element) cannot appear in a lastprivate clause.
5989 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5990 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5991 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5992 continue;
5993 }
5994 Decl *D = DE->getDecl();
5995 VarDecl *VD = cast<VarDecl>(D);
5996
5997 QualType Type = VD->getType();
5998 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5999 // It will be analyzed later.
6000 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006001 SrcExprs.push_back(nullptr);
6002 DstExprs.push_back(nullptr);
6003 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006004 continue;
6005 }
6006
6007 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6008 // A variable that appears in a lastprivate clause must not have an
6009 // incomplete type or a reference type.
6010 if (RequireCompleteType(ELoc, Type,
6011 diag::err_omp_lastprivate_incomplete_type)) {
6012 continue;
6013 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006014 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006015
6016 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6017 // in a Construct]
6018 // Variables with the predetermined data-sharing attributes may not be
6019 // listed in data-sharing attributes clauses, except for the cases
6020 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006021 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006022 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6023 DVar.CKind != OMPC_firstprivate &&
6024 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6025 Diag(ELoc, diag::err_omp_wrong_dsa)
6026 << getOpenMPClauseName(DVar.CKind)
6027 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006028 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006029 continue;
6030 }
6031
Alexey Bataevf29276e2014-06-18 04:14:57 +00006032 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6033 // OpenMP [2.14.3.5, Restrictions, p.2]
6034 // A list item that is private within a parallel region, or that appears in
6035 // the reduction clause of a parallel construct, must not appear in a
6036 // lastprivate clause on a worksharing construct if any of the corresponding
6037 // worksharing regions ever binds to any of the corresponding parallel
6038 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006039 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006040 if (isOpenMPWorksharingDirective(CurrDir) &&
6041 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006042 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006043 if (DVar.CKind != OMPC_shared) {
6044 Diag(ELoc, diag::err_omp_required_access)
6045 << getOpenMPClauseName(OMPC_lastprivate)
6046 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006047 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006048 continue;
6049 }
6050 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006051 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006052 // A variable of class type (or array thereof) that appears in a
6053 // lastprivate clause requires an accessible, unambiguous default
6054 // constructor for the class type, unless the list item is also specified
6055 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006056 // A variable of class type (or array thereof) that appears in a
6057 // lastprivate clause requires an accessible, unambiguous copy assignment
6058 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006059 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006060 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006061 Type.getUnqualifiedType(), ".lastprivate.src",
6062 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006063 auto *PseudoSrcExpr = buildDeclRefExpr(
6064 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006065 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006066 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6067 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006068 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006069 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006070 // For arrays generate assignment operation for single element and replace
6071 // it by the original array element in CodeGen.
6072 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6073 PseudoDstExpr, PseudoSrcExpr);
6074 if (AssignmentOp.isInvalid())
6075 continue;
6076 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6077 /*DiscardedValue=*/true);
6078 if (AssignmentOp.isInvalid())
6079 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006080
Alexey Bataev39f915b82015-05-08 10:41:21 +00006081 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006082 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006083 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006084 SrcExprs.push_back(PseudoSrcExpr);
6085 DstExprs.push_back(PseudoDstExpr);
6086 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006087 }
6088
6089 if (Vars.empty())
6090 return nullptr;
6091
6092 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006093 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006094}
6095
Alexey Bataev758e55e2013-09-06 18:03:48 +00006096OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6097 SourceLocation StartLoc,
6098 SourceLocation LParenLoc,
6099 SourceLocation EndLoc) {
6100 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006101 for (auto &RefExpr : VarList) {
6102 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6103 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006104 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006105 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006106 continue;
6107 }
6108
Alexey Bataeved09d242014-05-28 05:53:51 +00006109 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006110 // OpenMP [2.1, C/C++]
6111 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006112 // OpenMP [2.14.3.2, Restrictions, p.1]
6113 // A variable that is part of another variable (as an array or structure
6114 // element) cannot appear in a shared unless it is a static data member
6115 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006116 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006117 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006118 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006119 continue;
6120 }
6121 Decl *D = DE->getDecl();
6122 VarDecl *VD = cast<VarDecl>(D);
6123
6124 QualType Type = VD->getType();
6125 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6126 // It will be analyzed later.
6127 Vars.push_back(DE);
6128 continue;
6129 }
6130
6131 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6132 // in a Construct]
6133 // Variables with the predetermined data-sharing attributes may not be
6134 // listed in data-sharing attributes clauses, except for the cases
6135 // listed below. For these exceptions only, listing a predetermined
6136 // variable in a data-sharing attribute clause is allowed and overrides
6137 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006138 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006139 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6140 DVar.RefExpr) {
6141 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6142 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006143 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006144 continue;
6145 }
6146
6147 DSAStack->addDSA(VD, DE, OMPC_shared);
6148 Vars.push_back(DE);
6149 }
6150
Alexey Bataeved09d242014-05-28 05:53:51 +00006151 if (Vars.empty())
6152 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006153
6154 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6155}
6156
Alexey Bataevc5e02582014-06-16 07:08:35 +00006157namespace {
6158class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6159 DSAStackTy *Stack;
6160
6161public:
6162 bool VisitDeclRefExpr(DeclRefExpr *E) {
6163 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006164 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006165 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6166 return false;
6167 if (DVar.CKind != OMPC_unknown)
6168 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006169 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006170 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006171 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006172 return true;
6173 return false;
6174 }
6175 return false;
6176 }
6177 bool VisitStmt(Stmt *S) {
6178 for (auto Child : S->children()) {
6179 if (Child && Visit(Child))
6180 return true;
6181 }
6182 return false;
6183 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006184 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006185};
Alexey Bataev23b69422014-06-18 07:08:49 +00006186} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006187
6188OMPClause *Sema::ActOnOpenMPReductionClause(
6189 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6190 SourceLocation ColonLoc, SourceLocation EndLoc,
6191 CXXScopeSpec &ReductionIdScopeSpec,
6192 const DeclarationNameInfo &ReductionId) {
6193 // TODO: Allow scope specification search when 'declare reduction' is
6194 // supported.
6195 assert(ReductionIdScopeSpec.isEmpty() &&
6196 "No support for scoped reduction identifiers yet.");
6197
6198 auto DN = ReductionId.getName();
6199 auto OOK = DN.getCXXOverloadedOperator();
6200 BinaryOperatorKind BOK = BO_Comma;
6201
6202 // OpenMP [2.14.3.6, reduction clause]
6203 // C
6204 // reduction-identifier is either an identifier or one of the following
6205 // operators: +, -, *, &, |, ^, && and ||
6206 // C++
6207 // reduction-identifier is either an id-expression or one of the following
6208 // operators: +, -, *, &, |, ^, && and ||
6209 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6210 switch (OOK) {
6211 case OO_Plus:
6212 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006213 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006214 break;
6215 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006216 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006217 break;
6218 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006219 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006220 break;
6221 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006222 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006223 break;
6224 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006225 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006226 break;
6227 case OO_AmpAmp:
6228 BOK = BO_LAnd;
6229 break;
6230 case OO_PipePipe:
6231 BOK = BO_LOr;
6232 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006233 case OO_New:
6234 case OO_Delete:
6235 case OO_Array_New:
6236 case OO_Array_Delete:
6237 case OO_Slash:
6238 case OO_Percent:
6239 case OO_Tilde:
6240 case OO_Exclaim:
6241 case OO_Equal:
6242 case OO_Less:
6243 case OO_Greater:
6244 case OO_LessEqual:
6245 case OO_GreaterEqual:
6246 case OO_PlusEqual:
6247 case OO_MinusEqual:
6248 case OO_StarEqual:
6249 case OO_SlashEqual:
6250 case OO_PercentEqual:
6251 case OO_CaretEqual:
6252 case OO_AmpEqual:
6253 case OO_PipeEqual:
6254 case OO_LessLess:
6255 case OO_GreaterGreater:
6256 case OO_LessLessEqual:
6257 case OO_GreaterGreaterEqual:
6258 case OO_EqualEqual:
6259 case OO_ExclaimEqual:
6260 case OO_PlusPlus:
6261 case OO_MinusMinus:
6262 case OO_Comma:
6263 case OO_ArrowStar:
6264 case OO_Arrow:
6265 case OO_Call:
6266 case OO_Subscript:
6267 case OO_Conditional:
6268 case NUM_OVERLOADED_OPERATORS:
6269 llvm_unreachable("Unexpected reduction identifier");
6270 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006271 if (auto II = DN.getAsIdentifierInfo()) {
6272 if (II->isStr("max"))
6273 BOK = BO_GT;
6274 else if (II->isStr("min"))
6275 BOK = BO_LT;
6276 }
6277 break;
6278 }
6279 SourceRange ReductionIdRange;
6280 if (ReductionIdScopeSpec.isValid()) {
6281 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6282 }
6283 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6284 if (BOK == BO_Comma) {
6285 // Not allowed reduction identifier is found.
6286 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6287 << ReductionIdRange;
6288 return nullptr;
6289 }
6290
6291 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006292 SmallVector<Expr *, 8> LHSs;
6293 SmallVector<Expr *, 8> RHSs;
6294 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006295 for (auto RefExpr : VarList) {
6296 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6297 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6298 // It will be analyzed later.
6299 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006300 LHSs.push_back(nullptr);
6301 RHSs.push_back(nullptr);
6302 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006303 continue;
6304 }
6305
6306 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6307 RefExpr->isInstantiationDependent() ||
6308 RefExpr->containsUnexpandedParameterPack()) {
6309 // It will be analyzed later.
6310 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006311 LHSs.push_back(nullptr);
6312 RHSs.push_back(nullptr);
6313 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006314 continue;
6315 }
6316
6317 auto ELoc = RefExpr->getExprLoc();
6318 auto ERange = RefExpr->getSourceRange();
6319 // OpenMP [2.1, C/C++]
6320 // A list item is a variable or array section, subject to the restrictions
6321 // specified in Section 2.4 on page 42 and in each of the sections
6322 // describing clauses and directives for which a list appears.
6323 // OpenMP [2.14.3.3, Restrictions, p.1]
6324 // A variable that is part of another variable (as an array or
6325 // structure element) cannot appear in a private clause.
6326 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
6327 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6328 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
6329 continue;
6330 }
6331 auto D = DE->getDecl();
6332 auto VD = cast<VarDecl>(D);
6333 auto Type = VD->getType();
6334 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6335 // A variable that appears in a private clause must not have an incomplete
6336 // type or a reference type.
6337 if (RequireCompleteType(ELoc, Type,
6338 diag::err_omp_reduction_incomplete_type))
6339 continue;
6340 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6341 // Arrays may not appear in a reduction clause.
6342 if (Type.getNonReferenceType()->isArrayType()) {
6343 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
6344 bool IsDecl =
6345 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6346 Diag(VD->getLocation(),
6347 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6348 << VD;
6349 continue;
6350 }
6351 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6352 // A list item that appears in a reduction clause must not be
6353 // const-qualified.
6354 if (Type.getNonReferenceType().isConstant(Context)) {
6355 Diag(ELoc, diag::err_omp_const_variable)
6356 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
6357 bool IsDecl =
6358 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6359 Diag(VD->getLocation(),
6360 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6361 << VD;
6362 continue;
6363 }
6364 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6365 // If a list-item is a reference type then it must bind to the same object
6366 // for all threads of the team.
6367 VarDecl *VDDef = VD->getDefinition();
6368 if (Type->isReferenceType() && VDDef) {
6369 DSARefChecker Check(DSAStack);
6370 if (Check.Visit(VDDef->getInit())) {
6371 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6372 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6373 continue;
6374 }
6375 }
6376 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6377 // The type of a list item that appears in a reduction clause must be valid
6378 // for the reduction-identifier. For a max or min reduction in C, the type
6379 // of the list item must be an allowed arithmetic data type: char, int,
6380 // float, double, or _Bool, possibly modified with long, short, signed, or
6381 // unsigned. For a max or min reduction in C++, the type of the list item
6382 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6383 // double, or bool, possibly modified with long, short, signed, or unsigned.
6384 if ((BOK == BO_GT || BOK == BO_LT) &&
6385 !(Type->isScalarType() ||
6386 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6387 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6388 << getLangOpts().CPlusPlus;
6389 bool IsDecl =
6390 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6391 Diag(VD->getLocation(),
6392 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6393 << VD;
6394 continue;
6395 }
6396 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6397 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6398 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
6399 bool IsDecl =
6400 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6401 Diag(VD->getLocation(),
6402 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6403 << VD;
6404 continue;
6405 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006406 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6407 // in a Construct]
6408 // Variables with the predetermined data-sharing attributes may not be
6409 // listed in data-sharing attributes clauses, except for the cases
6410 // listed below. For these exceptions only, listing a predetermined
6411 // variable in a data-sharing attribute clause is allowed and overrides
6412 // the variable's predetermined data-sharing attributes.
6413 // OpenMP [2.14.3.6, Restrictions, p.3]
6414 // Any number of reduction clauses can be specified on the directive,
6415 // but a list item can appear only once in the reduction clauses for that
6416 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006417 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006418 if (DVar.CKind == OMPC_reduction) {
6419 Diag(ELoc, diag::err_omp_once_referenced)
6420 << getOpenMPClauseName(OMPC_reduction);
6421 if (DVar.RefExpr) {
6422 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
6423 }
6424 } else if (DVar.CKind != OMPC_unknown) {
6425 Diag(ELoc, diag::err_omp_wrong_dsa)
6426 << getOpenMPClauseName(DVar.CKind)
6427 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006428 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006429 continue;
6430 }
6431
6432 // OpenMP [2.14.3.6, Restrictions, p.1]
6433 // A list item that appears in a reduction clause of a worksharing
6434 // construct must be shared in the parallel regions to which any of the
6435 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00006436 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00006437 if (isOpenMPWorksharingDirective(CurrDir) &&
6438 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006439 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006440 if (DVar.CKind != OMPC_shared) {
6441 Diag(ELoc, diag::err_omp_required_access)
6442 << getOpenMPClauseName(OMPC_reduction)
6443 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006444 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006445 continue;
6446 }
6447 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006448 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006449 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
6450 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
6451 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
6452 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006453 // Add initializer for private variable.
6454 Expr *Init = nullptr;
6455 switch (BOK) {
6456 case BO_Add:
6457 case BO_Xor:
6458 case BO_Or:
6459 case BO_LOr:
6460 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6461 if (Type->isScalarType() || Type->isAnyComplexType()) {
6462 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006463 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006464 break;
6465 case BO_Mul:
6466 case BO_LAnd:
6467 if (Type->isScalarType() || Type->isAnyComplexType()) {
6468 // '*' and '&&' reduction ops - initializer is '1'.
6469 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6470 }
6471 break;
6472 case BO_And: {
6473 // '&' reduction op - initializer is '~0'.
6474 QualType OrigType = Type;
6475 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6476 Type = ComplexTy->getElementType();
6477 }
6478 if (Type->isRealFloatingType()) {
6479 llvm::APFloat InitValue =
6480 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6481 /*isIEEE=*/true);
6482 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6483 Type, ELoc);
6484 } else if (Type->isScalarType()) {
6485 auto Size = Context.getTypeSize(Type);
6486 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6487 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6488 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6489 }
6490 if (Init && OrigType->isAnyComplexType()) {
6491 // Init = 0xFFFF + 0xFFFFi;
6492 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6493 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6494 }
6495 Type = OrigType;
6496 break;
6497 }
6498 case BO_LT:
6499 case BO_GT: {
6500 // 'min' reduction op - initializer is 'Largest representable number in
6501 // the reduction list item type'.
6502 // 'max' reduction op - initializer is 'Least representable number in
6503 // the reduction list item type'.
6504 if (Type->isIntegerType() || Type->isPointerType()) {
6505 bool IsSigned = Type->hasSignedIntegerRepresentation();
6506 auto Size = Context.getTypeSize(Type);
6507 QualType IntTy =
6508 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6509 llvm::APInt InitValue =
6510 (BOK != BO_LT)
6511 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6512 : llvm::APInt::getMinValue(Size)
6513 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6514 : llvm::APInt::getMaxValue(Size);
6515 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6516 if (Type->isPointerType()) {
6517 // Cast to pointer type.
6518 auto CastExpr = BuildCStyleCastExpr(
6519 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6520 SourceLocation(), Init);
6521 if (CastExpr.isInvalid())
6522 continue;
6523 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006524 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006525 } else if (Type->isRealFloatingType()) {
6526 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6527 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6528 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6529 Type, ELoc);
6530 }
6531 break;
6532 }
6533 case BO_PtrMemD:
6534 case BO_PtrMemI:
6535 case BO_MulAssign:
6536 case BO_Div:
6537 case BO_Rem:
6538 case BO_Sub:
6539 case BO_Shl:
6540 case BO_Shr:
6541 case BO_LE:
6542 case BO_GE:
6543 case BO_EQ:
6544 case BO_NE:
6545 case BO_AndAssign:
6546 case BO_XorAssign:
6547 case BO_OrAssign:
6548 case BO_Assign:
6549 case BO_AddAssign:
6550 case BO_SubAssign:
6551 case BO_DivAssign:
6552 case BO_RemAssign:
6553 case BO_ShlAssign:
6554 case BO_ShrAssign:
6555 case BO_Comma:
6556 llvm_unreachable("Unexpected reduction operation");
6557 }
6558 if (Init) {
6559 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6560 /*TypeMayContainAuto=*/false);
6561 } else {
6562 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6563 }
6564 if (!RHSVD->hasInit()) {
6565 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6566 << ReductionIdRange;
6567 bool IsDecl =
6568 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6569 Diag(VD->getLocation(),
6570 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6571 << VD;
6572 continue;
6573 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006574 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6575 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006576 ExprResult ReductionOp =
6577 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6578 LHSDRE, RHSDRE);
6579 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006580 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006581 ReductionOp =
6582 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6583 BO_Assign, LHSDRE, ReductionOp.get());
6584 } else {
6585 auto *ConditionalOp = new (Context) ConditionalOperator(
6586 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6587 RHSDRE, Type, VK_LValue, OK_Ordinary);
6588 ReductionOp =
6589 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6590 BO_Assign, LHSDRE, ConditionalOp);
6591 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006592 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006593 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006594 if (ReductionOp.isInvalid())
6595 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006596
6597 DSAStack->addDSA(VD, DE, OMPC_reduction);
6598 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006599 LHSs.push_back(LHSDRE);
6600 RHSs.push_back(RHSDRE);
6601 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006602 }
6603
6604 if (Vars.empty())
6605 return nullptr;
6606
6607 return OMPReductionClause::Create(
6608 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006609 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6610 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006611}
6612
Alexey Bataev182227b2015-08-20 10:54:39 +00006613OMPClause *Sema::ActOnOpenMPLinearClause(
6614 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
6615 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
6616 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006617 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006618 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00006619 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00006620 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
6621 LinKind == OMPC_LINEAR_unknown) {
6622 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
6623 LinKind = OMPC_LINEAR_val;
6624 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006625 for (auto &RefExpr : VarList) {
6626 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6627 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006628 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006629 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006630 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006631 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006632 continue;
6633 }
6634
6635 // OpenMP [2.14.3.7, linear clause]
6636 // A list item that appears in a linear clause is subject to the private
6637 // clause semantics described in Section 2.14.3.3 on page 159 except as
6638 // noted. In addition, the value of the new list item on each iteration
6639 // of the associated loop(s) corresponds to the value of the original
6640 // list item before entering the construct plus the logical number of
6641 // the iteration times linear-step.
6642
Alexey Bataeved09d242014-05-28 05:53:51 +00006643 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006644 // OpenMP [2.1, C/C++]
6645 // A list item is a variable name.
6646 // OpenMP [2.14.3.3, Restrictions, p.1]
6647 // A variable that is part of another variable (as an array or
6648 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006649 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006650 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006651 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006652 continue;
6653 }
6654
6655 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6656
6657 // OpenMP [2.14.3.7, linear clause]
6658 // A list-item cannot appear in more than one linear clause.
6659 // A list-item that appears in a linear clause cannot appear in any
6660 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006661 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006662 if (DVar.RefExpr) {
6663 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6664 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006665 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006666 continue;
6667 }
6668
6669 QualType QType = VD->getType();
6670 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6671 // It will be analyzed later.
6672 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006673 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006674 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006675 continue;
6676 }
6677
6678 // A variable must not have an incomplete type or a reference type.
6679 if (RequireCompleteType(ELoc, QType,
6680 diag::err_omp_linear_incomplete_type)) {
6681 continue;
6682 }
Alexey Bataev1185e192015-08-20 12:15:57 +00006683 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
6684 !QType->isReferenceType()) {
6685 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
6686 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
6687 continue;
6688 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006689 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00006690
6691 // A list item must not be const-qualified.
6692 if (QType.isConstant(Context)) {
6693 Diag(ELoc, diag::err_omp_const_variable)
6694 << getOpenMPClauseName(OMPC_linear);
6695 bool IsDecl =
6696 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6697 Diag(VD->getLocation(),
6698 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6699 << VD;
6700 continue;
6701 }
6702
6703 // A list item must be of integral or pointer type.
6704 QType = QType.getUnqualifiedType().getCanonicalType();
6705 const Type *Ty = QType.getTypePtrOrNull();
6706 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6707 !Ty->isPointerType())) {
6708 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6709 bool IsDecl =
6710 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6711 Diag(VD->getLocation(),
6712 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6713 << VD;
6714 continue;
6715 }
6716
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006717 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006718 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
6719 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006720 auto *PrivateRef = buildDeclRefExpr(
6721 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00006722 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006723 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006724 Expr *InitExpr;
6725 if (LinKind == OMPC_LINEAR_uval)
6726 InitExpr = VD->getInit();
6727 else
6728 InitExpr = DE;
6729 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00006730 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006731 auto InitRef = buildDeclRefExpr(
6732 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006733 DSAStack->addDSA(VD, DE, OMPC_linear);
6734 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006735 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00006736 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006737 }
6738
6739 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006740 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006741
6742 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006743 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006744 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6745 !Step->isInstantiationDependent() &&
6746 !Step->containsUnexpandedParameterPack()) {
6747 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006748 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006749 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006750 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006751 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006752
Alexander Musman3276a272015-03-21 10:12:56 +00006753 // Build var to save the step value.
6754 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006755 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006756 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006757 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006758 ExprResult CalcStep =
6759 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006760 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00006761
Alexander Musman8dba6642014-04-22 13:09:42 +00006762 // Warn about zero linear step (it would be probably better specified as
6763 // making corresponding variables 'const').
6764 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006765 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6766 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006767 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6768 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006769 if (!IsConstant && CalcStep.isUsable()) {
6770 // Calculate the step beforehand instead of doing this on each iteration.
6771 // (This is not used if the number of iterations may be kfold-ed).
6772 CalcStepExpr = CalcStep.get();
6773 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006774 }
6775
Alexey Bataev182227b2015-08-20 10:54:39 +00006776 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
6777 ColonLoc, EndLoc, Vars, Privates, Inits,
6778 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006779}
6780
6781static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6782 Expr *NumIterations, Sema &SemaRef,
6783 Scope *S) {
6784 // Walk the vars and build update/final expressions for the CodeGen.
6785 SmallVector<Expr *, 8> Updates;
6786 SmallVector<Expr *, 8> Finals;
6787 Expr *Step = Clause.getStep();
6788 Expr *CalcStep = Clause.getCalcStep();
6789 // OpenMP [2.14.3.7, linear clause]
6790 // If linear-step is not specified it is assumed to be 1.
6791 if (Step == nullptr)
6792 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6793 else if (CalcStep)
6794 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6795 bool HasErrors = false;
6796 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006797 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006798 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00006799 for (auto &RefExpr : Clause.varlists()) {
6800 Expr *InitExpr = *CurInit;
6801
6802 // Build privatized reference to the current linear var.
6803 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006804 Expr *CapturedRef;
6805 if (LinKind == OMPC_LINEAR_uval)
6806 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
6807 else
6808 CapturedRef =
6809 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6810 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6811 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006812
6813 // Build update: Var = InitExpr + IV * Step
6814 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006815 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00006816 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006817 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
6818 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006819
6820 // Build final: Var = InitExpr + NumIterations * Step
6821 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006822 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00006823 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006824 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
6825 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006826 if (!Update.isUsable() || !Final.isUsable()) {
6827 Updates.push_back(nullptr);
6828 Finals.push_back(nullptr);
6829 HasErrors = true;
6830 } else {
6831 Updates.push_back(Update.get());
6832 Finals.push_back(Final.get());
6833 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006834 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00006835 }
6836 Clause.setUpdates(Updates);
6837 Clause.setFinals(Finals);
6838 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006839}
6840
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006841OMPClause *Sema::ActOnOpenMPAlignedClause(
6842 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6843 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6844
6845 SmallVector<Expr *, 8> Vars;
6846 for (auto &RefExpr : VarList) {
6847 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6848 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6849 // It will be analyzed later.
6850 Vars.push_back(RefExpr);
6851 continue;
6852 }
6853
6854 SourceLocation ELoc = RefExpr->getExprLoc();
6855 // OpenMP [2.1, C/C++]
6856 // A list item is a variable name.
6857 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6858 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6859 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6860 continue;
6861 }
6862
6863 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6864
6865 // OpenMP [2.8.1, simd construct, Restrictions]
6866 // The type of list items appearing in the aligned clause must be
6867 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006868 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006869 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006870 const Type *Ty = QType.getTypePtrOrNull();
6871 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6872 !Ty->isPointerType())) {
6873 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6874 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6875 bool IsDecl =
6876 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6877 Diag(VD->getLocation(),
6878 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6879 << VD;
6880 continue;
6881 }
6882
6883 // OpenMP [2.8.1, simd construct, Restrictions]
6884 // A list-item cannot appear in more than one aligned clause.
6885 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6886 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6887 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6888 << getOpenMPClauseName(OMPC_aligned);
6889 continue;
6890 }
6891
6892 Vars.push_back(DE);
6893 }
6894
6895 // OpenMP [2.8.1, simd construct, Description]
6896 // The parameter of the aligned clause, alignment, must be a constant
6897 // positive integer expression.
6898 // If no optional parameter is specified, implementation-defined default
6899 // alignments for SIMD instructions on the target platforms are assumed.
6900 if (Alignment != nullptr) {
6901 ExprResult AlignResult =
6902 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6903 if (AlignResult.isInvalid())
6904 return nullptr;
6905 Alignment = AlignResult.get();
6906 }
6907 if (Vars.empty())
6908 return nullptr;
6909
6910 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6911 EndLoc, Vars, Alignment);
6912}
6913
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006914OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6915 SourceLocation StartLoc,
6916 SourceLocation LParenLoc,
6917 SourceLocation EndLoc) {
6918 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006919 SmallVector<Expr *, 8> SrcExprs;
6920 SmallVector<Expr *, 8> DstExprs;
6921 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006922 for (auto &RefExpr : VarList) {
6923 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6924 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006925 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006926 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006927 SrcExprs.push_back(nullptr);
6928 DstExprs.push_back(nullptr);
6929 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006930 continue;
6931 }
6932
Alexey Bataeved09d242014-05-28 05:53:51 +00006933 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006934 // OpenMP [2.1, C/C++]
6935 // A list item is a variable name.
6936 // OpenMP [2.14.4.1, Restrictions, p.1]
6937 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006938 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006939 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006940 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006941 continue;
6942 }
6943
6944 Decl *D = DE->getDecl();
6945 VarDecl *VD = cast<VarDecl>(D);
6946
6947 QualType Type = VD->getType();
6948 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6949 // It will be analyzed later.
6950 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006951 SrcExprs.push_back(nullptr);
6952 DstExprs.push_back(nullptr);
6953 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006954 continue;
6955 }
6956
6957 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6958 // A list item that appears in a copyin clause must be threadprivate.
6959 if (!DSAStack->isThreadPrivate(VD)) {
6960 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006961 << getOpenMPClauseName(OMPC_copyin)
6962 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006963 continue;
6964 }
6965
6966 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6967 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006968 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006969 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006970 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006971 auto *SrcVD =
6972 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
6973 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006974 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006975 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6976 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006977 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
6978 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006979 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006980 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006981 // For arrays generate assignment operation for single element and replace
6982 // it by the original array element in CodeGen.
6983 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6984 PseudoDstExpr, PseudoSrcExpr);
6985 if (AssignmentOp.isInvalid())
6986 continue;
6987 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6988 /*DiscardedValue=*/true);
6989 if (AssignmentOp.isInvalid())
6990 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006991
6992 DSAStack->addDSA(VD, DE, OMPC_copyin);
6993 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006994 SrcExprs.push_back(PseudoSrcExpr);
6995 DstExprs.push_back(PseudoDstExpr);
6996 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006997 }
6998
Alexey Bataeved09d242014-05-28 05:53:51 +00006999 if (Vars.empty())
7000 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007001
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007002 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7003 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007004}
7005
Alexey Bataevbae9a792014-06-27 10:37:06 +00007006OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7007 SourceLocation StartLoc,
7008 SourceLocation LParenLoc,
7009 SourceLocation EndLoc) {
7010 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007011 SmallVector<Expr *, 8> SrcExprs;
7012 SmallVector<Expr *, 8> DstExprs;
7013 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007014 for (auto &RefExpr : VarList) {
7015 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7016 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7017 // It will be analyzed later.
7018 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007019 SrcExprs.push_back(nullptr);
7020 DstExprs.push_back(nullptr);
7021 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007022 continue;
7023 }
7024
7025 SourceLocation ELoc = RefExpr->getExprLoc();
7026 // OpenMP [2.1, C/C++]
7027 // A list item is a variable name.
7028 // OpenMP [2.14.4.1, Restrictions, p.1]
7029 // A list item that appears in a copyin clause must be threadprivate.
7030 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7031 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7032 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7033 continue;
7034 }
7035
7036 Decl *D = DE->getDecl();
7037 VarDecl *VD = cast<VarDecl>(D);
7038
7039 QualType Type = VD->getType();
7040 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7041 // It will be analyzed later.
7042 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007043 SrcExprs.push_back(nullptr);
7044 DstExprs.push_back(nullptr);
7045 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007046 continue;
7047 }
7048
7049 // OpenMP [2.14.4.2, Restrictions, p.2]
7050 // A list item that appears in a copyprivate clause may not appear in a
7051 // private or firstprivate clause on the single construct.
7052 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007053 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007054 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7055 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007056 Diag(ELoc, diag::err_omp_wrong_dsa)
7057 << getOpenMPClauseName(DVar.CKind)
7058 << getOpenMPClauseName(OMPC_copyprivate);
7059 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7060 continue;
7061 }
7062
7063 // OpenMP [2.11.4.2, Restrictions, p.1]
7064 // All list items that appear in a copyprivate clause must be either
7065 // threadprivate or private in the enclosing context.
7066 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007067 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007068 if (DVar.CKind == OMPC_shared) {
7069 Diag(ELoc, diag::err_omp_required_access)
7070 << getOpenMPClauseName(OMPC_copyprivate)
7071 << "threadprivate or private in the enclosing context";
7072 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7073 continue;
7074 }
7075 }
7076 }
7077
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007078 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007079 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007080 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007081 << getOpenMPClauseName(OMPC_copyprivate) << Type
7082 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007083 bool IsDecl =
7084 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7085 Diag(VD->getLocation(),
7086 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7087 << VD;
7088 continue;
7089 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007090
Alexey Bataevbae9a792014-06-27 10:37:06 +00007091 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7092 // A variable of class type (or array thereof) that appears in a
7093 // copyin clause requires an accessible, unambiguous copy assignment
7094 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007095 Type = Context.getBaseElementType(Type.getNonReferenceType())
7096 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007097 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007098 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7099 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007100 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007101 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007102 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007103 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7104 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007105 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007106 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007107 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7108 PseudoDstExpr, PseudoSrcExpr);
7109 if (AssignmentOp.isInvalid())
7110 continue;
7111 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7112 /*DiscardedValue=*/true);
7113 if (AssignmentOp.isInvalid())
7114 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007115
7116 // No need to mark vars as copyprivate, they are already threadprivate or
7117 // implicitly private.
7118 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007119 SrcExprs.push_back(PseudoSrcExpr);
7120 DstExprs.push_back(PseudoDstExpr);
7121 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007122 }
7123
7124 if (Vars.empty())
7125 return nullptr;
7126
Alexey Bataeva63048e2015-03-23 06:18:07 +00007127 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7128 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007129}
7130
Alexey Bataev6125da92014-07-21 11:26:11 +00007131OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7132 SourceLocation StartLoc,
7133 SourceLocation LParenLoc,
7134 SourceLocation EndLoc) {
7135 if (VarList.empty())
7136 return nullptr;
7137
7138 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7139}
Alexey Bataevdea47612014-07-23 07:46:59 +00007140
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007141OMPClause *
7142Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7143 SourceLocation DepLoc, SourceLocation ColonLoc,
7144 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7145 SourceLocation LParenLoc, SourceLocation EndLoc) {
7146 if (DepKind == OMPC_DEPEND_unknown) {
7147 std::string Values;
7148 std::string Sep(", ");
7149 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7150 Values += "'";
7151 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7152 Values += "'";
7153 switch (i) {
7154 case OMPC_DEPEND_unknown - 2:
7155 Values += " or ";
7156 break;
7157 case OMPC_DEPEND_unknown - 1:
7158 break;
7159 default:
7160 Values += Sep;
7161 break;
7162 }
7163 }
7164 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7165 << Values << getOpenMPClauseName(OMPC_depend);
7166 return nullptr;
7167 }
7168 SmallVector<Expr *, 8> Vars;
7169 for (auto &RefExpr : VarList) {
7170 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7171 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7172 // It will be analyzed later.
7173 Vars.push_back(RefExpr);
7174 continue;
7175 }
7176
7177 SourceLocation ELoc = RefExpr->getExprLoc();
7178 // OpenMP [2.11.1.1, Restrictions, p.3]
7179 // A variable that is part of another variable (such as a field of a
7180 // structure) but is not an array element or an array section cannot appear
7181 // in a depend clause.
7182 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007183 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7184 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7185 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7186 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7187 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007188 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7189 !ASE->getBase()->getType()->isArrayType())) {
7190 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7191 << RefExpr->getSourceRange();
7192 continue;
7193 }
7194
7195 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7196 }
7197
7198 if (Vars.empty())
7199 return nullptr;
7200
7201 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7202 DepLoc, ColonLoc, Vars);
7203}
Michael Wonge710d542015-08-07 16:16:36 +00007204
7205OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7206 SourceLocation LParenLoc,
7207 SourceLocation EndLoc) {
7208 Expr *ValExpr = Device;
7209 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7210 !ValExpr->isInstantiationDependent()) {
7211 SourceLocation Loc = ValExpr->getExprLoc();
7212 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7213 if (Value.isInvalid())
7214 return nullptr;
7215
7216 // OpenMP [2.9.1, Restrictions]
7217 // The device expression must evaluate to a non-negative integer value.
7218 llvm::APSInt Result;
7219 if (Value.get()->isIntegerConstantExpr(Result, Context) &&
7220 Result.isSigned() && !Result.isStrictlyPositive()) {
7221 Diag(Loc, diag::err_omp_negative_expression_in_clause)
7222 << "device" << ValExpr->getSourceRange();
7223 return nullptr;
7224 }
7225 }
Michael Wonge710d542015-08-07 16:16:36 +00007226 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7227}