blob: 5b38f436fb70d755d32c1c63a807f571f6d98a5d [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
Alexey Bataev758e55e2013-09-06 18:03:48 +000080private:
Samuel Antao5de996e2016-01-22 20:21:36 +000081 typedef SmallVector<Expr *, 4> MapInfo;
82
Alexey Bataev758e55e2013-09-06 18:03:48 +000083 struct DSAInfo {
84 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000085 Expr *RefExpr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000087 typedef llvm::SmallDenseMap<ValueDecl *, DSAInfo, 64> DeclSAMapTy;
88 typedef llvm::SmallDenseMap<ValueDecl *, Expr *, 64> AlignedMapTy;
89 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
90 typedef llvm::SmallDenseMap<ValueDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000091 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
92 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093
94 struct SharingMapTy {
95 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000096 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000097 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000098 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000099 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000100 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 OpenMPDirectiveKind Directive;
102 DeclarationNameInfo DirectiveName;
103 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000105 /// \brief first argument (Expr *) contains optional argument of the
106 /// 'ordered' clause, the second one is true if the regions has 'ordered'
107 /// clause, false otherwise.
108 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000109 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000110 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000111 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000112 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000113 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000114 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000115 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000116 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000117 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000118 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000121 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000122 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124 };
125
Axel Naumann323862e2016-02-03 10:45:22 +0000126 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127
128 /// \brief Stack of used declaration and their data-sharing attributes.
129 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000130 /// \brief true, if check for DSA must be from parent directive, false, if
131 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000132 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000133 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000134 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000135 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
138
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000139 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000140
141 /// \brief Checks if the variable is a local for OpenMP region.
142 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000145 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000146 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
147 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000148
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
150 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000152 bool isForceVarCapturing() const { return ForceCapturing; }
153 void setForceVarCapturing(bool V) { ForceCapturing = V; }
154
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000156 Scope *CurScope, SourceLocation Loc) {
157 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
158 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 }
160
161 void pop() {
162 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
163 Stack.pop_back();
164 }
165
Alexey Bataev28c75412015-12-15 08:19:24 +0000166 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
167 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
168 }
169 const std::pair<OMPCriticalDirective *, llvm::APSInt>
170 getCriticalWithHint(const DeclarationNameInfo &Name) const {
171 auto I = Criticals.find(Name.getAsString());
172 if (I != Criticals.end())
173 return I->second;
174 return std::make_pair(nullptr, llvm::APSInt());
175 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000176 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000177 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000178 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000179 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180
Alexey Bataev9c821032015-04-30 04:23:23 +0000181 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000182 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000183 /// \brief Check if the specified variable is a loop control variable for
184 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000185 /// \return The index of the loop control variable in the list of associated
186 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000187 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000188 /// \brief Check if the specified variable is a loop control variable for
189 /// parent region.
190 /// \return The index of the loop control variable in the list of associated
191 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000192 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000193 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
194 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000195 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000196
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000198 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200 /// \brief Returns data sharing attributes from top of the stack for the
201 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000202 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000204 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000205 /// \brief Checks if the specified variables has data-sharing attributes which
206 /// match specified \a CPred predicate in any directive which matches \a DPred
207 /// predicate.
208 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000210 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000211 /// \brief Checks if the specified variables has data-sharing attributes which
212 /// match specified \a CPred predicate in any innermost directive which
213 /// matches \a DPred predicate.
214 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000215 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
216 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000217 /// \brief Checks if the specified variables has explicit data-sharing
218 /// attributes which match specified \a CPred predicate at the specified
219 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000220 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000221 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
222 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000223
224 /// \brief Returns true if the directive at level \Level matches in the
225 /// specified \a DPred predicate.
226 bool hasExplicitDirective(
227 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
228 unsigned Level);
229
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000230 /// \brief Finds a directive which matches specified \a DPred predicate.
231 template <class NamedDirectivesPredicate>
232 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000233
Alexey Bataev758e55e2013-09-06 18:03:48 +0000234 /// \brief Returns currently analyzed directive.
235 OpenMPDirectiveKind getCurrentDirective() const {
236 return Stack.back().Directive;
237 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000238 /// \brief Returns parent directive.
239 OpenMPDirectiveKind getParentDirective() const {
240 if (Stack.size() > 2)
241 return Stack[Stack.size() - 2].Directive;
242 return OMPD_unknown;
243 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000244 /// \brief Return the directive associated with the provided scope.
245 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000248 void setDefaultDSANone(SourceLocation Loc) {
249 Stack.back().DefaultAttr = DSA_none;
250 Stack.back().DefaultAttrLoc = Loc;
251 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 void setDefaultDSAShared(SourceLocation Loc) {
254 Stack.back().DefaultAttr = DSA_shared;
255 Stack.back().DefaultAttrLoc = Loc;
256 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000257
258 DefaultDataSharingAttributes getDefaultDSA() const {
259 return Stack.back().DefaultAttr;
260 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000261 SourceLocation getDefaultDSALocation() const {
262 return Stack.back().DefaultAttrLoc;
263 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264
Alexey Bataevf29276e2014-06-18 04:14:57 +0000265 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000266 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000267 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 }
270
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000271 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 void setOrderedRegion(bool IsOrdered, Expr *Param) {
273 Stack.back().OrderedRegion.setInt(IsOrdered);
274 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000275 }
276 /// \brief Returns true, if parent region is ordered (has associated
277 /// 'ordered' clause), false - otherwise.
278 bool isParentOrderedRegion() const {
279 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000280 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000281 return false;
282 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 /// \brief Returns optional parameter for the ordered region.
284 Expr *getParentOrderedRegionParam() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
287 return nullptr;
288 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000289 /// \brief Marks current region as nowait (it has a 'nowait' clause).
290 void setNowaitRegion(bool IsNowait = true) {
291 Stack.back().NowaitRegion = IsNowait;
292 }
293 /// \brief Returns true, if parent region is nowait (has associated
294 /// 'nowait' clause), false - otherwise.
295 bool isParentNowaitRegion() const {
296 if (Stack.size() > 2)
297 return Stack[Stack.size() - 2].NowaitRegion;
298 return false;
299 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000300 /// \brief Marks parent region as cancel region.
301 void setParentCancelRegion(bool Cancel = true) {
302 if (Stack.size() > 2)
303 Stack[Stack.size() - 2].CancelRegion =
304 Stack[Stack.size() - 2].CancelRegion || Cancel;
305 }
306 /// \brief Return true if current region has inner cancel construct.
307 bool isCancelRegion() const {
308 return Stack.back().CancelRegion;
309 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000310
Alexey Bataev9c821032015-04-30 04:23:23 +0000311 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000312 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000313 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000314 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000315
Alexey Bataev13314bf2014-10-09 04:18:56 +0000316 /// \brief Marks current target region as one with closely nested teams
317 /// region.
318 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
319 if (Stack.size() > 2)
320 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
321 }
322 /// \brief Returns true, if current region has closely nested teams region.
323 bool hasInnerTeamsRegion() const {
324 return getInnerTeamsRegionLoc().isValid();
325 }
326 /// \brief Returns location of the nested teams region (if any).
327 SourceLocation getInnerTeamsRegionLoc() const {
328 if (Stack.size() > 1)
329 return Stack.back().InnerTeamsRegionLoc;
330 return SourceLocation();
331 }
332
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000333 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000334 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000335 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000336
Samuel Antao5de996e2016-01-22 20:21:36 +0000337 // Do the check specified in MapInfoCheck and return true if any issue is
338 // found.
339 template <class MapInfoCheck>
340 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
341 MapInfoCheck Check) {
342 auto SI = Stack.rbegin();
343 auto SE = Stack.rend();
344
345 if (SI == SE)
346 return false;
347
348 if (CurrentRegionOnly) {
349 SE = std::next(SI);
350 } else {
351 ++SI;
352 }
353
354 for (; SI != SE; ++SI) {
355 auto MI = SI->MappedDecls.find(VD);
356 if (MI != SI->MappedDecls.end()) {
357 for (Expr *E : MI->second) {
358 if (Check(E))
359 return true;
360 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000361 }
362 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000363 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000364 }
365
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000368 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000369 }
370 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000371};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
373 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000374 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000375 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000376}
Alexey Bataeved09d242014-05-28 05:53:51 +0000377} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000378
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000379static ValueDecl *getCanonicalDecl(ValueDecl *D) {
380 auto *VD = dyn_cast<VarDecl>(D);
381 auto *FD = dyn_cast<FieldDecl>(D);
382 if (VD != nullptr) {
383 VD = VD->getCanonicalDecl();
384 D = VD;
385 } else {
386 assert(FD);
387 FD = FD->getCanonicalDecl();
388 D = FD;
389 }
390 return D;
391}
392
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000394 ValueDecl *D) {
395 D = getCanonicalDecl(D);
396 auto *VD = dyn_cast<VarDecl>(D);
397 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000399 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000400 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
401 // in a region but not in construct]
402 // File-scope or namespace-scope variables referenced in called routines
403 // in the region are shared unless they appear in a threadprivate
404 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000405 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000406 DVar.CKind = OMPC_shared;
407
408 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
409 // in a region but not in construct]
410 // Variables with static storage duration that are declared in called
411 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000412 if (VD && VD->hasGlobalStorage())
413 DVar.CKind = OMPC_shared;
414
415 // Non-static data members are shared by default.
416 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000417 DVar.CKind = OMPC_shared;
418
Alexey Bataev758e55e2013-09-06 18:03:48 +0000419 return DVar;
420 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000421
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000423 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
424 // in a Construct, C/C++, predetermined, p.1]
425 // Variables with automatic storage duration that are declared in a scope
426 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000427 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
428 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000429 DVar.CKind = OMPC_private;
430 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000431 }
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 // Explicitly specified attributes and local variables with predetermined
434 // attributes.
435 if (Iter->SharingMap.count(D)) {
436 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
437 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
441
442 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
443 // in a Construct, C/C++, implicitly determined, p.1]
444 // In a parallel or task construct, the data-sharing attributes of these
445 // variables are determined by the default clause, if present.
446 switch (Iter->DefaultAttr) {
447 case DSA_shared:
448 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000449 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450 return DVar;
451 case DSA_none:
452 return DVar;
453 case DSA_unspecified:
454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
455 // in a Construct, implicitly determined, p.2]
456 // In a parallel construct, if no default clause is present, these
457 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000458 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000459 if (isOpenMPParallelDirective(DVar.DKind) ||
460 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000461 DVar.CKind = OMPC_shared;
462 return DVar;
463 }
464
465 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
466 // in a Construct, implicitly determined, p.4]
467 // In a task construct, if no default clause is present, a variable that in
468 // the enclosing context is determined to be shared by all implicit tasks
469 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 if (DVar.DKind == OMPD_task) {
471 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000472 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000473 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000474 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
475 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 // in a Construct, implicitly determined, p.6]
477 // In a task construct, if no default clause is present, a variable
478 // whose data-sharing attribute is not determined by the rules above is
479 // firstprivate.
480 DVarTemp = getDSA(I, D);
481 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000482 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000483 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000484 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485 return DVar;
486 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000487 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000488 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 }
490 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 return DVar;
494 }
495 }
496 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
497 // in a Construct, implicitly determined, p.3]
498 // For constructs other than task, if no default clause is present, these
499 // variables inherit their data-sharing attributes from the enclosing
500 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000501 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502}
503
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000504Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000505 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000506 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000507 auto It = Stack.back().AlignedMap.find(D);
508 if (It == Stack.back().AlignedMap.end()) {
509 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
510 Stack.back().AlignedMap[D] = NewDE;
511 return nullptr;
512 } else {
513 assert(It->second && "Unexpected nullptr expr in the aligned map");
514 return It->second;
515 }
516 return nullptr;
517}
518
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000519void DSAStackTy::addLoopControlVariable(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 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000523}
524
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000526 assert(Stack.size() > 1 && "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.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
529}
530
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000532 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000534 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
535 ? Stack[Stack.size() - 2].LCVMap[D]
536 : 0;
537}
538
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000539ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000540 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
541 if (Stack[Stack.size() - 2].LCVMap.size() < I)
542 return nullptr;
543 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
544 if (Pair.second == I)
545 return Pair.first;
546 }
547 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000548}
549
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000550void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A) {
551 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000552 if (A == OMPC_threadprivate) {
553 Stack[0].SharingMap[D].Attributes = A;
554 Stack[0].SharingMap[D].RefExpr = E;
555 } else {
556 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
557 Stack.back().SharingMap[D].Attributes = A;
558 Stack.back().SharingMap[D].RefExpr = E;
559 }
560}
561
Alexey Bataeved09d242014-05-28 05:53:51 +0000562bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000563 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000564 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000565 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000566 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000567 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000568 ++I;
569 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000570 if (I == E)
571 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000572 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 Scope *CurScope = getCurScope();
574 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000575 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000576 }
577 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000578 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000579 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000580}
581
Alexey Bataev39f915b82015-05-08 10:41:21 +0000582/// \brief Build a variable declaration for OpenMP loop iteration variable.
583static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000584 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000585 DeclContext *DC = SemaRef.CurContext;
586 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
587 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
588 VarDecl *Decl =
589 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000590 if (Attrs) {
591 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
592 I != E; ++I)
593 Decl->addAttr(*I);
594 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000595 Decl->setImplicit();
596 return Decl;
597}
598
599static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
600 SourceLocation Loc,
601 bool RefersToCapture = false) {
602 D->setReferenced();
603 D->markUsed(S.Context);
604 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
605 SourceLocation(), D, RefersToCapture, Loc, Ty,
606 VK_LValue);
607}
608
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000609DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
610 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000611 DSAVarData DVar;
612
613 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
614 // in a Construct, C/C++, predetermined, p.1]
615 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000616 auto *VD = dyn_cast<VarDecl>(D);
617 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
618 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000619 SemaRef.getLangOpts().OpenMPUseTLS &&
620 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000621 (VD && VD->getStorageClass() == SC_Register &&
622 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
623 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000624 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000625 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000626 }
627 if (Stack[0].SharingMap.count(D)) {
628 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
629 DVar.CKind = OMPC_threadprivate;
630 return DVar;
631 }
632
633 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000634 // in a Construct, C/C++, predetermined, p.4]
635 // Static data members are shared.
636 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
637 // in a Construct, C/C++, predetermined, p.7]
638 // Variables with static storage duration that are declared in a scope
639 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000640 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000641 DSAVarData DVarTemp =
642 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
643 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000644 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000645
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000646 DVar.CKind = OMPC_shared;
647 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000648 }
649
650 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000651 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
652 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
654 // in a Construct, C/C++, predetermined, p.6]
655 // Variables with const qualified type having no mutable member are
656 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000657 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000658 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000659 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
660 if (auto *CTD = CTSD->getSpecializedTemplate())
661 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000663 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 // Variables with const-qualified type having no mutable member may be
665 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000666 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
667 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000668 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
669 return DVar;
670
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 DVar.CKind = OMPC_shared;
672 return DVar;
673 }
674
Alexey Bataev758e55e2013-09-06 18:03:48 +0000675 // Explicitly specified attributes and local variables with predetermined
676 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000677 auto StartI = std::next(Stack.rbegin());
678 auto EndI = std::prev(Stack.rend());
679 if (FromParent && StartI != EndI) {
680 StartI = std::next(StartI);
681 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000682 auto I = std::prev(StartI);
683 if (I->SharingMap.count(D)) {
684 DVar.RefExpr = I->SharingMap[D].RefExpr;
685 DVar.CKind = I->SharingMap[D].Attributes;
686 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000687 }
688
689 return DVar;
690}
691
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000692DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
693 bool FromParent) {
694 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000695 auto StartI = Stack.rbegin();
696 auto EndI = std::prev(Stack.rend());
697 if (FromParent && StartI != EndI) {
698 StartI = std::next(StartI);
699 }
700 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701}
702
Alexey Bataevf29276e2014-06-18 04:14:57 +0000703template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000704DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000705 DirectivesPredicate DPred,
706 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000707 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000708 auto StartI = std::next(Stack.rbegin());
709 auto EndI = std::prev(Stack.rend());
710 if (FromParent && StartI != EndI) {
711 StartI = std::next(StartI);
712 }
713 for (auto I = StartI, EE = EndI; I != EE; ++I) {
714 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000715 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000716 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000717 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000718 return DVar;
719 }
720 return DSAVarData();
721}
722
Alexey Bataevf29276e2014-06-18 04:14:57 +0000723template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000725DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000726 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000727 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 auto StartI = std::next(Stack.rbegin());
729 auto EndI = std::prev(Stack.rend());
730 if (FromParent && StartI != EndI) {
731 StartI = std::next(StartI);
732 }
733 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000736 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000737 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000738 return DVar;
739 return DSAVarData();
740 }
741 return DSAVarData();
742}
743
Alexey Bataevaac108a2015-06-23 04:51:00 +0000744bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000745 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000746 unsigned Level) {
747 if (CPred(ClauseKindMode))
748 return true;
749 if (isClauseParsingMode())
750 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000752 auto StartI = Stack.rbegin();
753 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000754 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000755 return false;
756 std::advance(StartI, Level);
757 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
758 CPred(StartI->SharingMap[D].Attributes);
759}
760
Samuel Antao4be30e92015-10-02 17:14:03 +0000761bool DSAStackTy::hasExplicitDirective(
762 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
763 unsigned Level) {
764 if (isClauseParsingMode())
765 ++Level;
766 auto StartI = Stack.rbegin();
767 auto EndI = std::prev(Stack.rend());
768 if (std::distance(StartI, EndI) <= (int)Level)
769 return false;
770 std::advance(StartI, Level);
771 return DPred(StartI->Directive);
772}
773
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000774template <class NamedDirectivesPredicate>
775bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
776 auto StartI = std::next(Stack.rbegin());
777 auto EndI = std::prev(Stack.rend());
778 if (FromParent && StartI != EndI) {
779 StartI = std::next(StartI);
780 }
781 for (auto I = StartI, EE = EndI; I != EE; ++I) {
782 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
783 return true;
784 }
785 return false;
786}
787
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000788OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
789 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
790 if (I->CurScope == S)
791 return I->Directive;
792 return OMPD_unknown;
793}
794
Alexey Bataev758e55e2013-09-06 18:03:48 +0000795void Sema::InitDataSharingAttributesStack() {
796 VarDataSharingAttributesStack = new DSAStackTy(*this);
797}
798
799#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
800
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000801bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000802 const CapturedRegionScopeInfo *RSI) {
803 assert(LangOpts.OpenMP && "OpenMP is not allowed");
804
805 auto &Ctx = getASTContext();
806 bool IsByRef = true;
807
808 // Find the directive that is associated with the provided scope.
809 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000810 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000811
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000812 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000813 // This table summarizes how a given variable should be passed to the device
814 // given its type and the clauses where it appears. This table is based on
815 // the description in OpenMP 4.5 [2.10.4, target Construct] and
816 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
817 //
818 // =========================================================================
819 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
820 // | |(tofrom:scalar)| | pvt | | | |
821 // =========================================================================
822 // | scl | | | | - | | bycopy|
823 // | scl | | - | x | - | - | bycopy|
824 // | scl | | x | - | - | - | null |
825 // | scl | x | | | - | | byref |
826 // | scl | x | - | x | - | - | bycopy|
827 // | scl | x | x | - | - | - | null |
828 // | scl | | - | - | - | x | byref |
829 // | scl | x | - | - | - | x | byref |
830 //
831 // | agg | n.a. | | | - | | byref |
832 // | agg | n.a. | - | x | - | - | byref |
833 // | agg | n.a. | x | - | - | - | null |
834 // | agg | n.a. | - | - | - | x | byref |
835 // | agg | n.a. | - | - | - | x[] | byref |
836 //
837 // | ptr | n.a. | | | - | | bycopy|
838 // | ptr | n.a. | - | x | - | - | bycopy|
839 // | ptr | n.a. | x | - | - | - | null |
840 // | ptr | n.a. | - | - | - | x | byref |
841 // | ptr | n.a. | - | - | - | x[] | bycopy|
842 // | ptr | n.a. | - | - | x | | bycopy|
843 // | ptr | n.a. | - | - | x | x | bycopy|
844 // | ptr | n.a. | - | - | x | x[] | bycopy|
845 // =========================================================================
846 // Legend:
847 // scl - scalar
848 // ptr - pointer
849 // agg - aggregate
850 // x - applies
851 // - - invalid in this combination
852 // [] - mapped with an array section
853 // byref - should be mapped by reference
854 // byval - should be mapped by value
855 // null - initialize a local variable to null on the device
856 //
857 // Observations:
858 // - All scalar declarations that show up in a map clause have to be passed
859 // by reference, because they may have been mapped in the enclosing data
860 // environment.
861 // - If the scalar value does not fit the size of uintptr, it has to be
862 // passed by reference, regardless the result in the table above.
863 // - For pointers mapped by value that have either an implicit map or an
864 // array section, the runtime library may pass the NULL value to the
865 // device instead of the value passed to it by the compiler.
866
867 // FIXME: Right now, only implicit maps are implemented. Properly mapping
868 // values requires having the map, private, and firstprivate clauses SEMA
869 // and parsing in place, which we don't yet.
870
871 if (Ty->isReferenceType())
872 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
873 IsByRef = !Ty->isScalarType();
874 }
875
876 // When passing data by value, we need to make sure it fits the uintptr size
877 // and alignment, because the runtime library only deals with uintptr types.
878 // If it does not fit the uintptr size, we need to pass the data by reference
879 // instead.
880 if (!IsByRef &&
881 (Ctx.getTypeSizeInChars(Ty) >
882 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000883 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000884 IsByRef = true;
885
886 return IsByRef;
887}
888
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000889bool Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000890 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000891 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000892
893 // If we are attempting to capture a global variable in a directive with
894 // 'target' we return true so that this global is also mapped to the device.
895 //
896 // FIXME: If the declaration is enclosed in a 'declare target' directive,
897 // then it should not be captured. Therefore, an extra check has to be
898 // inserted here once support for 'declare target' is added.
899 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000900 auto *VD = dyn_cast<VarDecl>(D);
901 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000902 if (DSAStack->getCurrentDirective() == OMPD_target &&
903 !DSAStack->isClauseParsingMode()) {
904 return true;
905 }
906 if (DSAStack->getCurScope() &&
907 DSAStack->hasDirective(
908 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
909 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000910 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000911 },
912 false)) {
913 return true;
914 }
915 }
916
Alexey Bataev48977c32015-08-04 08:10:48 +0000917 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
918 (!DSAStack->isClauseParsingMode() ||
919 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000920 if (DSAStack->isLoopControlVariable(D) ||
921 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000922 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000923 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev9c821032015-04-30 04:23:23 +0000924 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000925 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000926 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
927 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000928 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000929 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000930 return DVarPrivate.CKind != OMPC_unknown;
931 }
932 return false;
933}
934
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000935bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000936 assert(LangOpts.OpenMP && "OpenMP is not allowed");
937 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000938 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000939}
940
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000941bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000942 assert(LangOpts.OpenMP && "OpenMP is not allowed");
943 // Return true if the current level is no longer enclosed in a target region.
944
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945 auto *VD = dyn_cast<VarDecl>(D);
946 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000947 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
948 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000949}
950
Alexey Bataeved09d242014-05-28 05:53:51 +0000951void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000952
953void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
954 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000955 Scope *CurScope, SourceLocation Loc) {
956 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 PushExpressionEvaluationContext(PotentiallyEvaluated);
958}
959
Alexey Bataevaac108a2015-06-23 04:51:00 +0000960void Sema::StartOpenMPClause(OpenMPClauseKind K) {
961 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000962}
963
Alexey Bataevaac108a2015-06-23 04:51:00 +0000964void Sema::EndOpenMPClause() {
965 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000966}
967
Alexey Bataev758e55e2013-09-06 18:03:48 +0000968void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000969 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
970 // A variable of class type (or array thereof) that appears in a lastprivate
971 // clause requires an accessible, unambiguous default constructor for the
972 // class type, unless the list item is also specified in a firstprivate
973 // clause.
974 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000975 for (auto *C : D->clauses()) {
976 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
977 SmallVector<Expr *, 8> PrivateCopies;
978 for (auto *DE : Clause->varlists()) {
979 if (DE->isValueDependent() || DE->isTypeDependent()) {
980 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000981 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000982 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000983 DE = DE->IgnoreParens();
984 VarDecl *VD = nullptr;
985 FieldDecl *FD = nullptr;
986 ValueDecl *D;
987 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
988 VD = cast<VarDecl>(DRE->getDecl());
989 D = VD;
990 } else {
991 assert(isa<MemberExpr>(DE));
992 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
993 D = FD;
994 }
995 QualType Type = D->getType().getNonReferenceType();
996 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000998 // Generate helper private variable and initialize it with the
999 // default value. The address of the original variable is replaced
1000 // by the address of the new private variable in CodeGen. This new
1001 // variable is not added to IdResolver, so the code in the OpenMP
1002 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001003 auto *VDPrivate = buildVarDecl(
1004 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001005 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001006 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1007 if (VDPrivate->isInvalidDecl())
1008 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001009 PrivateCopies.push_back(buildDeclRefExpr(
1010 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001011 } else {
1012 // The variable is also a firstprivate, so initialization sequence
1013 // for private copy is generated already.
1014 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001015 }
1016 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001018 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001019 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001020 }
1021 }
1022 }
1023
Alexey Bataev758e55e2013-09-06 18:03:48 +00001024 DSAStack->pop();
1025 DiscardCleanupsInEvaluationContext();
1026 PopExpressionEvaluationContext();
1027}
1028
Alexander Musman3276a272015-03-21 10:12:56 +00001029static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1030 Expr *NumIterations, Sema &SemaRef,
1031 Scope *S);
1032
Alexey Bataeva769e072013-03-22 06:34:35 +00001033namespace {
1034
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001035class VarDeclFilterCCC : public CorrectionCandidateCallback {
1036private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001037 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001038
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001039public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001040 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001041 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042 NamedDecl *ND = Candidate.getCorrectionDecl();
1043 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1044 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001045 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1046 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001047 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050};
Alexey Bataeved09d242014-05-28 05:53:51 +00001051} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052
1053ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1054 CXXScopeSpec &ScopeSpec,
1055 const DeclarationNameInfo &Id) {
1056 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1057 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1058
1059 if (Lookup.isAmbiguous())
1060 return ExprError();
1061
1062 VarDecl *VD;
1063 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001064 if (TypoCorrection Corrected = CorrectTypo(
1065 Id, LookupOrdinaryName, CurScope, nullptr,
1066 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001067 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001068 PDiag(Lookup.empty()
1069 ? diag::err_undeclared_var_use_suggest
1070 : diag::err_omp_expected_var_arg_suggest)
1071 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001072 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001073 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001074 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1075 : diag::err_omp_expected_var_arg)
1076 << Id.getName();
1077 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001079 } else {
1080 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001081 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001082 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1083 return ExprError();
1084 }
1085 }
1086 Lookup.suppressDiagnostics();
1087
1088 // OpenMP [2.9.2, Syntax, C/C++]
1089 // Variables must be file-scope, namespace-scope, or static block-scope.
1090 if (!VD->hasGlobalStorage()) {
1091 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1093 bool IsDecl =
1094 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001095 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001096 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1097 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001098 return ExprError();
1099 }
1100
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001101 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1102 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001103 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1104 // A threadprivate directive for file-scope variables must appear outside
1105 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001106 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1107 !getCurLexicalContext()->isTranslationUnit()) {
1108 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001109 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1110 bool IsDecl =
1111 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1112 Diag(VD->getLocation(),
1113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1114 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001115 return ExprError();
1116 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001117 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1118 // A threadprivate directive for static class member variables must appear
1119 // in the class definition, in the same scope in which the member
1120 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001121 if (CanonicalVD->isStaticDataMember() &&
1122 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1123 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001124 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1125 bool IsDecl =
1126 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1127 Diag(VD->getLocation(),
1128 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1129 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001130 return ExprError();
1131 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001132 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1133 // A threadprivate directive for namespace-scope variables must appear
1134 // outside any definition or declaration other than the namespace
1135 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001136 if (CanonicalVD->getDeclContext()->isNamespace() &&
1137 (!getCurLexicalContext()->isFileContext() ||
1138 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1139 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001140 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1141 bool IsDecl =
1142 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1143 Diag(VD->getLocation(),
1144 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1145 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001146 return ExprError();
1147 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1149 // A threadprivate directive for static block-scope variables must appear
1150 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001151 if (CanonicalVD->isStaticLocal() && CurScope &&
1152 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001153 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001154 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1155 bool IsDecl =
1156 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1157 Diag(VD->getLocation(),
1158 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1159 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 return ExprError();
1161 }
1162
1163 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1164 // A threadprivate directive must lexically precede all references to any
1165 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001166 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001168 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 return ExprError();
1170 }
1171
1172 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001173 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 return DE;
1175}
1176
Alexey Bataeved09d242014-05-28 05:53:51 +00001177Sema::DeclGroupPtrTy
1178Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1179 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001181 CurContext->addDecl(D);
1182 return DeclGroupPtrTy::make(DeclGroupRef(D));
1183 }
David Blaikie0403cb12016-01-15 23:43:25 +00001184 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001185}
1186
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001187namespace {
1188class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1189 Sema &SemaRef;
1190
1191public:
1192 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1193 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1194 if (VD->hasLocalStorage()) {
1195 SemaRef.Diag(E->getLocStart(),
1196 diag::err_omp_local_var_in_threadprivate_init)
1197 << E->getSourceRange();
1198 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1199 << VD << VD->getSourceRange();
1200 return true;
1201 }
1202 }
1203 return false;
1204 }
1205 bool VisitStmt(const Stmt *S) {
1206 for (auto Child : S->children()) {
1207 if (Child && Visit(Child))
1208 return true;
1209 }
1210 return false;
1211 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001212 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001213};
1214} // namespace
1215
Alexey Bataeved09d242014-05-28 05:53:51 +00001216OMPThreadPrivateDecl *
1217Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001218 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001219 for (auto &RefExpr : VarList) {
1220 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001221 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1222 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001223
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001224 QualType QType = VD->getType();
1225 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1226 // It will be analyzed later.
1227 Vars.push_back(DE);
1228 continue;
1229 }
1230
Alexey Bataeva769e072013-03-22 06:34:35 +00001231 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1232 // A threadprivate variable must not have an incomplete type.
1233 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001234 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001235 continue;
1236 }
1237
1238 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1239 // A threadprivate variable must not have a reference type.
1240 if (VD->getType()->isReferenceType()) {
1241 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001242 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1243 bool IsDecl =
1244 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1245 Diag(VD->getLocation(),
1246 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1247 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 continue;
1249 }
1250
Samuel Antaof8b50122015-07-13 22:54:53 +00001251 // Check if this is a TLS variable. If TLS is not being supported, produce
1252 // the corresponding diagnostic.
1253 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1254 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1255 getLangOpts().OpenMPUseTLS &&
1256 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001257 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1258 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001259 Diag(ILoc, diag::err_omp_var_thread_local)
1260 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001261 bool IsDecl =
1262 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1263 Diag(VD->getLocation(),
1264 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1265 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001266 continue;
1267 }
1268
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001269 // Check if initial value of threadprivate variable reference variable with
1270 // local storage (it is not supported by runtime).
1271 if (auto Init = VD->getAnyInitializer()) {
1272 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001273 if (Checker.Visit(Init))
1274 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001275 }
1276
Alexey Bataeved09d242014-05-28 05:53:51 +00001277 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001278 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001279 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1280 Context, SourceRange(Loc, Loc)));
1281 if (auto *ML = Context.getASTMutationListener())
1282 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001283 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001284 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001285 if (!Vars.empty()) {
1286 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1287 Vars);
1288 D->setAccess(AS_public);
1289 }
1290 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001291}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001292
Alexey Bataev7ff55242014-06-19 09:13:45 +00001293static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001294 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001295 bool IsLoopIterVar = false) {
1296 if (DVar.RefExpr) {
1297 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1298 << getOpenMPClauseName(DVar.CKind);
1299 return;
1300 }
1301 enum {
1302 PDSA_StaticMemberShared,
1303 PDSA_StaticLocalVarShared,
1304 PDSA_LoopIterVarPrivate,
1305 PDSA_LoopIterVarLinear,
1306 PDSA_LoopIterVarLastprivate,
1307 PDSA_ConstVarShared,
1308 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001309 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001310 PDSA_LocalVarPrivate,
1311 PDSA_Implicit
1312 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001313 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001314 auto ReportLoc = D->getLocation();
1315 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001316 if (IsLoopIterVar) {
1317 if (DVar.CKind == OMPC_private)
1318 Reason = PDSA_LoopIterVarPrivate;
1319 else if (DVar.CKind == OMPC_lastprivate)
1320 Reason = PDSA_LoopIterVarLastprivate;
1321 else
1322 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001323 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1324 Reason = PDSA_TaskVarFirstprivate;
1325 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001326 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001327 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001328 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001329 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001330 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001331 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001332 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001333 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001334 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001335 ReportHint = true;
1336 Reason = PDSA_LocalVarPrivate;
1337 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001338 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001339 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001340 << Reason << ReportHint
1341 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1342 } else if (DVar.ImplicitDSALoc.isValid()) {
1343 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1344 << getOpenMPClauseName(DVar.CKind);
1345 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001346}
1347
Alexey Bataev758e55e2013-09-06 18:03:48 +00001348namespace {
1349class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1350 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001351 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001352 bool ErrorFound;
1353 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001354 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001355 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001356
Alexey Bataev758e55e2013-09-06 18:03:48 +00001357public:
1358 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001359 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001361 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1362 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001364 auto DVar = Stack->getTopDSA(VD, false);
1365 // Check if the variable has explicit DSA set and stop analysis if it so.
1366 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001367
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001368 auto ELoc = E->getExprLoc();
1369 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001370 // The default(none) clause requires that each variable that is referenced
1371 // in the construct, and does not have a predetermined data-sharing
1372 // attribute, must have its data-sharing attribute explicitly determined
1373 // by being listed in a data-sharing attribute clause.
1374 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001375 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001376 VarsWithInheritedDSA.count(VD) == 0) {
1377 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001378 return;
1379 }
1380
1381 // OpenMP [2.9.3.6, Restrictions, p.2]
1382 // A list item that appears in a reduction clause of the innermost
1383 // enclosing worksharing or parallel construct may not be accessed in an
1384 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001385 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001386 [](OpenMPDirectiveKind K) -> bool {
1387 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001388 isOpenMPWorksharingDirective(K) ||
1389 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001390 },
1391 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001392 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1393 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001394 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1395 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001396 return;
1397 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001398
1399 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001400 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001401 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001402 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001403 }
1404 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001405 void VisitMemberExpr(MemberExpr *E) {
1406 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1407 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1408 auto DVar = Stack->getTopDSA(FD, false);
1409 // Check if the variable has explicit DSA set and stop analysis if it
1410 // so.
1411 if (DVar.RefExpr)
1412 return;
1413
1414 auto ELoc = E->getExprLoc();
1415 auto DKind = Stack->getCurrentDirective();
1416 // OpenMP [2.9.3.6, Restrictions, p.2]
1417 // A list item that appears in a reduction clause of the innermost
1418 // enclosing worksharing or parallel construct may not be accessed in
1419 // an
1420 // explicit task.
1421 DVar =
1422 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1423 [](OpenMPDirectiveKind K) -> bool {
1424 return isOpenMPParallelDirective(K) ||
1425 isOpenMPWorksharingDirective(K) ||
1426 isOpenMPTeamsDirective(K);
1427 },
1428 false);
1429 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1430 ErrorFound = true;
1431 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1432 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1433 return;
1434 }
1435
1436 // Define implicit data-sharing attributes for task.
1437 DVar = Stack->getImplicitDSA(FD, false);
1438 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1439 ImplicitFirstprivate.push_back(E);
1440 }
1441 }
1442 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001443 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 for (auto *C : S->clauses()) {
1445 // Skip analysis of arguments of implicitly defined firstprivate clause
1446 // for task directives.
1447 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1448 for (auto *CC : C->children()) {
1449 if (CC)
1450 Visit(CC);
1451 }
1452 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453 }
1454 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001455 for (auto *C : S->children()) {
1456 if (C && !isa<OMPExecutableDirective>(C))
1457 Visit(C);
1458 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001459 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001460
1461 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001462 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001463 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001464 return VarsWithInheritedDSA;
1465 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001466
Alexey Bataev7ff55242014-06-19 09:13:45 +00001467 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1468 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469};
Alexey Bataeved09d242014-05-28 05:53:51 +00001470} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataevbae9a792014-06-27 10:37:06 +00001472void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001473 switch (DKind) {
1474 case OMPD_parallel: {
1475 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001476 QualType KmpInt32PtrTy =
1477 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001478 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001479 std::make_pair(".global_tid.", KmpInt32PtrTy),
1480 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1481 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001482 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001483 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1484 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001485 break;
1486 }
1487 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001488 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001489 std::make_pair(StringRef(), QualType()) // __context with shared vars
1490 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001491 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1492 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001493 break;
1494 }
1495 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001496 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001497 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1500 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 break;
1502 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001503 case OMPD_for_simd: {
1504 Sema::CapturedParamNameType Params[] = {
1505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
1507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
1509 break;
1510 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001511 case OMPD_sections: {
1512 Sema::CapturedParamNameType Params[] = {
1513 std::make_pair(StringRef(), QualType()) // __context with shared vars
1514 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1516 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001517 break;
1518 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001519 case OMPD_section: {
1520 Sema::CapturedParamNameType Params[] = {
1521 std::make_pair(StringRef(), QualType()) // __context with shared vars
1522 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001523 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1524 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001525 break;
1526 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001527 case OMPD_single: {
1528 Sema::CapturedParamNameType Params[] = {
1529 std::make_pair(StringRef(), QualType()) // __context with shared vars
1530 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001531 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1532 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001533 break;
1534 }
Alexander Musman80c22892014-07-17 08:54:58 +00001535 case OMPD_master: {
1536 Sema::CapturedParamNameType Params[] = {
1537 std::make_pair(StringRef(), QualType()) // __context with shared vars
1538 };
1539 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1540 Params);
1541 break;
1542 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001543 case OMPD_critical: {
1544 Sema::CapturedParamNameType Params[] = {
1545 std::make_pair(StringRef(), QualType()) // __context with shared vars
1546 };
1547 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1548 Params);
1549 break;
1550 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001551 case OMPD_parallel_for: {
1552 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001553 QualType KmpInt32PtrTy =
1554 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001555 Sema::CapturedParamNameType Params[] = {
1556 std::make_pair(".global_tid.", KmpInt32PtrTy),
1557 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1558 std::make_pair(StringRef(), QualType()) // __context with shared vars
1559 };
1560 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1561 Params);
1562 break;
1563 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001564 case OMPD_parallel_for_simd: {
1565 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001566 QualType KmpInt32PtrTy =
1567 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001568 Sema::CapturedParamNameType Params[] = {
1569 std::make_pair(".global_tid.", KmpInt32PtrTy),
1570 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1571 std::make_pair(StringRef(), QualType()) // __context with shared vars
1572 };
1573 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1574 Params);
1575 break;
1576 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001577 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001578 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001579 QualType KmpInt32PtrTy =
1580 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001581 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001582 std::make_pair(".global_tid.", KmpInt32PtrTy),
1583 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001584 std::make_pair(StringRef(), QualType()) // __context with shared vars
1585 };
1586 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1587 Params);
1588 break;
1589 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001590 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001591 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001592 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1593 FunctionProtoType::ExtProtoInfo EPI;
1594 EPI.Variadic = true;
1595 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001597 std::make_pair(".global_tid.", KmpInt32Ty),
1598 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001599 std::make_pair(".privates.",
1600 Context.VoidPtrTy.withConst().withRestrict()),
1601 std::make_pair(
1602 ".copy_fn.",
1603 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001604 std::make_pair(StringRef(), QualType()) // __context with shared vars
1605 };
1606 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1607 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001608 // Mark this captured region as inlined, because we don't use outlined
1609 // function directly.
1610 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1611 AlwaysInlineAttr::CreateImplicit(
1612 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001613 break;
1614 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001615 case OMPD_ordered: {
1616 Sema::CapturedParamNameType Params[] = {
1617 std::make_pair(StringRef(), QualType()) // __context with shared vars
1618 };
1619 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1620 Params);
1621 break;
1622 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001623 case OMPD_atomic: {
1624 Sema::CapturedParamNameType Params[] = {
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
1627 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628 Params);
1629 break;
1630 }
Michael Wong65f367f2015-07-21 13:44:28 +00001631 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001632 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001633 case OMPD_target_parallel:
1634 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001635 Sema::CapturedParamNameType Params[] = {
1636 std::make_pair(StringRef(), QualType()) // __context with shared vars
1637 };
1638 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1639 Params);
1640 break;
1641 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001642 case OMPD_teams: {
1643 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001644 QualType KmpInt32PtrTy =
1645 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001646 Sema::CapturedParamNameType Params[] = {
1647 std::make_pair(".global_tid.", KmpInt32PtrTy),
1648 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1649 std::make_pair(StringRef(), QualType()) // __context with shared vars
1650 };
1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1652 Params);
1653 break;
1654 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001655 case OMPD_taskgroup: {
1656 Sema::CapturedParamNameType Params[] = {
1657 std::make_pair(StringRef(), QualType()) // __context with shared vars
1658 };
1659 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1660 Params);
1661 break;
1662 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001663 case OMPD_taskloop: {
1664 Sema::CapturedParamNameType Params[] = {
1665 std::make_pair(StringRef(), QualType()) // __context with shared vars
1666 };
1667 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1668 Params);
1669 break;
1670 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001671 case OMPD_taskloop_simd: {
1672 Sema::CapturedParamNameType Params[] = {
1673 std::make_pair(StringRef(), QualType()) // __context with shared vars
1674 };
1675 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1676 Params);
1677 break;
1678 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001679 case OMPD_distribute: {
1680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(StringRef(), QualType()) // __context with shared vars
1682 };
1683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1684 Params);
1685 break;
1686 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001687 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001688 case OMPD_taskyield:
1689 case OMPD_barrier:
1690 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001691 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001692 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001693 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001694 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001695 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001696 llvm_unreachable("OpenMP Directive is not allowed");
1697 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001698 llvm_unreachable("Unknown OpenMP directive");
1699 }
1700}
1701
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001702StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1703 ArrayRef<OMPClause *> Clauses) {
1704 if (!S.isUsable()) {
1705 ActOnCapturedRegionError();
1706 return StmtError();
1707 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001708
1709 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001710 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001711 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001712 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001713 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001714 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001715 Clause->getClauseKind() == OMPC_copyprivate ||
1716 (getLangOpts().OpenMPUseTLS &&
1717 getASTContext().getTargetInfo().isTLSSupported() &&
1718 Clause->getClauseKind() == OMPC_copyin)) {
1719 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001720 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001721 for (auto *VarRef : Clause->children()) {
1722 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001723 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001724 }
1725 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001726 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001727 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1728 Clause->getClauseKind() == OMPC_schedule) {
1729 // Mark all variables in private list clauses as used in inner region.
1730 // Required for proper codegen of combined directives.
1731 // TODO: add processing for other clauses.
1732 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001733 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1734 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001735 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001736 if (Clause->getClauseKind() == OMPC_schedule)
1737 SC = cast<OMPScheduleClause>(Clause);
1738 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001739 OC = cast<OMPOrderedClause>(Clause);
1740 else if (Clause->getClauseKind() == OMPC_linear)
1741 LCs.push_back(cast<OMPLinearClause>(Clause));
1742 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001743 bool ErrorFound = false;
1744 // OpenMP, 2.7.1 Loop Construct, Restrictions
1745 // The nonmonotonic modifier cannot be specified if an ordered clause is
1746 // specified.
1747 if (SC &&
1748 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1749 SC->getSecondScheduleModifier() ==
1750 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1751 OC) {
1752 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1753 ? SC->getFirstScheduleModifierLoc()
1754 : SC->getSecondScheduleModifierLoc(),
1755 diag::err_omp_schedule_nonmonotonic_ordered)
1756 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1757 ErrorFound = true;
1758 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001759 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1760 for (auto *C : LCs) {
1761 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1762 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1763 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001764 ErrorFound = true;
1765 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001766 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1767 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1768 OC->getNumForLoops()) {
1769 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1770 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1771 ErrorFound = true;
1772 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001773 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001774 ActOnCapturedRegionError();
1775 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001776 }
1777 return ActOnCapturedRegionEnd(S.get());
1778}
1779
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001780static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1781 OpenMPDirectiveKind CurrentRegion,
1782 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001783 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001784 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001785 // Allowed nesting of constructs
1786 // +------------------+-----------------+------------------------------------+
1787 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1788 // +------------------+-----------------+------------------------------------+
1789 // | parallel | parallel | * |
1790 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001791 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001792 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001793 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001794 // | parallel | simd | * |
1795 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001796 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001797 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001798 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001799 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001800 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001801 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001802 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001803 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001804 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001805 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001806 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001807 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001808 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001809 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001810 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001811 // | parallel | target parallel | * |
1812 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001813 // | parallel | target enter | * |
1814 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001815 // | parallel | target exit | * |
1816 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001817 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001818 // | parallel | cancellation | |
1819 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001820 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001821 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001822 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001823 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001824 // +------------------+-----------------+------------------------------------+
1825 // | for | parallel | * |
1826 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001827 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001828 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001829 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001830 // | for | simd | * |
1831 // | for | sections | + |
1832 // | for | section | + |
1833 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001834 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001835 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001836 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001837 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001838 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001839 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001840 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001841 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001842 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001843 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001844 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001845 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001846 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001847 // | for | target parallel | * |
1848 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001849 // | for | target enter | * |
1850 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001851 // | for | target exit | * |
1852 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001853 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001854 // | for | cancellation | |
1855 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001856 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001857 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001858 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001859 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001860 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001861 // | master | parallel | * |
1862 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001863 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001864 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001865 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001866 // | master | simd | * |
1867 // | master | sections | + |
1868 // | master | section | + |
1869 // | master | single | + |
1870 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001871 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001872 // | master |parallel sections| * |
1873 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001874 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001875 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001876 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001877 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001878 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001879 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001880 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001881 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001882 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001883 // | master | target parallel | * |
1884 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001885 // | master | target enter | * |
1886 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001887 // | master | target exit | * |
1888 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001889 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001890 // | master | cancellation | |
1891 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001892 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001893 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001894 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001895 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001896 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001897 // | critical | parallel | * |
1898 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001899 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001900 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001901 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001902 // | critical | simd | * |
1903 // | critical | sections | + |
1904 // | critical | section | + |
1905 // | critical | single | + |
1906 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001907 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001908 // | critical |parallel sections| * |
1909 // | critical | task | * |
1910 // | critical | taskyield | * |
1911 // | critical | barrier | + |
1912 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001913 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001914 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001915 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001916 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001917 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001918 // | critical | target parallel | * |
1919 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001920 // | critical | target enter | * |
1921 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001922 // | critical | target exit | * |
1923 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001924 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001925 // | critical | cancellation | |
1926 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001927 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001928 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001929 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001930 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001931 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001932 // | simd | parallel | |
1933 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001934 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001935 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001936 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001937 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001938 // | simd | sections | |
1939 // | simd | section | |
1940 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001941 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001942 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001943 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001944 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001945 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001946 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001947 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001948 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001949 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001950 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001951 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001952 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001953 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001954 // | simd | target parallel | |
1955 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001956 // | simd | target enter | |
1957 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001958 // | simd | target exit | |
1959 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001960 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001961 // | simd | cancellation | |
1962 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001963 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001964 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001965 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001966 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001967 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001968 // | for simd | parallel | |
1969 // | for simd | for | |
1970 // | for simd | for simd | |
1971 // | for simd | master | |
1972 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001973 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001974 // | for simd | sections | |
1975 // | for simd | section | |
1976 // | for simd | single | |
1977 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001978 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001979 // | for simd |parallel sections| |
1980 // | for simd | task | |
1981 // | for simd | taskyield | |
1982 // | for simd | barrier | |
1983 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001984 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001985 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001986 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001987 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001988 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001989 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001990 // | for simd | target parallel | |
1991 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001992 // | for simd | target enter | |
1993 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001994 // | for simd | target exit | |
1995 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001996 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001997 // | for simd | cancellation | |
1998 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001999 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002000 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002001 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002002 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002003 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002004 // | parallel for simd| parallel | |
2005 // | parallel for simd| for | |
2006 // | parallel for simd| for simd | |
2007 // | parallel for simd| master | |
2008 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002009 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002010 // | parallel for simd| sections | |
2011 // | parallel for simd| section | |
2012 // | parallel for simd| single | |
2013 // | parallel for simd| parallel for | |
2014 // | parallel for simd|parallel for simd| |
2015 // | parallel for simd|parallel sections| |
2016 // | parallel for simd| task | |
2017 // | parallel for simd| taskyield | |
2018 // | parallel for simd| barrier | |
2019 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002020 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002021 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002022 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002023 // | parallel for simd| atomic | |
2024 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002025 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002026 // | parallel for simd| target parallel | |
2027 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002028 // | parallel for simd| target enter | |
2029 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002030 // | parallel for simd| target exit | |
2031 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002032 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002033 // | parallel for simd| cancellation | |
2034 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002035 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002036 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002037 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002038 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002039 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002040 // | sections | parallel | * |
2041 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002042 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002043 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002044 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002045 // | sections | simd | * |
2046 // | sections | sections | + |
2047 // | sections | section | * |
2048 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002049 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002050 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002051 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002052 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002053 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002054 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002055 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002056 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002057 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002058 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002059 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002060 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002061 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002062 // | sections | target parallel | * |
2063 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002064 // | sections | target enter | * |
2065 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002066 // | sections | target exit | * |
2067 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002068 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002069 // | sections | cancellation | |
2070 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002071 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002072 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002073 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002074 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002075 // +------------------+-----------------+------------------------------------+
2076 // | section | parallel | * |
2077 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002078 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002079 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002080 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002081 // | section | simd | * |
2082 // | section | sections | + |
2083 // | section | section | + |
2084 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002085 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002086 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002087 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002088 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002089 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002090 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002091 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002092 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002093 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002094 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002095 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002096 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002097 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002098 // | section | target parallel | * |
2099 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002100 // | section | target enter | * |
2101 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002102 // | section | target exit | * |
2103 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002104 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002105 // | section | cancellation | |
2106 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002107 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002108 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002109 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002110 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002111 // +------------------+-----------------+------------------------------------+
2112 // | single | parallel | * |
2113 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002114 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002115 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002116 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002117 // | single | simd | * |
2118 // | single | sections | + |
2119 // | single | section | + |
2120 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002121 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002122 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002123 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002124 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002125 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002126 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002127 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002128 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002129 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002130 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002131 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002132 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002133 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002134 // | single | target parallel | * |
2135 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002136 // | single | target enter | * |
2137 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002138 // | single | target exit | * |
2139 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002140 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002141 // | single | cancellation | |
2142 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002143 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002144 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002145 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002146 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002147 // +------------------+-----------------+------------------------------------+
2148 // | parallel for | parallel | * |
2149 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002150 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002151 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002152 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002153 // | parallel for | simd | * |
2154 // | parallel for | sections | + |
2155 // | parallel for | section | + |
2156 // | parallel for | single | + |
2157 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002158 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002159 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002160 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002161 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002162 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002163 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002164 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002165 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002166 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002167 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002168 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002169 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002170 // | parallel for | target parallel | * |
2171 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002172 // | parallel for | target enter | * |
2173 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002174 // | parallel for | target exit | * |
2175 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002176 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002177 // | parallel for | cancellation | |
2178 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002179 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002180 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002181 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002182 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002183 // +------------------+-----------------+------------------------------------+
2184 // | parallel sections| parallel | * |
2185 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002186 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002187 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002188 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002189 // | parallel sections| simd | * |
2190 // | parallel sections| sections | + |
2191 // | parallel sections| section | * |
2192 // | parallel sections| single | + |
2193 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002194 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002195 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002196 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002197 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002198 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002199 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002200 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002201 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002202 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002203 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002204 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002205 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002206 // | parallel sections| target parallel | * |
2207 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002208 // | parallel sections| target enter | * |
2209 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002210 // | parallel sections| target exit | * |
2211 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002212 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002213 // | parallel sections| cancellation | |
2214 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002215 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002216 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002217 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002218 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002219 // +------------------+-----------------+------------------------------------+
2220 // | task | parallel | * |
2221 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002222 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002223 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002224 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002225 // | task | simd | * |
2226 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002227 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002228 // | task | single | + |
2229 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002230 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002231 // | task |parallel sections| * |
2232 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002233 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002234 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002235 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002236 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002237 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002238 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002239 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002240 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002241 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002242 // | task | target parallel | * |
2243 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002244 // | task | target enter | * |
2245 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002246 // | task | target exit | * |
2247 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002248 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002249 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002250 // | | point | ! |
2251 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002252 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002253 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002254 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002255 // +------------------+-----------------+------------------------------------+
2256 // | ordered | parallel | * |
2257 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002258 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002259 // | ordered | master | * |
2260 // | ordered | critical | * |
2261 // | ordered | simd | * |
2262 // | ordered | sections | + |
2263 // | ordered | section | + |
2264 // | ordered | single | + |
2265 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002266 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002267 // | ordered |parallel sections| * |
2268 // | ordered | task | * |
2269 // | ordered | taskyield | * |
2270 // | ordered | barrier | + |
2271 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002272 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002273 // | ordered | flush | * |
2274 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002275 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002276 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002277 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002278 // | ordered | target parallel | * |
2279 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002280 // | ordered | target enter | * |
2281 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002282 // | ordered | target exit | * |
2283 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002284 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002285 // | ordered | cancellation | |
2286 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002287 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002288 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002289 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002290 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002291 // +------------------+-----------------+------------------------------------+
2292 // | atomic | parallel | |
2293 // | atomic | for | |
2294 // | atomic | for simd | |
2295 // | atomic | master | |
2296 // | atomic | critical | |
2297 // | atomic | simd | |
2298 // | atomic | sections | |
2299 // | atomic | section | |
2300 // | atomic | single | |
2301 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002302 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002303 // | atomic |parallel sections| |
2304 // | atomic | task | |
2305 // | atomic | taskyield | |
2306 // | atomic | barrier | |
2307 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002308 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002309 // | atomic | flush | |
2310 // | atomic | ordered | |
2311 // | atomic | atomic | |
2312 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002313 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002314 // | atomic | target parallel | |
2315 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002316 // | atomic | target enter | |
2317 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002318 // | atomic | target exit | |
2319 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002320 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002321 // | atomic | cancellation | |
2322 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002323 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002324 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002325 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002326 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002327 // +------------------+-----------------+------------------------------------+
2328 // | target | parallel | * |
2329 // | target | for | * |
2330 // | target | for simd | * |
2331 // | target | master | * |
2332 // | target | critical | * |
2333 // | target | simd | * |
2334 // | target | sections | * |
2335 // | target | section | * |
2336 // | target | single | * |
2337 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002338 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002339 // | target |parallel sections| * |
2340 // | target | task | * |
2341 // | target | taskyield | * |
2342 // | target | barrier | * |
2343 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002344 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002345 // | target | flush | * |
2346 // | target | ordered | * |
2347 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002348 // | target | target | |
2349 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002350 // | target | target parallel | |
2351 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002352 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002353 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002354 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002355 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002356 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002357 // | target | cancellation | |
2358 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002359 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002360 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002361 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002362 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002363 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002364 // | target parallel | parallel | * |
2365 // | target parallel | for | * |
2366 // | target parallel | for simd | * |
2367 // | target parallel | master | * |
2368 // | target parallel | critical | * |
2369 // | target parallel | simd | * |
2370 // | target parallel | sections | * |
2371 // | target parallel | section | * |
2372 // | target parallel | single | * |
2373 // | target parallel | parallel for | * |
2374 // | target parallel |parallel for simd| * |
2375 // | target parallel |parallel sections| * |
2376 // | target parallel | task | * |
2377 // | target parallel | taskyield | * |
2378 // | target parallel | barrier | * |
2379 // | target parallel | taskwait | * |
2380 // | target parallel | taskgroup | * |
2381 // | target parallel | flush | * |
2382 // | target parallel | ordered | * |
2383 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002384 // | target parallel | target | |
2385 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002386 // | target parallel | target parallel | |
2387 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002388 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002389 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002390 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002391 // | | data | |
2392 // | target parallel | teams | |
2393 // | target parallel | cancellation | |
2394 // | | point | ! |
2395 // | target parallel | cancel | ! |
2396 // | target parallel | taskloop | * |
2397 // | target parallel | taskloop simd | * |
2398 // | target parallel | distribute | |
2399 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002400 // | target parallel | parallel | * |
2401 // | for | | |
2402 // | target parallel | for | * |
2403 // | for | | |
2404 // | target parallel | for simd | * |
2405 // | for | | |
2406 // | target parallel | master | * |
2407 // | for | | |
2408 // | target parallel | critical | * |
2409 // | for | | |
2410 // | target parallel | simd | * |
2411 // | for | | |
2412 // | target parallel | sections | * |
2413 // | for | | |
2414 // | target parallel | section | * |
2415 // | for | | |
2416 // | target parallel | single | * |
2417 // | for | | |
2418 // | target parallel | parallel for | * |
2419 // | for | | |
2420 // | target parallel |parallel for simd| * |
2421 // | for | | |
2422 // | target parallel |parallel sections| * |
2423 // | for | | |
2424 // | target parallel | task | * |
2425 // | for | | |
2426 // | target parallel | taskyield | * |
2427 // | for | | |
2428 // | target parallel | barrier | * |
2429 // | for | | |
2430 // | target parallel | taskwait | * |
2431 // | for | | |
2432 // | target parallel | taskgroup | * |
2433 // | for | | |
2434 // | target parallel | flush | * |
2435 // | for | | |
2436 // | target parallel | ordered | * |
2437 // | for | | |
2438 // | target parallel | atomic | * |
2439 // | for | | |
2440 // | target parallel | target | |
2441 // | for | | |
2442 // | target parallel | target parallel | |
2443 // | for | | |
2444 // | target parallel | target parallel | |
2445 // | for | for | |
2446 // | target parallel | target enter | |
2447 // | for | data | |
2448 // | target parallel | target exit | |
2449 // | for | data | |
2450 // | target parallel | teams | |
2451 // | for | | |
2452 // | target parallel | cancellation | |
2453 // | for | point | ! |
2454 // | target parallel | cancel | ! |
2455 // | for | | |
2456 // | target parallel | taskloop | * |
2457 // | for | | |
2458 // | target parallel | taskloop simd | * |
2459 // | for | | |
2460 // | target parallel | distribute | |
2461 // | for | | |
2462 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002463 // | teams | parallel | * |
2464 // | teams | for | + |
2465 // | teams | for simd | + |
2466 // | teams | master | + |
2467 // | teams | critical | + |
2468 // | teams | simd | + |
2469 // | teams | sections | + |
2470 // | teams | section | + |
2471 // | teams | single | + |
2472 // | teams | parallel for | * |
2473 // | teams |parallel for simd| * |
2474 // | teams |parallel sections| * |
2475 // | teams | task | + |
2476 // | teams | taskyield | + |
2477 // | teams | barrier | + |
2478 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002479 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002480 // | teams | flush | + |
2481 // | teams | ordered | + |
2482 // | teams | atomic | + |
2483 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002484 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002485 // | teams | target parallel | + |
2486 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002487 // | teams | target enter | + |
2488 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002489 // | teams | target exit | + |
2490 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002491 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002492 // | teams | cancellation | |
2493 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002494 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002495 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002496 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002497 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002498 // +------------------+-----------------+------------------------------------+
2499 // | taskloop | parallel | * |
2500 // | taskloop | for | + |
2501 // | taskloop | for simd | + |
2502 // | taskloop | master | + |
2503 // | taskloop | critical | * |
2504 // | taskloop | simd | * |
2505 // | taskloop | sections | + |
2506 // | taskloop | section | + |
2507 // | taskloop | single | + |
2508 // | taskloop | parallel for | * |
2509 // | taskloop |parallel for simd| * |
2510 // | taskloop |parallel sections| * |
2511 // | taskloop | task | * |
2512 // | taskloop | taskyield | * |
2513 // | taskloop | barrier | + |
2514 // | taskloop | taskwait | * |
2515 // | taskloop | taskgroup | * |
2516 // | taskloop | flush | * |
2517 // | taskloop | ordered | + |
2518 // | taskloop | atomic | * |
2519 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002520 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002521 // | taskloop | target parallel | * |
2522 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002523 // | taskloop | target enter | * |
2524 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002525 // | taskloop | target exit | * |
2526 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002527 // | taskloop | teams | + |
2528 // | taskloop | cancellation | |
2529 // | | point | |
2530 // | taskloop | cancel | |
2531 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002532 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002533 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002534 // | taskloop simd | parallel | |
2535 // | taskloop simd | for | |
2536 // | taskloop simd | for simd | |
2537 // | taskloop simd | master | |
2538 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002539 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002540 // | taskloop simd | sections | |
2541 // | taskloop simd | section | |
2542 // | taskloop simd | single | |
2543 // | taskloop simd | parallel for | |
2544 // | taskloop simd |parallel for simd| |
2545 // | taskloop simd |parallel sections| |
2546 // | taskloop simd | task | |
2547 // | taskloop simd | taskyield | |
2548 // | taskloop simd | barrier | |
2549 // | taskloop simd | taskwait | |
2550 // | taskloop simd | taskgroup | |
2551 // | taskloop simd | flush | |
2552 // | taskloop simd | ordered | + (with simd clause) |
2553 // | taskloop simd | atomic | |
2554 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002555 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002556 // | taskloop simd | target parallel | |
2557 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002558 // | taskloop simd | target enter | |
2559 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002560 // | taskloop simd | target exit | |
2561 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002562 // | taskloop simd | teams | |
2563 // | taskloop simd | cancellation | |
2564 // | | point | |
2565 // | taskloop simd | cancel | |
2566 // | taskloop simd | taskloop | |
2567 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002568 // | taskloop simd | distribute | |
2569 // +------------------+-----------------+------------------------------------+
2570 // | distribute | parallel | * |
2571 // | distribute | for | * |
2572 // | distribute | for simd | * |
2573 // | distribute | master | * |
2574 // | distribute | critical | * |
2575 // | distribute | simd | * |
2576 // | distribute | sections | * |
2577 // | distribute | section | * |
2578 // | distribute | single | * |
2579 // | distribute | parallel for | * |
2580 // | distribute |parallel for simd| * |
2581 // | distribute |parallel sections| * |
2582 // | distribute | task | * |
2583 // | distribute | taskyield | * |
2584 // | distribute | barrier | * |
2585 // | distribute | taskwait | * |
2586 // | distribute | taskgroup | * |
2587 // | distribute | flush | * |
2588 // | distribute | ordered | + |
2589 // | distribute | atomic | * |
2590 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002591 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002592 // | distribute | target parallel | |
2593 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002594 // | distribute | target enter | |
2595 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002596 // | distribute | target exit | |
2597 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002598 // | distribute | teams | |
2599 // | distribute | cancellation | + |
2600 // | | point | |
2601 // | distribute | cancel | + |
2602 // | distribute | taskloop | * |
2603 // | distribute | taskloop simd | * |
2604 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002605 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002606 if (Stack->getCurScope()) {
2607 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002608 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002609 bool NestingProhibited = false;
2610 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002611 enum {
2612 NoRecommend,
2613 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002614 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002615 ShouldBeInTargetRegion,
2616 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002617 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002618 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2619 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002620 // OpenMP [2.16, Nesting of Regions]
2621 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002622 // OpenMP [2.8.1,simd Construct, Restrictions]
2623 // An ordered construct with the simd clause is the only OpenMP construct
2624 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002625 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2626 return true;
2627 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002628 if (ParentRegion == OMPD_atomic) {
2629 // OpenMP [2.16, Nesting of Regions]
2630 // OpenMP constructs may not be nested inside an atomic region.
2631 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2632 return true;
2633 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002634 if (CurrentRegion == OMPD_section) {
2635 // OpenMP [2.7.2, sections Construct, Restrictions]
2636 // Orphaned section directives are prohibited. That is, the section
2637 // directives must appear within the sections construct and must not be
2638 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002639 if (ParentRegion != OMPD_sections &&
2640 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002641 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2642 << (ParentRegion != OMPD_unknown)
2643 << getOpenMPDirectiveName(ParentRegion);
2644 return true;
2645 }
2646 return false;
2647 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002648 // Allow some constructs to be orphaned (they could be used in functions,
2649 // called from OpenMP regions with the required preconditions).
2650 if (ParentRegion == OMPD_unknown)
2651 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002652 if (CurrentRegion == OMPD_cancellation_point ||
2653 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002654 // OpenMP [2.16, Nesting of Regions]
2655 // A cancellation point construct for which construct-type-clause is
2656 // taskgroup must be nested inside a task construct. A cancellation
2657 // point construct for which construct-type-clause is not taskgroup must
2658 // be closely nested inside an OpenMP construct that matches the type
2659 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002660 // A cancel construct for which construct-type-clause is taskgroup must be
2661 // nested inside a task construct. A cancel construct for which
2662 // construct-type-clause is not taskgroup must be closely nested inside an
2663 // OpenMP construct that matches the type specified in
2664 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002665 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002666 !((CancelRegion == OMPD_parallel &&
2667 (ParentRegion == OMPD_parallel ||
2668 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002669 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002670 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2671 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002672 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2673 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002674 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2675 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002676 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002677 // OpenMP [2.16, Nesting of Regions]
2678 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002679 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002680 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002681 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002682 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002683 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2684 // OpenMP [2.16, Nesting of Regions]
2685 // A critical region may not be nested (closely or otherwise) inside a
2686 // critical region with the same name. Note that this restriction is not
2687 // sufficient to prevent deadlock.
2688 SourceLocation PreviousCriticalLoc;
2689 bool DeadLock =
2690 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2691 OpenMPDirectiveKind K,
2692 const DeclarationNameInfo &DNI,
2693 SourceLocation Loc)
2694 ->bool {
2695 if (K == OMPD_critical &&
2696 DNI.getName() == CurrentName.getName()) {
2697 PreviousCriticalLoc = Loc;
2698 return true;
2699 } else
2700 return false;
2701 },
2702 false /* skip top directive */);
2703 if (DeadLock) {
2704 SemaRef.Diag(StartLoc,
2705 diag::err_omp_prohibited_region_critical_same_name)
2706 << CurrentName.getName();
2707 if (PreviousCriticalLoc.isValid())
2708 SemaRef.Diag(PreviousCriticalLoc,
2709 diag::note_omp_previous_critical_region);
2710 return true;
2711 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002712 } else if (CurrentRegion == OMPD_barrier) {
2713 // OpenMP [2.16, Nesting of Regions]
2714 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002715 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002716 NestingProhibited =
2717 isOpenMPWorksharingDirective(ParentRegion) ||
2718 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002719 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002720 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002721 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002722 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002723 // OpenMP [2.16, Nesting of Regions]
2724 // A worksharing region may not be closely nested inside a worksharing,
2725 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002726 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002727 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002728 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002729 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002730 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002731 Recommend = ShouldBeInParallelRegion;
2732 } else if (CurrentRegion == OMPD_ordered) {
2733 // OpenMP [2.16, Nesting of Regions]
2734 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002735 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002736 // An ordered region must be closely nested inside a loop region (or
2737 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002738 // OpenMP [2.8.1,simd Construct, Restrictions]
2739 // An ordered construct with the simd clause is the only OpenMP construct
2740 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002741 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002742 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002743 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002744 !(isOpenMPSimdDirective(ParentRegion) ||
2745 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002746 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002747 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2748 // OpenMP [2.16, Nesting of Regions]
2749 // If specified, a teams construct must be contained within a target
2750 // construct.
2751 NestingProhibited = ParentRegion != OMPD_target;
2752 Recommend = ShouldBeInTargetRegion;
2753 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2754 }
2755 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2756 // OpenMP [2.16, Nesting of Regions]
2757 // distribute, parallel, parallel sections, parallel workshare, and the
2758 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2759 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002760 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2761 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002762 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002763 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002764 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2765 // OpenMP 4.5 [2.17 Nesting of Regions]
2766 // The region associated with the distribute construct must be strictly
2767 // nested inside a teams region
2768 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2769 Recommend = ShouldBeInTeamsRegion;
2770 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002771 if (!NestingProhibited &&
2772 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2773 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2774 // OpenMP 4.5 [2.17 Nesting of Regions]
2775 // If a target, target update, target data, target enter data, or
2776 // target exit data construct is encountered during execution of a
2777 // target region, the behavior is unspecified.
2778 NestingProhibited = Stack->hasDirective(
2779 [&OffendingRegion](OpenMPDirectiveKind K,
2780 const DeclarationNameInfo &DNI,
2781 SourceLocation Loc) -> bool {
2782 if (isOpenMPTargetExecutionDirective(K)) {
2783 OffendingRegion = K;
2784 return true;
2785 } else
2786 return false;
2787 },
2788 false /* don't skip top directive */);
2789 CloseNesting = false;
2790 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002791 if (NestingProhibited) {
2792 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002793 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2794 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002795 return true;
2796 }
2797 }
2798 return false;
2799}
2800
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002801static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2802 ArrayRef<OMPClause *> Clauses,
2803 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2804 bool ErrorFound = false;
2805 unsigned NamedModifiersNumber = 0;
2806 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2807 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002808 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002809 for (const auto *C : Clauses) {
2810 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2811 // At most one if clause without a directive-name-modifier can appear on
2812 // the directive.
2813 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2814 if (FoundNameModifiers[CurNM]) {
2815 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2816 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2817 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2818 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002819 } else if (CurNM != OMPD_unknown) {
2820 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002821 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002822 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002823 FoundNameModifiers[CurNM] = IC;
2824 if (CurNM == OMPD_unknown)
2825 continue;
2826 // Check if the specified name modifier is allowed for the current
2827 // directive.
2828 // At most one if clause with the particular directive-name-modifier can
2829 // appear on the directive.
2830 bool MatchFound = false;
2831 for (auto NM : AllowedNameModifiers) {
2832 if (CurNM == NM) {
2833 MatchFound = true;
2834 break;
2835 }
2836 }
2837 if (!MatchFound) {
2838 S.Diag(IC->getNameModifierLoc(),
2839 diag::err_omp_wrong_if_directive_name_modifier)
2840 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2841 ErrorFound = true;
2842 }
2843 }
2844 }
2845 // If any if clause on the directive includes a directive-name-modifier then
2846 // all if clauses on the directive must include a directive-name-modifier.
2847 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2848 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2849 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2850 diag::err_omp_no_more_if_clause);
2851 } else {
2852 std::string Values;
2853 std::string Sep(", ");
2854 unsigned AllowedCnt = 0;
2855 unsigned TotalAllowedNum =
2856 AllowedNameModifiers.size() - NamedModifiersNumber;
2857 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2858 ++Cnt) {
2859 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2860 if (!FoundNameModifiers[NM]) {
2861 Values += "'";
2862 Values += getOpenMPDirectiveName(NM);
2863 Values += "'";
2864 if (AllowedCnt + 2 == TotalAllowedNum)
2865 Values += " or ";
2866 else if (AllowedCnt + 1 != TotalAllowedNum)
2867 Values += Sep;
2868 ++AllowedCnt;
2869 }
2870 }
2871 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2872 diag::err_omp_unnamed_if_clause)
2873 << (TotalAllowedNum > 1) << Values;
2874 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002875 for (auto Loc : NameModifierLoc) {
2876 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2877 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002878 ErrorFound = true;
2879 }
2880 return ErrorFound;
2881}
2882
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002883StmtResult Sema::ActOnOpenMPExecutableDirective(
2884 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2885 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2886 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002887 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002888 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2889 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002890 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002891
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002892 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002893 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002894 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002895 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002896 if (AStmt) {
2897 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2898
2899 // Check default data sharing attributes for referenced variables.
2900 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2901 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2902 if (DSAChecker.isErrorFound())
2903 return StmtError();
2904 // Generate list of implicitly defined firstprivate variables.
2905 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002906
2907 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2908 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2909 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2910 SourceLocation(), SourceLocation())) {
2911 ClausesWithImplicit.push_back(Implicit);
2912 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2913 DSAChecker.getImplicitFirstprivate().size();
2914 } else
2915 ErrorFound = true;
2916 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002917 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002918
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002919 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002920 switch (Kind) {
2921 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002922 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2923 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002924 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002925 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002926 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002927 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2928 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002929 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002930 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002931 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2932 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002933 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002934 case OMPD_for_simd:
2935 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2936 EndLoc, VarsWithInheritedDSA);
2937 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002938 case OMPD_sections:
2939 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2940 EndLoc);
2941 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002942 case OMPD_section:
2943 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002944 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002945 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2946 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002947 case OMPD_single:
2948 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2949 EndLoc);
2950 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002951 case OMPD_master:
2952 assert(ClausesWithImplicit.empty() &&
2953 "No clauses are allowed for 'omp master' directive");
2954 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2955 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002956 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002957 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2958 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002959 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002960 case OMPD_parallel_for:
2961 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2962 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002963 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002964 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002965 case OMPD_parallel_for_simd:
2966 Res = ActOnOpenMPParallelForSimdDirective(
2967 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002968 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002969 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002970 case OMPD_parallel_sections:
2971 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2972 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002973 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002974 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002975 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002976 Res =
2977 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002978 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002979 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002980 case OMPD_taskyield:
2981 assert(ClausesWithImplicit.empty() &&
2982 "No clauses are allowed for 'omp taskyield' directive");
2983 assert(AStmt == nullptr &&
2984 "No associated statement allowed for 'omp taskyield' directive");
2985 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2986 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002987 case OMPD_barrier:
2988 assert(ClausesWithImplicit.empty() &&
2989 "No clauses are allowed for 'omp barrier' directive");
2990 assert(AStmt == nullptr &&
2991 "No associated statement allowed for 'omp barrier' directive");
2992 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2993 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002994 case OMPD_taskwait:
2995 assert(ClausesWithImplicit.empty() &&
2996 "No clauses are allowed for 'omp taskwait' directive");
2997 assert(AStmt == nullptr &&
2998 "No associated statement allowed for 'omp taskwait' directive");
2999 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3000 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003001 case OMPD_taskgroup:
3002 assert(ClausesWithImplicit.empty() &&
3003 "No clauses are allowed for 'omp taskgroup' directive");
3004 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3005 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003006 case OMPD_flush:
3007 assert(AStmt == nullptr &&
3008 "No associated statement allowed for 'omp flush' directive");
3009 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3010 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003011 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003012 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3013 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003014 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003015 case OMPD_atomic:
3016 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3017 EndLoc);
3018 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003019 case OMPD_teams:
3020 Res =
3021 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3022 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003023 case OMPD_target:
3024 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3025 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003026 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003027 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003028 case OMPD_target_parallel:
3029 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3030 StartLoc, EndLoc);
3031 AllowedNameModifiers.push_back(OMPD_target);
3032 AllowedNameModifiers.push_back(OMPD_parallel);
3033 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003034 case OMPD_target_parallel_for:
3035 Res = ActOnOpenMPTargetParallelForDirective(
3036 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3037 AllowedNameModifiers.push_back(OMPD_target);
3038 AllowedNameModifiers.push_back(OMPD_parallel);
3039 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003040 case OMPD_cancellation_point:
3041 assert(ClausesWithImplicit.empty() &&
3042 "No clauses are allowed for 'omp cancellation point' directive");
3043 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3044 "cancellation point' directive");
3045 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3046 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003047 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003048 assert(AStmt == nullptr &&
3049 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003050 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3051 CancelRegion);
3052 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003053 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003054 case OMPD_target_data:
3055 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3056 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003057 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003058 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003059 case OMPD_target_enter_data:
3060 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3061 EndLoc);
3062 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3063 break;
Samuel Antao72590762016-01-19 20:04:50 +00003064 case OMPD_target_exit_data:
3065 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3066 EndLoc);
3067 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3068 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003069 case OMPD_taskloop:
3070 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3071 EndLoc, VarsWithInheritedDSA);
3072 AllowedNameModifiers.push_back(OMPD_taskloop);
3073 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003074 case OMPD_taskloop_simd:
3075 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3076 EndLoc, VarsWithInheritedDSA);
3077 AllowedNameModifiers.push_back(OMPD_taskloop);
3078 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003079 case OMPD_distribute:
3080 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3081 EndLoc, VarsWithInheritedDSA);
3082 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003083 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003084 llvm_unreachable("OpenMP Directive is not allowed");
3085 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003086 llvm_unreachable("Unknown OpenMP directive");
3087 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003088
Alexey Bataev4acb8592014-07-07 13:01:15 +00003089 for (auto P : VarsWithInheritedDSA) {
3090 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3091 << P.first << P.second->getSourceRange();
3092 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003093 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3094
3095 if (!AllowedNameModifiers.empty())
3096 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3097 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003098
Alexey Bataeved09d242014-05-28 05:53:51 +00003099 if (ErrorFound)
3100 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003101 return Res;
3102}
3103
3104StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3105 Stmt *AStmt,
3106 SourceLocation StartLoc,
3107 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003108 if (!AStmt)
3109 return StmtError();
3110
Alexey Bataev9959db52014-05-06 10:08:46 +00003111 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3112 // 1.2.2 OpenMP Language Terminology
3113 // Structured block - An executable statement with a single entry at the
3114 // top and a single exit at the bottom.
3115 // The point of exit cannot be a branch out of the structured block.
3116 // longjmp() and throw() must not violate the entry/exit criteria.
3117 CS->getCapturedDecl()->setNothrow();
3118
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003119 getCurFunction()->setHasBranchProtectedScope();
3120
Alexey Bataev25e5b442015-09-15 12:52:43 +00003121 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3122 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003123}
3124
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003125namespace {
3126/// \brief Helper class for checking canonical form of the OpenMP loops and
3127/// extracting iteration space of each loop in the loop nest, that will be used
3128/// for IR generation.
3129class OpenMPIterationSpaceChecker {
3130 /// \brief Reference to Sema.
3131 Sema &SemaRef;
3132 /// \brief A location for diagnostics (when there is no some better location).
3133 SourceLocation DefaultLoc;
3134 /// \brief A location for diagnostics (when increment is not compatible).
3135 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003136 /// \brief A source location for referring to loop init later.
3137 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003138 /// \brief A source location for referring to condition later.
3139 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003140 /// \brief A source location for referring to increment later.
3141 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003142 /// \brief Loop variable.
3143 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003144 /// \brief Reference to loop variable.
3145 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003146 /// \brief Lower bound (initializer for the var).
3147 Expr *LB;
3148 /// \brief Upper bound.
3149 Expr *UB;
3150 /// \brief Loop step (increment).
3151 Expr *Step;
3152 /// \brief This flag is true when condition is one of:
3153 /// Var < UB
3154 /// Var <= UB
3155 /// UB > Var
3156 /// UB >= Var
3157 bool TestIsLessOp;
3158 /// \brief This flag is true when condition is strict ( < or > ).
3159 bool TestIsStrictOp;
3160 /// \brief This flag is true when step is subtracted on each iteration.
3161 bool SubtractStep;
3162
3163public:
3164 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3165 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003166 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3167 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003168 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3169 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003170 /// \brief Check init-expr for canonical loop form and save loop counter
3171 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003172 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003173 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3174 /// for less/greater and for strict/non-strict comparison.
3175 bool CheckCond(Expr *S);
3176 /// \brief Check incr-expr for canonical loop form and return true if it
3177 /// does not conform, otherwise save loop step (#Step).
3178 bool CheckInc(Expr *S);
3179 /// \brief Return the loop counter variable.
3180 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003181 /// \brief Return the reference expression to loop counter variable.
3182 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003183 /// \brief Source range of the loop init.
3184 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3185 /// \brief Source range of the loop condition.
3186 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3187 /// \brief Source range of the loop increment.
3188 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3189 /// \brief True if the step should be subtracted.
3190 bool ShouldSubtractStep() const { return SubtractStep; }
3191 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003192 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003193 /// \brief Build the precondition expression for the loops.
3194 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003195 /// \brief Build reference expression to the counter be used for codegen.
3196 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003197 /// \brief Build reference expression to the private counter be used for
3198 /// codegen.
3199 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003200 /// \brief Build initization of the counter be used for codegen.
3201 Expr *BuildCounterInit() const;
3202 /// \brief Build step of the counter be used for codegen.
3203 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003204 /// \brief Return true if any expression is dependent.
3205 bool Dependent() const;
3206
3207private:
3208 /// \brief Check the right-hand side of an assignment in the increment
3209 /// expression.
3210 bool CheckIncRHS(Expr *RHS);
3211 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003212 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003213 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003214 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003215 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003216 /// \brief Helper to set loop increment.
3217 bool SetStep(Expr *NewStep, bool Subtract);
3218};
3219
3220bool OpenMPIterationSpaceChecker::Dependent() const {
3221 if (!Var) {
3222 assert(!LB && !UB && !Step);
3223 return false;
3224 }
3225 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3226 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3227}
3228
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003229template <typename T>
3230static T *getExprAsWritten(T *E) {
3231 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3232 E = ExprTemp->getSubExpr();
3233
3234 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3235 E = MTE->GetTemporaryExpr();
3236
3237 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3238 E = Binder->getSubExpr();
3239
3240 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3241 E = ICE->getSubExprAsWritten();
3242 return E->IgnoreParens();
3243}
3244
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003245bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3246 DeclRefExpr *NewVarRefExpr,
3247 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003248 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003249 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3250 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003251 if (!NewVar || !NewLB)
3252 return true;
3253 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003254 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003255 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3256 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003257 if ((Ctor->isCopyOrMoveConstructor() ||
3258 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3259 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003260 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003261 LB = NewLB;
3262 return false;
3263}
3264
3265bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003266 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003267 // State consistency checking to ensure correct usage.
3268 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3269 !TestIsLessOp && !TestIsStrictOp);
3270 if (!NewUB)
3271 return true;
3272 UB = NewUB;
3273 TestIsLessOp = LessOp;
3274 TestIsStrictOp = StrictOp;
3275 ConditionSrcRange = SR;
3276 ConditionLoc = SL;
3277 return false;
3278}
3279
3280bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3281 // State consistency checking to ensure correct usage.
3282 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3283 if (!NewStep)
3284 return true;
3285 if (!NewStep->isValueDependent()) {
3286 // Check that the step is integer expression.
3287 SourceLocation StepLoc = NewStep->getLocStart();
3288 ExprResult Val =
3289 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3290 if (Val.isInvalid())
3291 return true;
3292 NewStep = Val.get();
3293
3294 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3295 // If test-expr is of form var relational-op b and relational-op is < or
3296 // <= then incr-expr must cause var to increase on each iteration of the
3297 // loop. If test-expr is of form var relational-op b and relational-op is
3298 // > or >= then incr-expr must cause var to decrease on each iteration of
3299 // the loop.
3300 // If test-expr is of form b relational-op var and relational-op is < or
3301 // <= then incr-expr must cause var to decrease on each iteration of the
3302 // loop. If test-expr is of form b relational-op var and relational-op is
3303 // > or >= then incr-expr must cause var to increase on each iteration of
3304 // the loop.
3305 llvm::APSInt Result;
3306 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3307 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3308 bool IsConstNeg =
3309 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003310 bool IsConstPos =
3311 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003312 bool IsConstZero = IsConstant && !Result.getBoolValue();
3313 if (UB && (IsConstZero ||
3314 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003315 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003316 SemaRef.Diag(NewStep->getExprLoc(),
3317 diag::err_omp_loop_incr_not_compatible)
3318 << Var << TestIsLessOp << NewStep->getSourceRange();
3319 SemaRef.Diag(ConditionLoc,
3320 diag::note_omp_loop_cond_requres_compatible_incr)
3321 << TestIsLessOp << ConditionSrcRange;
3322 return true;
3323 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003324 if (TestIsLessOp == Subtract) {
3325 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3326 NewStep).get();
3327 Subtract = !Subtract;
3328 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003329 }
3330
3331 Step = NewStep;
3332 SubtractStep = Subtract;
3333 return false;
3334}
3335
Alexey Bataev9c821032015-04-30 04:23:23 +00003336bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003337 // Check init-expr for canonical loop form and save loop counter
3338 // variable - #Var and its initialization value - #LB.
3339 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3340 // var = lb
3341 // integer-type var = lb
3342 // random-access-iterator-type var = lb
3343 // pointer-type var = lb
3344 //
3345 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003346 if (EmitDiags) {
3347 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3348 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003349 return true;
3350 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003351 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003352 if (Expr *E = dyn_cast<Expr>(S))
3353 S = E->IgnoreParens();
3354 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3355 if (BO->getOpcode() == BO_Assign)
3356 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003357 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003358 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003359 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3360 if (DS->isSingleDecl()) {
3361 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003362 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003363 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003364 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003365 SemaRef.Diag(S->getLocStart(),
3366 diag::ext_omp_loop_not_canonical_init)
3367 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003368 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003369 }
3370 }
3371 }
3372 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3373 if (CE->getOperator() == OO_Equal)
3374 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003375 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3376 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003377
Alexey Bataev9c821032015-04-30 04:23:23 +00003378 if (EmitDiags) {
3379 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3380 << S->getSourceRange();
3381 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003382 return true;
3383}
3384
Alexey Bataev23b69422014-06-18 07:08:49 +00003385/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003386/// variable (which may be the loop variable) if possible.
3387static const VarDecl *GetInitVarDecl(const Expr *E) {
3388 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003389 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003390 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003391 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3392 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003393 if ((Ctor->isCopyOrMoveConstructor() ||
3394 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3395 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003396 E = CE->getArg(0)->IgnoreParenImpCasts();
3397 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3398 if (!DRE)
3399 return nullptr;
3400 return dyn_cast<VarDecl>(DRE->getDecl());
3401}
3402
3403bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3404 // Check test-expr for canonical form, save upper-bound UB, flags for
3405 // less/greater and for strict/non-strict comparison.
3406 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3407 // var relational-op b
3408 // b relational-op var
3409 //
3410 if (!S) {
3411 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3412 return true;
3413 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003414 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003415 SourceLocation CondLoc = S->getLocStart();
3416 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3417 if (BO->isRelationalOp()) {
3418 if (GetInitVarDecl(BO->getLHS()) == Var)
3419 return SetUB(BO->getRHS(),
3420 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3421 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3422 BO->getSourceRange(), BO->getOperatorLoc());
3423 if (GetInitVarDecl(BO->getRHS()) == Var)
3424 return SetUB(BO->getLHS(),
3425 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3426 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3427 BO->getSourceRange(), BO->getOperatorLoc());
3428 }
3429 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3430 if (CE->getNumArgs() == 2) {
3431 auto Op = CE->getOperator();
3432 switch (Op) {
3433 case OO_Greater:
3434 case OO_GreaterEqual:
3435 case OO_Less:
3436 case OO_LessEqual:
3437 if (GetInitVarDecl(CE->getArg(0)) == Var)
3438 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3439 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3440 CE->getOperatorLoc());
3441 if (GetInitVarDecl(CE->getArg(1)) == Var)
3442 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3443 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3444 CE->getOperatorLoc());
3445 break;
3446 default:
3447 break;
3448 }
3449 }
3450 }
3451 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3452 << S->getSourceRange() << Var;
3453 return true;
3454}
3455
3456bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3457 // RHS of canonical loop form increment can be:
3458 // var + incr
3459 // incr + var
3460 // var - incr
3461 //
3462 RHS = RHS->IgnoreParenImpCasts();
3463 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3464 if (BO->isAdditiveOp()) {
3465 bool IsAdd = BO->getOpcode() == BO_Add;
3466 if (GetInitVarDecl(BO->getLHS()) == Var)
3467 return SetStep(BO->getRHS(), !IsAdd);
3468 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3469 return SetStep(BO->getLHS(), false);
3470 }
3471 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3472 bool IsAdd = CE->getOperator() == OO_Plus;
3473 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3474 if (GetInitVarDecl(CE->getArg(0)) == Var)
3475 return SetStep(CE->getArg(1), !IsAdd);
3476 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3477 return SetStep(CE->getArg(0), false);
3478 }
3479 }
3480 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3481 << RHS->getSourceRange() << Var;
3482 return true;
3483}
3484
3485bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3486 // Check incr-expr for canonical loop form and return true if it
3487 // does not conform.
3488 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3489 // ++var
3490 // var++
3491 // --var
3492 // var--
3493 // var += incr
3494 // var -= incr
3495 // var = var + incr
3496 // var = incr + var
3497 // var = var - incr
3498 //
3499 if (!S) {
3500 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3501 return true;
3502 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003503 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003504 S = S->IgnoreParens();
3505 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3506 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3507 return SetStep(
3508 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3509 (UO->isDecrementOp() ? -1 : 1)).get(),
3510 false);
3511 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3512 switch (BO->getOpcode()) {
3513 case BO_AddAssign:
3514 case BO_SubAssign:
3515 if (GetInitVarDecl(BO->getLHS()) == Var)
3516 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3517 break;
3518 case BO_Assign:
3519 if (GetInitVarDecl(BO->getLHS()) == Var)
3520 return CheckIncRHS(BO->getRHS());
3521 break;
3522 default:
3523 break;
3524 }
3525 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3526 switch (CE->getOperator()) {
3527 case OO_PlusPlus:
3528 case OO_MinusMinus:
3529 if (GetInitVarDecl(CE->getArg(0)) == Var)
3530 return SetStep(
3531 SemaRef.ActOnIntegerConstant(
3532 CE->getLocStart(),
3533 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3534 false);
3535 break;
3536 case OO_PlusEqual:
3537 case OO_MinusEqual:
3538 if (GetInitVarDecl(CE->getArg(0)) == Var)
3539 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3540 break;
3541 case OO_Equal:
3542 if (GetInitVarDecl(CE->getArg(0)) == Var)
3543 return CheckIncRHS(CE->getArg(1));
3544 break;
3545 default:
3546 break;
3547 }
3548 }
3549 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3550 << S->getSourceRange() << Var;
3551 return true;
3552}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003553
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003554namespace {
3555// Transform variables declared in GNU statement expressions to new ones to
3556// avoid crash on codegen.
3557class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3558 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3559
3560public:
3561 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3562
3563 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3564 if (auto *VD = cast<VarDecl>(D))
3565 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3566 !isa<ImplicitParamDecl>(D)) {
3567 auto *NewVD = VarDecl::Create(
3568 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3569 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3570 VD->getTypeSourceInfo(), VD->getStorageClass());
3571 NewVD->setTSCSpec(VD->getTSCSpec());
3572 NewVD->setInit(VD->getInit());
3573 NewVD->setInitStyle(VD->getInitStyle());
3574 NewVD->setExceptionVariable(VD->isExceptionVariable());
3575 NewVD->setNRVOVariable(VD->isNRVOVariable());
3576 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3577 NewVD->setConstexpr(VD->isConstexpr());
3578 NewVD->setInitCapture(VD->isInitCapture());
3579 NewVD->setPreviousDeclInSameBlockScope(
3580 VD->isPreviousDeclInSameBlockScope());
3581 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003582 if (VD->hasAttrs())
3583 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003584 transformedLocalDecl(VD, NewVD);
3585 return NewVD;
3586 }
3587 return BaseTransform::TransformDefinition(Loc, D);
3588 }
3589
3590 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3591 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3592 if (E->getDecl() != NewD) {
3593 NewD->setReferenced();
3594 NewD->markUsed(SemaRef.Context);
3595 return DeclRefExpr::Create(
3596 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3597 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3598 E->getNameInfo(), E->getType(), E->getValueKind());
3599 }
3600 return BaseTransform::TransformDeclRefExpr(E);
3601 }
3602};
3603}
3604
Alexander Musmana5f070a2014-10-01 06:03:56 +00003605/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003606Expr *
3607OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3608 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003609 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003610 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003611 auto VarType = Var->getType().getNonReferenceType();
3612 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003613 SemaRef.getLangOpts().CPlusPlus) {
3614 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003615 auto *UBExpr = TestIsLessOp ? UB : LB;
3616 auto *LBExpr = TestIsLessOp ? LB : UB;
3617 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3618 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3619 if (!Upper || !Lower)
3620 return nullptr;
3621 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3622 Sema::AA_Converting,
3623 /*AllowExplicit=*/true)
3624 .get();
3625 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3626 Sema::AA_Converting,
3627 /*AllowExplicit=*/true)
3628 .get();
3629 if (!Upper || !Lower)
3630 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003631
3632 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3633
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003634 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003635 // BuildBinOp already emitted error, this one is to point user to upper
3636 // and lower bound, and to tell what is passed to 'operator-'.
3637 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3638 << Upper->getSourceRange() << Lower->getSourceRange();
3639 return nullptr;
3640 }
3641 }
3642
3643 if (!Diff.isUsable())
3644 return nullptr;
3645
3646 // Upper - Lower [- 1]
3647 if (TestIsStrictOp)
3648 Diff = SemaRef.BuildBinOp(
3649 S, DefaultLoc, BO_Sub, Diff.get(),
3650 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3651 if (!Diff.isUsable())
3652 return nullptr;
3653
3654 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003655 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3656 if (NewStep.isInvalid())
3657 return nullptr;
3658 NewStep = SemaRef.PerformImplicitConversion(
3659 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3660 /*AllowExplicit=*/true);
3661 if (NewStep.isInvalid())
3662 return nullptr;
3663 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003664 if (!Diff.isUsable())
3665 return nullptr;
3666
3667 // Parentheses (for dumping/debugging purposes only).
3668 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3669 if (!Diff.isUsable())
3670 return nullptr;
3671
3672 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003673 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3674 if (NewStep.isInvalid())
3675 return nullptr;
3676 NewStep = SemaRef.PerformImplicitConversion(
3677 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3678 /*AllowExplicit=*/true);
3679 if (NewStep.isInvalid())
3680 return nullptr;
3681 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003682 if (!Diff.isUsable())
3683 return nullptr;
3684
Alexander Musman174b3ca2014-10-06 11:16:29 +00003685 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003686 QualType Type = Diff.get()->getType();
3687 auto &C = SemaRef.Context;
3688 bool UseVarType = VarType->hasIntegerRepresentation() &&
3689 C.getTypeSize(Type) > C.getTypeSize(VarType);
3690 if (!Type->isIntegerType() || UseVarType) {
3691 unsigned NewSize =
3692 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3693 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3694 : Type->hasSignedIntegerRepresentation();
3695 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3696 Diff = SemaRef.PerformImplicitConversion(
3697 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3698 if (!Diff.isUsable())
3699 return nullptr;
3700 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003701 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003702 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3703 if (NewSize != C.getTypeSize(Type)) {
3704 if (NewSize < C.getTypeSize(Type)) {
3705 assert(NewSize == 64 && "incorrect loop var size");
3706 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3707 << InitSrcRange << ConditionSrcRange;
3708 }
3709 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003710 NewSize, Type->hasSignedIntegerRepresentation() ||
3711 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003712 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3713 Sema::AA_Converting, true);
3714 if (!Diff.isUsable())
3715 return nullptr;
3716 }
3717 }
3718
Alexander Musmana5f070a2014-10-01 06:03:56 +00003719 return Diff.get();
3720}
3721
Alexey Bataev62dbb972015-04-22 11:59:37 +00003722Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3723 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3724 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3725 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003726 TransformToNewDefs Transform(SemaRef);
3727
3728 auto NewLB = Transform.TransformExpr(LB);
3729 auto NewUB = Transform.TransformExpr(UB);
3730 if (NewLB.isInvalid() || NewUB.isInvalid())
3731 return Cond;
3732 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3733 Sema::AA_Converting,
3734 /*AllowExplicit=*/true);
3735 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3736 Sema::AA_Converting,
3737 /*AllowExplicit=*/true);
3738 if (NewLB.isInvalid() || NewUB.isInvalid())
3739 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003740 auto CondExpr = SemaRef.BuildBinOp(
3741 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3742 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003743 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003744 if (CondExpr.isUsable()) {
3745 CondExpr = SemaRef.PerformImplicitConversion(
3746 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3747 /*AllowExplicit=*/true);
3748 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003749 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3750 // Otherwise use original loop conditon and evaluate it in runtime.
3751 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3752}
3753
Alexander Musmana5f070a2014-10-01 06:03:56 +00003754/// \brief Build reference expression to the counter be used for codegen.
3755Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003756 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3757 DefaultLoc);
3758}
3759
3760Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3761 if (Var && !Var->isInvalidDecl()) {
3762 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003763 auto *PrivateVar =
3764 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3765 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003766 if (PrivateVar->isInvalidDecl())
3767 return nullptr;
3768 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3769 }
3770 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003771}
3772
3773/// \brief Build initization of the counter be used for codegen.
3774Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3775
3776/// \brief Build step of the counter be used for codegen.
3777Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3778
3779/// \brief Iteration space of a single for loop.
3780struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003781 /// \brief Condition of the loop.
3782 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003783 /// \brief This expression calculates the number of iterations in the loop.
3784 /// It is always possible to calculate it before starting the loop.
3785 Expr *NumIterations;
3786 /// \brief The loop counter variable.
3787 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003788 /// \brief Private loop counter variable.
3789 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003790 /// \brief This is initializer for the initial value of #CounterVar.
3791 Expr *CounterInit;
3792 /// \brief This is step for the #CounterVar used to generate its update:
3793 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3794 Expr *CounterStep;
3795 /// \brief Should step be subtracted?
3796 bool Subtract;
3797 /// \brief Source range of the loop init.
3798 SourceRange InitSrcRange;
3799 /// \brief Source range of the loop condition.
3800 SourceRange CondSrcRange;
3801 /// \brief Source range of the loop increment.
3802 SourceRange IncSrcRange;
3803};
3804
Alexey Bataev23b69422014-06-18 07:08:49 +00003805} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003806
Alexey Bataev9c821032015-04-30 04:23:23 +00003807void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3808 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3809 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003810 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3811 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003812 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3813 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003814 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003815 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003816 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003817 }
3818}
3819
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003820/// \brief Called on a for stmt to check and extract its iteration space
3821/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003822static bool CheckOpenMPIterationSpace(
3823 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3824 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003825 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003826 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003827 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003828 // OpenMP [2.6, Canonical Loop Form]
3829 // for (init-expr; test-expr; incr-expr) structured-block
3830 auto For = dyn_cast_or_null<ForStmt>(S);
3831 if (!For) {
3832 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003833 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3834 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3835 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3836 if (NestedLoopCount > 1) {
3837 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3838 SemaRef.Diag(DSA.getConstructLoc(),
3839 diag::note_omp_collapse_ordered_expr)
3840 << 2 << CollapseLoopCountExpr->getSourceRange()
3841 << OrderedLoopCountExpr->getSourceRange();
3842 else if (CollapseLoopCountExpr)
3843 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3844 diag::note_omp_collapse_ordered_expr)
3845 << 0 << CollapseLoopCountExpr->getSourceRange();
3846 else
3847 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3848 diag::note_omp_collapse_ordered_expr)
3849 << 1 << OrderedLoopCountExpr->getSourceRange();
3850 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003851 return true;
3852 }
3853 assert(For->getBody());
3854
3855 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3856
3857 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003858 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003859 if (ISC.CheckInit(Init)) {
3860 return true;
3861 }
3862
3863 bool HasErrors = false;
3864
3865 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003866 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003867
3868 // OpenMP [2.6, Canonical Loop Form]
3869 // Var is one of the following:
3870 // A variable of signed or unsigned integer type.
3871 // For C++, a variable of a random access iterator type.
3872 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003873 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003874 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3875 !VarType->isPointerType() &&
3876 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3877 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3878 << SemaRef.getLangOpts().CPlusPlus;
3879 HasErrors = true;
3880 }
3881
Alexey Bataev4acb8592014-07-07 13:01:15 +00003882 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3883 // Construct
3884 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3885 // parallel for construct is (are) private.
3886 // The loop iteration variable in the associated for-loop of a simd construct
3887 // with just one associated for-loop is linear with a constant-linear-step
3888 // that is the increment of the associated for-loop.
3889 // Exclude loop var from the list of variables with implicitly defined data
3890 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003891 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003892
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003893 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3894 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003895 // The loop iteration variable in the associated for-loop of a simd construct
3896 // with just one associated for-loop may be listed in a linear clause with a
3897 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003898 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3899 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003900 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003901 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3902 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3903 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003904 auto PredeterminedCKind =
3905 isOpenMPSimdDirective(DKind)
3906 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3907 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003908 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003909 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003910 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003911 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003912 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003913 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3914 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003915 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003916 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3917 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003918 if (DVar.RefExpr == nullptr)
3919 DVar.CKind = PredeterminedCKind;
3920 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003921 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003922 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003923 // Make the loop iteration variable private (for worksharing constructs),
3924 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003925 // lastprivate (for simd directives with several collapsed or ordered
3926 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003927 if (DVar.CKind == OMPC_unknown)
3928 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3929 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003930 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003931 }
3932
Alexey Bataev7ff55242014-06-19 09:13:45 +00003933 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003934
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003935 // Check test-expr.
3936 HasErrors |= ISC.CheckCond(For->getCond());
3937
3938 // Check incr-expr.
3939 HasErrors |= ISC.CheckInc(For->getInc());
3940
Alexander Musmana5f070a2014-10-01 06:03:56 +00003941 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942 return HasErrors;
3943
Alexander Musmana5f070a2014-10-01 06:03:56 +00003944 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003945 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003946 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003947 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003948 isOpenMPTaskLoopDirective(DKind) ||
3949 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003950 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003951 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003952 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3953 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3954 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3955 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3956 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3957 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3958
Alexey Bataev62dbb972015-04-22 11:59:37 +00003959 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3960 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003961 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003962 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003963 ResultIterSpace.CounterInit == nullptr ||
3964 ResultIterSpace.CounterStep == nullptr);
3965
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003966 return HasErrors;
3967}
3968
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003969/// \brief Build 'VarRef = Start.
3970static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3971 ExprResult VarRef, ExprResult Start) {
3972 TransformToNewDefs Transform(SemaRef);
3973 // Build 'VarRef = Start.
3974 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3975 if (NewStart.isInvalid())
3976 return ExprError();
3977 NewStart = SemaRef.PerformImplicitConversion(
3978 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3979 Sema::AA_Converting,
3980 /*AllowExplicit=*/true);
3981 if (NewStart.isInvalid())
3982 return ExprError();
3983 NewStart = SemaRef.PerformImplicitConversion(
3984 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3985 /*AllowExplicit=*/true);
3986 if (!NewStart.isUsable())
3987 return ExprError();
3988
3989 auto Init =
3990 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3991 return Init;
3992}
3993
Alexander Musmana5f070a2014-10-01 06:03:56 +00003994/// \brief Build 'VarRef = Start + Iter * Step'.
3995static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3996 SourceLocation Loc, ExprResult VarRef,
3997 ExprResult Start, ExprResult Iter,
3998 ExprResult Step, bool Subtract) {
3999 // Add parentheses (for debugging purposes only).
4000 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4001 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4002 !Step.isUsable())
4003 return ExprError();
4004
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004005 TransformToNewDefs Transform(SemaRef);
4006 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
4007 if (NewStep.isInvalid())
4008 return ExprError();
4009 NewStep = SemaRef.PerformImplicitConversion(
4010 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
4011 Sema::AA_Converting,
4012 /*AllowExplicit=*/true);
4013 if (NewStep.isInvalid())
4014 return ExprError();
4015 ExprResult Update =
4016 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004017 if (!Update.isUsable())
4018 return ExprError();
4019
4020 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004021 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4022 if (NewStart.isInvalid())
4023 return ExprError();
4024 NewStart = SemaRef.PerformImplicitConversion(
4025 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4026 Sema::AA_Converting,
4027 /*AllowExplicit=*/true);
4028 if (NewStart.isInvalid())
4029 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004030 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004031 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004032 if (!Update.isUsable())
4033 return ExprError();
4034
4035 Update = SemaRef.PerformImplicitConversion(
4036 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4037 if (!Update.isUsable())
4038 return ExprError();
4039
4040 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4041 return Update;
4042}
4043
4044/// \brief Convert integer expression \a E to make it have at least \a Bits
4045/// bits.
4046static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4047 Sema &SemaRef) {
4048 if (E == nullptr)
4049 return ExprError();
4050 auto &C = SemaRef.Context;
4051 QualType OldType = E->getType();
4052 unsigned HasBits = C.getTypeSize(OldType);
4053 if (HasBits >= Bits)
4054 return ExprResult(E);
4055 // OK to convert to signed, because new type has more bits than old.
4056 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4057 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4058 true);
4059}
4060
4061/// \brief Check if the given expression \a E is a constant integer that fits
4062/// into \a Bits bits.
4063static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4064 if (E == nullptr)
4065 return false;
4066 llvm::APSInt Result;
4067 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4068 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4069 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004070}
4071
4072/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004073/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4074/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004075static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004076CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4077 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4078 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004079 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004080 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004081 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004082 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004083 // Found 'collapse' clause - calculate collapse number.
4084 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004085 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004086 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004087 }
4088 if (OrderedLoopCountExpr) {
4089 // Found 'ordered' clause - calculate collapse number.
4090 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004091 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4092 if (Result.getLimitedValue() < NestedLoopCount) {
4093 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4094 diag::err_omp_wrong_ordered_loop_count)
4095 << OrderedLoopCountExpr->getSourceRange();
4096 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4097 diag::note_collapse_loop_count)
4098 << CollapseLoopCountExpr->getSourceRange();
4099 }
4100 NestedLoopCount = Result.getLimitedValue();
4101 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004102 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004103 // This is helper routine for loop directives (e.g., 'for', 'simd',
4104 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004105 SmallVector<LoopIterationSpace, 4> IterSpaces;
4106 IterSpaces.resize(NestedLoopCount);
4107 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004108 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004109 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004110 NestedLoopCount, CollapseLoopCountExpr,
4111 OrderedLoopCountExpr, VarsWithImplicitDSA,
4112 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004113 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004114 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004115 // OpenMP [2.8.1, simd construct, Restrictions]
4116 // All loops associated with the construct must be perfectly nested; that
4117 // is, there must be no intervening code nor any OpenMP directive between
4118 // any two loops.
4119 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004120 }
4121
Alexander Musmana5f070a2014-10-01 06:03:56 +00004122 Built.clear(/* size */ NestedLoopCount);
4123
4124 if (SemaRef.CurContext->isDependentContext())
4125 return NestedLoopCount;
4126
4127 // An example of what is generated for the following code:
4128 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004129 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004130 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004131 // for (k = 0; k < NK; ++k)
4132 // for (j = J0; j < NJ; j+=2) {
4133 // <loop body>
4134 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004135 //
4136 // We generate the code below.
4137 // Note: the loop body may be outlined in CodeGen.
4138 // Note: some counters may be C++ classes, operator- is used to find number of
4139 // iterations and operator+= to calculate counter value.
4140 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4141 // or i64 is currently supported).
4142 //
4143 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4144 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4145 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4146 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4147 // // similar updates for vars in clauses (e.g. 'linear')
4148 // <loop body (using local i and j)>
4149 // }
4150 // i = NI; // assign final values of counters
4151 // j = NJ;
4152 //
4153
4154 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4155 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004156 // Precondition tests if there is at least one iteration (all conditions are
4157 // true).
4158 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004159 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004160 ExprResult LastIteration32 = WidenIterationCount(
4161 32 /* Bits */, SemaRef.PerformImplicitConversion(
4162 N0->IgnoreImpCasts(), N0->getType(),
4163 Sema::AA_Converting, /*AllowExplicit=*/true)
4164 .get(),
4165 SemaRef);
4166 ExprResult LastIteration64 = WidenIterationCount(
4167 64 /* Bits */, SemaRef.PerformImplicitConversion(
4168 N0->IgnoreImpCasts(), N0->getType(),
4169 Sema::AA_Converting, /*AllowExplicit=*/true)
4170 .get(),
4171 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004172
4173 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4174 return NestedLoopCount;
4175
4176 auto &C = SemaRef.Context;
4177 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4178
4179 Scope *CurScope = DSA.getCurScope();
4180 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004181 if (PreCond.isUsable()) {
4182 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4183 PreCond.get(), IterSpaces[Cnt].PreCond);
4184 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004185 auto N = IterSpaces[Cnt].NumIterations;
4186 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4187 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004188 LastIteration32 = SemaRef.BuildBinOp(
4189 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4190 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4191 Sema::AA_Converting,
4192 /*AllowExplicit=*/true)
4193 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004194 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004195 LastIteration64 = SemaRef.BuildBinOp(
4196 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4197 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4198 Sema::AA_Converting,
4199 /*AllowExplicit=*/true)
4200 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004201 }
4202
4203 // Choose either the 32-bit or 64-bit version.
4204 ExprResult LastIteration = LastIteration64;
4205 if (LastIteration32.isUsable() &&
4206 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4207 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4208 FitsInto(
4209 32 /* Bits */,
4210 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4211 LastIteration64.get(), SemaRef)))
4212 LastIteration = LastIteration32;
4213
4214 if (!LastIteration.isUsable())
4215 return 0;
4216
4217 // Save the number of iterations.
4218 ExprResult NumIterations = LastIteration;
4219 {
4220 LastIteration = SemaRef.BuildBinOp(
4221 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4222 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4223 if (!LastIteration.isUsable())
4224 return 0;
4225 }
4226
4227 // Calculate the last iteration number beforehand instead of doing this on
4228 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4229 llvm::APSInt Result;
4230 bool IsConstant =
4231 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4232 ExprResult CalcLastIteration;
4233 if (!IsConstant) {
4234 SourceLocation SaveLoc;
4235 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004236 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004237 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004238 ExprResult SaveRef = buildDeclRefExpr(
4239 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004240 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4241 SaveRef.get(), LastIteration.get());
4242 LastIteration = SaveRef;
4243
4244 // Prepare SaveRef + 1.
4245 NumIterations = SemaRef.BuildBinOp(
4246 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4247 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4248 if (!NumIterations.isUsable())
4249 return 0;
4250 }
4251
4252 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4253
Alexander Musmanc6388682014-12-15 07:07:06 +00004254 QualType VType = LastIteration.get()->getType();
4255 // Build variables passed into runtime, nesessary for worksharing directives.
4256 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004257 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4258 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004259 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004260 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4261 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004262 SemaRef.AddInitializerToDecl(
4263 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4264 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4265
4266 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004267 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4268 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004269 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4270 /*DirectInit*/ false,
4271 /*TypeMayContainAuto*/ false);
4272
4273 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4274 // This will be used to implement clause 'lastprivate'.
4275 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004276 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4277 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004278 SemaRef.AddInitializerToDecl(
4279 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4280 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4281
4282 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004283 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4284 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004285 SemaRef.AddInitializerToDecl(
4286 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4287 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4288
4289 // Build expression: UB = min(UB, LastIteration)
4290 // It is nesessary for CodeGen of directives with static scheduling.
4291 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4292 UB.get(), LastIteration.get());
4293 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4294 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4295 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4296 CondOp.get());
4297 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4298 }
4299
4300 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004301 ExprResult IV;
4302 ExprResult Init;
4303 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004304 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4305 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004306 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004307 isOpenMPTaskLoopDirective(DKind) ||
4308 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004309 ? LB.get()
4310 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4311 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4312 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004313 }
4314
Alexander Musmanc6388682014-12-15 07:07:06 +00004315 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004316 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004317 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004318 (isOpenMPWorksharingDirective(DKind) ||
4319 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004320 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4321 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4322 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004323
4324 // Loop increment (IV = IV + 1)
4325 SourceLocation IncLoc;
4326 ExprResult Inc =
4327 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4328 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4329 if (!Inc.isUsable())
4330 return 0;
4331 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004332 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4333 if (!Inc.isUsable())
4334 return 0;
4335
4336 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4337 // Used for directives with static scheduling.
4338 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004339 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4340 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004341 // LB + ST
4342 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4343 if (!NextLB.isUsable())
4344 return 0;
4345 // LB = LB + ST
4346 NextLB =
4347 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4348 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4349 if (!NextLB.isUsable())
4350 return 0;
4351 // UB + ST
4352 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4353 if (!NextUB.isUsable())
4354 return 0;
4355 // UB = UB + ST
4356 NextUB =
4357 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4358 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4359 if (!NextUB.isUsable())
4360 return 0;
4361 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004362
4363 // Build updates and final values of the loop counters.
4364 bool HasErrors = false;
4365 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004366 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004367 Built.Updates.resize(NestedLoopCount);
4368 Built.Finals.resize(NestedLoopCount);
4369 {
4370 ExprResult Div;
4371 // Go from inner nested loop to outer.
4372 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4373 LoopIterationSpace &IS = IterSpaces[Cnt];
4374 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4375 // Build: Iter = (IV / Div) % IS.NumIters
4376 // where Div is product of previous iterations' IS.NumIters.
4377 ExprResult Iter;
4378 if (Div.isUsable()) {
4379 Iter =
4380 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4381 } else {
4382 Iter = IV;
4383 assert((Cnt == (int)NestedLoopCount - 1) &&
4384 "unusable div expected on first iteration only");
4385 }
4386
4387 if (Cnt != 0 && Iter.isUsable())
4388 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4389 IS.NumIterations);
4390 if (!Iter.isUsable()) {
4391 HasErrors = true;
4392 break;
4393 }
4394
Alexey Bataev39f915b82015-05-08 10:41:21 +00004395 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4396 auto *CounterVar = buildDeclRefExpr(
4397 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4398 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4399 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004400 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4401 IS.CounterInit);
4402 if (!Init.isUsable()) {
4403 HasErrors = true;
4404 break;
4405 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004406 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004407 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004408 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4409 if (!Update.isUsable()) {
4410 HasErrors = true;
4411 break;
4412 }
4413
4414 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4415 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004416 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004417 IS.NumIterations, IS.CounterStep, IS.Subtract);
4418 if (!Final.isUsable()) {
4419 HasErrors = true;
4420 break;
4421 }
4422
4423 // Build Div for the next iteration: Div <- Div * IS.NumIters
4424 if (Cnt != 0) {
4425 if (Div.isUnset())
4426 Div = IS.NumIterations;
4427 else
4428 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4429 IS.NumIterations);
4430
4431 // Add parentheses (for debugging purposes only).
4432 if (Div.isUsable())
4433 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4434 if (!Div.isUsable()) {
4435 HasErrors = true;
4436 break;
4437 }
4438 }
4439 if (!Update.isUsable() || !Final.isUsable()) {
4440 HasErrors = true;
4441 break;
4442 }
4443 // Save results
4444 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004445 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004446 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004447 Built.Updates[Cnt] = Update.get();
4448 Built.Finals[Cnt] = Final.get();
4449 }
4450 }
4451
4452 if (HasErrors)
4453 return 0;
4454
4455 // Save results
4456 Built.IterationVarRef = IV.get();
4457 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004458 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004459 Built.CalcLastIteration =
4460 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004461 Built.PreCond = PreCond.get();
4462 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004463 Built.Init = Init.get();
4464 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004465 Built.LB = LB.get();
4466 Built.UB = UB.get();
4467 Built.IL = IL.get();
4468 Built.ST = ST.get();
4469 Built.EUB = EUB.get();
4470 Built.NLB = NextLB.get();
4471 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472
Alexey Bataevabfc0692014-06-25 06:52:00 +00004473 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004474}
4475
Alexey Bataev10e775f2015-07-30 11:36:16 +00004476static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004477 auto CollapseClauses =
4478 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4479 if (CollapseClauses.begin() != CollapseClauses.end())
4480 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004481 return nullptr;
4482}
4483
Alexey Bataev10e775f2015-07-30 11:36:16 +00004484static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004485 auto OrderedClauses =
4486 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4487 if (OrderedClauses.begin() != OrderedClauses.end())
4488 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004489 return nullptr;
4490}
4491
Alexey Bataev66b15b52015-08-21 11:14:16 +00004492static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4493 const Expr *Safelen) {
4494 llvm::APSInt SimdlenRes, SafelenRes;
4495 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4496 Simdlen->isInstantiationDependent() ||
4497 Simdlen->containsUnexpandedParameterPack())
4498 return false;
4499 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4500 Safelen->isInstantiationDependent() ||
4501 Safelen->containsUnexpandedParameterPack())
4502 return false;
4503 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4504 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4505 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4506 // If both simdlen and safelen clauses are specified, the value of the simdlen
4507 // parameter must be less than or equal to the value of the safelen parameter.
4508 if (SimdlenRes > SafelenRes) {
4509 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4510 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4511 return true;
4512 }
4513 return false;
4514}
4515
Alexey Bataev4acb8592014-07-07 13:01:15 +00004516StmtResult Sema::ActOnOpenMPSimdDirective(
4517 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4518 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004519 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004520 if (!AStmt)
4521 return StmtError();
4522
4523 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004524 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004525 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4526 // define the nested loops number.
4527 unsigned NestedLoopCount = CheckOpenMPLoop(
4528 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4529 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004530 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004531 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004532
Alexander Musmana5f070a2014-10-01 06:03:56 +00004533 assert((CurContext->isDependentContext() || B.builtAll()) &&
4534 "omp simd loop exprs were not built");
4535
Alexander Musman3276a272015-03-21 10:12:56 +00004536 if (!CurContext->isDependentContext()) {
4537 // Finalize the clauses that need pre-built expressions for CodeGen.
4538 for (auto C : Clauses) {
4539 if (auto LC = dyn_cast<OMPLinearClause>(C))
4540 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4541 B.NumIterations, *this, CurScope))
4542 return StmtError();
4543 }
4544 }
4545
Alexey Bataev66b15b52015-08-21 11:14:16 +00004546 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4547 // If both simdlen and safelen clauses are specified, the value of the simdlen
4548 // parameter must be less than or equal to the value of the safelen parameter.
4549 OMPSafelenClause *Safelen = nullptr;
4550 OMPSimdlenClause *Simdlen = nullptr;
4551 for (auto *Clause : Clauses) {
4552 if (Clause->getClauseKind() == OMPC_safelen)
4553 Safelen = cast<OMPSafelenClause>(Clause);
4554 else if (Clause->getClauseKind() == OMPC_simdlen)
4555 Simdlen = cast<OMPSimdlenClause>(Clause);
4556 if (Safelen && Simdlen)
4557 break;
4558 }
4559 if (Simdlen && Safelen &&
4560 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4561 Safelen->getSafelen()))
4562 return StmtError();
4563
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004564 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004565 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4566 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004567}
4568
Alexey Bataev4acb8592014-07-07 13:01:15 +00004569StmtResult Sema::ActOnOpenMPForDirective(
4570 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4571 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004572 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004573 if (!AStmt)
4574 return StmtError();
4575
4576 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004577 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004578 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4579 // define the nested loops number.
4580 unsigned NestedLoopCount = CheckOpenMPLoop(
4581 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4582 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004583 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004584 return StmtError();
4585
Alexander Musmana5f070a2014-10-01 06:03:56 +00004586 assert((CurContext->isDependentContext() || B.builtAll()) &&
4587 "omp for loop exprs were not built");
4588
Alexey Bataev54acd402015-08-04 11:18:19 +00004589 if (!CurContext->isDependentContext()) {
4590 // Finalize the clauses that need pre-built expressions for CodeGen.
4591 for (auto C : Clauses) {
4592 if (auto LC = dyn_cast<OMPLinearClause>(C))
4593 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4594 B.NumIterations, *this, CurScope))
4595 return StmtError();
4596 }
4597 }
4598
Alexey Bataevf29276e2014-06-18 04:14:57 +00004599 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004600 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004601 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004602}
4603
Alexander Musmanf82886e2014-09-18 05:12:34 +00004604StmtResult Sema::ActOnOpenMPForSimdDirective(
4605 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4606 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004607 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004608 if (!AStmt)
4609 return StmtError();
4610
4611 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004612 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004613 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4614 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004615 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004616 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4617 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4618 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004619 if (NestedLoopCount == 0)
4620 return StmtError();
4621
Alexander Musmanc6388682014-12-15 07:07:06 +00004622 assert((CurContext->isDependentContext() || B.builtAll()) &&
4623 "omp for simd loop exprs were not built");
4624
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004625 if (!CurContext->isDependentContext()) {
4626 // Finalize the clauses that need pre-built expressions for CodeGen.
4627 for (auto C : Clauses) {
4628 if (auto LC = dyn_cast<OMPLinearClause>(C))
4629 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4630 B.NumIterations, *this, CurScope))
4631 return StmtError();
4632 }
4633 }
4634
Alexey Bataev66b15b52015-08-21 11:14:16 +00004635 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4636 // If both simdlen and safelen clauses are specified, the value of the simdlen
4637 // parameter must be less than or equal to the value of the safelen parameter.
4638 OMPSafelenClause *Safelen = nullptr;
4639 OMPSimdlenClause *Simdlen = nullptr;
4640 for (auto *Clause : Clauses) {
4641 if (Clause->getClauseKind() == OMPC_safelen)
4642 Safelen = cast<OMPSafelenClause>(Clause);
4643 else if (Clause->getClauseKind() == OMPC_simdlen)
4644 Simdlen = cast<OMPSimdlenClause>(Clause);
4645 if (Safelen && Simdlen)
4646 break;
4647 }
4648 if (Simdlen && Safelen &&
4649 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4650 Safelen->getSafelen()))
4651 return StmtError();
4652
Alexander Musmanf82886e2014-09-18 05:12:34 +00004653 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004654 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4655 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004656}
4657
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004658StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4659 Stmt *AStmt,
4660 SourceLocation StartLoc,
4661 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004662 if (!AStmt)
4663 return StmtError();
4664
4665 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004666 auto BaseStmt = AStmt;
4667 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4668 BaseStmt = CS->getCapturedStmt();
4669 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4670 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004671 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004672 return StmtError();
4673 // All associated statements must be '#pragma omp section' except for
4674 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004675 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004676 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4677 if (SectionStmt)
4678 Diag(SectionStmt->getLocStart(),
4679 diag::err_omp_sections_substmt_not_section);
4680 return StmtError();
4681 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004682 cast<OMPSectionDirective>(SectionStmt)
4683 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004684 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004685 } else {
4686 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4687 return StmtError();
4688 }
4689
4690 getCurFunction()->setHasBranchProtectedScope();
4691
Alexey Bataev25e5b442015-09-15 12:52:43 +00004692 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4693 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004694}
4695
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004696StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4697 SourceLocation StartLoc,
4698 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004699 if (!AStmt)
4700 return StmtError();
4701
4702 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004703
4704 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004705 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004706
Alexey Bataev25e5b442015-09-15 12:52:43 +00004707 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4708 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004709}
4710
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004711StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4712 Stmt *AStmt,
4713 SourceLocation StartLoc,
4714 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004715 if (!AStmt)
4716 return StmtError();
4717
4718 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004719
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004720 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004721
Alexey Bataev3255bf32015-01-19 05:20:46 +00004722 // OpenMP [2.7.3, single Construct, Restrictions]
4723 // The copyprivate clause must not be used with the nowait clause.
4724 OMPClause *Nowait = nullptr;
4725 OMPClause *Copyprivate = nullptr;
4726 for (auto *Clause : Clauses) {
4727 if (Clause->getClauseKind() == OMPC_nowait)
4728 Nowait = Clause;
4729 else if (Clause->getClauseKind() == OMPC_copyprivate)
4730 Copyprivate = Clause;
4731 if (Copyprivate && Nowait) {
4732 Diag(Copyprivate->getLocStart(),
4733 diag::err_omp_single_copyprivate_with_nowait);
4734 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4735 return StmtError();
4736 }
4737 }
4738
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004739 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4740}
4741
Alexander Musman80c22892014-07-17 08:54:58 +00004742StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4743 SourceLocation StartLoc,
4744 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004745 if (!AStmt)
4746 return StmtError();
4747
4748 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004749
4750 getCurFunction()->setHasBranchProtectedScope();
4751
4752 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4753}
4754
Alexey Bataev28c75412015-12-15 08:19:24 +00004755StmtResult Sema::ActOnOpenMPCriticalDirective(
4756 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4757 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004758 if (!AStmt)
4759 return StmtError();
4760
4761 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004762
Alexey Bataev28c75412015-12-15 08:19:24 +00004763 bool ErrorFound = false;
4764 llvm::APSInt Hint;
4765 SourceLocation HintLoc;
4766 bool DependentHint = false;
4767 for (auto *C : Clauses) {
4768 if (C->getClauseKind() == OMPC_hint) {
4769 if (!DirName.getName()) {
4770 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4771 ErrorFound = true;
4772 }
4773 Expr *E = cast<OMPHintClause>(C)->getHint();
4774 if (E->isTypeDependent() || E->isValueDependent() ||
4775 E->isInstantiationDependent())
4776 DependentHint = true;
4777 else {
4778 Hint = E->EvaluateKnownConstInt(Context);
4779 HintLoc = C->getLocStart();
4780 }
4781 }
4782 }
4783 if (ErrorFound)
4784 return StmtError();
4785 auto Pair = DSAStack->getCriticalWithHint(DirName);
4786 if (Pair.first && DirName.getName() && !DependentHint) {
4787 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4788 Diag(StartLoc, diag::err_omp_critical_with_hint);
4789 if (HintLoc.isValid()) {
4790 Diag(HintLoc, diag::note_omp_critical_hint_here)
4791 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4792 } else
4793 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4794 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4795 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4796 << 1
4797 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4798 /*Radix=*/10, /*Signed=*/false);
4799 } else
4800 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4801 }
4802 }
4803
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004804 getCurFunction()->setHasBranchProtectedScope();
4805
Alexey Bataev28c75412015-12-15 08:19:24 +00004806 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4807 Clauses, AStmt);
4808 if (!Pair.first && DirName.getName() && !DependentHint)
4809 DSAStack->addCriticalWithHint(Dir, Hint);
4810 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004811}
4812
Alexey Bataev4acb8592014-07-07 13:01:15 +00004813StmtResult Sema::ActOnOpenMPParallelForDirective(
4814 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4815 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004816 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004817 if (!AStmt)
4818 return StmtError();
4819
Alexey Bataev4acb8592014-07-07 13:01:15 +00004820 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4821 // 1.2.2 OpenMP Language Terminology
4822 // Structured block - An executable statement with a single entry at the
4823 // top and a single exit at the bottom.
4824 // The point of exit cannot be a branch out of the structured block.
4825 // longjmp() and throw() must not violate the entry/exit criteria.
4826 CS->getCapturedDecl()->setNothrow();
4827
Alexander Musmanc6388682014-12-15 07:07:06 +00004828 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004829 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4830 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004831 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004832 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4833 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4834 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004835 if (NestedLoopCount == 0)
4836 return StmtError();
4837
Alexander Musmana5f070a2014-10-01 06:03:56 +00004838 assert((CurContext->isDependentContext() || B.builtAll()) &&
4839 "omp parallel for loop exprs were not built");
4840
Alexey Bataev54acd402015-08-04 11:18:19 +00004841 if (!CurContext->isDependentContext()) {
4842 // Finalize the clauses that need pre-built expressions for CodeGen.
4843 for (auto C : Clauses) {
4844 if (auto LC = dyn_cast<OMPLinearClause>(C))
4845 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4846 B.NumIterations, *this, CurScope))
4847 return StmtError();
4848 }
4849 }
4850
Alexey Bataev4acb8592014-07-07 13:01:15 +00004851 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004852 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004853 NestedLoopCount, Clauses, AStmt, B,
4854 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004855}
4856
Alexander Musmane4e893b2014-09-23 09:33:00 +00004857StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4858 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4859 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004860 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004861 if (!AStmt)
4862 return StmtError();
4863
Alexander Musmane4e893b2014-09-23 09:33:00 +00004864 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4865 // 1.2.2 OpenMP Language Terminology
4866 // Structured block - An executable statement with a single entry at the
4867 // top and a single exit at the bottom.
4868 // The point of exit cannot be a branch out of the structured block.
4869 // longjmp() and throw() must not violate the entry/exit criteria.
4870 CS->getCapturedDecl()->setNothrow();
4871
Alexander Musmanc6388682014-12-15 07:07:06 +00004872 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004873 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4874 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004875 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004876 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4877 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4878 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004879 if (NestedLoopCount == 0)
4880 return StmtError();
4881
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004882 if (!CurContext->isDependentContext()) {
4883 // Finalize the clauses that need pre-built expressions for CodeGen.
4884 for (auto C : Clauses) {
4885 if (auto LC = dyn_cast<OMPLinearClause>(C))
4886 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4887 B.NumIterations, *this, CurScope))
4888 return StmtError();
4889 }
4890 }
4891
Alexey Bataev66b15b52015-08-21 11:14:16 +00004892 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4893 // If both simdlen and safelen clauses are specified, the value of the simdlen
4894 // parameter must be less than or equal to the value of the safelen parameter.
4895 OMPSafelenClause *Safelen = nullptr;
4896 OMPSimdlenClause *Simdlen = nullptr;
4897 for (auto *Clause : Clauses) {
4898 if (Clause->getClauseKind() == OMPC_safelen)
4899 Safelen = cast<OMPSafelenClause>(Clause);
4900 else if (Clause->getClauseKind() == OMPC_simdlen)
4901 Simdlen = cast<OMPSimdlenClause>(Clause);
4902 if (Safelen && Simdlen)
4903 break;
4904 }
4905 if (Simdlen && Safelen &&
4906 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4907 Safelen->getSafelen()))
4908 return StmtError();
4909
Alexander Musmane4e893b2014-09-23 09:33:00 +00004910 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004911 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004912 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004913}
4914
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004915StmtResult
4916Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4917 Stmt *AStmt, SourceLocation StartLoc,
4918 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004919 if (!AStmt)
4920 return StmtError();
4921
4922 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004923 auto BaseStmt = AStmt;
4924 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4925 BaseStmt = CS->getCapturedStmt();
4926 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4927 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004928 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004929 return StmtError();
4930 // All associated statements must be '#pragma omp section' except for
4931 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004932 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004933 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4934 if (SectionStmt)
4935 Diag(SectionStmt->getLocStart(),
4936 diag::err_omp_parallel_sections_substmt_not_section);
4937 return StmtError();
4938 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004939 cast<OMPSectionDirective>(SectionStmt)
4940 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004941 }
4942 } else {
4943 Diag(AStmt->getLocStart(),
4944 diag::err_omp_parallel_sections_not_compound_stmt);
4945 return StmtError();
4946 }
4947
4948 getCurFunction()->setHasBranchProtectedScope();
4949
Alexey Bataev25e5b442015-09-15 12:52:43 +00004950 return OMPParallelSectionsDirective::Create(
4951 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004952}
4953
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004954StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4955 Stmt *AStmt, SourceLocation StartLoc,
4956 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004957 if (!AStmt)
4958 return StmtError();
4959
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004960 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4961 // 1.2.2 OpenMP Language Terminology
4962 // Structured block - An executable statement with a single entry at the
4963 // top and a single exit at the bottom.
4964 // The point of exit cannot be a branch out of the structured block.
4965 // longjmp() and throw() must not violate the entry/exit criteria.
4966 CS->getCapturedDecl()->setNothrow();
4967
4968 getCurFunction()->setHasBranchProtectedScope();
4969
Alexey Bataev25e5b442015-09-15 12:52:43 +00004970 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4971 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004972}
4973
Alexey Bataev68446b72014-07-18 07:47:19 +00004974StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4975 SourceLocation EndLoc) {
4976 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4977}
4978
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004979StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4980 SourceLocation EndLoc) {
4981 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4982}
4983
Alexey Bataev2df347a2014-07-18 10:17:07 +00004984StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4985 SourceLocation EndLoc) {
4986 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4987}
4988
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004989StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4990 SourceLocation StartLoc,
4991 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004992 if (!AStmt)
4993 return StmtError();
4994
4995 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004996
4997 getCurFunction()->setHasBranchProtectedScope();
4998
4999 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5000}
5001
Alexey Bataev6125da92014-07-21 11:26:11 +00005002StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5003 SourceLocation StartLoc,
5004 SourceLocation EndLoc) {
5005 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5006 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5007}
5008
Alexey Bataev346265e2015-09-25 10:37:12 +00005009StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5010 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005011 SourceLocation StartLoc,
5012 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005013 OMPClause *DependFound = nullptr;
5014 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005015 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005016 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005017 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005018 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005019 for (auto *C : Clauses) {
5020 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5021 DependFound = C;
5022 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5023 if (DependSourceClause) {
5024 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5025 << getOpenMPDirectiveName(OMPD_ordered)
5026 << getOpenMPClauseName(OMPC_depend) << 2;
5027 ErrorFound = true;
5028 } else
5029 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005030 if (DependSinkClause) {
5031 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5032 << 0;
5033 ErrorFound = true;
5034 }
5035 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5036 if (DependSourceClause) {
5037 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5038 << 1;
5039 ErrorFound = true;
5040 }
5041 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005042 }
5043 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005044 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005045 else if (C->getClauseKind() == OMPC_simd)
5046 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005047 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005048 if (!ErrorFound && !SC &&
5049 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005050 // OpenMP [2.8.1,simd Construct, Restrictions]
5051 // An ordered construct with the simd clause is the only OpenMP construct
5052 // that can appear in the simd region.
5053 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005054 ErrorFound = true;
5055 } else if (DependFound && (TC || SC)) {
5056 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5057 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5058 ErrorFound = true;
5059 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5060 Diag(DependFound->getLocStart(),
5061 diag::err_omp_ordered_directive_without_param);
5062 ErrorFound = true;
5063 } else if (TC || Clauses.empty()) {
5064 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5065 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5066 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5067 << (TC != nullptr);
5068 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5069 ErrorFound = true;
5070 }
5071 }
5072 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005073 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005074
5075 if (AStmt) {
5076 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5077
5078 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005079 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005080
5081 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005082}
5083
Alexey Bataev1d160b12015-03-13 12:27:31 +00005084namespace {
5085/// \brief Helper class for checking expression in 'omp atomic [update]'
5086/// construct.
5087class OpenMPAtomicUpdateChecker {
5088 /// \brief Error results for atomic update expressions.
5089 enum ExprAnalysisErrorCode {
5090 /// \brief A statement is not an expression statement.
5091 NotAnExpression,
5092 /// \brief Expression is not builtin binary or unary operation.
5093 NotABinaryOrUnaryExpression,
5094 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5095 NotAnUnaryIncDecExpression,
5096 /// \brief An expression is not of scalar type.
5097 NotAScalarType,
5098 /// \brief A binary operation is not an assignment operation.
5099 NotAnAssignmentOp,
5100 /// \brief RHS part of the binary operation is not a binary expression.
5101 NotABinaryExpression,
5102 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5103 /// expression.
5104 NotABinaryOperator,
5105 /// \brief RHS binary operation does not have reference to the updated LHS
5106 /// part.
5107 NotAnUpdateExpression,
5108 /// \brief No errors is found.
5109 NoError
5110 };
5111 /// \brief Reference to Sema.
5112 Sema &SemaRef;
5113 /// \brief A location for note diagnostics (when error is found).
5114 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005115 /// \brief 'x' lvalue part of the source atomic expression.
5116 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005117 /// \brief 'expr' rvalue part of the source atomic expression.
5118 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005119 /// \brief Helper expression of the form
5120 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5121 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5122 Expr *UpdateExpr;
5123 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5124 /// important for non-associative operations.
5125 bool IsXLHSInRHSPart;
5126 BinaryOperatorKind Op;
5127 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005128 /// \brief true if the source expression is a postfix unary operation, false
5129 /// if it is a prefix unary operation.
5130 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005131
5132public:
5133 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005134 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005135 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005136 /// \brief Check specified statement that it is suitable for 'atomic update'
5137 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005138 /// expression. If DiagId and NoteId == 0, then only check is performed
5139 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005140 /// \param DiagId Diagnostic which should be emitted if error is found.
5141 /// \param NoteId Diagnostic note for the main error message.
5142 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005143 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005144 /// \brief Return the 'x' lvalue part of the source atomic expression.
5145 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005146 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5147 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005148 /// \brief Return the update expression used in calculation of the updated
5149 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5150 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5151 Expr *getUpdateExpr() const { return UpdateExpr; }
5152 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5153 /// false otherwise.
5154 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5155
Alexey Bataevb78ca832015-04-01 03:33:17 +00005156 /// \brief true if the source expression is a postfix unary operation, false
5157 /// if it is a prefix unary operation.
5158 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5159
Alexey Bataev1d160b12015-03-13 12:27:31 +00005160private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005161 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5162 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005163};
5164} // namespace
5165
5166bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5167 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5168 ExprAnalysisErrorCode ErrorFound = NoError;
5169 SourceLocation ErrorLoc, NoteLoc;
5170 SourceRange ErrorRange, NoteRange;
5171 // Allowed constructs are:
5172 // x = x binop expr;
5173 // x = expr binop x;
5174 if (AtomicBinOp->getOpcode() == BO_Assign) {
5175 X = AtomicBinOp->getLHS();
5176 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5177 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5178 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5179 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5180 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005181 Op = AtomicInnerBinOp->getOpcode();
5182 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005183 auto *LHS = AtomicInnerBinOp->getLHS();
5184 auto *RHS = AtomicInnerBinOp->getRHS();
5185 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5186 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5187 /*Canonical=*/true);
5188 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5189 /*Canonical=*/true);
5190 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5191 /*Canonical=*/true);
5192 if (XId == LHSId) {
5193 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005194 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005195 } else if (XId == RHSId) {
5196 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005197 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005198 } else {
5199 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5200 ErrorRange = AtomicInnerBinOp->getSourceRange();
5201 NoteLoc = X->getExprLoc();
5202 NoteRange = X->getSourceRange();
5203 ErrorFound = NotAnUpdateExpression;
5204 }
5205 } else {
5206 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5207 ErrorRange = AtomicInnerBinOp->getSourceRange();
5208 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5209 NoteRange = SourceRange(NoteLoc, NoteLoc);
5210 ErrorFound = NotABinaryOperator;
5211 }
5212 } else {
5213 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5214 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5215 ErrorFound = NotABinaryExpression;
5216 }
5217 } else {
5218 ErrorLoc = AtomicBinOp->getExprLoc();
5219 ErrorRange = AtomicBinOp->getSourceRange();
5220 NoteLoc = AtomicBinOp->getOperatorLoc();
5221 NoteRange = SourceRange(NoteLoc, NoteLoc);
5222 ErrorFound = NotAnAssignmentOp;
5223 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005224 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005225 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5226 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5227 return true;
5228 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005229 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005230 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005231}
5232
5233bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5234 unsigned NoteId) {
5235 ExprAnalysisErrorCode ErrorFound = NoError;
5236 SourceLocation ErrorLoc, NoteLoc;
5237 SourceRange ErrorRange, NoteRange;
5238 // Allowed constructs are:
5239 // x++;
5240 // x--;
5241 // ++x;
5242 // --x;
5243 // x binop= expr;
5244 // x = x binop expr;
5245 // x = expr binop x;
5246 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5247 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5248 if (AtomicBody->getType()->isScalarType() ||
5249 AtomicBody->isInstantiationDependent()) {
5250 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5251 AtomicBody->IgnoreParenImpCasts())) {
5252 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005253 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005254 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005255 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005256 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005257 X = AtomicCompAssignOp->getLHS();
5258 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005259 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5260 AtomicBody->IgnoreParenImpCasts())) {
5261 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005262 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5263 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005264 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005265 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5266 // Check for Unary Operation
5267 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005268 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005269 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5270 OpLoc = AtomicUnaryOp->getOperatorLoc();
5271 X = AtomicUnaryOp->getSubExpr();
5272 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5273 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005274 } else {
5275 ErrorFound = NotAnUnaryIncDecExpression;
5276 ErrorLoc = AtomicUnaryOp->getExprLoc();
5277 ErrorRange = AtomicUnaryOp->getSourceRange();
5278 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5279 NoteRange = SourceRange(NoteLoc, NoteLoc);
5280 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005281 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005282 ErrorFound = NotABinaryOrUnaryExpression;
5283 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5284 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5285 }
5286 } else {
5287 ErrorFound = NotAScalarType;
5288 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5289 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5290 }
5291 } else {
5292 ErrorFound = NotAnExpression;
5293 NoteLoc = ErrorLoc = S->getLocStart();
5294 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5295 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005296 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005297 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5298 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5299 return true;
5300 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005301 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005302 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005303 // Build an update expression of form 'OpaqueValueExpr(x) binop
5304 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5305 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5306 auto *OVEX = new (SemaRef.getASTContext())
5307 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5308 auto *OVEExpr = new (SemaRef.getASTContext())
5309 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5310 auto Update =
5311 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5312 IsXLHSInRHSPart ? OVEExpr : OVEX);
5313 if (Update.isInvalid())
5314 return true;
5315 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5316 Sema::AA_Casting);
5317 if (Update.isInvalid())
5318 return true;
5319 UpdateExpr = Update.get();
5320 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005321 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005322}
5323
Alexey Bataev0162e452014-07-22 10:10:35 +00005324StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5325 Stmt *AStmt,
5326 SourceLocation StartLoc,
5327 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005328 if (!AStmt)
5329 return StmtError();
5330
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005331 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005332 // 1.2.2 OpenMP Language Terminology
5333 // Structured block - An executable statement with a single entry at the
5334 // top and a single exit at the bottom.
5335 // The point of exit cannot be a branch out of the structured block.
5336 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005337 OpenMPClauseKind AtomicKind = OMPC_unknown;
5338 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005339 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005340 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005341 C->getClauseKind() == OMPC_update ||
5342 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005343 if (AtomicKind != OMPC_unknown) {
5344 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5345 << SourceRange(C->getLocStart(), C->getLocEnd());
5346 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5347 << getOpenMPClauseName(AtomicKind);
5348 } else {
5349 AtomicKind = C->getClauseKind();
5350 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005351 }
5352 }
5353 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005354
Alexey Bataev459dec02014-07-24 06:46:57 +00005355 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005356 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5357 Body = EWC->getSubExpr();
5358
Alexey Bataev62cec442014-11-18 10:14:22 +00005359 Expr *X = nullptr;
5360 Expr *V = nullptr;
5361 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005362 Expr *UE = nullptr;
5363 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005364 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005365 // OpenMP [2.12.6, atomic Construct]
5366 // In the next expressions:
5367 // * x and v (as applicable) are both l-value expressions with scalar type.
5368 // * During the execution of an atomic region, multiple syntactic
5369 // occurrences of x must designate the same storage location.
5370 // * Neither of v and expr (as applicable) may access the storage location
5371 // designated by x.
5372 // * Neither of x and expr (as applicable) may access the storage location
5373 // designated by v.
5374 // * expr is an expression with scalar type.
5375 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5376 // * binop, binop=, ++, and -- are not overloaded operators.
5377 // * The expression x binop expr must be numerically equivalent to x binop
5378 // (expr). This requirement is satisfied if the operators in expr have
5379 // precedence greater than binop, or by using parentheses around expr or
5380 // subexpressions of expr.
5381 // * The expression expr binop x must be numerically equivalent to (expr)
5382 // binop x. This requirement is satisfied if the operators in expr have
5383 // precedence equal to or greater than binop, or by using parentheses around
5384 // expr or subexpressions of expr.
5385 // * For forms that allow multiple occurrences of x, the number of times
5386 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005387 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005388 enum {
5389 NotAnExpression,
5390 NotAnAssignmentOp,
5391 NotAScalarType,
5392 NotAnLValue,
5393 NoError
5394 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005395 SourceLocation ErrorLoc, NoteLoc;
5396 SourceRange ErrorRange, NoteRange;
5397 // If clause is read:
5398 // v = x;
5399 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5400 auto AtomicBinOp =
5401 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5402 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5403 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5404 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5405 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5406 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5407 if (!X->isLValue() || !V->isLValue()) {
5408 auto NotLValueExpr = X->isLValue() ? V : X;
5409 ErrorFound = NotAnLValue;
5410 ErrorLoc = AtomicBinOp->getExprLoc();
5411 ErrorRange = AtomicBinOp->getSourceRange();
5412 NoteLoc = NotLValueExpr->getExprLoc();
5413 NoteRange = NotLValueExpr->getSourceRange();
5414 }
5415 } else if (!X->isInstantiationDependent() ||
5416 !V->isInstantiationDependent()) {
5417 auto NotScalarExpr =
5418 (X->isInstantiationDependent() || X->getType()->isScalarType())
5419 ? V
5420 : X;
5421 ErrorFound = NotAScalarType;
5422 ErrorLoc = AtomicBinOp->getExprLoc();
5423 ErrorRange = AtomicBinOp->getSourceRange();
5424 NoteLoc = NotScalarExpr->getExprLoc();
5425 NoteRange = NotScalarExpr->getSourceRange();
5426 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005427 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005428 ErrorFound = NotAnAssignmentOp;
5429 ErrorLoc = AtomicBody->getExprLoc();
5430 ErrorRange = AtomicBody->getSourceRange();
5431 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5432 : AtomicBody->getExprLoc();
5433 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5434 : AtomicBody->getSourceRange();
5435 }
5436 } else {
5437 ErrorFound = NotAnExpression;
5438 NoteLoc = ErrorLoc = Body->getLocStart();
5439 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005440 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005441 if (ErrorFound != NoError) {
5442 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5443 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005444 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5445 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005446 return StmtError();
5447 } else if (CurContext->isDependentContext())
5448 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005449 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005450 enum {
5451 NotAnExpression,
5452 NotAnAssignmentOp,
5453 NotAScalarType,
5454 NotAnLValue,
5455 NoError
5456 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005457 SourceLocation ErrorLoc, NoteLoc;
5458 SourceRange ErrorRange, NoteRange;
5459 // If clause is write:
5460 // x = expr;
5461 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5462 auto AtomicBinOp =
5463 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5464 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005465 X = AtomicBinOp->getLHS();
5466 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005467 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5468 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5469 if (!X->isLValue()) {
5470 ErrorFound = NotAnLValue;
5471 ErrorLoc = AtomicBinOp->getExprLoc();
5472 ErrorRange = AtomicBinOp->getSourceRange();
5473 NoteLoc = X->getExprLoc();
5474 NoteRange = X->getSourceRange();
5475 }
5476 } else if (!X->isInstantiationDependent() ||
5477 !E->isInstantiationDependent()) {
5478 auto NotScalarExpr =
5479 (X->isInstantiationDependent() || X->getType()->isScalarType())
5480 ? E
5481 : X;
5482 ErrorFound = NotAScalarType;
5483 ErrorLoc = AtomicBinOp->getExprLoc();
5484 ErrorRange = AtomicBinOp->getSourceRange();
5485 NoteLoc = NotScalarExpr->getExprLoc();
5486 NoteRange = NotScalarExpr->getSourceRange();
5487 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005488 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005489 ErrorFound = NotAnAssignmentOp;
5490 ErrorLoc = AtomicBody->getExprLoc();
5491 ErrorRange = AtomicBody->getSourceRange();
5492 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5493 : AtomicBody->getExprLoc();
5494 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5495 : AtomicBody->getSourceRange();
5496 }
5497 } else {
5498 ErrorFound = NotAnExpression;
5499 NoteLoc = ErrorLoc = Body->getLocStart();
5500 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005501 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005502 if (ErrorFound != NoError) {
5503 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5504 << ErrorRange;
5505 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5506 << NoteRange;
5507 return StmtError();
5508 } else if (CurContext->isDependentContext())
5509 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005510 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005511 // If clause is update:
5512 // x++;
5513 // x--;
5514 // ++x;
5515 // --x;
5516 // x binop= expr;
5517 // x = x binop expr;
5518 // x = expr binop x;
5519 OpenMPAtomicUpdateChecker Checker(*this);
5520 if (Checker.checkStatement(
5521 Body, (AtomicKind == OMPC_update)
5522 ? diag::err_omp_atomic_update_not_expression_statement
5523 : diag::err_omp_atomic_not_expression_statement,
5524 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005525 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005526 if (!CurContext->isDependentContext()) {
5527 E = Checker.getExpr();
5528 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005529 UE = Checker.getUpdateExpr();
5530 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005531 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005532 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005533 enum {
5534 NotAnAssignmentOp,
5535 NotACompoundStatement,
5536 NotTwoSubstatements,
5537 NotASpecificExpression,
5538 NoError
5539 } ErrorFound = NoError;
5540 SourceLocation ErrorLoc, NoteLoc;
5541 SourceRange ErrorRange, NoteRange;
5542 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5543 // If clause is a capture:
5544 // v = x++;
5545 // v = x--;
5546 // v = ++x;
5547 // v = --x;
5548 // v = x binop= expr;
5549 // v = x = x binop expr;
5550 // v = x = expr binop x;
5551 auto *AtomicBinOp =
5552 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5553 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5554 V = AtomicBinOp->getLHS();
5555 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5556 OpenMPAtomicUpdateChecker Checker(*this);
5557 if (Checker.checkStatement(
5558 Body, diag::err_omp_atomic_capture_not_expression_statement,
5559 diag::note_omp_atomic_update))
5560 return StmtError();
5561 E = Checker.getExpr();
5562 X = Checker.getX();
5563 UE = Checker.getUpdateExpr();
5564 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5565 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005566 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005567 ErrorLoc = AtomicBody->getExprLoc();
5568 ErrorRange = AtomicBody->getSourceRange();
5569 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5570 : AtomicBody->getExprLoc();
5571 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5572 : AtomicBody->getSourceRange();
5573 ErrorFound = NotAnAssignmentOp;
5574 }
5575 if (ErrorFound != NoError) {
5576 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5577 << ErrorRange;
5578 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5579 return StmtError();
5580 } else if (CurContext->isDependentContext()) {
5581 UE = V = E = X = nullptr;
5582 }
5583 } else {
5584 // If clause is a capture:
5585 // { v = x; x = expr; }
5586 // { v = x; x++; }
5587 // { v = x; x--; }
5588 // { v = x; ++x; }
5589 // { v = x; --x; }
5590 // { v = x; x binop= expr; }
5591 // { v = x; x = x binop expr; }
5592 // { v = x; x = expr binop x; }
5593 // { x++; v = x; }
5594 // { x--; v = x; }
5595 // { ++x; v = x; }
5596 // { --x; v = x; }
5597 // { x binop= expr; v = x; }
5598 // { x = x binop expr; v = x; }
5599 // { x = expr binop x; v = x; }
5600 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5601 // Check that this is { expr1; expr2; }
5602 if (CS->size() == 2) {
5603 auto *First = CS->body_front();
5604 auto *Second = CS->body_back();
5605 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5606 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5607 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5608 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5609 // Need to find what subexpression is 'v' and what is 'x'.
5610 OpenMPAtomicUpdateChecker Checker(*this);
5611 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5612 BinaryOperator *BinOp = nullptr;
5613 if (IsUpdateExprFound) {
5614 BinOp = dyn_cast<BinaryOperator>(First);
5615 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5616 }
5617 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5618 // { v = x; x++; }
5619 // { v = x; x--; }
5620 // { v = x; ++x; }
5621 // { v = x; --x; }
5622 // { v = x; x binop= expr; }
5623 // { v = x; x = x binop expr; }
5624 // { v = x; x = expr binop x; }
5625 // Check that the first expression has form v = x.
5626 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5627 llvm::FoldingSetNodeID XId, PossibleXId;
5628 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5629 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5630 IsUpdateExprFound = XId == PossibleXId;
5631 if (IsUpdateExprFound) {
5632 V = BinOp->getLHS();
5633 X = Checker.getX();
5634 E = Checker.getExpr();
5635 UE = Checker.getUpdateExpr();
5636 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005637 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005638 }
5639 }
5640 if (!IsUpdateExprFound) {
5641 IsUpdateExprFound = !Checker.checkStatement(First);
5642 BinOp = nullptr;
5643 if (IsUpdateExprFound) {
5644 BinOp = dyn_cast<BinaryOperator>(Second);
5645 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5646 }
5647 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5648 // { x++; v = x; }
5649 // { x--; v = x; }
5650 // { ++x; v = x; }
5651 // { --x; v = x; }
5652 // { x binop= expr; v = x; }
5653 // { x = x binop expr; v = x; }
5654 // { x = expr binop x; v = x; }
5655 // Check that the second expression has form v = x.
5656 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5657 llvm::FoldingSetNodeID XId, PossibleXId;
5658 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5659 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5660 IsUpdateExprFound = XId == PossibleXId;
5661 if (IsUpdateExprFound) {
5662 V = BinOp->getLHS();
5663 X = Checker.getX();
5664 E = Checker.getExpr();
5665 UE = Checker.getUpdateExpr();
5666 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005667 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005668 }
5669 }
5670 }
5671 if (!IsUpdateExprFound) {
5672 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005673 auto *FirstExpr = dyn_cast<Expr>(First);
5674 auto *SecondExpr = dyn_cast<Expr>(Second);
5675 if (!FirstExpr || !SecondExpr ||
5676 !(FirstExpr->isInstantiationDependent() ||
5677 SecondExpr->isInstantiationDependent())) {
5678 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5679 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005680 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005681 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5682 : First->getLocStart();
5683 NoteRange = ErrorRange = FirstBinOp
5684 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005685 : SourceRange(ErrorLoc, ErrorLoc);
5686 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005687 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5688 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5689 ErrorFound = NotAnAssignmentOp;
5690 NoteLoc = ErrorLoc = SecondBinOp
5691 ? SecondBinOp->getOperatorLoc()
5692 : Second->getLocStart();
5693 NoteRange = ErrorRange =
5694 SecondBinOp ? SecondBinOp->getSourceRange()
5695 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005696 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005697 auto *PossibleXRHSInFirst =
5698 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5699 auto *PossibleXLHSInSecond =
5700 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5701 llvm::FoldingSetNodeID X1Id, X2Id;
5702 PossibleXRHSInFirst->Profile(X1Id, Context,
5703 /*Canonical=*/true);
5704 PossibleXLHSInSecond->Profile(X2Id, Context,
5705 /*Canonical=*/true);
5706 IsUpdateExprFound = X1Id == X2Id;
5707 if (IsUpdateExprFound) {
5708 V = FirstBinOp->getLHS();
5709 X = SecondBinOp->getLHS();
5710 E = SecondBinOp->getRHS();
5711 UE = nullptr;
5712 IsXLHSInRHSPart = false;
5713 IsPostfixUpdate = true;
5714 } else {
5715 ErrorFound = NotASpecificExpression;
5716 ErrorLoc = FirstBinOp->getExprLoc();
5717 ErrorRange = FirstBinOp->getSourceRange();
5718 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5719 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5720 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005721 }
5722 }
5723 }
5724 }
5725 } else {
5726 NoteLoc = ErrorLoc = Body->getLocStart();
5727 NoteRange = ErrorRange =
5728 SourceRange(Body->getLocStart(), Body->getLocStart());
5729 ErrorFound = NotTwoSubstatements;
5730 }
5731 } else {
5732 NoteLoc = ErrorLoc = Body->getLocStart();
5733 NoteRange = ErrorRange =
5734 SourceRange(Body->getLocStart(), Body->getLocStart());
5735 ErrorFound = NotACompoundStatement;
5736 }
5737 if (ErrorFound != NoError) {
5738 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5739 << ErrorRange;
5740 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5741 return StmtError();
5742 } else if (CurContext->isDependentContext()) {
5743 UE = V = E = X = nullptr;
5744 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005745 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005746 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005747
5748 getCurFunction()->setHasBranchProtectedScope();
5749
Alexey Bataev62cec442014-11-18 10:14:22 +00005750 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005751 X, V, E, UE, IsXLHSInRHSPart,
5752 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005753}
5754
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005755StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5756 Stmt *AStmt,
5757 SourceLocation StartLoc,
5758 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005759 if (!AStmt)
5760 return StmtError();
5761
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005762 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5763 // 1.2.2 OpenMP Language Terminology
5764 // Structured block - An executable statement with a single entry at the
5765 // top and a single exit at the bottom.
5766 // The point of exit cannot be a branch out of the structured block.
5767 // longjmp() and throw() must not violate the entry/exit criteria.
5768 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005769
Alexey Bataev13314bf2014-10-09 04:18:56 +00005770 // OpenMP [2.16, Nesting of Regions]
5771 // If specified, a teams construct must be contained within a target
5772 // construct. That target construct must contain no statements or directives
5773 // outside of the teams construct.
5774 if (DSAStack->hasInnerTeamsRegion()) {
5775 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5776 bool OMPTeamsFound = true;
5777 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5778 auto I = CS->body_begin();
5779 while (I != CS->body_end()) {
5780 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5781 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5782 OMPTeamsFound = false;
5783 break;
5784 }
5785 ++I;
5786 }
5787 assert(I != CS->body_end() && "Not found statement");
5788 S = *I;
5789 }
5790 if (!OMPTeamsFound) {
5791 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5792 Diag(DSAStack->getInnerTeamsRegionLoc(),
5793 diag::note_omp_nested_teams_construct_here);
5794 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5795 << isa<OMPExecutableDirective>(S);
5796 return StmtError();
5797 }
5798 }
5799
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005800 getCurFunction()->setHasBranchProtectedScope();
5801
5802 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5803}
5804
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005805StmtResult
5806Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5807 Stmt *AStmt, SourceLocation StartLoc,
5808 SourceLocation EndLoc) {
5809 if (!AStmt)
5810 return StmtError();
5811
5812 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5813 // 1.2.2 OpenMP Language Terminology
5814 // Structured block - An executable statement with a single entry at the
5815 // top and a single exit at the bottom.
5816 // The point of exit cannot be a branch out of the structured block.
5817 // longjmp() and throw() must not violate the entry/exit criteria.
5818 CS->getCapturedDecl()->setNothrow();
5819
5820 getCurFunction()->setHasBranchProtectedScope();
5821
5822 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5823 AStmt);
5824}
5825
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005826StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5827 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5828 SourceLocation EndLoc,
5829 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5830 if (!AStmt)
5831 return StmtError();
5832
5833 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5834 // 1.2.2 OpenMP Language Terminology
5835 // Structured block - An executable statement with a single entry at the
5836 // top and a single exit at the bottom.
5837 // The point of exit cannot be a branch out of the structured block.
5838 // longjmp() and throw() must not violate the entry/exit criteria.
5839 CS->getCapturedDecl()->setNothrow();
5840
5841 OMPLoopDirective::HelperExprs B;
5842 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5843 // define the nested loops number.
5844 unsigned NestedLoopCount =
5845 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5846 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5847 VarsWithImplicitDSA, B);
5848 if (NestedLoopCount == 0)
5849 return StmtError();
5850
5851 assert((CurContext->isDependentContext() || B.builtAll()) &&
5852 "omp target parallel for loop exprs were not built");
5853
5854 if (!CurContext->isDependentContext()) {
5855 // Finalize the clauses that need pre-built expressions for CodeGen.
5856 for (auto C : Clauses) {
5857 if (auto LC = dyn_cast<OMPLinearClause>(C))
5858 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5859 B.NumIterations, *this, CurScope))
5860 return StmtError();
5861 }
5862 }
5863
5864 getCurFunction()->setHasBranchProtectedScope();
5865 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5866 NestedLoopCount, Clauses, AStmt,
5867 B, DSAStack->isCancelRegion());
5868}
5869
Samuel Antaodf67fc42016-01-19 19:15:56 +00005870/// \brief Check for existence of a map clause in the list of clauses.
5871static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5872 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5873 I != E; ++I) {
5874 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5875 return true;
5876 }
5877 }
5878
5879 return false;
5880}
5881
Michael Wong65f367f2015-07-21 13:44:28 +00005882StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5883 Stmt *AStmt,
5884 SourceLocation StartLoc,
5885 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005886 if (!AStmt)
5887 return StmtError();
5888
5889 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5890
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005891 // OpenMP [2.10.1, Restrictions, p. 97]
5892 // At least one map clause must appear on the directive.
5893 if (!HasMapClause(Clauses)) {
5894 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5895 getOpenMPDirectiveName(OMPD_target_data);
5896 return StmtError();
5897 }
5898
Michael Wong65f367f2015-07-21 13:44:28 +00005899 getCurFunction()->setHasBranchProtectedScope();
5900
5901 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5902 AStmt);
5903}
5904
Samuel Antaodf67fc42016-01-19 19:15:56 +00005905StmtResult
5906Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5907 SourceLocation StartLoc,
5908 SourceLocation EndLoc) {
5909 // OpenMP [2.10.2, Restrictions, p. 99]
5910 // At least one map clause must appear on the directive.
5911 if (!HasMapClause(Clauses)) {
5912 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5913 << getOpenMPDirectiveName(OMPD_target_enter_data);
5914 return StmtError();
5915 }
5916
5917 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5918 Clauses);
5919}
5920
Samuel Antao72590762016-01-19 20:04:50 +00005921StmtResult
5922Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5923 SourceLocation StartLoc,
5924 SourceLocation EndLoc) {
5925 // OpenMP [2.10.3, Restrictions, p. 102]
5926 // At least one map clause must appear on the directive.
5927 if (!HasMapClause(Clauses)) {
5928 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5929 << getOpenMPDirectiveName(OMPD_target_exit_data);
5930 return StmtError();
5931 }
5932
5933 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5934}
5935
Alexey Bataev13314bf2014-10-09 04:18:56 +00005936StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5937 Stmt *AStmt, SourceLocation StartLoc,
5938 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005939 if (!AStmt)
5940 return StmtError();
5941
Alexey Bataev13314bf2014-10-09 04:18:56 +00005942 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5943 // 1.2.2 OpenMP Language Terminology
5944 // Structured block - An executable statement with a single entry at the
5945 // top and a single exit at the bottom.
5946 // The point of exit cannot be a branch out of the structured block.
5947 // longjmp() and throw() must not violate the entry/exit criteria.
5948 CS->getCapturedDecl()->setNothrow();
5949
5950 getCurFunction()->setHasBranchProtectedScope();
5951
5952 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5953}
5954
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005955StmtResult
5956Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5957 SourceLocation EndLoc,
5958 OpenMPDirectiveKind CancelRegion) {
5959 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5960 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5961 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5962 << getOpenMPDirectiveName(CancelRegion);
5963 return StmtError();
5964 }
5965 if (DSAStack->isParentNowaitRegion()) {
5966 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5967 return StmtError();
5968 }
5969 if (DSAStack->isParentOrderedRegion()) {
5970 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5971 return StmtError();
5972 }
5973 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5974 CancelRegion);
5975}
5976
Alexey Bataev87933c72015-09-18 08:07:34 +00005977StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5978 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005979 SourceLocation EndLoc,
5980 OpenMPDirectiveKind CancelRegion) {
5981 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5982 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5983 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5984 << getOpenMPDirectiveName(CancelRegion);
5985 return StmtError();
5986 }
5987 if (DSAStack->isParentNowaitRegion()) {
5988 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5989 return StmtError();
5990 }
5991 if (DSAStack->isParentOrderedRegion()) {
5992 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5993 return StmtError();
5994 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005995 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005996 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5997 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005998}
5999
Alexey Bataev382967a2015-12-08 12:06:20 +00006000static bool checkGrainsizeNumTasksClauses(Sema &S,
6001 ArrayRef<OMPClause *> Clauses) {
6002 OMPClause *PrevClause = nullptr;
6003 bool ErrorFound = false;
6004 for (auto *C : Clauses) {
6005 if (C->getClauseKind() == OMPC_grainsize ||
6006 C->getClauseKind() == OMPC_num_tasks) {
6007 if (!PrevClause)
6008 PrevClause = C;
6009 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6010 S.Diag(C->getLocStart(),
6011 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6012 << getOpenMPClauseName(C->getClauseKind())
6013 << getOpenMPClauseName(PrevClause->getClauseKind());
6014 S.Diag(PrevClause->getLocStart(),
6015 diag::note_omp_previous_grainsize_num_tasks)
6016 << getOpenMPClauseName(PrevClause->getClauseKind());
6017 ErrorFound = true;
6018 }
6019 }
6020 }
6021 return ErrorFound;
6022}
6023
Alexey Bataev49f6e782015-12-01 04:18:41 +00006024StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6025 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6026 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006027 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006028 if (!AStmt)
6029 return StmtError();
6030
6031 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6032 OMPLoopDirective::HelperExprs B;
6033 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6034 // define the nested loops number.
6035 unsigned NestedLoopCount =
6036 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006037 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006038 VarsWithImplicitDSA, B);
6039 if (NestedLoopCount == 0)
6040 return StmtError();
6041
6042 assert((CurContext->isDependentContext() || B.builtAll()) &&
6043 "omp for loop exprs were not built");
6044
Alexey Bataev382967a2015-12-08 12:06:20 +00006045 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6046 // The grainsize clause and num_tasks clause are mutually exclusive and may
6047 // not appear on the same taskloop directive.
6048 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6049 return StmtError();
6050
Alexey Bataev49f6e782015-12-01 04:18:41 +00006051 getCurFunction()->setHasBranchProtectedScope();
6052 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6053 NestedLoopCount, Clauses, AStmt, B);
6054}
6055
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006056StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6057 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6058 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006059 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006060 if (!AStmt)
6061 return StmtError();
6062
6063 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6064 OMPLoopDirective::HelperExprs B;
6065 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6066 // define the nested loops number.
6067 unsigned NestedLoopCount =
6068 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6069 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6070 VarsWithImplicitDSA, B);
6071 if (NestedLoopCount == 0)
6072 return StmtError();
6073
6074 assert((CurContext->isDependentContext() || B.builtAll()) &&
6075 "omp for loop exprs were not built");
6076
Alexey Bataev382967a2015-12-08 12:06:20 +00006077 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6078 // The grainsize clause and num_tasks clause are mutually exclusive and may
6079 // not appear on the same taskloop directive.
6080 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6081 return StmtError();
6082
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006083 getCurFunction()->setHasBranchProtectedScope();
6084 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6085 NestedLoopCount, Clauses, AStmt, B);
6086}
6087
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006088StmtResult Sema::ActOnOpenMPDistributeDirective(
6089 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6090 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006091 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006092 if (!AStmt)
6093 return StmtError();
6094
6095 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6096 OMPLoopDirective::HelperExprs B;
6097 // In presence of clause 'collapse' with number of loops, it will
6098 // define the nested loops number.
6099 unsigned NestedLoopCount =
6100 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6101 nullptr /*ordered not a clause on distribute*/, AStmt,
6102 *this, *DSAStack, VarsWithImplicitDSA, B);
6103 if (NestedLoopCount == 0)
6104 return StmtError();
6105
6106 assert((CurContext->isDependentContext() || B.builtAll()) &&
6107 "omp for loop exprs were not built");
6108
6109 getCurFunction()->setHasBranchProtectedScope();
6110 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6111 NestedLoopCount, Clauses, AStmt, B);
6112}
6113
Alexey Bataeved09d242014-05-28 05:53:51 +00006114OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006115 SourceLocation StartLoc,
6116 SourceLocation LParenLoc,
6117 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006118 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006119 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006120 case OMPC_final:
6121 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6122 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006123 case OMPC_num_threads:
6124 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6125 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006126 case OMPC_safelen:
6127 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6128 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006129 case OMPC_simdlen:
6130 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6131 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006132 case OMPC_collapse:
6133 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6134 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006135 case OMPC_ordered:
6136 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6137 break;
Michael Wonge710d542015-08-07 16:16:36 +00006138 case OMPC_device:
6139 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6140 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006141 case OMPC_num_teams:
6142 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6143 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006144 case OMPC_thread_limit:
6145 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6146 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006147 case OMPC_priority:
6148 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6149 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006150 case OMPC_grainsize:
6151 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6152 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006153 case OMPC_num_tasks:
6154 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6155 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006156 case OMPC_hint:
6157 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6158 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006159 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006160 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006161 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006162 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006163 case OMPC_private:
6164 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006165 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006166 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006167 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006168 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006169 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006170 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006171 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006172 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006173 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006174 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006175 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006176 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006177 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006178 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006179 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006180 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006181 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006182 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006183 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006184 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006185 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006186 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006187 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006188 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006189 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006190 llvm_unreachable("Clause is not allowed.");
6191 }
6192 return Res;
6193}
6194
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006195OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6196 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006197 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006198 SourceLocation NameModifierLoc,
6199 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006200 SourceLocation EndLoc) {
6201 Expr *ValExpr = Condition;
6202 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6203 !Condition->isInstantiationDependent() &&
6204 !Condition->containsUnexpandedParameterPack()) {
6205 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006206 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006207 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006208 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006209
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006210 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006211 }
6212
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006213 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6214 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006215}
6216
Alexey Bataev3778b602014-07-17 07:32:53 +00006217OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6218 SourceLocation StartLoc,
6219 SourceLocation LParenLoc,
6220 SourceLocation EndLoc) {
6221 Expr *ValExpr = Condition;
6222 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6223 !Condition->isInstantiationDependent() &&
6224 !Condition->containsUnexpandedParameterPack()) {
6225 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6226 Condition->getExprLoc(), Condition);
6227 if (Val.isInvalid())
6228 return nullptr;
6229
6230 ValExpr = Val.get();
6231 }
6232
6233 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6234}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006235ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6236 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006237 if (!Op)
6238 return ExprError();
6239
6240 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6241 public:
6242 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006243 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006244 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6245 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006246 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6247 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006248 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6249 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006250 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6251 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006252 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6253 QualType T,
6254 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006255 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6256 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006257 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6258 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006259 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006260 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006261 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006262 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6263 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006264 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6265 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006266 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6267 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006268 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006269 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006270 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006271 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6272 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006273 llvm_unreachable("conversion functions are permitted");
6274 }
6275 } ConvertDiagnoser;
6276 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6277}
6278
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006279static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006280 OpenMPClauseKind CKind,
6281 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006282 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6283 !ValExpr->isInstantiationDependent()) {
6284 SourceLocation Loc = ValExpr->getExprLoc();
6285 ExprResult Value =
6286 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6287 if (Value.isInvalid())
6288 return false;
6289
6290 ValExpr = Value.get();
6291 // The expression must evaluate to a non-negative integer value.
6292 llvm::APSInt Result;
6293 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006294 Result.isSigned() &&
6295 !((!StrictlyPositive && Result.isNonNegative()) ||
6296 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006297 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006298 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6299 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006300 return false;
6301 }
6302 }
6303 return true;
6304}
6305
Alexey Bataev568a8332014-03-06 06:15:19 +00006306OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6307 SourceLocation StartLoc,
6308 SourceLocation LParenLoc,
6309 SourceLocation EndLoc) {
6310 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006311
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006312 // OpenMP [2.5, Restrictions]
6313 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006314 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6315 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006316 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006317
Alexey Bataeved09d242014-05-28 05:53:51 +00006318 return new (Context)
6319 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006320}
6321
Alexey Bataev62c87d22014-03-21 04:51:18 +00006322ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006323 OpenMPClauseKind CKind,
6324 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006325 if (!E)
6326 return ExprError();
6327 if (E->isValueDependent() || E->isTypeDependent() ||
6328 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006329 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006330 llvm::APSInt Result;
6331 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6332 if (ICE.isInvalid())
6333 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006334 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6335 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006336 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006337 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6338 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006339 return ExprError();
6340 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006341 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6342 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6343 << E->getSourceRange();
6344 return ExprError();
6345 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006346 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6347 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006348 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006349 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006350 return ICE;
6351}
6352
6353OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6354 SourceLocation LParenLoc,
6355 SourceLocation EndLoc) {
6356 // OpenMP [2.8.1, simd construct, Description]
6357 // The parameter of the safelen clause must be a constant
6358 // positive integer expression.
6359 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6360 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006361 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006362 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006363 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006364}
6365
Alexey Bataev66b15b52015-08-21 11:14:16 +00006366OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6367 SourceLocation LParenLoc,
6368 SourceLocation EndLoc) {
6369 // OpenMP [2.8.1, simd construct, Description]
6370 // The parameter of the simdlen clause must be a constant
6371 // positive integer expression.
6372 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6373 if (Simdlen.isInvalid())
6374 return nullptr;
6375 return new (Context)
6376 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6377}
6378
Alexander Musman64d33f12014-06-04 07:53:32 +00006379OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6380 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006381 SourceLocation LParenLoc,
6382 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006383 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006384 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006385 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006386 // The parameter of the collapse clause must be a constant
6387 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006388 ExprResult NumForLoopsResult =
6389 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6390 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006391 return nullptr;
6392 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006393 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006394}
6395
Alexey Bataev10e775f2015-07-30 11:36:16 +00006396OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6397 SourceLocation EndLoc,
6398 SourceLocation LParenLoc,
6399 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006400 // OpenMP [2.7.1, loop construct, Description]
6401 // OpenMP [2.8.1, simd construct, Description]
6402 // OpenMP [2.9.6, distribute construct, Description]
6403 // The parameter of the ordered clause must be a constant
6404 // positive integer expression if any.
6405 if (NumForLoops && LParenLoc.isValid()) {
6406 ExprResult NumForLoopsResult =
6407 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6408 if (NumForLoopsResult.isInvalid())
6409 return nullptr;
6410 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006411 } else
6412 NumForLoops = nullptr;
6413 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006414 return new (Context)
6415 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6416}
6417
Alexey Bataeved09d242014-05-28 05:53:51 +00006418OMPClause *Sema::ActOnOpenMPSimpleClause(
6419 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6420 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006421 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006422 switch (Kind) {
6423 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006424 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006425 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6426 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006427 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006428 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006429 Res = ActOnOpenMPProcBindClause(
6430 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6431 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006432 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006433 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006434 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006435 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006436 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006437 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006438 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006439 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006440 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006441 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006442 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006443 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006444 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006445 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006446 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006447 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006448 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006449 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006450 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006451 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006452 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006453 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006454 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006455 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006456 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006457 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006458 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006459 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006460 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006461 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006462 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006463 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006464 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006465 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006466 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006467 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006468 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006469 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006470 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006471 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006472 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006473 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006474 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006475 llvm_unreachable("Clause is not allowed.");
6476 }
6477 return Res;
6478}
6479
Alexey Bataev6402bca2015-12-28 07:25:51 +00006480static std::string
6481getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6482 ArrayRef<unsigned> Exclude = llvm::None) {
6483 std::string Values;
6484 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6485 unsigned Skipped = Exclude.size();
6486 auto S = Exclude.begin(), E = Exclude.end();
6487 for (unsigned i = First; i < Last; ++i) {
6488 if (std::find(S, E, i) != E) {
6489 --Skipped;
6490 continue;
6491 }
6492 Values += "'";
6493 Values += getOpenMPSimpleClauseTypeName(K, i);
6494 Values += "'";
6495 if (i == Bound - Skipped)
6496 Values += " or ";
6497 else if (i != Bound + 1 - Skipped)
6498 Values += ", ";
6499 }
6500 return Values;
6501}
6502
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006503OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6504 SourceLocation KindKwLoc,
6505 SourceLocation StartLoc,
6506 SourceLocation LParenLoc,
6507 SourceLocation EndLoc) {
6508 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006509 static_assert(OMPC_DEFAULT_unknown > 0,
6510 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006511 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006512 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6513 /*Last=*/OMPC_DEFAULT_unknown)
6514 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006515 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006516 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006517 switch (Kind) {
6518 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006519 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006520 break;
6521 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006522 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006523 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006524 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006525 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006526 break;
6527 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006528 return new (Context)
6529 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006530}
6531
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006532OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6533 SourceLocation KindKwLoc,
6534 SourceLocation StartLoc,
6535 SourceLocation LParenLoc,
6536 SourceLocation EndLoc) {
6537 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006538 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006539 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6540 /*Last=*/OMPC_PROC_BIND_unknown)
6541 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006542 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006543 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006544 return new (Context)
6545 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006546}
6547
Alexey Bataev56dafe82014-06-20 07:16:17 +00006548OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006549 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006550 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006551 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006552 SourceLocation EndLoc) {
6553 OMPClause *Res = nullptr;
6554 switch (Kind) {
6555 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006556 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6557 assert(Argument.size() == NumberOfElements &&
6558 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006559 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006560 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6561 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6562 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6563 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6564 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006565 break;
6566 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006567 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6568 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6569 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6570 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006571 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006572 case OMPC_dist_schedule:
6573 Res = ActOnOpenMPDistScheduleClause(
6574 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6575 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6576 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006577 case OMPC_defaultmap:
6578 enum { Modifier, DefaultmapKind };
6579 Res = ActOnOpenMPDefaultmapClause(
6580 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6581 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6582 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6583 ArgumentLoc[DefaultmapKind], EndLoc);
6584 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006585 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006586 case OMPC_num_threads:
6587 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006588 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006589 case OMPC_collapse:
6590 case OMPC_default:
6591 case OMPC_proc_bind:
6592 case OMPC_private:
6593 case OMPC_firstprivate:
6594 case OMPC_lastprivate:
6595 case OMPC_shared:
6596 case OMPC_reduction:
6597 case OMPC_linear:
6598 case OMPC_aligned:
6599 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006600 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006601 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006602 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006603 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006604 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006605 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006606 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006607 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006608 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006609 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006610 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006611 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006612 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006613 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006614 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006615 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006616 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006617 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006618 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006619 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006620 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006621 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006622 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006623 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006624 case OMPC_unknown:
6625 llvm_unreachable("Clause is not allowed.");
6626 }
6627 return Res;
6628}
6629
Alexey Bataev6402bca2015-12-28 07:25:51 +00006630static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6631 OpenMPScheduleClauseModifier M2,
6632 SourceLocation M1Loc, SourceLocation M2Loc) {
6633 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6634 SmallVector<unsigned, 2> Excluded;
6635 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6636 Excluded.push_back(M2);
6637 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6638 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6639 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6640 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6641 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6642 << getListOfPossibleValues(OMPC_schedule,
6643 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6644 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6645 Excluded)
6646 << getOpenMPClauseName(OMPC_schedule);
6647 return true;
6648 }
6649 return false;
6650}
6651
Alexey Bataev56dafe82014-06-20 07:16:17 +00006652OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006653 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006654 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006655 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6656 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6657 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6658 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6659 return nullptr;
6660 // OpenMP, 2.7.1, Loop Construct, Restrictions
6661 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6662 // but not both.
6663 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6664 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6665 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6666 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6667 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6668 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6669 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6670 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6671 return nullptr;
6672 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006673 if (Kind == OMPC_SCHEDULE_unknown) {
6674 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006675 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6676 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6677 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6678 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6679 Exclude);
6680 } else {
6681 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6682 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006683 }
6684 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6685 << Values << getOpenMPClauseName(OMPC_schedule);
6686 return nullptr;
6687 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006688 // OpenMP, 2.7.1, Loop Construct, Restrictions
6689 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6690 // schedule(guided).
6691 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6692 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6693 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6694 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6695 diag::err_omp_schedule_nonmonotonic_static);
6696 return nullptr;
6697 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006698 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006699 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006700 if (ChunkSize) {
6701 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6702 !ChunkSize->isInstantiationDependent() &&
6703 !ChunkSize->containsUnexpandedParameterPack()) {
6704 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6705 ExprResult Val =
6706 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6707 if (Val.isInvalid())
6708 return nullptr;
6709
6710 ValExpr = Val.get();
6711
6712 // OpenMP [2.7.1, Restrictions]
6713 // chunk_size must be a loop invariant integer expression with a positive
6714 // value.
6715 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006716 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6717 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6718 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006719 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006720 return nullptr;
6721 }
6722 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6723 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6724 ChunkSize->getType(), ".chunk.");
6725 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6726 ChunkSize->getExprLoc(),
6727 /*RefersToCapture=*/true);
6728 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006729 }
6730 }
6731 }
6732
Alexey Bataev6402bca2015-12-28 07:25:51 +00006733 return new (Context)
6734 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6735 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006736}
6737
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006738OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6739 SourceLocation StartLoc,
6740 SourceLocation EndLoc) {
6741 OMPClause *Res = nullptr;
6742 switch (Kind) {
6743 case OMPC_ordered:
6744 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6745 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006746 case OMPC_nowait:
6747 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6748 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006749 case OMPC_untied:
6750 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6751 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006752 case OMPC_mergeable:
6753 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6754 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006755 case OMPC_read:
6756 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6757 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006758 case OMPC_write:
6759 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6760 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006761 case OMPC_update:
6762 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6763 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006764 case OMPC_capture:
6765 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6766 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006767 case OMPC_seq_cst:
6768 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6769 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006770 case OMPC_threads:
6771 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6772 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006773 case OMPC_simd:
6774 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6775 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006776 case OMPC_nogroup:
6777 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6778 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006779 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006780 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006781 case OMPC_num_threads:
6782 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006783 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006784 case OMPC_collapse:
6785 case OMPC_schedule:
6786 case OMPC_private:
6787 case OMPC_firstprivate:
6788 case OMPC_lastprivate:
6789 case OMPC_shared:
6790 case OMPC_reduction:
6791 case OMPC_linear:
6792 case OMPC_aligned:
6793 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006794 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006795 case OMPC_default:
6796 case OMPC_proc_bind:
6797 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006798 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006799 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006800 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006801 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006802 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006803 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006804 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006805 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006806 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006807 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006808 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006809 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006810 case OMPC_unknown:
6811 llvm_unreachable("Clause is not allowed.");
6812 }
6813 return Res;
6814}
6815
Alexey Bataev236070f2014-06-20 11:19:47 +00006816OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6817 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006818 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006819 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6820}
6821
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006822OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6823 SourceLocation EndLoc) {
6824 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6825}
6826
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006827OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6828 SourceLocation EndLoc) {
6829 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6830}
6831
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006832OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6833 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006834 return new (Context) OMPReadClause(StartLoc, EndLoc);
6835}
6836
Alexey Bataevdea47612014-07-23 07:46:59 +00006837OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6838 SourceLocation EndLoc) {
6839 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6840}
6841
Alexey Bataev67a4f222014-07-23 10:25:33 +00006842OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6843 SourceLocation EndLoc) {
6844 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6845}
6846
Alexey Bataev459dec02014-07-24 06:46:57 +00006847OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6848 SourceLocation EndLoc) {
6849 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6850}
6851
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006852OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6853 SourceLocation EndLoc) {
6854 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6855}
6856
Alexey Bataev346265e2015-09-25 10:37:12 +00006857OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6858 SourceLocation EndLoc) {
6859 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6860}
6861
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006862OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6863 SourceLocation EndLoc) {
6864 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6865}
6866
Alexey Bataevb825de12015-12-07 10:51:44 +00006867OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6868 SourceLocation EndLoc) {
6869 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6870}
6871
Alexey Bataevc5e02582014-06-16 07:08:35 +00006872OMPClause *Sema::ActOnOpenMPVarListClause(
6873 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6874 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6875 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006876 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006877 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6878 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6879 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006880 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006881 switch (Kind) {
6882 case OMPC_private:
6883 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6884 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006885 case OMPC_firstprivate:
6886 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6887 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006888 case OMPC_lastprivate:
6889 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6890 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006891 case OMPC_shared:
6892 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6893 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006894 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006895 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6896 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006897 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006898 case OMPC_linear:
6899 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006900 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006901 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006902 case OMPC_aligned:
6903 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6904 ColonLoc, EndLoc);
6905 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006906 case OMPC_copyin:
6907 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6908 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006909 case OMPC_copyprivate:
6910 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6911 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006912 case OMPC_flush:
6913 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6914 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006915 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006916 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6917 StartLoc, LParenLoc, EndLoc);
6918 break;
6919 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006920 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6921 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6922 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006923 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006924 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006925 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006926 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006927 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006928 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006929 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006930 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006931 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006932 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006933 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006934 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006935 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006936 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006937 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006938 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006939 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006940 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006941 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006942 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006943 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006944 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006945 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006946 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006947 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006948 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006949 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006950 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006951 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006952 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006953 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006954 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006955 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006956 llvm_unreachable("Clause is not allowed.");
6957 }
6958 return Res;
6959}
6960
6961OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6962 SourceLocation StartLoc,
6963 SourceLocation LParenLoc,
6964 SourceLocation EndLoc) {
6965 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006966 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006967 for (auto &RefExpr : VarList) {
6968 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006969 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6970 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006971 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006972 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006973 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006974 continue;
6975 }
6976
Alexey Bataeved09d242014-05-28 05:53:51 +00006977 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00006978 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006979 // A list item is a variable name.
6980 // OpenMP [2.9.3.3, Restrictions, p.1]
6981 // A variable that is part of another variable (as an array or
6982 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006983 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
6984 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
6985 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
6986 (getCurrentThisType().isNull() || !ME ||
6987 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
6988 !isa<FieldDecl>(ME->getMemberDecl()))) {
6989 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
6990 << (getCurrentThisType().isNull() ? 0 : 1)
6991 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006992 continue;
6993 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006994 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
6995 QualType Type = D->getType();
6996 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006997
6998 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6999 // A variable that appears in a private clause must not have an incomplete
7000 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007001 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007002 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007003 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007004
Alexey Bataev758e55e2013-09-06 18:03:48 +00007005 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7006 // in a Construct]
7007 // Variables with the predetermined data-sharing attributes may not be
7008 // listed in data-sharing attributes clauses, except for the cases
7009 // listed below. For these exceptions only, listing a predetermined
7010 // variable in a data-sharing attribute clause is allowed and overrides
7011 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007012 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007013 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007014 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7015 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007016 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007017 continue;
7018 }
7019
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007020 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007021 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007022 DSAStack->getCurrentDirective() == OMPD_task) {
7023 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7024 << getOpenMPClauseName(OMPC_private) << Type
7025 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7026 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007027 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007028 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007029 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007030 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007031 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007032 continue;
7033 }
7034
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007035 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7036 // A variable of class type (or array thereof) that appears in a private
7037 // clause requires an accessible, unambiguous default constructor for the
7038 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007039 // Generate helper private variable and initialize it with the default
7040 // value. The address of the original variable is replaced by the address of
7041 // the new private variable in CodeGen. This new variable is not added to
7042 // IdResolver, so the code in the OpenMP region uses original variable for
7043 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007044 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007045 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7046 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007047 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007048 if (VDPrivate->isInvalidDecl())
7049 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007050 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007051 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007052
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007053 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private);
7054 Vars.push_back(RefExpr->IgnoreParens());
Alexey Bataev03b340a2014-10-21 03:16:40 +00007055 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007056 }
7057
Alexey Bataeved09d242014-05-28 05:53:51 +00007058 if (Vars.empty())
7059 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007060
Alexey Bataev03b340a2014-10-21 03:16:40 +00007061 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7062 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007063}
7064
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007065namespace {
7066class DiagsUninitializedSeveretyRAII {
7067private:
7068 DiagnosticsEngine &Diags;
7069 SourceLocation SavedLoc;
7070 bool IsIgnored;
7071
7072public:
7073 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7074 bool IsIgnored)
7075 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7076 if (!IsIgnored) {
7077 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7078 /*Map*/ diag::Severity::Ignored, Loc);
7079 }
7080 }
7081 ~DiagsUninitializedSeveretyRAII() {
7082 if (!IsIgnored)
7083 Diags.popMappings(SavedLoc);
7084 }
7085};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007086}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007087
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007088OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7089 SourceLocation StartLoc,
7090 SourceLocation LParenLoc,
7091 SourceLocation EndLoc) {
7092 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007093 SmallVector<Expr *, 8> PrivateCopies;
7094 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007095 bool IsImplicitClause =
7096 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7097 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7098
Alexey Bataeved09d242014-05-28 05:53:51 +00007099 for (auto &RefExpr : VarList) {
7100 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
7101 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007102 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007103 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007104 PrivateCopies.push_back(nullptr);
7105 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007106 continue;
7107 }
7108
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007109 SourceLocation ELoc =
7110 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007111 // OpenMP [2.1, C/C++]
7112 // A list item is a variable name.
7113 // OpenMP [2.9.3.3, Restrictions, p.1]
7114 // A variable that is part of another variable (as an array or
7115 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007116 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007117 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007118 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7119 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007120 continue;
7121 }
7122 Decl *D = DE->getDecl();
7123 VarDecl *VD = cast<VarDecl>(D);
7124
7125 QualType Type = VD->getType();
7126 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7127 // It will be analyzed later.
7128 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007129 PrivateCopies.push_back(nullptr);
7130 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007131 continue;
7132 }
7133
7134 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7135 // A variable that appears in a private clause must not have an incomplete
7136 // type or a reference type.
7137 if (RequireCompleteType(ELoc, Type,
7138 diag::err_omp_firstprivate_incomplete_type)) {
7139 continue;
7140 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007141 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007142
7143 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7144 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007145 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007146 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007147 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007148
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007149 // If an implicit firstprivate variable found it was checked already.
7150 if (!IsImplicitClause) {
7151 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007152 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007153 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7154 // A list item that specifies a given variable may not appear in more
7155 // than one clause on the same directive, except that a variable may be
7156 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007157 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007158 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007159 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007160 << getOpenMPClauseName(DVar.CKind)
7161 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007162 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007163 continue;
7164 }
7165
7166 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7167 // in a Construct]
7168 // Variables with the predetermined data-sharing attributes may not be
7169 // listed in data-sharing attributes clauses, except for the cases
7170 // listed below. For these exceptions only, listing a predetermined
7171 // variable in a data-sharing attribute clause is allowed and overrides
7172 // the variable's predetermined data-sharing attributes.
7173 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7174 // in a Construct, C/C++, p.2]
7175 // Variables with const-qualified type having no mutable member may be
7176 // listed in a firstprivate clause, even if they are static data members.
7177 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7178 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7179 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007180 << getOpenMPClauseName(DVar.CKind)
7181 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007182 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007183 continue;
7184 }
7185
Alexey Bataevf29276e2014-06-18 04:14:57 +00007186 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007187 // OpenMP [2.9.3.4, Restrictions, p.2]
7188 // A list item that is private within a parallel region must not appear
7189 // in a firstprivate clause on a worksharing construct if any of the
7190 // worksharing regions arising from the worksharing construct ever bind
7191 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007192 if (isOpenMPWorksharingDirective(CurrDir) &&
7193 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007194 DVar = DSAStack->getImplicitDSA(VD, true);
7195 if (DVar.CKind != OMPC_shared &&
7196 (isOpenMPParallelDirective(DVar.DKind) ||
7197 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007198 Diag(ELoc, diag::err_omp_required_access)
7199 << getOpenMPClauseName(OMPC_firstprivate)
7200 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007201 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007202 continue;
7203 }
7204 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007205 // OpenMP [2.9.3.4, Restrictions, p.3]
7206 // A list item that appears in a reduction clause of a parallel construct
7207 // must not appear in a firstprivate clause on a worksharing or task
7208 // construct if any of the worksharing or task regions arising from the
7209 // worksharing or task construct ever bind to any of the parallel regions
7210 // arising from the parallel construct.
7211 // OpenMP [2.9.3.4, Restrictions, p.4]
7212 // A list item that appears in a reduction clause in worksharing
7213 // construct must not appear in a firstprivate clause in a task construct
7214 // encountered during execution of any of the worksharing regions arising
7215 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007216 if (CurrDir == OMPD_task) {
7217 DVar =
7218 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7219 [](OpenMPDirectiveKind K) -> bool {
7220 return isOpenMPParallelDirective(K) ||
7221 isOpenMPWorksharingDirective(K);
7222 },
7223 false);
7224 if (DVar.CKind == OMPC_reduction &&
7225 (isOpenMPParallelDirective(DVar.DKind) ||
7226 isOpenMPWorksharingDirective(DVar.DKind))) {
7227 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7228 << getOpenMPDirectiveName(DVar.DKind);
7229 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7230 continue;
7231 }
7232 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007233
7234 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7235 // A list item that is private within a teams region must not appear in a
7236 // firstprivate clause on a distribute construct if any of the distribute
7237 // regions arising from the distribute construct ever bind to any of the
7238 // teams regions arising from the teams construct.
7239 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7240 // A list item that appears in a reduction clause of a teams construct
7241 // must not appear in a firstprivate clause on a distribute construct if
7242 // any of the distribute regions arising from the distribute construct
7243 // ever bind to any of the teams regions arising from the teams construct.
7244 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7245 // A list item may appear in a firstprivate or lastprivate clause but not
7246 // both.
7247 if (CurrDir == OMPD_distribute) {
7248 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7249 [](OpenMPDirectiveKind K) -> bool {
7250 return isOpenMPTeamsDirective(K);
7251 },
7252 false);
7253 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7254 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7255 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7256 continue;
7257 }
7258 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7259 [](OpenMPDirectiveKind K) -> bool {
7260 return isOpenMPTeamsDirective(K);
7261 },
7262 false);
7263 if (DVar.CKind == OMPC_reduction &&
7264 isOpenMPTeamsDirective(DVar.DKind)) {
7265 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7266 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7267 continue;
7268 }
7269 DVar = DSAStack->getTopDSA(VD, false);
7270 if (DVar.CKind == OMPC_lastprivate) {
7271 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7272 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7273 continue;
7274 }
7275 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007276 }
7277
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007278 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007279 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007280 DSAStack->getCurrentDirective() == OMPD_task) {
7281 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7282 << getOpenMPClauseName(OMPC_firstprivate) << Type
7283 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7284 bool IsDecl =
7285 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7286 Diag(VD->getLocation(),
7287 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7288 << VD;
7289 continue;
7290 }
7291
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007292 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007293 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7294 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007295 // Generate helper private variable and initialize it with the value of the
7296 // original variable. The address of the original variable is replaced by
7297 // the address of the new private variable in the CodeGen. This new variable
7298 // is not added to IdResolver, so the code in the OpenMP region uses
7299 // original variable for proper diagnostics and variable capturing.
7300 Expr *VDInitRefExpr = nullptr;
7301 // For arrays generate initializer for single element and replace it by the
7302 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007303 if (Type->isArrayType()) {
7304 auto VDInit =
7305 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7306 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007307 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007308 ElemType = ElemType.getUnqualifiedType();
7309 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7310 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007311 InitializedEntity Entity =
7312 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007313 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7314
7315 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7316 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7317 if (Result.isInvalid())
7318 VDPrivate->setInvalidDecl();
7319 else
7320 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007321 // Remove temp variable declaration.
7322 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007323 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007324 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007325 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007326 VDInitRefExpr =
7327 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007328 AddInitializerToDecl(VDPrivate,
7329 DefaultLvalueConversion(VDInitRefExpr).get(),
7330 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007331 }
7332 if (VDPrivate->isInvalidDecl()) {
7333 if (IsImplicitClause) {
7334 Diag(DE->getExprLoc(),
7335 diag::note_omp_task_predetermined_firstprivate_here);
7336 }
7337 continue;
7338 }
7339 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007340 auto VDPrivateRefExpr = buildDeclRefExpr(
7341 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007342 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7343 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007344 PrivateCopies.push_back(VDPrivateRefExpr);
7345 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007346 }
7347
Alexey Bataeved09d242014-05-28 05:53:51 +00007348 if (Vars.empty())
7349 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007350
7351 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007352 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007353}
7354
Alexander Musman1bb328c2014-06-04 13:06:39 +00007355OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7356 SourceLocation StartLoc,
7357 SourceLocation LParenLoc,
7358 SourceLocation EndLoc) {
7359 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007360 SmallVector<Expr *, 8> SrcExprs;
7361 SmallVector<Expr *, 8> DstExprs;
7362 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007363 for (auto &RefExpr : VarList) {
7364 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7365 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7366 // It will be analyzed later.
7367 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007368 SrcExprs.push_back(nullptr);
7369 DstExprs.push_back(nullptr);
7370 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007371 continue;
7372 }
7373
7374 SourceLocation ELoc = RefExpr->getExprLoc();
7375 // OpenMP [2.1, C/C++]
7376 // A list item is a variable name.
7377 // OpenMP [2.14.3.5, Restrictions, p.1]
7378 // A variable that is part of another variable (as an array or structure
7379 // element) cannot appear in a lastprivate clause.
7380 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7381 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007382 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7383 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007384 continue;
7385 }
7386 Decl *D = DE->getDecl();
7387 VarDecl *VD = cast<VarDecl>(D);
7388
7389 QualType Type = VD->getType();
7390 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7391 // It will be analyzed later.
7392 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007393 SrcExprs.push_back(nullptr);
7394 DstExprs.push_back(nullptr);
7395 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007396 continue;
7397 }
7398
7399 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7400 // A variable that appears in a lastprivate clause must not have an
7401 // incomplete type or a reference type.
7402 if (RequireCompleteType(ELoc, Type,
7403 diag::err_omp_lastprivate_incomplete_type)) {
7404 continue;
7405 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007406 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007407
7408 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7409 // in a Construct]
7410 // Variables with the predetermined data-sharing attributes may not be
7411 // listed in data-sharing attributes clauses, except for the cases
7412 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007413 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007414 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7415 DVar.CKind != OMPC_firstprivate &&
7416 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7417 Diag(ELoc, diag::err_omp_wrong_dsa)
7418 << getOpenMPClauseName(DVar.CKind)
7419 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007420 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007421 continue;
7422 }
7423
Alexey Bataevf29276e2014-06-18 04:14:57 +00007424 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7425 // OpenMP [2.14.3.5, Restrictions, p.2]
7426 // A list item that is private within a parallel region, or that appears in
7427 // the reduction clause of a parallel construct, must not appear in a
7428 // lastprivate clause on a worksharing construct if any of the corresponding
7429 // worksharing regions ever binds to any of the corresponding parallel
7430 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007431 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007432 if (isOpenMPWorksharingDirective(CurrDir) &&
7433 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007434 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007435 if (DVar.CKind != OMPC_shared) {
7436 Diag(ELoc, diag::err_omp_required_access)
7437 << getOpenMPClauseName(OMPC_lastprivate)
7438 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007439 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007440 continue;
7441 }
7442 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007443 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007444 // A variable of class type (or array thereof) that appears in a
7445 // lastprivate clause requires an accessible, unambiguous default
7446 // constructor for the class type, unless the list item is also specified
7447 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007448 // A variable of class type (or array thereof) that appears in a
7449 // lastprivate clause requires an accessible, unambiguous copy assignment
7450 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007451 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007452 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007453 Type.getUnqualifiedType(), ".lastprivate.src",
7454 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007455 auto *PseudoSrcExpr = buildDeclRefExpr(
7456 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007457 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007458 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7459 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007460 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007461 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007462 // For arrays generate assignment operation for single element and replace
7463 // it by the original array element in CodeGen.
7464 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7465 PseudoDstExpr, PseudoSrcExpr);
7466 if (AssignmentOp.isInvalid())
7467 continue;
7468 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7469 /*DiscardedValue=*/true);
7470 if (AssignmentOp.isInvalid())
7471 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007472
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007473 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7474 // A list item may appear in a firstprivate or lastprivate clause but not
7475 // both.
7476 if (CurrDir == OMPD_distribute) {
7477 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7478 if (DVar.CKind == OMPC_firstprivate) {
7479 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7480 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7481 continue;
7482 }
7483 }
7484
Alexey Bataev39f915b82015-05-08 10:41:21 +00007485 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007486 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007487 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007488 SrcExprs.push_back(PseudoSrcExpr);
7489 DstExprs.push_back(PseudoDstExpr);
7490 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007491 }
7492
7493 if (Vars.empty())
7494 return nullptr;
7495
7496 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007497 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007498}
7499
Alexey Bataev758e55e2013-09-06 18:03:48 +00007500OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7501 SourceLocation StartLoc,
7502 SourceLocation LParenLoc,
7503 SourceLocation EndLoc) {
7504 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007505 for (auto &RefExpr : VarList) {
7506 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7507 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007508 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007509 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007510 continue;
7511 }
7512
Alexey Bataeved09d242014-05-28 05:53:51 +00007513 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007514 // OpenMP [2.1, C/C++]
7515 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007516 // OpenMP [2.14.3.2, Restrictions, p.1]
7517 // A variable that is part of another variable (as an array or structure
7518 // element) cannot appear in a shared unless it is a static data member
7519 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007520 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007521 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007522 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7523 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007524 continue;
7525 }
7526 Decl *D = DE->getDecl();
7527 VarDecl *VD = cast<VarDecl>(D);
7528
7529 QualType Type = VD->getType();
7530 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7531 // It will be analyzed later.
7532 Vars.push_back(DE);
7533 continue;
7534 }
7535
7536 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7537 // in a Construct]
7538 // Variables with the predetermined data-sharing attributes may not be
7539 // listed in data-sharing attributes clauses, except for the cases
7540 // listed below. For these exceptions only, listing a predetermined
7541 // variable in a data-sharing attribute clause is allowed and overrides
7542 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007543 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007544 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7545 DVar.RefExpr) {
7546 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7547 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007548 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007549 continue;
7550 }
7551
7552 DSAStack->addDSA(VD, DE, OMPC_shared);
7553 Vars.push_back(DE);
7554 }
7555
Alexey Bataeved09d242014-05-28 05:53:51 +00007556 if (Vars.empty())
7557 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007558
7559 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7560}
7561
Alexey Bataevc5e02582014-06-16 07:08:35 +00007562namespace {
7563class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7564 DSAStackTy *Stack;
7565
7566public:
7567 bool VisitDeclRefExpr(DeclRefExpr *E) {
7568 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007569 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007570 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7571 return false;
7572 if (DVar.CKind != OMPC_unknown)
7573 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007574 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007575 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007576 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007577 return true;
7578 return false;
7579 }
7580 return false;
7581 }
7582 bool VisitStmt(Stmt *S) {
7583 for (auto Child : S->children()) {
7584 if (Child && Visit(Child))
7585 return true;
7586 }
7587 return false;
7588 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007589 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007590};
Alexey Bataev23b69422014-06-18 07:08:49 +00007591} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007592
7593OMPClause *Sema::ActOnOpenMPReductionClause(
7594 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7595 SourceLocation ColonLoc, SourceLocation EndLoc,
7596 CXXScopeSpec &ReductionIdScopeSpec,
7597 const DeclarationNameInfo &ReductionId) {
7598 // TODO: Allow scope specification search when 'declare reduction' is
7599 // supported.
7600 assert(ReductionIdScopeSpec.isEmpty() &&
7601 "No support for scoped reduction identifiers yet.");
7602
7603 auto DN = ReductionId.getName();
7604 auto OOK = DN.getCXXOverloadedOperator();
7605 BinaryOperatorKind BOK = BO_Comma;
7606
7607 // OpenMP [2.14.3.6, reduction clause]
7608 // C
7609 // reduction-identifier is either an identifier or one of the following
7610 // operators: +, -, *, &, |, ^, && and ||
7611 // C++
7612 // reduction-identifier is either an id-expression or one of the following
7613 // operators: +, -, *, &, |, ^, && and ||
7614 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7615 switch (OOK) {
7616 case OO_Plus:
7617 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007618 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007619 break;
7620 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007621 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007622 break;
7623 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007624 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007625 break;
7626 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007627 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007628 break;
7629 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007630 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007631 break;
7632 case OO_AmpAmp:
7633 BOK = BO_LAnd;
7634 break;
7635 case OO_PipePipe:
7636 BOK = BO_LOr;
7637 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007638 case OO_New:
7639 case OO_Delete:
7640 case OO_Array_New:
7641 case OO_Array_Delete:
7642 case OO_Slash:
7643 case OO_Percent:
7644 case OO_Tilde:
7645 case OO_Exclaim:
7646 case OO_Equal:
7647 case OO_Less:
7648 case OO_Greater:
7649 case OO_LessEqual:
7650 case OO_GreaterEqual:
7651 case OO_PlusEqual:
7652 case OO_MinusEqual:
7653 case OO_StarEqual:
7654 case OO_SlashEqual:
7655 case OO_PercentEqual:
7656 case OO_CaretEqual:
7657 case OO_AmpEqual:
7658 case OO_PipeEqual:
7659 case OO_LessLess:
7660 case OO_GreaterGreater:
7661 case OO_LessLessEqual:
7662 case OO_GreaterGreaterEqual:
7663 case OO_EqualEqual:
7664 case OO_ExclaimEqual:
7665 case OO_PlusPlus:
7666 case OO_MinusMinus:
7667 case OO_Comma:
7668 case OO_ArrowStar:
7669 case OO_Arrow:
7670 case OO_Call:
7671 case OO_Subscript:
7672 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007673 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007674 case NUM_OVERLOADED_OPERATORS:
7675 llvm_unreachable("Unexpected reduction identifier");
7676 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007677 if (auto II = DN.getAsIdentifierInfo()) {
7678 if (II->isStr("max"))
7679 BOK = BO_GT;
7680 else if (II->isStr("min"))
7681 BOK = BO_LT;
7682 }
7683 break;
7684 }
7685 SourceRange ReductionIdRange;
7686 if (ReductionIdScopeSpec.isValid()) {
7687 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7688 }
7689 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7690 if (BOK == BO_Comma) {
7691 // Not allowed reduction identifier is found.
7692 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7693 << ReductionIdRange;
7694 return nullptr;
7695 }
7696
7697 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007698 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007699 SmallVector<Expr *, 8> LHSs;
7700 SmallVector<Expr *, 8> RHSs;
7701 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007702 for (auto RefExpr : VarList) {
7703 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7704 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7705 // It will be analyzed later.
7706 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007707 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007708 LHSs.push_back(nullptr);
7709 RHSs.push_back(nullptr);
7710 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007711 continue;
7712 }
7713
7714 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7715 RefExpr->isInstantiationDependent() ||
7716 RefExpr->containsUnexpandedParameterPack()) {
7717 // It will be analyzed later.
7718 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007719 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007720 LHSs.push_back(nullptr);
7721 RHSs.push_back(nullptr);
7722 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007723 continue;
7724 }
7725
7726 auto ELoc = RefExpr->getExprLoc();
7727 auto ERange = RefExpr->getSourceRange();
7728 // OpenMP [2.1, C/C++]
7729 // A list item is a variable or array section, subject to the restrictions
7730 // specified in Section 2.4 on page 42 and in each of the sections
7731 // describing clauses and directives for which a list appears.
7732 // OpenMP [2.14.3.3, Restrictions, p.1]
7733 // A variable that is part of another variable (as an array or
7734 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007735 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7736 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7737 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7738 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007739 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7740 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007741 continue;
7742 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007743 QualType Type;
7744 VarDecl *VD = nullptr;
7745 if (DE) {
7746 auto D = DE->getDecl();
7747 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007748 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007749 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007750 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007751 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7752 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7753 Base = TempASE->getBase()->IgnoreParenImpCasts();
7754 DE = dyn_cast<DeclRefExpr>(Base);
7755 if (DE)
7756 VD = dyn_cast<VarDecl>(DE->getDecl());
7757 if (!VD) {
7758 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7759 << 0 << Base->getSourceRange();
7760 continue;
7761 }
7762 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007763 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7764 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7765 Type = ATy->getElementType();
7766 else
7767 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007768 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007769 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7770 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7771 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7772 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7773 Base = TempASE->getBase()->IgnoreParenImpCasts();
7774 DE = dyn_cast<DeclRefExpr>(Base);
7775 if (DE)
7776 VD = dyn_cast<VarDecl>(DE->getDecl());
7777 if (!VD) {
7778 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7779 << 1 << Base->getSourceRange();
7780 continue;
7781 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007782 }
7783
Alexey Bataevc5e02582014-06-16 07:08:35 +00007784 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7785 // A variable that appears in a private clause must not have an incomplete
7786 // type or a reference type.
7787 if (RequireCompleteType(ELoc, Type,
7788 diag::err_omp_reduction_incomplete_type))
7789 continue;
7790 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007791 // A list item that appears in a reduction clause must not be
7792 // const-qualified.
7793 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007794 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007795 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007796 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007797 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7798 VarDecl::DeclarationOnly;
7799 Diag(VD->getLocation(),
7800 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7801 << VD;
7802 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007803 continue;
7804 }
7805 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7806 // If a list-item is a reference type then it must bind to the same object
7807 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007808 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007809 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007810 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007811 DSARefChecker Check(DSAStack);
7812 if (Check.Visit(VDDef->getInit())) {
7813 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7814 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7815 continue;
7816 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007817 }
7818 }
7819 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7820 // The type of a list item that appears in a reduction clause must be valid
7821 // for the reduction-identifier. For a max or min reduction in C, the type
7822 // of the list item must be an allowed arithmetic data type: char, int,
7823 // float, double, or _Bool, possibly modified with long, short, signed, or
7824 // unsigned. For a max or min reduction in C++, the type of the list item
7825 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7826 // double, or bool, possibly modified with long, short, signed, or unsigned.
7827 if ((BOK == BO_GT || BOK == BO_LT) &&
7828 !(Type->isScalarType() ||
7829 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7830 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7831 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007832 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007833 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7834 VarDecl::DeclarationOnly;
7835 Diag(VD->getLocation(),
7836 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7837 << VD;
7838 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007839 continue;
7840 }
7841 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7842 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7843 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007844 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007845 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7846 VarDecl::DeclarationOnly;
7847 Diag(VD->getLocation(),
7848 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7849 << VD;
7850 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007851 continue;
7852 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007853 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7854 // in a Construct]
7855 // Variables with the predetermined data-sharing attributes may not be
7856 // listed in data-sharing attributes clauses, except for the cases
7857 // listed below. For these exceptions only, listing a predetermined
7858 // variable in a data-sharing attribute clause is allowed and overrides
7859 // the variable's predetermined data-sharing attributes.
7860 // OpenMP [2.14.3.6, Restrictions, p.3]
7861 // Any number of reduction clauses can be specified on the directive,
7862 // but a list item can appear only once in the reduction clauses for that
7863 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007864 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007865 DVar = DSAStack->getTopDSA(VD, false);
7866 if (DVar.CKind == OMPC_reduction) {
7867 Diag(ELoc, diag::err_omp_once_referenced)
7868 << getOpenMPClauseName(OMPC_reduction);
7869 if (DVar.RefExpr) {
7870 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007871 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007872 } else if (DVar.CKind != OMPC_unknown) {
7873 Diag(ELoc, diag::err_omp_wrong_dsa)
7874 << getOpenMPClauseName(DVar.CKind)
7875 << getOpenMPClauseName(OMPC_reduction);
7876 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7877 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007878 }
7879
7880 // OpenMP [2.14.3.6, Restrictions, p.1]
7881 // A list item that appears in a reduction clause of a worksharing
7882 // construct must be shared in the parallel regions to which any of the
7883 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007884 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7885 if (isOpenMPWorksharingDirective(CurrDir) &&
7886 !isOpenMPParallelDirective(CurrDir)) {
7887 DVar = DSAStack->getImplicitDSA(VD, true);
7888 if (DVar.CKind != OMPC_shared) {
7889 Diag(ELoc, diag::err_omp_required_access)
7890 << getOpenMPClauseName(OMPC_reduction)
7891 << getOpenMPClauseName(OMPC_shared);
7892 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7893 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007894 }
7895 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007896
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007897 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007898 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7899 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7900 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7901 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7902 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007903 if (OASE ||
7904 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7905 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007906 // Create pseudo array type for private copy. The size for this array will
7907 // be generated during codegen.
7908 // For array subscripts or single variables Private Ty is the same as Type
7909 // (type of the variable or single array element).
7910 PrivateTy = Context.getVariableArrayType(
7911 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7912 Context.getSizeType(), VK_RValue),
7913 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007914 } else if (DE &&
7915 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7916 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007917 // Private copy.
7918 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7919 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007920 // Add initializer for private variable.
7921 Expr *Init = nullptr;
7922 switch (BOK) {
7923 case BO_Add:
7924 case BO_Xor:
7925 case BO_Or:
7926 case BO_LOr:
7927 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7928 if (Type->isScalarType() || Type->isAnyComplexType()) {
7929 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007930 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007931 break;
7932 case BO_Mul:
7933 case BO_LAnd:
7934 if (Type->isScalarType() || Type->isAnyComplexType()) {
7935 // '*' and '&&' reduction ops - initializer is '1'.
7936 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7937 }
7938 break;
7939 case BO_And: {
7940 // '&' reduction op - initializer is '~0'.
7941 QualType OrigType = Type;
7942 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7943 Type = ComplexTy->getElementType();
7944 }
7945 if (Type->isRealFloatingType()) {
7946 llvm::APFloat InitValue =
7947 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7948 /*isIEEE=*/true);
7949 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7950 Type, ELoc);
7951 } else if (Type->isScalarType()) {
7952 auto Size = Context.getTypeSize(Type);
7953 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7954 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7955 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7956 }
7957 if (Init && OrigType->isAnyComplexType()) {
7958 // Init = 0xFFFF + 0xFFFFi;
7959 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7960 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7961 }
7962 Type = OrigType;
7963 break;
7964 }
7965 case BO_LT:
7966 case BO_GT: {
7967 // 'min' reduction op - initializer is 'Largest representable number in
7968 // the reduction list item type'.
7969 // 'max' reduction op - initializer is 'Least representable number in
7970 // the reduction list item type'.
7971 if (Type->isIntegerType() || Type->isPointerType()) {
7972 bool IsSigned = Type->hasSignedIntegerRepresentation();
7973 auto Size = Context.getTypeSize(Type);
7974 QualType IntTy =
7975 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7976 llvm::APInt InitValue =
7977 (BOK != BO_LT)
7978 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7979 : llvm::APInt::getMinValue(Size)
7980 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7981 : llvm::APInt::getMaxValue(Size);
7982 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7983 if (Type->isPointerType()) {
7984 // Cast to pointer type.
7985 auto CastExpr = BuildCStyleCastExpr(
7986 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7987 SourceLocation(), Init);
7988 if (CastExpr.isInvalid())
7989 continue;
7990 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007991 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007992 } else if (Type->isRealFloatingType()) {
7993 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7994 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7995 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7996 Type, ELoc);
7997 }
7998 break;
7999 }
8000 case BO_PtrMemD:
8001 case BO_PtrMemI:
8002 case BO_MulAssign:
8003 case BO_Div:
8004 case BO_Rem:
8005 case BO_Sub:
8006 case BO_Shl:
8007 case BO_Shr:
8008 case BO_LE:
8009 case BO_GE:
8010 case BO_EQ:
8011 case BO_NE:
8012 case BO_AndAssign:
8013 case BO_XorAssign:
8014 case BO_OrAssign:
8015 case BO_Assign:
8016 case BO_AddAssign:
8017 case BO_SubAssign:
8018 case BO_DivAssign:
8019 case BO_RemAssign:
8020 case BO_ShlAssign:
8021 case BO_ShrAssign:
8022 case BO_Comma:
8023 llvm_unreachable("Unexpected reduction operation");
8024 }
8025 if (Init) {
8026 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8027 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008028 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008029 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008030 if (!RHSVD->hasInit()) {
8031 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8032 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008033 if (VD) {
8034 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8035 VarDecl::DeclarationOnly;
8036 Diag(VD->getLocation(),
8037 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8038 << VD;
8039 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008040 continue;
8041 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008042 // Store initializer for single element in private copy. Will be used during
8043 // codegen.
8044 PrivateVD->setInit(RHSVD->getInit());
8045 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008046 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8047 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008048 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008049 ExprResult ReductionOp =
8050 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8051 LHSDRE, RHSDRE);
8052 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008053 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008054 ReductionOp =
8055 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8056 BO_Assign, LHSDRE, ReductionOp.get());
8057 } else {
8058 auto *ConditionalOp = new (Context) ConditionalOperator(
8059 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8060 RHSDRE, Type, VK_LValue, OK_Ordinary);
8061 ReductionOp =
8062 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8063 BO_Assign, LHSDRE, ConditionalOp);
8064 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008065 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008066 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008067 if (ReductionOp.isInvalid())
8068 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008069
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008070 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008071 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008072 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008073 LHSs.push_back(LHSDRE);
8074 RHSs.push_back(RHSDRE);
8075 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008076 }
8077
8078 if (Vars.empty())
8079 return nullptr;
8080
8081 return OMPReductionClause::Create(
8082 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008083 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8084 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008085}
8086
Alexey Bataev182227b2015-08-20 10:54:39 +00008087OMPClause *Sema::ActOnOpenMPLinearClause(
8088 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8089 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8090 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008091 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008092 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008093 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008094 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8095 LinKind == OMPC_LINEAR_unknown) {
8096 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8097 LinKind = OMPC_LINEAR_val;
8098 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008099 for (auto &RefExpr : VarList) {
8100 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8101 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008102 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008103 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008104 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008105 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008106 continue;
8107 }
8108
8109 // OpenMP [2.14.3.7, linear clause]
8110 // A list item that appears in a linear clause is subject to the private
8111 // clause semantics described in Section 2.14.3.3 on page 159 except as
8112 // noted. In addition, the value of the new list item on each iteration
8113 // of the associated loop(s) corresponds to the value of the original
8114 // list item before entering the construct plus the logical number of
8115 // the iteration times linear-step.
8116
Alexey Bataeved09d242014-05-28 05:53:51 +00008117 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008118 // OpenMP [2.1, C/C++]
8119 // A list item is a variable name.
8120 // OpenMP [2.14.3.3, Restrictions, p.1]
8121 // A variable that is part of another variable (as an array or
8122 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008123 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008124 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008125 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8126 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008127 continue;
8128 }
8129
8130 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8131
8132 // OpenMP [2.14.3.7, linear clause]
8133 // A list-item cannot appear in more than one linear clause.
8134 // A list-item that appears in a linear clause cannot appear in any
8135 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008136 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008137 if (DVar.RefExpr) {
8138 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8139 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008140 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008141 continue;
8142 }
8143
8144 QualType QType = VD->getType();
8145 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8146 // It will be analyzed later.
8147 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008148 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008149 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008150 continue;
8151 }
8152
8153 // A variable must not have an incomplete type or a reference type.
8154 if (RequireCompleteType(ELoc, QType,
8155 diag::err_omp_linear_incomplete_type)) {
8156 continue;
8157 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008158 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8159 !QType->isReferenceType()) {
8160 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8161 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8162 continue;
8163 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008164 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008165
8166 // A list item must not be const-qualified.
8167 if (QType.isConstant(Context)) {
8168 Diag(ELoc, diag::err_omp_const_variable)
8169 << getOpenMPClauseName(OMPC_linear);
8170 bool IsDecl =
8171 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8172 Diag(VD->getLocation(),
8173 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8174 << VD;
8175 continue;
8176 }
8177
8178 // A list item must be of integral or pointer type.
8179 QType = QType.getUnqualifiedType().getCanonicalType();
8180 const Type *Ty = QType.getTypePtrOrNull();
8181 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8182 !Ty->isPointerType())) {
8183 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8184 bool IsDecl =
8185 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8186 Diag(VD->getLocation(),
8187 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8188 << VD;
8189 continue;
8190 }
8191
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008192 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008193 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8194 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008195 auto *PrivateRef = buildDeclRefExpr(
8196 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008197 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008198 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008199 Expr *InitExpr;
8200 if (LinKind == OMPC_LINEAR_uval)
8201 InitExpr = VD->getInit();
8202 else
8203 InitExpr = DE;
8204 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008205 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008206 auto InitRef = buildDeclRefExpr(
8207 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008208 DSAStack->addDSA(VD, DE, OMPC_linear);
8209 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008210 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008211 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008212 }
8213
8214 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008215 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008216
8217 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008218 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008219 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8220 !Step->isInstantiationDependent() &&
8221 !Step->containsUnexpandedParameterPack()) {
8222 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008223 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008224 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008225 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008226 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008227
Alexander Musman3276a272015-03-21 10:12:56 +00008228 // Build var to save the step value.
8229 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008230 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008231 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008232 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008233 ExprResult CalcStep =
8234 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008235 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008236
Alexander Musman8dba6642014-04-22 13:09:42 +00008237 // Warn about zero linear step (it would be probably better specified as
8238 // making corresponding variables 'const').
8239 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008240 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8241 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008242 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8243 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008244 if (!IsConstant && CalcStep.isUsable()) {
8245 // Calculate the step beforehand instead of doing this on each iteration.
8246 // (This is not used if the number of iterations may be kfold-ed).
8247 CalcStepExpr = CalcStep.get();
8248 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008249 }
8250
Alexey Bataev182227b2015-08-20 10:54:39 +00008251 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8252 ColonLoc, EndLoc, Vars, Privates, Inits,
8253 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008254}
8255
8256static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8257 Expr *NumIterations, Sema &SemaRef,
8258 Scope *S) {
8259 // Walk the vars and build update/final expressions for the CodeGen.
8260 SmallVector<Expr *, 8> Updates;
8261 SmallVector<Expr *, 8> Finals;
8262 Expr *Step = Clause.getStep();
8263 Expr *CalcStep = Clause.getCalcStep();
8264 // OpenMP [2.14.3.7, linear clause]
8265 // If linear-step is not specified it is assumed to be 1.
8266 if (Step == nullptr)
8267 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8268 else if (CalcStep)
8269 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8270 bool HasErrors = false;
8271 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008272 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008273 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008274 for (auto &RefExpr : Clause.varlists()) {
8275 Expr *InitExpr = *CurInit;
8276
8277 // Build privatized reference to the current linear var.
8278 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008279 Expr *CapturedRef;
8280 if (LinKind == OMPC_LINEAR_uval)
8281 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8282 else
8283 CapturedRef =
8284 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8285 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8286 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008287
8288 // Build update: Var = InitExpr + IV * Step
8289 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008290 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008291 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008292 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8293 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008294
8295 // Build final: Var = InitExpr + NumIterations * Step
8296 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008297 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008298 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008299 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8300 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008301 if (!Update.isUsable() || !Final.isUsable()) {
8302 Updates.push_back(nullptr);
8303 Finals.push_back(nullptr);
8304 HasErrors = true;
8305 } else {
8306 Updates.push_back(Update.get());
8307 Finals.push_back(Final.get());
8308 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008309 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008310 }
8311 Clause.setUpdates(Updates);
8312 Clause.setFinals(Finals);
8313 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008314}
8315
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008316OMPClause *Sema::ActOnOpenMPAlignedClause(
8317 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8318 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8319
8320 SmallVector<Expr *, 8> Vars;
8321 for (auto &RefExpr : VarList) {
8322 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8323 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8324 // It will be analyzed later.
8325 Vars.push_back(RefExpr);
8326 continue;
8327 }
8328
8329 SourceLocation ELoc = RefExpr->getExprLoc();
8330 // OpenMP [2.1, C/C++]
8331 // A list item is a variable name.
8332 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8333 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008334 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8335 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008336 continue;
8337 }
8338
8339 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8340
8341 // OpenMP [2.8.1, simd construct, Restrictions]
8342 // The type of list items appearing in the aligned clause must be
8343 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008344 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008345 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008346 const Type *Ty = QType.getTypePtrOrNull();
8347 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8348 !Ty->isPointerType())) {
8349 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8350 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8351 bool IsDecl =
8352 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8353 Diag(VD->getLocation(),
8354 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8355 << VD;
8356 continue;
8357 }
8358
8359 // OpenMP [2.8.1, simd construct, Restrictions]
8360 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008361 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008362 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8363 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8364 << getOpenMPClauseName(OMPC_aligned);
8365 continue;
8366 }
8367
8368 Vars.push_back(DE);
8369 }
8370
8371 // OpenMP [2.8.1, simd construct, Description]
8372 // The parameter of the aligned clause, alignment, must be a constant
8373 // positive integer expression.
8374 // If no optional parameter is specified, implementation-defined default
8375 // alignments for SIMD instructions on the target platforms are assumed.
8376 if (Alignment != nullptr) {
8377 ExprResult AlignResult =
8378 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8379 if (AlignResult.isInvalid())
8380 return nullptr;
8381 Alignment = AlignResult.get();
8382 }
8383 if (Vars.empty())
8384 return nullptr;
8385
8386 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8387 EndLoc, Vars, Alignment);
8388}
8389
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008390OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8391 SourceLocation StartLoc,
8392 SourceLocation LParenLoc,
8393 SourceLocation EndLoc) {
8394 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008395 SmallVector<Expr *, 8> SrcExprs;
8396 SmallVector<Expr *, 8> DstExprs;
8397 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008398 for (auto &RefExpr : VarList) {
8399 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8400 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008401 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008402 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008403 SrcExprs.push_back(nullptr);
8404 DstExprs.push_back(nullptr);
8405 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008406 continue;
8407 }
8408
Alexey Bataeved09d242014-05-28 05:53:51 +00008409 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008410 // OpenMP [2.1, C/C++]
8411 // A list item is a variable name.
8412 // OpenMP [2.14.4.1, Restrictions, p.1]
8413 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008414 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008415 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008416 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8417 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008418 continue;
8419 }
8420
8421 Decl *D = DE->getDecl();
8422 VarDecl *VD = cast<VarDecl>(D);
8423
8424 QualType Type = VD->getType();
8425 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8426 // It will be analyzed later.
8427 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008428 SrcExprs.push_back(nullptr);
8429 DstExprs.push_back(nullptr);
8430 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008431 continue;
8432 }
8433
8434 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8435 // A list item that appears in a copyin clause must be threadprivate.
8436 if (!DSAStack->isThreadPrivate(VD)) {
8437 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008438 << getOpenMPClauseName(OMPC_copyin)
8439 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008440 continue;
8441 }
8442
8443 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8444 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008445 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008446 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008447 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008448 auto *SrcVD =
8449 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8450 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008451 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008452 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8453 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008454 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8455 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008456 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008457 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008458 // For arrays generate assignment operation for single element and replace
8459 // it by the original array element in CodeGen.
8460 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8461 PseudoDstExpr, PseudoSrcExpr);
8462 if (AssignmentOp.isInvalid())
8463 continue;
8464 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8465 /*DiscardedValue=*/true);
8466 if (AssignmentOp.isInvalid())
8467 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008468
8469 DSAStack->addDSA(VD, DE, OMPC_copyin);
8470 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008471 SrcExprs.push_back(PseudoSrcExpr);
8472 DstExprs.push_back(PseudoDstExpr);
8473 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008474 }
8475
Alexey Bataeved09d242014-05-28 05:53:51 +00008476 if (Vars.empty())
8477 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008478
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008479 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8480 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008481}
8482
Alexey Bataevbae9a792014-06-27 10:37:06 +00008483OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8484 SourceLocation StartLoc,
8485 SourceLocation LParenLoc,
8486 SourceLocation EndLoc) {
8487 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008488 SmallVector<Expr *, 8> SrcExprs;
8489 SmallVector<Expr *, 8> DstExprs;
8490 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008491 for (auto &RefExpr : VarList) {
8492 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8493 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8494 // It will be analyzed later.
8495 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008496 SrcExprs.push_back(nullptr);
8497 DstExprs.push_back(nullptr);
8498 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008499 continue;
8500 }
8501
8502 SourceLocation ELoc = RefExpr->getExprLoc();
8503 // OpenMP [2.1, C/C++]
8504 // A list item is a variable name.
8505 // OpenMP [2.14.4.1, Restrictions, p.1]
8506 // A list item that appears in a copyin clause must be threadprivate.
8507 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8508 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008509 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8510 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008511 continue;
8512 }
8513
8514 Decl *D = DE->getDecl();
8515 VarDecl *VD = cast<VarDecl>(D);
8516
8517 QualType Type = VD->getType();
8518 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8519 // It will be analyzed later.
8520 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008521 SrcExprs.push_back(nullptr);
8522 DstExprs.push_back(nullptr);
8523 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008524 continue;
8525 }
8526
8527 // OpenMP [2.14.4.2, Restrictions, p.2]
8528 // A list item that appears in a copyprivate clause may not appear in a
8529 // private or firstprivate clause on the single construct.
8530 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008531 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008532 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8533 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008534 Diag(ELoc, diag::err_omp_wrong_dsa)
8535 << getOpenMPClauseName(DVar.CKind)
8536 << getOpenMPClauseName(OMPC_copyprivate);
8537 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8538 continue;
8539 }
8540
8541 // OpenMP [2.11.4.2, Restrictions, p.1]
8542 // All list items that appear in a copyprivate clause must be either
8543 // threadprivate or private in the enclosing context.
8544 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008545 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008546 if (DVar.CKind == OMPC_shared) {
8547 Diag(ELoc, diag::err_omp_required_access)
8548 << getOpenMPClauseName(OMPC_copyprivate)
8549 << "threadprivate or private in the enclosing context";
8550 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8551 continue;
8552 }
8553 }
8554 }
8555
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008556 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008557 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008558 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008559 << getOpenMPClauseName(OMPC_copyprivate) << Type
8560 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008561 bool IsDecl =
8562 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8563 Diag(VD->getLocation(),
8564 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8565 << VD;
8566 continue;
8567 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008568
Alexey Bataevbae9a792014-06-27 10:37:06 +00008569 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8570 // A variable of class type (or array thereof) that appears in a
8571 // copyin clause requires an accessible, unambiguous copy assignment
8572 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008573 Type = Context.getBaseElementType(Type.getNonReferenceType())
8574 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008575 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008576 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8577 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008578 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008579 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008580 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008581 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8582 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008583 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008584 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008585 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8586 PseudoDstExpr, PseudoSrcExpr);
8587 if (AssignmentOp.isInvalid())
8588 continue;
8589 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8590 /*DiscardedValue=*/true);
8591 if (AssignmentOp.isInvalid())
8592 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008593
8594 // No need to mark vars as copyprivate, they are already threadprivate or
8595 // implicitly private.
8596 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008597 SrcExprs.push_back(PseudoSrcExpr);
8598 DstExprs.push_back(PseudoDstExpr);
8599 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008600 }
8601
8602 if (Vars.empty())
8603 return nullptr;
8604
Alexey Bataeva63048e2015-03-23 06:18:07 +00008605 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8606 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008607}
8608
Alexey Bataev6125da92014-07-21 11:26:11 +00008609OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8610 SourceLocation StartLoc,
8611 SourceLocation LParenLoc,
8612 SourceLocation EndLoc) {
8613 if (VarList.empty())
8614 return nullptr;
8615
8616 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8617}
Alexey Bataevdea47612014-07-23 07:46:59 +00008618
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008619OMPClause *
8620Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8621 SourceLocation DepLoc, SourceLocation ColonLoc,
8622 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8623 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008624 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008625 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008626 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008627 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008628 return nullptr;
8629 }
8630 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008631 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8632 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008633 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008634 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008635 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8636 /*Last=*/OMPC_DEPEND_unknown, Except)
8637 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008638 return nullptr;
8639 }
8640 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008641 llvm::APSInt DepCounter(/*BitWidth=*/32);
8642 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8643 if (DepKind == OMPC_DEPEND_sink) {
8644 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8645 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8646 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008647 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008648 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008649 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8650 DSAStack->getParentOrderedRegionParam()) {
8651 for (auto &RefExpr : VarList) {
8652 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8653 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8654 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8655 // It will be analyzed later.
8656 Vars.push_back(RefExpr);
8657 continue;
8658 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008659
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008660 SourceLocation ELoc = RefExpr->getExprLoc();
8661 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8662 if (DepKind == OMPC_DEPEND_sink) {
8663 if (DepCounter >= TotalDepCount) {
8664 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8665 continue;
8666 }
8667 ++DepCounter;
8668 // OpenMP [2.13.9, Summary]
8669 // depend(dependence-type : vec), where dependence-type is:
8670 // 'sink' and where vec is the iteration vector, which has the form:
8671 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8672 // where n is the value specified by the ordered clause in the loop
8673 // directive, xi denotes the loop iteration variable of the i-th nested
8674 // loop associated with the loop directive, and di is a constant
8675 // non-negative integer.
8676 SimpleExpr = SimpleExpr->IgnoreImplicit();
8677 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8678 if (!DE) {
8679 OverloadedOperatorKind OOK = OO_None;
8680 SourceLocation OOLoc;
8681 Expr *LHS, *RHS;
8682 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8683 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8684 OOLoc = BO->getOperatorLoc();
8685 LHS = BO->getLHS()->IgnoreParenImpCasts();
8686 RHS = BO->getRHS()->IgnoreParenImpCasts();
8687 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8688 OOK = OCE->getOperator();
8689 OOLoc = OCE->getOperatorLoc();
8690 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8691 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8692 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8693 OOK = MCE->getMethodDecl()
8694 ->getNameInfo()
8695 .getName()
8696 .getCXXOverloadedOperator();
8697 OOLoc = MCE->getCallee()->getExprLoc();
8698 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8699 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8700 } else {
8701 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8702 continue;
8703 }
8704 DE = dyn_cast<DeclRefExpr>(LHS);
8705 if (!DE) {
8706 Diag(LHS->getExprLoc(),
8707 diag::err_omp_depend_sink_expected_loop_iteration)
8708 << DSAStack->getParentLoopControlVariable(
8709 DepCounter.getZExtValue());
8710 continue;
8711 }
8712 if (OOK != OO_Plus && OOK != OO_Minus) {
8713 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8714 continue;
8715 }
8716 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8717 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8718 if (Res.isInvalid())
8719 continue;
8720 }
8721 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8722 if (!CurContext->isDependentContext() &&
8723 DSAStack->getParentOrderedRegionParam() &&
8724 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8725 Diag(DE->getExprLoc(),
8726 diag::err_omp_depend_sink_expected_loop_iteration)
8727 << DSAStack->getParentLoopControlVariable(
8728 DepCounter.getZExtValue());
8729 continue;
8730 }
8731 } else {
8732 // OpenMP [2.11.1.1, Restrictions, p.3]
8733 // A variable that is part of another variable (such as a field of a
8734 // structure) but is not an array element or an array section cannot
8735 // appear in a depend clause.
8736 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8737 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8738 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8739 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8740 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008741 (ASE &&
8742 !ASE->getBase()
8743 ->getType()
8744 .getNonReferenceType()
8745 ->isPointerType() &&
8746 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008747 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8748 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008749 continue;
8750 }
8751 }
8752
8753 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8754 }
8755
8756 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8757 TotalDepCount > VarList.size() &&
8758 DSAStack->getParentOrderedRegionParam()) {
8759 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8760 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8761 }
8762 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8763 Vars.empty())
8764 return nullptr;
8765 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008766
8767 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8768 DepLoc, ColonLoc, Vars);
8769}
Michael Wonge710d542015-08-07 16:16:36 +00008770
8771OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8772 SourceLocation LParenLoc,
8773 SourceLocation EndLoc) {
8774 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008775
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008776 // OpenMP [2.9.1, Restrictions]
8777 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008778 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8779 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008780 return nullptr;
8781
Michael Wonge710d542015-08-07 16:16:36 +00008782 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8783}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008784
8785static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8786 DSAStackTy *Stack, CXXRecordDecl *RD) {
8787 if (!RD || RD->isInvalidDecl())
8788 return true;
8789
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008790 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8791 if (auto *CTD = CTSD->getSpecializedTemplate())
8792 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008793 auto QTy = SemaRef.Context.getRecordType(RD);
8794 if (RD->isDynamicClass()) {
8795 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8796 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8797 return false;
8798 }
8799 auto *DC = RD;
8800 bool IsCorrect = true;
8801 for (auto *I : DC->decls()) {
8802 if (I) {
8803 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8804 if (MD->isStatic()) {
8805 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8806 SemaRef.Diag(MD->getLocation(),
8807 diag::note_omp_static_member_in_target);
8808 IsCorrect = false;
8809 }
8810 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8811 if (VD->isStaticDataMember()) {
8812 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8813 SemaRef.Diag(VD->getLocation(),
8814 diag::note_omp_static_member_in_target);
8815 IsCorrect = false;
8816 }
8817 }
8818 }
8819 }
8820
8821 for (auto &I : RD->bases()) {
8822 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8823 I.getType()->getAsCXXRecordDecl()))
8824 IsCorrect = false;
8825 }
8826 return IsCorrect;
8827}
8828
8829static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8830 DSAStackTy *Stack, QualType QTy) {
8831 NamedDecl *ND;
8832 if (QTy->isIncompleteType(&ND)) {
8833 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8834 return false;
8835 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8836 if (!RD->isInvalidDecl() &&
8837 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8838 return false;
8839 }
8840 return true;
8841}
8842
Samuel Antao5de996e2016-01-22 20:21:36 +00008843// Return the expression of the base of the map clause or null if it cannot
8844// be determined and do all the necessary checks to see if the expression is
8845// valid as a standalone map clause expression.
8846static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8847 SourceLocation ELoc = E->getExprLoc();
8848 SourceRange ERange = E->getSourceRange();
8849
8850 // The base of elements of list in a map clause have to be either:
8851 // - a reference to variable or field.
8852 // - a member expression.
8853 // - an array expression.
8854 //
8855 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8856 // reference to 'r'.
8857 //
8858 // If we have:
8859 //
8860 // struct SS {
8861 // Bla S;
8862 // foo() {
8863 // #pragma omp target map (S.Arr[:12]);
8864 // }
8865 // }
8866 //
8867 // We want to retrieve the member expression 'this->S';
8868
8869 Expr *RelevantExpr = nullptr;
8870
8871 // Flags to help capture some memory
8872
8873 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8874 // If a list item is an array section, it must specify contiguous storage.
8875 //
8876 // For this restriction it is sufficient that we make sure only references
8877 // to variables or fields and array expressions, and that no array sections
8878 // exist except in the rightmost expression. E.g. these would be invalid:
8879 //
8880 // r.ArrS[3:5].Arr[6:7]
8881 //
8882 // r.ArrS[3:5].x
8883 //
8884 // but these would be valid:
8885 // r.ArrS[3].Arr[6:7]
8886 //
8887 // r.ArrS[3].x
8888
8889 bool IsRightMostExpression = true;
8890
8891 while (!RelevantExpr) {
8892 auto AllowArraySection = IsRightMostExpression;
8893 IsRightMostExpression = false;
8894
8895 E = E->IgnoreParenImpCasts();
8896
8897 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8898 if (!isa<VarDecl>(CurE->getDecl()))
8899 break;
8900
8901 RelevantExpr = CurE;
8902 continue;
8903 }
8904
8905 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8906 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8907
8908 if (isa<CXXThisExpr>(BaseE))
8909 // We found a base expression: this->Val.
8910 RelevantExpr = CurE;
8911 else
8912 E = BaseE;
8913
8914 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8915 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8916 << CurE->getSourceRange();
8917 break;
8918 }
8919
8920 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8921
8922 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8923 // A bit-field cannot appear in a map clause.
8924 //
8925 if (FD->isBitField()) {
8926 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8927 << CurE->getSourceRange();
8928 break;
8929 }
8930
8931 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8932 // If the type of a list item is a reference to a type T then the type
8933 // will be considered to be T for all purposes of this clause.
8934 QualType CurType = BaseE->getType();
8935 if (CurType->isReferenceType())
8936 CurType = CurType->getPointeeType();
8937
8938 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
8939 // A list item cannot be a variable that is a member of a structure with
8940 // a union type.
8941 //
8942 if (auto *RT = CurType->getAs<RecordType>())
8943 if (RT->isUnionType()) {
8944 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
8945 << CurE->getSourceRange();
8946 break;
8947 }
8948
8949 continue;
8950 }
8951
8952 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
8953 E = CurE->getBase()->IgnoreParenImpCasts();
8954
8955 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
8956 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8957 << 0 << CurE->getSourceRange();
8958 break;
8959 }
8960 continue;
8961 }
8962
8963 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
8964 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
8965 // If a list item is an element of a structure, only the rightmost symbol
8966 // of the variable reference can be an array section.
8967 //
8968 if (!AllowArraySection) {
8969 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
8970 << CurE->getSourceRange();
8971 break;
8972 }
8973
8974 E = CurE->getBase()->IgnoreParenImpCasts();
8975
8976 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8977 // If the type of a list item is a reference to a type T then the type
8978 // will be considered to be T for all purposes of this clause.
8979 QualType CurType = E->getType();
8980 if (CurType->isReferenceType())
8981 CurType = CurType->getPointeeType();
8982
8983 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
8984 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
8985 << 0 << CurE->getSourceRange();
8986 break;
8987 }
8988
8989 continue;
8990 }
8991
8992 // If nothing else worked, this is not a valid map clause expression.
8993 SemaRef.Diag(ELoc,
8994 diag::err_omp_expected_named_var_member_or_array_expression)
8995 << ERange;
8996 break;
8997 }
8998
8999 return RelevantExpr;
9000}
9001
9002// Return true if expression E associated with value VD has conflicts with other
9003// map information.
9004static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9005 Expr *E, bool CurrentRegionOnly) {
9006 assert(VD && E);
9007
9008 // Types used to organize the components of a valid map clause.
9009 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9010 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9011
9012 // Helper to extract the components in the map clause expression E and store
9013 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9014 // it has already passed the single clause checks.
9015 auto ExtractMapExpressionComponents = [](Expr *TE,
9016 MapExpressionComponents &MEC) {
9017 while (true) {
9018 TE = TE->IgnoreParenImpCasts();
9019
9020 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9021 MEC.push_back(
9022 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9023 break;
9024 }
9025
9026 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9027 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9028
9029 MEC.push_back(MapExpressionComponent(
9030 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9031 if (isa<CXXThisExpr>(BaseE))
9032 break;
9033
9034 TE = BaseE;
9035 continue;
9036 }
9037
9038 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9039 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9040 TE = CurE->getBase()->IgnoreParenImpCasts();
9041 continue;
9042 }
9043
9044 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9045 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9046 TE = CurE->getBase()->IgnoreParenImpCasts();
9047 continue;
9048 }
9049
9050 llvm_unreachable(
9051 "Expecting only valid map clause expressions at this point!");
9052 }
9053 };
9054
9055 SourceLocation ELoc = E->getExprLoc();
9056 SourceRange ERange = E->getSourceRange();
9057
9058 // In order to easily check the conflicts we need to match each component of
9059 // the expression under test with the components of the expressions that are
9060 // already in the stack.
9061
9062 MapExpressionComponents CurComponents;
9063 ExtractMapExpressionComponents(E, CurComponents);
9064
9065 assert(!CurComponents.empty() && "Map clause expression with no components!");
9066 assert(CurComponents.back().second == VD &&
9067 "Map clause expression with unexpected base!");
9068
9069 // Variables to help detecting enclosing problems in data environment nests.
9070 bool IsEnclosedByDataEnvironmentExpr = false;
9071 Expr *EnclosingExpr = nullptr;
9072
9073 bool FoundError =
9074 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9075 MapExpressionComponents StackComponents;
9076 ExtractMapExpressionComponents(RE, StackComponents);
9077 assert(!StackComponents.empty() &&
9078 "Map clause expression with no components!");
9079 assert(StackComponents.back().second == VD &&
9080 "Map clause expression with unexpected base!");
9081
9082 // Expressions must start from the same base. Here we detect at which
9083 // point both expressions diverge from each other and see if we can
9084 // detect if the memory referred to both expressions is contiguous and
9085 // do not overlap.
9086 auto CI = CurComponents.rbegin();
9087 auto CE = CurComponents.rend();
9088 auto SI = StackComponents.rbegin();
9089 auto SE = StackComponents.rend();
9090 for (; CI != CE && SI != SE; ++CI, ++SI) {
9091
9092 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9093 // At most one list item can be an array item derived from a given
9094 // variable in map clauses of the same construct.
9095 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9096 isa<OMPArraySectionExpr>(CI->first)) &&
9097 (isa<ArraySubscriptExpr>(SI->first) ||
9098 isa<OMPArraySectionExpr>(SI->first))) {
9099 SemaRef.Diag(CI->first->getExprLoc(),
9100 diag::err_omp_multiple_array_items_in_map_clause)
9101 << CI->first->getSourceRange();
9102 ;
9103 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9104 << SI->first->getSourceRange();
9105 return true;
9106 }
9107
9108 // Do both expressions have the same kind?
9109 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9110 break;
9111
9112 // Are we dealing with different variables/fields?
9113 if (CI->second != SI->second)
9114 break;
9115 }
9116
9117 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9118 // List items of map clauses in the same construct must not share
9119 // original storage.
9120 //
9121 // If the expressions are exactly the same or one is a subset of the
9122 // other, it means they are sharing storage.
9123 if (CI == CE && SI == SE) {
9124 if (CurrentRegionOnly) {
9125 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9126 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9127 << RE->getSourceRange();
9128 return true;
9129 } else {
9130 // If we find the same expression in the enclosing data environment,
9131 // that is legal.
9132 IsEnclosedByDataEnvironmentExpr = true;
9133 return false;
9134 }
9135 }
9136
9137 QualType DerivedType = std::prev(CI)->first->getType();
9138 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9139
9140 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9141 // If the type of a list item is a reference to a type T then the type
9142 // will be considered to be T for all purposes of this clause.
9143 if (DerivedType->isReferenceType())
9144 DerivedType = DerivedType->getPointeeType();
9145
9146 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9147 // A variable for which the type is pointer and an array section
9148 // derived from that variable must not appear as list items of map
9149 // clauses of the same construct.
9150 //
9151 // Also, cover one of the cases in:
9152 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9153 // If any part of the original storage of a list item has corresponding
9154 // storage in the device data environment, all of the original storage
9155 // must have corresponding storage in the device data environment.
9156 //
9157 if (DerivedType->isAnyPointerType()) {
9158 if (CI == CE || SI == SE) {
9159 SemaRef.Diag(
9160 DerivedLoc,
9161 diag::err_omp_pointer_mapped_along_with_derived_section)
9162 << DerivedLoc;
9163 } else {
9164 assert(CI != CE && SI != SE);
9165 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9166 << DerivedLoc;
9167 }
9168 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9169 << RE->getSourceRange();
9170 return true;
9171 }
9172
9173 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9174 // List items of map clauses in the same construct must not share
9175 // original storage.
9176 //
9177 // An expression is a subset of the other.
9178 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9179 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9180 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9181 << RE->getSourceRange();
9182 return true;
9183 }
9184
9185 // The current expression uses the same base as other expression in the
9186 // data environment but does not contain it completelly.
9187 if (!CurrentRegionOnly && SI != SE)
9188 EnclosingExpr = RE;
9189
9190 // The current expression is a subset of the expression in the data
9191 // environment.
9192 IsEnclosedByDataEnvironmentExpr |=
9193 (!CurrentRegionOnly && CI != CE && SI == SE);
9194
9195 return false;
9196 });
9197
9198 if (CurrentRegionOnly)
9199 return FoundError;
9200
9201 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9202 // If any part of the original storage of a list item has corresponding
9203 // storage in the device data environment, all of the original storage must
9204 // have corresponding storage in the device data environment.
9205 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9206 // If a list item is an element of a structure, and a different element of
9207 // the structure has a corresponding list item in the device data environment
9208 // prior to a task encountering the construct associated with the map clause,
9209 // then the list item must also have a correspnding list item in the device
9210 // data environment prior to the task encountering the construct.
9211 //
9212 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9213 SemaRef.Diag(ELoc,
9214 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9215 << ERange;
9216 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9217 << EnclosingExpr->getSourceRange();
9218 return true;
9219 }
9220
9221 return FoundError;
9222}
9223
Samuel Antao23abd722016-01-19 20:40:49 +00009224OMPClause *
9225Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9226 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9227 SourceLocation MapLoc, SourceLocation ColonLoc,
9228 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9229 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009230 SmallVector<Expr *, 4> Vars;
9231
9232 for (auto &RE : VarList) {
9233 assert(RE && "Null expr in omp map");
9234 if (isa<DependentScopeDeclRefExpr>(RE)) {
9235 // It will be analyzed later.
9236 Vars.push_back(RE);
9237 continue;
9238 }
9239 SourceLocation ELoc = RE->getExprLoc();
9240
Kelvin Li0bff7af2015-11-23 05:32:03 +00009241 auto *VE = RE->IgnoreParenLValueCasts();
9242
9243 if (VE->isValueDependent() || VE->isTypeDependent() ||
9244 VE->isInstantiationDependent() ||
9245 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009246 // We can only analyze this information once the missing information is
9247 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009248 Vars.push_back(RE);
9249 continue;
9250 }
9251
9252 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009253
Samuel Antao5de996e2016-01-22 20:21:36 +00009254 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9255 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9256 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009257 continue;
9258 }
9259
Samuel Antao5de996e2016-01-22 20:21:36 +00009260 // Obtain the array or member expression bases if required.
9261 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9262 if (!BE)
9263 continue;
9264
9265 // If the base is a reference to a variable, we rely on that variable for
9266 // the following checks. If it is a 'this' expression we rely on the field.
9267 ValueDecl *D = nullptr;
9268 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9269 D = DRE->getDecl();
9270 } else {
9271 auto *ME = cast<MemberExpr>(BE);
9272 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9273 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009274 }
9275 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009276
Samuel Antao5de996e2016-01-22 20:21:36 +00009277 auto *VD = dyn_cast<VarDecl>(D);
9278 auto *FD = dyn_cast<FieldDecl>(D);
9279
9280 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009281 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009282
9283 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9284 // threadprivate variables cannot appear in a map clause.
9285 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009286 auto DVar = DSAStack->getTopDSA(VD, false);
9287 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9288 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9289 continue;
9290 }
9291
Samuel Antao5de996e2016-01-22 20:21:36 +00009292 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9293 // A list item cannot appear in both a map clause and a data-sharing
9294 // attribute clause on the same construct.
9295 //
9296 // TODO: Implement this check - it cannot currently be tested because of
9297 // missing implementation of the other data sharing clauses in target
9298 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009299
Samuel Antao5de996e2016-01-22 20:21:36 +00009300 // Check conflicts with other map clause expressions. We check the conflicts
9301 // with the current construct separately from the enclosing data
9302 // environment, because the restrictions are different.
9303 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9304 /*CurrentRegionOnly=*/true))
9305 break;
9306 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9307 /*CurrentRegionOnly=*/false))
9308 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009309
Samuel Antao5de996e2016-01-22 20:21:36 +00009310 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9311 // If the type of a list item is a reference to a type T then the type will
9312 // be considered to be T for all purposes of this clause.
9313 QualType Type = D->getType();
9314 if (Type->isReferenceType())
9315 Type = Type->getPointeeType();
9316
9317 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009318 // A list item must have a mappable type.
9319 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9320 DSAStack, Type))
9321 continue;
9322
Samuel Antaodf67fc42016-01-19 19:15:56 +00009323 // target enter data
9324 // OpenMP [2.10.2, Restrictions, p. 99]
9325 // A map-type must be specified in all map clauses and must be either
9326 // to or alloc.
9327 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9328 if (DKind == OMPD_target_enter_data &&
9329 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9330 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009331 << (IsMapTypeImplicit ? 1 : 0)
9332 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009333 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009334 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009335 }
9336
Samuel Antao72590762016-01-19 20:04:50 +00009337 // target exit_data
9338 // OpenMP [2.10.3, Restrictions, p. 102]
9339 // A map-type must be specified in all map clauses and must be either
9340 // from, release, or delete.
9341 DKind = DSAStack->getCurrentDirective();
9342 if (DKind == OMPD_target_exit_data &&
9343 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9344 MapType == OMPC_MAP_delete)) {
9345 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009346 << (IsMapTypeImplicit ? 1 : 0)
9347 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009348 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009349 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009350 }
9351
Kelvin Li0bff7af2015-11-23 05:32:03 +00009352 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009353 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009354 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009355
Samuel Antao5de996e2016-01-22 20:21:36 +00009356 // We need to produce a map clause even if we don't have variables so that
9357 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009358 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009359 MapTypeModifier, MapType, IsMapTypeImplicit,
9360 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009361}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009362
9363OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9364 SourceLocation StartLoc,
9365 SourceLocation LParenLoc,
9366 SourceLocation EndLoc) {
9367 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009368
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009369 // OpenMP [teams Constrcut, Restrictions]
9370 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009371 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9372 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009373 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009374
9375 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9376}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009377
9378OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9379 SourceLocation StartLoc,
9380 SourceLocation LParenLoc,
9381 SourceLocation EndLoc) {
9382 Expr *ValExpr = ThreadLimit;
9383
9384 // OpenMP [teams Constrcut, Restrictions]
9385 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009386 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9387 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009388 return nullptr;
9389
9390 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9391 EndLoc);
9392}
Alexey Bataeva0569352015-12-01 10:17:31 +00009393
9394OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9395 SourceLocation StartLoc,
9396 SourceLocation LParenLoc,
9397 SourceLocation EndLoc) {
9398 Expr *ValExpr = Priority;
9399
9400 // OpenMP [2.9.1, task Constrcut]
9401 // The priority-value is a non-negative numerical scalar expression.
9402 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9403 /*StrictlyPositive=*/false))
9404 return nullptr;
9405
9406 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9407}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009408
9409OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9410 SourceLocation StartLoc,
9411 SourceLocation LParenLoc,
9412 SourceLocation EndLoc) {
9413 Expr *ValExpr = Grainsize;
9414
9415 // OpenMP [2.9.2, taskloop Constrcut]
9416 // The parameter of the grainsize clause must be a positive integer
9417 // expression.
9418 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9419 /*StrictlyPositive=*/true))
9420 return nullptr;
9421
9422 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9423}
Alexey Bataev382967a2015-12-08 12:06:20 +00009424
9425OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9426 SourceLocation StartLoc,
9427 SourceLocation LParenLoc,
9428 SourceLocation EndLoc) {
9429 Expr *ValExpr = NumTasks;
9430
9431 // OpenMP [2.9.2, taskloop Constrcut]
9432 // The parameter of the num_tasks clause must be a positive integer
9433 // expression.
9434 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9435 /*StrictlyPositive=*/true))
9436 return nullptr;
9437
9438 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9439}
9440
Alexey Bataev28c75412015-12-15 08:19:24 +00009441OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9442 SourceLocation LParenLoc,
9443 SourceLocation EndLoc) {
9444 // OpenMP [2.13.2, critical construct, Description]
9445 // ... where hint-expression is an integer constant expression that evaluates
9446 // to a valid lock hint.
9447 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9448 if (HintExpr.isInvalid())
9449 return nullptr;
9450 return new (Context)
9451 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9452}
9453
Carlo Bertollib4adf552016-01-15 18:50:31 +00009454OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9455 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9456 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9457 SourceLocation EndLoc) {
9458 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9459 std::string Values;
9460 Values += "'";
9461 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9462 Values += "'";
9463 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9464 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9465 return nullptr;
9466 }
9467 Expr *ValExpr = ChunkSize;
9468 Expr *HelperValExpr = nullptr;
9469 if (ChunkSize) {
9470 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9471 !ChunkSize->isInstantiationDependent() &&
9472 !ChunkSize->containsUnexpandedParameterPack()) {
9473 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9474 ExprResult Val =
9475 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9476 if (Val.isInvalid())
9477 return nullptr;
9478
9479 ValExpr = Val.get();
9480
9481 // OpenMP [2.7.1, Restrictions]
9482 // chunk_size must be a loop invariant integer expression with a positive
9483 // value.
9484 llvm::APSInt Result;
9485 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9486 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9487 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9488 << "dist_schedule" << ChunkSize->getSourceRange();
9489 return nullptr;
9490 }
9491 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9492 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9493 ChunkSize->getType(), ".chunk.");
9494 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9495 ChunkSize->getExprLoc(),
9496 /*RefersToCapture=*/true);
9497 HelperValExpr = ImpVarRef;
9498 }
9499 }
9500 }
9501
9502 return new (Context)
9503 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9504 Kind, ValExpr, HelperValExpr);
9505}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009506
9507OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9508 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9509 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9510 SourceLocation KindLoc, SourceLocation EndLoc) {
9511 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9512 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9513 Kind != OMPC_DEFAULTMAP_scalar) {
9514 std::string Value;
9515 SourceLocation Loc;
9516 Value += "'";
9517 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9518 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9519 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9520 Loc = MLoc;
9521 } else {
9522 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9523 OMPC_DEFAULTMAP_scalar);
9524 Loc = KindLoc;
9525 }
9526 Value += "'";
9527 Diag(Loc, diag::err_omp_unexpected_clause_value)
9528 << Value << getOpenMPClauseName(OMPC_defaultmap);
9529 return nullptr;
9530 }
9531
9532 return new (Context)
9533 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9534}