blob: 40c8c18a50d79f11059b57bb77cacfca42806266 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000073 Expr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Kelvin Li0bff7af2015-11-23 05:32:03 +000080public:
81 struct MapInfo {
82 Expr *RefExpr;
83 };
84
Alexey Bataev758e55e2013-09-06 18:03:48 +000085private:
86 struct DSAInfo {
87 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000088 Expr *RefExpr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000089 };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000090 typedef llvm::SmallDenseMap<ValueDecl *, DSAInfo, 64> DeclSAMapTy;
91 typedef llvm::SmallDenseMap<ValueDecl *, Expr *, 64> AlignedMapTy;
92 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
93 typedef llvm::SmallDenseMap<ValueDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000094 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
95 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000096
97 struct SharingMapTy {
98 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000099 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000100 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000101 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 OpenMPDirectiveKind Directive;
105 DeclarationNameInfo DirectiveName;
106 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000108 /// \brief first argument (Expr *) contains optional argument of the
109 /// 'ordered' clause, the second one is true if the regions has 'ordered'
110 /// clause, false otherwise.
111 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000112 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000113 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000114 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000115 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000116 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000118 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000120 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000121 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000124 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000125 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000126 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 };
128
129 typedef SmallVector<SharingMapTy, 64> StackTy;
130
131 /// \brief Stack of used declaration and their data-sharing attributes.
132 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133 /// \brief true, if check for DSA must be from parent directive, false, if
134 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000135 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000136 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000138 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139
140 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
141
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000142 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000143
144 /// \brief Checks if the variable is a local for OpenMP region.
145 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000146
Alexey Bataev758e55e2013-09-06 18:03:48 +0000147public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000148 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000149 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
150 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000151
Alexey Bataevaac108a2015-06-23 04:51:00 +0000152 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
153 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000155 bool isForceVarCapturing() const { return ForceCapturing; }
156 void setForceVarCapturing(bool V) { ForceCapturing = V; }
157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000159 Scope *CurScope, SourceLocation Loc) {
160 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
161 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162 }
163
164 void pop() {
165 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
166 Stack.pop_back();
167 }
168
Alexey Bataev28c75412015-12-15 08:19:24 +0000169 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
170 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
171 }
172 const std::pair<OMPCriticalDirective *, llvm::APSInt>
173 getCriticalWithHint(const DeclarationNameInfo &Name) const {
174 auto I = Criticals.find(Name.getAsString());
175 if (I != Criticals.end())
176 return I->second;
177 return std::make_pair(nullptr, llvm::APSInt());
178 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000179 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000180 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000182 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000183
Alexey Bataev9c821032015-04-30 04:23:23 +0000184 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000185 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000186 /// \brief Check if the specified variable is a loop control variable for
187 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000188 /// \return The index of the loop control variable in the list of associated
189 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000191 /// \brief Check if the specified variable is a loop control variable for
192 /// parent region.
193 /// \return The index of the loop control variable in the list of associated
194 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000195 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000196 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
197 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000198 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000199
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000201 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data sharing attributes from top of the stack for the
204 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000205 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 /// \brief Checks if the specified variables has data-sharing attributes which
209 /// match specified \a CPred predicate in any directive which matches \a DPred
210 /// predicate.
211 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000212 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000213 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000214 /// \brief Checks if the specified variables has data-sharing attributes which
215 /// match specified \a CPred predicate in any innermost directive which
216 /// matches \a DPred predicate.
217 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000218 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
219 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000220 /// \brief Checks if the specified variables has explicit data-sharing
221 /// attributes which match specified \a CPred predicate at the specified
222 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000223 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000224 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
225 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000226
227 /// \brief Returns true if the directive at level \Level matches in the
228 /// specified \a DPred predicate.
229 bool hasExplicitDirective(
230 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
231 unsigned Level);
232
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000233 /// \brief Finds a directive which matches specified \a DPred predicate.
234 template <class NamedDirectivesPredicate>
235 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 /// \brief Returns currently analyzed directive.
238 OpenMPDirectiveKind getCurrentDirective() const {
239 return Stack.back().Directive;
240 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000241 /// \brief Returns parent directive.
242 OpenMPDirectiveKind getParentDirective() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].Directive;
245 return OMPD_unknown;
246 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000247 /// \brief Return the directive associated with the provided scope.
248 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249
250 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000251 void setDefaultDSANone(SourceLocation Loc) {
252 Stack.back().DefaultAttr = DSA_none;
253 Stack.back().DefaultAttrLoc = Loc;
254 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000256 void setDefaultDSAShared(SourceLocation Loc) {
257 Stack.back().DefaultAttr = DSA_shared;
258 Stack.back().DefaultAttrLoc = Loc;
259 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260
261 DefaultDataSharingAttributes getDefaultDSA() const {
262 return Stack.back().DefaultAttr;
263 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000264 SourceLocation getDefaultDSALocation() const {
265 return Stack.back().DefaultAttrLoc;
266 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000270 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 }
273
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000274 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000275 void setOrderedRegion(bool IsOrdered, Expr *Param) {
276 Stack.back().OrderedRegion.setInt(IsOrdered);
277 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000278 }
279 /// \brief Returns true, if parent region is ordered (has associated
280 /// 'ordered' clause), false - otherwise.
281 bool isParentOrderedRegion() const {
282 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000284 return false;
285 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 /// \brief Returns optional parameter for the ordered region.
287 Expr *getParentOrderedRegionParam() const {
288 if (Stack.size() > 2)
289 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
290 return nullptr;
291 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000292 /// \brief Marks current region as nowait (it has a 'nowait' clause).
293 void setNowaitRegion(bool IsNowait = true) {
294 Stack.back().NowaitRegion = IsNowait;
295 }
296 /// \brief Returns true, if parent region is nowait (has associated
297 /// 'nowait' clause), false - otherwise.
298 bool isParentNowaitRegion() const {
299 if (Stack.size() > 2)
300 return Stack[Stack.size() - 2].NowaitRegion;
301 return false;
302 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000303 /// \brief Marks parent region as cancel region.
304 void setParentCancelRegion(bool Cancel = true) {
305 if (Stack.size() > 2)
306 Stack[Stack.size() - 2].CancelRegion =
307 Stack[Stack.size() - 2].CancelRegion || Cancel;
308 }
309 /// \brief Return true if current region has inner cancel construct.
310 bool isCancelRegion() const {
311 return Stack.back().CancelRegion;
312 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000313
Alexey Bataev9c821032015-04-30 04:23:23 +0000314 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000315 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318
Alexey Bataev13314bf2014-10-09 04:18:56 +0000319 /// \brief Marks current target region as one with closely nested teams
320 /// region.
321 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
322 if (Stack.size() > 2)
323 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
324 }
325 /// \brief Returns true, if current region has closely nested teams region.
326 bool hasInnerTeamsRegion() const {
327 return getInnerTeamsRegionLoc().isValid();
328 }
329 /// \brief Returns location of the nested teams region (if any).
330 SourceLocation getInnerTeamsRegionLoc() const {
331 if (Stack.size() > 1)
332 return Stack.back().InnerTeamsRegionLoc;
333 return SourceLocation();
334 }
335
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000336 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000338 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000339
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000340 MapInfo getMapInfoForVar(ValueDecl *VD) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000341 MapInfo VarMI = {0};
342 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
343 if (Stack[Cnt].MappedDecls.count(VD)) {
344 VarMI = Stack[Cnt].MappedDecls[VD];
345 break;
346 }
347 }
348 return VarMI;
349 }
350
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000351 void addMapInfoForVar(ValueDecl *VD, MapInfo MI) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000352 if (Stack.size() > 1) {
353 Stack.back().MappedDecls[VD] = MI;
354 }
355 }
356
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000357 MapInfo IsMappedInCurrentRegion(ValueDecl *VD) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000358 assert(Stack.size() > 1 && "Target level is 0");
359 MapInfo VarMI = {0};
360 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
361 VarMI = Stack.back().MappedDecls[VD];
362 }
363 return VarMI;
364 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000365};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000366bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
367 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000368 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000369 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000370}
Alexey Bataeved09d242014-05-28 05:53:51 +0000371} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000372
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000373static ValueDecl *getCanonicalDecl(ValueDecl *D) {
374 auto *VD = dyn_cast<VarDecl>(D);
375 auto *FD = dyn_cast<FieldDecl>(D);
376 if (VD != nullptr) {
377 VD = VD->getCanonicalDecl();
378 D = VD;
379 } else {
380 assert(FD);
381 FD = FD->getCanonicalDecl();
382 D = FD;
383 }
384 return D;
385}
386
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000388 ValueDecl *D) {
389 D = getCanonicalDecl(D);
390 auto *VD = dyn_cast<VarDecl>(D);
391 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000393 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000394 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
395 // in a region but not in construct]
396 // File-scope or namespace-scope variables referenced in called routines
397 // in the region are shared unless they appear in a threadprivate
398 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000399 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000400 DVar.CKind = OMPC_shared;
401
402 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
403 // in a region but not in construct]
404 // Variables with static storage duration that are declared in called
405 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000406 if (VD && VD->hasGlobalStorage())
407 DVar.CKind = OMPC_shared;
408
409 // Non-static data members are shared by default.
410 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000411 DVar.CKind = OMPC_shared;
412
Alexey Bataev758e55e2013-09-06 18:03:48 +0000413 return DVar;
414 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000415
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000417 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
418 // in a Construct, C/C++, predetermined, p.1]
419 // Variables with automatic storage duration that are declared in a scope
420 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000421 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
422 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000423 DVar.CKind = OMPC_private;
424 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000425 }
426
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 // Explicitly specified attributes and local variables with predetermined
428 // attributes.
429 if (Iter->SharingMap.count(D)) {
430 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
431 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000432 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 return DVar;
434 }
435
436 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
437 // in a Construct, C/C++, implicitly determined, p.1]
438 // In a parallel or task construct, the data-sharing attributes of these
439 // variables are determined by the default clause, if present.
440 switch (Iter->DefaultAttr) {
441 case DSA_shared:
442 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000443 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444 return DVar;
445 case DSA_none:
446 return DVar;
447 case DSA_unspecified:
448 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
449 // in a Construct, implicitly determined, p.2]
450 // In a parallel construct, if no default clause is present, these
451 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000452 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000453 if (isOpenMPParallelDirective(DVar.DKind) ||
454 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455 DVar.CKind = OMPC_shared;
456 return DVar;
457 }
458
459 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
460 // in a Construct, implicitly determined, p.4]
461 // In a task construct, if no default clause is present, a variable that in
462 // the enclosing context is determined to be shared by all implicit tasks
463 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000464 if (DVar.DKind == OMPD_task) {
465 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000466 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000467 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000468 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
469 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 // in a Construct, implicitly determined, p.6]
471 // In a task construct, if no default clause is present, a variable
472 // whose data-sharing attribute is not determined by the rules above is
473 // firstprivate.
474 DVarTemp = getDSA(I, D);
475 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000476 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000478 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000479 return DVar;
480 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000481 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000482 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000483 }
484 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000486 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 return DVar;
488 }
489 }
490 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
491 // in a Construct, implicitly determined, p.3]
492 // For constructs other than task, if no default clause is present, these
493 // variables inherit their data-sharing attributes from the enclosing
494 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000495 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496}
497
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000498Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000499 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000500 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000501 auto It = Stack.back().AlignedMap.find(D);
502 if (It == Stack.back().AlignedMap.end()) {
503 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
504 Stack.back().AlignedMap[D] = NewDE;
505 return nullptr;
506 } else {
507 assert(It->second && "Unexpected nullptr expr in the aligned map");
508 return It->second;
509 }
510 return nullptr;
511}
512
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000513void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000514 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000515 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000516 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000517}
518
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000519unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000520 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000521 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000522 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
523}
524
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000526 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000527 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000528 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
529 ? Stack[Stack.size() - 2].LCVMap[D]
530 : 0;
531}
532
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000534 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
535 if (Stack[Stack.size() - 2].LCVMap.size() < I)
536 return nullptr;
537 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
538 if (Pair.second == I)
539 return Pair.first;
540 }
541 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000542}
543
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000544void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A) {
545 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000546 if (A == OMPC_threadprivate) {
547 Stack[0].SharingMap[D].Attributes = A;
548 Stack[0].SharingMap[D].RefExpr = E;
549 } else {
550 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
551 Stack.back().SharingMap[D].Attributes = A;
552 Stack.back().SharingMap[D].RefExpr = E;
553 }
554}
555
Alexey Bataeved09d242014-05-28 05:53:51 +0000556bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000557 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000558 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000559 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000560 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000561 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000562 ++I;
563 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000564 if (I == E)
565 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000566 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000567 Scope *CurScope = getCurScope();
568 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000569 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000570 }
571 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000574}
575
Alexey Bataev39f915b82015-05-08 10:41:21 +0000576/// \brief Build a variable declaration for OpenMP loop iteration variable.
577static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000578 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000579 DeclContext *DC = SemaRef.CurContext;
580 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
581 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
582 VarDecl *Decl =
583 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000584 if (Attrs) {
585 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
586 I != E; ++I)
587 Decl->addAttr(*I);
588 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000589 Decl->setImplicit();
590 return Decl;
591}
592
593static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
594 SourceLocation Loc,
595 bool RefersToCapture = false) {
596 D->setReferenced();
597 D->markUsed(S.Context);
598 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
599 SourceLocation(), D, RefersToCapture, Loc, Ty,
600 VK_LValue);
601}
602
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000603DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
604 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000605 DSAVarData DVar;
606
607 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
608 // in a Construct, C/C++, predetermined, p.1]
609 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000610 auto *VD = dyn_cast<VarDecl>(D);
611 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
612 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000613 SemaRef.getLangOpts().OpenMPUseTLS &&
614 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000615 (VD && VD->getStorageClass() == SC_Register &&
616 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
617 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000618 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000619 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620 }
621 if (Stack[0].SharingMap.count(D)) {
622 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
623 DVar.CKind = OMPC_threadprivate;
624 return DVar;
625 }
626
627 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000628 // in a Construct, C/C++, predetermined, p.4]
629 // Static data members are shared.
630 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
631 // in a Construct, C/C++, predetermined, p.7]
632 // Variables with static storage duration that are declared in a scope
633 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000634 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000635 DSAVarData DVarTemp =
636 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
637 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000638 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000639
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000640 DVar.CKind = OMPC_shared;
641 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000642 }
643
644 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000645 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
646 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000647 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
648 // in a Construct, C/C++, predetermined, p.6]
649 // Variables with const qualified type having no mutable member are
650 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000651 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000652 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000653 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
654 if (auto *CTD = CTSD->getSpecializedTemplate())
655 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000656 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000657 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000658 // Variables with const-qualified type having no mutable member may be
659 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000660 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
661 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000662 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
663 return DVar;
664
Alexey Bataev758e55e2013-09-06 18:03:48 +0000665 DVar.CKind = OMPC_shared;
666 return DVar;
667 }
668
Alexey Bataev758e55e2013-09-06 18:03:48 +0000669 // Explicitly specified attributes and local variables with predetermined
670 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000671 auto StartI = std::next(Stack.rbegin());
672 auto EndI = std::prev(Stack.rend());
673 if (FromParent && StartI != EndI) {
674 StartI = std::next(StartI);
675 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000676 auto I = std::prev(StartI);
677 if (I->SharingMap.count(D)) {
678 DVar.RefExpr = I->SharingMap[D].RefExpr;
679 DVar.CKind = I->SharingMap[D].Attributes;
680 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 }
682
683 return DVar;
684}
685
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000686DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
687 bool FromParent) {
688 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000689 auto StartI = Stack.rbegin();
690 auto EndI = std::prev(Stack.rend());
691 if (FromParent && StartI != EndI) {
692 StartI = std::next(StartI);
693 }
694 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000695}
696
Alexey Bataevf29276e2014-06-18 04:14:57 +0000697template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000698DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000699 DirectivesPredicate DPred,
700 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000701 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000702 auto StartI = std::next(Stack.rbegin());
703 auto EndI = std::prev(Stack.rend());
704 if (FromParent && StartI != EndI) {
705 StartI = std::next(StartI);
706 }
707 for (auto I = StartI, EE = EndI; I != EE; ++I) {
708 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000709 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000710 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000711 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000712 return DVar;
713 }
714 return DSAVarData();
715}
716
Alexey Bataevf29276e2014-06-18 04:14:57 +0000717template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000718DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000719DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000720 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000721 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000722 auto StartI = std::next(Stack.rbegin());
723 auto EndI = std::prev(Stack.rend());
724 if (FromParent && StartI != EndI) {
725 StartI = std::next(StartI);
726 }
727 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000728 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000729 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000730 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000731 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000732 return DVar;
733 return DSAVarData();
734 }
735 return DSAVarData();
736}
737
Alexey Bataevaac108a2015-06-23 04:51:00 +0000738bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000739 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000740 unsigned Level) {
741 if (CPred(ClauseKindMode))
742 return true;
743 if (isClauseParsingMode())
744 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000745 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000746 auto StartI = Stack.rbegin();
747 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000748 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000749 return false;
750 std::advance(StartI, Level);
751 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
752 CPred(StartI->SharingMap[D].Attributes);
753}
754
Samuel Antao4be30e92015-10-02 17:14:03 +0000755bool DSAStackTy::hasExplicitDirective(
756 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
757 unsigned Level) {
758 if (isClauseParsingMode())
759 ++Level;
760 auto StartI = Stack.rbegin();
761 auto EndI = std::prev(Stack.rend());
762 if (std::distance(StartI, EndI) <= (int)Level)
763 return false;
764 std::advance(StartI, Level);
765 return DPred(StartI->Directive);
766}
767
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000768template <class NamedDirectivesPredicate>
769bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
770 auto StartI = std::next(Stack.rbegin());
771 auto EndI = std::prev(Stack.rend());
772 if (FromParent && StartI != EndI) {
773 StartI = std::next(StartI);
774 }
775 for (auto I = StartI, EE = EndI; I != EE; ++I) {
776 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
777 return true;
778 }
779 return false;
780}
781
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000782OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
783 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
784 if (I->CurScope == S)
785 return I->Directive;
786 return OMPD_unknown;
787}
788
Alexey Bataev758e55e2013-09-06 18:03:48 +0000789void Sema::InitDataSharingAttributesStack() {
790 VarDataSharingAttributesStack = new DSAStackTy(*this);
791}
792
793#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
794
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000795bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000796 const CapturedRegionScopeInfo *RSI) {
797 assert(LangOpts.OpenMP && "OpenMP is not allowed");
798
799 auto &Ctx = getASTContext();
800 bool IsByRef = true;
801
802 // Find the directive that is associated with the provided scope.
803 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000804 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000805
806 if (isOpenMPTargetDirective(DKind)) {
807 // This table summarizes how a given variable should be passed to the device
808 // given its type and the clauses where it appears. This table is based on
809 // the description in OpenMP 4.5 [2.10.4, target Construct] and
810 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
811 //
812 // =========================================================================
813 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
814 // | |(tofrom:scalar)| | pvt | | | |
815 // =========================================================================
816 // | scl | | | | - | | bycopy|
817 // | scl | | - | x | - | - | bycopy|
818 // | scl | | x | - | - | - | null |
819 // | scl | x | | | - | | byref |
820 // | scl | x | - | x | - | - | bycopy|
821 // | scl | x | x | - | - | - | null |
822 // | scl | | - | - | - | x | byref |
823 // | scl | x | - | - | - | x | byref |
824 //
825 // | agg | n.a. | | | - | | byref |
826 // | agg | n.a. | - | x | - | - | byref |
827 // | agg | n.a. | x | - | - | - | null |
828 // | agg | n.a. | - | - | - | x | byref |
829 // | agg | n.a. | - | - | - | x[] | byref |
830 //
831 // | ptr | n.a. | | | - | | bycopy|
832 // | ptr | n.a. | - | x | - | - | bycopy|
833 // | ptr | n.a. | x | - | - | - | null |
834 // | ptr | n.a. | - | - | - | x | byref |
835 // | ptr | n.a. | - | - | - | x[] | bycopy|
836 // | ptr | n.a. | - | - | x | | bycopy|
837 // | ptr | n.a. | - | - | x | x | bycopy|
838 // | ptr | n.a. | - | - | x | x[] | bycopy|
839 // =========================================================================
840 // Legend:
841 // scl - scalar
842 // ptr - pointer
843 // agg - aggregate
844 // x - applies
845 // - - invalid in this combination
846 // [] - mapped with an array section
847 // byref - should be mapped by reference
848 // byval - should be mapped by value
849 // null - initialize a local variable to null on the device
850 //
851 // Observations:
852 // - All scalar declarations that show up in a map clause have to be passed
853 // by reference, because they may have been mapped in the enclosing data
854 // environment.
855 // - If the scalar value does not fit the size of uintptr, it has to be
856 // passed by reference, regardless the result in the table above.
857 // - For pointers mapped by value that have either an implicit map or an
858 // array section, the runtime library may pass the NULL value to the
859 // device instead of the value passed to it by the compiler.
860
861 // FIXME: Right now, only implicit maps are implemented. Properly mapping
862 // values requires having the map, private, and firstprivate clauses SEMA
863 // and parsing in place, which we don't yet.
864
865 if (Ty->isReferenceType())
866 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
867 IsByRef = !Ty->isScalarType();
868 }
869
870 // When passing data by value, we need to make sure it fits the uintptr size
871 // and alignment, because the runtime library only deals with uintptr types.
872 // If it does not fit the uintptr size, we need to pass the data by reference
873 // instead.
874 if (!IsByRef &&
875 (Ctx.getTypeSizeInChars(Ty) >
876 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000877 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000878 IsByRef = true;
879
880 return IsByRef;
881}
882
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000883bool Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000884 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000885 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000886
887 // If we are attempting to capture a global variable in a directive with
888 // 'target' we return true so that this global is also mapped to the device.
889 //
890 // FIXME: If the declaration is enclosed in a 'declare target' directive,
891 // then it should not be captured. Therefore, an extra check has to be
892 // inserted here once support for 'declare target' is added.
893 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000894 auto *VD = dyn_cast<VarDecl>(D);
895 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000896 if (DSAStack->getCurrentDirective() == OMPD_target &&
897 !DSAStack->isClauseParsingMode()) {
898 return true;
899 }
900 if (DSAStack->getCurScope() &&
901 DSAStack->hasDirective(
902 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
903 SourceLocation Loc) -> bool {
904 return isOpenMPTargetDirective(K);
905 },
906 false)) {
907 return true;
908 }
909 }
910
Alexey Bataev48977c32015-08-04 08:10:48 +0000911 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
912 (!DSAStack->isClauseParsingMode() ||
913 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000914 if (DSAStack->isLoopControlVariable(D) ||
915 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000916 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000917 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev9c821032015-04-30 04:23:23 +0000918 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000919 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000920 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
921 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000922 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000923 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000924 return DVarPrivate.CKind != OMPC_unknown;
925 }
926 return false;
927}
928
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000929bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000930 assert(LangOpts.OpenMP && "OpenMP is not allowed");
931 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000932 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000933}
934
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000935bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000936 assert(LangOpts.OpenMP && "OpenMP is not allowed");
937 // Return true if the current level is no longer enclosed in a target region.
938
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000939 auto *VD = dyn_cast<VarDecl>(D);
940 return VD && !VD->hasLocalStorage() &&
Samuel Antao4be30e92015-10-02 17:14:03 +0000941 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
942}
943
Alexey Bataeved09d242014-05-28 05:53:51 +0000944void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000945
946void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
947 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000948 Scope *CurScope, SourceLocation Loc) {
949 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000950 PushExpressionEvaluationContext(PotentiallyEvaluated);
951}
952
Alexey Bataevaac108a2015-06-23 04:51:00 +0000953void Sema::StartOpenMPClause(OpenMPClauseKind K) {
954 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000955}
956
Alexey Bataevaac108a2015-06-23 04:51:00 +0000957void Sema::EndOpenMPClause() {
958 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000959}
960
Alexey Bataev758e55e2013-09-06 18:03:48 +0000961void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000962 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
963 // A variable of class type (or array thereof) that appears in a lastprivate
964 // clause requires an accessible, unambiguous default constructor for the
965 // class type, unless the list item is also specified in a firstprivate
966 // clause.
967 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000968 for (auto *C : D->clauses()) {
969 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
970 SmallVector<Expr *, 8> PrivateCopies;
971 for (auto *DE : Clause->varlists()) {
972 if (DE->isValueDependent() || DE->isTypeDependent()) {
973 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000974 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000975 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000976 DE = DE->IgnoreParens();
977 VarDecl *VD = nullptr;
978 FieldDecl *FD = nullptr;
979 ValueDecl *D;
980 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
981 VD = cast<VarDecl>(DRE->getDecl());
982 D = VD;
983 } else {
984 assert(isa<MemberExpr>(DE));
985 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
986 D = FD;
987 }
988 QualType Type = D->getType().getNonReferenceType();
989 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000990 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000991 // Generate helper private variable and initialize it with the
992 // default value. The address of the original variable is replaced
993 // by the address of the new private variable in CodeGen. This new
994 // variable is not added to IdResolver, so the code in the OpenMP
995 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000996 auto *VDPrivate = buildVarDecl(
997 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000998 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000999 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1000 if (VDPrivate->isInvalidDecl())
1001 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001002 PrivateCopies.push_back(buildDeclRefExpr(
1003 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001004 } else {
1005 // The variable is also a firstprivate, so initialization sequence
1006 // for private copy is generated already.
1007 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001008 }
1009 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001010 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001011 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001012 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001013 }
1014 }
1015 }
1016
Alexey Bataev758e55e2013-09-06 18:03:48 +00001017 DSAStack->pop();
1018 DiscardCleanupsInEvaluationContext();
1019 PopExpressionEvaluationContext();
1020}
1021
Alexander Musman3276a272015-03-21 10:12:56 +00001022static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1023 Expr *NumIterations, Sema &SemaRef,
1024 Scope *S);
1025
Alexey Bataeva769e072013-03-22 06:34:35 +00001026namespace {
1027
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028class VarDeclFilterCCC : public CorrectionCandidateCallback {
1029private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001030 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001031
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001032public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001033 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001034 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035 NamedDecl *ND = Candidate.getCorrectionDecl();
1036 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1037 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001038 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1039 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001040 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001042 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043};
Alexey Bataeved09d242014-05-28 05:53:51 +00001044} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001045
1046ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1047 CXXScopeSpec &ScopeSpec,
1048 const DeclarationNameInfo &Id) {
1049 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1050 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1051
1052 if (Lookup.isAmbiguous())
1053 return ExprError();
1054
1055 VarDecl *VD;
1056 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001057 if (TypoCorrection Corrected = CorrectTypo(
1058 Id, LookupOrdinaryName, CurScope, nullptr,
1059 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001060 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001061 PDiag(Lookup.empty()
1062 ? diag::err_undeclared_var_use_suggest
1063 : diag::err_omp_expected_var_arg_suggest)
1064 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001065 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001066 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001067 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1068 : diag::err_omp_expected_var_arg)
1069 << Id.getName();
1070 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001071 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001072 } else {
1073 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001074 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001075 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1076 return ExprError();
1077 }
1078 }
1079 Lookup.suppressDiagnostics();
1080
1081 // OpenMP [2.9.2, Syntax, C/C++]
1082 // Variables must be file-scope, namespace-scope, or static block-scope.
1083 if (!VD->hasGlobalStorage()) {
1084 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001085 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1086 bool IsDecl =
1087 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001088 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001089 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1090 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001091 return ExprError();
1092 }
1093
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001094 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1095 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001096 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1097 // A threadprivate directive for file-scope variables must appear outside
1098 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001099 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1100 !getCurLexicalContext()->isTranslationUnit()) {
1101 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001102 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1103 bool IsDecl =
1104 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1105 Diag(VD->getLocation(),
1106 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1107 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001108 return ExprError();
1109 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001110 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1111 // A threadprivate directive for static class member variables must appear
1112 // in the class definition, in the same scope in which the member
1113 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001114 if (CanonicalVD->isStaticDataMember() &&
1115 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1116 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001117 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1118 bool IsDecl =
1119 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1120 Diag(VD->getLocation(),
1121 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1122 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001123 return ExprError();
1124 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001125 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1126 // A threadprivate directive for namespace-scope variables must appear
1127 // outside any definition or declaration other than the namespace
1128 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001129 if (CanonicalVD->getDeclContext()->isNamespace() &&
1130 (!getCurLexicalContext()->isFileContext() ||
1131 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1132 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001133 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1134 bool IsDecl =
1135 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1136 Diag(VD->getLocation(),
1137 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1138 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001139 return ExprError();
1140 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001141 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1142 // A threadprivate directive for static block-scope variables must appear
1143 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001144 if (CanonicalVD->isStaticLocal() && CurScope &&
1145 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001146 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001147 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1148 bool IsDecl =
1149 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1150 Diag(VD->getLocation(),
1151 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1152 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001153 return ExprError();
1154 }
1155
1156 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1157 // A threadprivate directive must lexically precede all references to any
1158 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001159 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001161 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001162 return ExprError();
1163 }
1164
1165 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001166 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 return DE;
1168}
1169
Alexey Bataeved09d242014-05-28 05:53:51 +00001170Sema::DeclGroupPtrTy
1171Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1172 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001173 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001174 CurContext->addDecl(D);
1175 return DeclGroupPtrTy::make(DeclGroupRef(D));
1176 }
David Blaikie0403cb12016-01-15 23:43:25 +00001177 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001178}
1179
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001180namespace {
1181class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1182 Sema &SemaRef;
1183
1184public:
1185 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1186 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1187 if (VD->hasLocalStorage()) {
1188 SemaRef.Diag(E->getLocStart(),
1189 diag::err_omp_local_var_in_threadprivate_init)
1190 << E->getSourceRange();
1191 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1192 << VD << VD->getSourceRange();
1193 return true;
1194 }
1195 }
1196 return false;
1197 }
1198 bool VisitStmt(const Stmt *S) {
1199 for (auto Child : S->children()) {
1200 if (Child && Visit(Child))
1201 return true;
1202 }
1203 return false;
1204 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001205 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001206};
1207} // namespace
1208
Alexey Bataeved09d242014-05-28 05:53:51 +00001209OMPThreadPrivateDecl *
1210Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001211 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001212 for (auto &RefExpr : VarList) {
1213 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001214 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1215 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001216
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001217 QualType QType = VD->getType();
1218 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1219 // It will be analyzed later.
1220 Vars.push_back(DE);
1221 continue;
1222 }
1223
Alexey Bataeva769e072013-03-22 06:34:35 +00001224 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1225 // A threadprivate variable must not have an incomplete type.
1226 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001227 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001228 continue;
1229 }
1230
1231 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1232 // A threadprivate variable must not have a reference type.
1233 if (VD->getType()->isReferenceType()) {
1234 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001235 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1236 bool IsDecl =
1237 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1238 Diag(VD->getLocation(),
1239 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1240 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001241 continue;
1242 }
1243
Samuel Antaof8b50122015-07-13 22:54:53 +00001244 // Check if this is a TLS variable. If TLS is not being supported, produce
1245 // the corresponding diagnostic.
1246 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1247 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1248 getLangOpts().OpenMPUseTLS &&
1249 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001250 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1251 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001252 Diag(ILoc, diag::err_omp_var_thread_local)
1253 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001254 bool IsDecl =
1255 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1256 Diag(VD->getLocation(),
1257 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1258 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001259 continue;
1260 }
1261
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001262 // Check if initial value of threadprivate variable reference variable with
1263 // local storage (it is not supported by runtime).
1264 if (auto Init = VD->getAnyInitializer()) {
1265 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001266 if (Checker.Visit(Init))
1267 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001268 }
1269
Alexey Bataeved09d242014-05-28 05:53:51 +00001270 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001271 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001272 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1273 Context, SourceRange(Loc, Loc)));
1274 if (auto *ML = Context.getASTMutationListener())
1275 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001276 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001277 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001278 if (!Vars.empty()) {
1279 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1280 Vars);
1281 D->setAccess(AS_public);
1282 }
1283 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001284}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001285
Alexey Bataev7ff55242014-06-19 09:13:45 +00001286static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001287 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001288 bool IsLoopIterVar = false) {
1289 if (DVar.RefExpr) {
1290 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1291 << getOpenMPClauseName(DVar.CKind);
1292 return;
1293 }
1294 enum {
1295 PDSA_StaticMemberShared,
1296 PDSA_StaticLocalVarShared,
1297 PDSA_LoopIterVarPrivate,
1298 PDSA_LoopIterVarLinear,
1299 PDSA_LoopIterVarLastprivate,
1300 PDSA_ConstVarShared,
1301 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001302 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001303 PDSA_LocalVarPrivate,
1304 PDSA_Implicit
1305 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001306 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001307 auto ReportLoc = D->getLocation();
1308 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001309 if (IsLoopIterVar) {
1310 if (DVar.CKind == OMPC_private)
1311 Reason = PDSA_LoopIterVarPrivate;
1312 else if (DVar.CKind == OMPC_lastprivate)
1313 Reason = PDSA_LoopIterVarLastprivate;
1314 else
1315 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001316 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1317 Reason = PDSA_TaskVarFirstprivate;
1318 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001319 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001320 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001321 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001322 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001323 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001324 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001325 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001327 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001328 ReportHint = true;
1329 Reason = PDSA_LocalVarPrivate;
1330 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001331 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001332 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001333 << Reason << ReportHint
1334 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1335 } else if (DVar.ImplicitDSALoc.isValid()) {
1336 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1337 << getOpenMPClauseName(DVar.CKind);
1338 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001339}
1340
Alexey Bataev758e55e2013-09-06 18:03:48 +00001341namespace {
1342class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1343 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001344 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001345 bool ErrorFound;
1346 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001347 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001348 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001349
Alexey Bataev758e55e2013-09-06 18:03:48 +00001350public:
1351 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001352 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001353 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001354 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1355 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001356
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001357 auto DVar = Stack->getTopDSA(VD, false);
1358 // Check if the variable has explicit DSA set and stop analysis if it so.
1359 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001361 auto ELoc = E->getExprLoc();
1362 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363 // The default(none) clause requires that each variable that is referenced
1364 // in the construct, and does not have a predetermined data-sharing
1365 // attribute, must have its data-sharing attribute explicitly determined
1366 // by being listed in a data-sharing attribute clause.
1367 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001368 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001369 VarsWithInheritedDSA.count(VD) == 0) {
1370 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001371 return;
1372 }
1373
1374 // OpenMP [2.9.3.6, Restrictions, p.2]
1375 // A list item that appears in a reduction clause of the innermost
1376 // enclosing worksharing or parallel construct may not be accessed in an
1377 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001378 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001379 [](OpenMPDirectiveKind K) -> bool {
1380 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001381 isOpenMPWorksharingDirective(K) ||
1382 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001383 },
1384 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001385 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1386 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001387 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1388 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001389 return;
1390 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001391
1392 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001393 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001394 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001395 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001396 }
1397 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001398 void VisitMemberExpr(MemberExpr *E) {
1399 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1400 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1401 auto DVar = Stack->getTopDSA(FD, false);
1402 // Check if the variable has explicit DSA set and stop analysis if it
1403 // so.
1404 if (DVar.RefExpr)
1405 return;
1406
1407 auto ELoc = E->getExprLoc();
1408 auto DKind = Stack->getCurrentDirective();
1409 // OpenMP [2.9.3.6, Restrictions, p.2]
1410 // A list item that appears in a reduction clause of the innermost
1411 // enclosing worksharing or parallel construct may not be accessed in
1412 // an
1413 // explicit task.
1414 DVar =
1415 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1416 [](OpenMPDirectiveKind K) -> bool {
1417 return isOpenMPParallelDirective(K) ||
1418 isOpenMPWorksharingDirective(K) ||
1419 isOpenMPTeamsDirective(K);
1420 },
1421 false);
1422 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1423 ErrorFound = true;
1424 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1425 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1426 return;
1427 }
1428
1429 // Define implicit data-sharing attributes for task.
1430 DVar = Stack->getImplicitDSA(FD, false);
1431 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1432 ImplicitFirstprivate.push_back(E);
1433 }
1434 }
1435 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001436 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001437 for (auto *C : S->clauses()) {
1438 // Skip analysis of arguments of implicitly defined firstprivate clause
1439 // for task directives.
1440 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1441 for (auto *CC : C->children()) {
1442 if (CC)
1443 Visit(CC);
1444 }
1445 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001446 }
1447 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001448 for (auto *C : S->children()) {
1449 if (C && !isa<OMPExecutableDirective>(C))
1450 Visit(C);
1451 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001452 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453
1454 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001455 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001456 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001457 return VarsWithInheritedDSA;
1458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459
Alexey Bataev7ff55242014-06-19 09:13:45 +00001460 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1461 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462};
Alexey Bataeved09d242014-05-28 05:53:51 +00001463} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001464
Alexey Bataevbae9a792014-06-27 10:37:06 +00001465void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001466 switch (DKind) {
1467 case OMPD_parallel: {
1468 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001469 QualType KmpInt32PtrTy =
1470 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001471 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001472 std::make_pair(".global_tid.", KmpInt32PtrTy),
1473 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1474 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001475 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001476 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1477 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001478 break;
1479 }
1480 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001481 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001482 std::make_pair(StringRef(), QualType()) // __context with shared vars
1483 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001484 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1485 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001486 break;
1487 }
1488 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001489 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001490 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001491 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001492 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1493 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001494 break;
1495 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001496 case OMPD_for_simd: {
1497 Sema::CapturedParamNameType Params[] = {
1498 std::make_pair(StringRef(), QualType()) // __context with shared vars
1499 };
1500 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1501 Params);
1502 break;
1503 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001504 case OMPD_sections: {
1505 Sema::CapturedParamNameType Params[] = {
1506 std::make_pair(StringRef(), QualType()) // __context with shared vars
1507 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001508 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1509 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001510 break;
1511 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001512 case OMPD_section: {
1513 Sema::CapturedParamNameType Params[] = {
1514 std::make_pair(StringRef(), QualType()) // __context with shared vars
1515 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001516 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1517 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001518 break;
1519 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001520 case OMPD_single: {
1521 Sema::CapturedParamNameType Params[] = {
1522 std::make_pair(StringRef(), QualType()) // __context with shared vars
1523 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001524 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1525 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001526 break;
1527 }
Alexander Musman80c22892014-07-17 08:54:58 +00001528 case OMPD_master: {
1529 Sema::CapturedParamNameType Params[] = {
1530 std::make_pair(StringRef(), QualType()) // __context with shared vars
1531 };
1532 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1533 Params);
1534 break;
1535 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001536 case OMPD_critical: {
1537 Sema::CapturedParamNameType Params[] = {
1538 std::make_pair(StringRef(), QualType()) // __context with shared vars
1539 };
1540 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1541 Params);
1542 break;
1543 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001544 case OMPD_parallel_for: {
1545 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001546 QualType KmpInt32PtrTy =
1547 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001548 Sema::CapturedParamNameType Params[] = {
1549 std::make_pair(".global_tid.", KmpInt32PtrTy),
1550 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1551 std::make_pair(StringRef(), QualType()) // __context with shared vars
1552 };
1553 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1554 Params);
1555 break;
1556 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001557 case OMPD_parallel_for_simd: {
1558 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001559 QualType KmpInt32PtrTy =
1560 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001561 Sema::CapturedParamNameType Params[] = {
1562 std::make_pair(".global_tid.", KmpInt32PtrTy),
1563 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1564 std::make_pair(StringRef(), QualType()) // __context with shared vars
1565 };
1566 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1567 Params);
1568 break;
1569 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001570 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001571 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001572 QualType KmpInt32PtrTy =
1573 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001574 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001575 std::make_pair(".global_tid.", KmpInt32PtrTy),
1576 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001577 std::make_pair(StringRef(), QualType()) // __context with shared vars
1578 };
1579 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1580 Params);
1581 break;
1582 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001583 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001584 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001585 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1586 FunctionProtoType::ExtProtoInfo EPI;
1587 EPI.Variadic = true;
1588 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001589 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001590 std::make_pair(".global_tid.", KmpInt32Ty),
1591 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001592 std::make_pair(".privates.",
1593 Context.VoidPtrTy.withConst().withRestrict()),
1594 std::make_pair(
1595 ".copy_fn.",
1596 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001597 std::make_pair(StringRef(), QualType()) // __context with shared vars
1598 };
1599 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1600 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001601 // Mark this captured region as inlined, because we don't use outlined
1602 // function directly.
1603 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1604 AlwaysInlineAttr::CreateImplicit(
1605 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001606 break;
1607 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001608 case OMPD_ordered: {
1609 Sema::CapturedParamNameType Params[] = {
1610 std::make_pair(StringRef(), QualType()) // __context with shared vars
1611 };
1612 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1613 Params);
1614 break;
1615 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001616 case OMPD_atomic: {
1617 Sema::CapturedParamNameType Params[] = {
1618 std::make_pair(StringRef(), QualType()) // __context with shared vars
1619 };
1620 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1621 Params);
1622 break;
1623 }
Michael Wong65f367f2015-07-21 13:44:28 +00001624 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001625 case OMPD_target: {
1626 Sema::CapturedParamNameType Params[] = {
1627 std::make_pair(StringRef(), QualType()) // __context with shared vars
1628 };
1629 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1630 Params);
1631 break;
1632 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001633 case OMPD_teams: {
1634 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001635 QualType KmpInt32PtrTy =
1636 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001637 Sema::CapturedParamNameType Params[] = {
1638 std::make_pair(".global_tid.", KmpInt32PtrTy),
1639 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1640 std::make_pair(StringRef(), QualType()) // __context with shared vars
1641 };
1642 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1643 Params);
1644 break;
1645 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001646 case OMPD_taskgroup: {
1647 Sema::CapturedParamNameType Params[] = {
1648 std::make_pair(StringRef(), QualType()) // __context with shared vars
1649 };
1650 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1651 Params);
1652 break;
1653 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001654 case OMPD_taskloop: {
1655 Sema::CapturedParamNameType Params[] = {
1656 std::make_pair(StringRef(), QualType()) // __context with shared vars
1657 };
1658 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1659 Params);
1660 break;
1661 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001662 case OMPD_taskloop_simd: {
1663 Sema::CapturedParamNameType Params[] = {
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001670 case OMPD_distribute: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001678 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001679 case OMPD_taskyield:
1680 case OMPD_barrier:
1681 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001682 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001683 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001684 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001685 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001686 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001687 llvm_unreachable("OpenMP Directive is not allowed");
1688 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001689 llvm_unreachable("Unknown OpenMP directive");
1690 }
1691}
1692
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001693StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1694 ArrayRef<OMPClause *> Clauses) {
1695 if (!S.isUsable()) {
1696 ActOnCapturedRegionError();
1697 return StmtError();
1698 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001699
1700 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001701 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001702 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001703 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001704 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001705 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001706 Clause->getClauseKind() == OMPC_copyprivate ||
1707 (getLangOpts().OpenMPUseTLS &&
1708 getASTContext().getTargetInfo().isTLSSupported() &&
1709 Clause->getClauseKind() == OMPC_copyin)) {
1710 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001711 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001712 for (auto *VarRef : Clause->children()) {
1713 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001714 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001715 }
1716 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001717 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001718 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1719 Clause->getClauseKind() == OMPC_schedule) {
1720 // Mark all variables in private list clauses as used in inner region.
1721 // Required for proper codegen of combined directives.
1722 // TODO: add processing for other clauses.
1723 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001724 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1725 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001726 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001727 if (Clause->getClauseKind() == OMPC_schedule)
1728 SC = cast<OMPScheduleClause>(Clause);
1729 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001730 OC = cast<OMPOrderedClause>(Clause);
1731 else if (Clause->getClauseKind() == OMPC_linear)
1732 LCs.push_back(cast<OMPLinearClause>(Clause));
1733 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001734 bool ErrorFound = false;
1735 // OpenMP, 2.7.1 Loop Construct, Restrictions
1736 // The nonmonotonic modifier cannot be specified if an ordered clause is
1737 // specified.
1738 if (SC &&
1739 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1740 SC->getSecondScheduleModifier() ==
1741 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1742 OC) {
1743 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1744 ? SC->getFirstScheduleModifierLoc()
1745 : SC->getSecondScheduleModifierLoc(),
1746 diag::err_omp_schedule_nonmonotonic_ordered)
1747 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1748 ErrorFound = true;
1749 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001750 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1751 for (auto *C : LCs) {
1752 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1753 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1754 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001755 ErrorFound = true;
1756 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001757 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1758 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1759 OC->getNumForLoops()) {
1760 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1761 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1762 ErrorFound = true;
1763 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001764 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001765 ActOnCapturedRegionError();
1766 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001767 }
1768 return ActOnCapturedRegionEnd(S.get());
1769}
1770
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001771static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1772 OpenMPDirectiveKind CurrentRegion,
1773 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001774 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001775 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001776 // Allowed nesting of constructs
1777 // +------------------+-----------------+------------------------------------+
1778 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1779 // +------------------+-----------------+------------------------------------+
1780 // | parallel | parallel | * |
1781 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001782 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001783 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001784 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001785 // | parallel | simd | * |
1786 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001787 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001788 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001789 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001790 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001791 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001792 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001793 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001794 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001795 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001796 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001797 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001798 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001799 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001800 // | parallel | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001801 // | parallel | target enter | * |
1802 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001803 // | parallel | target exit | * |
1804 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001805 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001806 // | parallel | cancellation | |
1807 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001808 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001809 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001810 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001811 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001812 // +------------------+-----------------+------------------------------------+
1813 // | for | parallel | * |
1814 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001815 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001816 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001817 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001818 // | for | simd | * |
1819 // | for | sections | + |
1820 // | for | section | + |
1821 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001822 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001823 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001824 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001825 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001826 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001827 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001828 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001829 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001830 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001831 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001832 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001833 // | for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001834 // | for | target enter | * |
1835 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001836 // | for | target exit | * |
1837 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001838 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001839 // | for | cancellation | |
1840 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001841 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001842 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001843 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001844 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001845 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001846 // | master | parallel | * |
1847 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001848 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001849 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001850 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001851 // | master | simd | * |
1852 // | master | sections | + |
1853 // | master | section | + |
1854 // | master | single | + |
1855 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001856 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001857 // | master |parallel sections| * |
1858 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001859 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001860 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001861 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001862 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001863 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001864 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001865 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001866 // | master | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001867 // | master | target enter | * |
1868 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001869 // | master | target exit | * |
1870 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001871 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001872 // | master | cancellation | |
1873 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001874 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001875 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001876 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001877 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001878 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001879 // | critical | parallel | * |
1880 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001881 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001882 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001883 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001884 // | critical | simd | * |
1885 // | critical | sections | + |
1886 // | critical | section | + |
1887 // | critical | single | + |
1888 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001889 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001890 // | critical |parallel sections| * |
1891 // | critical | task | * |
1892 // | critical | taskyield | * |
1893 // | critical | barrier | + |
1894 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001895 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001896 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001897 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001898 // | critical | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001899 // | critical | target enter | * |
1900 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001901 // | critical | target exit | * |
1902 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001903 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001904 // | critical | cancellation | |
1905 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001906 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001907 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001908 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001909 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001910 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001911 // | simd | parallel | |
1912 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001913 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001914 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001915 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001916 // | simd | simd | |
1917 // | simd | sections | |
1918 // | simd | section | |
1919 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001920 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001921 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001922 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001923 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001924 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001925 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001926 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001927 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001928 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001929 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001930 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001931 // | simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001932 // | simd | target enter | |
1933 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001934 // | simd | target exit | |
1935 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001936 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001937 // | simd | cancellation | |
1938 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001939 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001940 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001941 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001942 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001943 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001944 // | for simd | parallel | |
1945 // | for simd | for | |
1946 // | for simd | for simd | |
1947 // | for simd | master | |
1948 // | for simd | critical | |
1949 // | for simd | simd | |
1950 // | for simd | sections | |
1951 // | for simd | section | |
1952 // | for simd | single | |
1953 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001954 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001955 // | for simd |parallel sections| |
1956 // | for simd | task | |
1957 // | for simd | taskyield | |
1958 // | for simd | barrier | |
1959 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001960 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001961 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001962 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001963 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001964 // | for simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001965 // | for simd | target enter | |
1966 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001967 // | for simd | target exit | |
1968 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001969 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001970 // | for simd | cancellation | |
1971 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001972 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001973 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001974 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001975 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001976 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001977 // | parallel for simd| parallel | |
1978 // | parallel for simd| for | |
1979 // | parallel for simd| for simd | |
1980 // | parallel for simd| master | |
1981 // | parallel for simd| critical | |
1982 // | parallel for simd| simd | |
1983 // | parallel for simd| sections | |
1984 // | parallel for simd| section | |
1985 // | parallel for simd| single | |
1986 // | parallel for simd| parallel for | |
1987 // | parallel for simd|parallel for simd| |
1988 // | parallel for simd|parallel sections| |
1989 // | parallel for simd| task | |
1990 // | parallel for simd| taskyield | |
1991 // | parallel for simd| barrier | |
1992 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001993 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001994 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001995 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001996 // | parallel for simd| atomic | |
1997 // | parallel for simd| target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001998 // | parallel for simd| target enter | |
1999 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002000 // | parallel for simd| target exit | |
2001 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002002 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002003 // | parallel for simd| cancellation | |
2004 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002005 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002006 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002007 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002008 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002009 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002010 // | sections | parallel | * |
2011 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002012 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002014 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002015 // | sections | simd | * |
2016 // | sections | sections | + |
2017 // | sections | section | * |
2018 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002019 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002020 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002021 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002022 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002023 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002024 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002025 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002026 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002027 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002028 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002029 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002030 // | sections | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002031 // | sections | target enter | * |
2032 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002033 // | sections | target exit | * |
2034 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002035 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002036 // | sections | cancellation | |
2037 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002038 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002039 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002040 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002041 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002042 // +------------------+-----------------+------------------------------------+
2043 // | section | parallel | * |
2044 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002045 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002046 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002047 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002048 // | section | simd | * |
2049 // | section | sections | + |
2050 // | section | section | + |
2051 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002052 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002053 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002054 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002055 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002056 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002057 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002058 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002059 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002060 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002061 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002062 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002063 // | section | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002064 // | section | target enter | * |
2065 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002066 // | section | target exit | * |
2067 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002068 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002069 // | section | cancellation | |
2070 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002071 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002072 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002073 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002074 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002075 // +------------------+-----------------+------------------------------------+
2076 // | single | parallel | * |
2077 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002078 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002079 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002080 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002081 // | single | simd | * |
2082 // | single | sections | + |
2083 // | single | section | + |
2084 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002085 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002086 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002087 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002088 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002089 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002090 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002091 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002092 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002093 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002094 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002095 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002096 // | single | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002097 // | single | target enter | * |
2098 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002099 // | single | target exit | * |
2100 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002101 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002102 // | single | cancellation | |
2103 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002104 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002105 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002106 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002107 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002108 // +------------------+-----------------+------------------------------------+
2109 // | parallel for | parallel | * |
2110 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002111 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002112 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002113 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002114 // | parallel for | simd | * |
2115 // | parallel for | sections | + |
2116 // | parallel for | section | + |
2117 // | parallel for | single | + |
2118 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002119 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002120 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002121 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002122 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002123 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002124 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002125 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002126 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002127 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002128 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002129 // | parallel for | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002130 // | parallel for | target enter | * |
2131 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002132 // | parallel for | target exit | * |
2133 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002134 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002135 // | parallel for | cancellation | |
2136 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002137 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002138 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002139 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002140 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002141 // +------------------+-----------------+------------------------------------+
2142 // | parallel sections| parallel | * |
2143 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002144 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002145 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002146 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002147 // | parallel sections| simd | * |
2148 // | parallel sections| sections | + |
2149 // | parallel sections| section | * |
2150 // | parallel sections| single | + |
2151 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002152 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002153 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002154 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002155 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002156 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002157 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002158 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002159 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002160 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002161 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002162 // | parallel sections| target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002163 // | parallel sections| target enter | * |
2164 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002165 // | parallel sections| target exit | * |
2166 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002167 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002168 // | parallel sections| cancellation | |
2169 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002170 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002171 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002172 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002173 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002174 // +------------------+-----------------+------------------------------------+
2175 // | task | parallel | * |
2176 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002177 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002178 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002179 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002180 // | task | simd | * |
2181 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002182 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002183 // | task | single | + |
2184 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002185 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002186 // | task |parallel sections| * |
2187 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002188 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002189 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002190 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002191 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002192 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002193 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002194 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002195 // | task | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002196 // | task | target enter | * |
2197 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002198 // | task | target exit | * |
2199 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002200 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002201 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002202 // | | point | ! |
2203 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002204 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002205 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002206 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002207 // +------------------+-----------------+------------------------------------+
2208 // | ordered | parallel | * |
2209 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002210 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002211 // | ordered | master | * |
2212 // | ordered | critical | * |
2213 // | ordered | simd | * |
2214 // | ordered | sections | + |
2215 // | ordered | section | + |
2216 // | ordered | single | + |
2217 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002218 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002219 // | ordered |parallel sections| * |
2220 // | ordered | task | * |
2221 // | ordered | taskyield | * |
2222 // | ordered | barrier | + |
2223 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002224 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002225 // | ordered | flush | * |
2226 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002227 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002228 // | ordered | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002229 // | ordered | target enter | * |
2230 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002231 // | ordered | target exit | * |
2232 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002233 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002234 // | ordered | cancellation | |
2235 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002236 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002237 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002238 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002239 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002240 // +------------------+-----------------+------------------------------------+
2241 // | atomic | parallel | |
2242 // | atomic | for | |
2243 // | atomic | for simd | |
2244 // | atomic | master | |
2245 // | atomic | critical | |
2246 // | atomic | simd | |
2247 // | atomic | sections | |
2248 // | atomic | section | |
2249 // | atomic | single | |
2250 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002251 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002252 // | atomic |parallel sections| |
2253 // | atomic | task | |
2254 // | atomic | taskyield | |
2255 // | atomic | barrier | |
2256 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002257 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002258 // | atomic | flush | |
2259 // | atomic | ordered | |
2260 // | atomic | atomic | |
2261 // | atomic | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002262 // | atomic | target enter | |
2263 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002264 // | atomic | target exit | |
2265 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002266 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002267 // | atomic | cancellation | |
2268 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002269 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002270 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002271 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002272 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002273 // +------------------+-----------------+------------------------------------+
2274 // | target | parallel | * |
2275 // | target | for | * |
2276 // | target | for simd | * |
2277 // | target | master | * |
2278 // | target | critical | * |
2279 // | target | simd | * |
2280 // | target | sections | * |
2281 // | target | section | * |
2282 // | target | single | * |
2283 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002284 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002285 // | target |parallel sections| * |
2286 // | target | task | * |
2287 // | target | taskyield | * |
2288 // | target | barrier | * |
2289 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002290 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002291 // | target | flush | * |
2292 // | target | ordered | * |
2293 // | target | atomic | * |
2294 // | target | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002295 // | target | target enter | * |
2296 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002297 // | target | target exit | * |
2298 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002299 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002300 // | target | cancellation | |
2301 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002302 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002303 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002304 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002305 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002306 // +------------------+-----------------+------------------------------------+
2307 // | teams | parallel | * |
2308 // | teams | for | + |
2309 // | teams | for simd | + |
2310 // | teams | master | + |
2311 // | teams | critical | + |
2312 // | teams | simd | + |
2313 // | teams | sections | + |
2314 // | teams | section | + |
2315 // | teams | single | + |
2316 // | teams | parallel for | * |
2317 // | teams |parallel for simd| * |
2318 // | teams |parallel sections| * |
2319 // | teams | task | + |
2320 // | teams | taskyield | + |
2321 // | teams | barrier | + |
2322 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002323 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002324 // | teams | flush | + |
2325 // | teams | ordered | + |
2326 // | teams | atomic | + |
2327 // | teams | target | + |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002328 // | teams | target enter | + |
2329 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002330 // | teams | target exit | + |
2331 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002332 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002333 // | teams | cancellation | |
2334 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002335 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002336 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002337 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002338 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002339 // +------------------+-----------------+------------------------------------+
2340 // | taskloop | parallel | * |
2341 // | taskloop | for | + |
2342 // | taskloop | for simd | + |
2343 // | taskloop | master | + |
2344 // | taskloop | critical | * |
2345 // | taskloop | simd | * |
2346 // | taskloop | sections | + |
2347 // | taskloop | section | + |
2348 // | taskloop | single | + |
2349 // | taskloop | parallel for | * |
2350 // | taskloop |parallel for simd| * |
2351 // | taskloop |parallel sections| * |
2352 // | taskloop | task | * |
2353 // | taskloop | taskyield | * |
2354 // | taskloop | barrier | + |
2355 // | taskloop | taskwait | * |
2356 // | taskloop | taskgroup | * |
2357 // | taskloop | flush | * |
2358 // | taskloop | ordered | + |
2359 // | taskloop | atomic | * |
2360 // | taskloop | target | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002361 // | taskloop | target enter | * |
2362 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002363 // | taskloop | target exit | * |
2364 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002365 // | taskloop | teams | + |
2366 // | taskloop | cancellation | |
2367 // | | point | |
2368 // | taskloop | cancel | |
2369 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002370 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002371 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002372 // | taskloop simd | parallel | |
2373 // | taskloop simd | for | |
2374 // | taskloop simd | for simd | |
2375 // | taskloop simd | master | |
2376 // | taskloop simd | critical | |
2377 // | taskloop simd | simd | |
2378 // | taskloop simd | sections | |
2379 // | taskloop simd | section | |
2380 // | taskloop simd | single | |
2381 // | taskloop simd | parallel for | |
2382 // | taskloop simd |parallel for simd| |
2383 // | taskloop simd |parallel sections| |
2384 // | taskloop simd | task | |
2385 // | taskloop simd | taskyield | |
2386 // | taskloop simd | barrier | |
2387 // | taskloop simd | taskwait | |
2388 // | taskloop simd | taskgroup | |
2389 // | taskloop simd | flush | |
2390 // | taskloop simd | ordered | + (with simd clause) |
2391 // | taskloop simd | atomic | |
2392 // | taskloop simd | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002393 // | taskloop simd | target enter | |
2394 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002395 // | taskloop simd | target exit | |
2396 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002397 // | taskloop simd | teams | |
2398 // | taskloop simd | cancellation | |
2399 // | | point | |
2400 // | taskloop simd | cancel | |
2401 // | taskloop simd | taskloop | |
2402 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002403 // | taskloop simd | distribute | |
2404 // +------------------+-----------------+------------------------------------+
2405 // | distribute | parallel | * |
2406 // | distribute | for | * |
2407 // | distribute | for simd | * |
2408 // | distribute | master | * |
2409 // | distribute | critical | * |
2410 // | distribute | simd | * |
2411 // | distribute | sections | * |
2412 // | distribute | section | * |
2413 // | distribute | single | * |
2414 // | distribute | parallel for | * |
2415 // | distribute |parallel for simd| * |
2416 // | distribute |parallel sections| * |
2417 // | distribute | task | * |
2418 // | distribute | taskyield | * |
2419 // | distribute | barrier | * |
2420 // | distribute | taskwait | * |
2421 // | distribute | taskgroup | * |
2422 // | distribute | flush | * |
2423 // | distribute | ordered | + |
2424 // | distribute | atomic | * |
2425 // | distribute | target | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002426 // | distribute | target enter | |
2427 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002428 // | distribute | target exit | |
2429 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002430 // | distribute | teams | |
2431 // | distribute | cancellation | + |
2432 // | | point | |
2433 // | distribute | cancel | + |
2434 // | distribute | taskloop | * |
2435 // | distribute | taskloop simd | * |
2436 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002437 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002438 if (Stack->getCurScope()) {
2439 auto ParentRegion = Stack->getParentDirective();
2440 bool NestingProhibited = false;
2441 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002442 enum {
2443 NoRecommend,
2444 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002445 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002446 ShouldBeInTargetRegion,
2447 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002448 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002449 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002450 // OpenMP [2.16, Nesting of Regions]
2451 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002452 // OpenMP [2.8.1,simd Construct, Restrictions]
2453 // An ordered construct with the simd clause is the only OpenMP construct
2454 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002455 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2456 return true;
2457 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002458 if (ParentRegion == OMPD_atomic) {
2459 // OpenMP [2.16, Nesting of Regions]
2460 // OpenMP constructs may not be nested inside an atomic region.
2461 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2462 return true;
2463 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002464 if (CurrentRegion == OMPD_section) {
2465 // OpenMP [2.7.2, sections Construct, Restrictions]
2466 // Orphaned section directives are prohibited. That is, the section
2467 // directives must appear within the sections construct and must not be
2468 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002469 if (ParentRegion != OMPD_sections &&
2470 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002471 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2472 << (ParentRegion != OMPD_unknown)
2473 << getOpenMPDirectiveName(ParentRegion);
2474 return true;
2475 }
2476 return false;
2477 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002478 // Allow some constructs to be orphaned (they could be used in functions,
2479 // called from OpenMP regions with the required preconditions).
2480 if (ParentRegion == OMPD_unknown)
2481 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002482 if (CurrentRegion == OMPD_cancellation_point ||
2483 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002484 // OpenMP [2.16, Nesting of Regions]
2485 // A cancellation point construct for which construct-type-clause is
2486 // taskgroup must be nested inside a task construct. A cancellation
2487 // point construct for which construct-type-clause is not taskgroup must
2488 // be closely nested inside an OpenMP construct that matches the type
2489 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002490 // A cancel construct for which construct-type-clause is taskgroup must be
2491 // nested inside a task construct. A cancel construct for which
2492 // construct-type-clause is not taskgroup must be closely nested inside an
2493 // OpenMP construct that matches the type specified in
2494 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002495 NestingProhibited =
2496 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002497 (CancelRegion == OMPD_for &&
2498 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002499 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2500 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002501 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2502 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002503 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002504 // OpenMP [2.16, Nesting of Regions]
2505 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002506 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002507 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002508 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002509 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002510 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2511 // OpenMP [2.16, Nesting of Regions]
2512 // A critical region may not be nested (closely or otherwise) inside a
2513 // critical region with the same name. Note that this restriction is not
2514 // sufficient to prevent deadlock.
2515 SourceLocation PreviousCriticalLoc;
2516 bool DeadLock =
2517 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2518 OpenMPDirectiveKind K,
2519 const DeclarationNameInfo &DNI,
2520 SourceLocation Loc)
2521 ->bool {
2522 if (K == OMPD_critical &&
2523 DNI.getName() == CurrentName.getName()) {
2524 PreviousCriticalLoc = Loc;
2525 return true;
2526 } else
2527 return false;
2528 },
2529 false /* skip top directive */);
2530 if (DeadLock) {
2531 SemaRef.Diag(StartLoc,
2532 diag::err_omp_prohibited_region_critical_same_name)
2533 << CurrentName.getName();
2534 if (PreviousCriticalLoc.isValid())
2535 SemaRef.Diag(PreviousCriticalLoc,
2536 diag::note_omp_previous_critical_region);
2537 return true;
2538 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002539 } else if (CurrentRegion == OMPD_barrier) {
2540 // OpenMP [2.16, Nesting of Regions]
2541 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002542 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002543 NestingProhibited =
2544 isOpenMPWorksharingDirective(ParentRegion) ||
2545 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002546 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002547 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002548 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002549 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002550 // OpenMP [2.16, Nesting of Regions]
2551 // A worksharing region may not be closely nested inside a worksharing,
2552 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002553 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002554 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002555 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002556 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002557 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002558 Recommend = ShouldBeInParallelRegion;
2559 } else if (CurrentRegion == OMPD_ordered) {
2560 // OpenMP [2.16, Nesting of Regions]
2561 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002562 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002563 // An ordered region must be closely nested inside a loop region (or
2564 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002565 // OpenMP [2.8.1,simd Construct, Restrictions]
2566 // An ordered construct with the simd clause is the only OpenMP construct
2567 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002568 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002569 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002570 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002571 !(isOpenMPSimdDirective(ParentRegion) ||
2572 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002573 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002574 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2575 // OpenMP [2.16, Nesting of Regions]
2576 // If specified, a teams construct must be contained within a target
2577 // construct.
2578 NestingProhibited = ParentRegion != OMPD_target;
2579 Recommend = ShouldBeInTargetRegion;
2580 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2581 }
2582 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2583 // OpenMP [2.16, Nesting of Regions]
2584 // distribute, parallel, parallel sections, parallel workshare, and the
2585 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2586 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002587 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2588 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002589 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002590 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002591 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2592 // OpenMP 4.5 [2.17 Nesting of Regions]
2593 // The region associated with the distribute construct must be strictly
2594 // nested inside a teams region
2595 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2596 Recommend = ShouldBeInTeamsRegion;
2597 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002598 if (NestingProhibited) {
2599 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002600 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2601 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002602 return true;
2603 }
2604 }
2605 return false;
2606}
2607
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002608static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2609 ArrayRef<OMPClause *> Clauses,
2610 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2611 bool ErrorFound = false;
2612 unsigned NamedModifiersNumber = 0;
2613 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2614 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002615 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002616 for (const auto *C : Clauses) {
2617 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2618 // At most one if clause without a directive-name-modifier can appear on
2619 // the directive.
2620 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2621 if (FoundNameModifiers[CurNM]) {
2622 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2623 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2624 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2625 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002626 } else if (CurNM != OMPD_unknown) {
2627 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002628 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002629 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002630 FoundNameModifiers[CurNM] = IC;
2631 if (CurNM == OMPD_unknown)
2632 continue;
2633 // Check if the specified name modifier is allowed for the current
2634 // directive.
2635 // At most one if clause with the particular directive-name-modifier can
2636 // appear on the directive.
2637 bool MatchFound = false;
2638 for (auto NM : AllowedNameModifiers) {
2639 if (CurNM == NM) {
2640 MatchFound = true;
2641 break;
2642 }
2643 }
2644 if (!MatchFound) {
2645 S.Diag(IC->getNameModifierLoc(),
2646 diag::err_omp_wrong_if_directive_name_modifier)
2647 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2648 ErrorFound = true;
2649 }
2650 }
2651 }
2652 // If any if clause on the directive includes a directive-name-modifier then
2653 // all if clauses on the directive must include a directive-name-modifier.
2654 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2655 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2656 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2657 diag::err_omp_no_more_if_clause);
2658 } else {
2659 std::string Values;
2660 std::string Sep(", ");
2661 unsigned AllowedCnt = 0;
2662 unsigned TotalAllowedNum =
2663 AllowedNameModifiers.size() - NamedModifiersNumber;
2664 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2665 ++Cnt) {
2666 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2667 if (!FoundNameModifiers[NM]) {
2668 Values += "'";
2669 Values += getOpenMPDirectiveName(NM);
2670 Values += "'";
2671 if (AllowedCnt + 2 == TotalAllowedNum)
2672 Values += " or ";
2673 else if (AllowedCnt + 1 != TotalAllowedNum)
2674 Values += Sep;
2675 ++AllowedCnt;
2676 }
2677 }
2678 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2679 diag::err_omp_unnamed_if_clause)
2680 << (TotalAllowedNum > 1) << Values;
2681 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002682 for (auto Loc : NameModifierLoc) {
2683 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2684 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002685 ErrorFound = true;
2686 }
2687 return ErrorFound;
2688}
2689
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002690StmtResult Sema::ActOnOpenMPExecutableDirective(
2691 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2692 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2693 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002694 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002695 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2696 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002697 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002698
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002699 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002700 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002701 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002702 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002703 if (AStmt) {
2704 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2705
2706 // Check default data sharing attributes for referenced variables.
2707 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2708 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2709 if (DSAChecker.isErrorFound())
2710 return StmtError();
2711 // Generate list of implicitly defined firstprivate variables.
2712 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002713
2714 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2715 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2716 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2717 SourceLocation(), SourceLocation())) {
2718 ClausesWithImplicit.push_back(Implicit);
2719 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2720 DSAChecker.getImplicitFirstprivate().size();
2721 } else
2722 ErrorFound = true;
2723 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002724 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002725
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002726 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002727 switch (Kind) {
2728 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002729 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2730 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002731 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002732 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002733 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002734 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2735 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002736 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002737 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002738 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2739 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002740 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002741 case OMPD_for_simd:
2742 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2743 EndLoc, VarsWithInheritedDSA);
2744 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002745 case OMPD_sections:
2746 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2747 EndLoc);
2748 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002749 case OMPD_section:
2750 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002751 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002752 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2753 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002754 case OMPD_single:
2755 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2756 EndLoc);
2757 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002758 case OMPD_master:
2759 assert(ClausesWithImplicit.empty() &&
2760 "No clauses are allowed for 'omp master' directive");
2761 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2762 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002763 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002764 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2765 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002766 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002767 case OMPD_parallel_for:
2768 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2769 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002770 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002771 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002772 case OMPD_parallel_for_simd:
2773 Res = ActOnOpenMPParallelForSimdDirective(
2774 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002775 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002776 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002777 case OMPD_parallel_sections:
2778 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2779 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002780 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002781 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002782 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002783 Res =
2784 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002785 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002786 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002787 case OMPD_taskyield:
2788 assert(ClausesWithImplicit.empty() &&
2789 "No clauses are allowed for 'omp taskyield' directive");
2790 assert(AStmt == nullptr &&
2791 "No associated statement allowed for 'omp taskyield' directive");
2792 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2793 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002794 case OMPD_barrier:
2795 assert(ClausesWithImplicit.empty() &&
2796 "No clauses are allowed for 'omp barrier' directive");
2797 assert(AStmt == nullptr &&
2798 "No associated statement allowed for 'omp barrier' directive");
2799 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2800 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002801 case OMPD_taskwait:
2802 assert(ClausesWithImplicit.empty() &&
2803 "No clauses are allowed for 'omp taskwait' directive");
2804 assert(AStmt == nullptr &&
2805 "No associated statement allowed for 'omp taskwait' directive");
2806 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2807 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002808 case OMPD_taskgroup:
2809 assert(ClausesWithImplicit.empty() &&
2810 "No clauses are allowed for 'omp taskgroup' directive");
2811 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2812 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002813 case OMPD_flush:
2814 assert(AStmt == nullptr &&
2815 "No associated statement allowed for 'omp flush' directive");
2816 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2817 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002818 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002819 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2820 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002821 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002822 case OMPD_atomic:
2823 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2824 EndLoc);
2825 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002826 case OMPD_teams:
2827 Res =
2828 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2829 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002830 case OMPD_target:
2831 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2832 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002833 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002834 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002835 case OMPD_cancellation_point:
2836 assert(ClausesWithImplicit.empty() &&
2837 "No clauses are allowed for 'omp cancellation point' directive");
2838 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2839 "cancellation point' directive");
2840 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2841 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002842 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002843 assert(AStmt == nullptr &&
2844 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002845 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2846 CancelRegion);
2847 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002848 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002849 case OMPD_target_data:
2850 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2851 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002852 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002853 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002854 case OMPD_target_enter_data:
2855 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2856 EndLoc);
2857 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2858 break;
Samuel Antao72590762016-01-19 20:04:50 +00002859 case OMPD_target_exit_data:
2860 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2861 EndLoc);
2862 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2863 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002864 case OMPD_taskloop:
2865 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2866 EndLoc, VarsWithInheritedDSA);
2867 AllowedNameModifiers.push_back(OMPD_taskloop);
2868 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002869 case OMPD_taskloop_simd:
2870 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2871 EndLoc, VarsWithInheritedDSA);
2872 AllowedNameModifiers.push_back(OMPD_taskloop);
2873 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002874 case OMPD_distribute:
2875 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2876 EndLoc, VarsWithInheritedDSA);
2877 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002878 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002879 llvm_unreachable("OpenMP Directive is not allowed");
2880 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002881 llvm_unreachable("Unknown OpenMP directive");
2882 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002883
Alexey Bataev4acb8592014-07-07 13:01:15 +00002884 for (auto P : VarsWithInheritedDSA) {
2885 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2886 << P.first << P.second->getSourceRange();
2887 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002888 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2889
2890 if (!AllowedNameModifiers.empty())
2891 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2892 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002893
Alexey Bataeved09d242014-05-28 05:53:51 +00002894 if (ErrorFound)
2895 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002896 return Res;
2897}
2898
2899StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2900 Stmt *AStmt,
2901 SourceLocation StartLoc,
2902 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002903 if (!AStmt)
2904 return StmtError();
2905
Alexey Bataev9959db52014-05-06 10:08:46 +00002906 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2907 // 1.2.2 OpenMP Language Terminology
2908 // Structured block - An executable statement with a single entry at the
2909 // top and a single exit at the bottom.
2910 // The point of exit cannot be a branch out of the structured block.
2911 // longjmp() and throw() must not violate the entry/exit criteria.
2912 CS->getCapturedDecl()->setNothrow();
2913
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002914 getCurFunction()->setHasBranchProtectedScope();
2915
Alexey Bataev25e5b442015-09-15 12:52:43 +00002916 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2917 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002918}
2919
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002920namespace {
2921/// \brief Helper class for checking canonical form of the OpenMP loops and
2922/// extracting iteration space of each loop in the loop nest, that will be used
2923/// for IR generation.
2924class OpenMPIterationSpaceChecker {
2925 /// \brief Reference to Sema.
2926 Sema &SemaRef;
2927 /// \brief A location for diagnostics (when there is no some better location).
2928 SourceLocation DefaultLoc;
2929 /// \brief A location for diagnostics (when increment is not compatible).
2930 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002931 /// \brief A source location for referring to loop init later.
2932 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002933 /// \brief A source location for referring to condition later.
2934 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002935 /// \brief A source location for referring to increment later.
2936 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002937 /// \brief Loop variable.
2938 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002939 /// \brief Reference to loop variable.
2940 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002941 /// \brief Lower bound (initializer for the var).
2942 Expr *LB;
2943 /// \brief Upper bound.
2944 Expr *UB;
2945 /// \brief Loop step (increment).
2946 Expr *Step;
2947 /// \brief This flag is true when condition is one of:
2948 /// Var < UB
2949 /// Var <= UB
2950 /// UB > Var
2951 /// UB >= Var
2952 bool TestIsLessOp;
2953 /// \brief This flag is true when condition is strict ( < or > ).
2954 bool TestIsStrictOp;
2955 /// \brief This flag is true when step is subtracted on each iteration.
2956 bool SubtractStep;
2957
2958public:
2959 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2960 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002961 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2962 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002963 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2964 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002965 /// \brief Check init-expr for canonical loop form and save loop counter
2966 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002967 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002968 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2969 /// for less/greater and for strict/non-strict comparison.
2970 bool CheckCond(Expr *S);
2971 /// \brief Check incr-expr for canonical loop form and return true if it
2972 /// does not conform, otherwise save loop step (#Step).
2973 bool CheckInc(Expr *S);
2974 /// \brief Return the loop counter variable.
2975 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002976 /// \brief Return the reference expression to loop counter variable.
2977 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002978 /// \brief Source range of the loop init.
2979 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2980 /// \brief Source range of the loop condition.
2981 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2982 /// \brief Source range of the loop increment.
2983 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2984 /// \brief True if the step should be subtracted.
2985 bool ShouldSubtractStep() const { return SubtractStep; }
2986 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002987 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002988 /// \brief Build the precondition expression for the loops.
2989 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002990 /// \brief Build reference expression to the counter be used for codegen.
2991 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002992 /// \brief Build reference expression to the private counter be used for
2993 /// codegen.
2994 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002995 /// \brief Build initization of the counter be used for codegen.
2996 Expr *BuildCounterInit() const;
2997 /// \brief Build step of the counter be used for codegen.
2998 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002999 /// \brief Return true if any expression is dependent.
3000 bool Dependent() const;
3001
3002private:
3003 /// \brief Check the right-hand side of an assignment in the increment
3004 /// expression.
3005 bool CheckIncRHS(Expr *RHS);
3006 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003007 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003008 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003009 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003010 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003011 /// \brief Helper to set loop increment.
3012 bool SetStep(Expr *NewStep, bool Subtract);
3013};
3014
3015bool OpenMPIterationSpaceChecker::Dependent() const {
3016 if (!Var) {
3017 assert(!LB && !UB && !Step);
3018 return false;
3019 }
3020 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3021 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3022}
3023
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003024template <typename T>
3025static T *getExprAsWritten(T *E) {
3026 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3027 E = ExprTemp->getSubExpr();
3028
3029 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3030 E = MTE->GetTemporaryExpr();
3031
3032 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3033 E = Binder->getSubExpr();
3034
3035 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3036 E = ICE->getSubExprAsWritten();
3037 return E->IgnoreParens();
3038}
3039
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003040bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3041 DeclRefExpr *NewVarRefExpr,
3042 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003043 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003044 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3045 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003046 if (!NewVar || !NewLB)
3047 return true;
3048 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003049 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003050 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3051 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003052 if ((Ctor->isCopyOrMoveConstructor() ||
3053 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3054 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003055 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003056 LB = NewLB;
3057 return false;
3058}
3059
3060bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003061 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003062 // State consistency checking to ensure correct usage.
3063 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3064 !TestIsLessOp && !TestIsStrictOp);
3065 if (!NewUB)
3066 return true;
3067 UB = NewUB;
3068 TestIsLessOp = LessOp;
3069 TestIsStrictOp = StrictOp;
3070 ConditionSrcRange = SR;
3071 ConditionLoc = SL;
3072 return false;
3073}
3074
3075bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3076 // State consistency checking to ensure correct usage.
3077 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3078 if (!NewStep)
3079 return true;
3080 if (!NewStep->isValueDependent()) {
3081 // Check that the step is integer expression.
3082 SourceLocation StepLoc = NewStep->getLocStart();
3083 ExprResult Val =
3084 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3085 if (Val.isInvalid())
3086 return true;
3087 NewStep = Val.get();
3088
3089 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3090 // If test-expr is of form var relational-op b and relational-op is < or
3091 // <= then incr-expr must cause var to increase on each iteration of the
3092 // loop. If test-expr is of form var relational-op b and relational-op is
3093 // > or >= then incr-expr must cause var to decrease on each iteration of
3094 // the loop.
3095 // If test-expr is of form b relational-op var and relational-op is < or
3096 // <= then incr-expr must cause var to decrease on each iteration of the
3097 // loop. If test-expr is of form b relational-op var and relational-op is
3098 // > or >= then incr-expr must cause var to increase on each iteration of
3099 // the loop.
3100 llvm::APSInt Result;
3101 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3102 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3103 bool IsConstNeg =
3104 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003105 bool IsConstPos =
3106 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003107 bool IsConstZero = IsConstant && !Result.getBoolValue();
3108 if (UB && (IsConstZero ||
3109 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003110 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003111 SemaRef.Diag(NewStep->getExprLoc(),
3112 diag::err_omp_loop_incr_not_compatible)
3113 << Var << TestIsLessOp << NewStep->getSourceRange();
3114 SemaRef.Diag(ConditionLoc,
3115 diag::note_omp_loop_cond_requres_compatible_incr)
3116 << TestIsLessOp << ConditionSrcRange;
3117 return true;
3118 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003119 if (TestIsLessOp == Subtract) {
3120 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3121 NewStep).get();
3122 Subtract = !Subtract;
3123 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003124 }
3125
3126 Step = NewStep;
3127 SubtractStep = Subtract;
3128 return false;
3129}
3130
Alexey Bataev9c821032015-04-30 04:23:23 +00003131bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003132 // Check init-expr for canonical loop form and save loop counter
3133 // variable - #Var and its initialization value - #LB.
3134 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3135 // var = lb
3136 // integer-type var = lb
3137 // random-access-iterator-type var = lb
3138 // pointer-type var = lb
3139 //
3140 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003141 if (EmitDiags) {
3142 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3143 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003144 return true;
3145 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003146 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003147 if (Expr *E = dyn_cast<Expr>(S))
3148 S = E->IgnoreParens();
3149 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3150 if (BO->getOpcode() == BO_Assign)
3151 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003152 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003153 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003154 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3155 if (DS->isSingleDecl()) {
3156 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003157 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003158 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003159 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003160 SemaRef.Diag(S->getLocStart(),
3161 diag::ext_omp_loop_not_canonical_init)
3162 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003163 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003164 }
3165 }
3166 }
3167 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3168 if (CE->getOperator() == OO_Equal)
3169 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003170 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3171 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003172
Alexey Bataev9c821032015-04-30 04:23:23 +00003173 if (EmitDiags) {
3174 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3175 << S->getSourceRange();
3176 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003177 return true;
3178}
3179
Alexey Bataev23b69422014-06-18 07:08:49 +00003180/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003181/// variable (which may be the loop variable) if possible.
3182static const VarDecl *GetInitVarDecl(const Expr *E) {
3183 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003184 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003185 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003186 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3187 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003188 if ((Ctor->isCopyOrMoveConstructor() ||
3189 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3190 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003191 E = CE->getArg(0)->IgnoreParenImpCasts();
3192 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3193 if (!DRE)
3194 return nullptr;
3195 return dyn_cast<VarDecl>(DRE->getDecl());
3196}
3197
3198bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3199 // Check test-expr for canonical form, save upper-bound UB, flags for
3200 // less/greater and for strict/non-strict comparison.
3201 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3202 // var relational-op b
3203 // b relational-op var
3204 //
3205 if (!S) {
3206 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3207 return true;
3208 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003209 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003210 SourceLocation CondLoc = S->getLocStart();
3211 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3212 if (BO->isRelationalOp()) {
3213 if (GetInitVarDecl(BO->getLHS()) == Var)
3214 return SetUB(BO->getRHS(),
3215 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3216 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3217 BO->getSourceRange(), BO->getOperatorLoc());
3218 if (GetInitVarDecl(BO->getRHS()) == Var)
3219 return SetUB(BO->getLHS(),
3220 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3221 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3222 BO->getSourceRange(), BO->getOperatorLoc());
3223 }
3224 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3225 if (CE->getNumArgs() == 2) {
3226 auto Op = CE->getOperator();
3227 switch (Op) {
3228 case OO_Greater:
3229 case OO_GreaterEqual:
3230 case OO_Less:
3231 case OO_LessEqual:
3232 if (GetInitVarDecl(CE->getArg(0)) == Var)
3233 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3234 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3235 CE->getOperatorLoc());
3236 if (GetInitVarDecl(CE->getArg(1)) == Var)
3237 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3238 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3239 CE->getOperatorLoc());
3240 break;
3241 default:
3242 break;
3243 }
3244 }
3245 }
3246 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3247 << S->getSourceRange() << Var;
3248 return true;
3249}
3250
3251bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3252 // RHS of canonical loop form increment can be:
3253 // var + incr
3254 // incr + var
3255 // var - incr
3256 //
3257 RHS = RHS->IgnoreParenImpCasts();
3258 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3259 if (BO->isAdditiveOp()) {
3260 bool IsAdd = BO->getOpcode() == BO_Add;
3261 if (GetInitVarDecl(BO->getLHS()) == Var)
3262 return SetStep(BO->getRHS(), !IsAdd);
3263 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3264 return SetStep(BO->getLHS(), false);
3265 }
3266 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3267 bool IsAdd = CE->getOperator() == OO_Plus;
3268 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3269 if (GetInitVarDecl(CE->getArg(0)) == Var)
3270 return SetStep(CE->getArg(1), !IsAdd);
3271 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3272 return SetStep(CE->getArg(0), false);
3273 }
3274 }
3275 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3276 << RHS->getSourceRange() << Var;
3277 return true;
3278}
3279
3280bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3281 // Check incr-expr for canonical loop form and return true if it
3282 // does not conform.
3283 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3284 // ++var
3285 // var++
3286 // --var
3287 // var--
3288 // var += incr
3289 // var -= incr
3290 // var = var + incr
3291 // var = incr + var
3292 // var = var - incr
3293 //
3294 if (!S) {
3295 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3296 return true;
3297 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003298 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003299 S = S->IgnoreParens();
3300 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3301 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3302 return SetStep(
3303 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3304 (UO->isDecrementOp() ? -1 : 1)).get(),
3305 false);
3306 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3307 switch (BO->getOpcode()) {
3308 case BO_AddAssign:
3309 case BO_SubAssign:
3310 if (GetInitVarDecl(BO->getLHS()) == Var)
3311 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3312 break;
3313 case BO_Assign:
3314 if (GetInitVarDecl(BO->getLHS()) == Var)
3315 return CheckIncRHS(BO->getRHS());
3316 break;
3317 default:
3318 break;
3319 }
3320 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3321 switch (CE->getOperator()) {
3322 case OO_PlusPlus:
3323 case OO_MinusMinus:
3324 if (GetInitVarDecl(CE->getArg(0)) == Var)
3325 return SetStep(
3326 SemaRef.ActOnIntegerConstant(
3327 CE->getLocStart(),
3328 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3329 false);
3330 break;
3331 case OO_PlusEqual:
3332 case OO_MinusEqual:
3333 if (GetInitVarDecl(CE->getArg(0)) == Var)
3334 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3335 break;
3336 case OO_Equal:
3337 if (GetInitVarDecl(CE->getArg(0)) == Var)
3338 return CheckIncRHS(CE->getArg(1));
3339 break;
3340 default:
3341 break;
3342 }
3343 }
3344 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3345 << S->getSourceRange() << Var;
3346 return true;
3347}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003348
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003349namespace {
3350// Transform variables declared in GNU statement expressions to new ones to
3351// avoid crash on codegen.
3352class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3353 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3354
3355public:
3356 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3357
3358 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3359 if (auto *VD = cast<VarDecl>(D))
3360 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3361 !isa<ImplicitParamDecl>(D)) {
3362 auto *NewVD = VarDecl::Create(
3363 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3364 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3365 VD->getTypeSourceInfo(), VD->getStorageClass());
3366 NewVD->setTSCSpec(VD->getTSCSpec());
3367 NewVD->setInit(VD->getInit());
3368 NewVD->setInitStyle(VD->getInitStyle());
3369 NewVD->setExceptionVariable(VD->isExceptionVariable());
3370 NewVD->setNRVOVariable(VD->isNRVOVariable());
3371 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3372 NewVD->setConstexpr(VD->isConstexpr());
3373 NewVD->setInitCapture(VD->isInitCapture());
3374 NewVD->setPreviousDeclInSameBlockScope(
3375 VD->isPreviousDeclInSameBlockScope());
3376 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003377 if (VD->hasAttrs())
3378 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003379 transformedLocalDecl(VD, NewVD);
3380 return NewVD;
3381 }
3382 return BaseTransform::TransformDefinition(Loc, D);
3383 }
3384
3385 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3386 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3387 if (E->getDecl() != NewD) {
3388 NewD->setReferenced();
3389 NewD->markUsed(SemaRef.Context);
3390 return DeclRefExpr::Create(
3391 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3392 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3393 E->getNameInfo(), E->getType(), E->getValueKind());
3394 }
3395 return BaseTransform::TransformDeclRefExpr(E);
3396 }
3397};
3398}
3399
Alexander Musmana5f070a2014-10-01 06:03:56 +00003400/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003401Expr *
3402OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3403 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003404 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003405 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003406 auto VarType = Var->getType().getNonReferenceType();
3407 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003408 SemaRef.getLangOpts().CPlusPlus) {
3409 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003410 auto *UBExpr = TestIsLessOp ? UB : LB;
3411 auto *LBExpr = TestIsLessOp ? LB : UB;
3412 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3413 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3414 if (!Upper || !Lower)
3415 return nullptr;
3416 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3417 Sema::AA_Converting,
3418 /*AllowExplicit=*/true)
3419 .get();
3420 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3421 Sema::AA_Converting,
3422 /*AllowExplicit=*/true)
3423 .get();
3424 if (!Upper || !Lower)
3425 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003426
3427 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3428
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003429 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003430 // BuildBinOp already emitted error, this one is to point user to upper
3431 // and lower bound, and to tell what is passed to 'operator-'.
3432 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3433 << Upper->getSourceRange() << Lower->getSourceRange();
3434 return nullptr;
3435 }
3436 }
3437
3438 if (!Diff.isUsable())
3439 return nullptr;
3440
3441 // Upper - Lower [- 1]
3442 if (TestIsStrictOp)
3443 Diff = SemaRef.BuildBinOp(
3444 S, DefaultLoc, BO_Sub, Diff.get(),
3445 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3446 if (!Diff.isUsable())
3447 return nullptr;
3448
3449 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003450 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3451 if (NewStep.isInvalid())
3452 return nullptr;
3453 NewStep = SemaRef.PerformImplicitConversion(
3454 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3455 /*AllowExplicit=*/true);
3456 if (NewStep.isInvalid())
3457 return nullptr;
3458 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003459 if (!Diff.isUsable())
3460 return nullptr;
3461
3462 // Parentheses (for dumping/debugging purposes only).
3463 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3464 if (!Diff.isUsable())
3465 return nullptr;
3466
3467 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003468 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3469 if (NewStep.isInvalid())
3470 return nullptr;
3471 NewStep = SemaRef.PerformImplicitConversion(
3472 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3473 /*AllowExplicit=*/true);
3474 if (NewStep.isInvalid())
3475 return nullptr;
3476 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003477 if (!Diff.isUsable())
3478 return nullptr;
3479
Alexander Musman174b3ca2014-10-06 11:16:29 +00003480 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003481 QualType Type = Diff.get()->getType();
3482 auto &C = SemaRef.Context;
3483 bool UseVarType = VarType->hasIntegerRepresentation() &&
3484 C.getTypeSize(Type) > C.getTypeSize(VarType);
3485 if (!Type->isIntegerType() || UseVarType) {
3486 unsigned NewSize =
3487 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3488 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3489 : Type->hasSignedIntegerRepresentation();
3490 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3491 Diff = SemaRef.PerformImplicitConversion(
3492 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3493 if (!Diff.isUsable())
3494 return nullptr;
3495 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003496 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003497 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3498 if (NewSize != C.getTypeSize(Type)) {
3499 if (NewSize < C.getTypeSize(Type)) {
3500 assert(NewSize == 64 && "incorrect loop var size");
3501 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3502 << InitSrcRange << ConditionSrcRange;
3503 }
3504 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003505 NewSize, Type->hasSignedIntegerRepresentation() ||
3506 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003507 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3508 Sema::AA_Converting, true);
3509 if (!Diff.isUsable())
3510 return nullptr;
3511 }
3512 }
3513
Alexander Musmana5f070a2014-10-01 06:03:56 +00003514 return Diff.get();
3515}
3516
Alexey Bataev62dbb972015-04-22 11:59:37 +00003517Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3518 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3519 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3520 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003521 TransformToNewDefs Transform(SemaRef);
3522
3523 auto NewLB = Transform.TransformExpr(LB);
3524 auto NewUB = Transform.TransformExpr(UB);
3525 if (NewLB.isInvalid() || NewUB.isInvalid())
3526 return Cond;
3527 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3528 Sema::AA_Converting,
3529 /*AllowExplicit=*/true);
3530 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3531 Sema::AA_Converting,
3532 /*AllowExplicit=*/true);
3533 if (NewLB.isInvalid() || NewUB.isInvalid())
3534 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003535 auto CondExpr = SemaRef.BuildBinOp(
3536 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3537 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003538 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003539 if (CondExpr.isUsable()) {
3540 CondExpr = SemaRef.PerformImplicitConversion(
3541 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3542 /*AllowExplicit=*/true);
3543 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003544 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3545 // Otherwise use original loop conditon and evaluate it in runtime.
3546 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3547}
3548
Alexander Musmana5f070a2014-10-01 06:03:56 +00003549/// \brief Build reference expression to the counter be used for codegen.
3550Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003551 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3552 DefaultLoc);
3553}
3554
3555Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3556 if (Var && !Var->isInvalidDecl()) {
3557 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003558 auto *PrivateVar =
3559 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3560 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003561 if (PrivateVar->isInvalidDecl())
3562 return nullptr;
3563 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3564 }
3565 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003566}
3567
3568/// \brief Build initization of the counter be used for codegen.
3569Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3570
3571/// \brief Build step of the counter be used for codegen.
3572Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3573
3574/// \brief Iteration space of a single for loop.
3575struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003576 /// \brief Condition of the loop.
3577 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003578 /// \brief This expression calculates the number of iterations in the loop.
3579 /// It is always possible to calculate it before starting the loop.
3580 Expr *NumIterations;
3581 /// \brief The loop counter variable.
3582 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003583 /// \brief Private loop counter variable.
3584 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003585 /// \brief This is initializer for the initial value of #CounterVar.
3586 Expr *CounterInit;
3587 /// \brief This is step for the #CounterVar used to generate its update:
3588 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3589 Expr *CounterStep;
3590 /// \brief Should step be subtracted?
3591 bool Subtract;
3592 /// \brief Source range of the loop init.
3593 SourceRange InitSrcRange;
3594 /// \brief Source range of the loop condition.
3595 SourceRange CondSrcRange;
3596 /// \brief Source range of the loop increment.
3597 SourceRange IncSrcRange;
3598};
3599
Alexey Bataev23b69422014-06-18 07:08:49 +00003600} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003601
Alexey Bataev9c821032015-04-30 04:23:23 +00003602void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3603 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3604 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003605 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3606 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003607 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3608 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003609 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003610 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003611 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003612 }
3613}
3614
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003615/// \brief Called on a for stmt to check and extract its iteration space
3616/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003617static bool CheckOpenMPIterationSpace(
3618 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3619 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003620 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003621 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003622 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003623 // OpenMP [2.6, Canonical Loop Form]
3624 // for (init-expr; test-expr; incr-expr) structured-block
3625 auto For = dyn_cast_or_null<ForStmt>(S);
3626 if (!For) {
3627 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003628 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3629 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3630 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3631 if (NestedLoopCount > 1) {
3632 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3633 SemaRef.Diag(DSA.getConstructLoc(),
3634 diag::note_omp_collapse_ordered_expr)
3635 << 2 << CollapseLoopCountExpr->getSourceRange()
3636 << OrderedLoopCountExpr->getSourceRange();
3637 else if (CollapseLoopCountExpr)
3638 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3639 diag::note_omp_collapse_ordered_expr)
3640 << 0 << CollapseLoopCountExpr->getSourceRange();
3641 else
3642 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3643 diag::note_omp_collapse_ordered_expr)
3644 << 1 << OrderedLoopCountExpr->getSourceRange();
3645 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003646 return true;
3647 }
3648 assert(For->getBody());
3649
3650 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3651
3652 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003653 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003654 if (ISC.CheckInit(Init)) {
3655 return true;
3656 }
3657
3658 bool HasErrors = false;
3659
3660 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003661 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003662
3663 // OpenMP [2.6, Canonical Loop Form]
3664 // Var is one of the following:
3665 // A variable of signed or unsigned integer type.
3666 // For C++, a variable of a random access iterator type.
3667 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003668 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003669 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3670 !VarType->isPointerType() &&
3671 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3672 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3673 << SemaRef.getLangOpts().CPlusPlus;
3674 HasErrors = true;
3675 }
3676
Alexey Bataev4acb8592014-07-07 13:01:15 +00003677 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3678 // Construct
3679 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3680 // parallel for construct is (are) private.
3681 // The loop iteration variable in the associated for-loop of a simd construct
3682 // with just one associated for-loop is linear with a constant-linear-step
3683 // that is the increment of the associated for-loop.
3684 // Exclude loop var from the list of variables with implicitly defined data
3685 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003686 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003687
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003688 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3689 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003690 // The loop iteration variable in the associated for-loop of a simd construct
3691 // with just one associated for-loop may be listed in a linear clause with a
3692 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003693 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3694 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003695 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003696 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3697 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3698 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003699 auto PredeterminedCKind =
3700 isOpenMPSimdDirective(DKind)
3701 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3702 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003703 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003704 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003705 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003706 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003707 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003708 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3709 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003710 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003711 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3712 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003713 if (DVar.RefExpr == nullptr)
3714 DVar.CKind = PredeterminedCKind;
3715 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003716 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003717 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003718 // Make the loop iteration variable private (for worksharing constructs),
3719 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003720 // lastprivate (for simd directives with several collapsed or ordered
3721 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003722 if (DVar.CKind == OMPC_unknown)
3723 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3724 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003725 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003726 }
3727
Alexey Bataev7ff55242014-06-19 09:13:45 +00003728 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003729
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003730 // Check test-expr.
3731 HasErrors |= ISC.CheckCond(For->getCond());
3732
3733 // Check incr-expr.
3734 HasErrors |= ISC.CheckInc(For->getInc());
3735
Alexander Musmana5f070a2014-10-01 06:03:56 +00003736 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003737 return HasErrors;
3738
Alexander Musmana5f070a2014-10-01 06:03:56 +00003739 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003740 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003741 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003742 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003743 isOpenMPTaskLoopDirective(DKind) ||
3744 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003745 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003746 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003747 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3748 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3749 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3750 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3751 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3752 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3753
Alexey Bataev62dbb972015-04-22 11:59:37 +00003754 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3755 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003756 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003757 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003758 ResultIterSpace.CounterInit == nullptr ||
3759 ResultIterSpace.CounterStep == nullptr);
3760
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003761 return HasErrors;
3762}
3763
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003764/// \brief Build 'VarRef = Start.
3765static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3766 ExprResult VarRef, ExprResult Start) {
3767 TransformToNewDefs Transform(SemaRef);
3768 // Build 'VarRef = Start.
3769 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3770 if (NewStart.isInvalid())
3771 return ExprError();
3772 NewStart = SemaRef.PerformImplicitConversion(
3773 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3774 Sema::AA_Converting,
3775 /*AllowExplicit=*/true);
3776 if (NewStart.isInvalid())
3777 return ExprError();
3778 NewStart = SemaRef.PerformImplicitConversion(
3779 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3780 /*AllowExplicit=*/true);
3781 if (!NewStart.isUsable())
3782 return ExprError();
3783
3784 auto Init =
3785 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3786 return Init;
3787}
3788
Alexander Musmana5f070a2014-10-01 06:03:56 +00003789/// \brief Build 'VarRef = Start + Iter * Step'.
3790static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3791 SourceLocation Loc, ExprResult VarRef,
3792 ExprResult Start, ExprResult Iter,
3793 ExprResult Step, bool Subtract) {
3794 // Add parentheses (for debugging purposes only).
3795 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3796 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3797 !Step.isUsable())
3798 return ExprError();
3799
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003800 TransformToNewDefs Transform(SemaRef);
3801 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3802 if (NewStep.isInvalid())
3803 return ExprError();
3804 NewStep = SemaRef.PerformImplicitConversion(
3805 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3806 Sema::AA_Converting,
3807 /*AllowExplicit=*/true);
3808 if (NewStep.isInvalid())
3809 return ExprError();
3810 ExprResult Update =
3811 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003812 if (!Update.isUsable())
3813 return ExprError();
3814
3815 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003816 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3817 if (NewStart.isInvalid())
3818 return ExprError();
3819 NewStart = SemaRef.PerformImplicitConversion(
3820 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3821 Sema::AA_Converting,
3822 /*AllowExplicit=*/true);
3823 if (NewStart.isInvalid())
3824 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003825 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003826 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003827 if (!Update.isUsable())
3828 return ExprError();
3829
3830 Update = SemaRef.PerformImplicitConversion(
3831 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3832 if (!Update.isUsable())
3833 return ExprError();
3834
3835 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3836 return Update;
3837}
3838
3839/// \brief Convert integer expression \a E to make it have at least \a Bits
3840/// bits.
3841static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3842 Sema &SemaRef) {
3843 if (E == nullptr)
3844 return ExprError();
3845 auto &C = SemaRef.Context;
3846 QualType OldType = E->getType();
3847 unsigned HasBits = C.getTypeSize(OldType);
3848 if (HasBits >= Bits)
3849 return ExprResult(E);
3850 // OK to convert to signed, because new type has more bits than old.
3851 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3852 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3853 true);
3854}
3855
3856/// \brief Check if the given expression \a E is a constant integer that fits
3857/// into \a Bits bits.
3858static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3859 if (E == nullptr)
3860 return false;
3861 llvm::APSInt Result;
3862 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3863 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3864 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003865}
3866
3867/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003868/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3869/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003870static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003871CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3872 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3873 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003874 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003875 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003876 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003877 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003878 // Found 'collapse' clause - calculate collapse number.
3879 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003880 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003881 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003882 }
3883 if (OrderedLoopCountExpr) {
3884 // Found 'ordered' clause - calculate collapse number.
3885 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003886 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3887 if (Result.getLimitedValue() < NestedLoopCount) {
3888 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3889 diag::err_omp_wrong_ordered_loop_count)
3890 << OrderedLoopCountExpr->getSourceRange();
3891 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3892 diag::note_collapse_loop_count)
3893 << CollapseLoopCountExpr->getSourceRange();
3894 }
3895 NestedLoopCount = Result.getLimitedValue();
3896 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003897 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003898 // This is helper routine for loop directives (e.g., 'for', 'simd',
3899 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003900 SmallVector<LoopIterationSpace, 4> IterSpaces;
3901 IterSpaces.resize(NestedLoopCount);
3902 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003903 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003904 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003905 NestedLoopCount, CollapseLoopCountExpr,
3906 OrderedLoopCountExpr, VarsWithImplicitDSA,
3907 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003908 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003909 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003910 // OpenMP [2.8.1, simd construct, Restrictions]
3911 // All loops associated with the construct must be perfectly nested; that
3912 // is, there must be no intervening code nor any OpenMP directive between
3913 // any two loops.
3914 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003915 }
3916
Alexander Musmana5f070a2014-10-01 06:03:56 +00003917 Built.clear(/* size */ NestedLoopCount);
3918
3919 if (SemaRef.CurContext->isDependentContext())
3920 return NestedLoopCount;
3921
3922 // An example of what is generated for the following code:
3923 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003924 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003925 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003926 // for (k = 0; k < NK; ++k)
3927 // for (j = J0; j < NJ; j+=2) {
3928 // <loop body>
3929 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003930 //
3931 // We generate the code below.
3932 // Note: the loop body may be outlined in CodeGen.
3933 // Note: some counters may be C++ classes, operator- is used to find number of
3934 // iterations and operator+= to calculate counter value.
3935 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3936 // or i64 is currently supported).
3937 //
3938 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3939 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3940 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3941 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3942 // // similar updates for vars in clauses (e.g. 'linear')
3943 // <loop body (using local i and j)>
3944 // }
3945 // i = NI; // assign final values of counters
3946 // j = NJ;
3947 //
3948
3949 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3950 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003951 // Precondition tests if there is at least one iteration (all conditions are
3952 // true).
3953 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003954 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003955 ExprResult LastIteration32 = WidenIterationCount(
3956 32 /* Bits */, SemaRef.PerformImplicitConversion(
3957 N0->IgnoreImpCasts(), N0->getType(),
3958 Sema::AA_Converting, /*AllowExplicit=*/true)
3959 .get(),
3960 SemaRef);
3961 ExprResult LastIteration64 = WidenIterationCount(
3962 64 /* Bits */, SemaRef.PerformImplicitConversion(
3963 N0->IgnoreImpCasts(), N0->getType(),
3964 Sema::AA_Converting, /*AllowExplicit=*/true)
3965 .get(),
3966 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003967
3968 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3969 return NestedLoopCount;
3970
3971 auto &C = SemaRef.Context;
3972 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3973
3974 Scope *CurScope = DSA.getCurScope();
3975 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003976 if (PreCond.isUsable()) {
3977 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3978 PreCond.get(), IterSpaces[Cnt].PreCond);
3979 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003980 auto N = IterSpaces[Cnt].NumIterations;
3981 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3982 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003983 LastIteration32 = SemaRef.BuildBinOp(
3984 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3985 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3986 Sema::AA_Converting,
3987 /*AllowExplicit=*/true)
3988 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003989 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003990 LastIteration64 = SemaRef.BuildBinOp(
3991 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3992 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3993 Sema::AA_Converting,
3994 /*AllowExplicit=*/true)
3995 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003996 }
3997
3998 // Choose either the 32-bit or 64-bit version.
3999 ExprResult LastIteration = LastIteration64;
4000 if (LastIteration32.isUsable() &&
4001 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4002 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4003 FitsInto(
4004 32 /* Bits */,
4005 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4006 LastIteration64.get(), SemaRef)))
4007 LastIteration = LastIteration32;
4008
4009 if (!LastIteration.isUsable())
4010 return 0;
4011
4012 // Save the number of iterations.
4013 ExprResult NumIterations = LastIteration;
4014 {
4015 LastIteration = SemaRef.BuildBinOp(
4016 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4017 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4018 if (!LastIteration.isUsable())
4019 return 0;
4020 }
4021
4022 // Calculate the last iteration number beforehand instead of doing this on
4023 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4024 llvm::APSInt Result;
4025 bool IsConstant =
4026 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4027 ExprResult CalcLastIteration;
4028 if (!IsConstant) {
4029 SourceLocation SaveLoc;
4030 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004031 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004032 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004033 ExprResult SaveRef = buildDeclRefExpr(
4034 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004035 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4036 SaveRef.get(), LastIteration.get());
4037 LastIteration = SaveRef;
4038
4039 // Prepare SaveRef + 1.
4040 NumIterations = SemaRef.BuildBinOp(
4041 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4042 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4043 if (!NumIterations.isUsable())
4044 return 0;
4045 }
4046
4047 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4048
Alexander Musmanc6388682014-12-15 07:07:06 +00004049 QualType VType = LastIteration.get()->getType();
4050 // Build variables passed into runtime, nesessary for worksharing directives.
4051 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004052 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4053 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004054 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004055 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4056 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004057 SemaRef.AddInitializerToDecl(
4058 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4059 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4060
4061 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004062 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4063 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004064 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4065 /*DirectInit*/ false,
4066 /*TypeMayContainAuto*/ false);
4067
4068 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4069 // This will be used to implement clause 'lastprivate'.
4070 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004071 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4072 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004073 SemaRef.AddInitializerToDecl(
4074 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4075 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4076
4077 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004078 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4079 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004080 SemaRef.AddInitializerToDecl(
4081 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4082 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4083
4084 // Build expression: UB = min(UB, LastIteration)
4085 // It is nesessary for CodeGen of directives with static scheduling.
4086 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4087 UB.get(), LastIteration.get());
4088 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4089 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4090 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4091 CondOp.get());
4092 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4093 }
4094
4095 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004096 ExprResult IV;
4097 ExprResult Init;
4098 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004099 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4100 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004101 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004102 isOpenMPTaskLoopDirective(DKind) ||
4103 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004104 ? LB.get()
4105 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4106 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4107 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004108 }
4109
Alexander Musmanc6388682014-12-15 07:07:06 +00004110 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004111 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004112 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004113 (isOpenMPWorksharingDirective(DKind) ||
4114 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004115 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4116 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4117 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004118
4119 // Loop increment (IV = IV + 1)
4120 SourceLocation IncLoc;
4121 ExprResult Inc =
4122 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4123 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4124 if (!Inc.isUsable())
4125 return 0;
4126 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004127 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4128 if (!Inc.isUsable())
4129 return 0;
4130
4131 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4132 // Used for directives with static scheduling.
4133 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004134 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4135 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004136 // LB + ST
4137 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4138 if (!NextLB.isUsable())
4139 return 0;
4140 // LB = LB + ST
4141 NextLB =
4142 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4143 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4144 if (!NextLB.isUsable())
4145 return 0;
4146 // UB + ST
4147 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4148 if (!NextUB.isUsable())
4149 return 0;
4150 // UB = UB + ST
4151 NextUB =
4152 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4153 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4154 if (!NextUB.isUsable())
4155 return 0;
4156 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004157
4158 // Build updates and final values of the loop counters.
4159 bool HasErrors = false;
4160 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004161 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004162 Built.Updates.resize(NestedLoopCount);
4163 Built.Finals.resize(NestedLoopCount);
4164 {
4165 ExprResult Div;
4166 // Go from inner nested loop to outer.
4167 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4168 LoopIterationSpace &IS = IterSpaces[Cnt];
4169 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4170 // Build: Iter = (IV / Div) % IS.NumIters
4171 // where Div is product of previous iterations' IS.NumIters.
4172 ExprResult Iter;
4173 if (Div.isUsable()) {
4174 Iter =
4175 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4176 } else {
4177 Iter = IV;
4178 assert((Cnt == (int)NestedLoopCount - 1) &&
4179 "unusable div expected on first iteration only");
4180 }
4181
4182 if (Cnt != 0 && Iter.isUsable())
4183 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4184 IS.NumIterations);
4185 if (!Iter.isUsable()) {
4186 HasErrors = true;
4187 break;
4188 }
4189
Alexey Bataev39f915b82015-05-08 10:41:21 +00004190 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4191 auto *CounterVar = buildDeclRefExpr(
4192 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4193 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4194 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004195 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4196 IS.CounterInit);
4197 if (!Init.isUsable()) {
4198 HasErrors = true;
4199 break;
4200 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004201 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004202 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004203 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4204 if (!Update.isUsable()) {
4205 HasErrors = true;
4206 break;
4207 }
4208
4209 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4210 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004211 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004212 IS.NumIterations, IS.CounterStep, IS.Subtract);
4213 if (!Final.isUsable()) {
4214 HasErrors = true;
4215 break;
4216 }
4217
4218 // Build Div for the next iteration: Div <- Div * IS.NumIters
4219 if (Cnt != 0) {
4220 if (Div.isUnset())
4221 Div = IS.NumIterations;
4222 else
4223 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4224 IS.NumIterations);
4225
4226 // Add parentheses (for debugging purposes only).
4227 if (Div.isUsable())
4228 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4229 if (!Div.isUsable()) {
4230 HasErrors = true;
4231 break;
4232 }
4233 }
4234 if (!Update.isUsable() || !Final.isUsable()) {
4235 HasErrors = true;
4236 break;
4237 }
4238 // Save results
4239 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004240 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004241 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004242 Built.Updates[Cnt] = Update.get();
4243 Built.Finals[Cnt] = Final.get();
4244 }
4245 }
4246
4247 if (HasErrors)
4248 return 0;
4249
4250 // Save results
4251 Built.IterationVarRef = IV.get();
4252 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004253 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004254 Built.CalcLastIteration =
4255 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004256 Built.PreCond = PreCond.get();
4257 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004258 Built.Init = Init.get();
4259 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004260 Built.LB = LB.get();
4261 Built.UB = UB.get();
4262 Built.IL = IL.get();
4263 Built.ST = ST.get();
4264 Built.EUB = EUB.get();
4265 Built.NLB = NextLB.get();
4266 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004267
Alexey Bataevabfc0692014-06-25 06:52:00 +00004268 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269}
4270
Alexey Bataev10e775f2015-07-30 11:36:16 +00004271static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004272 auto CollapseClauses =
4273 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4274 if (CollapseClauses.begin() != CollapseClauses.end())
4275 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004276 return nullptr;
4277}
4278
Alexey Bataev10e775f2015-07-30 11:36:16 +00004279static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004280 auto OrderedClauses =
4281 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4282 if (OrderedClauses.begin() != OrderedClauses.end())
4283 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004284 return nullptr;
4285}
4286
Alexey Bataev66b15b52015-08-21 11:14:16 +00004287static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4288 const Expr *Safelen) {
4289 llvm::APSInt SimdlenRes, SafelenRes;
4290 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4291 Simdlen->isInstantiationDependent() ||
4292 Simdlen->containsUnexpandedParameterPack())
4293 return false;
4294 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4295 Safelen->isInstantiationDependent() ||
4296 Safelen->containsUnexpandedParameterPack())
4297 return false;
4298 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4299 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4300 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4301 // If both simdlen and safelen clauses are specified, the value of the simdlen
4302 // parameter must be less than or equal to the value of the safelen parameter.
4303 if (SimdlenRes > SafelenRes) {
4304 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4305 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4306 return true;
4307 }
4308 return false;
4309}
4310
Alexey Bataev4acb8592014-07-07 13:01:15 +00004311StmtResult Sema::ActOnOpenMPSimdDirective(
4312 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4313 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004314 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004315 if (!AStmt)
4316 return StmtError();
4317
4318 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004319 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004320 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4321 // define the nested loops number.
4322 unsigned NestedLoopCount = CheckOpenMPLoop(
4323 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4324 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004325 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004326 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004327
Alexander Musmana5f070a2014-10-01 06:03:56 +00004328 assert((CurContext->isDependentContext() || B.builtAll()) &&
4329 "omp simd loop exprs were not built");
4330
Alexander Musman3276a272015-03-21 10:12:56 +00004331 if (!CurContext->isDependentContext()) {
4332 // Finalize the clauses that need pre-built expressions for CodeGen.
4333 for (auto C : Clauses) {
4334 if (auto LC = dyn_cast<OMPLinearClause>(C))
4335 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4336 B.NumIterations, *this, CurScope))
4337 return StmtError();
4338 }
4339 }
4340
Alexey Bataev66b15b52015-08-21 11:14:16 +00004341 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4342 // If both simdlen and safelen clauses are specified, the value of the simdlen
4343 // parameter must be less than or equal to the value of the safelen parameter.
4344 OMPSafelenClause *Safelen = nullptr;
4345 OMPSimdlenClause *Simdlen = nullptr;
4346 for (auto *Clause : Clauses) {
4347 if (Clause->getClauseKind() == OMPC_safelen)
4348 Safelen = cast<OMPSafelenClause>(Clause);
4349 else if (Clause->getClauseKind() == OMPC_simdlen)
4350 Simdlen = cast<OMPSimdlenClause>(Clause);
4351 if (Safelen && Simdlen)
4352 break;
4353 }
4354 if (Simdlen && Safelen &&
4355 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4356 Safelen->getSafelen()))
4357 return StmtError();
4358
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004359 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004360 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4361 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004362}
4363
Alexey Bataev4acb8592014-07-07 13:01:15 +00004364StmtResult Sema::ActOnOpenMPForDirective(
4365 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4366 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004367 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004368 if (!AStmt)
4369 return StmtError();
4370
4371 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004372 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004373 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4374 // define the nested loops number.
4375 unsigned NestedLoopCount = CheckOpenMPLoop(
4376 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4377 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004378 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004379 return StmtError();
4380
Alexander Musmana5f070a2014-10-01 06:03:56 +00004381 assert((CurContext->isDependentContext() || B.builtAll()) &&
4382 "omp for loop exprs were not built");
4383
Alexey Bataev54acd402015-08-04 11:18:19 +00004384 if (!CurContext->isDependentContext()) {
4385 // Finalize the clauses that need pre-built expressions for CodeGen.
4386 for (auto C : Clauses) {
4387 if (auto LC = dyn_cast<OMPLinearClause>(C))
4388 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4389 B.NumIterations, *this, CurScope))
4390 return StmtError();
4391 }
4392 }
4393
Alexey Bataevf29276e2014-06-18 04:14:57 +00004394 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004395 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004396 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004397}
4398
Alexander Musmanf82886e2014-09-18 05:12:34 +00004399StmtResult Sema::ActOnOpenMPForSimdDirective(
4400 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4401 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004402 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004403 if (!AStmt)
4404 return StmtError();
4405
4406 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004407 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004408 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4409 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004410 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004411 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4412 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4413 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004414 if (NestedLoopCount == 0)
4415 return StmtError();
4416
Alexander Musmanc6388682014-12-15 07:07:06 +00004417 assert((CurContext->isDependentContext() || B.builtAll()) &&
4418 "omp for simd loop exprs were not built");
4419
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004420 if (!CurContext->isDependentContext()) {
4421 // Finalize the clauses that need pre-built expressions for CodeGen.
4422 for (auto C : Clauses) {
4423 if (auto LC = dyn_cast<OMPLinearClause>(C))
4424 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4425 B.NumIterations, *this, CurScope))
4426 return StmtError();
4427 }
4428 }
4429
Alexey Bataev66b15b52015-08-21 11:14:16 +00004430 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4431 // If both simdlen and safelen clauses are specified, the value of the simdlen
4432 // parameter must be less than or equal to the value of the safelen parameter.
4433 OMPSafelenClause *Safelen = nullptr;
4434 OMPSimdlenClause *Simdlen = nullptr;
4435 for (auto *Clause : Clauses) {
4436 if (Clause->getClauseKind() == OMPC_safelen)
4437 Safelen = cast<OMPSafelenClause>(Clause);
4438 else if (Clause->getClauseKind() == OMPC_simdlen)
4439 Simdlen = cast<OMPSimdlenClause>(Clause);
4440 if (Safelen && Simdlen)
4441 break;
4442 }
4443 if (Simdlen && Safelen &&
4444 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4445 Safelen->getSafelen()))
4446 return StmtError();
4447
Alexander Musmanf82886e2014-09-18 05:12:34 +00004448 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004449 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4450 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004451}
4452
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004453StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4454 Stmt *AStmt,
4455 SourceLocation StartLoc,
4456 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004457 if (!AStmt)
4458 return StmtError();
4459
4460 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004461 auto BaseStmt = AStmt;
4462 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4463 BaseStmt = CS->getCapturedStmt();
4464 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4465 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004466 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004467 return StmtError();
4468 // All associated statements must be '#pragma omp section' except for
4469 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004470 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004471 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4472 if (SectionStmt)
4473 Diag(SectionStmt->getLocStart(),
4474 diag::err_omp_sections_substmt_not_section);
4475 return StmtError();
4476 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004477 cast<OMPSectionDirective>(SectionStmt)
4478 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004479 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004480 } else {
4481 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4482 return StmtError();
4483 }
4484
4485 getCurFunction()->setHasBranchProtectedScope();
4486
Alexey Bataev25e5b442015-09-15 12:52:43 +00004487 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4488 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004489}
4490
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004491StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4492 SourceLocation StartLoc,
4493 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004494 if (!AStmt)
4495 return StmtError();
4496
4497 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004498
4499 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004500 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004501
Alexey Bataev25e5b442015-09-15 12:52:43 +00004502 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4503 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004504}
4505
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004506StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4507 Stmt *AStmt,
4508 SourceLocation StartLoc,
4509 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004510 if (!AStmt)
4511 return StmtError();
4512
4513 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004514
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004515 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004516
Alexey Bataev3255bf32015-01-19 05:20:46 +00004517 // OpenMP [2.7.3, single Construct, Restrictions]
4518 // The copyprivate clause must not be used with the nowait clause.
4519 OMPClause *Nowait = nullptr;
4520 OMPClause *Copyprivate = nullptr;
4521 for (auto *Clause : Clauses) {
4522 if (Clause->getClauseKind() == OMPC_nowait)
4523 Nowait = Clause;
4524 else if (Clause->getClauseKind() == OMPC_copyprivate)
4525 Copyprivate = Clause;
4526 if (Copyprivate && Nowait) {
4527 Diag(Copyprivate->getLocStart(),
4528 diag::err_omp_single_copyprivate_with_nowait);
4529 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4530 return StmtError();
4531 }
4532 }
4533
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004534 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4535}
4536
Alexander Musman80c22892014-07-17 08:54:58 +00004537StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4538 SourceLocation StartLoc,
4539 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004540 if (!AStmt)
4541 return StmtError();
4542
4543 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004544
4545 getCurFunction()->setHasBranchProtectedScope();
4546
4547 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4548}
4549
Alexey Bataev28c75412015-12-15 08:19:24 +00004550StmtResult Sema::ActOnOpenMPCriticalDirective(
4551 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4552 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004553 if (!AStmt)
4554 return StmtError();
4555
4556 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004557
Alexey Bataev28c75412015-12-15 08:19:24 +00004558 bool ErrorFound = false;
4559 llvm::APSInt Hint;
4560 SourceLocation HintLoc;
4561 bool DependentHint = false;
4562 for (auto *C : Clauses) {
4563 if (C->getClauseKind() == OMPC_hint) {
4564 if (!DirName.getName()) {
4565 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4566 ErrorFound = true;
4567 }
4568 Expr *E = cast<OMPHintClause>(C)->getHint();
4569 if (E->isTypeDependent() || E->isValueDependent() ||
4570 E->isInstantiationDependent())
4571 DependentHint = true;
4572 else {
4573 Hint = E->EvaluateKnownConstInt(Context);
4574 HintLoc = C->getLocStart();
4575 }
4576 }
4577 }
4578 if (ErrorFound)
4579 return StmtError();
4580 auto Pair = DSAStack->getCriticalWithHint(DirName);
4581 if (Pair.first && DirName.getName() && !DependentHint) {
4582 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4583 Diag(StartLoc, diag::err_omp_critical_with_hint);
4584 if (HintLoc.isValid()) {
4585 Diag(HintLoc, diag::note_omp_critical_hint_here)
4586 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4587 } else
4588 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4589 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4590 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4591 << 1
4592 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4593 /*Radix=*/10, /*Signed=*/false);
4594 } else
4595 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4596 }
4597 }
4598
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004599 getCurFunction()->setHasBranchProtectedScope();
4600
Alexey Bataev28c75412015-12-15 08:19:24 +00004601 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4602 Clauses, AStmt);
4603 if (!Pair.first && DirName.getName() && !DependentHint)
4604 DSAStack->addCriticalWithHint(Dir, Hint);
4605 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004606}
4607
Alexey Bataev4acb8592014-07-07 13:01:15 +00004608StmtResult Sema::ActOnOpenMPParallelForDirective(
4609 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4610 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004611 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004612 if (!AStmt)
4613 return StmtError();
4614
Alexey Bataev4acb8592014-07-07 13:01:15 +00004615 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4616 // 1.2.2 OpenMP Language Terminology
4617 // Structured block - An executable statement with a single entry at the
4618 // top and a single exit at the bottom.
4619 // The point of exit cannot be a branch out of the structured block.
4620 // longjmp() and throw() must not violate the entry/exit criteria.
4621 CS->getCapturedDecl()->setNothrow();
4622
Alexander Musmanc6388682014-12-15 07:07:06 +00004623 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004624 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4625 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004626 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004627 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4628 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4629 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004630 if (NestedLoopCount == 0)
4631 return StmtError();
4632
Alexander Musmana5f070a2014-10-01 06:03:56 +00004633 assert((CurContext->isDependentContext() || B.builtAll()) &&
4634 "omp parallel for loop exprs were not built");
4635
Alexey Bataev54acd402015-08-04 11:18:19 +00004636 if (!CurContext->isDependentContext()) {
4637 // Finalize the clauses that need pre-built expressions for CodeGen.
4638 for (auto C : Clauses) {
4639 if (auto LC = dyn_cast<OMPLinearClause>(C))
4640 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4641 B.NumIterations, *this, CurScope))
4642 return StmtError();
4643 }
4644 }
4645
Alexey Bataev4acb8592014-07-07 13:01:15 +00004646 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004647 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004648 NestedLoopCount, Clauses, AStmt, B,
4649 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004650}
4651
Alexander Musmane4e893b2014-09-23 09:33:00 +00004652StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4653 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4654 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004655 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004656 if (!AStmt)
4657 return StmtError();
4658
Alexander Musmane4e893b2014-09-23 09:33:00 +00004659 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4660 // 1.2.2 OpenMP Language Terminology
4661 // Structured block - An executable statement with a single entry at the
4662 // top and a single exit at the bottom.
4663 // The point of exit cannot be a branch out of the structured block.
4664 // longjmp() and throw() must not violate the entry/exit criteria.
4665 CS->getCapturedDecl()->setNothrow();
4666
Alexander Musmanc6388682014-12-15 07:07:06 +00004667 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004668 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4669 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004670 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004671 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4672 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4673 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004674 if (NestedLoopCount == 0)
4675 return StmtError();
4676
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004677 if (!CurContext->isDependentContext()) {
4678 // Finalize the clauses that need pre-built expressions for CodeGen.
4679 for (auto C : Clauses) {
4680 if (auto LC = dyn_cast<OMPLinearClause>(C))
4681 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4682 B.NumIterations, *this, CurScope))
4683 return StmtError();
4684 }
4685 }
4686
Alexey Bataev66b15b52015-08-21 11:14:16 +00004687 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4688 // If both simdlen and safelen clauses are specified, the value of the simdlen
4689 // parameter must be less than or equal to the value of the safelen parameter.
4690 OMPSafelenClause *Safelen = nullptr;
4691 OMPSimdlenClause *Simdlen = nullptr;
4692 for (auto *Clause : Clauses) {
4693 if (Clause->getClauseKind() == OMPC_safelen)
4694 Safelen = cast<OMPSafelenClause>(Clause);
4695 else if (Clause->getClauseKind() == OMPC_simdlen)
4696 Simdlen = cast<OMPSimdlenClause>(Clause);
4697 if (Safelen && Simdlen)
4698 break;
4699 }
4700 if (Simdlen && Safelen &&
4701 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4702 Safelen->getSafelen()))
4703 return StmtError();
4704
Alexander Musmane4e893b2014-09-23 09:33:00 +00004705 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004706 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004707 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004708}
4709
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004710StmtResult
4711Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4712 Stmt *AStmt, SourceLocation StartLoc,
4713 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004714 if (!AStmt)
4715 return StmtError();
4716
4717 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004718 auto BaseStmt = AStmt;
4719 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4720 BaseStmt = CS->getCapturedStmt();
4721 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4722 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004723 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004724 return StmtError();
4725 // All associated statements must be '#pragma omp section' except for
4726 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004727 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004728 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4729 if (SectionStmt)
4730 Diag(SectionStmt->getLocStart(),
4731 diag::err_omp_parallel_sections_substmt_not_section);
4732 return StmtError();
4733 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004734 cast<OMPSectionDirective>(SectionStmt)
4735 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004736 }
4737 } else {
4738 Diag(AStmt->getLocStart(),
4739 diag::err_omp_parallel_sections_not_compound_stmt);
4740 return StmtError();
4741 }
4742
4743 getCurFunction()->setHasBranchProtectedScope();
4744
Alexey Bataev25e5b442015-09-15 12:52:43 +00004745 return OMPParallelSectionsDirective::Create(
4746 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004747}
4748
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004749StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4750 Stmt *AStmt, SourceLocation StartLoc,
4751 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004752 if (!AStmt)
4753 return StmtError();
4754
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004755 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4756 // 1.2.2 OpenMP Language Terminology
4757 // Structured block - An executable statement with a single entry at the
4758 // top and a single exit at the bottom.
4759 // The point of exit cannot be a branch out of the structured block.
4760 // longjmp() and throw() must not violate the entry/exit criteria.
4761 CS->getCapturedDecl()->setNothrow();
4762
4763 getCurFunction()->setHasBranchProtectedScope();
4764
Alexey Bataev25e5b442015-09-15 12:52:43 +00004765 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4766 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004767}
4768
Alexey Bataev68446b72014-07-18 07:47:19 +00004769StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4770 SourceLocation EndLoc) {
4771 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4772}
4773
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004774StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4775 SourceLocation EndLoc) {
4776 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4777}
4778
Alexey Bataev2df347a2014-07-18 10:17:07 +00004779StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4780 SourceLocation EndLoc) {
4781 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4782}
4783
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004784StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4785 SourceLocation StartLoc,
4786 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004787 if (!AStmt)
4788 return StmtError();
4789
4790 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004791
4792 getCurFunction()->setHasBranchProtectedScope();
4793
4794 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4795}
4796
Alexey Bataev6125da92014-07-21 11:26:11 +00004797StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4798 SourceLocation StartLoc,
4799 SourceLocation EndLoc) {
4800 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4801 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4802}
4803
Alexey Bataev346265e2015-09-25 10:37:12 +00004804StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4805 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004806 SourceLocation StartLoc,
4807 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004808 OMPClause *DependFound = nullptr;
4809 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004810 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004811 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004812 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004813 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004814 for (auto *C : Clauses) {
4815 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4816 DependFound = C;
4817 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4818 if (DependSourceClause) {
4819 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4820 << getOpenMPDirectiveName(OMPD_ordered)
4821 << getOpenMPClauseName(OMPC_depend) << 2;
4822 ErrorFound = true;
4823 } else
4824 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004825 if (DependSinkClause) {
4826 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4827 << 0;
4828 ErrorFound = true;
4829 }
4830 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4831 if (DependSourceClause) {
4832 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4833 << 1;
4834 ErrorFound = true;
4835 }
4836 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004837 }
4838 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004839 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004840 else if (C->getClauseKind() == OMPC_simd)
4841 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004842 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004843 if (!ErrorFound && !SC &&
4844 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004845 // OpenMP [2.8.1,simd Construct, Restrictions]
4846 // An ordered construct with the simd clause is the only OpenMP construct
4847 // that can appear in the simd region.
4848 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004849 ErrorFound = true;
4850 } else if (DependFound && (TC || SC)) {
4851 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4852 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4853 ErrorFound = true;
4854 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4855 Diag(DependFound->getLocStart(),
4856 diag::err_omp_ordered_directive_without_param);
4857 ErrorFound = true;
4858 } else if (TC || Clauses.empty()) {
4859 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4860 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4861 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4862 << (TC != nullptr);
4863 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4864 ErrorFound = true;
4865 }
4866 }
4867 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004868 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004869
4870 if (AStmt) {
4871 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4872
4873 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004874 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004875
4876 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004877}
4878
Alexey Bataev1d160b12015-03-13 12:27:31 +00004879namespace {
4880/// \brief Helper class for checking expression in 'omp atomic [update]'
4881/// construct.
4882class OpenMPAtomicUpdateChecker {
4883 /// \brief Error results for atomic update expressions.
4884 enum ExprAnalysisErrorCode {
4885 /// \brief A statement is not an expression statement.
4886 NotAnExpression,
4887 /// \brief Expression is not builtin binary or unary operation.
4888 NotABinaryOrUnaryExpression,
4889 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4890 NotAnUnaryIncDecExpression,
4891 /// \brief An expression is not of scalar type.
4892 NotAScalarType,
4893 /// \brief A binary operation is not an assignment operation.
4894 NotAnAssignmentOp,
4895 /// \brief RHS part of the binary operation is not a binary expression.
4896 NotABinaryExpression,
4897 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4898 /// expression.
4899 NotABinaryOperator,
4900 /// \brief RHS binary operation does not have reference to the updated LHS
4901 /// part.
4902 NotAnUpdateExpression,
4903 /// \brief No errors is found.
4904 NoError
4905 };
4906 /// \brief Reference to Sema.
4907 Sema &SemaRef;
4908 /// \brief A location for note diagnostics (when error is found).
4909 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004910 /// \brief 'x' lvalue part of the source atomic expression.
4911 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004912 /// \brief 'expr' rvalue part of the source atomic expression.
4913 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004914 /// \brief Helper expression of the form
4915 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4916 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4917 Expr *UpdateExpr;
4918 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4919 /// important for non-associative operations.
4920 bool IsXLHSInRHSPart;
4921 BinaryOperatorKind Op;
4922 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004923 /// \brief true if the source expression is a postfix unary operation, false
4924 /// if it is a prefix unary operation.
4925 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004926
4927public:
4928 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004929 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004930 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004931 /// \brief Check specified statement that it is suitable for 'atomic update'
4932 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004933 /// expression. If DiagId and NoteId == 0, then only check is performed
4934 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004935 /// \param DiagId Diagnostic which should be emitted if error is found.
4936 /// \param NoteId Diagnostic note for the main error message.
4937 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004938 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004939 /// \brief Return the 'x' lvalue part of the source atomic expression.
4940 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004941 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4942 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004943 /// \brief Return the update expression used in calculation of the updated
4944 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4945 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4946 Expr *getUpdateExpr() const { return UpdateExpr; }
4947 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4948 /// false otherwise.
4949 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4950
Alexey Bataevb78ca832015-04-01 03:33:17 +00004951 /// \brief true if the source expression is a postfix unary operation, false
4952 /// if it is a prefix unary operation.
4953 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4954
Alexey Bataev1d160b12015-03-13 12:27:31 +00004955private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004956 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4957 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004958};
4959} // namespace
4960
4961bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4962 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4963 ExprAnalysisErrorCode ErrorFound = NoError;
4964 SourceLocation ErrorLoc, NoteLoc;
4965 SourceRange ErrorRange, NoteRange;
4966 // Allowed constructs are:
4967 // x = x binop expr;
4968 // x = expr binop x;
4969 if (AtomicBinOp->getOpcode() == BO_Assign) {
4970 X = AtomicBinOp->getLHS();
4971 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4972 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4973 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4974 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4975 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004976 Op = AtomicInnerBinOp->getOpcode();
4977 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004978 auto *LHS = AtomicInnerBinOp->getLHS();
4979 auto *RHS = AtomicInnerBinOp->getRHS();
4980 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4981 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4982 /*Canonical=*/true);
4983 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4984 /*Canonical=*/true);
4985 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4986 /*Canonical=*/true);
4987 if (XId == LHSId) {
4988 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004989 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004990 } else if (XId == RHSId) {
4991 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004992 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004993 } else {
4994 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4995 ErrorRange = AtomicInnerBinOp->getSourceRange();
4996 NoteLoc = X->getExprLoc();
4997 NoteRange = X->getSourceRange();
4998 ErrorFound = NotAnUpdateExpression;
4999 }
5000 } else {
5001 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5002 ErrorRange = AtomicInnerBinOp->getSourceRange();
5003 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5004 NoteRange = SourceRange(NoteLoc, NoteLoc);
5005 ErrorFound = NotABinaryOperator;
5006 }
5007 } else {
5008 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5009 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5010 ErrorFound = NotABinaryExpression;
5011 }
5012 } else {
5013 ErrorLoc = AtomicBinOp->getExprLoc();
5014 ErrorRange = AtomicBinOp->getSourceRange();
5015 NoteLoc = AtomicBinOp->getOperatorLoc();
5016 NoteRange = SourceRange(NoteLoc, NoteLoc);
5017 ErrorFound = NotAnAssignmentOp;
5018 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005019 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005020 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5021 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5022 return true;
5023 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005024 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005025 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005026}
5027
5028bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5029 unsigned NoteId) {
5030 ExprAnalysisErrorCode ErrorFound = NoError;
5031 SourceLocation ErrorLoc, NoteLoc;
5032 SourceRange ErrorRange, NoteRange;
5033 // Allowed constructs are:
5034 // x++;
5035 // x--;
5036 // ++x;
5037 // --x;
5038 // x binop= expr;
5039 // x = x binop expr;
5040 // x = expr binop x;
5041 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5042 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5043 if (AtomicBody->getType()->isScalarType() ||
5044 AtomicBody->isInstantiationDependent()) {
5045 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5046 AtomicBody->IgnoreParenImpCasts())) {
5047 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005048 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005049 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005050 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005051 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005052 X = AtomicCompAssignOp->getLHS();
5053 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005054 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5055 AtomicBody->IgnoreParenImpCasts())) {
5056 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005057 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5058 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005059 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005060 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5061 // Check for Unary Operation
5062 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005063 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005064 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5065 OpLoc = AtomicUnaryOp->getOperatorLoc();
5066 X = AtomicUnaryOp->getSubExpr();
5067 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5068 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005069 } else {
5070 ErrorFound = NotAnUnaryIncDecExpression;
5071 ErrorLoc = AtomicUnaryOp->getExprLoc();
5072 ErrorRange = AtomicUnaryOp->getSourceRange();
5073 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5074 NoteRange = SourceRange(NoteLoc, NoteLoc);
5075 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005076 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005077 ErrorFound = NotABinaryOrUnaryExpression;
5078 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5079 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5080 }
5081 } else {
5082 ErrorFound = NotAScalarType;
5083 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5084 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5085 }
5086 } else {
5087 ErrorFound = NotAnExpression;
5088 NoteLoc = ErrorLoc = S->getLocStart();
5089 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5090 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005091 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005092 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5093 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5094 return true;
5095 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005096 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005097 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005098 // Build an update expression of form 'OpaqueValueExpr(x) binop
5099 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5100 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5101 auto *OVEX = new (SemaRef.getASTContext())
5102 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5103 auto *OVEExpr = new (SemaRef.getASTContext())
5104 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5105 auto Update =
5106 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5107 IsXLHSInRHSPart ? OVEExpr : OVEX);
5108 if (Update.isInvalid())
5109 return true;
5110 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5111 Sema::AA_Casting);
5112 if (Update.isInvalid())
5113 return true;
5114 UpdateExpr = Update.get();
5115 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005116 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005117}
5118
Alexey Bataev0162e452014-07-22 10:10:35 +00005119StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5120 Stmt *AStmt,
5121 SourceLocation StartLoc,
5122 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005123 if (!AStmt)
5124 return StmtError();
5125
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005126 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005127 // 1.2.2 OpenMP Language Terminology
5128 // Structured block - An executable statement with a single entry at the
5129 // top and a single exit at the bottom.
5130 // The point of exit cannot be a branch out of the structured block.
5131 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005132 OpenMPClauseKind AtomicKind = OMPC_unknown;
5133 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005134 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005135 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005136 C->getClauseKind() == OMPC_update ||
5137 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005138 if (AtomicKind != OMPC_unknown) {
5139 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5140 << SourceRange(C->getLocStart(), C->getLocEnd());
5141 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5142 << getOpenMPClauseName(AtomicKind);
5143 } else {
5144 AtomicKind = C->getClauseKind();
5145 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005146 }
5147 }
5148 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005149
Alexey Bataev459dec02014-07-24 06:46:57 +00005150 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005151 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5152 Body = EWC->getSubExpr();
5153
Alexey Bataev62cec442014-11-18 10:14:22 +00005154 Expr *X = nullptr;
5155 Expr *V = nullptr;
5156 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005157 Expr *UE = nullptr;
5158 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005159 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005160 // OpenMP [2.12.6, atomic Construct]
5161 // In the next expressions:
5162 // * x and v (as applicable) are both l-value expressions with scalar type.
5163 // * During the execution of an atomic region, multiple syntactic
5164 // occurrences of x must designate the same storage location.
5165 // * Neither of v and expr (as applicable) may access the storage location
5166 // designated by x.
5167 // * Neither of x and expr (as applicable) may access the storage location
5168 // designated by v.
5169 // * expr is an expression with scalar type.
5170 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5171 // * binop, binop=, ++, and -- are not overloaded operators.
5172 // * The expression x binop expr must be numerically equivalent to x binop
5173 // (expr). This requirement is satisfied if the operators in expr have
5174 // precedence greater than binop, or by using parentheses around expr or
5175 // subexpressions of expr.
5176 // * The expression expr binop x must be numerically equivalent to (expr)
5177 // binop x. This requirement is satisfied if the operators in expr have
5178 // precedence equal to or greater than binop, or by using parentheses around
5179 // expr or subexpressions of expr.
5180 // * For forms that allow multiple occurrences of x, the number of times
5181 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005182 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005183 enum {
5184 NotAnExpression,
5185 NotAnAssignmentOp,
5186 NotAScalarType,
5187 NotAnLValue,
5188 NoError
5189 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005190 SourceLocation ErrorLoc, NoteLoc;
5191 SourceRange ErrorRange, NoteRange;
5192 // If clause is read:
5193 // v = x;
5194 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5195 auto AtomicBinOp =
5196 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5197 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5198 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5199 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5200 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5201 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5202 if (!X->isLValue() || !V->isLValue()) {
5203 auto NotLValueExpr = X->isLValue() ? V : X;
5204 ErrorFound = NotAnLValue;
5205 ErrorLoc = AtomicBinOp->getExprLoc();
5206 ErrorRange = AtomicBinOp->getSourceRange();
5207 NoteLoc = NotLValueExpr->getExprLoc();
5208 NoteRange = NotLValueExpr->getSourceRange();
5209 }
5210 } else if (!X->isInstantiationDependent() ||
5211 !V->isInstantiationDependent()) {
5212 auto NotScalarExpr =
5213 (X->isInstantiationDependent() || X->getType()->isScalarType())
5214 ? V
5215 : X;
5216 ErrorFound = NotAScalarType;
5217 ErrorLoc = AtomicBinOp->getExprLoc();
5218 ErrorRange = AtomicBinOp->getSourceRange();
5219 NoteLoc = NotScalarExpr->getExprLoc();
5220 NoteRange = NotScalarExpr->getSourceRange();
5221 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005222 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005223 ErrorFound = NotAnAssignmentOp;
5224 ErrorLoc = AtomicBody->getExprLoc();
5225 ErrorRange = AtomicBody->getSourceRange();
5226 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5227 : AtomicBody->getExprLoc();
5228 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5229 : AtomicBody->getSourceRange();
5230 }
5231 } else {
5232 ErrorFound = NotAnExpression;
5233 NoteLoc = ErrorLoc = Body->getLocStart();
5234 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005235 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005236 if (ErrorFound != NoError) {
5237 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5238 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005239 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5240 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005241 return StmtError();
5242 } else if (CurContext->isDependentContext())
5243 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005244 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005245 enum {
5246 NotAnExpression,
5247 NotAnAssignmentOp,
5248 NotAScalarType,
5249 NotAnLValue,
5250 NoError
5251 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005252 SourceLocation ErrorLoc, NoteLoc;
5253 SourceRange ErrorRange, NoteRange;
5254 // If clause is write:
5255 // x = expr;
5256 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5257 auto AtomicBinOp =
5258 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5259 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005260 X = AtomicBinOp->getLHS();
5261 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005262 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5263 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5264 if (!X->isLValue()) {
5265 ErrorFound = NotAnLValue;
5266 ErrorLoc = AtomicBinOp->getExprLoc();
5267 ErrorRange = AtomicBinOp->getSourceRange();
5268 NoteLoc = X->getExprLoc();
5269 NoteRange = X->getSourceRange();
5270 }
5271 } else if (!X->isInstantiationDependent() ||
5272 !E->isInstantiationDependent()) {
5273 auto NotScalarExpr =
5274 (X->isInstantiationDependent() || X->getType()->isScalarType())
5275 ? E
5276 : X;
5277 ErrorFound = NotAScalarType;
5278 ErrorLoc = AtomicBinOp->getExprLoc();
5279 ErrorRange = AtomicBinOp->getSourceRange();
5280 NoteLoc = NotScalarExpr->getExprLoc();
5281 NoteRange = NotScalarExpr->getSourceRange();
5282 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005283 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005284 ErrorFound = NotAnAssignmentOp;
5285 ErrorLoc = AtomicBody->getExprLoc();
5286 ErrorRange = AtomicBody->getSourceRange();
5287 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5288 : AtomicBody->getExprLoc();
5289 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5290 : AtomicBody->getSourceRange();
5291 }
5292 } else {
5293 ErrorFound = NotAnExpression;
5294 NoteLoc = ErrorLoc = Body->getLocStart();
5295 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005296 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005297 if (ErrorFound != NoError) {
5298 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5299 << ErrorRange;
5300 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5301 << NoteRange;
5302 return StmtError();
5303 } else if (CurContext->isDependentContext())
5304 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005305 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005306 // If clause is update:
5307 // x++;
5308 // x--;
5309 // ++x;
5310 // --x;
5311 // x binop= expr;
5312 // x = x binop expr;
5313 // x = expr binop x;
5314 OpenMPAtomicUpdateChecker Checker(*this);
5315 if (Checker.checkStatement(
5316 Body, (AtomicKind == OMPC_update)
5317 ? diag::err_omp_atomic_update_not_expression_statement
5318 : diag::err_omp_atomic_not_expression_statement,
5319 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005320 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005321 if (!CurContext->isDependentContext()) {
5322 E = Checker.getExpr();
5323 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005324 UE = Checker.getUpdateExpr();
5325 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005326 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005327 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005328 enum {
5329 NotAnAssignmentOp,
5330 NotACompoundStatement,
5331 NotTwoSubstatements,
5332 NotASpecificExpression,
5333 NoError
5334 } ErrorFound = NoError;
5335 SourceLocation ErrorLoc, NoteLoc;
5336 SourceRange ErrorRange, NoteRange;
5337 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5338 // If clause is a capture:
5339 // v = x++;
5340 // v = x--;
5341 // v = ++x;
5342 // v = --x;
5343 // v = x binop= expr;
5344 // v = x = x binop expr;
5345 // v = x = expr binop x;
5346 auto *AtomicBinOp =
5347 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5348 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5349 V = AtomicBinOp->getLHS();
5350 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5351 OpenMPAtomicUpdateChecker Checker(*this);
5352 if (Checker.checkStatement(
5353 Body, diag::err_omp_atomic_capture_not_expression_statement,
5354 diag::note_omp_atomic_update))
5355 return StmtError();
5356 E = Checker.getExpr();
5357 X = Checker.getX();
5358 UE = Checker.getUpdateExpr();
5359 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5360 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005361 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005362 ErrorLoc = AtomicBody->getExprLoc();
5363 ErrorRange = AtomicBody->getSourceRange();
5364 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5365 : AtomicBody->getExprLoc();
5366 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5367 : AtomicBody->getSourceRange();
5368 ErrorFound = NotAnAssignmentOp;
5369 }
5370 if (ErrorFound != NoError) {
5371 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5372 << ErrorRange;
5373 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5374 return StmtError();
5375 } else if (CurContext->isDependentContext()) {
5376 UE = V = E = X = nullptr;
5377 }
5378 } else {
5379 // If clause is a capture:
5380 // { v = x; x = expr; }
5381 // { v = x; x++; }
5382 // { v = x; x--; }
5383 // { v = x; ++x; }
5384 // { v = x; --x; }
5385 // { v = x; x binop= expr; }
5386 // { v = x; x = x binop expr; }
5387 // { v = x; x = expr binop x; }
5388 // { x++; v = x; }
5389 // { x--; v = x; }
5390 // { ++x; v = x; }
5391 // { --x; v = x; }
5392 // { x binop= expr; v = x; }
5393 // { x = x binop expr; v = x; }
5394 // { x = expr binop x; v = x; }
5395 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5396 // Check that this is { expr1; expr2; }
5397 if (CS->size() == 2) {
5398 auto *First = CS->body_front();
5399 auto *Second = CS->body_back();
5400 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5401 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5402 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5403 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5404 // Need to find what subexpression is 'v' and what is 'x'.
5405 OpenMPAtomicUpdateChecker Checker(*this);
5406 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5407 BinaryOperator *BinOp = nullptr;
5408 if (IsUpdateExprFound) {
5409 BinOp = dyn_cast<BinaryOperator>(First);
5410 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5411 }
5412 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5413 // { v = x; x++; }
5414 // { v = x; x--; }
5415 // { v = x; ++x; }
5416 // { v = x; --x; }
5417 // { v = x; x binop= expr; }
5418 // { v = x; x = x binop expr; }
5419 // { v = x; x = expr binop x; }
5420 // Check that the first expression has form v = x.
5421 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5422 llvm::FoldingSetNodeID XId, PossibleXId;
5423 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5424 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5425 IsUpdateExprFound = XId == PossibleXId;
5426 if (IsUpdateExprFound) {
5427 V = BinOp->getLHS();
5428 X = Checker.getX();
5429 E = Checker.getExpr();
5430 UE = Checker.getUpdateExpr();
5431 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005432 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005433 }
5434 }
5435 if (!IsUpdateExprFound) {
5436 IsUpdateExprFound = !Checker.checkStatement(First);
5437 BinOp = nullptr;
5438 if (IsUpdateExprFound) {
5439 BinOp = dyn_cast<BinaryOperator>(Second);
5440 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5441 }
5442 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5443 // { x++; v = x; }
5444 // { x--; v = x; }
5445 // { ++x; v = x; }
5446 // { --x; v = x; }
5447 // { x binop= expr; v = x; }
5448 // { x = x binop expr; v = x; }
5449 // { x = expr binop x; v = x; }
5450 // Check that the second expression has form v = x.
5451 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5452 llvm::FoldingSetNodeID XId, PossibleXId;
5453 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5454 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5455 IsUpdateExprFound = XId == PossibleXId;
5456 if (IsUpdateExprFound) {
5457 V = BinOp->getLHS();
5458 X = Checker.getX();
5459 E = Checker.getExpr();
5460 UE = Checker.getUpdateExpr();
5461 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005462 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005463 }
5464 }
5465 }
5466 if (!IsUpdateExprFound) {
5467 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005468 auto *FirstExpr = dyn_cast<Expr>(First);
5469 auto *SecondExpr = dyn_cast<Expr>(Second);
5470 if (!FirstExpr || !SecondExpr ||
5471 !(FirstExpr->isInstantiationDependent() ||
5472 SecondExpr->isInstantiationDependent())) {
5473 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5474 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005475 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005476 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5477 : First->getLocStart();
5478 NoteRange = ErrorRange = FirstBinOp
5479 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005480 : SourceRange(ErrorLoc, ErrorLoc);
5481 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005482 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5483 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5484 ErrorFound = NotAnAssignmentOp;
5485 NoteLoc = ErrorLoc = SecondBinOp
5486 ? SecondBinOp->getOperatorLoc()
5487 : Second->getLocStart();
5488 NoteRange = ErrorRange =
5489 SecondBinOp ? SecondBinOp->getSourceRange()
5490 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005491 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005492 auto *PossibleXRHSInFirst =
5493 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5494 auto *PossibleXLHSInSecond =
5495 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5496 llvm::FoldingSetNodeID X1Id, X2Id;
5497 PossibleXRHSInFirst->Profile(X1Id, Context,
5498 /*Canonical=*/true);
5499 PossibleXLHSInSecond->Profile(X2Id, Context,
5500 /*Canonical=*/true);
5501 IsUpdateExprFound = X1Id == X2Id;
5502 if (IsUpdateExprFound) {
5503 V = FirstBinOp->getLHS();
5504 X = SecondBinOp->getLHS();
5505 E = SecondBinOp->getRHS();
5506 UE = nullptr;
5507 IsXLHSInRHSPart = false;
5508 IsPostfixUpdate = true;
5509 } else {
5510 ErrorFound = NotASpecificExpression;
5511 ErrorLoc = FirstBinOp->getExprLoc();
5512 ErrorRange = FirstBinOp->getSourceRange();
5513 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5514 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5515 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005516 }
5517 }
5518 }
5519 }
5520 } else {
5521 NoteLoc = ErrorLoc = Body->getLocStart();
5522 NoteRange = ErrorRange =
5523 SourceRange(Body->getLocStart(), Body->getLocStart());
5524 ErrorFound = NotTwoSubstatements;
5525 }
5526 } else {
5527 NoteLoc = ErrorLoc = Body->getLocStart();
5528 NoteRange = ErrorRange =
5529 SourceRange(Body->getLocStart(), Body->getLocStart());
5530 ErrorFound = NotACompoundStatement;
5531 }
5532 if (ErrorFound != NoError) {
5533 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5534 << ErrorRange;
5535 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5536 return StmtError();
5537 } else if (CurContext->isDependentContext()) {
5538 UE = V = E = X = nullptr;
5539 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005540 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005541 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005542
5543 getCurFunction()->setHasBranchProtectedScope();
5544
Alexey Bataev62cec442014-11-18 10:14:22 +00005545 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005546 X, V, E, UE, IsXLHSInRHSPart,
5547 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005548}
5549
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005550StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5551 Stmt *AStmt,
5552 SourceLocation StartLoc,
5553 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005554 if (!AStmt)
5555 return StmtError();
5556
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005557 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5558 // 1.2.2 OpenMP Language Terminology
5559 // Structured block - An executable statement with a single entry at the
5560 // top and a single exit at the bottom.
5561 // The point of exit cannot be a branch out of the structured block.
5562 // longjmp() and throw() must not violate the entry/exit criteria.
5563 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005564
Alexey Bataev13314bf2014-10-09 04:18:56 +00005565 // OpenMP [2.16, Nesting of Regions]
5566 // If specified, a teams construct must be contained within a target
5567 // construct. That target construct must contain no statements or directives
5568 // outside of the teams construct.
5569 if (DSAStack->hasInnerTeamsRegion()) {
5570 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5571 bool OMPTeamsFound = true;
5572 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5573 auto I = CS->body_begin();
5574 while (I != CS->body_end()) {
5575 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5576 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5577 OMPTeamsFound = false;
5578 break;
5579 }
5580 ++I;
5581 }
5582 assert(I != CS->body_end() && "Not found statement");
5583 S = *I;
5584 }
5585 if (!OMPTeamsFound) {
5586 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5587 Diag(DSAStack->getInnerTeamsRegionLoc(),
5588 diag::note_omp_nested_teams_construct_here);
5589 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5590 << isa<OMPExecutableDirective>(S);
5591 return StmtError();
5592 }
5593 }
5594
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005595 getCurFunction()->setHasBranchProtectedScope();
5596
5597 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5598}
5599
Samuel Antaodf67fc42016-01-19 19:15:56 +00005600/// \brief Check for existence of a map clause in the list of clauses.
5601static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5602 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5603 I != E; ++I) {
5604 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5605 return true;
5606 }
5607 }
5608
5609 return false;
5610}
5611
Michael Wong65f367f2015-07-21 13:44:28 +00005612StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5613 Stmt *AStmt,
5614 SourceLocation StartLoc,
5615 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005616 if (!AStmt)
5617 return StmtError();
5618
5619 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5620
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005621 // OpenMP [2.10.1, Restrictions, p. 97]
5622 // At least one map clause must appear on the directive.
5623 if (!HasMapClause(Clauses)) {
5624 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5625 getOpenMPDirectiveName(OMPD_target_data);
5626 return StmtError();
5627 }
5628
Michael Wong65f367f2015-07-21 13:44:28 +00005629 getCurFunction()->setHasBranchProtectedScope();
5630
5631 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5632 AStmt);
5633}
5634
Samuel Antaodf67fc42016-01-19 19:15:56 +00005635StmtResult
5636Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5637 SourceLocation StartLoc,
5638 SourceLocation EndLoc) {
5639 // OpenMP [2.10.2, Restrictions, p. 99]
5640 // At least one map clause must appear on the directive.
5641 if (!HasMapClause(Clauses)) {
5642 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5643 << getOpenMPDirectiveName(OMPD_target_enter_data);
5644 return StmtError();
5645 }
5646
5647 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5648 Clauses);
5649}
5650
Samuel Antao72590762016-01-19 20:04:50 +00005651StmtResult
5652Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5653 SourceLocation StartLoc,
5654 SourceLocation EndLoc) {
5655 // OpenMP [2.10.3, Restrictions, p. 102]
5656 // At least one map clause must appear on the directive.
5657 if (!HasMapClause(Clauses)) {
5658 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5659 << getOpenMPDirectiveName(OMPD_target_exit_data);
5660 return StmtError();
5661 }
5662
5663 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5664}
5665
Alexey Bataev13314bf2014-10-09 04:18:56 +00005666StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5667 Stmt *AStmt, SourceLocation StartLoc,
5668 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005669 if (!AStmt)
5670 return StmtError();
5671
Alexey Bataev13314bf2014-10-09 04:18:56 +00005672 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5673 // 1.2.2 OpenMP Language Terminology
5674 // Structured block - An executable statement with a single entry at the
5675 // top and a single exit at the bottom.
5676 // The point of exit cannot be a branch out of the structured block.
5677 // longjmp() and throw() must not violate the entry/exit criteria.
5678 CS->getCapturedDecl()->setNothrow();
5679
5680 getCurFunction()->setHasBranchProtectedScope();
5681
5682 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5683}
5684
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005685StmtResult
5686Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5687 SourceLocation EndLoc,
5688 OpenMPDirectiveKind CancelRegion) {
5689 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5690 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5691 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5692 << getOpenMPDirectiveName(CancelRegion);
5693 return StmtError();
5694 }
5695 if (DSAStack->isParentNowaitRegion()) {
5696 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5697 return StmtError();
5698 }
5699 if (DSAStack->isParentOrderedRegion()) {
5700 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5701 return StmtError();
5702 }
5703 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5704 CancelRegion);
5705}
5706
Alexey Bataev87933c72015-09-18 08:07:34 +00005707StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5708 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005709 SourceLocation EndLoc,
5710 OpenMPDirectiveKind CancelRegion) {
5711 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5712 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5713 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5714 << getOpenMPDirectiveName(CancelRegion);
5715 return StmtError();
5716 }
5717 if (DSAStack->isParentNowaitRegion()) {
5718 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5719 return StmtError();
5720 }
5721 if (DSAStack->isParentOrderedRegion()) {
5722 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5723 return StmtError();
5724 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005725 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005726 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5727 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005728}
5729
Alexey Bataev382967a2015-12-08 12:06:20 +00005730static bool checkGrainsizeNumTasksClauses(Sema &S,
5731 ArrayRef<OMPClause *> Clauses) {
5732 OMPClause *PrevClause = nullptr;
5733 bool ErrorFound = false;
5734 for (auto *C : Clauses) {
5735 if (C->getClauseKind() == OMPC_grainsize ||
5736 C->getClauseKind() == OMPC_num_tasks) {
5737 if (!PrevClause)
5738 PrevClause = C;
5739 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5740 S.Diag(C->getLocStart(),
5741 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5742 << getOpenMPClauseName(C->getClauseKind())
5743 << getOpenMPClauseName(PrevClause->getClauseKind());
5744 S.Diag(PrevClause->getLocStart(),
5745 diag::note_omp_previous_grainsize_num_tasks)
5746 << getOpenMPClauseName(PrevClause->getClauseKind());
5747 ErrorFound = true;
5748 }
5749 }
5750 }
5751 return ErrorFound;
5752}
5753
Alexey Bataev49f6e782015-12-01 04:18:41 +00005754StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5755 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5756 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005757 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005758 if (!AStmt)
5759 return StmtError();
5760
5761 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5762 OMPLoopDirective::HelperExprs B;
5763 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5764 // define the nested loops number.
5765 unsigned NestedLoopCount =
5766 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005767 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005768 VarsWithImplicitDSA, B);
5769 if (NestedLoopCount == 0)
5770 return StmtError();
5771
5772 assert((CurContext->isDependentContext() || B.builtAll()) &&
5773 "omp for loop exprs were not built");
5774
Alexey Bataev382967a2015-12-08 12:06:20 +00005775 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5776 // The grainsize clause and num_tasks clause are mutually exclusive and may
5777 // not appear on the same taskloop directive.
5778 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5779 return StmtError();
5780
Alexey Bataev49f6e782015-12-01 04:18:41 +00005781 getCurFunction()->setHasBranchProtectedScope();
5782 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5783 NestedLoopCount, Clauses, AStmt, B);
5784}
5785
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005786StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5787 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5788 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005789 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005790 if (!AStmt)
5791 return StmtError();
5792
5793 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5794 OMPLoopDirective::HelperExprs B;
5795 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5796 // define the nested loops number.
5797 unsigned NestedLoopCount =
5798 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5799 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5800 VarsWithImplicitDSA, B);
5801 if (NestedLoopCount == 0)
5802 return StmtError();
5803
5804 assert((CurContext->isDependentContext() || B.builtAll()) &&
5805 "omp for loop exprs were not built");
5806
Alexey Bataev382967a2015-12-08 12:06:20 +00005807 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5808 // The grainsize clause and num_tasks clause are mutually exclusive and may
5809 // not appear on the same taskloop directive.
5810 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5811 return StmtError();
5812
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005813 getCurFunction()->setHasBranchProtectedScope();
5814 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5815 NestedLoopCount, Clauses, AStmt, B);
5816}
5817
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005818StmtResult Sema::ActOnOpenMPDistributeDirective(
5819 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5820 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005821 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005822 if (!AStmt)
5823 return StmtError();
5824
5825 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5826 OMPLoopDirective::HelperExprs B;
5827 // In presence of clause 'collapse' with number of loops, it will
5828 // define the nested loops number.
5829 unsigned NestedLoopCount =
5830 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5831 nullptr /*ordered not a clause on distribute*/, AStmt,
5832 *this, *DSAStack, VarsWithImplicitDSA, B);
5833 if (NestedLoopCount == 0)
5834 return StmtError();
5835
5836 assert((CurContext->isDependentContext() || B.builtAll()) &&
5837 "omp for loop exprs were not built");
5838
5839 getCurFunction()->setHasBranchProtectedScope();
5840 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5841 NestedLoopCount, Clauses, AStmt, B);
5842}
5843
Alexey Bataeved09d242014-05-28 05:53:51 +00005844OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005845 SourceLocation StartLoc,
5846 SourceLocation LParenLoc,
5847 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005848 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005849 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005850 case OMPC_final:
5851 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5852 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005853 case OMPC_num_threads:
5854 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5855 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005856 case OMPC_safelen:
5857 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5858 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005859 case OMPC_simdlen:
5860 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5861 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005862 case OMPC_collapse:
5863 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5864 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005865 case OMPC_ordered:
5866 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5867 break;
Michael Wonge710d542015-08-07 16:16:36 +00005868 case OMPC_device:
5869 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5870 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005871 case OMPC_num_teams:
5872 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5873 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005874 case OMPC_thread_limit:
5875 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5876 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005877 case OMPC_priority:
5878 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5879 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005880 case OMPC_grainsize:
5881 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5882 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005883 case OMPC_num_tasks:
5884 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5885 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005886 case OMPC_hint:
5887 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5888 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005889 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005890 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005891 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005892 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005893 case OMPC_private:
5894 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005895 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005896 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005897 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005898 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005899 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005900 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005901 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005902 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005903 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005904 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005905 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005906 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005907 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005908 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005909 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005910 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005911 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005912 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005913 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005914 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005915 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005916 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00005917 case OMPC_dist_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005918 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005919 llvm_unreachable("Clause is not allowed.");
5920 }
5921 return Res;
5922}
5923
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005924OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5925 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005926 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005927 SourceLocation NameModifierLoc,
5928 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005929 SourceLocation EndLoc) {
5930 Expr *ValExpr = Condition;
5931 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5932 !Condition->isInstantiationDependent() &&
5933 !Condition->containsUnexpandedParameterPack()) {
5934 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005935 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005936 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005937 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005938
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005939 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005940 }
5941
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005942 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5943 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005944}
5945
Alexey Bataev3778b602014-07-17 07:32:53 +00005946OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5947 SourceLocation StartLoc,
5948 SourceLocation LParenLoc,
5949 SourceLocation EndLoc) {
5950 Expr *ValExpr = Condition;
5951 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5952 !Condition->isInstantiationDependent() &&
5953 !Condition->containsUnexpandedParameterPack()) {
5954 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5955 Condition->getExprLoc(), Condition);
5956 if (Val.isInvalid())
5957 return nullptr;
5958
5959 ValExpr = Val.get();
5960 }
5961
5962 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5963}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005964ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5965 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005966 if (!Op)
5967 return ExprError();
5968
5969 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5970 public:
5971 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005972 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005973 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5974 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005975 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5976 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005977 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5978 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005979 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5980 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005981 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5982 QualType T,
5983 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005984 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5985 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005986 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5987 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005988 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005989 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005990 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005991 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5992 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005993 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5994 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005995 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5996 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005997 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005998 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005999 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006000 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6001 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006002 llvm_unreachable("conversion functions are permitted");
6003 }
6004 } ConvertDiagnoser;
6005 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6006}
6007
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006008static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006009 OpenMPClauseKind CKind,
6010 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006011 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6012 !ValExpr->isInstantiationDependent()) {
6013 SourceLocation Loc = ValExpr->getExprLoc();
6014 ExprResult Value =
6015 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6016 if (Value.isInvalid())
6017 return false;
6018
6019 ValExpr = Value.get();
6020 // The expression must evaluate to a non-negative integer value.
6021 llvm::APSInt Result;
6022 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006023 Result.isSigned() &&
6024 !((!StrictlyPositive && Result.isNonNegative()) ||
6025 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006026 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006027 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6028 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006029 return false;
6030 }
6031 }
6032 return true;
6033}
6034
Alexey Bataev568a8332014-03-06 06:15:19 +00006035OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6036 SourceLocation StartLoc,
6037 SourceLocation LParenLoc,
6038 SourceLocation EndLoc) {
6039 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006040
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006041 // OpenMP [2.5, Restrictions]
6042 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006043 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6044 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006045 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006046
Alexey Bataeved09d242014-05-28 05:53:51 +00006047 return new (Context)
6048 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006049}
6050
Alexey Bataev62c87d22014-03-21 04:51:18 +00006051ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006052 OpenMPClauseKind CKind,
6053 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006054 if (!E)
6055 return ExprError();
6056 if (E->isValueDependent() || E->isTypeDependent() ||
6057 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006058 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006059 llvm::APSInt Result;
6060 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6061 if (ICE.isInvalid())
6062 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006063 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6064 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006065 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006066 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6067 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006068 return ExprError();
6069 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006070 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6071 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6072 << E->getSourceRange();
6073 return ExprError();
6074 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006075 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6076 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006077 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006078 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006079 return ICE;
6080}
6081
6082OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6083 SourceLocation LParenLoc,
6084 SourceLocation EndLoc) {
6085 // OpenMP [2.8.1, simd construct, Description]
6086 // The parameter of the safelen clause must be a constant
6087 // positive integer expression.
6088 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6089 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006090 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006091 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006092 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006093}
6094
Alexey Bataev66b15b52015-08-21 11:14:16 +00006095OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6096 SourceLocation LParenLoc,
6097 SourceLocation EndLoc) {
6098 // OpenMP [2.8.1, simd construct, Description]
6099 // The parameter of the simdlen clause must be a constant
6100 // positive integer expression.
6101 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6102 if (Simdlen.isInvalid())
6103 return nullptr;
6104 return new (Context)
6105 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6106}
6107
Alexander Musman64d33f12014-06-04 07:53:32 +00006108OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6109 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006110 SourceLocation LParenLoc,
6111 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006112 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006113 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006114 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006115 // The parameter of the collapse clause must be a constant
6116 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006117 ExprResult NumForLoopsResult =
6118 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6119 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006120 return nullptr;
6121 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006122 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006123}
6124
Alexey Bataev10e775f2015-07-30 11:36:16 +00006125OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6126 SourceLocation EndLoc,
6127 SourceLocation LParenLoc,
6128 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006129 // OpenMP [2.7.1, loop construct, Description]
6130 // OpenMP [2.8.1, simd construct, Description]
6131 // OpenMP [2.9.6, distribute construct, Description]
6132 // The parameter of the ordered clause must be a constant
6133 // positive integer expression if any.
6134 if (NumForLoops && LParenLoc.isValid()) {
6135 ExprResult NumForLoopsResult =
6136 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6137 if (NumForLoopsResult.isInvalid())
6138 return nullptr;
6139 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006140 } else
6141 NumForLoops = nullptr;
6142 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006143 return new (Context)
6144 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6145}
6146
Alexey Bataeved09d242014-05-28 05:53:51 +00006147OMPClause *Sema::ActOnOpenMPSimpleClause(
6148 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6149 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006150 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006151 switch (Kind) {
6152 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006153 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006154 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6155 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006156 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006157 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006158 Res = ActOnOpenMPProcBindClause(
6159 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6160 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006161 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006162 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006163 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006164 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006165 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006166 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006167 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006168 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006169 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006170 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006171 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006172 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006173 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006174 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006175 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006176 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006177 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006178 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006179 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006180 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006181 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006182 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006183 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006184 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006185 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006186 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006187 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006188 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006189 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006190 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006191 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006192 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006193 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006194 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006195 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006196 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006197 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006198 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006199 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006200 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006201 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006202 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006203 llvm_unreachable("Clause is not allowed.");
6204 }
6205 return Res;
6206}
6207
Alexey Bataev6402bca2015-12-28 07:25:51 +00006208static std::string
6209getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6210 ArrayRef<unsigned> Exclude = llvm::None) {
6211 std::string Values;
6212 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6213 unsigned Skipped = Exclude.size();
6214 auto S = Exclude.begin(), E = Exclude.end();
6215 for (unsigned i = First; i < Last; ++i) {
6216 if (std::find(S, E, i) != E) {
6217 --Skipped;
6218 continue;
6219 }
6220 Values += "'";
6221 Values += getOpenMPSimpleClauseTypeName(K, i);
6222 Values += "'";
6223 if (i == Bound - Skipped)
6224 Values += " or ";
6225 else if (i != Bound + 1 - Skipped)
6226 Values += ", ";
6227 }
6228 return Values;
6229}
6230
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006231OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6232 SourceLocation KindKwLoc,
6233 SourceLocation StartLoc,
6234 SourceLocation LParenLoc,
6235 SourceLocation EndLoc) {
6236 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006237 static_assert(OMPC_DEFAULT_unknown > 0,
6238 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006239 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006240 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6241 /*Last=*/OMPC_DEFAULT_unknown)
6242 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006243 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006245 switch (Kind) {
6246 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006247 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006248 break;
6249 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006250 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006251 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006252 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006253 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006254 break;
6255 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006256 return new (Context)
6257 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006258}
6259
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006260OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6261 SourceLocation KindKwLoc,
6262 SourceLocation StartLoc,
6263 SourceLocation LParenLoc,
6264 SourceLocation EndLoc) {
6265 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006266 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006267 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6268 /*Last=*/OMPC_PROC_BIND_unknown)
6269 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006270 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006271 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006272 return new (Context)
6273 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006274}
6275
Alexey Bataev56dafe82014-06-20 07:16:17 +00006276OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006277 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006278 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006279 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006280 SourceLocation EndLoc) {
6281 OMPClause *Res = nullptr;
6282 switch (Kind) {
6283 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006284 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6285 assert(Argument.size() == NumberOfElements &&
6286 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006287 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006288 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6289 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6290 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6291 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6292 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006293 break;
6294 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006295 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6296 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6297 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6298 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006299 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006300 case OMPC_dist_schedule:
6301 Res = ActOnOpenMPDistScheduleClause(
6302 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6303 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6304 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006305 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006306 case OMPC_num_threads:
6307 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006308 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006309 case OMPC_collapse:
6310 case OMPC_default:
6311 case OMPC_proc_bind:
6312 case OMPC_private:
6313 case OMPC_firstprivate:
6314 case OMPC_lastprivate:
6315 case OMPC_shared:
6316 case OMPC_reduction:
6317 case OMPC_linear:
6318 case OMPC_aligned:
6319 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006320 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006321 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006322 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006323 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006324 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006325 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006326 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006327 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006328 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006329 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006330 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006331 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006332 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006333 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006334 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006335 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006336 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006337 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006338 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006339 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006340 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006341 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006342 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006343 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006344 case OMPC_unknown:
6345 llvm_unreachable("Clause is not allowed.");
6346 }
6347 return Res;
6348}
6349
Alexey Bataev6402bca2015-12-28 07:25:51 +00006350static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6351 OpenMPScheduleClauseModifier M2,
6352 SourceLocation M1Loc, SourceLocation M2Loc) {
6353 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6354 SmallVector<unsigned, 2> Excluded;
6355 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6356 Excluded.push_back(M2);
6357 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6358 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6359 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6360 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6361 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6362 << getListOfPossibleValues(OMPC_schedule,
6363 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6364 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6365 Excluded)
6366 << getOpenMPClauseName(OMPC_schedule);
6367 return true;
6368 }
6369 return false;
6370}
6371
Alexey Bataev56dafe82014-06-20 07:16:17 +00006372OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006373 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006374 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006375 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6376 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6377 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6378 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6379 return nullptr;
6380 // OpenMP, 2.7.1, Loop Construct, Restrictions
6381 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6382 // but not both.
6383 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6384 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6385 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6386 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6387 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6388 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6389 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6390 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6391 return nullptr;
6392 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006393 if (Kind == OMPC_SCHEDULE_unknown) {
6394 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006395 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6396 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6397 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6398 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6399 Exclude);
6400 } else {
6401 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6402 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006403 }
6404 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6405 << Values << getOpenMPClauseName(OMPC_schedule);
6406 return nullptr;
6407 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006408 // OpenMP, 2.7.1, Loop Construct, Restrictions
6409 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6410 // schedule(guided).
6411 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6412 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6413 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6414 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6415 diag::err_omp_schedule_nonmonotonic_static);
6416 return nullptr;
6417 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006418 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006419 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006420 if (ChunkSize) {
6421 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6422 !ChunkSize->isInstantiationDependent() &&
6423 !ChunkSize->containsUnexpandedParameterPack()) {
6424 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6425 ExprResult Val =
6426 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6427 if (Val.isInvalid())
6428 return nullptr;
6429
6430 ValExpr = Val.get();
6431
6432 // OpenMP [2.7.1, Restrictions]
6433 // chunk_size must be a loop invariant integer expression with a positive
6434 // value.
6435 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006436 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6437 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6438 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006439 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006440 return nullptr;
6441 }
6442 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6443 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6444 ChunkSize->getType(), ".chunk.");
6445 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6446 ChunkSize->getExprLoc(),
6447 /*RefersToCapture=*/true);
6448 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006449 }
6450 }
6451 }
6452
Alexey Bataev6402bca2015-12-28 07:25:51 +00006453 return new (Context)
6454 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6455 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006456}
6457
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006458OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6459 SourceLocation StartLoc,
6460 SourceLocation EndLoc) {
6461 OMPClause *Res = nullptr;
6462 switch (Kind) {
6463 case OMPC_ordered:
6464 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6465 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006466 case OMPC_nowait:
6467 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6468 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006469 case OMPC_untied:
6470 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6471 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006472 case OMPC_mergeable:
6473 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6474 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006475 case OMPC_read:
6476 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6477 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006478 case OMPC_write:
6479 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6480 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006481 case OMPC_update:
6482 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6483 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006484 case OMPC_capture:
6485 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6486 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006487 case OMPC_seq_cst:
6488 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6489 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006490 case OMPC_threads:
6491 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6492 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006493 case OMPC_simd:
6494 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6495 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006496 case OMPC_nogroup:
6497 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6498 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006499 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006500 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006501 case OMPC_num_threads:
6502 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006503 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006504 case OMPC_collapse:
6505 case OMPC_schedule:
6506 case OMPC_private:
6507 case OMPC_firstprivate:
6508 case OMPC_lastprivate:
6509 case OMPC_shared:
6510 case OMPC_reduction:
6511 case OMPC_linear:
6512 case OMPC_aligned:
6513 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006514 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006515 case OMPC_default:
6516 case OMPC_proc_bind:
6517 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006518 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006519 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006520 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006521 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006522 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006523 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006524 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006525 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006526 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006527 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006528 case OMPC_dist_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006529 case OMPC_unknown:
6530 llvm_unreachable("Clause is not allowed.");
6531 }
6532 return Res;
6533}
6534
Alexey Bataev236070f2014-06-20 11:19:47 +00006535OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6536 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006537 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006538 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6539}
6540
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006541OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6542 SourceLocation EndLoc) {
6543 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6544}
6545
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006546OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6547 SourceLocation EndLoc) {
6548 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6549}
6550
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006551OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6552 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006553 return new (Context) OMPReadClause(StartLoc, EndLoc);
6554}
6555
Alexey Bataevdea47612014-07-23 07:46:59 +00006556OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6557 SourceLocation EndLoc) {
6558 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6559}
6560
Alexey Bataev67a4f222014-07-23 10:25:33 +00006561OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6562 SourceLocation EndLoc) {
6563 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6564}
6565
Alexey Bataev459dec02014-07-24 06:46:57 +00006566OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6567 SourceLocation EndLoc) {
6568 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6569}
6570
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006571OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6572 SourceLocation EndLoc) {
6573 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6574}
6575
Alexey Bataev346265e2015-09-25 10:37:12 +00006576OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6577 SourceLocation EndLoc) {
6578 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6579}
6580
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006581OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6582 SourceLocation EndLoc) {
6583 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6584}
6585
Alexey Bataevb825de12015-12-07 10:51:44 +00006586OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6587 SourceLocation EndLoc) {
6588 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6589}
6590
Alexey Bataevc5e02582014-06-16 07:08:35 +00006591OMPClause *Sema::ActOnOpenMPVarListClause(
6592 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6593 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6594 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006595 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006596 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6597 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6598 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006599 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006600 switch (Kind) {
6601 case OMPC_private:
6602 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6603 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006604 case OMPC_firstprivate:
6605 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6606 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006607 case OMPC_lastprivate:
6608 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6609 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006610 case OMPC_shared:
6611 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6612 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006613 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006614 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6615 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006616 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006617 case OMPC_linear:
6618 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006619 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006620 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006621 case OMPC_aligned:
6622 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6623 ColonLoc, EndLoc);
6624 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006625 case OMPC_copyin:
6626 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6627 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006628 case OMPC_copyprivate:
6629 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6630 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006631 case OMPC_flush:
6632 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6633 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006634 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006635 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6636 StartLoc, LParenLoc, EndLoc);
6637 break;
6638 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006639 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6640 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6641 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006642 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006643 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006644 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006645 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006646 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006647 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006648 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006649 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006650 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006651 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006652 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006653 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006654 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006655 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006656 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006657 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006658 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006659 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006660 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006661 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006662 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006663 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006664 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006665 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006666 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006667 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006668 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006669 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006670 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006671 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006672 case OMPC_dist_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006673 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006674 llvm_unreachable("Clause is not allowed.");
6675 }
6676 return Res;
6677}
6678
6679OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6680 SourceLocation StartLoc,
6681 SourceLocation LParenLoc,
6682 SourceLocation EndLoc) {
6683 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006684 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006685 for (auto &RefExpr : VarList) {
6686 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006687 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6688 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006689 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006690 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006691 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006692 continue;
6693 }
6694
Alexey Bataeved09d242014-05-28 05:53:51 +00006695 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006696 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006697 // A list item is a variable name.
6698 // OpenMP [2.9.3.3, Restrictions, p.1]
6699 // A variable that is part of another variable (as an array or
6700 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006701 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6702 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6703 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6704 (getCurrentThisType().isNull() || !ME ||
6705 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6706 !isa<FieldDecl>(ME->getMemberDecl()))) {
6707 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6708 << (getCurrentThisType().isNull() ? 0 : 1)
6709 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006710 continue;
6711 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006712 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6713 QualType Type = D->getType();
6714 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006715
6716 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6717 // A variable that appears in a private clause must not have an incomplete
6718 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006719 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006720 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006721 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006722
Alexey Bataev758e55e2013-09-06 18:03:48 +00006723 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6724 // in a Construct]
6725 // Variables with the predetermined data-sharing attributes may not be
6726 // listed in data-sharing attributes clauses, except for the cases
6727 // listed below. For these exceptions only, listing a predetermined
6728 // variable in a data-sharing attribute clause is allowed and overrides
6729 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006730 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006731 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006732 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6733 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006734 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006735 continue;
6736 }
6737
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006738 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006739 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006740 DSAStack->getCurrentDirective() == OMPD_task) {
6741 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6742 << getOpenMPClauseName(OMPC_private) << Type
6743 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6744 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006745 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006746 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006747 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006748 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006749 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006750 continue;
6751 }
6752
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006753 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6754 // A variable of class type (or array thereof) that appears in a private
6755 // clause requires an accessible, unambiguous default constructor for the
6756 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006757 // Generate helper private variable and initialize it with the default
6758 // value. The address of the original variable is replaced by the address of
6759 // the new private variable in CodeGen. This new variable is not added to
6760 // IdResolver, so the code in the OpenMP region uses original variable for
6761 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006762 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006763 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
6764 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006765 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006766 if (VDPrivate->isInvalidDecl())
6767 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006768 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006769 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006770
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006771 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
6772 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006773 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006774 }
6775
Alexey Bataeved09d242014-05-28 05:53:51 +00006776 if (Vars.empty())
6777 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006778
Alexey Bataev03b340a2014-10-21 03:16:40 +00006779 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6780 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006781}
6782
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006783namespace {
6784class DiagsUninitializedSeveretyRAII {
6785private:
6786 DiagnosticsEngine &Diags;
6787 SourceLocation SavedLoc;
6788 bool IsIgnored;
6789
6790public:
6791 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6792 bool IsIgnored)
6793 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6794 if (!IsIgnored) {
6795 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6796 /*Map*/ diag::Severity::Ignored, Loc);
6797 }
6798 }
6799 ~DiagsUninitializedSeveretyRAII() {
6800 if (!IsIgnored)
6801 Diags.popMappings(SavedLoc);
6802 }
6803};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006804}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006805
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006806OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6807 SourceLocation StartLoc,
6808 SourceLocation LParenLoc,
6809 SourceLocation EndLoc) {
6810 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006811 SmallVector<Expr *, 8> PrivateCopies;
6812 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006813 bool IsImplicitClause =
6814 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6815 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6816
Alexey Bataeved09d242014-05-28 05:53:51 +00006817 for (auto &RefExpr : VarList) {
6818 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6819 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006820 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006821 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006822 PrivateCopies.push_back(nullptr);
6823 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006824 continue;
6825 }
6826
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006827 SourceLocation ELoc =
6828 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006829 // OpenMP [2.1, C/C++]
6830 // A list item is a variable name.
6831 // OpenMP [2.9.3.3, Restrictions, p.1]
6832 // A variable that is part of another variable (as an array or
6833 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006834 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006835 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006836 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6837 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006838 continue;
6839 }
6840 Decl *D = DE->getDecl();
6841 VarDecl *VD = cast<VarDecl>(D);
6842
6843 QualType Type = VD->getType();
6844 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6845 // It will be analyzed later.
6846 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006847 PrivateCopies.push_back(nullptr);
6848 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006849 continue;
6850 }
6851
6852 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6853 // A variable that appears in a private clause must not have an incomplete
6854 // type or a reference type.
6855 if (RequireCompleteType(ELoc, Type,
6856 diag::err_omp_firstprivate_incomplete_type)) {
6857 continue;
6858 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006859 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006860
6861 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6862 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006863 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006864 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006865 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006866
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006867 // If an implicit firstprivate variable found it was checked already.
6868 if (!IsImplicitClause) {
6869 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006870 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006871 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6872 // A list item that specifies a given variable may not appear in more
6873 // than one clause on the same directive, except that a variable may be
6874 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006875 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006876 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006877 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006878 << getOpenMPClauseName(DVar.CKind)
6879 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006880 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006881 continue;
6882 }
6883
6884 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6885 // in a Construct]
6886 // Variables with the predetermined data-sharing attributes may not be
6887 // listed in data-sharing attributes clauses, except for the cases
6888 // listed below. For these exceptions only, listing a predetermined
6889 // variable in a data-sharing attribute clause is allowed and overrides
6890 // the variable's predetermined data-sharing attributes.
6891 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6892 // in a Construct, C/C++, p.2]
6893 // Variables with const-qualified type having no mutable member may be
6894 // listed in a firstprivate clause, even if they are static data members.
6895 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6896 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6897 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006898 << getOpenMPClauseName(DVar.CKind)
6899 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006900 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006901 continue;
6902 }
6903
Alexey Bataevf29276e2014-06-18 04:14:57 +00006904 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006905 // OpenMP [2.9.3.4, Restrictions, p.2]
6906 // A list item that is private within a parallel region must not appear
6907 // in a firstprivate clause on a worksharing construct if any of the
6908 // worksharing regions arising from the worksharing construct ever bind
6909 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006910 if (isOpenMPWorksharingDirective(CurrDir) &&
6911 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006912 DVar = DSAStack->getImplicitDSA(VD, true);
6913 if (DVar.CKind != OMPC_shared &&
6914 (isOpenMPParallelDirective(DVar.DKind) ||
6915 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006916 Diag(ELoc, diag::err_omp_required_access)
6917 << getOpenMPClauseName(OMPC_firstprivate)
6918 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006919 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006920 continue;
6921 }
6922 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006923 // OpenMP [2.9.3.4, Restrictions, p.3]
6924 // A list item that appears in a reduction clause of a parallel construct
6925 // must not appear in a firstprivate clause on a worksharing or task
6926 // construct if any of the worksharing or task regions arising from the
6927 // worksharing or task construct ever bind to any of the parallel regions
6928 // arising from the parallel construct.
6929 // OpenMP [2.9.3.4, Restrictions, p.4]
6930 // A list item that appears in a reduction clause in worksharing
6931 // construct must not appear in a firstprivate clause in a task construct
6932 // encountered during execution of any of the worksharing regions arising
6933 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006934 if (CurrDir == OMPD_task) {
6935 DVar =
6936 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6937 [](OpenMPDirectiveKind K) -> bool {
6938 return isOpenMPParallelDirective(K) ||
6939 isOpenMPWorksharingDirective(K);
6940 },
6941 false);
6942 if (DVar.CKind == OMPC_reduction &&
6943 (isOpenMPParallelDirective(DVar.DKind) ||
6944 isOpenMPWorksharingDirective(DVar.DKind))) {
6945 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6946 << getOpenMPDirectiveName(DVar.DKind);
6947 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6948 continue;
6949 }
6950 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006951
6952 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6953 // A list item that is private within a teams region must not appear in a
6954 // firstprivate clause on a distribute construct if any of the distribute
6955 // regions arising from the distribute construct ever bind to any of the
6956 // teams regions arising from the teams construct.
6957 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6958 // A list item that appears in a reduction clause of a teams construct
6959 // must not appear in a firstprivate clause on a distribute construct if
6960 // any of the distribute regions arising from the distribute construct
6961 // ever bind to any of the teams regions arising from the teams construct.
6962 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6963 // A list item may appear in a firstprivate or lastprivate clause but not
6964 // both.
6965 if (CurrDir == OMPD_distribute) {
6966 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6967 [](OpenMPDirectiveKind K) -> bool {
6968 return isOpenMPTeamsDirective(K);
6969 },
6970 false);
6971 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6972 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6973 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6974 continue;
6975 }
6976 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6977 [](OpenMPDirectiveKind K) -> bool {
6978 return isOpenMPTeamsDirective(K);
6979 },
6980 false);
6981 if (DVar.CKind == OMPC_reduction &&
6982 isOpenMPTeamsDirective(DVar.DKind)) {
6983 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6984 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6985 continue;
6986 }
6987 DVar = DSAStack->getTopDSA(VD, false);
6988 if (DVar.CKind == OMPC_lastprivate) {
6989 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6990 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6991 continue;
6992 }
6993 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006994 }
6995
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006996 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006997 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006998 DSAStack->getCurrentDirective() == OMPD_task) {
6999 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7000 << getOpenMPClauseName(OMPC_firstprivate) << Type
7001 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7002 bool IsDecl =
7003 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7004 Diag(VD->getLocation(),
7005 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7006 << VD;
7007 continue;
7008 }
7009
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007010 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007011 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7012 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007013 // Generate helper private variable and initialize it with the value of the
7014 // original variable. The address of the original variable is replaced by
7015 // the address of the new private variable in the CodeGen. This new variable
7016 // is not added to IdResolver, so the code in the OpenMP region uses
7017 // original variable for proper diagnostics and variable capturing.
7018 Expr *VDInitRefExpr = nullptr;
7019 // For arrays generate initializer for single element and replace it by the
7020 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007021 if (Type->isArrayType()) {
7022 auto VDInit =
7023 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7024 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007025 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007026 ElemType = ElemType.getUnqualifiedType();
7027 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7028 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007029 InitializedEntity Entity =
7030 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007031 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7032
7033 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7034 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7035 if (Result.isInvalid())
7036 VDPrivate->setInvalidDecl();
7037 else
7038 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007039 // Remove temp variable declaration.
7040 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007041 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007042 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007043 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007044 VDInitRefExpr =
7045 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007046 AddInitializerToDecl(VDPrivate,
7047 DefaultLvalueConversion(VDInitRefExpr).get(),
7048 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007049 }
7050 if (VDPrivate->isInvalidDecl()) {
7051 if (IsImplicitClause) {
7052 Diag(DE->getExprLoc(),
7053 diag::note_omp_task_predetermined_firstprivate_here);
7054 }
7055 continue;
7056 }
7057 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007058 auto VDPrivateRefExpr = buildDeclRefExpr(
7059 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007060 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7061 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007062 PrivateCopies.push_back(VDPrivateRefExpr);
7063 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007064 }
7065
Alexey Bataeved09d242014-05-28 05:53:51 +00007066 if (Vars.empty())
7067 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007068
7069 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007070 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007071}
7072
Alexander Musman1bb328c2014-06-04 13:06:39 +00007073OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7074 SourceLocation StartLoc,
7075 SourceLocation LParenLoc,
7076 SourceLocation EndLoc) {
7077 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007078 SmallVector<Expr *, 8> SrcExprs;
7079 SmallVector<Expr *, 8> DstExprs;
7080 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007081 for (auto &RefExpr : VarList) {
7082 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7083 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7084 // It will be analyzed later.
7085 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007086 SrcExprs.push_back(nullptr);
7087 DstExprs.push_back(nullptr);
7088 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007089 continue;
7090 }
7091
7092 SourceLocation ELoc = RefExpr->getExprLoc();
7093 // OpenMP [2.1, C/C++]
7094 // A list item is a variable name.
7095 // OpenMP [2.14.3.5, Restrictions, p.1]
7096 // A variable that is part of another variable (as an array or structure
7097 // element) cannot appear in a lastprivate clause.
7098 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7099 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007100 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7101 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007102 continue;
7103 }
7104 Decl *D = DE->getDecl();
7105 VarDecl *VD = cast<VarDecl>(D);
7106
7107 QualType Type = VD->getType();
7108 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7109 // It will be analyzed later.
7110 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007111 SrcExprs.push_back(nullptr);
7112 DstExprs.push_back(nullptr);
7113 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007114 continue;
7115 }
7116
7117 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7118 // A variable that appears in a lastprivate clause must not have an
7119 // incomplete type or a reference type.
7120 if (RequireCompleteType(ELoc, Type,
7121 diag::err_omp_lastprivate_incomplete_type)) {
7122 continue;
7123 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007124 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007125
7126 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7127 // in a Construct]
7128 // Variables with the predetermined data-sharing attributes may not be
7129 // listed in data-sharing attributes clauses, except for the cases
7130 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007131 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007132 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7133 DVar.CKind != OMPC_firstprivate &&
7134 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7135 Diag(ELoc, diag::err_omp_wrong_dsa)
7136 << getOpenMPClauseName(DVar.CKind)
7137 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007138 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007139 continue;
7140 }
7141
Alexey Bataevf29276e2014-06-18 04:14:57 +00007142 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7143 // OpenMP [2.14.3.5, Restrictions, p.2]
7144 // A list item that is private within a parallel region, or that appears in
7145 // the reduction clause of a parallel construct, must not appear in a
7146 // lastprivate clause on a worksharing construct if any of the corresponding
7147 // worksharing regions ever binds to any of the corresponding parallel
7148 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007149 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007150 if (isOpenMPWorksharingDirective(CurrDir) &&
7151 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007152 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007153 if (DVar.CKind != OMPC_shared) {
7154 Diag(ELoc, diag::err_omp_required_access)
7155 << getOpenMPClauseName(OMPC_lastprivate)
7156 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007157 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007158 continue;
7159 }
7160 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007161 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007162 // A variable of class type (or array thereof) that appears in a
7163 // lastprivate clause requires an accessible, unambiguous default
7164 // constructor for the class type, unless the list item is also specified
7165 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007166 // A variable of class type (or array thereof) that appears in a
7167 // lastprivate clause requires an accessible, unambiguous copy assignment
7168 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007169 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007170 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007171 Type.getUnqualifiedType(), ".lastprivate.src",
7172 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007173 auto *PseudoSrcExpr = buildDeclRefExpr(
7174 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007175 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007176 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7177 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007178 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007179 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007180 // For arrays generate assignment operation for single element and replace
7181 // it by the original array element in CodeGen.
7182 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7183 PseudoDstExpr, PseudoSrcExpr);
7184 if (AssignmentOp.isInvalid())
7185 continue;
7186 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7187 /*DiscardedValue=*/true);
7188 if (AssignmentOp.isInvalid())
7189 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007190
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007191 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7192 // A list item may appear in a firstprivate or lastprivate clause but not
7193 // both.
7194 if (CurrDir == OMPD_distribute) {
7195 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7196 if (DVar.CKind == OMPC_firstprivate) {
7197 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7198 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7199 continue;
7200 }
7201 }
7202
Alexey Bataev39f915b82015-05-08 10:41:21 +00007203 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007204 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007205 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007206 SrcExprs.push_back(PseudoSrcExpr);
7207 DstExprs.push_back(PseudoDstExpr);
7208 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007209 }
7210
7211 if (Vars.empty())
7212 return nullptr;
7213
7214 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007215 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007216}
7217
Alexey Bataev758e55e2013-09-06 18:03:48 +00007218OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7219 SourceLocation StartLoc,
7220 SourceLocation LParenLoc,
7221 SourceLocation EndLoc) {
7222 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007223 for (auto &RefExpr : VarList) {
7224 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7225 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007226 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007227 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007228 continue;
7229 }
7230
Alexey Bataeved09d242014-05-28 05:53:51 +00007231 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007232 // OpenMP [2.1, C/C++]
7233 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007234 // OpenMP [2.14.3.2, Restrictions, p.1]
7235 // A variable that is part of another variable (as an array or structure
7236 // element) cannot appear in a shared unless it is a static data member
7237 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007238 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007239 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007240 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7241 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007242 continue;
7243 }
7244 Decl *D = DE->getDecl();
7245 VarDecl *VD = cast<VarDecl>(D);
7246
7247 QualType Type = VD->getType();
7248 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7249 // It will be analyzed later.
7250 Vars.push_back(DE);
7251 continue;
7252 }
7253
7254 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7255 // in a Construct]
7256 // Variables with the predetermined data-sharing attributes may not be
7257 // listed in data-sharing attributes clauses, except for the cases
7258 // listed below. For these exceptions only, listing a predetermined
7259 // variable in a data-sharing attribute clause is allowed and overrides
7260 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007261 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007262 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7263 DVar.RefExpr) {
7264 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7265 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007266 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007267 continue;
7268 }
7269
7270 DSAStack->addDSA(VD, DE, OMPC_shared);
7271 Vars.push_back(DE);
7272 }
7273
Alexey Bataeved09d242014-05-28 05:53:51 +00007274 if (Vars.empty())
7275 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007276
7277 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7278}
7279
Alexey Bataevc5e02582014-06-16 07:08:35 +00007280namespace {
7281class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7282 DSAStackTy *Stack;
7283
7284public:
7285 bool VisitDeclRefExpr(DeclRefExpr *E) {
7286 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007287 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007288 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7289 return false;
7290 if (DVar.CKind != OMPC_unknown)
7291 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007292 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007293 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007294 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007295 return true;
7296 return false;
7297 }
7298 return false;
7299 }
7300 bool VisitStmt(Stmt *S) {
7301 for (auto Child : S->children()) {
7302 if (Child && Visit(Child))
7303 return true;
7304 }
7305 return false;
7306 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007307 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007308};
Alexey Bataev23b69422014-06-18 07:08:49 +00007309} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007310
7311OMPClause *Sema::ActOnOpenMPReductionClause(
7312 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7313 SourceLocation ColonLoc, SourceLocation EndLoc,
7314 CXXScopeSpec &ReductionIdScopeSpec,
7315 const DeclarationNameInfo &ReductionId) {
7316 // TODO: Allow scope specification search when 'declare reduction' is
7317 // supported.
7318 assert(ReductionIdScopeSpec.isEmpty() &&
7319 "No support for scoped reduction identifiers yet.");
7320
7321 auto DN = ReductionId.getName();
7322 auto OOK = DN.getCXXOverloadedOperator();
7323 BinaryOperatorKind BOK = BO_Comma;
7324
7325 // OpenMP [2.14.3.6, reduction clause]
7326 // C
7327 // reduction-identifier is either an identifier or one of the following
7328 // operators: +, -, *, &, |, ^, && and ||
7329 // C++
7330 // reduction-identifier is either an id-expression or one of the following
7331 // operators: +, -, *, &, |, ^, && and ||
7332 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7333 switch (OOK) {
7334 case OO_Plus:
7335 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007336 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007337 break;
7338 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007339 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007340 break;
7341 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007342 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007343 break;
7344 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007345 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007346 break;
7347 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007348 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007349 break;
7350 case OO_AmpAmp:
7351 BOK = BO_LAnd;
7352 break;
7353 case OO_PipePipe:
7354 BOK = BO_LOr;
7355 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007356 case OO_New:
7357 case OO_Delete:
7358 case OO_Array_New:
7359 case OO_Array_Delete:
7360 case OO_Slash:
7361 case OO_Percent:
7362 case OO_Tilde:
7363 case OO_Exclaim:
7364 case OO_Equal:
7365 case OO_Less:
7366 case OO_Greater:
7367 case OO_LessEqual:
7368 case OO_GreaterEqual:
7369 case OO_PlusEqual:
7370 case OO_MinusEqual:
7371 case OO_StarEqual:
7372 case OO_SlashEqual:
7373 case OO_PercentEqual:
7374 case OO_CaretEqual:
7375 case OO_AmpEqual:
7376 case OO_PipeEqual:
7377 case OO_LessLess:
7378 case OO_GreaterGreater:
7379 case OO_LessLessEqual:
7380 case OO_GreaterGreaterEqual:
7381 case OO_EqualEqual:
7382 case OO_ExclaimEqual:
7383 case OO_PlusPlus:
7384 case OO_MinusMinus:
7385 case OO_Comma:
7386 case OO_ArrowStar:
7387 case OO_Arrow:
7388 case OO_Call:
7389 case OO_Subscript:
7390 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007391 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007392 case NUM_OVERLOADED_OPERATORS:
7393 llvm_unreachable("Unexpected reduction identifier");
7394 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007395 if (auto II = DN.getAsIdentifierInfo()) {
7396 if (II->isStr("max"))
7397 BOK = BO_GT;
7398 else if (II->isStr("min"))
7399 BOK = BO_LT;
7400 }
7401 break;
7402 }
7403 SourceRange ReductionIdRange;
7404 if (ReductionIdScopeSpec.isValid()) {
7405 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7406 }
7407 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7408 if (BOK == BO_Comma) {
7409 // Not allowed reduction identifier is found.
7410 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7411 << ReductionIdRange;
7412 return nullptr;
7413 }
7414
7415 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007416 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007417 SmallVector<Expr *, 8> LHSs;
7418 SmallVector<Expr *, 8> RHSs;
7419 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007420 for (auto RefExpr : VarList) {
7421 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7422 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7423 // It will be analyzed later.
7424 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007425 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007426 LHSs.push_back(nullptr);
7427 RHSs.push_back(nullptr);
7428 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007429 continue;
7430 }
7431
7432 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7433 RefExpr->isInstantiationDependent() ||
7434 RefExpr->containsUnexpandedParameterPack()) {
7435 // It will be analyzed later.
7436 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007437 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007438 LHSs.push_back(nullptr);
7439 RHSs.push_back(nullptr);
7440 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007441 continue;
7442 }
7443
7444 auto ELoc = RefExpr->getExprLoc();
7445 auto ERange = RefExpr->getSourceRange();
7446 // OpenMP [2.1, C/C++]
7447 // A list item is a variable or array section, subject to the restrictions
7448 // specified in Section 2.4 on page 42 and in each of the sections
7449 // describing clauses and directives for which a list appears.
7450 // OpenMP [2.14.3.3, Restrictions, p.1]
7451 // A variable that is part of another variable (as an array or
7452 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007453 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7454 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7455 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7456 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007457 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7458 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007459 continue;
7460 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007461 QualType Type;
7462 VarDecl *VD = nullptr;
7463 if (DE) {
7464 auto D = DE->getDecl();
7465 VD = cast<VarDecl>(D);
7466 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007467 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007468 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007469 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7470 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7471 Base = TempASE->getBase()->IgnoreParenImpCasts();
7472 DE = dyn_cast<DeclRefExpr>(Base);
7473 if (DE)
7474 VD = dyn_cast<VarDecl>(DE->getDecl());
7475 if (!VD) {
7476 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7477 << 0 << Base->getSourceRange();
7478 continue;
7479 }
7480 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007481 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7482 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7483 Type = ATy->getElementType();
7484 else
7485 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007486 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7487 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7488 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7489 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7490 Base = TempASE->getBase()->IgnoreParenImpCasts();
7491 DE = dyn_cast<DeclRefExpr>(Base);
7492 if (DE)
7493 VD = dyn_cast<VarDecl>(DE->getDecl());
7494 if (!VD) {
7495 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7496 << 1 << Base->getSourceRange();
7497 continue;
7498 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007499 }
7500
Alexey Bataevc5e02582014-06-16 07:08:35 +00007501 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7502 // A variable that appears in a private clause must not have an incomplete
7503 // type or a reference type.
7504 if (RequireCompleteType(ELoc, Type,
7505 diag::err_omp_reduction_incomplete_type))
7506 continue;
7507 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7508 // Arrays may not appear in a reduction clause.
7509 if (Type.getNonReferenceType()->isArrayType()) {
7510 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007511 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007512 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7513 VarDecl::DeclarationOnly;
7514 Diag(VD->getLocation(),
7515 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7516 << VD;
7517 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007518 continue;
7519 }
7520 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7521 // A list item that appears in a reduction clause must not be
7522 // const-qualified.
7523 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007524 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007525 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007526 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007527 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7528 VarDecl::DeclarationOnly;
7529 Diag(VD->getLocation(),
7530 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7531 << VD;
7532 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007533 continue;
7534 }
7535 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7536 // If a list-item is a reference type then it must bind to the same object
7537 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007538 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007539 VarDecl *VDDef = VD->getDefinition();
7540 if (Type->isReferenceType() && VDDef) {
7541 DSARefChecker Check(DSAStack);
7542 if (Check.Visit(VDDef->getInit())) {
7543 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7544 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7545 continue;
7546 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007547 }
7548 }
7549 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7550 // The type of a list item that appears in a reduction clause must be valid
7551 // for the reduction-identifier. For a max or min reduction in C, the type
7552 // of the list item must be an allowed arithmetic data type: char, int,
7553 // float, double, or _Bool, possibly modified with long, short, signed, or
7554 // unsigned. For a max or min reduction in C++, the type of the list item
7555 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7556 // double, or bool, possibly modified with long, short, signed, or unsigned.
7557 if ((BOK == BO_GT || BOK == BO_LT) &&
7558 !(Type->isScalarType() ||
7559 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7560 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7561 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007562 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007563 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7564 VarDecl::DeclarationOnly;
7565 Diag(VD->getLocation(),
7566 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7567 << VD;
7568 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007569 continue;
7570 }
7571 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7572 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7573 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007574 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007575 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7576 VarDecl::DeclarationOnly;
7577 Diag(VD->getLocation(),
7578 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7579 << VD;
7580 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007581 continue;
7582 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007583 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7584 // in a Construct]
7585 // Variables with the predetermined data-sharing attributes may not be
7586 // listed in data-sharing attributes clauses, except for the cases
7587 // listed below. For these exceptions only, listing a predetermined
7588 // variable in a data-sharing attribute clause is allowed and overrides
7589 // the variable's predetermined data-sharing attributes.
7590 // OpenMP [2.14.3.6, Restrictions, p.3]
7591 // Any number of reduction clauses can be specified on the directive,
7592 // but a list item can appear only once in the reduction clauses for that
7593 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007594 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007595 DVar = DSAStack->getTopDSA(VD, false);
7596 if (DVar.CKind == OMPC_reduction) {
7597 Diag(ELoc, diag::err_omp_once_referenced)
7598 << getOpenMPClauseName(OMPC_reduction);
7599 if (DVar.RefExpr) {
7600 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007601 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007602 } else if (DVar.CKind != OMPC_unknown) {
7603 Diag(ELoc, diag::err_omp_wrong_dsa)
7604 << getOpenMPClauseName(DVar.CKind)
7605 << getOpenMPClauseName(OMPC_reduction);
7606 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7607 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007608 }
7609
7610 // OpenMP [2.14.3.6, Restrictions, p.1]
7611 // A list item that appears in a reduction clause of a worksharing
7612 // construct must be shared in the parallel regions to which any of the
7613 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007614 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7615 if (isOpenMPWorksharingDirective(CurrDir) &&
7616 !isOpenMPParallelDirective(CurrDir)) {
7617 DVar = DSAStack->getImplicitDSA(VD, true);
7618 if (DVar.CKind != OMPC_shared) {
7619 Diag(ELoc, diag::err_omp_required_access)
7620 << getOpenMPClauseName(OMPC_reduction)
7621 << getOpenMPClauseName(OMPC_shared);
7622 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7623 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007624 }
7625 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007626
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007627 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007628 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7629 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7630 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7631 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7632 auto PrivateTy = Type;
7633 if (OASE) {
7634 // For array sections only:
7635 // Create pseudo array type for private copy. The size for this array will
7636 // be generated during codegen.
7637 // For array subscripts or single variables Private Ty is the same as Type
7638 // (type of the variable or single array element).
7639 PrivateTy = Context.getVariableArrayType(
7640 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7641 Context.getSizeType(), VK_RValue),
7642 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7643 }
7644 // Private copy.
7645 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7646 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007647 // Add initializer for private variable.
7648 Expr *Init = nullptr;
7649 switch (BOK) {
7650 case BO_Add:
7651 case BO_Xor:
7652 case BO_Or:
7653 case BO_LOr:
7654 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7655 if (Type->isScalarType() || Type->isAnyComplexType()) {
7656 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007657 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007658 break;
7659 case BO_Mul:
7660 case BO_LAnd:
7661 if (Type->isScalarType() || Type->isAnyComplexType()) {
7662 // '*' and '&&' reduction ops - initializer is '1'.
7663 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7664 }
7665 break;
7666 case BO_And: {
7667 // '&' reduction op - initializer is '~0'.
7668 QualType OrigType = Type;
7669 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7670 Type = ComplexTy->getElementType();
7671 }
7672 if (Type->isRealFloatingType()) {
7673 llvm::APFloat InitValue =
7674 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7675 /*isIEEE=*/true);
7676 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7677 Type, ELoc);
7678 } else if (Type->isScalarType()) {
7679 auto Size = Context.getTypeSize(Type);
7680 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7681 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7682 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7683 }
7684 if (Init && OrigType->isAnyComplexType()) {
7685 // Init = 0xFFFF + 0xFFFFi;
7686 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7687 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7688 }
7689 Type = OrigType;
7690 break;
7691 }
7692 case BO_LT:
7693 case BO_GT: {
7694 // 'min' reduction op - initializer is 'Largest representable number in
7695 // the reduction list item type'.
7696 // 'max' reduction op - initializer is 'Least representable number in
7697 // the reduction list item type'.
7698 if (Type->isIntegerType() || Type->isPointerType()) {
7699 bool IsSigned = Type->hasSignedIntegerRepresentation();
7700 auto Size = Context.getTypeSize(Type);
7701 QualType IntTy =
7702 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7703 llvm::APInt InitValue =
7704 (BOK != BO_LT)
7705 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7706 : llvm::APInt::getMinValue(Size)
7707 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7708 : llvm::APInt::getMaxValue(Size);
7709 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7710 if (Type->isPointerType()) {
7711 // Cast to pointer type.
7712 auto CastExpr = BuildCStyleCastExpr(
7713 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7714 SourceLocation(), Init);
7715 if (CastExpr.isInvalid())
7716 continue;
7717 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007718 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007719 } else if (Type->isRealFloatingType()) {
7720 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7721 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7722 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7723 Type, ELoc);
7724 }
7725 break;
7726 }
7727 case BO_PtrMemD:
7728 case BO_PtrMemI:
7729 case BO_MulAssign:
7730 case BO_Div:
7731 case BO_Rem:
7732 case BO_Sub:
7733 case BO_Shl:
7734 case BO_Shr:
7735 case BO_LE:
7736 case BO_GE:
7737 case BO_EQ:
7738 case BO_NE:
7739 case BO_AndAssign:
7740 case BO_XorAssign:
7741 case BO_OrAssign:
7742 case BO_Assign:
7743 case BO_AddAssign:
7744 case BO_SubAssign:
7745 case BO_DivAssign:
7746 case BO_RemAssign:
7747 case BO_ShlAssign:
7748 case BO_ShrAssign:
7749 case BO_Comma:
7750 llvm_unreachable("Unexpected reduction operation");
7751 }
7752 if (Init) {
7753 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7754 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007755 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007756 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007757 if (!RHSVD->hasInit()) {
7758 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7759 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007760 if (VD) {
7761 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7762 VarDecl::DeclarationOnly;
7763 Diag(VD->getLocation(),
7764 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7765 << VD;
7766 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007767 continue;
7768 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007769 // Store initializer for single element in private copy. Will be used during
7770 // codegen.
7771 PrivateVD->setInit(RHSVD->getInit());
7772 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007773 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7774 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007775 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007776 ExprResult ReductionOp =
7777 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7778 LHSDRE, RHSDRE);
7779 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007780 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007781 ReductionOp =
7782 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7783 BO_Assign, LHSDRE, ReductionOp.get());
7784 } else {
7785 auto *ConditionalOp = new (Context) ConditionalOperator(
7786 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7787 RHSDRE, Type, VK_LValue, OK_Ordinary);
7788 ReductionOp =
7789 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7790 BO_Assign, LHSDRE, ConditionalOp);
7791 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007792 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007793 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007794 if (ReductionOp.isInvalid())
7795 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007796
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007797 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007798 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007799 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007800 LHSs.push_back(LHSDRE);
7801 RHSs.push_back(RHSDRE);
7802 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007803 }
7804
7805 if (Vars.empty())
7806 return nullptr;
7807
7808 return OMPReductionClause::Create(
7809 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007810 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7811 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007812}
7813
Alexey Bataev182227b2015-08-20 10:54:39 +00007814OMPClause *Sema::ActOnOpenMPLinearClause(
7815 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7816 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7817 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007818 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007819 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007820 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007821 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7822 LinKind == OMPC_LINEAR_unknown) {
7823 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7824 LinKind = OMPC_LINEAR_val;
7825 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007826 for (auto &RefExpr : VarList) {
7827 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7828 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007829 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007830 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007831 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007832 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007833 continue;
7834 }
7835
7836 // OpenMP [2.14.3.7, linear clause]
7837 // A list item that appears in a linear clause is subject to the private
7838 // clause semantics described in Section 2.14.3.3 on page 159 except as
7839 // noted. In addition, the value of the new list item on each iteration
7840 // of the associated loop(s) corresponds to the value of the original
7841 // list item before entering the construct plus the logical number of
7842 // the iteration times linear-step.
7843
Alexey Bataeved09d242014-05-28 05:53:51 +00007844 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007845 // OpenMP [2.1, C/C++]
7846 // A list item is a variable name.
7847 // OpenMP [2.14.3.3, Restrictions, p.1]
7848 // A variable that is part of another variable (as an array or
7849 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007850 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007851 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007852 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7853 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007854 continue;
7855 }
7856
7857 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7858
7859 // OpenMP [2.14.3.7, linear clause]
7860 // A list-item cannot appear in more than one linear clause.
7861 // A list-item that appears in a linear clause cannot appear in any
7862 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007863 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007864 if (DVar.RefExpr) {
7865 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7866 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007867 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007868 continue;
7869 }
7870
7871 QualType QType = VD->getType();
7872 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7873 // It will be analyzed later.
7874 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007875 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007876 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007877 continue;
7878 }
7879
7880 // A variable must not have an incomplete type or a reference type.
7881 if (RequireCompleteType(ELoc, QType,
7882 diag::err_omp_linear_incomplete_type)) {
7883 continue;
7884 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007885 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7886 !QType->isReferenceType()) {
7887 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7888 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7889 continue;
7890 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007891 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007892
7893 // A list item must not be const-qualified.
7894 if (QType.isConstant(Context)) {
7895 Diag(ELoc, diag::err_omp_const_variable)
7896 << getOpenMPClauseName(OMPC_linear);
7897 bool IsDecl =
7898 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7899 Diag(VD->getLocation(),
7900 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7901 << VD;
7902 continue;
7903 }
7904
7905 // A list item must be of integral or pointer type.
7906 QType = QType.getUnqualifiedType().getCanonicalType();
7907 const Type *Ty = QType.getTypePtrOrNull();
7908 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7909 !Ty->isPointerType())) {
7910 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7911 bool IsDecl =
7912 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7913 Diag(VD->getLocation(),
7914 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7915 << VD;
7916 continue;
7917 }
7918
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007919 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007920 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7921 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007922 auto *PrivateRef = buildDeclRefExpr(
7923 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007924 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007925 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007926 Expr *InitExpr;
7927 if (LinKind == OMPC_LINEAR_uval)
7928 InitExpr = VD->getInit();
7929 else
7930 InitExpr = DE;
7931 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007932 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007933 auto InitRef = buildDeclRefExpr(
7934 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007935 DSAStack->addDSA(VD, DE, OMPC_linear);
7936 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007937 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007938 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007939 }
7940
7941 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007942 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007943
7944 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007945 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007946 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7947 !Step->isInstantiationDependent() &&
7948 !Step->containsUnexpandedParameterPack()) {
7949 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007950 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007951 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007952 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007953 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007954
Alexander Musman3276a272015-03-21 10:12:56 +00007955 // Build var to save the step value.
7956 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007957 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007958 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007959 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007960 ExprResult CalcStep =
7961 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007962 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007963
Alexander Musman8dba6642014-04-22 13:09:42 +00007964 // Warn about zero linear step (it would be probably better specified as
7965 // making corresponding variables 'const').
7966 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007967 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7968 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007969 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7970 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007971 if (!IsConstant && CalcStep.isUsable()) {
7972 // Calculate the step beforehand instead of doing this on each iteration.
7973 // (This is not used if the number of iterations may be kfold-ed).
7974 CalcStepExpr = CalcStep.get();
7975 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007976 }
7977
Alexey Bataev182227b2015-08-20 10:54:39 +00007978 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7979 ColonLoc, EndLoc, Vars, Privates, Inits,
7980 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007981}
7982
7983static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7984 Expr *NumIterations, Sema &SemaRef,
7985 Scope *S) {
7986 // Walk the vars and build update/final expressions for the CodeGen.
7987 SmallVector<Expr *, 8> Updates;
7988 SmallVector<Expr *, 8> Finals;
7989 Expr *Step = Clause.getStep();
7990 Expr *CalcStep = Clause.getCalcStep();
7991 // OpenMP [2.14.3.7, linear clause]
7992 // If linear-step is not specified it is assumed to be 1.
7993 if (Step == nullptr)
7994 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7995 else if (CalcStep)
7996 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7997 bool HasErrors = false;
7998 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007999 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008000 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008001 for (auto &RefExpr : Clause.varlists()) {
8002 Expr *InitExpr = *CurInit;
8003
8004 // Build privatized reference to the current linear var.
8005 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008006 Expr *CapturedRef;
8007 if (LinKind == OMPC_LINEAR_uval)
8008 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8009 else
8010 CapturedRef =
8011 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8012 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8013 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008014
8015 // Build update: Var = InitExpr + IV * Step
8016 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008017 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008018 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008019 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8020 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008021
8022 // Build final: Var = InitExpr + NumIterations * Step
8023 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008024 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008025 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008026 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8027 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008028 if (!Update.isUsable() || !Final.isUsable()) {
8029 Updates.push_back(nullptr);
8030 Finals.push_back(nullptr);
8031 HasErrors = true;
8032 } else {
8033 Updates.push_back(Update.get());
8034 Finals.push_back(Final.get());
8035 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008036 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008037 }
8038 Clause.setUpdates(Updates);
8039 Clause.setFinals(Finals);
8040 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008041}
8042
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008043OMPClause *Sema::ActOnOpenMPAlignedClause(
8044 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8045 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8046
8047 SmallVector<Expr *, 8> Vars;
8048 for (auto &RefExpr : VarList) {
8049 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8050 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8051 // It will be analyzed later.
8052 Vars.push_back(RefExpr);
8053 continue;
8054 }
8055
8056 SourceLocation ELoc = RefExpr->getExprLoc();
8057 // OpenMP [2.1, C/C++]
8058 // A list item is a variable name.
8059 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8060 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008061 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8062 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008063 continue;
8064 }
8065
8066 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8067
8068 // OpenMP [2.8.1, simd construct, Restrictions]
8069 // The type of list items appearing in the aligned clause must be
8070 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008071 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008072 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008073 const Type *Ty = QType.getTypePtrOrNull();
8074 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8075 !Ty->isPointerType())) {
8076 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8077 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8078 bool IsDecl =
8079 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8080 Diag(VD->getLocation(),
8081 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8082 << VD;
8083 continue;
8084 }
8085
8086 // OpenMP [2.8.1, simd construct, Restrictions]
8087 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008088 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008089 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8090 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8091 << getOpenMPClauseName(OMPC_aligned);
8092 continue;
8093 }
8094
8095 Vars.push_back(DE);
8096 }
8097
8098 // OpenMP [2.8.1, simd construct, Description]
8099 // The parameter of the aligned clause, alignment, must be a constant
8100 // positive integer expression.
8101 // If no optional parameter is specified, implementation-defined default
8102 // alignments for SIMD instructions on the target platforms are assumed.
8103 if (Alignment != nullptr) {
8104 ExprResult AlignResult =
8105 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8106 if (AlignResult.isInvalid())
8107 return nullptr;
8108 Alignment = AlignResult.get();
8109 }
8110 if (Vars.empty())
8111 return nullptr;
8112
8113 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8114 EndLoc, Vars, Alignment);
8115}
8116
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008117OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8118 SourceLocation StartLoc,
8119 SourceLocation LParenLoc,
8120 SourceLocation EndLoc) {
8121 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008122 SmallVector<Expr *, 8> SrcExprs;
8123 SmallVector<Expr *, 8> DstExprs;
8124 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008125 for (auto &RefExpr : VarList) {
8126 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8127 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008128 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008129 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008130 SrcExprs.push_back(nullptr);
8131 DstExprs.push_back(nullptr);
8132 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008133 continue;
8134 }
8135
Alexey Bataeved09d242014-05-28 05:53:51 +00008136 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008137 // OpenMP [2.1, C/C++]
8138 // A list item is a variable name.
8139 // OpenMP [2.14.4.1, Restrictions, p.1]
8140 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008141 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008142 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008143 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8144 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008145 continue;
8146 }
8147
8148 Decl *D = DE->getDecl();
8149 VarDecl *VD = cast<VarDecl>(D);
8150
8151 QualType Type = VD->getType();
8152 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8153 // It will be analyzed later.
8154 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008155 SrcExprs.push_back(nullptr);
8156 DstExprs.push_back(nullptr);
8157 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008158 continue;
8159 }
8160
8161 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8162 // A list item that appears in a copyin clause must be threadprivate.
8163 if (!DSAStack->isThreadPrivate(VD)) {
8164 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008165 << getOpenMPClauseName(OMPC_copyin)
8166 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008167 continue;
8168 }
8169
8170 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8171 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008172 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008173 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008174 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008175 auto *SrcVD =
8176 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8177 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008178 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008179 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8180 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008181 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8182 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008183 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008184 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008185 // For arrays generate assignment operation for single element and replace
8186 // it by the original array element in CodeGen.
8187 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8188 PseudoDstExpr, PseudoSrcExpr);
8189 if (AssignmentOp.isInvalid())
8190 continue;
8191 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8192 /*DiscardedValue=*/true);
8193 if (AssignmentOp.isInvalid())
8194 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008195
8196 DSAStack->addDSA(VD, DE, OMPC_copyin);
8197 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008198 SrcExprs.push_back(PseudoSrcExpr);
8199 DstExprs.push_back(PseudoDstExpr);
8200 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008201 }
8202
Alexey Bataeved09d242014-05-28 05:53:51 +00008203 if (Vars.empty())
8204 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008205
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008206 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8207 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008208}
8209
Alexey Bataevbae9a792014-06-27 10:37:06 +00008210OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8211 SourceLocation StartLoc,
8212 SourceLocation LParenLoc,
8213 SourceLocation EndLoc) {
8214 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008215 SmallVector<Expr *, 8> SrcExprs;
8216 SmallVector<Expr *, 8> DstExprs;
8217 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008218 for (auto &RefExpr : VarList) {
8219 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8220 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8221 // It will be analyzed later.
8222 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008223 SrcExprs.push_back(nullptr);
8224 DstExprs.push_back(nullptr);
8225 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008226 continue;
8227 }
8228
8229 SourceLocation ELoc = RefExpr->getExprLoc();
8230 // OpenMP [2.1, C/C++]
8231 // A list item is a variable name.
8232 // OpenMP [2.14.4.1, Restrictions, p.1]
8233 // A list item that appears in a copyin clause must be threadprivate.
8234 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8235 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008236 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8237 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008238 continue;
8239 }
8240
8241 Decl *D = DE->getDecl();
8242 VarDecl *VD = cast<VarDecl>(D);
8243
8244 QualType Type = VD->getType();
8245 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8246 // It will be analyzed later.
8247 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008248 SrcExprs.push_back(nullptr);
8249 DstExprs.push_back(nullptr);
8250 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008251 continue;
8252 }
8253
8254 // OpenMP [2.14.4.2, Restrictions, p.2]
8255 // A list item that appears in a copyprivate clause may not appear in a
8256 // private or firstprivate clause on the single construct.
8257 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008258 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008259 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8260 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008261 Diag(ELoc, diag::err_omp_wrong_dsa)
8262 << getOpenMPClauseName(DVar.CKind)
8263 << getOpenMPClauseName(OMPC_copyprivate);
8264 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8265 continue;
8266 }
8267
8268 // OpenMP [2.11.4.2, Restrictions, p.1]
8269 // All list items that appear in a copyprivate clause must be either
8270 // threadprivate or private in the enclosing context.
8271 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008272 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008273 if (DVar.CKind == OMPC_shared) {
8274 Diag(ELoc, diag::err_omp_required_access)
8275 << getOpenMPClauseName(OMPC_copyprivate)
8276 << "threadprivate or private in the enclosing context";
8277 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8278 continue;
8279 }
8280 }
8281 }
8282
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008283 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008284 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008285 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008286 << getOpenMPClauseName(OMPC_copyprivate) << Type
8287 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008288 bool IsDecl =
8289 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8290 Diag(VD->getLocation(),
8291 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8292 << VD;
8293 continue;
8294 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008295
Alexey Bataevbae9a792014-06-27 10:37:06 +00008296 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8297 // A variable of class type (or array thereof) that appears in a
8298 // copyin clause requires an accessible, unambiguous copy assignment
8299 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008300 Type = Context.getBaseElementType(Type.getNonReferenceType())
8301 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008302 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008303 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8304 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008305 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008306 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008307 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008308 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8309 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008310 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008311 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008312 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8313 PseudoDstExpr, PseudoSrcExpr);
8314 if (AssignmentOp.isInvalid())
8315 continue;
8316 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8317 /*DiscardedValue=*/true);
8318 if (AssignmentOp.isInvalid())
8319 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008320
8321 // No need to mark vars as copyprivate, they are already threadprivate or
8322 // implicitly private.
8323 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008324 SrcExprs.push_back(PseudoSrcExpr);
8325 DstExprs.push_back(PseudoDstExpr);
8326 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008327 }
8328
8329 if (Vars.empty())
8330 return nullptr;
8331
Alexey Bataeva63048e2015-03-23 06:18:07 +00008332 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8333 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008334}
8335
Alexey Bataev6125da92014-07-21 11:26:11 +00008336OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8337 SourceLocation StartLoc,
8338 SourceLocation LParenLoc,
8339 SourceLocation EndLoc) {
8340 if (VarList.empty())
8341 return nullptr;
8342
8343 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8344}
Alexey Bataevdea47612014-07-23 07:46:59 +00008345
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008346OMPClause *
8347Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8348 SourceLocation DepLoc, SourceLocation ColonLoc,
8349 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8350 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008351 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008352 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008353 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008354 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008355 return nullptr;
8356 }
8357 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008358 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8359 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008360 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008361 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008362 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8363 /*Last=*/OMPC_DEPEND_unknown, Except)
8364 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008365 return nullptr;
8366 }
8367 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008368 llvm::APSInt DepCounter(/*BitWidth=*/32);
8369 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8370 if (DepKind == OMPC_DEPEND_sink) {
8371 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8372 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8373 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008374 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008375 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008376 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8377 DSAStack->getParentOrderedRegionParam()) {
8378 for (auto &RefExpr : VarList) {
8379 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8380 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8381 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8382 // It will be analyzed later.
8383 Vars.push_back(RefExpr);
8384 continue;
8385 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008386
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008387 SourceLocation ELoc = RefExpr->getExprLoc();
8388 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8389 if (DepKind == OMPC_DEPEND_sink) {
8390 if (DepCounter >= TotalDepCount) {
8391 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8392 continue;
8393 }
8394 ++DepCounter;
8395 // OpenMP [2.13.9, Summary]
8396 // depend(dependence-type : vec), where dependence-type is:
8397 // 'sink' and where vec is the iteration vector, which has the form:
8398 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8399 // where n is the value specified by the ordered clause in the loop
8400 // directive, xi denotes the loop iteration variable of the i-th nested
8401 // loop associated with the loop directive, and di is a constant
8402 // non-negative integer.
8403 SimpleExpr = SimpleExpr->IgnoreImplicit();
8404 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8405 if (!DE) {
8406 OverloadedOperatorKind OOK = OO_None;
8407 SourceLocation OOLoc;
8408 Expr *LHS, *RHS;
8409 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8410 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8411 OOLoc = BO->getOperatorLoc();
8412 LHS = BO->getLHS()->IgnoreParenImpCasts();
8413 RHS = BO->getRHS()->IgnoreParenImpCasts();
8414 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8415 OOK = OCE->getOperator();
8416 OOLoc = OCE->getOperatorLoc();
8417 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8418 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8419 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8420 OOK = MCE->getMethodDecl()
8421 ->getNameInfo()
8422 .getName()
8423 .getCXXOverloadedOperator();
8424 OOLoc = MCE->getCallee()->getExprLoc();
8425 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8426 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8427 } else {
8428 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8429 continue;
8430 }
8431 DE = dyn_cast<DeclRefExpr>(LHS);
8432 if (!DE) {
8433 Diag(LHS->getExprLoc(),
8434 diag::err_omp_depend_sink_expected_loop_iteration)
8435 << DSAStack->getParentLoopControlVariable(
8436 DepCounter.getZExtValue());
8437 continue;
8438 }
8439 if (OOK != OO_Plus && OOK != OO_Minus) {
8440 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8441 continue;
8442 }
8443 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8444 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8445 if (Res.isInvalid())
8446 continue;
8447 }
8448 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8449 if (!CurContext->isDependentContext() &&
8450 DSAStack->getParentOrderedRegionParam() &&
8451 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8452 Diag(DE->getExprLoc(),
8453 diag::err_omp_depend_sink_expected_loop_iteration)
8454 << DSAStack->getParentLoopControlVariable(
8455 DepCounter.getZExtValue());
8456 continue;
8457 }
8458 } else {
8459 // OpenMP [2.11.1.1, Restrictions, p.3]
8460 // A variable that is part of another variable (such as a field of a
8461 // structure) but is not an array element or an array section cannot
8462 // appear in a depend clause.
8463 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8464 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8465 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8466 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8467 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
8468 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8469 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008470 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8471 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008472 continue;
8473 }
8474 }
8475
8476 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8477 }
8478
8479 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8480 TotalDepCount > VarList.size() &&
8481 DSAStack->getParentOrderedRegionParam()) {
8482 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8483 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8484 }
8485 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8486 Vars.empty())
8487 return nullptr;
8488 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008489
8490 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8491 DepLoc, ColonLoc, Vars);
8492}
Michael Wonge710d542015-08-07 16:16:36 +00008493
8494OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8495 SourceLocation LParenLoc,
8496 SourceLocation EndLoc) {
8497 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008498
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008499 // OpenMP [2.9.1, Restrictions]
8500 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008501 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8502 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008503 return nullptr;
8504
Michael Wonge710d542015-08-07 16:16:36 +00008505 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8506}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008507
8508static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8509 DSAStackTy *Stack, CXXRecordDecl *RD) {
8510 if (!RD || RD->isInvalidDecl())
8511 return true;
8512
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008513 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8514 if (auto *CTD = CTSD->getSpecializedTemplate())
8515 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008516 auto QTy = SemaRef.Context.getRecordType(RD);
8517 if (RD->isDynamicClass()) {
8518 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8519 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8520 return false;
8521 }
8522 auto *DC = RD;
8523 bool IsCorrect = true;
8524 for (auto *I : DC->decls()) {
8525 if (I) {
8526 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8527 if (MD->isStatic()) {
8528 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8529 SemaRef.Diag(MD->getLocation(),
8530 diag::note_omp_static_member_in_target);
8531 IsCorrect = false;
8532 }
8533 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8534 if (VD->isStaticDataMember()) {
8535 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8536 SemaRef.Diag(VD->getLocation(),
8537 diag::note_omp_static_member_in_target);
8538 IsCorrect = false;
8539 }
8540 }
8541 }
8542 }
8543
8544 for (auto &I : RD->bases()) {
8545 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8546 I.getType()->getAsCXXRecordDecl()))
8547 IsCorrect = false;
8548 }
8549 return IsCorrect;
8550}
8551
8552static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8553 DSAStackTy *Stack, QualType QTy) {
8554 NamedDecl *ND;
8555 if (QTy->isIncompleteType(&ND)) {
8556 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8557 return false;
8558 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8559 if (!RD->isInvalidDecl() &&
8560 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8561 return false;
8562 }
8563 return true;
8564}
8565
Samuel Antao23abd722016-01-19 20:40:49 +00008566OMPClause *
8567Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
8568 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8569 SourceLocation MapLoc, SourceLocation ColonLoc,
8570 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8571 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00008572 SmallVector<Expr *, 4> Vars;
8573
8574 for (auto &RE : VarList) {
8575 assert(RE && "Null expr in omp map");
8576 if (isa<DependentScopeDeclRefExpr>(RE)) {
8577 // It will be analyzed later.
8578 Vars.push_back(RE);
8579 continue;
8580 }
8581 SourceLocation ELoc = RE->getExprLoc();
8582
8583 // OpenMP [2.14.5, Restrictions]
8584 // A variable that is part of another variable (such as field of a
8585 // structure) but is not an array element or an array section cannot appear
8586 // in a map clause.
8587 auto *VE = RE->IgnoreParenLValueCasts();
8588
8589 if (VE->isValueDependent() || VE->isTypeDependent() ||
8590 VE->isInstantiationDependent() ||
8591 VE->containsUnexpandedParameterPack()) {
8592 // It will be analyzed later.
8593 Vars.push_back(RE);
8594 continue;
8595 }
8596
8597 auto *SimpleExpr = RE->IgnoreParenCasts();
8598 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8599 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8600 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8601
8602 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8603 (!OASE && !ASE && !DE) ||
8604 (DE && !isa<VarDecl>(DE->getDecl())) ||
8605 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8606 !ASE->getBase()->getType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008607 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8608 << 0 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008609 continue;
8610 }
8611
8612 Decl *D = nullptr;
8613 if (DE) {
8614 D = DE->getDecl();
8615 } else if (ASE) {
8616 auto *B = ASE->getBase()->IgnoreParenCasts();
8617 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8618 } else if (OASE) {
8619 auto *B = OASE->getBase();
8620 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8621 }
8622 assert(D && "Null decl on map clause.");
8623 auto *VD = cast<VarDecl>(D);
8624
8625 // OpenMP [2.14.5, Restrictions, p.8]
8626 // threadprivate variables cannot appear in a map clause.
8627 if (DSAStack->isThreadPrivate(VD)) {
8628 auto DVar = DSAStack->getTopDSA(VD, false);
8629 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8630 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8631 continue;
8632 }
8633
8634 // OpenMP [2.14.5, Restrictions, p.2]
8635 // At most one list item can be an array item derived from a given variable
8636 // in map clauses of the same construct.
8637 // OpenMP [2.14.5, Restrictions, p.3]
8638 // List items of map clauses in the same construct must not share original
8639 // storage.
8640 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8641 // A variable for which the type is pointer, reference to array, or
8642 // reference to pointer and an array section derived from that variable
8643 // must not appear as list items of map clauses of the same construct.
8644 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8645 if (MI.RefExpr) {
8646 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8647 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8648 << MI.RefExpr->getSourceRange();
8649 continue;
8650 }
8651
8652 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8653 // A variable for which the type is pointer, reference to array, or
8654 // reference to pointer must not appear as a list item if the enclosing
8655 // device data environment already contains an array section derived from
8656 // that variable.
8657 // An array section derived from a variable for which the type is pointer,
8658 // reference to array, or reference to pointer must not appear as a list
8659 // item if the enclosing device data environment already contains that
8660 // variable.
8661 QualType Type = VD->getType();
8662 MI = DSAStack->getMapInfoForVar(VD);
8663 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8664 isa<DeclRefExpr>(VE)) &&
8665 (Type->isPointerType() || Type->isReferenceType())) {
8666 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8667 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8668 << MI.RefExpr->getSourceRange();
8669 continue;
8670 }
8671
8672 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8673 // A list item must have a mappable type.
8674 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8675 DSAStack, Type))
8676 continue;
8677
Samuel Antaodf67fc42016-01-19 19:15:56 +00008678 // target enter data
8679 // OpenMP [2.10.2, Restrictions, p. 99]
8680 // A map-type must be specified in all map clauses and must be either
8681 // to or alloc.
8682 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8683 if (DKind == OMPD_target_enter_data &&
8684 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
8685 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00008686 << (IsMapTypeImplicit ? 1 : 0)
8687 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00008688 << getOpenMPDirectiveName(DKind);
8689 // Proceed to add the variable in a map clause anyway, to prevent
8690 // further spurious messages
8691 }
8692
Samuel Antao72590762016-01-19 20:04:50 +00008693 // target exit_data
8694 // OpenMP [2.10.3, Restrictions, p. 102]
8695 // A map-type must be specified in all map clauses and must be either
8696 // from, release, or delete.
8697 DKind = DSAStack->getCurrentDirective();
8698 if (DKind == OMPD_target_exit_data &&
8699 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
8700 MapType == OMPC_MAP_delete)) {
8701 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00008702 << (IsMapTypeImplicit ? 1 : 0)
8703 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00008704 << getOpenMPDirectiveName(DKind);
8705 // Proceed to add the variable in a map clause anyway, to prevent
8706 // further spurious messages
8707 }
8708
Kelvin Li0bff7af2015-11-23 05:32:03 +00008709 Vars.push_back(RE);
8710 MI.RefExpr = RE;
8711 DSAStack->addMapInfoForVar(VD, MI);
8712 }
8713 if (Vars.empty())
8714 return nullptr;
8715
8716 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00008717 MapTypeModifier, MapType, IsMapTypeImplicit,
8718 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00008719}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008720
8721OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8722 SourceLocation StartLoc,
8723 SourceLocation LParenLoc,
8724 SourceLocation EndLoc) {
8725 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008726
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008727 // OpenMP [teams Constrcut, Restrictions]
8728 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008729 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8730 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008731 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008732
8733 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8734}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008735
8736OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8737 SourceLocation StartLoc,
8738 SourceLocation LParenLoc,
8739 SourceLocation EndLoc) {
8740 Expr *ValExpr = ThreadLimit;
8741
8742 // OpenMP [teams Constrcut, Restrictions]
8743 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008744 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8745 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008746 return nullptr;
8747
8748 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8749 EndLoc);
8750}
Alexey Bataeva0569352015-12-01 10:17:31 +00008751
8752OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8753 SourceLocation StartLoc,
8754 SourceLocation LParenLoc,
8755 SourceLocation EndLoc) {
8756 Expr *ValExpr = Priority;
8757
8758 // OpenMP [2.9.1, task Constrcut]
8759 // The priority-value is a non-negative numerical scalar expression.
8760 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8761 /*StrictlyPositive=*/false))
8762 return nullptr;
8763
8764 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8765}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008766
8767OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8768 SourceLocation StartLoc,
8769 SourceLocation LParenLoc,
8770 SourceLocation EndLoc) {
8771 Expr *ValExpr = Grainsize;
8772
8773 // OpenMP [2.9.2, taskloop Constrcut]
8774 // The parameter of the grainsize clause must be a positive integer
8775 // expression.
8776 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8777 /*StrictlyPositive=*/true))
8778 return nullptr;
8779
8780 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8781}
Alexey Bataev382967a2015-12-08 12:06:20 +00008782
8783OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8784 SourceLocation StartLoc,
8785 SourceLocation LParenLoc,
8786 SourceLocation EndLoc) {
8787 Expr *ValExpr = NumTasks;
8788
8789 // OpenMP [2.9.2, taskloop Constrcut]
8790 // The parameter of the num_tasks clause must be a positive integer
8791 // expression.
8792 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8793 /*StrictlyPositive=*/true))
8794 return nullptr;
8795
8796 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8797}
8798
Alexey Bataev28c75412015-12-15 08:19:24 +00008799OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8800 SourceLocation LParenLoc,
8801 SourceLocation EndLoc) {
8802 // OpenMP [2.13.2, critical construct, Description]
8803 // ... where hint-expression is an integer constant expression that evaluates
8804 // to a valid lock hint.
8805 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8806 if (HintExpr.isInvalid())
8807 return nullptr;
8808 return new (Context)
8809 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8810}
8811
Carlo Bertollib4adf552016-01-15 18:50:31 +00008812OMPClause *Sema::ActOnOpenMPDistScheduleClause(
8813 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8814 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
8815 SourceLocation EndLoc) {
8816 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
8817 std::string Values;
8818 Values += "'";
8819 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
8820 Values += "'";
8821 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8822 << Values << getOpenMPClauseName(OMPC_dist_schedule);
8823 return nullptr;
8824 }
8825 Expr *ValExpr = ChunkSize;
8826 Expr *HelperValExpr = nullptr;
8827 if (ChunkSize) {
8828 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8829 !ChunkSize->isInstantiationDependent() &&
8830 !ChunkSize->containsUnexpandedParameterPack()) {
8831 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8832 ExprResult Val =
8833 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8834 if (Val.isInvalid())
8835 return nullptr;
8836
8837 ValExpr = Val.get();
8838
8839 // OpenMP [2.7.1, Restrictions]
8840 // chunk_size must be a loop invariant integer expression with a positive
8841 // value.
8842 llvm::APSInt Result;
8843 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8844 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8845 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8846 << "dist_schedule" << ChunkSize->getSourceRange();
8847 return nullptr;
8848 }
8849 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
8850 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
8851 ChunkSize->getType(), ".chunk.");
8852 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
8853 ChunkSize->getExprLoc(),
8854 /*RefersToCapture=*/true);
8855 HelperValExpr = ImpVarRef;
8856 }
8857 }
8858 }
8859
8860 return new (Context)
8861 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
8862 Kind, ValExpr, HelperValExpr);
8863}