blob: 4877d3f55fc4323d5a639e92b046f48d5f8f4a92 [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 Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
24#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000026#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Scope.h"
28#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000029#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030using namespace clang;
31
Alexey Bataev758e55e2013-09-06 18:03:48 +000032//===----------------------------------------------------------------------===//
33// Stack of data-sharing attributes for variables
34//===----------------------------------------------------------------------===//
35
36namespace {
37/// \brief Default data sharing attributes, which can be applied to directive.
38enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000039 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
40 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
41 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000042};
Alexey Bataev7ff55242014-06-19 09:13:45 +000043
Alexey Bataevf29276e2014-06-18 04:14:57 +000044template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000045 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000046 bool operator()(T Kind) {
47 for (auto KindEl : Arr)
48 if (KindEl == Kind)
49 return true;
50 return false;
51 }
52
53private:
54 ArrayRef<T> Arr;
55};
Alexey Bataev23b69422014-06-18 07:08:49 +000056struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000057 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000058 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000059};
60
61typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
62typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000063
64/// \brief Stack for tracking declarations used in OpenMP directives and
65/// clauses and their data-sharing attributes.
66class DSAStackTy {
67public:
68 struct DSAVarData {
69 OpenMPDirectiveKind DKind;
70 OpenMPClauseKind CKind;
71 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000072 SourceLocation ImplicitDSALoc;
73 DSAVarData()
74 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
75 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000076 };
Alexey Bataeved09d242014-05-28 05:53:51 +000077
Alexey Bataev758e55e2013-09-06 18:03:48 +000078private:
79 struct DSAInfo {
80 OpenMPClauseKind Attributes;
81 DeclRefExpr *RefExpr;
82 };
83 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000085 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086
87 struct SharingMapTy {
88 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000089 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000090 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000092 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093 OpenMPDirectiveKind Directive;
94 DeclarationNameInfo DirectiveName;
95 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000096 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000097 bool OrderedRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +000098 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +000099 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000100 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000102 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev9c821032015-04-30 04:23:23 +0000104 ConstructLoc(Loc), OrderedRegion(false), CollapseNumber(1),
105 InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000107 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000108 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev9c821032015-04-30 04:23:23 +0000109 ConstructLoc(), OrderedRegion(false), CollapseNumber(1),
110 InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111 };
112
113 typedef SmallVector<SharingMapTy, 64> StackTy;
114
115 /// \brief Stack of used declaration and their data-sharing attributes.
116 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000117 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118
119 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
120
121 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000122
123 /// \brief Checks if the variable is a local for OpenMP region.
124 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000125
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000127 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128
129 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000130 Scope *CurScope, SourceLocation Loc) {
131 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
132 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000133 }
134
135 void pop() {
136 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
137 Stack.pop_back();
138 }
139
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000140 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000141 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000142 /// for diagnostics.
143 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
144
Alexey Bataev9c821032015-04-30 04:23:23 +0000145 /// \brief Register specified variable as loop control variable.
146 void addLoopControlVariable(VarDecl *D);
147 /// \brief Check if the specified variable is a loop control variable for
148 /// current region.
149 bool isLoopControlVariable(VarDecl *D);
150
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151 /// \brief Adds explicit data sharing attribute to the specified declaration.
152 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
153
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154 /// \brief Returns data sharing attributes from top of the stack for the
155 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000156 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000158 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000159 /// \brief Checks if the specified variables has data-sharing attributes which
160 /// match specified \a CPred predicate in any directive which matches \a DPred
161 /// predicate.
162 template <class ClausesPredicate, class DirectivesPredicate>
163 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000164 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000165 /// \brief Checks if the specified variables has data-sharing attributes which
166 /// match specified \a CPred predicate in any innermost directive which
167 /// matches \a DPred predicate.
168 template <class ClausesPredicate, class DirectivesPredicate>
169 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000170 DirectivesPredicate DPred,
171 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000172 /// \brief Finds a directive which matches specified \a DPred predicate.
173 template <class NamedDirectivesPredicate>
174 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000175
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176 /// \brief Returns currently analyzed directive.
177 OpenMPDirectiveKind getCurrentDirective() const {
178 return Stack.back().Directive;
179 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000180 /// \brief Returns parent directive.
181 OpenMPDirectiveKind getParentDirective() const {
182 if (Stack.size() > 2)
183 return Stack[Stack.size() - 2].Directive;
184 return OMPD_unknown;
185 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186
187 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000188 void setDefaultDSANone(SourceLocation Loc) {
189 Stack.back().DefaultAttr = DSA_none;
190 Stack.back().DefaultAttrLoc = Loc;
191 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000192 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000193 void setDefaultDSAShared(SourceLocation Loc) {
194 Stack.back().DefaultAttr = DSA_shared;
195 Stack.back().DefaultAttrLoc = Loc;
196 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197
198 DefaultDataSharingAttributes getDefaultDSA() const {
199 return Stack.back().DefaultAttr;
200 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000201 SourceLocation getDefaultDSALocation() const {
202 return Stack.back().DefaultAttrLoc;
203 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000204
Alexey Bataevf29276e2014-06-18 04:14:57 +0000205 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000206 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000207 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000209 }
210
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000211 /// \brief Marks current region as ordered (it has an 'ordered' clause).
212 void setOrderedRegion(bool IsOrdered = true) {
213 Stack.back().OrderedRegion = IsOrdered;
214 }
215 /// \brief Returns true, if parent region is ordered (has associated
216 /// 'ordered' clause), false - otherwise.
217 bool isParentOrderedRegion() const {
218 if (Stack.size() > 2)
219 return Stack[Stack.size() - 2].OrderedRegion;
220 return false;
221 }
222
Alexey Bataev9c821032015-04-30 04:23:23 +0000223 /// \brief Set collapse value for the region.
224 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
225 /// \brief Return collapse value for region.
226 unsigned getCollapseNumber() const {
227 return Stack.back().CollapseNumber;
228 }
229
Alexey Bataev13314bf2014-10-09 04:18:56 +0000230 /// \brief Marks current target region as one with closely nested teams
231 /// region.
232 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
233 if (Stack.size() > 2)
234 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
235 }
236 /// \brief Returns true, if current region has closely nested teams region.
237 bool hasInnerTeamsRegion() const {
238 return getInnerTeamsRegionLoc().isValid();
239 }
240 /// \brief Returns location of the nested teams region (if any).
241 SourceLocation getInnerTeamsRegionLoc() const {
242 if (Stack.size() > 1)
243 return Stack.back().InnerTeamsRegionLoc;
244 return SourceLocation();
245 }
246
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000247 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000248 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000249 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000250};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000251bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
252 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000253 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000254}
Alexey Bataeved09d242014-05-28 05:53:51 +0000255} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256
257DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
258 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000259 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000261 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a region but not in construct]
264 // File-scope or namespace-scope variables referenced in called routines
265 // in the region are shared unless they appear in a threadprivate
266 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000267 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000268 DVar.CKind = OMPC_shared;
269
270 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
271 // in a region but not in construct]
272 // Variables with static storage duration that are declared in called
273 // routines in the region are shared.
274 if (D->hasGlobalStorage())
275 DVar.CKind = OMPC_shared;
276
Alexey Bataev758e55e2013-09-06 18:03:48 +0000277 return DVar;
278 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000279
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000281 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
282 // in a Construct, C/C++, predetermined, p.1]
283 // Variables with automatic storage duration that are declared in a scope
284 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000285 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
286 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
287 DVar.CKind = OMPC_private;
288 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000289 }
290
Alexey Bataev758e55e2013-09-06 18:03:48 +0000291 // Explicitly specified attributes and local variables with predetermined
292 // attributes.
293 if (Iter->SharingMap.count(D)) {
294 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
295 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000296 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297 return DVar;
298 }
299
300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301 // in a Construct, C/C++, implicitly determined, p.1]
302 // In a parallel or task construct, the data-sharing attributes of these
303 // variables are determined by the default clause, if present.
304 switch (Iter->DefaultAttr) {
305 case DSA_shared:
306 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000307 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000308 return DVar;
309 case DSA_none:
310 return DVar;
311 case DSA_unspecified:
312 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
313 // in a Construct, implicitly determined, p.2]
314 // In a parallel construct, if no default clause is present, these
315 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000316 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000317 if (isOpenMPParallelDirective(DVar.DKind) ||
318 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000319 DVar.CKind = OMPC_shared;
320 return DVar;
321 }
322
323 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
324 // in a Construct, implicitly determined, p.4]
325 // In a task construct, if no default clause is present, a variable that in
326 // the enclosing context is determined to be shared by all implicit tasks
327 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 if (DVar.DKind == OMPD_task) {
329 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000330 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000331 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000332 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
333 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000334 // in a Construct, implicitly determined, p.6]
335 // In a task construct, if no default clause is present, a variable
336 // whose data-sharing attribute is not determined by the rules above is
337 // firstprivate.
338 DVarTemp = getDSA(I, D);
339 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000340 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000341 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000342 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000343 return DVar;
344 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000345 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000346 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000347 }
348 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000349 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000350 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000351 return DVar;
352 }
353 }
354 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
355 // in a Construct, implicitly determined, p.3]
356 // For constructs other than task, if no default clause is present, these
357 // variables inherit their data-sharing attributes from the enclosing
358 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000359 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000360}
361
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000362DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
363 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000364 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000365 auto It = Stack.back().AlignedMap.find(D);
366 if (It == Stack.back().AlignedMap.end()) {
367 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
368 Stack.back().AlignedMap[D] = NewDE;
369 return nullptr;
370 } else {
371 assert(It->second && "Unexpected nullptr expr in the aligned map");
372 return It->second;
373 }
374 return nullptr;
375}
376
Alexey Bataev9c821032015-04-30 04:23:23 +0000377void DSAStackTy::addLoopControlVariable(VarDecl *D) {
378 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
379 D = D->getCanonicalDecl();
380 Stack.back().LCVSet.insert(D);
381}
382
383bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
384 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
385 D = D->getCanonicalDecl();
386 return Stack.back().LCVSet.count(D) > 0;
387}
388
Alexey Bataev758e55e2013-09-06 18:03:48 +0000389void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000390 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000391 if (A == OMPC_threadprivate) {
392 Stack[0].SharingMap[D].Attributes = A;
393 Stack[0].SharingMap[D].RefExpr = E;
394 } else {
395 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
396 Stack.back().SharingMap[D].Attributes = A;
397 Stack.back().SharingMap[D].RefExpr = E;
398 }
399}
400
Alexey Bataeved09d242014-05-28 05:53:51 +0000401bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000402 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000403 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000404 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000405 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000406 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000407 ++I;
408 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000409 if (I == E)
410 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000411 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000412 Scope *CurScope = getCurScope();
413 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000415 }
416 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000417 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000418 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000419}
420
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000421DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000422 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 DSAVarData DVar;
424
425 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
426 // in a Construct, C/C++, predetermined, p.1]
427 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev26a39242015-01-13 03:35:30 +0000428 if (D->getTLSKind() != VarDecl::TLS_None ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000429 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
430 !D->isLocalVarDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000431 DVar.CKind = OMPC_threadprivate;
432 return DVar;
433 }
434 if (Stack[0].SharingMap.count(D)) {
435 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
436 DVar.CKind = OMPC_threadprivate;
437 return DVar;
438 }
439
440 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
441 // in a Construct, C/C++, predetermined, p.1]
442 // Variables with automatic storage duration that are declared in a scope
443 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000444 OpenMPDirectiveKind Kind =
445 FromParent ? getParentDirective() : getCurrentDirective();
446 auto StartI = std::next(Stack.rbegin());
447 auto EndI = std::prev(Stack.rend());
448 if (FromParent && StartI != EndI) {
449 StartI = std::next(StartI);
450 }
451 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000452 if (isOpenMPLocal(D, StartI) &&
453 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
454 D->getStorageClass() == SC_None)) ||
455 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000456 DVar.CKind = OMPC_private;
457 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000459
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000460 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
461 // in a Construct, C/C++, predetermined, p.4]
462 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000463 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
464 // in a Construct, C/C++, predetermined, p.7]
465 // Variables with static storage duration that are declared in a scope
466 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000467 if (D->isStaticDataMember() || D->isStaticLocal()) {
468 DSAVarData DVarTemp =
469 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
470 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
471 return DVar;
472
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000473 DVar.CKind = OMPC_shared;
474 return DVar;
475 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 }
477
478 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000479 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 while (Type->isArrayType()) {
481 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
482 Type = ElemType.getNonReferenceType().getCanonicalType();
483 }
484 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
485 // in a Construct, C/C++, predetermined, p.6]
486 // Variables with const qualified type having no mutable member are
487 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000488 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000489 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000490 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000491 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000492 // Variables with const-qualified type having no mutable member may be
493 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000494 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
495 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000496 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
497 return DVar;
498
Alexey Bataev758e55e2013-09-06 18:03:48 +0000499 DVar.CKind = OMPC_shared;
500 return DVar;
501 }
502
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 // Explicitly specified attributes and local variables with predetermined
504 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000505 auto I = std::prev(StartI);
506 if (I->SharingMap.count(D)) {
507 DVar.RefExpr = I->SharingMap[D].RefExpr;
508 DVar.CKind = I->SharingMap[D].Attributes;
509 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 }
511
512 return DVar;
513}
514
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000515DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000516 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000517 auto StartI = Stack.rbegin();
518 auto EndI = std::prev(Stack.rend());
519 if (FromParent && StartI != EndI) {
520 StartI = std::next(StartI);
521 }
522 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000523}
524
Alexey Bataevf29276e2014-06-18 04:14:57 +0000525template <class ClausesPredicate, class DirectivesPredicate>
526DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000527 DirectivesPredicate DPred,
528 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000529 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000530 auto StartI = std::next(Stack.rbegin());
531 auto EndI = std::prev(Stack.rend());
532 if (FromParent && StartI != EndI) {
533 StartI = std::next(StartI);
534 }
535 for (auto I = StartI, EE = EndI; I != EE; ++I) {
536 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000537 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000538 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000539 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000540 return DVar;
541 }
542 return DSAVarData();
543}
544
Alexey Bataevf29276e2014-06-18 04:14:57 +0000545template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000546DSAStackTy::DSAVarData
547DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
548 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000549 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000550 auto StartI = std::next(Stack.rbegin());
551 auto EndI = std::prev(Stack.rend());
552 if (FromParent && StartI != EndI) {
553 StartI = std::next(StartI);
554 }
555 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000556 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000557 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000558 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000559 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000560 return DVar;
561 return DSAVarData();
562 }
563 return DSAVarData();
564}
565
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000566template <class NamedDirectivesPredicate>
567bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
568 auto StartI = std::next(Stack.rbegin());
569 auto EndI = std::prev(Stack.rend());
570 if (FromParent && StartI != EndI) {
571 StartI = std::next(StartI);
572 }
573 for (auto I = StartI, EE = EndI; I != EE; ++I) {
574 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
575 return true;
576 }
577 return false;
578}
579
Alexey Bataev758e55e2013-09-06 18:03:48 +0000580void Sema::InitDataSharingAttributesStack() {
581 VarDataSharingAttributesStack = new DSAStackTy(*this);
582}
583
584#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
585
Alexey Bataevf841bd92014-12-16 07:00:22 +0000586bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
587 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000588 VD = VD->getCanonicalDecl();
Alexey Bataevf841bd92014-12-16 07:00:22 +0000589 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000590 if (DSAStack->isLoopControlVariable(VD))
591 return true;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000592 auto DVarPrivate = DSAStack->getTopDSA(VD, /*FromParent=*/false);
593 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
594 return true;
595 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
596 /*FromParent=*/false);
597 return DVarPrivate.CKind != OMPC_unknown;
598 }
599 return false;
600}
601
Alexey Bataeved09d242014-05-28 05:53:51 +0000602void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000603
604void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
605 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000606 Scope *CurScope, SourceLocation Loc) {
607 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000608 PushExpressionEvaluationContext(PotentiallyEvaluated);
609}
610
611void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000612 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
613 // A variable of class type (or array thereof) that appears in a lastprivate
614 // clause requires an accessible, unambiguous default constructor for the
615 // class type, unless the list item is also specified in a firstprivate
616 // clause.
617 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000618 for (auto *C : D->clauses()) {
619 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
620 SmallVector<Expr *, 8> PrivateCopies;
621 for (auto *DE : Clause->varlists()) {
622 if (DE->isValueDependent() || DE->isTypeDependent()) {
623 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000624 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000625 }
626 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000627 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000628 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000629 // Generate helper private variable and initialize it with the
630 // default value. The address of the original variable is replaced
631 // by the address of the new private variable in CodeGen. This new
632 // variable is not added to IdResolver, so the code in the OpenMP
633 // region uses original variable for proper diagnostics.
634 auto *VDPrivate = VarDecl::Create(
635 Context, CurContext, DE->getLocStart(), DE->getExprLoc(),
636 VD->getIdentifier(), VD->getType(), VD->getTypeSourceInfo(),
637 SC_Auto);
638 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
639 if (VDPrivate->isInvalidDecl())
640 continue;
641 CurContext->addDecl(VDPrivate);
642 PrivateCopies.push_back(DeclRefExpr::Create(
643 Context, NestedNameSpecifierLoc(), SourceLocation(), VDPrivate,
644 /*RefersToEnclosingVariableOrCapture=*/false, SourceLocation(),
645 DE->getType(), VK_LValue));
646 } else {
647 // The variable is also a firstprivate, so initialization sequence
648 // for private copy is generated already.
649 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000650 }
651 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000652 // Set initializers to private copies if no errors were found.
653 if (PrivateCopies.size() == Clause->varlist_size()) {
654 Clause->setPrivateCopies(PrivateCopies);
655 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000656 }
657 }
658 }
659
Alexey Bataev758e55e2013-09-06 18:03:48 +0000660 DSAStack->pop();
661 DiscardCleanupsInEvaluationContext();
662 PopExpressionEvaluationContext();
663}
664
Alexander Musman3276a272015-03-21 10:12:56 +0000665static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
666 Expr *NumIterations, Sema &SemaRef,
667 Scope *S);
668
Alexey Bataeva769e072013-03-22 06:34:35 +0000669namespace {
670
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000671class VarDeclFilterCCC : public CorrectionCandidateCallback {
672private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000673 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000674
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000675public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000676 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000677 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000678 NamedDecl *ND = Candidate.getCorrectionDecl();
679 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
680 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000681 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
682 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000683 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000684 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000685 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000686};
Alexey Bataeved09d242014-05-28 05:53:51 +0000687} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000688
689ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
690 CXXScopeSpec &ScopeSpec,
691 const DeclarationNameInfo &Id) {
692 LookupResult Lookup(*this, Id, LookupOrdinaryName);
693 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
694
695 if (Lookup.isAmbiguous())
696 return ExprError();
697
698 VarDecl *VD;
699 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000700 if (TypoCorrection Corrected = CorrectTypo(
701 Id, LookupOrdinaryName, CurScope, nullptr,
702 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000703 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000704 PDiag(Lookup.empty()
705 ? diag::err_undeclared_var_use_suggest
706 : diag::err_omp_expected_var_arg_suggest)
707 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000708 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000710 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
711 : diag::err_omp_expected_var_arg)
712 << Id.getName();
713 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000714 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 } else {
716 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000717 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000718 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
719 return ExprError();
720 }
721 }
722 Lookup.suppressDiagnostics();
723
724 // OpenMP [2.9.2, Syntax, C/C++]
725 // Variables must be file-scope, namespace-scope, or static block-scope.
726 if (!VD->hasGlobalStorage()) {
727 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000728 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
729 bool IsDecl =
730 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000731 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000732 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
733 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000734 return ExprError();
735 }
736
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000737 VarDecl *CanonicalVD = VD->getCanonicalDecl();
738 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000739 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
740 // A threadprivate directive for file-scope variables must appear outside
741 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000742 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
743 !getCurLexicalContext()->isTranslationUnit()) {
744 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000745 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
746 bool IsDecl =
747 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
748 Diag(VD->getLocation(),
749 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
750 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000751 return ExprError();
752 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
754 // A threadprivate directive for static class member variables must appear
755 // in the class definition, in the same scope in which the member
756 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000757 if (CanonicalVD->isStaticDataMember() &&
758 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
759 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000760 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
761 bool IsDecl =
762 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
763 Diag(VD->getLocation(),
764 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
765 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000766 return ExprError();
767 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000768 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
769 // A threadprivate directive for namespace-scope variables must appear
770 // outside any definition or declaration other than the namespace
771 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000772 if (CanonicalVD->getDeclContext()->isNamespace() &&
773 (!getCurLexicalContext()->isFileContext() ||
774 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
775 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000776 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
777 bool IsDecl =
778 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
779 Diag(VD->getLocation(),
780 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
781 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000782 return ExprError();
783 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000784 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
785 // A threadprivate directive for static block-scope variables must appear
786 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000787 if (CanonicalVD->isStaticLocal() && CurScope &&
788 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000789 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000790 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
791 bool IsDecl =
792 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
793 Diag(VD->getLocation(),
794 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
795 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000796 return ExprError();
797 }
798
799 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
800 // A threadprivate directive must lexically precede all references to any
801 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000802 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000803 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000804 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000805 return ExprError();
806 }
807
808 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000809 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000810 return DE;
811}
812
Alexey Bataeved09d242014-05-28 05:53:51 +0000813Sema::DeclGroupPtrTy
814Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
815 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000816 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000817 CurContext->addDecl(D);
818 return DeclGroupPtrTy::make(DeclGroupRef(D));
819 }
820 return DeclGroupPtrTy();
821}
822
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000823namespace {
824class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
825 Sema &SemaRef;
826
827public:
828 bool VisitDeclRefExpr(const DeclRefExpr *E) {
829 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
830 if (VD->hasLocalStorage()) {
831 SemaRef.Diag(E->getLocStart(),
832 diag::err_omp_local_var_in_threadprivate_init)
833 << E->getSourceRange();
834 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
835 << VD << VD->getSourceRange();
836 return true;
837 }
838 }
839 return false;
840 }
841 bool VisitStmt(const Stmt *S) {
842 for (auto Child : S->children()) {
843 if (Child && Visit(Child))
844 return true;
845 }
846 return false;
847 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000848 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000849};
850} // namespace
851
Alexey Bataeved09d242014-05-28 05:53:51 +0000852OMPThreadPrivateDecl *
853Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000854 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000855 for (auto &RefExpr : VarList) {
856 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000857 VarDecl *VD = cast<VarDecl>(DE->getDecl());
858 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000859
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000860 QualType QType = VD->getType();
861 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
862 // It will be analyzed later.
863 Vars.push_back(DE);
864 continue;
865 }
866
Alexey Bataeva769e072013-03-22 06:34:35 +0000867 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
868 // A threadprivate variable must not have an incomplete type.
869 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000870 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000871 continue;
872 }
873
874 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
875 // A threadprivate variable must not have a reference type.
876 if (VD->getType()->isReferenceType()) {
877 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000878 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
879 bool IsDecl =
880 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
881 Diag(VD->getLocation(),
882 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
883 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000884 continue;
885 }
886
Richard Smithfd3834f2013-04-13 02:43:54 +0000887 // Check if this is a TLS variable.
Alexey Bataev26a39242015-01-13 03:35:30 +0000888 if (VD->getTLSKind() != VarDecl::TLS_None ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000889 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
890 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000891 Diag(ILoc, diag::err_omp_var_thread_local)
892 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000893 bool IsDecl =
894 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
895 Diag(VD->getLocation(),
896 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
897 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000898 continue;
899 }
900
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000901 // Check if initial value of threadprivate variable reference variable with
902 // local storage (it is not supported by runtime).
903 if (auto Init = VD->getAnyInitializer()) {
904 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000905 if (Checker.Visit(Init))
906 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000907 }
908
Alexey Bataeved09d242014-05-28 05:53:51 +0000909 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000910 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000911 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
912 Context, SourceRange(Loc, Loc)));
913 if (auto *ML = Context.getASTMutationListener())
914 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000915 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000916 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000917 if (!Vars.empty()) {
918 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
919 Vars);
920 D->setAccess(AS_public);
921 }
922 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000923}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000924
Alexey Bataev7ff55242014-06-19 09:13:45 +0000925static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
926 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
927 bool IsLoopIterVar = false) {
928 if (DVar.RefExpr) {
929 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
930 << getOpenMPClauseName(DVar.CKind);
931 return;
932 }
933 enum {
934 PDSA_StaticMemberShared,
935 PDSA_StaticLocalVarShared,
936 PDSA_LoopIterVarPrivate,
937 PDSA_LoopIterVarLinear,
938 PDSA_LoopIterVarLastprivate,
939 PDSA_ConstVarShared,
940 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000941 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000942 PDSA_LocalVarPrivate,
943 PDSA_Implicit
944 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000945 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000946 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000947 if (IsLoopIterVar) {
948 if (DVar.CKind == OMPC_private)
949 Reason = PDSA_LoopIterVarPrivate;
950 else if (DVar.CKind == OMPC_lastprivate)
951 Reason = PDSA_LoopIterVarLastprivate;
952 else
953 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000954 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
955 Reason = PDSA_TaskVarFirstprivate;
956 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000957 } else if (VD->isStaticLocal())
958 Reason = PDSA_StaticLocalVarShared;
959 else if (VD->isStaticDataMember())
960 Reason = PDSA_StaticMemberShared;
961 else if (VD->isFileVarDecl())
962 Reason = PDSA_GlobalVarShared;
963 else if (VD->getType().isConstant(SemaRef.getASTContext()))
964 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000965 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000966 ReportHint = true;
967 Reason = PDSA_LocalVarPrivate;
968 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000969 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000970 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000971 << Reason << ReportHint
972 << getOpenMPDirectiveName(Stack->getCurrentDirective());
973 } else if (DVar.ImplicitDSALoc.isValid()) {
974 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
975 << getOpenMPClauseName(DVar.CKind);
976 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000977}
978
Alexey Bataev758e55e2013-09-06 18:03:48 +0000979namespace {
980class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
981 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000982 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000983 bool ErrorFound;
984 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000985 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000986 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000987
Alexey Bataev758e55e2013-09-06 18:03:48 +0000988public:
989 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000990 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000991 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000992 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
993 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000994
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000995 auto DVar = Stack->getTopDSA(VD, false);
996 // Check if the variable has explicit DSA set and stop analysis if it so.
997 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000998
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000999 auto ELoc = E->getExprLoc();
1000 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001001 // The default(none) clause requires that each variable that is referenced
1002 // in the construct, and does not have a predetermined data-sharing
1003 // attribute, must have its data-sharing attribute explicitly determined
1004 // by being listed in a data-sharing attribute clause.
1005 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001006 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001007 VarsWithInheritedDSA.count(VD) == 0) {
1008 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001009 return;
1010 }
1011
1012 // OpenMP [2.9.3.6, Restrictions, p.2]
1013 // A list item that appears in a reduction clause of the innermost
1014 // enclosing worksharing or parallel construct may not be accessed in an
1015 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001016 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001017 [](OpenMPDirectiveKind K) -> bool {
1018 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001019 isOpenMPWorksharingDirective(K) ||
1020 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001021 },
1022 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001023 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1024 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001025 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1026 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001027 return;
1028 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001029
1030 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001031 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001032 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001033 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034 }
1035 }
1036 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001037 for (auto *C : S->clauses()) {
1038 // Skip analysis of arguments of implicitly defined firstprivate clause
1039 // for task directives.
1040 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1041 for (auto *CC : C->children()) {
1042 if (CC)
1043 Visit(CC);
1044 }
1045 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001046 }
1047 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001048 for (auto *C : S->children()) {
1049 if (C && !isa<OMPExecutableDirective>(C))
1050 Visit(C);
1051 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001052 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001053
1054 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001055 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001056 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1057 return VarsWithInheritedDSA;
1058 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001059
Alexey Bataev7ff55242014-06-19 09:13:45 +00001060 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1061 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001062};
Alexey Bataeved09d242014-05-28 05:53:51 +00001063} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001064
Alexey Bataevbae9a792014-06-27 10:37:06 +00001065void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001066 switch (DKind) {
1067 case OMPD_parallel: {
1068 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1069 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001070 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001071 std::make_pair(".global_tid.", KmpInt32PtrTy),
1072 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1073 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001074 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001075 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1076 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001077 break;
1078 }
1079 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001080 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001081 std::make_pair(StringRef(), QualType()) // __context with shared vars
1082 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001083 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1084 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001085 break;
1086 }
1087 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001088 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001089 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001090 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001091 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1092 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001093 break;
1094 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001095 case OMPD_for_simd: {
1096 Sema::CapturedParamNameType Params[] = {
1097 std::make_pair(StringRef(), QualType()) // __context with shared vars
1098 };
1099 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1100 Params);
1101 break;
1102 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001103 case OMPD_sections: {
1104 Sema::CapturedParamNameType Params[] = {
1105 std::make_pair(StringRef(), QualType()) // __context with shared vars
1106 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001107 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1108 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001109 break;
1110 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001111 case OMPD_section: {
1112 Sema::CapturedParamNameType Params[] = {
1113 std::make_pair(StringRef(), QualType()) // __context with shared vars
1114 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001115 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1116 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001117 break;
1118 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001119 case OMPD_single: {
1120 Sema::CapturedParamNameType Params[] = {
1121 std::make_pair(StringRef(), QualType()) // __context with shared vars
1122 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001123 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1124 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001125 break;
1126 }
Alexander Musman80c22892014-07-17 08:54:58 +00001127 case OMPD_master: {
1128 Sema::CapturedParamNameType Params[] = {
1129 std::make_pair(StringRef(), QualType()) // __context with shared vars
1130 };
1131 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1132 Params);
1133 break;
1134 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001135 case OMPD_critical: {
1136 Sema::CapturedParamNameType Params[] = {
1137 std::make_pair(StringRef(), QualType()) // __context with shared vars
1138 };
1139 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1140 Params);
1141 break;
1142 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001143 case OMPD_parallel_for: {
1144 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1145 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1146 Sema::CapturedParamNameType Params[] = {
1147 std::make_pair(".global_tid.", KmpInt32PtrTy),
1148 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1149 std::make_pair(StringRef(), QualType()) // __context with shared vars
1150 };
1151 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1152 Params);
1153 break;
1154 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001155 case OMPD_parallel_for_simd: {
1156 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1157 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1158 Sema::CapturedParamNameType Params[] = {
1159 std::make_pair(".global_tid.", KmpInt32PtrTy),
1160 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1161 std::make_pair(StringRef(), QualType()) // __context with shared vars
1162 };
1163 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1164 Params);
1165 break;
1166 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001167 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001168 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1169 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001170 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001171 std::make_pair(".global_tid.", KmpInt32PtrTy),
1172 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001173 std::make_pair(StringRef(), QualType()) // __context with shared vars
1174 };
1175 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1176 Params);
1177 break;
1178 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001179 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001180 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001181 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001182 std::make_pair(".global_tid.", KmpInt32Ty),
1183 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001184 std::make_pair(StringRef(), QualType()) // __context with shared vars
1185 };
1186 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1187 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001188 // Mark this captured region as inlined, because we don't use outlined
1189 // function directly.
1190 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1191 AlwaysInlineAttr::CreateImplicit(
1192 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001193 break;
1194 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001195 case OMPD_ordered: {
1196 Sema::CapturedParamNameType Params[] = {
1197 std::make_pair(StringRef(), QualType()) // __context with shared vars
1198 };
1199 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1200 Params);
1201 break;
1202 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001203 case OMPD_atomic: {
1204 Sema::CapturedParamNameType Params[] = {
1205 std::make_pair(StringRef(), QualType()) // __context with shared vars
1206 };
1207 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1208 Params);
1209 break;
1210 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001211 case OMPD_target: {
1212 Sema::CapturedParamNameType Params[] = {
1213 std::make_pair(StringRef(), QualType()) // __context with shared vars
1214 };
1215 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1216 Params);
1217 break;
1218 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001219 case OMPD_teams: {
1220 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1221 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1222 Sema::CapturedParamNameType Params[] = {
1223 std::make_pair(".global_tid.", KmpInt32PtrTy),
1224 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1225 std::make_pair(StringRef(), QualType()) // __context with shared vars
1226 };
1227 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1228 Params);
1229 break;
1230 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001231 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001232 case OMPD_taskyield:
1233 case OMPD_barrier:
1234 case OMPD_taskwait:
1235 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001236 llvm_unreachable("OpenMP Directive is not allowed");
1237 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001238 llvm_unreachable("Unknown OpenMP directive");
1239 }
1240}
1241
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001242StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1243 ArrayRef<OMPClause *> Clauses) {
1244 if (!S.isUsable()) {
1245 ActOnCapturedRegionError();
1246 return StmtError();
1247 }
1248 // Mark all variables in private list clauses as used in inner region. This is
1249 // required for proper codegen.
1250 for (auto *Clause : Clauses) {
1251 if (isOpenMPPrivate(Clause->getClauseKind())) {
1252 for (auto *VarRef : Clause->children()) {
1253 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001254 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001255 }
1256 }
1257 }
1258 }
1259 return ActOnCapturedRegionEnd(S.get());
1260}
1261
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001262static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1263 OpenMPDirectiveKind CurrentRegion,
1264 const DeclarationNameInfo &CurrentName,
1265 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001266 // Allowed nesting of constructs
1267 // +------------------+-----------------+------------------------------------+
1268 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1269 // +------------------+-----------------+------------------------------------+
1270 // | parallel | parallel | * |
1271 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001272 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001273 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001274 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001275 // | parallel | simd | * |
1276 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001277 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001278 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001279 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001280 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001281 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001282 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001283 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001284 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001285 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001286 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001287 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001288 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001289 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001290 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001291 // +------------------+-----------------+------------------------------------+
1292 // | for | parallel | * |
1293 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001294 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001295 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001296 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001297 // | for | simd | * |
1298 // | for | sections | + |
1299 // | for | section | + |
1300 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001301 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001302 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001303 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001304 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001305 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001306 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001307 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001308 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001309 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001310 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001311 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001312 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001313 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001314 // | master | parallel | * |
1315 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001316 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001317 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001318 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001319 // | master | simd | * |
1320 // | master | sections | + |
1321 // | master | section | + |
1322 // | master | single | + |
1323 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001324 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001325 // | master |parallel sections| * |
1326 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001327 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001328 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001329 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001330 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001331 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001332 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001333 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001334 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001335 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001336 // | critical | parallel | * |
1337 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001338 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001339 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001340 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001341 // | critical | simd | * |
1342 // | critical | sections | + |
1343 // | critical | section | + |
1344 // | critical | single | + |
1345 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001346 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001347 // | critical |parallel sections| * |
1348 // | critical | task | * |
1349 // | critical | taskyield | * |
1350 // | critical | barrier | + |
1351 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001352 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001353 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001354 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001355 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001356 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001357 // | simd | parallel | |
1358 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001359 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001360 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001361 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001362 // | simd | simd | |
1363 // | simd | sections | |
1364 // | simd | section | |
1365 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001366 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001367 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001368 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001369 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001370 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001371 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001372 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001373 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001374 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001375 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001376 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001377 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001378 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001379 // | for simd | parallel | |
1380 // | for simd | for | |
1381 // | for simd | for simd | |
1382 // | for simd | master | |
1383 // | for simd | critical | |
1384 // | for simd | simd | |
1385 // | for simd | sections | |
1386 // | for simd | section | |
1387 // | for simd | single | |
1388 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001389 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001390 // | for simd |parallel sections| |
1391 // | for simd | task | |
1392 // | for simd | taskyield | |
1393 // | for simd | barrier | |
1394 // | for simd | taskwait | |
1395 // | for simd | flush | |
1396 // | for simd | ordered | |
1397 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001398 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001399 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001400 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001401 // | parallel for simd| parallel | |
1402 // | parallel for simd| for | |
1403 // | parallel for simd| for simd | |
1404 // | parallel for simd| master | |
1405 // | parallel for simd| critical | |
1406 // | parallel for simd| simd | |
1407 // | parallel for simd| sections | |
1408 // | parallel for simd| section | |
1409 // | parallel for simd| single | |
1410 // | parallel for simd| parallel for | |
1411 // | parallel for simd|parallel for simd| |
1412 // | parallel for simd|parallel sections| |
1413 // | parallel for simd| task | |
1414 // | parallel for simd| taskyield | |
1415 // | parallel for simd| barrier | |
1416 // | parallel for simd| taskwait | |
1417 // | parallel for simd| flush | |
1418 // | parallel for simd| ordered | |
1419 // | parallel for simd| atomic | |
1420 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001421 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001422 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001423 // | sections | parallel | * |
1424 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001425 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001426 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001427 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001428 // | sections | simd | * |
1429 // | sections | sections | + |
1430 // | sections | section | * |
1431 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001432 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001433 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001434 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001435 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001436 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001437 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001438 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001439 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001440 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001441 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001442 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001443 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001444 // +------------------+-----------------+------------------------------------+
1445 // | section | parallel | * |
1446 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001447 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001448 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001449 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001450 // | section | simd | * |
1451 // | section | sections | + |
1452 // | section | section | + |
1453 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001454 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001455 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001456 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001457 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001458 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001459 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001460 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001461 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001462 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001463 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001464 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001465 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001466 // +------------------+-----------------+------------------------------------+
1467 // | single | parallel | * |
1468 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001469 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001470 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001471 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001472 // | single | simd | * |
1473 // | single | sections | + |
1474 // | single | section | + |
1475 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001476 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001477 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001478 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001479 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001480 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001481 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001482 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001483 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001484 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001485 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001486 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001487 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001488 // +------------------+-----------------+------------------------------------+
1489 // | parallel for | parallel | * |
1490 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001491 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001492 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001493 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001494 // | parallel for | simd | * |
1495 // | parallel for | sections | + |
1496 // | parallel for | section | + |
1497 // | parallel for | single | + |
1498 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001499 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001500 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001501 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001502 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001503 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001504 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001505 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001506 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001507 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001508 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001509 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001510 // +------------------+-----------------+------------------------------------+
1511 // | parallel sections| parallel | * |
1512 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001513 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001514 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001515 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001516 // | parallel sections| simd | * |
1517 // | parallel sections| sections | + |
1518 // | parallel sections| section | * |
1519 // | parallel sections| single | + |
1520 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001521 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001522 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001523 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001524 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001525 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001526 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001527 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001528 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001529 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001530 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001531 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001532 // +------------------+-----------------+------------------------------------+
1533 // | task | parallel | * |
1534 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001535 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001536 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001537 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001538 // | task | simd | * |
1539 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001540 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001541 // | task | single | + |
1542 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001543 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001544 // | task |parallel sections| * |
1545 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001546 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001547 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001548 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001549 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001550 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001551 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001552 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001553 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001554 // +------------------+-----------------+------------------------------------+
1555 // | ordered | parallel | * |
1556 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001557 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001558 // | ordered | master | * |
1559 // | ordered | critical | * |
1560 // | ordered | simd | * |
1561 // | ordered | sections | + |
1562 // | ordered | section | + |
1563 // | ordered | single | + |
1564 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001565 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001566 // | ordered |parallel sections| * |
1567 // | ordered | task | * |
1568 // | ordered | taskyield | * |
1569 // | ordered | barrier | + |
1570 // | ordered | taskwait | * |
1571 // | ordered | flush | * |
1572 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001573 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001574 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001575 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001576 // +------------------+-----------------+------------------------------------+
1577 // | atomic | parallel | |
1578 // | atomic | for | |
1579 // | atomic | for simd | |
1580 // | atomic | master | |
1581 // | atomic | critical | |
1582 // | atomic | simd | |
1583 // | atomic | sections | |
1584 // | atomic | section | |
1585 // | atomic | single | |
1586 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001587 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001588 // | atomic |parallel sections| |
1589 // | atomic | task | |
1590 // | atomic | taskyield | |
1591 // | atomic | barrier | |
1592 // | atomic | taskwait | |
1593 // | atomic | flush | |
1594 // | atomic | ordered | |
1595 // | atomic | atomic | |
1596 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001597 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001598 // +------------------+-----------------+------------------------------------+
1599 // | target | parallel | * |
1600 // | target | for | * |
1601 // | target | for simd | * |
1602 // | target | master | * |
1603 // | target | critical | * |
1604 // | target | simd | * |
1605 // | target | sections | * |
1606 // | target | section | * |
1607 // | target | single | * |
1608 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001609 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001610 // | target |parallel sections| * |
1611 // | target | task | * |
1612 // | target | taskyield | * |
1613 // | target | barrier | * |
1614 // | target | taskwait | * |
1615 // | target | flush | * |
1616 // | target | ordered | * |
1617 // | target | atomic | * |
1618 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001619 // | target | teams | * |
1620 // +------------------+-----------------+------------------------------------+
1621 // | teams | parallel | * |
1622 // | teams | for | + |
1623 // | teams | for simd | + |
1624 // | teams | master | + |
1625 // | teams | critical | + |
1626 // | teams | simd | + |
1627 // | teams | sections | + |
1628 // | teams | section | + |
1629 // | teams | single | + |
1630 // | teams | parallel for | * |
1631 // | teams |parallel for simd| * |
1632 // | teams |parallel sections| * |
1633 // | teams | task | + |
1634 // | teams | taskyield | + |
1635 // | teams | barrier | + |
1636 // | teams | taskwait | + |
1637 // | teams | flush | + |
1638 // | teams | ordered | + |
1639 // | teams | atomic | + |
1640 // | teams | target | + |
1641 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001642 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001643 if (Stack->getCurScope()) {
1644 auto ParentRegion = Stack->getParentDirective();
1645 bool NestingProhibited = false;
1646 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001647 enum {
1648 NoRecommend,
1649 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001650 ShouldBeInOrderedRegion,
1651 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001652 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001653 if (isOpenMPSimdDirective(ParentRegion)) {
1654 // OpenMP [2.16, Nesting of Regions]
1655 // OpenMP constructs may not be nested inside a simd region.
1656 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1657 return true;
1658 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001659 if (ParentRegion == OMPD_atomic) {
1660 // OpenMP [2.16, Nesting of Regions]
1661 // OpenMP constructs may not be nested inside an atomic region.
1662 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1663 return true;
1664 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001665 if (CurrentRegion == OMPD_section) {
1666 // OpenMP [2.7.2, sections Construct, Restrictions]
1667 // Orphaned section directives are prohibited. That is, the section
1668 // directives must appear within the sections construct and must not be
1669 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001670 if (ParentRegion != OMPD_sections &&
1671 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001672 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1673 << (ParentRegion != OMPD_unknown)
1674 << getOpenMPDirectiveName(ParentRegion);
1675 return true;
1676 }
1677 return false;
1678 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001679 // Allow some constructs to be orphaned (they could be used in functions,
1680 // called from OpenMP regions with the required preconditions).
1681 if (ParentRegion == OMPD_unknown)
1682 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001683 if (CurrentRegion == OMPD_master) {
1684 // OpenMP [2.16, Nesting of Regions]
1685 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001686 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001687 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1688 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001689 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1690 // OpenMP [2.16, Nesting of Regions]
1691 // A critical region may not be nested (closely or otherwise) inside a
1692 // critical region with the same name. Note that this restriction is not
1693 // sufficient to prevent deadlock.
1694 SourceLocation PreviousCriticalLoc;
1695 bool DeadLock =
1696 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1697 OpenMPDirectiveKind K,
1698 const DeclarationNameInfo &DNI,
1699 SourceLocation Loc)
1700 ->bool {
1701 if (K == OMPD_critical &&
1702 DNI.getName() == CurrentName.getName()) {
1703 PreviousCriticalLoc = Loc;
1704 return true;
1705 } else
1706 return false;
1707 },
1708 false /* skip top directive */);
1709 if (DeadLock) {
1710 SemaRef.Diag(StartLoc,
1711 diag::err_omp_prohibited_region_critical_same_name)
1712 << CurrentName.getName();
1713 if (PreviousCriticalLoc.isValid())
1714 SemaRef.Diag(PreviousCriticalLoc,
1715 diag::note_omp_previous_critical_region);
1716 return true;
1717 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001718 } else if (CurrentRegion == OMPD_barrier) {
1719 // OpenMP [2.16, Nesting of Regions]
1720 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001721 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001722 NestingProhibited =
1723 isOpenMPWorksharingDirective(ParentRegion) ||
1724 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1725 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001726 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001727 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001728 // OpenMP [2.16, Nesting of Regions]
1729 // A worksharing region may not be closely nested inside a worksharing,
1730 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001731 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001732 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001733 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1734 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1735 Recommend = ShouldBeInParallelRegion;
1736 } else if (CurrentRegion == OMPD_ordered) {
1737 // OpenMP [2.16, Nesting of Regions]
1738 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001739 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001740 // An ordered region must be closely nested inside a loop region (or
1741 // parallel loop region) with an ordered clause.
1742 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001743 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001744 !Stack->isParentOrderedRegion();
1745 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001746 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1747 // OpenMP [2.16, Nesting of Regions]
1748 // If specified, a teams construct must be contained within a target
1749 // construct.
1750 NestingProhibited = ParentRegion != OMPD_target;
1751 Recommend = ShouldBeInTargetRegion;
1752 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1753 }
1754 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1755 // OpenMP [2.16, Nesting of Regions]
1756 // distribute, parallel, parallel sections, parallel workshare, and the
1757 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1758 // constructs that can be closely nested in the teams region.
1759 // TODO: add distribute directive.
1760 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1761 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001762 }
1763 if (NestingProhibited) {
1764 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001765 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1766 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001767 return true;
1768 }
1769 }
1770 return false;
1771}
1772
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001773StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001774 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001775 ArrayRef<OMPClause *> Clauses,
1776 Stmt *AStmt,
1777 SourceLocation StartLoc,
1778 SourceLocation EndLoc) {
1779 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001780 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001781 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001782
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001783 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001784 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001785 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001786 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001787 if (AStmt) {
1788 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1789
1790 // Check default data sharing attributes for referenced variables.
1791 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1792 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1793 if (DSAChecker.isErrorFound())
1794 return StmtError();
1795 // Generate list of implicitly defined firstprivate variables.
1796 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001797
1798 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1799 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1800 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1801 SourceLocation(), SourceLocation())) {
1802 ClausesWithImplicit.push_back(Implicit);
1803 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1804 DSAChecker.getImplicitFirstprivate().size();
1805 } else
1806 ErrorFound = true;
1807 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001808 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001809
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001810 switch (Kind) {
1811 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001812 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1813 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001814 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001815 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001816 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1817 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001818 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001819 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001820 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1821 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001822 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001823 case OMPD_for_simd:
1824 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1825 EndLoc, VarsWithInheritedDSA);
1826 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001827 case OMPD_sections:
1828 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1829 EndLoc);
1830 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001831 case OMPD_section:
1832 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001833 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001834 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1835 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001836 case OMPD_single:
1837 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1838 EndLoc);
1839 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001840 case OMPD_master:
1841 assert(ClausesWithImplicit.empty() &&
1842 "No clauses are allowed for 'omp master' directive");
1843 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1844 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001845 case OMPD_critical:
1846 assert(ClausesWithImplicit.empty() &&
1847 "No clauses are allowed for 'omp critical' directive");
1848 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1849 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001850 case OMPD_parallel_for:
1851 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1852 EndLoc, VarsWithInheritedDSA);
1853 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001854 case OMPD_parallel_for_simd:
1855 Res = ActOnOpenMPParallelForSimdDirective(
1856 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1857 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001858 case OMPD_parallel_sections:
1859 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1860 StartLoc, EndLoc);
1861 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001862 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001863 Res =
1864 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1865 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001866 case OMPD_taskyield:
1867 assert(ClausesWithImplicit.empty() &&
1868 "No clauses are allowed for 'omp taskyield' directive");
1869 assert(AStmt == nullptr &&
1870 "No associated statement allowed for 'omp taskyield' directive");
1871 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1872 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001873 case OMPD_barrier:
1874 assert(ClausesWithImplicit.empty() &&
1875 "No clauses are allowed for 'omp barrier' directive");
1876 assert(AStmt == nullptr &&
1877 "No associated statement allowed for 'omp barrier' directive");
1878 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1879 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001880 case OMPD_taskwait:
1881 assert(ClausesWithImplicit.empty() &&
1882 "No clauses are allowed for 'omp taskwait' directive");
1883 assert(AStmt == nullptr &&
1884 "No associated statement allowed for 'omp taskwait' directive");
1885 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1886 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001887 case OMPD_flush:
1888 assert(AStmt == nullptr &&
1889 "No associated statement allowed for 'omp flush' directive");
1890 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1891 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001892 case OMPD_ordered:
1893 assert(ClausesWithImplicit.empty() &&
1894 "No clauses are allowed for 'omp ordered' directive");
1895 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1896 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001897 case OMPD_atomic:
1898 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1899 EndLoc);
1900 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001901 case OMPD_teams:
1902 Res =
1903 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1904 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001905 case OMPD_target:
1906 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1907 EndLoc);
1908 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001909 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001910 llvm_unreachable("OpenMP Directive is not allowed");
1911 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001912 llvm_unreachable("Unknown OpenMP directive");
1913 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001914
Alexey Bataev4acb8592014-07-07 13:01:15 +00001915 for (auto P : VarsWithInheritedDSA) {
1916 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1917 << P.first << P.second->getSourceRange();
1918 }
1919 if (!VarsWithInheritedDSA.empty())
1920 return StmtError();
1921
Alexey Bataeved09d242014-05-28 05:53:51 +00001922 if (ErrorFound)
1923 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001924 return Res;
1925}
1926
1927StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1928 Stmt *AStmt,
1929 SourceLocation StartLoc,
1930 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001931 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1932 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1933 // 1.2.2 OpenMP Language Terminology
1934 // Structured block - An executable statement with a single entry at the
1935 // top and a single exit at the bottom.
1936 // The point of exit cannot be a branch out of the structured block.
1937 // longjmp() and throw() must not violate the entry/exit criteria.
1938 CS->getCapturedDecl()->setNothrow();
1939
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001940 getCurFunction()->setHasBranchProtectedScope();
1941
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001942 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1943 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001944}
1945
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001946namespace {
1947/// \brief Helper class for checking canonical form of the OpenMP loops and
1948/// extracting iteration space of each loop in the loop nest, that will be used
1949/// for IR generation.
1950class OpenMPIterationSpaceChecker {
1951 /// \brief Reference to Sema.
1952 Sema &SemaRef;
1953 /// \brief A location for diagnostics (when there is no some better location).
1954 SourceLocation DefaultLoc;
1955 /// \brief A location for diagnostics (when increment is not compatible).
1956 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001957 /// \brief A source location for referring to loop init later.
1958 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001959 /// \brief A source location for referring to condition later.
1960 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001961 /// \brief A source location for referring to increment later.
1962 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001963 /// \brief Loop variable.
1964 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001965 /// \brief Reference to loop variable.
1966 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001967 /// \brief Lower bound (initializer for the var).
1968 Expr *LB;
1969 /// \brief Upper bound.
1970 Expr *UB;
1971 /// \brief Loop step (increment).
1972 Expr *Step;
1973 /// \brief This flag is true when condition is one of:
1974 /// Var < UB
1975 /// Var <= UB
1976 /// UB > Var
1977 /// UB >= Var
1978 bool TestIsLessOp;
1979 /// \brief This flag is true when condition is strict ( < or > ).
1980 bool TestIsStrictOp;
1981 /// \brief This flag is true when step is subtracted on each iteration.
1982 bool SubtractStep;
1983
1984public:
1985 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1986 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001987 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1988 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001989 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1990 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001991 /// \brief Check init-expr for canonical loop form and save loop counter
1992 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00001993 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001994 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1995 /// for less/greater and for strict/non-strict comparison.
1996 bool CheckCond(Expr *S);
1997 /// \brief Check incr-expr for canonical loop form and return true if it
1998 /// does not conform, otherwise save loop step (#Step).
1999 bool CheckInc(Expr *S);
2000 /// \brief Return the loop counter variable.
2001 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002002 /// \brief Return the reference expression to loop counter variable.
2003 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002004 /// \brief Source range of the loop init.
2005 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2006 /// \brief Source range of the loop condition.
2007 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2008 /// \brief Source range of the loop increment.
2009 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2010 /// \brief True if the step should be subtracted.
2011 bool ShouldSubtractStep() const { return SubtractStep; }
2012 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002013 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002014 /// \brief Build the precondition expression for the loops.
2015 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002016 /// \brief Build reference expression to the counter be used for codegen.
2017 Expr *BuildCounterVar() const;
2018 /// \brief Build initization of the counter be used for codegen.
2019 Expr *BuildCounterInit() const;
2020 /// \brief Build step of the counter be used for codegen.
2021 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002022 /// \brief Return true if any expression is dependent.
2023 bool Dependent() const;
2024
2025private:
2026 /// \brief Check the right-hand side of an assignment in the increment
2027 /// expression.
2028 bool CheckIncRHS(Expr *RHS);
2029 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002030 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002031 /// \brief Helper to set upper bound.
2032 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2033 const SourceLocation &SL);
2034 /// \brief Helper to set loop increment.
2035 bool SetStep(Expr *NewStep, bool Subtract);
2036};
2037
2038bool OpenMPIterationSpaceChecker::Dependent() const {
2039 if (!Var) {
2040 assert(!LB && !UB && !Step);
2041 return false;
2042 }
2043 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2044 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2045}
2046
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002047bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2048 DeclRefExpr *NewVarRefExpr,
2049 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002050 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002051 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2052 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002053 if (!NewVar || !NewLB)
2054 return true;
2055 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002056 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002057 LB = NewLB;
2058 return false;
2059}
2060
2061bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2062 const SourceRange &SR,
2063 const SourceLocation &SL) {
2064 // State consistency checking to ensure correct usage.
2065 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2066 !TestIsLessOp && !TestIsStrictOp);
2067 if (!NewUB)
2068 return true;
2069 UB = NewUB;
2070 TestIsLessOp = LessOp;
2071 TestIsStrictOp = StrictOp;
2072 ConditionSrcRange = SR;
2073 ConditionLoc = SL;
2074 return false;
2075}
2076
2077bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2078 // State consistency checking to ensure correct usage.
2079 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2080 if (!NewStep)
2081 return true;
2082 if (!NewStep->isValueDependent()) {
2083 // Check that the step is integer expression.
2084 SourceLocation StepLoc = NewStep->getLocStart();
2085 ExprResult Val =
2086 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2087 if (Val.isInvalid())
2088 return true;
2089 NewStep = Val.get();
2090
2091 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2092 // If test-expr is of form var relational-op b and relational-op is < or
2093 // <= then incr-expr must cause var to increase on each iteration of the
2094 // loop. If test-expr is of form var relational-op b and relational-op is
2095 // > or >= then incr-expr must cause var to decrease on each iteration of
2096 // the loop.
2097 // If test-expr is of form b relational-op var and relational-op is < or
2098 // <= then incr-expr must cause var to decrease on each iteration of the
2099 // loop. If test-expr is of form b relational-op var and relational-op is
2100 // > or >= then incr-expr must cause var to increase on each iteration of
2101 // the loop.
2102 llvm::APSInt Result;
2103 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2104 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2105 bool IsConstNeg =
2106 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002107 bool IsConstPos =
2108 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002109 bool IsConstZero = IsConstant && !Result.getBoolValue();
2110 if (UB && (IsConstZero ||
2111 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002112 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002113 SemaRef.Diag(NewStep->getExprLoc(),
2114 diag::err_omp_loop_incr_not_compatible)
2115 << Var << TestIsLessOp << NewStep->getSourceRange();
2116 SemaRef.Diag(ConditionLoc,
2117 diag::note_omp_loop_cond_requres_compatible_incr)
2118 << TestIsLessOp << ConditionSrcRange;
2119 return true;
2120 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002121 if (TestIsLessOp == Subtract) {
2122 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2123 NewStep).get();
2124 Subtract = !Subtract;
2125 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002126 }
2127
2128 Step = NewStep;
2129 SubtractStep = Subtract;
2130 return false;
2131}
2132
Alexey Bataev9c821032015-04-30 04:23:23 +00002133bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002134 // Check init-expr for canonical loop form and save loop counter
2135 // variable - #Var and its initialization value - #LB.
2136 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2137 // var = lb
2138 // integer-type var = lb
2139 // random-access-iterator-type var = lb
2140 // pointer-type var = lb
2141 //
2142 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002143 if (EmitDiags) {
2144 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2145 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002146 return true;
2147 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002148 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002149 if (Expr *E = dyn_cast<Expr>(S))
2150 S = E->IgnoreParens();
2151 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2152 if (BO->getOpcode() == BO_Assign)
2153 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002154 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002155 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002156 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2157 if (DS->isSingleDecl()) {
2158 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2159 if (Var->hasInit()) {
2160 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002161 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002162 SemaRef.Diag(S->getLocStart(),
2163 diag::ext_omp_loop_not_canonical_init)
2164 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002165 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002166 }
2167 }
2168 }
2169 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2170 if (CE->getOperator() == OO_Equal)
2171 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002172 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2173 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002174
Alexey Bataev9c821032015-04-30 04:23:23 +00002175 if (EmitDiags) {
2176 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2177 << S->getSourceRange();
2178 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002179 return true;
2180}
2181
Alexey Bataev23b69422014-06-18 07:08:49 +00002182/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002183/// variable (which may be the loop variable) if possible.
2184static const VarDecl *GetInitVarDecl(const Expr *E) {
2185 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002186 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002187 E = E->IgnoreParenImpCasts();
2188 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2189 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2190 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2191 CE->getArg(0) != nullptr)
2192 E = CE->getArg(0)->IgnoreParenImpCasts();
2193 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2194 if (!DRE)
2195 return nullptr;
2196 return dyn_cast<VarDecl>(DRE->getDecl());
2197}
2198
2199bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2200 // Check test-expr for canonical form, save upper-bound UB, flags for
2201 // less/greater and for strict/non-strict comparison.
2202 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2203 // var relational-op b
2204 // b relational-op var
2205 //
2206 if (!S) {
2207 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2208 return true;
2209 }
2210 S = S->IgnoreParenImpCasts();
2211 SourceLocation CondLoc = S->getLocStart();
2212 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2213 if (BO->isRelationalOp()) {
2214 if (GetInitVarDecl(BO->getLHS()) == Var)
2215 return SetUB(BO->getRHS(),
2216 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2217 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2218 BO->getSourceRange(), BO->getOperatorLoc());
2219 if (GetInitVarDecl(BO->getRHS()) == Var)
2220 return SetUB(BO->getLHS(),
2221 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2222 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2223 BO->getSourceRange(), BO->getOperatorLoc());
2224 }
2225 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2226 if (CE->getNumArgs() == 2) {
2227 auto Op = CE->getOperator();
2228 switch (Op) {
2229 case OO_Greater:
2230 case OO_GreaterEqual:
2231 case OO_Less:
2232 case OO_LessEqual:
2233 if (GetInitVarDecl(CE->getArg(0)) == Var)
2234 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2235 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2236 CE->getOperatorLoc());
2237 if (GetInitVarDecl(CE->getArg(1)) == Var)
2238 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2239 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2240 CE->getOperatorLoc());
2241 break;
2242 default:
2243 break;
2244 }
2245 }
2246 }
2247 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2248 << S->getSourceRange() << Var;
2249 return true;
2250}
2251
2252bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2253 // RHS of canonical loop form increment can be:
2254 // var + incr
2255 // incr + var
2256 // var - incr
2257 //
2258 RHS = RHS->IgnoreParenImpCasts();
2259 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2260 if (BO->isAdditiveOp()) {
2261 bool IsAdd = BO->getOpcode() == BO_Add;
2262 if (GetInitVarDecl(BO->getLHS()) == Var)
2263 return SetStep(BO->getRHS(), !IsAdd);
2264 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2265 return SetStep(BO->getLHS(), false);
2266 }
2267 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2268 bool IsAdd = CE->getOperator() == OO_Plus;
2269 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2270 if (GetInitVarDecl(CE->getArg(0)) == Var)
2271 return SetStep(CE->getArg(1), !IsAdd);
2272 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2273 return SetStep(CE->getArg(0), false);
2274 }
2275 }
2276 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2277 << RHS->getSourceRange() << Var;
2278 return true;
2279}
2280
2281bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2282 // Check incr-expr for canonical loop form and return true if it
2283 // does not conform.
2284 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2285 // ++var
2286 // var++
2287 // --var
2288 // var--
2289 // var += incr
2290 // var -= incr
2291 // var = var + incr
2292 // var = incr + var
2293 // var = var - incr
2294 //
2295 if (!S) {
2296 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2297 return true;
2298 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002299 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002300 S = S->IgnoreParens();
2301 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2302 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2303 return SetStep(
2304 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2305 (UO->isDecrementOp() ? -1 : 1)).get(),
2306 false);
2307 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2308 switch (BO->getOpcode()) {
2309 case BO_AddAssign:
2310 case BO_SubAssign:
2311 if (GetInitVarDecl(BO->getLHS()) == Var)
2312 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2313 break;
2314 case BO_Assign:
2315 if (GetInitVarDecl(BO->getLHS()) == Var)
2316 return CheckIncRHS(BO->getRHS());
2317 break;
2318 default:
2319 break;
2320 }
2321 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2322 switch (CE->getOperator()) {
2323 case OO_PlusPlus:
2324 case OO_MinusMinus:
2325 if (GetInitVarDecl(CE->getArg(0)) == Var)
2326 return SetStep(
2327 SemaRef.ActOnIntegerConstant(
2328 CE->getLocStart(),
2329 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2330 false);
2331 break;
2332 case OO_PlusEqual:
2333 case OO_MinusEqual:
2334 if (GetInitVarDecl(CE->getArg(0)) == Var)
2335 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2336 break;
2337 case OO_Equal:
2338 if (GetInitVarDecl(CE->getArg(0)) == Var)
2339 return CheckIncRHS(CE->getArg(1));
2340 break;
2341 default:
2342 break;
2343 }
2344 }
2345 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2346 << S->getSourceRange() << Var;
2347 return true;
2348}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002349
2350/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002351Expr *
2352OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2353 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002354 ExprResult Diff;
2355 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2356 SemaRef.getLangOpts().CPlusPlus) {
2357 // Upper - Lower
2358 Expr *Upper = TestIsLessOp ? UB : LB;
2359 Expr *Lower = TestIsLessOp ? LB : UB;
2360
2361 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2362
2363 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2364 // BuildBinOp already emitted error, this one is to point user to upper
2365 // and lower bound, and to tell what is passed to 'operator-'.
2366 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2367 << Upper->getSourceRange() << Lower->getSourceRange();
2368 return nullptr;
2369 }
2370 }
2371
2372 if (!Diff.isUsable())
2373 return nullptr;
2374
2375 // Upper - Lower [- 1]
2376 if (TestIsStrictOp)
2377 Diff = SemaRef.BuildBinOp(
2378 S, DefaultLoc, BO_Sub, Diff.get(),
2379 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2380 if (!Diff.isUsable())
2381 return nullptr;
2382
2383 // Upper - Lower [- 1] + Step
2384 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2385 Step->IgnoreImplicit());
2386 if (!Diff.isUsable())
2387 return nullptr;
2388
2389 // Parentheses (for dumping/debugging purposes only).
2390 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2391 if (!Diff.isUsable())
2392 return nullptr;
2393
2394 // (Upper - Lower [- 1] + Step) / Step
2395 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2396 Step->IgnoreImplicit());
2397 if (!Diff.isUsable())
2398 return nullptr;
2399
Alexander Musman174b3ca2014-10-06 11:16:29 +00002400 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2401 if (LimitedType) {
2402 auto &C = SemaRef.Context;
2403 QualType Type = Diff.get()->getType();
2404 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2405 if (NewSize != C.getTypeSize(Type)) {
2406 if (NewSize < C.getTypeSize(Type)) {
2407 assert(NewSize == 64 && "incorrect loop var size");
2408 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2409 << InitSrcRange << ConditionSrcRange;
2410 }
2411 QualType NewType = C.getIntTypeForBitwidth(
2412 NewSize, Type->hasSignedIntegerRepresentation());
2413 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2414 Sema::AA_Converting, true);
2415 if (!Diff.isUsable())
2416 return nullptr;
2417 }
2418 }
2419
Alexander Musmana5f070a2014-10-01 06:03:56 +00002420 return Diff.get();
2421}
2422
Alexey Bataev62dbb972015-04-22 11:59:37 +00002423Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2424 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2425 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2426 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2427 auto CondExpr = SemaRef.BuildBinOp(
2428 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2429 : (TestIsStrictOp ? BO_GT : BO_GE),
2430 LB, UB);
2431 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2432 // Otherwise use original loop conditon and evaluate it in runtime.
2433 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2434}
2435
Alexander Musmana5f070a2014-10-01 06:03:56 +00002436/// \brief Build reference expression to the counter be used for codegen.
2437Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2438 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
Alexey Bataev9c821032015-04-30 04:23:23 +00002439 GetIncrementSrcRange().getBegin(), Var,
2440 /*RefersToEnclosingVariableOrCapture=*/true,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002441 DefaultLoc, Var->getType(), VK_LValue);
2442}
2443
2444/// \brief Build initization of the counter be used for codegen.
2445Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2446
2447/// \brief Build step of the counter be used for codegen.
2448Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2449
2450/// \brief Iteration space of a single for loop.
2451struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002452 /// \brief Condition of the loop.
2453 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002454 /// \brief This expression calculates the number of iterations in the loop.
2455 /// It is always possible to calculate it before starting the loop.
2456 Expr *NumIterations;
2457 /// \brief The loop counter variable.
2458 Expr *CounterVar;
2459 /// \brief This is initializer for the initial value of #CounterVar.
2460 Expr *CounterInit;
2461 /// \brief This is step for the #CounterVar used to generate its update:
2462 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2463 Expr *CounterStep;
2464 /// \brief Should step be subtracted?
2465 bool Subtract;
2466 /// \brief Source range of the loop init.
2467 SourceRange InitSrcRange;
2468 /// \brief Source range of the loop condition.
2469 SourceRange CondSrcRange;
2470 /// \brief Source range of the loop increment.
2471 SourceRange IncSrcRange;
2472};
2473
Alexey Bataev23b69422014-06-18 07:08:49 +00002474} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002475
Alexey Bataev9c821032015-04-30 04:23:23 +00002476void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2477 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2478 assert(Init && "Expected loop in canonical form.");
2479 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2480 if (CollapseIteration > 0 &&
2481 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2482 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2483 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2484 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2485 }
2486 DSAStack->setCollapseNumber(CollapseIteration - 1);
2487 }
2488}
2489
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002490/// \brief Called on a for stmt to check and extract its iteration space
2491/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002492static bool CheckOpenMPIterationSpace(
2493 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2494 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2495 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002496 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2497 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002498 // OpenMP [2.6, Canonical Loop Form]
2499 // for (init-expr; test-expr; incr-expr) structured-block
2500 auto For = dyn_cast_or_null<ForStmt>(S);
2501 if (!For) {
2502 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002503 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2504 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2505 << CurrentNestedLoopCount;
2506 if (NestedLoopCount > 1)
2507 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2508 diag::note_omp_collapse_expr)
2509 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002510 return true;
2511 }
2512 assert(For->getBody());
2513
2514 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2515
2516 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002517 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002518 if (ISC.CheckInit(Init)) {
2519 return true;
2520 }
2521
2522 bool HasErrors = false;
2523
2524 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002525 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002526
2527 // OpenMP [2.6, Canonical Loop Form]
2528 // Var is one of the following:
2529 // A variable of signed or unsigned integer type.
2530 // For C++, a variable of a random access iterator type.
2531 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002532 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002533 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2534 !VarType->isPointerType() &&
2535 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2536 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2537 << SemaRef.getLangOpts().CPlusPlus;
2538 HasErrors = true;
2539 }
2540
Alexey Bataev4acb8592014-07-07 13:01:15 +00002541 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2542 // Construct
2543 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2544 // parallel for construct is (are) private.
2545 // The loop iteration variable in the associated for-loop of a simd construct
2546 // with just one associated for-loop is linear with a constant-linear-step
2547 // that is the increment of the associated for-loop.
2548 // Exclude loop var from the list of variables with implicitly defined data
2549 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002550 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002551
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002552 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2553 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002554 // The loop iteration variable in the associated for-loop of a simd construct
2555 // with just one associated for-loop may be listed in a linear clause with a
2556 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002557 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2558 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002559 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002560 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2561 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2562 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002563 auto PredeterminedCKind =
2564 isOpenMPSimdDirective(DKind)
2565 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2566 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002567 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002568 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002569 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2570 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2571 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002572 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002573 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002574 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2575 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002576 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002577 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002578 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002579 // Make the loop iteration variable private (for worksharing constructs),
2580 // linear (for simd directives with the only one associated loop) or
2581 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002582 if (DVar.CKind == OMPC_unknown)
2583 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2584 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002585 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002586 }
2587
Alexey Bataev7ff55242014-06-19 09:13:45 +00002588 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002589
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002590 // Check test-expr.
2591 HasErrors |= ISC.CheckCond(For->getCond());
2592
2593 // Check incr-expr.
2594 HasErrors |= ISC.CheckInc(For->getInc());
2595
Alexander Musmana5f070a2014-10-01 06:03:56 +00002596 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002597 return HasErrors;
2598
Alexander Musmana5f070a2014-10-01 06:03:56 +00002599 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002600 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002601 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2602 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002603 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2604 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2605 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2606 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2607 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2608 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2609 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2610
Alexey Bataev62dbb972015-04-22 11:59:37 +00002611 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2612 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002613 ResultIterSpace.CounterVar == nullptr ||
2614 ResultIterSpace.CounterInit == nullptr ||
2615 ResultIterSpace.CounterStep == nullptr);
2616
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002617 return HasErrors;
2618}
2619
Alexander Musmana5f070a2014-10-01 06:03:56 +00002620/// \brief Build a variable declaration for OpenMP loop iteration variable.
2621static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2622 StringRef Name) {
2623 DeclContext *DC = SemaRef.CurContext;
2624 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2625 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2626 VarDecl *Decl =
2627 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2628 Decl->setImplicit();
2629 return Decl;
2630}
2631
2632/// \brief Build 'VarRef = Start + Iter * Step'.
2633static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2634 SourceLocation Loc, ExprResult VarRef,
2635 ExprResult Start, ExprResult Iter,
2636 ExprResult Step, bool Subtract) {
2637 // Add parentheses (for debugging purposes only).
2638 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2639 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2640 !Step.isUsable())
2641 return ExprError();
2642
2643 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2644 Step.get()->IgnoreImplicit());
2645 if (!Update.isUsable())
2646 return ExprError();
2647
2648 // Build 'VarRef = Start + Iter * Step'.
2649 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2650 Start.get()->IgnoreImplicit(), Update.get());
2651 if (!Update.isUsable())
2652 return ExprError();
2653
2654 Update = SemaRef.PerformImplicitConversion(
2655 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2656 if (!Update.isUsable())
2657 return ExprError();
2658
2659 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2660 return Update;
2661}
2662
2663/// \brief Convert integer expression \a E to make it have at least \a Bits
2664/// bits.
2665static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2666 Sema &SemaRef) {
2667 if (E == nullptr)
2668 return ExprError();
2669 auto &C = SemaRef.Context;
2670 QualType OldType = E->getType();
2671 unsigned HasBits = C.getTypeSize(OldType);
2672 if (HasBits >= Bits)
2673 return ExprResult(E);
2674 // OK to convert to signed, because new type has more bits than old.
2675 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2676 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2677 true);
2678}
2679
2680/// \brief Check if the given expression \a E is a constant integer that fits
2681/// into \a Bits bits.
2682static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2683 if (E == nullptr)
2684 return false;
2685 llvm::APSInt Result;
2686 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2687 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2688 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002689}
2690
2691/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002692/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2693/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002694static unsigned
2695CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2696 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002697 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002698 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002699 unsigned NestedLoopCount = 1;
2700 if (NestedLoopCountExpr) {
2701 // Found 'collapse' clause - calculate collapse number.
2702 llvm::APSInt Result;
2703 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2704 NestedLoopCount = Result.getLimitedValue();
2705 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002706 // This is helper routine for loop directives (e.g., 'for', 'simd',
2707 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002708 SmallVector<LoopIterationSpace, 4> IterSpaces;
2709 IterSpaces.resize(NestedLoopCount);
2710 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002711 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002712 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002713 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002714 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002715 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002716 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002717 // OpenMP [2.8.1, simd construct, Restrictions]
2718 // All loops associated with the construct must be perfectly nested; that
2719 // is, there must be no intervening code nor any OpenMP directive between
2720 // any two loops.
2721 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002722 }
2723
Alexander Musmana5f070a2014-10-01 06:03:56 +00002724 Built.clear(/* size */ NestedLoopCount);
2725
2726 if (SemaRef.CurContext->isDependentContext())
2727 return NestedLoopCount;
2728
2729 // An example of what is generated for the following code:
2730 //
2731 // #pragma omp simd collapse(2)
2732 // for (i = 0; i < NI; ++i)
2733 // for (j = J0; j < NJ; j+=2) {
2734 // <loop body>
2735 // }
2736 //
2737 // We generate the code below.
2738 // Note: the loop body may be outlined in CodeGen.
2739 // Note: some counters may be C++ classes, operator- is used to find number of
2740 // iterations and operator+= to calculate counter value.
2741 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2742 // or i64 is currently supported).
2743 //
2744 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2745 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2746 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2747 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2748 // // similar updates for vars in clauses (e.g. 'linear')
2749 // <loop body (using local i and j)>
2750 // }
2751 // i = NI; // assign final values of counters
2752 // j = NJ;
2753 //
2754
2755 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2756 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002757 // Precondition tests if there is at least one iteration (all conditions are
2758 // true).
2759 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002760 auto N0 = IterSpaces[0].NumIterations;
2761 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2762 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2763
2764 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2765 return NestedLoopCount;
2766
2767 auto &C = SemaRef.Context;
2768 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2769
2770 Scope *CurScope = DSA.getCurScope();
2771 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002772 if (PreCond.isUsable()) {
2773 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
2774 PreCond.get(), IterSpaces[Cnt].PreCond);
2775 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002776 auto N = IterSpaces[Cnt].NumIterations;
2777 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2778 if (LastIteration32.isUsable())
2779 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2780 LastIteration32.get(), N);
2781 if (LastIteration64.isUsable())
2782 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2783 LastIteration64.get(), N);
2784 }
2785
2786 // Choose either the 32-bit or 64-bit version.
2787 ExprResult LastIteration = LastIteration64;
2788 if (LastIteration32.isUsable() &&
2789 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2790 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2791 FitsInto(
2792 32 /* Bits */,
2793 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2794 LastIteration64.get(), SemaRef)))
2795 LastIteration = LastIteration32;
2796
2797 if (!LastIteration.isUsable())
2798 return 0;
2799
2800 // Save the number of iterations.
2801 ExprResult NumIterations = LastIteration;
2802 {
2803 LastIteration = SemaRef.BuildBinOp(
2804 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2805 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2806 if (!LastIteration.isUsable())
2807 return 0;
2808 }
2809
2810 // Calculate the last iteration number beforehand instead of doing this on
2811 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2812 llvm::APSInt Result;
2813 bool IsConstant =
2814 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2815 ExprResult CalcLastIteration;
2816 if (!IsConstant) {
2817 SourceLocation SaveLoc;
2818 VarDecl *SaveVar =
2819 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2820 ".omp.last.iteration");
2821 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2822 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2823 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2824 SaveRef.get(), LastIteration.get());
2825 LastIteration = SaveRef;
2826
2827 // Prepare SaveRef + 1.
2828 NumIterations = SemaRef.BuildBinOp(
2829 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2830 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2831 if (!NumIterations.isUsable())
2832 return 0;
2833 }
2834
2835 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2836
Alexander Musmanc6388682014-12-15 07:07:06 +00002837 QualType VType = LastIteration.get()->getType();
2838 // Build variables passed into runtime, nesessary for worksharing directives.
2839 ExprResult LB, UB, IL, ST, EUB;
2840 if (isOpenMPWorksharingDirective(DKind)) {
2841 // Lower bound variable, initialized with zero.
2842 VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
2843 LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc);
2844 SemaRef.AddInitializerToDecl(
2845 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2846 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2847
2848 // Upper bound variable, initialized with last iteration number.
2849 VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
2850 UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc);
2851 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
2852 /*DirectInit*/ false,
2853 /*TypeMayContainAuto*/ false);
2854
2855 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
2856 // This will be used to implement clause 'lastprivate'.
2857 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
2858 VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
2859 IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc);
2860 SemaRef.AddInitializerToDecl(
2861 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2862 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2863
2864 // Stride variable returned by runtime (we initialize it to 1 by default).
2865 VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
2866 ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc);
2867 SemaRef.AddInitializerToDecl(
2868 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
2869 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2870
2871 // Build expression: UB = min(UB, LastIteration)
2872 // It is nesessary for CodeGen of directives with static scheduling.
2873 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
2874 UB.get(), LastIteration.get());
2875 ExprResult CondOp = SemaRef.ActOnConditionalOp(
2876 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
2877 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
2878 CondOp.get());
2879 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
2880 }
2881
2882 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002883 ExprResult IV;
2884 ExprResult Init;
2885 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002886 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
2887 IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc);
2888 Expr *RHS = isOpenMPWorksharingDirective(DKind)
2889 ? LB.get()
2890 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
2891 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
2892 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002893 }
2894
Alexander Musmanc6388682014-12-15 07:07:06 +00002895 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002896 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00002897 ExprResult Cond =
2898 isOpenMPWorksharingDirective(DKind)
2899 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
2900 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2901 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002902 // Loop condition with 1 iteration separated (IV < LastIteration)
2903 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2904 IV.get(), LastIteration.get());
2905
2906 // Loop increment (IV = IV + 1)
2907 SourceLocation IncLoc;
2908 ExprResult Inc =
2909 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2910 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2911 if (!Inc.isUsable())
2912 return 0;
2913 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00002914 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
2915 if (!Inc.isUsable())
2916 return 0;
2917
2918 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
2919 // Used for directives with static scheduling.
2920 ExprResult NextLB, NextUB;
2921 if (isOpenMPWorksharingDirective(DKind)) {
2922 // LB + ST
2923 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
2924 if (!NextLB.isUsable())
2925 return 0;
2926 // LB = LB + ST
2927 NextLB =
2928 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
2929 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
2930 if (!NextLB.isUsable())
2931 return 0;
2932 // UB + ST
2933 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
2934 if (!NextUB.isUsable())
2935 return 0;
2936 // UB = UB + ST
2937 NextUB =
2938 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
2939 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
2940 if (!NextUB.isUsable())
2941 return 0;
2942 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002943
2944 // Build updates and final values of the loop counters.
2945 bool HasErrors = false;
2946 Built.Counters.resize(NestedLoopCount);
2947 Built.Updates.resize(NestedLoopCount);
2948 Built.Finals.resize(NestedLoopCount);
2949 {
2950 ExprResult Div;
2951 // Go from inner nested loop to outer.
2952 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2953 LoopIterationSpace &IS = IterSpaces[Cnt];
2954 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2955 // Build: Iter = (IV / Div) % IS.NumIters
2956 // where Div is product of previous iterations' IS.NumIters.
2957 ExprResult Iter;
2958 if (Div.isUsable()) {
2959 Iter =
2960 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2961 } else {
2962 Iter = IV;
2963 assert((Cnt == (int)NestedLoopCount - 1) &&
2964 "unusable div expected on first iteration only");
2965 }
2966
2967 if (Cnt != 0 && Iter.isUsable())
2968 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2969 IS.NumIterations);
2970 if (!Iter.isUsable()) {
2971 HasErrors = true;
2972 break;
2973 }
2974
2975 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2976 ExprResult Update =
2977 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2978 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2979 if (!Update.isUsable()) {
2980 HasErrors = true;
2981 break;
2982 }
2983
2984 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2985 ExprResult Final = BuildCounterUpdate(
2986 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2987 IS.NumIterations, IS.CounterStep, IS.Subtract);
2988 if (!Final.isUsable()) {
2989 HasErrors = true;
2990 break;
2991 }
2992
2993 // Build Div for the next iteration: Div <- Div * IS.NumIters
2994 if (Cnt != 0) {
2995 if (Div.isUnset())
2996 Div = IS.NumIterations;
2997 else
2998 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2999 IS.NumIterations);
3000
3001 // Add parentheses (for debugging purposes only).
3002 if (Div.isUsable())
3003 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3004 if (!Div.isUsable()) {
3005 HasErrors = true;
3006 break;
3007 }
3008 }
3009 if (!Update.isUsable() || !Final.isUsable()) {
3010 HasErrors = true;
3011 break;
3012 }
3013 // Save results
3014 Built.Counters[Cnt] = IS.CounterVar;
3015 Built.Updates[Cnt] = Update.get();
3016 Built.Finals[Cnt] = Final.get();
3017 }
3018 }
3019
3020 if (HasErrors)
3021 return 0;
3022
3023 // Save results
3024 Built.IterationVarRef = IV.get();
3025 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003026 Built.NumIterations = NumIterations.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003027 Built.CalcLastIteration = CalcLastIteration.get();
3028 Built.PreCond = PreCond.get();
3029 Built.Cond = Cond.get();
3030 Built.SeparatedCond = SeparatedCond.get();
3031 Built.Init = Init.get();
3032 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003033 Built.LB = LB.get();
3034 Built.UB = UB.get();
3035 Built.IL = IL.get();
3036 Built.ST = ST.get();
3037 Built.EUB = EUB.get();
3038 Built.NLB = NextLB.get();
3039 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003040
Alexey Bataevabfc0692014-06-25 06:52:00 +00003041 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003042}
3043
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003044static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003045 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003046 return C->getClauseKind() == OMPC_collapse;
3047 };
3048 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003049 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003050 if (I)
3051 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3052 return nullptr;
3053}
3054
Alexey Bataev4acb8592014-07-07 13:01:15 +00003055StmtResult Sema::ActOnOpenMPSimdDirective(
3056 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3057 SourceLocation EndLoc,
3058 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003059 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003060 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003061 unsigned NestedLoopCount =
3062 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003063 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003064 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003065 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003066
Alexander Musmana5f070a2014-10-01 06:03:56 +00003067 assert((CurContext->isDependentContext() || B.builtAll()) &&
3068 "omp simd loop exprs were not built");
3069
Alexander Musman3276a272015-03-21 10:12:56 +00003070 if (!CurContext->isDependentContext()) {
3071 // Finalize the clauses that need pre-built expressions for CodeGen.
3072 for (auto C : Clauses) {
3073 if (auto LC = dyn_cast<OMPLinearClause>(C))
3074 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3075 B.NumIterations, *this, CurScope))
3076 return StmtError();
3077 }
3078 }
3079
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003080 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003081 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3082 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003083}
3084
Alexey Bataev4acb8592014-07-07 13:01:15 +00003085StmtResult Sema::ActOnOpenMPForDirective(
3086 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3087 SourceLocation EndLoc,
3088 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003089 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003090 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003091 unsigned NestedLoopCount =
3092 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003093 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003094 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003095 return StmtError();
3096
Alexander Musmana5f070a2014-10-01 06:03:56 +00003097 assert((CurContext->isDependentContext() || B.builtAll()) &&
3098 "omp for loop exprs were not built");
3099
Alexey Bataevf29276e2014-06-18 04:14:57 +00003100 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003101 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3102 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003103}
3104
Alexander Musmanf82886e2014-09-18 05:12:34 +00003105StmtResult Sema::ActOnOpenMPForSimdDirective(
3106 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3107 SourceLocation EndLoc,
3108 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003109 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003110 // In presence of clause 'collapse', it will define the nested loops number.
3111 unsigned NestedLoopCount =
3112 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003113 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003114 if (NestedLoopCount == 0)
3115 return StmtError();
3116
Alexander Musmanc6388682014-12-15 07:07:06 +00003117 assert((CurContext->isDependentContext() || B.builtAll()) &&
3118 "omp for simd loop exprs were not built");
3119
Alexander Musmanf82886e2014-09-18 05:12:34 +00003120 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003121 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3122 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003123}
3124
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003125StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3126 Stmt *AStmt,
3127 SourceLocation StartLoc,
3128 SourceLocation EndLoc) {
3129 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3130 auto BaseStmt = AStmt;
3131 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3132 BaseStmt = CS->getCapturedStmt();
3133 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3134 auto S = C->children();
3135 if (!S)
3136 return StmtError();
3137 // All associated statements must be '#pragma omp section' except for
3138 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003139 for (++S; S; ++S) {
3140 auto SectionStmt = *S;
3141 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3142 if (SectionStmt)
3143 Diag(SectionStmt->getLocStart(),
3144 diag::err_omp_sections_substmt_not_section);
3145 return StmtError();
3146 }
3147 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003148 } else {
3149 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3150 return StmtError();
3151 }
3152
3153 getCurFunction()->setHasBranchProtectedScope();
3154
3155 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3156 AStmt);
3157}
3158
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003159StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3160 SourceLocation StartLoc,
3161 SourceLocation EndLoc) {
3162 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3163
3164 getCurFunction()->setHasBranchProtectedScope();
3165
3166 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3167}
3168
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003169StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3170 Stmt *AStmt,
3171 SourceLocation StartLoc,
3172 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003173 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3174
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003175 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003176
Alexey Bataev3255bf32015-01-19 05:20:46 +00003177 // OpenMP [2.7.3, single Construct, Restrictions]
3178 // The copyprivate clause must not be used with the nowait clause.
3179 OMPClause *Nowait = nullptr;
3180 OMPClause *Copyprivate = nullptr;
3181 for (auto *Clause : Clauses) {
3182 if (Clause->getClauseKind() == OMPC_nowait)
3183 Nowait = Clause;
3184 else if (Clause->getClauseKind() == OMPC_copyprivate)
3185 Copyprivate = Clause;
3186 if (Copyprivate && Nowait) {
3187 Diag(Copyprivate->getLocStart(),
3188 diag::err_omp_single_copyprivate_with_nowait);
3189 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3190 return StmtError();
3191 }
3192 }
3193
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003194 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3195}
3196
Alexander Musman80c22892014-07-17 08:54:58 +00003197StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3198 SourceLocation StartLoc,
3199 SourceLocation EndLoc) {
3200 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3201
3202 getCurFunction()->setHasBranchProtectedScope();
3203
3204 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3205}
3206
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003207StmtResult
3208Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3209 Stmt *AStmt, SourceLocation StartLoc,
3210 SourceLocation EndLoc) {
3211 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3212
3213 getCurFunction()->setHasBranchProtectedScope();
3214
3215 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3216 AStmt);
3217}
3218
Alexey Bataev4acb8592014-07-07 13:01:15 +00003219StmtResult Sema::ActOnOpenMPParallelForDirective(
3220 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3221 SourceLocation EndLoc,
3222 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3223 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3224 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3225 // 1.2.2 OpenMP Language Terminology
3226 // Structured block - An executable statement with a single entry at the
3227 // top and a single exit at the bottom.
3228 // The point of exit cannot be a branch out of the structured block.
3229 // longjmp() and throw() must not violate the entry/exit criteria.
3230 CS->getCapturedDecl()->setNothrow();
3231
Alexander Musmanc6388682014-12-15 07:07:06 +00003232 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003233 // In presence of clause 'collapse', it will define the nested loops number.
3234 unsigned NestedLoopCount =
3235 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003236 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003237 if (NestedLoopCount == 0)
3238 return StmtError();
3239
Alexander Musmana5f070a2014-10-01 06:03:56 +00003240 assert((CurContext->isDependentContext() || B.builtAll()) &&
3241 "omp parallel for loop exprs were not built");
3242
Alexey Bataev4acb8592014-07-07 13:01:15 +00003243 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003244 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3245 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003246}
3247
Alexander Musmane4e893b2014-09-23 09:33:00 +00003248StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3249 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3250 SourceLocation EndLoc,
3251 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3252 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3253 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3254 // 1.2.2 OpenMP Language Terminology
3255 // Structured block - An executable statement with a single entry at the
3256 // top and a single exit at the bottom.
3257 // The point of exit cannot be a branch out of the structured block.
3258 // longjmp() and throw() must not violate the entry/exit criteria.
3259 CS->getCapturedDecl()->setNothrow();
3260
Alexander Musmanc6388682014-12-15 07:07:06 +00003261 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003262 // In presence of clause 'collapse', it will define the nested loops number.
3263 unsigned NestedLoopCount =
3264 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003265 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003266 if (NestedLoopCount == 0)
3267 return StmtError();
3268
3269 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003270 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003271 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003272}
3273
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003274StmtResult
3275Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3276 Stmt *AStmt, SourceLocation StartLoc,
3277 SourceLocation EndLoc) {
3278 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3279 auto BaseStmt = AStmt;
3280 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3281 BaseStmt = CS->getCapturedStmt();
3282 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3283 auto S = C->children();
3284 if (!S)
3285 return StmtError();
3286 // All associated statements must be '#pragma omp section' except for
3287 // the first one.
3288 for (++S; S; ++S) {
3289 auto SectionStmt = *S;
3290 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3291 if (SectionStmt)
3292 Diag(SectionStmt->getLocStart(),
3293 diag::err_omp_parallel_sections_substmt_not_section);
3294 return StmtError();
3295 }
3296 }
3297 } else {
3298 Diag(AStmt->getLocStart(),
3299 diag::err_omp_parallel_sections_not_compound_stmt);
3300 return StmtError();
3301 }
3302
3303 getCurFunction()->setHasBranchProtectedScope();
3304
3305 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3306 Clauses, AStmt);
3307}
3308
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003309StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3310 Stmt *AStmt, SourceLocation StartLoc,
3311 SourceLocation EndLoc) {
3312 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3313 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3314 // 1.2.2 OpenMP Language Terminology
3315 // Structured block - An executable statement with a single entry at the
3316 // top and a single exit at the bottom.
3317 // The point of exit cannot be a branch out of the structured block.
3318 // longjmp() and throw() must not violate the entry/exit criteria.
3319 CS->getCapturedDecl()->setNothrow();
3320
3321 getCurFunction()->setHasBranchProtectedScope();
3322
3323 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3324}
3325
Alexey Bataev68446b72014-07-18 07:47:19 +00003326StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3327 SourceLocation EndLoc) {
3328 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3329}
3330
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003331StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3332 SourceLocation EndLoc) {
3333 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3334}
3335
Alexey Bataev2df347a2014-07-18 10:17:07 +00003336StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3337 SourceLocation EndLoc) {
3338 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3339}
3340
Alexey Bataev6125da92014-07-21 11:26:11 +00003341StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3342 SourceLocation StartLoc,
3343 SourceLocation EndLoc) {
3344 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3345 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3346}
3347
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003348StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3349 SourceLocation StartLoc,
3350 SourceLocation EndLoc) {
3351 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3352
3353 getCurFunction()->setHasBranchProtectedScope();
3354
3355 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3356}
3357
Alexey Bataev1d160b12015-03-13 12:27:31 +00003358namespace {
3359/// \brief Helper class for checking expression in 'omp atomic [update]'
3360/// construct.
3361class OpenMPAtomicUpdateChecker {
3362 /// \brief Error results for atomic update expressions.
3363 enum ExprAnalysisErrorCode {
3364 /// \brief A statement is not an expression statement.
3365 NotAnExpression,
3366 /// \brief Expression is not builtin binary or unary operation.
3367 NotABinaryOrUnaryExpression,
3368 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3369 NotAnUnaryIncDecExpression,
3370 /// \brief An expression is not of scalar type.
3371 NotAScalarType,
3372 /// \brief A binary operation is not an assignment operation.
3373 NotAnAssignmentOp,
3374 /// \brief RHS part of the binary operation is not a binary expression.
3375 NotABinaryExpression,
3376 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3377 /// expression.
3378 NotABinaryOperator,
3379 /// \brief RHS binary operation does not have reference to the updated LHS
3380 /// part.
3381 NotAnUpdateExpression,
3382 /// \brief No errors is found.
3383 NoError
3384 };
3385 /// \brief Reference to Sema.
3386 Sema &SemaRef;
3387 /// \brief A location for note diagnostics (when error is found).
3388 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003389 /// \brief 'x' lvalue part of the source atomic expression.
3390 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003391 /// \brief 'expr' rvalue part of the source atomic expression.
3392 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003393 /// \brief Helper expression of the form
3394 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3395 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3396 Expr *UpdateExpr;
3397 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3398 /// important for non-associative operations.
3399 bool IsXLHSInRHSPart;
3400 BinaryOperatorKind Op;
3401 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003402 /// \brief true if the source expression is a postfix unary operation, false
3403 /// if it is a prefix unary operation.
3404 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003405
3406public:
3407 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003408 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003409 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003410 /// \brief Check specified statement that it is suitable for 'atomic update'
3411 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003412 /// expression. If DiagId and NoteId == 0, then only check is performed
3413 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003414 /// \param DiagId Diagnostic which should be emitted if error is found.
3415 /// \param NoteId Diagnostic note for the main error message.
3416 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003417 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003418 /// \brief Return the 'x' lvalue part of the source atomic expression.
3419 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003420 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3421 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003422 /// \brief Return the update expression used in calculation of the updated
3423 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3424 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3425 Expr *getUpdateExpr() const { return UpdateExpr; }
3426 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3427 /// false otherwise.
3428 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3429
Alexey Bataevb78ca832015-04-01 03:33:17 +00003430 /// \brief true if the source expression is a postfix unary operation, false
3431 /// if it is a prefix unary operation.
3432 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3433
Alexey Bataev1d160b12015-03-13 12:27:31 +00003434private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003435 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3436 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003437};
3438} // namespace
3439
3440bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3441 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3442 ExprAnalysisErrorCode ErrorFound = NoError;
3443 SourceLocation ErrorLoc, NoteLoc;
3444 SourceRange ErrorRange, NoteRange;
3445 // Allowed constructs are:
3446 // x = x binop expr;
3447 // x = expr binop x;
3448 if (AtomicBinOp->getOpcode() == BO_Assign) {
3449 X = AtomicBinOp->getLHS();
3450 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3451 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3452 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3453 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3454 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003455 Op = AtomicInnerBinOp->getOpcode();
3456 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003457 auto *LHS = AtomicInnerBinOp->getLHS();
3458 auto *RHS = AtomicInnerBinOp->getRHS();
3459 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3460 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3461 /*Canonical=*/true);
3462 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3463 /*Canonical=*/true);
3464 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3465 /*Canonical=*/true);
3466 if (XId == LHSId) {
3467 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003468 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003469 } else if (XId == RHSId) {
3470 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003471 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003472 } else {
3473 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3474 ErrorRange = AtomicInnerBinOp->getSourceRange();
3475 NoteLoc = X->getExprLoc();
3476 NoteRange = X->getSourceRange();
3477 ErrorFound = NotAnUpdateExpression;
3478 }
3479 } else {
3480 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3481 ErrorRange = AtomicInnerBinOp->getSourceRange();
3482 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3483 NoteRange = SourceRange(NoteLoc, NoteLoc);
3484 ErrorFound = NotABinaryOperator;
3485 }
3486 } else {
3487 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3488 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3489 ErrorFound = NotABinaryExpression;
3490 }
3491 } else {
3492 ErrorLoc = AtomicBinOp->getExprLoc();
3493 ErrorRange = AtomicBinOp->getSourceRange();
3494 NoteLoc = AtomicBinOp->getOperatorLoc();
3495 NoteRange = SourceRange(NoteLoc, NoteLoc);
3496 ErrorFound = NotAnAssignmentOp;
3497 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003498 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003499 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3500 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3501 return true;
3502 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003503 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003504 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003505}
3506
3507bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3508 unsigned NoteId) {
3509 ExprAnalysisErrorCode ErrorFound = NoError;
3510 SourceLocation ErrorLoc, NoteLoc;
3511 SourceRange ErrorRange, NoteRange;
3512 // Allowed constructs are:
3513 // x++;
3514 // x--;
3515 // ++x;
3516 // --x;
3517 // x binop= expr;
3518 // x = x binop expr;
3519 // x = expr binop x;
3520 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3521 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3522 if (AtomicBody->getType()->isScalarType() ||
3523 AtomicBody->isInstantiationDependent()) {
3524 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3525 AtomicBody->IgnoreParenImpCasts())) {
3526 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003527 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003528 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003529 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003530 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003531 X = AtomicCompAssignOp->getLHS();
3532 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003533 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3534 AtomicBody->IgnoreParenImpCasts())) {
3535 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003536 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3537 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003538 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003539 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3540 // Check for Unary Operation
3541 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003542 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003543 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3544 OpLoc = AtomicUnaryOp->getOperatorLoc();
3545 X = AtomicUnaryOp->getSubExpr();
3546 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3547 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003548 } else {
3549 ErrorFound = NotAnUnaryIncDecExpression;
3550 ErrorLoc = AtomicUnaryOp->getExprLoc();
3551 ErrorRange = AtomicUnaryOp->getSourceRange();
3552 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3553 NoteRange = SourceRange(NoteLoc, NoteLoc);
3554 }
3555 } else {
3556 ErrorFound = NotABinaryOrUnaryExpression;
3557 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3558 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3559 }
3560 } else {
3561 ErrorFound = NotAScalarType;
3562 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3563 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3564 }
3565 } else {
3566 ErrorFound = NotAnExpression;
3567 NoteLoc = ErrorLoc = S->getLocStart();
3568 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3569 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003570 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003571 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3572 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3573 return true;
3574 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003575 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003576 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003577 // Build an update expression of form 'OpaqueValueExpr(x) binop
3578 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3579 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3580 auto *OVEX = new (SemaRef.getASTContext())
3581 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3582 auto *OVEExpr = new (SemaRef.getASTContext())
3583 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3584 auto Update =
3585 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3586 IsXLHSInRHSPart ? OVEExpr : OVEX);
3587 if (Update.isInvalid())
3588 return true;
3589 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3590 Sema::AA_Casting);
3591 if (Update.isInvalid())
3592 return true;
3593 UpdateExpr = Update.get();
3594 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003595 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003596}
3597
Alexey Bataev0162e452014-07-22 10:10:35 +00003598StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3599 Stmt *AStmt,
3600 SourceLocation StartLoc,
3601 SourceLocation EndLoc) {
3602 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003603 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003604 // 1.2.2 OpenMP Language Terminology
3605 // Structured block - An executable statement with a single entry at the
3606 // top and a single exit at the bottom.
3607 // The point of exit cannot be a branch out of the structured block.
3608 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003609 OpenMPClauseKind AtomicKind = OMPC_unknown;
3610 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003611 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003612 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003613 C->getClauseKind() == OMPC_update ||
3614 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003615 if (AtomicKind != OMPC_unknown) {
3616 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3617 << SourceRange(C->getLocStart(), C->getLocEnd());
3618 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3619 << getOpenMPClauseName(AtomicKind);
3620 } else {
3621 AtomicKind = C->getClauseKind();
3622 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003623 }
3624 }
3625 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003626
Alexey Bataev459dec02014-07-24 06:46:57 +00003627 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003628 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3629 Body = EWC->getSubExpr();
3630
Alexey Bataev62cec442014-11-18 10:14:22 +00003631 Expr *X = nullptr;
3632 Expr *V = nullptr;
3633 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003634 Expr *UE = nullptr;
3635 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003636 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003637 // OpenMP [2.12.6, atomic Construct]
3638 // In the next expressions:
3639 // * x and v (as applicable) are both l-value expressions with scalar type.
3640 // * During the execution of an atomic region, multiple syntactic
3641 // occurrences of x must designate the same storage location.
3642 // * Neither of v and expr (as applicable) may access the storage location
3643 // designated by x.
3644 // * Neither of x and expr (as applicable) may access the storage location
3645 // designated by v.
3646 // * expr is an expression with scalar type.
3647 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3648 // * binop, binop=, ++, and -- are not overloaded operators.
3649 // * The expression x binop expr must be numerically equivalent to x binop
3650 // (expr). This requirement is satisfied if the operators in expr have
3651 // precedence greater than binop, or by using parentheses around expr or
3652 // subexpressions of expr.
3653 // * The expression expr binop x must be numerically equivalent to (expr)
3654 // binop x. This requirement is satisfied if the operators in expr have
3655 // precedence equal to or greater than binop, or by using parentheses around
3656 // expr or subexpressions of expr.
3657 // * For forms that allow multiple occurrences of x, the number of times
3658 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003659 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003660 enum {
3661 NotAnExpression,
3662 NotAnAssignmentOp,
3663 NotAScalarType,
3664 NotAnLValue,
3665 NoError
3666 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003667 SourceLocation ErrorLoc, NoteLoc;
3668 SourceRange ErrorRange, NoteRange;
3669 // If clause is read:
3670 // v = x;
3671 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3672 auto AtomicBinOp =
3673 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3674 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3675 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3676 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3677 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3678 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3679 if (!X->isLValue() || !V->isLValue()) {
3680 auto NotLValueExpr = X->isLValue() ? V : X;
3681 ErrorFound = NotAnLValue;
3682 ErrorLoc = AtomicBinOp->getExprLoc();
3683 ErrorRange = AtomicBinOp->getSourceRange();
3684 NoteLoc = NotLValueExpr->getExprLoc();
3685 NoteRange = NotLValueExpr->getSourceRange();
3686 }
3687 } else if (!X->isInstantiationDependent() ||
3688 !V->isInstantiationDependent()) {
3689 auto NotScalarExpr =
3690 (X->isInstantiationDependent() || X->getType()->isScalarType())
3691 ? V
3692 : X;
3693 ErrorFound = NotAScalarType;
3694 ErrorLoc = AtomicBinOp->getExprLoc();
3695 ErrorRange = AtomicBinOp->getSourceRange();
3696 NoteLoc = NotScalarExpr->getExprLoc();
3697 NoteRange = NotScalarExpr->getSourceRange();
3698 }
3699 } else {
3700 ErrorFound = NotAnAssignmentOp;
3701 ErrorLoc = AtomicBody->getExprLoc();
3702 ErrorRange = AtomicBody->getSourceRange();
3703 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3704 : AtomicBody->getExprLoc();
3705 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3706 : AtomicBody->getSourceRange();
3707 }
3708 } else {
3709 ErrorFound = NotAnExpression;
3710 NoteLoc = ErrorLoc = Body->getLocStart();
3711 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003712 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003713 if (ErrorFound != NoError) {
3714 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3715 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003716 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3717 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003718 return StmtError();
3719 } else if (CurContext->isDependentContext())
3720 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003721 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003722 enum {
3723 NotAnExpression,
3724 NotAnAssignmentOp,
3725 NotAScalarType,
3726 NotAnLValue,
3727 NoError
3728 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003729 SourceLocation ErrorLoc, NoteLoc;
3730 SourceRange ErrorRange, NoteRange;
3731 // If clause is write:
3732 // x = expr;
3733 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3734 auto AtomicBinOp =
3735 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3736 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00003737 X = AtomicBinOp->getLHS();
3738 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00003739 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3740 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3741 if (!X->isLValue()) {
3742 ErrorFound = NotAnLValue;
3743 ErrorLoc = AtomicBinOp->getExprLoc();
3744 ErrorRange = AtomicBinOp->getSourceRange();
3745 NoteLoc = X->getExprLoc();
3746 NoteRange = X->getSourceRange();
3747 }
3748 } else if (!X->isInstantiationDependent() ||
3749 !E->isInstantiationDependent()) {
3750 auto NotScalarExpr =
3751 (X->isInstantiationDependent() || X->getType()->isScalarType())
3752 ? E
3753 : X;
3754 ErrorFound = NotAScalarType;
3755 ErrorLoc = AtomicBinOp->getExprLoc();
3756 ErrorRange = AtomicBinOp->getSourceRange();
3757 NoteLoc = NotScalarExpr->getExprLoc();
3758 NoteRange = NotScalarExpr->getSourceRange();
3759 }
3760 } else {
3761 ErrorFound = NotAnAssignmentOp;
3762 ErrorLoc = AtomicBody->getExprLoc();
3763 ErrorRange = AtomicBody->getSourceRange();
3764 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3765 : AtomicBody->getExprLoc();
3766 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3767 : AtomicBody->getSourceRange();
3768 }
3769 } else {
3770 ErrorFound = NotAnExpression;
3771 NoteLoc = ErrorLoc = Body->getLocStart();
3772 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003773 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00003774 if (ErrorFound != NoError) {
3775 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3776 << ErrorRange;
3777 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3778 << NoteRange;
3779 return StmtError();
3780 } else if (CurContext->isDependentContext())
3781 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003782 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003783 // If clause is update:
3784 // x++;
3785 // x--;
3786 // ++x;
3787 // --x;
3788 // x binop= expr;
3789 // x = x binop expr;
3790 // x = expr binop x;
3791 OpenMPAtomicUpdateChecker Checker(*this);
3792 if (Checker.checkStatement(
3793 Body, (AtomicKind == OMPC_update)
3794 ? diag::err_omp_atomic_update_not_expression_statement
3795 : diag::err_omp_atomic_not_expression_statement,
3796 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00003797 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003798 if (!CurContext->isDependentContext()) {
3799 E = Checker.getExpr();
3800 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003801 UE = Checker.getUpdateExpr();
3802 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00003803 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003804 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003805 enum {
3806 NotAnAssignmentOp,
3807 NotACompoundStatement,
3808 NotTwoSubstatements,
3809 NotASpecificExpression,
3810 NoError
3811 } ErrorFound = NoError;
3812 SourceLocation ErrorLoc, NoteLoc;
3813 SourceRange ErrorRange, NoteRange;
3814 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
3815 // If clause is a capture:
3816 // v = x++;
3817 // v = x--;
3818 // v = ++x;
3819 // v = --x;
3820 // v = x binop= expr;
3821 // v = x = x binop expr;
3822 // v = x = expr binop x;
3823 auto *AtomicBinOp =
3824 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3825 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3826 V = AtomicBinOp->getLHS();
3827 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3828 OpenMPAtomicUpdateChecker Checker(*this);
3829 if (Checker.checkStatement(
3830 Body, diag::err_omp_atomic_capture_not_expression_statement,
3831 diag::note_omp_atomic_update))
3832 return StmtError();
3833 E = Checker.getExpr();
3834 X = Checker.getX();
3835 UE = Checker.getUpdateExpr();
3836 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
3837 IsPostfixUpdate = Checker.isPostfixUpdate();
3838 } else {
3839 ErrorLoc = AtomicBody->getExprLoc();
3840 ErrorRange = AtomicBody->getSourceRange();
3841 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3842 : AtomicBody->getExprLoc();
3843 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3844 : AtomicBody->getSourceRange();
3845 ErrorFound = NotAnAssignmentOp;
3846 }
3847 if (ErrorFound != NoError) {
3848 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
3849 << ErrorRange;
3850 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
3851 return StmtError();
3852 } else if (CurContext->isDependentContext()) {
3853 UE = V = E = X = nullptr;
3854 }
3855 } else {
3856 // If clause is a capture:
3857 // { v = x; x = expr; }
3858 // { v = x; x++; }
3859 // { v = x; x--; }
3860 // { v = x; ++x; }
3861 // { v = x; --x; }
3862 // { v = x; x binop= expr; }
3863 // { v = x; x = x binop expr; }
3864 // { v = x; x = expr binop x; }
3865 // { x++; v = x; }
3866 // { x--; v = x; }
3867 // { ++x; v = x; }
3868 // { --x; v = x; }
3869 // { x binop= expr; v = x; }
3870 // { x = x binop expr; v = x; }
3871 // { x = expr binop x; v = x; }
3872 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
3873 // Check that this is { expr1; expr2; }
3874 if (CS->size() == 2) {
3875 auto *First = CS->body_front();
3876 auto *Second = CS->body_back();
3877 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
3878 First = EWC->getSubExpr()->IgnoreParenImpCasts();
3879 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
3880 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
3881 // Need to find what subexpression is 'v' and what is 'x'.
3882 OpenMPAtomicUpdateChecker Checker(*this);
3883 bool IsUpdateExprFound = !Checker.checkStatement(Second);
3884 BinaryOperator *BinOp = nullptr;
3885 if (IsUpdateExprFound) {
3886 BinOp = dyn_cast<BinaryOperator>(First);
3887 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
3888 }
3889 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
3890 // { v = x; x++; }
3891 // { v = x; x--; }
3892 // { v = x; ++x; }
3893 // { v = x; --x; }
3894 // { v = x; x binop= expr; }
3895 // { v = x; x = x binop expr; }
3896 // { v = x; x = expr binop x; }
3897 // Check that the first expression has form v = x.
3898 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
3899 llvm::FoldingSetNodeID XId, PossibleXId;
3900 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
3901 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
3902 IsUpdateExprFound = XId == PossibleXId;
3903 if (IsUpdateExprFound) {
3904 V = BinOp->getLHS();
3905 X = Checker.getX();
3906 E = Checker.getExpr();
3907 UE = Checker.getUpdateExpr();
3908 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00003909 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003910 }
3911 }
3912 if (!IsUpdateExprFound) {
3913 IsUpdateExprFound = !Checker.checkStatement(First);
3914 BinOp = nullptr;
3915 if (IsUpdateExprFound) {
3916 BinOp = dyn_cast<BinaryOperator>(Second);
3917 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
3918 }
3919 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
3920 // { x++; v = x; }
3921 // { x--; v = x; }
3922 // { ++x; v = x; }
3923 // { --x; v = x; }
3924 // { x binop= expr; v = x; }
3925 // { x = x binop expr; v = x; }
3926 // { x = expr binop x; v = x; }
3927 // Check that the second expression has form v = x.
3928 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
3929 llvm::FoldingSetNodeID XId, PossibleXId;
3930 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
3931 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
3932 IsUpdateExprFound = XId == PossibleXId;
3933 if (IsUpdateExprFound) {
3934 V = BinOp->getLHS();
3935 X = Checker.getX();
3936 E = Checker.getExpr();
3937 UE = Checker.getUpdateExpr();
3938 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00003939 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003940 }
3941 }
3942 }
3943 if (!IsUpdateExprFound) {
3944 // { v = x; x = expr; }
3945 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
3946 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
3947 ErrorFound = NotAnAssignmentOp;
3948 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
3949 : First->getLocStart();
3950 NoteRange = ErrorRange = FirstBinOp
3951 ? FirstBinOp->getSourceRange()
3952 : SourceRange(ErrorLoc, ErrorLoc);
3953 } else {
3954 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
3955 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
3956 ErrorFound = NotAnAssignmentOp;
3957 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
3958 : Second->getLocStart();
3959 NoteRange = ErrorRange = SecondBinOp
3960 ? SecondBinOp->getSourceRange()
3961 : SourceRange(ErrorLoc, ErrorLoc);
3962 } else {
3963 auto *PossibleXRHSInFirst =
3964 FirstBinOp->getRHS()->IgnoreParenImpCasts();
3965 auto *PossibleXLHSInSecond =
3966 SecondBinOp->getLHS()->IgnoreParenImpCasts();
3967 llvm::FoldingSetNodeID X1Id, X2Id;
3968 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
3969 PossibleXLHSInSecond->Profile(X2Id, Context,
3970 /*Canonical=*/true);
3971 IsUpdateExprFound = X1Id == X2Id;
3972 if (IsUpdateExprFound) {
3973 V = FirstBinOp->getLHS();
3974 X = SecondBinOp->getLHS();
3975 E = SecondBinOp->getRHS();
3976 UE = nullptr;
3977 IsXLHSInRHSPart = false;
3978 IsPostfixUpdate = true;
3979 } else {
3980 ErrorFound = NotASpecificExpression;
3981 ErrorLoc = FirstBinOp->getExprLoc();
3982 ErrorRange = FirstBinOp->getSourceRange();
3983 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
3984 NoteRange = SecondBinOp->getRHS()->getSourceRange();
3985 }
3986 }
3987 }
3988 }
3989 } else {
3990 NoteLoc = ErrorLoc = Body->getLocStart();
3991 NoteRange = ErrorRange =
3992 SourceRange(Body->getLocStart(), Body->getLocStart());
3993 ErrorFound = NotTwoSubstatements;
3994 }
3995 } else {
3996 NoteLoc = ErrorLoc = Body->getLocStart();
3997 NoteRange = ErrorRange =
3998 SourceRange(Body->getLocStart(), Body->getLocStart());
3999 ErrorFound = NotACompoundStatement;
4000 }
4001 if (ErrorFound != NoError) {
4002 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4003 << ErrorRange;
4004 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4005 return StmtError();
4006 } else if (CurContext->isDependentContext()) {
4007 UE = V = E = X = nullptr;
4008 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004009 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004010 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004011
4012 getCurFunction()->setHasBranchProtectedScope();
4013
Alexey Bataev62cec442014-11-18 10:14:22 +00004014 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004015 X, V, E, UE, IsXLHSInRHSPart,
4016 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004017}
4018
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004019StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4020 Stmt *AStmt,
4021 SourceLocation StartLoc,
4022 SourceLocation EndLoc) {
4023 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4024
Alexey Bataev13314bf2014-10-09 04:18:56 +00004025 // OpenMP [2.16, Nesting of Regions]
4026 // If specified, a teams construct must be contained within a target
4027 // construct. That target construct must contain no statements or directives
4028 // outside of the teams construct.
4029 if (DSAStack->hasInnerTeamsRegion()) {
4030 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4031 bool OMPTeamsFound = true;
4032 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4033 auto I = CS->body_begin();
4034 while (I != CS->body_end()) {
4035 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4036 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4037 OMPTeamsFound = false;
4038 break;
4039 }
4040 ++I;
4041 }
4042 assert(I != CS->body_end() && "Not found statement");
4043 S = *I;
4044 }
4045 if (!OMPTeamsFound) {
4046 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4047 Diag(DSAStack->getInnerTeamsRegionLoc(),
4048 diag::note_omp_nested_teams_construct_here);
4049 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4050 << isa<OMPExecutableDirective>(S);
4051 return StmtError();
4052 }
4053 }
4054
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004055 getCurFunction()->setHasBranchProtectedScope();
4056
4057 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4058}
4059
Alexey Bataev13314bf2014-10-09 04:18:56 +00004060StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4061 Stmt *AStmt, SourceLocation StartLoc,
4062 SourceLocation EndLoc) {
4063 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4064 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4065 // 1.2.2 OpenMP Language Terminology
4066 // Structured block - An executable statement with a single entry at the
4067 // top and a single exit at the bottom.
4068 // The point of exit cannot be a branch out of the structured block.
4069 // longjmp() and throw() must not violate the entry/exit criteria.
4070 CS->getCapturedDecl()->setNothrow();
4071
4072 getCurFunction()->setHasBranchProtectedScope();
4073
4074 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4075}
4076
Alexey Bataeved09d242014-05-28 05:53:51 +00004077OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004078 SourceLocation StartLoc,
4079 SourceLocation LParenLoc,
4080 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004081 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004082 switch (Kind) {
4083 case OMPC_if:
4084 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4085 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004086 case OMPC_final:
4087 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4088 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004089 case OMPC_num_threads:
4090 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4091 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004092 case OMPC_safelen:
4093 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4094 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004095 case OMPC_collapse:
4096 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4097 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004098 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004099 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004100 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004101 case OMPC_private:
4102 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004103 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004104 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004105 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004106 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004107 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004108 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004109 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004110 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004111 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004112 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004113 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004114 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004115 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004116 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004117 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004118 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004119 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004120 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004121 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004122 llvm_unreachable("Clause is not allowed.");
4123 }
4124 return Res;
4125}
4126
Alexey Bataeved09d242014-05-28 05:53:51 +00004127OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004128 SourceLocation LParenLoc,
4129 SourceLocation EndLoc) {
4130 Expr *ValExpr = Condition;
4131 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4132 !Condition->isInstantiationDependent() &&
4133 !Condition->containsUnexpandedParameterPack()) {
4134 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004135 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004136 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004137 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004138
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004139 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004140 }
4141
4142 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4143}
4144
Alexey Bataev3778b602014-07-17 07:32:53 +00004145OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4146 SourceLocation StartLoc,
4147 SourceLocation LParenLoc,
4148 SourceLocation EndLoc) {
4149 Expr *ValExpr = Condition;
4150 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4151 !Condition->isInstantiationDependent() &&
4152 !Condition->containsUnexpandedParameterPack()) {
4153 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4154 Condition->getExprLoc(), Condition);
4155 if (Val.isInvalid())
4156 return nullptr;
4157
4158 ValExpr = Val.get();
4159 }
4160
4161 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4162}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004163ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4164 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004165 if (!Op)
4166 return ExprError();
4167
4168 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4169 public:
4170 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004171 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004172 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4173 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004174 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4175 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004176 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4177 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004178 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4179 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004180 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4181 QualType T,
4182 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004183 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4184 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004185 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4186 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004187 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004188 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004189 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004190 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4191 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004192 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4193 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004194 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4195 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004196 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004197 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004198 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004199 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4200 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004201 llvm_unreachable("conversion functions are permitted");
4202 }
4203 } ConvertDiagnoser;
4204 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4205}
4206
4207OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4208 SourceLocation StartLoc,
4209 SourceLocation LParenLoc,
4210 SourceLocation EndLoc) {
4211 Expr *ValExpr = NumThreads;
4212 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004213 !NumThreads->containsUnexpandedParameterPack()) {
4214 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4215 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004217 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004218 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004219
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004220 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004221
4222 // OpenMP [2.5, Restrictions]
4223 // The num_threads expression must evaluate to a positive integer value.
4224 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004225 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4226 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004227 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4228 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004229 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004230 }
4231 }
4232
Alexey Bataeved09d242014-05-28 05:53:51 +00004233 return new (Context)
4234 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004235}
4236
Alexey Bataev62c87d22014-03-21 04:51:18 +00004237ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4238 OpenMPClauseKind CKind) {
4239 if (!E)
4240 return ExprError();
4241 if (E->isValueDependent() || E->isTypeDependent() ||
4242 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004243 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004244 llvm::APSInt Result;
4245 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4246 if (ICE.isInvalid())
4247 return ExprError();
4248 if (!Result.isStrictlyPositive()) {
4249 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4250 << getOpenMPClauseName(CKind) << E->getSourceRange();
4251 return ExprError();
4252 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004253 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4254 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4255 << E->getSourceRange();
4256 return ExprError();
4257 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004258 if (CKind == OMPC_collapse) {
4259 DSAStack->setCollapseNumber(Result.getExtValue());
4260 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004261 return ICE;
4262}
4263
4264OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4265 SourceLocation LParenLoc,
4266 SourceLocation EndLoc) {
4267 // OpenMP [2.8.1, simd construct, Description]
4268 // The parameter of the safelen clause must be a constant
4269 // positive integer expression.
4270 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4271 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004272 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004273 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004274 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004275}
4276
Alexander Musman64d33f12014-06-04 07:53:32 +00004277OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4278 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004279 SourceLocation LParenLoc,
4280 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004281 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004282 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004283 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004284 // The parameter of the collapse clause must be a constant
4285 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004286 ExprResult NumForLoopsResult =
4287 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4288 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004289 return nullptr;
4290 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004291 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004292}
4293
Alexey Bataeved09d242014-05-28 05:53:51 +00004294OMPClause *Sema::ActOnOpenMPSimpleClause(
4295 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4296 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004297 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004298 switch (Kind) {
4299 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004300 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004301 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4302 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004303 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004304 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004305 Res = ActOnOpenMPProcBindClause(
4306 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4307 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004308 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004309 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004310 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004311 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004312 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004313 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004314 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004315 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004316 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004317 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004318 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004319 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004320 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004321 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004322 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004323 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004324 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004325 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004326 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004327 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004328 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004329 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004330 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004331 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004332 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004333 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004334 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004335 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004336 llvm_unreachable("Clause is not allowed.");
4337 }
4338 return Res;
4339}
4340
4341OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4342 SourceLocation KindKwLoc,
4343 SourceLocation StartLoc,
4344 SourceLocation LParenLoc,
4345 SourceLocation EndLoc) {
4346 if (Kind == OMPC_DEFAULT_unknown) {
4347 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004348 static_assert(OMPC_DEFAULT_unknown > 0,
4349 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004350 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004351 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004352 Values += "'";
4353 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4354 Values += "'";
4355 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004356 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004357 Values += " or ";
4358 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004359 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004360 break;
4361 default:
4362 Values += Sep;
4363 break;
4364 }
4365 }
4366 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004367 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004368 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004369 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004370 switch (Kind) {
4371 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004372 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004373 break;
4374 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004375 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004376 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004377 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004378 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004379 break;
4380 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004381 return new (Context)
4382 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004383}
4384
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004385OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4386 SourceLocation KindKwLoc,
4387 SourceLocation StartLoc,
4388 SourceLocation LParenLoc,
4389 SourceLocation EndLoc) {
4390 if (Kind == OMPC_PROC_BIND_unknown) {
4391 std::string Values;
4392 std::string Sep(", ");
4393 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4394 Values += "'";
4395 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4396 Values += "'";
4397 switch (i) {
4398 case OMPC_PROC_BIND_unknown - 2:
4399 Values += " or ";
4400 break;
4401 case OMPC_PROC_BIND_unknown - 1:
4402 break;
4403 default:
4404 Values += Sep;
4405 break;
4406 }
4407 }
4408 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004409 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004410 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004411 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004412 return new (Context)
4413 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004414}
4415
Alexey Bataev56dafe82014-06-20 07:16:17 +00004416OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4417 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4418 SourceLocation StartLoc, SourceLocation LParenLoc,
4419 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4420 SourceLocation EndLoc) {
4421 OMPClause *Res = nullptr;
4422 switch (Kind) {
4423 case OMPC_schedule:
4424 Res = ActOnOpenMPScheduleClause(
4425 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4426 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4427 break;
4428 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004429 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004430 case OMPC_num_threads:
4431 case OMPC_safelen:
4432 case OMPC_collapse:
4433 case OMPC_default:
4434 case OMPC_proc_bind:
4435 case OMPC_private:
4436 case OMPC_firstprivate:
4437 case OMPC_lastprivate:
4438 case OMPC_shared:
4439 case OMPC_reduction:
4440 case OMPC_linear:
4441 case OMPC_aligned:
4442 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004443 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004444 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004445 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004446 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004447 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004448 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004449 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004450 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004451 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004452 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004453 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004454 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004455 case OMPC_unknown:
4456 llvm_unreachable("Clause is not allowed.");
4457 }
4458 return Res;
4459}
4460
4461OMPClause *Sema::ActOnOpenMPScheduleClause(
4462 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4463 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4464 SourceLocation EndLoc) {
4465 if (Kind == OMPC_SCHEDULE_unknown) {
4466 std::string Values;
4467 std::string Sep(", ");
4468 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4469 Values += "'";
4470 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4471 Values += "'";
4472 switch (i) {
4473 case OMPC_SCHEDULE_unknown - 2:
4474 Values += " or ";
4475 break;
4476 case OMPC_SCHEDULE_unknown - 1:
4477 break;
4478 default:
4479 Values += Sep;
4480 break;
4481 }
4482 }
4483 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4484 << Values << getOpenMPClauseName(OMPC_schedule);
4485 return nullptr;
4486 }
4487 Expr *ValExpr = ChunkSize;
4488 if (ChunkSize) {
4489 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4490 !ChunkSize->isInstantiationDependent() &&
4491 !ChunkSize->containsUnexpandedParameterPack()) {
4492 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4493 ExprResult Val =
4494 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4495 if (Val.isInvalid())
4496 return nullptr;
4497
4498 ValExpr = Val.get();
4499
4500 // OpenMP [2.7.1, Restrictions]
4501 // chunk_size must be a loop invariant integer expression with a positive
4502 // value.
4503 llvm::APSInt Result;
4504 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
4505 Result.isSigned() && !Result.isStrictlyPositive()) {
4506 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4507 << "schedule" << ChunkSize->getSourceRange();
4508 return nullptr;
4509 }
4510 }
4511 }
4512
4513 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
4514 EndLoc, Kind, ValExpr);
4515}
4516
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004517OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4518 SourceLocation StartLoc,
4519 SourceLocation EndLoc) {
4520 OMPClause *Res = nullptr;
4521 switch (Kind) {
4522 case OMPC_ordered:
4523 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4524 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004525 case OMPC_nowait:
4526 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4527 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004528 case OMPC_untied:
4529 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4530 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004531 case OMPC_mergeable:
4532 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4533 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004534 case OMPC_read:
4535 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4536 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004537 case OMPC_write:
4538 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4539 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004540 case OMPC_update:
4541 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4542 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004543 case OMPC_capture:
4544 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4545 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004546 case OMPC_seq_cst:
4547 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4548 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004549 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004550 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004551 case OMPC_num_threads:
4552 case OMPC_safelen:
4553 case OMPC_collapse:
4554 case OMPC_schedule:
4555 case OMPC_private:
4556 case OMPC_firstprivate:
4557 case OMPC_lastprivate:
4558 case OMPC_shared:
4559 case OMPC_reduction:
4560 case OMPC_linear:
4561 case OMPC_aligned:
4562 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004563 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004564 case OMPC_default:
4565 case OMPC_proc_bind:
4566 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004567 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004568 case OMPC_unknown:
4569 llvm_unreachable("Clause is not allowed.");
4570 }
4571 return Res;
4572}
4573
4574OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4575 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004576 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004577 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4578}
4579
Alexey Bataev236070f2014-06-20 11:19:47 +00004580OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4581 SourceLocation EndLoc) {
4582 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4583}
4584
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004585OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4586 SourceLocation EndLoc) {
4587 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4588}
4589
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004590OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4591 SourceLocation EndLoc) {
4592 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4593}
4594
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004595OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4596 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004597 return new (Context) OMPReadClause(StartLoc, EndLoc);
4598}
4599
Alexey Bataevdea47612014-07-23 07:46:59 +00004600OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4601 SourceLocation EndLoc) {
4602 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4603}
4604
Alexey Bataev67a4f222014-07-23 10:25:33 +00004605OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4606 SourceLocation EndLoc) {
4607 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4608}
4609
Alexey Bataev459dec02014-07-24 06:46:57 +00004610OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4611 SourceLocation EndLoc) {
4612 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4613}
4614
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004615OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4616 SourceLocation EndLoc) {
4617 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4618}
4619
Alexey Bataevc5e02582014-06-16 07:08:35 +00004620OMPClause *Sema::ActOnOpenMPVarListClause(
4621 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4622 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4623 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4624 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004625 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004626 switch (Kind) {
4627 case OMPC_private:
4628 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4629 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004630 case OMPC_firstprivate:
4631 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4632 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004633 case OMPC_lastprivate:
4634 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4635 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004636 case OMPC_shared:
4637 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4638 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004639 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004640 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4641 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004642 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004643 case OMPC_linear:
4644 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4645 ColonLoc, EndLoc);
4646 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004647 case OMPC_aligned:
4648 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4649 ColonLoc, EndLoc);
4650 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004651 case OMPC_copyin:
4652 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4653 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004654 case OMPC_copyprivate:
4655 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4656 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004657 case OMPC_flush:
4658 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4659 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004660 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004661 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004662 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004663 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004664 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004665 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004666 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004667 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004668 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004669 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004670 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004671 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004672 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004673 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004674 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004675 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004676 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004677 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004678 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004679 llvm_unreachable("Clause is not allowed.");
4680 }
4681 return Res;
4682}
4683
4684OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4685 SourceLocation StartLoc,
4686 SourceLocation LParenLoc,
4687 SourceLocation EndLoc) {
4688 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004689 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004690 for (auto &RefExpr : VarList) {
4691 assert(RefExpr && "NULL expr in OpenMP private clause.");
4692 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004693 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004694 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004695 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004696 continue;
4697 }
4698
Alexey Bataeved09d242014-05-28 05:53:51 +00004699 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004700 // OpenMP [2.1, C/C++]
4701 // A list item is a variable name.
4702 // OpenMP [2.9.3.3, Restrictions, p.1]
4703 // A variable that is part of another variable (as an array or
4704 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004705 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004706 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004707 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004708 continue;
4709 }
4710 Decl *D = DE->getDecl();
4711 VarDecl *VD = cast<VarDecl>(D);
4712
4713 QualType Type = VD->getType();
4714 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4715 // It will be analyzed later.
4716 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004717 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004718 continue;
4719 }
4720
4721 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4722 // A variable that appears in a private clause must not have an incomplete
4723 // type or a reference type.
4724 if (RequireCompleteType(ELoc, Type,
4725 diag::err_omp_private_incomplete_type)) {
4726 continue;
4727 }
4728 if (Type->isReferenceType()) {
4729 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004730 << getOpenMPClauseName(OMPC_private) << Type;
4731 bool IsDecl =
4732 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4733 Diag(VD->getLocation(),
4734 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4735 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004736 continue;
4737 }
4738
4739 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4740 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004741 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004742 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00004743 while (Type->isArrayType()) {
4744 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004745 }
4746
Alexey Bataev758e55e2013-09-06 18:03:48 +00004747 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4748 // in a Construct]
4749 // Variables with the predetermined data-sharing attributes may not be
4750 // listed in data-sharing attributes clauses, except for the cases
4751 // listed below. For these exceptions only, listing a predetermined
4752 // variable in a data-sharing attribute clause is allowed and overrides
4753 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004754 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004755 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004756 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4757 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004758 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004759 continue;
4760 }
4761
Alexey Bataev03b340a2014-10-21 03:16:40 +00004762 // Generate helper private variable and initialize it with the default
4763 // value. The address of the original variable is replaced by the address of
4764 // the new private variable in CodeGen. This new variable is not added to
4765 // IdResolver, so the code in the OpenMP region uses original variable for
4766 // proper diagnostics.
Alexey Bataev50a64582015-04-22 12:24:45 +00004767 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4768 DE->getExprLoc(), VD->getIdentifier(),
4769 VD->getType().getUnqualifiedType(),
4770 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004771 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4772 if (VDPrivate->isInvalidDecl())
4773 continue;
4774 CurContext->addDecl(VDPrivate);
Alexey Bataev07649fb2014-12-16 08:01:48 +00004775 auto VDPrivateRefExpr =
4776 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4777 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004778 /*RefersToEnclosingVariableOrCapture*/ false,
Alexey Bataev07649fb2014-12-16 08:01:48 +00004779 /*NameLoc*/ SourceLocation(), DE->getType(),
4780 /*VK*/ VK_LValue);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004781
Alexey Bataev758e55e2013-09-06 18:03:48 +00004782 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004783 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004784 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004785 }
4786
Alexey Bataeved09d242014-05-28 05:53:51 +00004787 if (Vars.empty())
4788 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004789
Alexey Bataev03b340a2014-10-21 03:16:40 +00004790 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4791 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004792}
4793
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004794namespace {
4795class DiagsUninitializedSeveretyRAII {
4796private:
4797 DiagnosticsEngine &Diags;
4798 SourceLocation SavedLoc;
4799 bool IsIgnored;
4800
4801public:
4802 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4803 bool IsIgnored)
4804 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4805 if (!IsIgnored) {
4806 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4807 /*Map*/ diag::Severity::Ignored, Loc);
4808 }
4809 }
4810 ~DiagsUninitializedSeveretyRAII() {
4811 if (!IsIgnored)
4812 Diags.popMappings(SavedLoc);
4813 }
4814};
4815}
4816
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004817OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4818 SourceLocation StartLoc,
4819 SourceLocation LParenLoc,
4820 SourceLocation EndLoc) {
4821 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004822 SmallVector<Expr *, 8> PrivateCopies;
4823 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004824 bool IsImplicitClause =
4825 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4826 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4827
Alexey Bataeved09d242014-05-28 05:53:51 +00004828 for (auto &RefExpr : VarList) {
4829 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4830 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004831 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004832 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004833 PrivateCopies.push_back(nullptr);
4834 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004835 continue;
4836 }
4837
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004838 SourceLocation ELoc =
4839 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004840 // OpenMP [2.1, C/C++]
4841 // A list item is a variable name.
4842 // OpenMP [2.9.3.3, Restrictions, p.1]
4843 // A variable that is part of another variable (as an array or
4844 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004845 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004846 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004847 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004848 continue;
4849 }
4850 Decl *D = DE->getDecl();
4851 VarDecl *VD = cast<VarDecl>(D);
4852
4853 QualType Type = VD->getType();
4854 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4855 // It will be analyzed later.
4856 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004857 PrivateCopies.push_back(nullptr);
4858 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004859 continue;
4860 }
4861
4862 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4863 // A variable that appears in a private clause must not have an incomplete
4864 // type or a reference type.
4865 if (RequireCompleteType(ELoc, Type,
4866 diag::err_omp_firstprivate_incomplete_type)) {
4867 continue;
4868 }
4869 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004870 if (IsImplicitClause) {
4871 Diag(ImplicitClauseLoc,
4872 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4873 << Type;
4874 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4875 } else {
4876 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4877 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4878 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004879 bool IsDecl =
4880 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4881 Diag(VD->getLocation(),
4882 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4883 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004884 continue;
4885 }
4886
4887 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4888 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004889 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004890 // class type.
Alexey Bataev69c62a92015-04-15 04:52:20 +00004891 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004892
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004893 // If an implicit firstprivate variable found it was checked already.
4894 if (!IsImplicitClause) {
4895 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004896 Type = Type.getNonReferenceType().getCanonicalType();
4897 bool IsConstant = Type.isConstant(Context);
4898 Type = Context.getBaseElementType(Type);
4899 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4900 // A list item that specifies a given variable may not appear in more
4901 // than one clause on the same directive, except that a variable may be
4902 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004903 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004904 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004905 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004906 << getOpenMPClauseName(DVar.CKind)
4907 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004908 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004909 continue;
4910 }
4911
4912 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4913 // in a Construct]
4914 // Variables with the predetermined data-sharing attributes may not be
4915 // listed in data-sharing attributes clauses, except for the cases
4916 // listed below. For these exceptions only, listing a predetermined
4917 // variable in a data-sharing attribute clause is allowed and overrides
4918 // the variable's predetermined data-sharing attributes.
4919 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4920 // in a Construct, C/C++, p.2]
4921 // Variables with const-qualified type having no mutable member may be
4922 // listed in a firstprivate clause, even if they are static data members.
4923 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4924 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4925 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004926 << getOpenMPClauseName(DVar.CKind)
4927 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004928 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004929 continue;
4930 }
4931
Alexey Bataevf29276e2014-06-18 04:14:57 +00004932 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004933 // OpenMP [2.9.3.4, Restrictions, p.2]
4934 // A list item that is private within a parallel region must not appear
4935 // in a firstprivate clause on a worksharing construct if any of the
4936 // worksharing regions arising from the worksharing construct ever bind
4937 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004938 if (isOpenMPWorksharingDirective(CurrDir) &&
4939 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004940 DVar = DSAStack->getImplicitDSA(VD, true);
4941 if (DVar.CKind != OMPC_shared &&
4942 (isOpenMPParallelDirective(DVar.DKind) ||
4943 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004944 Diag(ELoc, diag::err_omp_required_access)
4945 << getOpenMPClauseName(OMPC_firstprivate)
4946 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004947 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004948 continue;
4949 }
4950 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004951 // OpenMP [2.9.3.4, Restrictions, p.3]
4952 // A list item that appears in a reduction clause of a parallel construct
4953 // must not appear in a firstprivate clause on a worksharing or task
4954 // construct if any of the worksharing or task regions arising from the
4955 // worksharing or task construct ever bind to any of the parallel regions
4956 // arising from the parallel construct.
4957 // OpenMP [2.9.3.4, Restrictions, p.4]
4958 // A list item that appears in a reduction clause in worksharing
4959 // construct must not appear in a firstprivate clause in a task construct
4960 // encountered during execution of any of the worksharing regions arising
4961 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004962 if (CurrDir == OMPD_task) {
4963 DVar =
4964 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4965 [](OpenMPDirectiveKind K) -> bool {
4966 return isOpenMPParallelDirective(K) ||
4967 isOpenMPWorksharingDirective(K);
4968 },
4969 false);
4970 if (DVar.CKind == OMPC_reduction &&
4971 (isOpenMPParallelDirective(DVar.DKind) ||
4972 isOpenMPWorksharingDirective(DVar.DKind))) {
4973 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4974 << getOpenMPDirectiveName(DVar.DKind);
4975 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4976 continue;
4977 }
4978 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004979 }
4980
Alexey Bataev69c62a92015-04-15 04:52:20 +00004981 auto VDPrivate =
4982 VarDecl::Create(Context, CurContext, DE->getLocStart(), ELoc,
4983 VD->getIdentifier(), VD->getType().getUnqualifiedType(),
4984 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004985 // Generate helper private variable and initialize it with the value of the
4986 // original variable. The address of the original variable is replaced by
4987 // the address of the new private variable in the CodeGen. This new variable
4988 // is not added to IdResolver, so the code in the OpenMP region uses
4989 // original variable for proper diagnostics and variable capturing.
4990 Expr *VDInitRefExpr = nullptr;
4991 // For arrays generate initializer for single element and replace it by the
4992 // original array element in CodeGen.
4993 if (DE->getType()->isArrayType()) {
4994 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4995 ELoc, VD->getIdentifier(), Type,
4996 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4997 CurContext->addHiddenDecl(VDInit);
4998 VDInitRefExpr = DeclRefExpr::Create(
4999 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
5000 /*TemplateKWLoc*/ SourceLocation(), VDInit,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005001 /*RefersToEnclosingVariableOrCapture*/ true, ELoc, Type,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005002 /*VK*/ VK_LValue);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005003 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataev69c62a92015-04-15 04:52:20 +00005004 auto *VDInitTemp =
5005 BuildVarDecl(*this, DE->getLocStart(), Type.getUnqualifiedType(),
5006 ".firstprivate.temp");
5007 InitializedEntity Entity =
5008 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005009 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5010
5011 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5012 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5013 if (Result.isInvalid())
5014 VDPrivate->setInvalidDecl();
5015 else
5016 VDPrivate->setInit(Result.getAs<Expr>());
5017 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005018 auto *VDInit =
5019 BuildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
5020 VDInitRefExpr =
5021 BuildDeclRefExpr(VDInit, Type, VK_LValue, DE->getExprLoc()).get();
5022 AddInitializerToDecl(VDPrivate,
5023 DefaultLvalueConversion(VDInitRefExpr).get(),
5024 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005025 }
5026 if (VDPrivate->isInvalidDecl()) {
5027 if (IsImplicitClause) {
5028 Diag(DE->getExprLoc(),
5029 diag::note_omp_task_predetermined_firstprivate_here);
5030 }
5031 continue;
5032 }
5033 CurContext->addDecl(VDPrivate);
Alexey Bataev69c62a92015-04-15 04:52:20 +00005034 auto VDPrivateRefExpr = DeclRefExpr::Create(
5035 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
5036 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
5037 /*RefersToEnclosingVariableOrCapture*/ false, DE->getLocStart(),
5038 DE->getType().getUnqualifiedType(), /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005039 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5040 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005041 PrivateCopies.push_back(VDPrivateRefExpr);
5042 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005043 }
5044
Alexey Bataeved09d242014-05-28 05:53:51 +00005045 if (Vars.empty())
5046 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005047
5048 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005049 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005050}
5051
Alexander Musman1bb328c2014-06-04 13:06:39 +00005052OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5053 SourceLocation StartLoc,
5054 SourceLocation LParenLoc,
5055 SourceLocation EndLoc) {
5056 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005057 SmallVector<Expr *, 8> SrcExprs;
5058 SmallVector<Expr *, 8> DstExprs;
5059 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005060 for (auto &RefExpr : VarList) {
5061 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5062 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5063 // It will be analyzed later.
5064 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005065 SrcExprs.push_back(nullptr);
5066 DstExprs.push_back(nullptr);
5067 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005068 continue;
5069 }
5070
5071 SourceLocation ELoc = RefExpr->getExprLoc();
5072 // OpenMP [2.1, C/C++]
5073 // A list item is a variable name.
5074 // OpenMP [2.14.3.5, Restrictions, p.1]
5075 // A variable that is part of another variable (as an array or structure
5076 // element) cannot appear in a lastprivate clause.
5077 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5078 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5079 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5080 continue;
5081 }
5082 Decl *D = DE->getDecl();
5083 VarDecl *VD = cast<VarDecl>(D);
5084
5085 QualType Type = VD->getType();
5086 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5087 // It will be analyzed later.
5088 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005089 SrcExprs.push_back(nullptr);
5090 DstExprs.push_back(nullptr);
5091 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005092 continue;
5093 }
5094
5095 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5096 // A variable that appears in a lastprivate clause must not have an
5097 // incomplete type or a reference type.
5098 if (RequireCompleteType(ELoc, Type,
5099 diag::err_omp_lastprivate_incomplete_type)) {
5100 continue;
5101 }
5102 if (Type->isReferenceType()) {
5103 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5104 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5105 bool IsDecl =
5106 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5107 Diag(VD->getLocation(),
5108 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5109 << VD;
5110 continue;
5111 }
5112
5113 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5114 // in a Construct]
5115 // Variables with the predetermined data-sharing attributes may not be
5116 // listed in data-sharing attributes clauses, except for the cases
5117 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005118 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005119 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5120 DVar.CKind != OMPC_firstprivate &&
5121 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5122 Diag(ELoc, diag::err_omp_wrong_dsa)
5123 << getOpenMPClauseName(DVar.CKind)
5124 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005125 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005126 continue;
5127 }
5128
Alexey Bataevf29276e2014-06-18 04:14:57 +00005129 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5130 // OpenMP [2.14.3.5, Restrictions, p.2]
5131 // A list item that is private within a parallel region, or that appears in
5132 // the reduction clause of a parallel construct, must not appear in a
5133 // lastprivate clause on a worksharing construct if any of the corresponding
5134 // worksharing regions ever binds to any of the corresponding parallel
5135 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00005136 if (isOpenMPWorksharingDirective(CurrDir) &&
5137 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005138 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005139 if (DVar.CKind != OMPC_shared) {
5140 Diag(ELoc, diag::err_omp_required_access)
5141 << getOpenMPClauseName(OMPC_lastprivate)
5142 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005143 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005144 continue;
5145 }
5146 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005147 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005148 // A variable of class type (or array thereof) that appears in a
5149 // lastprivate clause requires an accessible, unambiguous default
5150 // constructor for the class type, unless the list item is also specified
5151 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005152 // A variable of class type (or array thereof) that appears in a
5153 // lastprivate clause requires an accessible, unambiguous copy assignment
5154 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005155 Type = Context.getBaseElementType(Type).getNonReferenceType();
5156 auto *SrcVD = BuildVarDecl(*this, DE->getLocStart(),
5157 Type.getUnqualifiedType(), ".lastprivate.src");
5158 auto *PseudoSrcExpr = BuildDeclRefExpr(SrcVD, Type.getUnqualifiedType(),
5159 VK_LValue, DE->getExprLoc()).get();
5160 auto *DstVD =
5161 BuildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
5162 auto *PseudoDstExpr =
5163 BuildDeclRefExpr(DstVD, Type, VK_LValue, DE->getExprLoc()).get();
5164 // For arrays generate assignment operation for single element and replace
5165 // it by the original array element in CodeGen.
5166 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5167 PseudoDstExpr, PseudoSrcExpr);
5168 if (AssignmentOp.isInvalid())
5169 continue;
5170 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5171 /*DiscardedValue=*/true);
5172 if (AssignmentOp.isInvalid())
5173 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005174
Alexey Bataevf29276e2014-06-18 04:14:57 +00005175 if (DVar.CKind != OMPC_firstprivate)
5176 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005177 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005178 SrcExprs.push_back(PseudoSrcExpr);
5179 DstExprs.push_back(PseudoDstExpr);
5180 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005181 }
5182
5183 if (Vars.empty())
5184 return nullptr;
5185
5186 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005187 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005188}
5189
Alexey Bataev758e55e2013-09-06 18:03:48 +00005190OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5191 SourceLocation StartLoc,
5192 SourceLocation LParenLoc,
5193 SourceLocation EndLoc) {
5194 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005195 for (auto &RefExpr : VarList) {
5196 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5197 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005198 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005199 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005200 continue;
5201 }
5202
Alexey Bataeved09d242014-05-28 05:53:51 +00005203 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005204 // OpenMP [2.1, C/C++]
5205 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005206 // OpenMP [2.14.3.2, Restrictions, p.1]
5207 // A variable that is part of another variable (as an array or structure
5208 // element) cannot appear in a shared unless it is a static data member
5209 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005210 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005211 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005212 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005213 continue;
5214 }
5215 Decl *D = DE->getDecl();
5216 VarDecl *VD = cast<VarDecl>(D);
5217
5218 QualType Type = VD->getType();
5219 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5220 // It will be analyzed later.
5221 Vars.push_back(DE);
5222 continue;
5223 }
5224
5225 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5226 // in a Construct]
5227 // Variables with the predetermined data-sharing attributes may not be
5228 // listed in data-sharing attributes clauses, except for the cases
5229 // listed below. For these exceptions only, listing a predetermined
5230 // variable in a data-sharing attribute clause is allowed and overrides
5231 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005232 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005233 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5234 DVar.RefExpr) {
5235 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5236 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005237 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005238 continue;
5239 }
5240
5241 DSAStack->addDSA(VD, DE, OMPC_shared);
5242 Vars.push_back(DE);
5243 }
5244
Alexey Bataeved09d242014-05-28 05:53:51 +00005245 if (Vars.empty())
5246 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005247
5248 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5249}
5250
Alexey Bataevc5e02582014-06-16 07:08:35 +00005251namespace {
5252class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5253 DSAStackTy *Stack;
5254
5255public:
5256 bool VisitDeclRefExpr(DeclRefExpr *E) {
5257 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005258 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005259 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5260 return false;
5261 if (DVar.CKind != OMPC_unknown)
5262 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005263 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005264 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005265 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005266 return true;
5267 return false;
5268 }
5269 return false;
5270 }
5271 bool VisitStmt(Stmt *S) {
5272 for (auto Child : S->children()) {
5273 if (Child && Visit(Child))
5274 return true;
5275 }
5276 return false;
5277 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005278 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005279};
Alexey Bataev23b69422014-06-18 07:08:49 +00005280} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005281
5282OMPClause *Sema::ActOnOpenMPReductionClause(
5283 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5284 SourceLocation ColonLoc, SourceLocation EndLoc,
5285 CXXScopeSpec &ReductionIdScopeSpec,
5286 const DeclarationNameInfo &ReductionId) {
5287 // TODO: Allow scope specification search when 'declare reduction' is
5288 // supported.
5289 assert(ReductionIdScopeSpec.isEmpty() &&
5290 "No support for scoped reduction identifiers yet.");
5291
5292 auto DN = ReductionId.getName();
5293 auto OOK = DN.getCXXOverloadedOperator();
5294 BinaryOperatorKind BOK = BO_Comma;
5295
5296 // OpenMP [2.14.3.6, reduction clause]
5297 // C
5298 // reduction-identifier is either an identifier or one of the following
5299 // operators: +, -, *, &, |, ^, && and ||
5300 // C++
5301 // reduction-identifier is either an id-expression or one of the following
5302 // operators: +, -, *, &, |, ^, && and ||
5303 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5304 switch (OOK) {
5305 case OO_Plus:
5306 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005307 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005308 break;
5309 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005310 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005311 break;
5312 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005313 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005314 break;
5315 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005316 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005317 break;
5318 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005319 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005320 break;
5321 case OO_AmpAmp:
5322 BOK = BO_LAnd;
5323 break;
5324 case OO_PipePipe:
5325 BOK = BO_LOr;
5326 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005327 case OO_New:
5328 case OO_Delete:
5329 case OO_Array_New:
5330 case OO_Array_Delete:
5331 case OO_Slash:
5332 case OO_Percent:
5333 case OO_Tilde:
5334 case OO_Exclaim:
5335 case OO_Equal:
5336 case OO_Less:
5337 case OO_Greater:
5338 case OO_LessEqual:
5339 case OO_GreaterEqual:
5340 case OO_PlusEqual:
5341 case OO_MinusEqual:
5342 case OO_StarEqual:
5343 case OO_SlashEqual:
5344 case OO_PercentEqual:
5345 case OO_CaretEqual:
5346 case OO_AmpEqual:
5347 case OO_PipeEqual:
5348 case OO_LessLess:
5349 case OO_GreaterGreater:
5350 case OO_LessLessEqual:
5351 case OO_GreaterGreaterEqual:
5352 case OO_EqualEqual:
5353 case OO_ExclaimEqual:
5354 case OO_PlusPlus:
5355 case OO_MinusMinus:
5356 case OO_Comma:
5357 case OO_ArrowStar:
5358 case OO_Arrow:
5359 case OO_Call:
5360 case OO_Subscript:
5361 case OO_Conditional:
5362 case NUM_OVERLOADED_OPERATORS:
5363 llvm_unreachable("Unexpected reduction identifier");
5364 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005365 if (auto II = DN.getAsIdentifierInfo()) {
5366 if (II->isStr("max"))
5367 BOK = BO_GT;
5368 else if (II->isStr("min"))
5369 BOK = BO_LT;
5370 }
5371 break;
5372 }
5373 SourceRange ReductionIdRange;
5374 if (ReductionIdScopeSpec.isValid()) {
5375 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5376 }
5377 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5378 if (BOK == BO_Comma) {
5379 // Not allowed reduction identifier is found.
5380 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5381 << ReductionIdRange;
5382 return nullptr;
5383 }
5384
5385 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005386 SmallVector<Expr *, 8> LHSs;
5387 SmallVector<Expr *, 8> RHSs;
5388 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005389 for (auto RefExpr : VarList) {
5390 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5391 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5392 // It will be analyzed later.
5393 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005394 LHSs.push_back(nullptr);
5395 RHSs.push_back(nullptr);
5396 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005397 continue;
5398 }
5399
5400 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5401 RefExpr->isInstantiationDependent() ||
5402 RefExpr->containsUnexpandedParameterPack()) {
5403 // It will be analyzed later.
5404 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005405 LHSs.push_back(nullptr);
5406 RHSs.push_back(nullptr);
5407 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005408 continue;
5409 }
5410
5411 auto ELoc = RefExpr->getExprLoc();
5412 auto ERange = RefExpr->getSourceRange();
5413 // OpenMP [2.1, C/C++]
5414 // A list item is a variable or array section, subject to the restrictions
5415 // specified in Section 2.4 on page 42 and in each of the sections
5416 // describing clauses and directives for which a list appears.
5417 // OpenMP [2.14.3.3, Restrictions, p.1]
5418 // A variable that is part of another variable (as an array or
5419 // structure element) cannot appear in a private clause.
5420 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5421 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5422 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5423 continue;
5424 }
5425 auto D = DE->getDecl();
5426 auto VD = cast<VarDecl>(D);
5427 auto Type = VD->getType();
5428 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5429 // A variable that appears in a private clause must not have an incomplete
5430 // type or a reference type.
5431 if (RequireCompleteType(ELoc, Type,
5432 diag::err_omp_reduction_incomplete_type))
5433 continue;
5434 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5435 // Arrays may not appear in a reduction clause.
5436 if (Type.getNonReferenceType()->isArrayType()) {
5437 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5438 bool IsDecl =
5439 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5440 Diag(VD->getLocation(),
5441 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5442 << VD;
5443 continue;
5444 }
5445 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5446 // A list item that appears in a reduction clause must not be
5447 // const-qualified.
5448 if (Type.getNonReferenceType().isConstant(Context)) {
5449 Diag(ELoc, diag::err_omp_const_variable)
5450 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5451 bool IsDecl =
5452 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5453 Diag(VD->getLocation(),
5454 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5455 << VD;
5456 continue;
5457 }
5458 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5459 // If a list-item is a reference type then it must bind to the same object
5460 // for all threads of the team.
5461 VarDecl *VDDef = VD->getDefinition();
5462 if (Type->isReferenceType() && VDDef) {
5463 DSARefChecker Check(DSAStack);
5464 if (Check.Visit(VDDef->getInit())) {
5465 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5466 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5467 continue;
5468 }
5469 }
5470 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5471 // The type of a list item that appears in a reduction clause must be valid
5472 // for the reduction-identifier. For a max or min reduction in C, the type
5473 // of the list item must be an allowed arithmetic data type: char, int,
5474 // float, double, or _Bool, possibly modified with long, short, signed, or
5475 // unsigned. For a max or min reduction in C++, the type of the list item
5476 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5477 // double, or bool, possibly modified with long, short, signed, or unsigned.
5478 if ((BOK == BO_GT || BOK == BO_LT) &&
5479 !(Type->isScalarType() ||
5480 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5481 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5482 << getLangOpts().CPlusPlus;
5483 bool IsDecl =
5484 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5485 Diag(VD->getLocation(),
5486 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5487 << VD;
5488 continue;
5489 }
5490 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5491 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5492 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5493 bool IsDecl =
5494 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5495 Diag(VD->getLocation(),
5496 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5497 << VD;
5498 continue;
5499 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005500 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5501 // in a Construct]
5502 // Variables with the predetermined data-sharing attributes may not be
5503 // listed in data-sharing attributes clauses, except for the cases
5504 // listed below. For these exceptions only, listing a predetermined
5505 // variable in a data-sharing attribute clause is allowed and overrides
5506 // the variable's predetermined data-sharing attributes.
5507 // OpenMP [2.14.3.6, Restrictions, p.3]
5508 // Any number of reduction clauses can be specified on the directive,
5509 // but a list item can appear only once in the reduction clauses for that
5510 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005511 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005512 if (DVar.CKind == OMPC_reduction) {
5513 Diag(ELoc, diag::err_omp_once_referenced)
5514 << getOpenMPClauseName(OMPC_reduction);
5515 if (DVar.RefExpr) {
5516 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5517 }
5518 } else if (DVar.CKind != OMPC_unknown) {
5519 Diag(ELoc, diag::err_omp_wrong_dsa)
5520 << getOpenMPClauseName(DVar.CKind)
5521 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005522 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005523 continue;
5524 }
5525
5526 // OpenMP [2.14.3.6, Restrictions, p.1]
5527 // A list item that appears in a reduction clause of a worksharing
5528 // construct must be shared in the parallel regions to which any of the
5529 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005530 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005531 if (isOpenMPWorksharingDirective(CurrDir) &&
5532 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005533 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005534 if (DVar.CKind != OMPC_shared) {
5535 Diag(ELoc, diag::err_omp_required_access)
5536 << getOpenMPClauseName(OMPC_reduction)
5537 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005538 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005539 continue;
5540 }
5541 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005542 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
5543 auto *LHSVD = BuildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5544 auto *RHSVD = BuildVarDecl(*this, ELoc, Type, VD->getName());
5545 // Add initializer for private variable.
5546 Expr *Init = nullptr;
5547 switch (BOK) {
5548 case BO_Add:
5549 case BO_Xor:
5550 case BO_Or:
5551 case BO_LOr:
5552 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5553 if (Type->isScalarType() || Type->isAnyComplexType()) {
5554 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005555 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005556 break;
5557 case BO_Mul:
5558 case BO_LAnd:
5559 if (Type->isScalarType() || Type->isAnyComplexType()) {
5560 // '*' and '&&' reduction ops - initializer is '1'.
5561 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5562 }
5563 break;
5564 case BO_And: {
5565 // '&' reduction op - initializer is '~0'.
5566 QualType OrigType = Type;
5567 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5568 Type = ComplexTy->getElementType();
5569 }
5570 if (Type->isRealFloatingType()) {
5571 llvm::APFloat InitValue =
5572 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5573 /*isIEEE=*/true);
5574 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5575 Type, ELoc);
5576 } else if (Type->isScalarType()) {
5577 auto Size = Context.getTypeSize(Type);
5578 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5579 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5580 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5581 }
5582 if (Init && OrigType->isAnyComplexType()) {
5583 // Init = 0xFFFF + 0xFFFFi;
5584 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5585 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5586 }
5587 Type = OrigType;
5588 break;
5589 }
5590 case BO_LT:
5591 case BO_GT: {
5592 // 'min' reduction op - initializer is 'Largest representable number in
5593 // the reduction list item type'.
5594 // 'max' reduction op - initializer is 'Least representable number in
5595 // the reduction list item type'.
5596 if (Type->isIntegerType() || Type->isPointerType()) {
5597 bool IsSigned = Type->hasSignedIntegerRepresentation();
5598 auto Size = Context.getTypeSize(Type);
5599 QualType IntTy =
5600 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5601 llvm::APInt InitValue =
5602 (BOK != BO_LT)
5603 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5604 : llvm::APInt::getMinValue(Size)
5605 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5606 : llvm::APInt::getMaxValue(Size);
5607 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5608 if (Type->isPointerType()) {
5609 // Cast to pointer type.
5610 auto CastExpr = BuildCStyleCastExpr(
5611 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5612 SourceLocation(), Init);
5613 if (CastExpr.isInvalid())
5614 continue;
5615 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005616 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005617 } else if (Type->isRealFloatingType()) {
5618 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5619 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5620 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5621 Type, ELoc);
5622 }
5623 break;
5624 }
5625 case BO_PtrMemD:
5626 case BO_PtrMemI:
5627 case BO_MulAssign:
5628 case BO_Div:
5629 case BO_Rem:
5630 case BO_Sub:
5631 case BO_Shl:
5632 case BO_Shr:
5633 case BO_LE:
5634 case BO_GE:
5635 case BO_EQ:
5636 case BO_NE:
5637 case BO_AndAssign:
5638 case BO_XorAssign:
5639 case BO_OrAssign:
5640 case BO_Assign:
5641 case BO_AddAssign:
5642 case BO_SubAssign:
5643 case BO_DivAssign:
5644 case BO_RemAssign:
5645 case BO_ShlAssign:
5646 case BO_ShrAssign:
5647 case BO_Comma:
5648 llvm_unreachable("Unexpected reduction operation");
5649 }
5650 if (Init) {
5651 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
5652 /*TypeMayContainAuto=*/false);
5653 } else {
5654 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
5655 }
5656 if (!RHSVD->hasInit()) {
5657 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
5658 << ReductionIdRange;
5659 bool IsDecl =
5660 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5661 Diag(VD->getLocation(),
5662 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5663 << VD;
5664 continue;
5665 }
5666 auto *LHSDRE = BuildDeclRefExpr(LHSVD, Type, VK_LValue, ELoc).get();
5667 auto *RHSDRE = BuildDeclRefExpr(RHSVD, Type, VK_LValue, ELoc).get();
5668 ExprResult ReductionOp =
5669 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
5670 LHSDRE, RHSDRE);
5671 if (ReductionOp.isUsable()) {
5672 if (BOK != BO_LOr && BOK != BO_LAnd) {
5673 ReductionOp =
5674 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5675 BO_Assign, LHSDRE, ReductionOp.get());
5676 } else {
5677 auto *ConditionalOp = new (Context) ConditionalOperator(
5678 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
5679 RHSDRE, Type, VK_LValue, OK_Ordinary);
5680 ReductionOp =
5681 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5682 BO_Assign, LHSDRE, ConditionalOp);
5683 }
5684 if (ReductionOp.isUsable()) {
5685 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005686 }
5687 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005688 if (ReductionOp.isInvalid())
5689 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005690
5691 DSAStack->addDSA(VD, DE, OMPC_reduction);
5692 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005693 LHSs.push_back(LHSDRE);
5694 RHSs.push_back(RHSDRE);
5695 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005696 }
5697
5698 if (Vars.empty())
5699 return nullptr;
5700
5701 return OMPReductionClause::Create(
5702 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005703 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
5704 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005705}
5706
Alexander Musman8dba6642014-04-22 13:09:42 +00005707OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
5708 SourceLocation StartLoc,
5709 SourceLocation LParenLoc,
5710 SourceLocation ColonLoc,
5711 SourceLocation EndLoc) {
5712 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00005713 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00005714 for (auto &RefExpr : VarList) {
5715 assert(RefExpr && "NULL expr in OpenMP linear clause.");
5716 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00005717 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005718 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00005719 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005720 continue;
5721 }
5722
5723 // OpenMP [2.14.3.7, linear clause]
5724 // A list item that appears in a linear clause is subject to the private
5725 // clause semantics described in Section 2.14.3.3 on page 159 except as
5726 // noted. In addition, the value of the new list item on each iteration
5727 // of the associated loop(s) corresponds to the value of the original
5728 // list item before entering the construct plus the logical number of
5729 // the iteration times linear-step.
5730
Alexey Bataeved09d242014-05-28 05:53:51 +00005731 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00005732 // OpenMP [2.1, C/C++]
5733 // A list item is a variable name.
5734 // OpenMP [2.14.3.3, Restrictions, p.1]
5735 // A variable that is part of another variable (as an array or
5736 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005737 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005738 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005739 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00005740 continue;
5741 }
5742
5743 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5744
5745 // OpenMP [2.14.3.7, linear clause]
5746 // A list-item cannot appear in more than one linear clause.
5747 // A list-item that appears in a linear clause cannot appear in any
5748 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005749 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00005750 if (DVar.RefExpr) {
5751 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5752 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005753 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00005754 continue;
5755 }
5756
5757 QualType QType = VD->getType();
5758 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
5759 // It will be analyzed later.
5760 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00005761 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005762 continue;
5763 }
5764
5765 // A variable must not have an incomplete type or a reference type.
5766 if (RequireCompleteType(ELoc, QType,
5767 diag::err_omp_linear_incomplete_type)) {
5768 continue;
5769 }
5770 if (QType->isReferenceType()) {
5771 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5772 << getOpenMPClauseName(OMPC_linear) << QType;
5773 bool IsDecl =
5774 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5775 Diag(VD->getLocation(),
5776 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5777 << VD;
5778 continue;
5779 }
5780
5781 // A list item must not be const-qualified.
5782 if (QType.isConstant(Context)) {
5783 Diag(ELoc, diag::err_omp_const_variable)
5784 << getOpenMPClauseName(OMPC_linear);
5785 bool IsDecl =
5786 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5787 Diag(VD->getLocation(),
5788 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5789 << VD;
5790 continue;
5791 }
5792
5793 // A list item must be of integral or pointer type.
5794 QType = QType.getUnqualifiedType().getCanonicalType();
5795 const Type *Ty = QType.getTypePtrOrNull();
5796 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
5797 !Ty->isPointerType())) {
5798 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
5799 bool IsDecl =
5800 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5801 Diag(VD->getLocation(),
5802 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5803 << VD;
5804 continue;
5805 }
5806
Alexander Musman3276a272015-03-21 10:12:56 +00005807 // Build var to save initial value.
5808 VarDecl *Init = BuildVarDecl(*this, ELoc, DE->getType(), ".linear.start");
5809 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
5810 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5811 CurContext->addDecl(Init);
5812 Init->setIsUsed();
5813 auto InitRef = DeclRefExpr::Create(
5814 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
5815 /*TemplateKWLoc*/ SourceLocation(), Init,
5816 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(),
5817 /*VK*/ VK_LValue);
Alexander Musman8dba6642014-04-22 13:09:42 +00005818 DSAStack->addDSA(VD, DE, OMPC_linear);
5819 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00005820 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00005821 }
5822
5823 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005824 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005825
5826 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00005827 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005828 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5829 !Step->isInstantiationDependent() &&
5830 !Step->containsUnexpandedParameterPack()) {
5831 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005832 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00005833 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005834 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005835 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00005836
Alexander Musman3276a272015-03-21 10:12:56 +00005837 // Build var to save the step value.
5838 VarDecl *SaveVar =
5839 BuildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
5840 CurContext->addDecl(SaveVar);
5841 SaveVar->setIsUsed();
5842 ExprResult SaveRef =
5843 BuildDeclRefExpr(SaveVar, StepExpr->getType(), VK_LValue, StepLoc);
5844 ExprResult CalcStep =
5845 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
5846
Alexander Musman8dba6642014-04-22 13:09:42 +00005847 // Warn about zero linear step (it would be probably better specified as
5848 // making corresponding variables 'const').
5849 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00005850 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
5851 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00005852 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5853 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00005854 if (!IsConstant && CalcStep.isUsable()) {
5855 // Calculate the step beforehand instead of doing this on each iteration.
5856 // (This is not used if the number of iterations may be kfold-ed).
5857 CalcStepExpr = CalcStep.get();
5858 }
Alexander Musman8dba6642014-04-22 13:09:42 +00005859 }
5860
5861 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00005862 Vars, Inits, StepExpr, CalcStepExpr);
5863}
5864
5865static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
5866 Expr *NumIterations, Sema &SemaRef,
5867 Scope *S) {
5868 // Walk the vars and build update/final expressions for the CodeGen.
5869 SmallVector<Expr *, 8> Updates;
5870 SmallVector<Expr *, 8> Finals;
5871 Expr *Step = Clause.getStep();
5872 Expr *CalcStep = Clause.getCalcStep();
5873 // OpenMP [2.14.3.7, linear clause]
5874 // If linear-step is not specified it is assumed to be 1.
5875 if (Step == nullptr)
5876 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
5877 else if (CalcStep)
5878 Step = cast<BinaryOperator>(CalcStep)->getLHS();
5879 bool HasErrors = false;
5880 auto CurInit = Clause.inits().begin();
5881 for (auto &RefExpr : Clause.varlists()) {
5882 Expr *InitExpr = *CurInit;
5883
5884 // Build privatized reference to the current linear var.
5885 auto DE = cast<DeclRefExpr>(RefExpr);
5886 auto PrivateRef = DeclRefExpr::Create(
5887 SemaRef.Context, /*QualifierLoc*/ DE->getQualifierLoc(),
5888 /*TemplateKWLoc*/ SourceLocation(), DE->getDecl(),
5889 /* RefersToEnclosingVariableOrCapture */ true, DE->getLocStart(),
5890 DE->getType(), /*VK*/ VK_LValue);
5891
5892 // Build update: Var = InitExpr + IV * Step
5893 ExprResult Update =
5894 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
5895 InitExpr, IV, Step, /* Subtract */ false);
5896 Update = SemaRef.ActOnFinishFullExpr(Update.get());
5897
5898 // Build final: Var = InitExpr + NumIterations * Step
5899 ExprResult Final =
5900 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), RefExpr, InitExpr,
5901 NumIterations, Step, /* Subtract */ false);
5902 Final = SemaRef.ActOnFinishFullExpr(Final.get());
5903 if (!Update.isUsable() || !Final.isUsable()) {
5904 Updates.push_back(nullptr);
5905 Finals.push_back(nullptr);
5906 HasErrors = true;
5907 } else {
5908 Updates.push_back(Update.get());
5909 Finals.push_back(Final.get());
5910 }
5911 ++CurInit;
5912 }
5913 Clause.setUpdates(Updates);
5914 Clause.setFinals(Finals);
5915 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00005916}
5917
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005918OMPClause *Sema::ActOnOpenMPAlignedClause(
5919 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5920 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5921
5922 SmallVector<Expr *, 8> Vars;
5923 for (auto &RefExpr : VarList) {
5924 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5925 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5926 // It will be analyzed later.
5927 Vars.push_back(RefExpr);
5928 continue;
5929 }
5930
5931 SourceLocation ELoc = RefExpr->getExprLoc();
5932 // OpenMP [2.1, C/C++]
5933 // A list item is a variable name.
5934 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5935 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5936 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5937 continue;
5938 }
5939
5940 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5941
5942 // OpenMP [2.8.1, simd construct, Restrictions]
5943 // The type of list items appearing in the aligned clause must be
5944 // array, pointer, reference to array, or reference to pointer.
5945 QualType QType = DE->getType()
5946 .getNonReferenceType()
5947 .getUnqualifiedType()
5948 .getCanonicalType();
5949 const Type *Ty = QType.getTypePtrOrNull();
5950 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5951 !Ty->isPointerType())) {
5952 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5953 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5954 bool IsDecl =
5955 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5956 Diag(VD->getLocation(),
5957 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5958 << VD;
5959 continue;
5960 }
5961
5962 // OpenMP [2.8.1, simd construct, Restrictions]
5963 // A list-item cannot appear in more than one aligned clause.
5964 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5965 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5966 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5967 << getOpenMPClauseName(OMPC_aligned);
5968 continue;
5969 }
5970
5971 Vars.push_back(DE);
5972 }
5973
5974 // OpenMP [2.8.1, simd construct, Description]
5975 // The parameter of the aligned clause, alignment, must be a constant
5976 // positive integer expression.
5977 // If no optional parameter is specified, implementation-defined default
5978 // alignments for SIMD instructions on the target platforms are assumed.
5979 if (Alignment != nullptr) {
5980 ExprResult AlignResult =
5981 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5982 if (AlignResult.isInvalid())
5983 return nullptr;
5984 Alignment = AlignResult.get();
5985 }
5986 if (Vars.empty())
5987 return nullptr;
5988
5989 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5990 EndLoc, Vars, Alignment);
5991}
5992
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005993OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5994 SourceLocation StartLoc,
5995 SourceLocation LParenLoc,
5996 SourceLocation EndLoc) {
5997 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00005998 SmallVector<Expr *, 8> SrcExprs;
5999 SmallVector<Expr *, 8> DstExprs;
6000 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006001 for (auto &RefExpr : VarList) {
6002 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6003 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006004 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006005 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006006 SrcExprs.push_back(nullptr);
6007 DstExprs.push_back(nullptr);
6008 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006009 continue;
6010 }
6011
Alexey Bataeved09d242014-05-28 05:53:51 +00006012 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006013 // OpenMP [2.1, C/C++]
6014 // A list item is a variable name.
6015 // OpenMP [2.14.4.1, Restrictions, p.1]
6016 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006017 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006018 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006019 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006020 continue;
6021 }
6022
6023 Decl *D = DE->getDecl();
6024 VarDecl *VD = cast<VarDecl>(D);
6025
6026 QualType Type = VD->getType();
6027 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6028 // It will be analyzed later.
6029 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006030 SrcExprs.push_back(nullptr);
6031 DstExprs.push_back(nullptr);
6032 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006033 continue;
6034 }
6035
6036 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6037 // A list item that appears in a copyin clause must be threadprivate.
6038 if (!DSAStack->isThreadPrivate(VD)) {
6039 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006040 << getOpenMPClauseName(OMPC_copyin)
6041 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006042 continue;
6043 }
6044
6045 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6046 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006047 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006048 // operator for the class type.
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006049 Type = Context.getBaseElementType(Type).getNonReferenceType();
6050 auto *SrcVD = BuildVarDecl(*this, DE->getLocStart(),
6051 Type.getUnqualifiedType(), ".copyin.src");
6052 auto *PseudoSrcExpr = BuildDeclRefExpr(SrcVD, Type.getUnqualifiedType(),
6053 VK_LValue, DE->getExprLoc())
6054 .get();
6055 auto *DstVD = BuildVarDecl(*this, DE->getLocStart(), Type, ".copyin.dst");
6056 auto *PseudoDstExpr =
6057 BuildDeclRefExpr(DstVD, Type, VK_LValue, DE->getExprLoc()).get();
6058 // For arrays generate assignment operation for single element and replace
6059 // it by the original array element in CodeGen.
6060 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6061 PseudoDstExpr, PseudoSrcExpr);
6062 if (AssignmentOp.isInvalid())
6063 continue;
6064 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6065 /*DiscardedValue=*/true);
6066 if (AssignmentOp.isInvalid())
6067 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006068
6069 DSAStack->addDSA(VD, DE, OMPC_copyin);
6070 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006071 SrcExprs.push_back(PseudoSrcExpr);
6072 DstExprs.push_back(PseudoDstExpr);
6073 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006074 }
6075
Alexey Bataeved09d242014-05-28 05:53:51 +00006076 if (Vars.empty())
6077 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006078
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006079 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6080 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006081}
6082
Alexey Bataevbae9a792014-06-27 10:37:06 +00006083OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6084 SourceLocation StartLoc,
6085 SourceLocation LParenLoc,
6086 SourceLocation EndLoc) {
6087 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006088 SmallVector<Expr *, 8> SrcExprs;
6089 SmallVector<Expr *, 8> DstExprs;
6090 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006091 for (auto &RefExpr : VarList) {
6092 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6093 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6094 // It will be analyzed later.
6095 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006096 SrcExprs.push_back(nullptr);
6097 DstExprs.push_back(nullptr);
6098 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006099 continue;
6100 }
6101
6102 SourceLocation ELoc = RefExpr->getExprLoc();
6103 // OpenMP [2.1, C/C++]
6104 // A list item is a variable name.
6105 // OpenMP [2.14.4.1, Restrictions, p.1]
6106 // A list item that appears in a copyin clause must be threadprivate.
6107 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6108 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6109 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6110 continue;
6111 }
6112
6113 Decl *D = DE->getDecl();
6114 VarDecl *VD = cast<VarDecl>(D);
6115
6116 QualType Type = VD->getType();
6117 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6118 // It will be analyzed later.
6119 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006120 SrcExprs.push_back(nullptr);
6121 DstExprs.push_back(nullptr);
6122 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006123 continue;
6124 }
6125
6126 // OpenMP [2.14.4.2, Restrictions, p.2]
6127 // A list item that appears in a copyprivate clause may not appear in a
6128 // private or firstprivate clause on the single construct.
6129 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006130 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006131 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6132 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006133 Diag(ELoc, diag::err_omp_wrong_dsa)
6134 << getOpenMPClauseName(DVar.CKind)
6135 << getOpenMPClauseName(OMPC_copyprivate);
6136 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6137 continue;
6138 }
6139
6140 // OpenMP [2.11.4.2, Restrictions, p.1]
6141 // All list items that appear in a copyprivate clause must be either
6142 // threadprivate or private in the enclosing context.
6143 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006144 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006145 if (DVar.CKind == OMPC_shared) {
6146 Diag(ELoc, diag::err_omp_required_access)
6147 << getOpenMPClauseName(OMPC_copyprivate)
6148 << "threadprivate or private in the enclosing context";
6149 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6150 continue;
6151 }
6152 }
6153 }
6154
6155 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6156 // A variable of class type (or array thereof) that appears in a
6157 // copyin clause requires an accessible, unambiguous copy assignment
6158 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006159 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6160 auto *SrcVD =
6161 BuildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
6162 auto *PseudoSrcExpr =
6163 BuildDeclRefExpr(SrcVD, Type, VK_LValue, DE->getExprLoc()).get();
6164 auto *DstVD =
6165 BuildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
6166 auto *PseudoDstExpr =
6167 BuildDeclRefExpr(DstVD, Type, VK_LValue, DE->getExprLoc()).get();
Alexey Bataeva63048e2015-03-23 06:18:07 +00006168 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6169 PseudoDstExpr, PseudoSrcExpr);
6170 if (AssignmentOp.isInvalid())
6171 continue;
6172 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6173 /*DiscardedValue=*/true);
6174 if (AssignmentOp.isInvalid())
6175 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006176
6177 // No need to mark vars as copyprivate, they are already threadprivate or
6178 // implicitly private.
6179 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006180 SrcExprs.push_back(PseudoSrcExpr);
6181 DstExprs.push_back(PseudoDstExpr);
6182 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006183 }
6184
6185 if (Vars.empty())
6186 return nullptr;
6187
Alexey Bataeva63048e2015-03-23 06:18:07 +00006188 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6189 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006190}
6191
Alexey Bataev6125da92014-07-21 11:26:11 +00006192OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6193 SourceLocation StartLoc,
6194 SourceLocation LParenLoc,
6195 SourceLocation EndLoc) {
6196 if (VarList.empty())
6197 return nullptr;
6198
6199 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6200}
Alexey Bataevdea47612014-07-23 07:46:59 +00006201