blob: 94eb6978ce921ddcc1f268f6625acb1eeb7826fa [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 Bataev94a4f0c2016-03-03 05:21:39 +000024#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000026#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000027#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000030#include "clang/Sema/Scope.h"
31#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000032#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000033using namespace clang;
34
Alexey Bataev758e55e2013-09-06 18:03:48 +000035//===----------------------------------------------------------------------===//
36// Stack of data-sharing attributes for variables
37//===----------------------------------------------------------------------===//
38
39namespace {
40/// \brief Default data sharing attributes, which can be applied to directive.
41enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000042 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
43 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
44 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000045};
Alexey Bataev7ff55242014-06-19 09:13:45 +000046
Alexey Bataevf29276e2014-06-18 04:14:57 +000047template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000048 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000049 bool operator()(T Kind) {
50 for (auto KindEl : Arr)
51 if (KindEl == Kind)
52 return true;
53 return false;
54 }
55
56private:
57 ArrayRef<T> Arr;
58};
Alexey Bataev23b69422014-06-18 07:08:49 +000059struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000060 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000061 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000062};
63
64typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
65typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000066
67/// \brief Stack for tracking declarations used in OpenMP directives and
68/// clauses and their data-sharing attributes.
69class DSAStackTy {
70public:
71 struct DSAVarData {
72 OpenMPDirectiveKind DKind;
73 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000074 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000075 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000076 SourceLocation ImplicitDSALoc;
77 DSAVarData()
78 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000079 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000080 };
Alexey Bataeved09d242014-05-28 05:53:51 +000081
Alexey Bataev758e55e2013-09-06 18:03:48 +000082private:
Samuel Antao5de996e2016-01-22 20:21:36 +000083 typedef SmallVector<Expr *, 4> MapInfo;
84
Alexey Bataev758e55e2013-09-06 18:03:48 +000085 struct DSAInfo {
86 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000087 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000088 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000089 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000090 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
91 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000092 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000093 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000094 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
95 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000096
97 struct SharingMapTy {
98 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000099 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000100 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000101 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 OpenMPDirectiveKind Directive;
105 DeclarationNameInfo DirectiveName;
106 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000108 /// \brief first argument (Expr *) contains optional argument of the
109 /// 'ordered' clause, the second one is true if the regions has 'ordered'
110 /// clause, false otherwise.
111 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000112 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000113 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000114 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000115 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000116 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000118 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000120 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000121 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000124 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000125 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000126 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 };
128
Axel Naumann323862e2016-02-03 10:45:22 +0000129 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130
131 /// \brief Stack of used declaration and their data-sharing attributes.
132 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133 /// \brief true, if check for DSA must be from parent directive, false, if
134 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000135 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000136 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000138 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139
140 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
141
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000142 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000143
144 /// \brief Checks if the variable is a local for OpenMP region.
145 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000146
Alexey Bataev758e55e2013-09-06 18:03:48 +0000147public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000148 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000149 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
150 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000151
Alexey Bataevaac108a2015-06-23 04:51:00 +0000152 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
153 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000155 bool isForceVarCapturing() const { return ForceCapturing; }
156 void setForceVarCapturing(bool V) { ForceCapturing = V; }
157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000159 Scope *CurScope, SourceLocation Loc) {
160 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
161 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162 }
163
164 void pop() {
165 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
166 Stack.pop_back();
167 }
168
Alexey Bataev28c75412015-12-15 08:19:24 +0000169 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
170 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
171 }
172 const std::pair<OMPCriticalDirective *, llvm::APSInt>
173 getCriticalWithHint(const DeclarationNameInfo &Name) const {
174 auto I = Criticals.find(Name.getAsString());
175 if (I != Criticals.end())
176 return I->second;
177 return std::make_pair(nullptr, llvm::APSInt());
178 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000179 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000180 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000182 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000183
Alexey Bataev9c821032015-04-30 04:23:23 +0000184 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000185 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000186 /// \brief Check if the specified variable is a loop control variable for
187 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000188 /// \return The index of the loop control variable in the list of associated
189 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000191 /// \brief Check if the specified variable is a loop control variable for
192 /// parent region.
193 /// \return The index of the loop control variable in the list of associated
194 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000195 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000196 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
197 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000198 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000199
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000201 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
202 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203
Alexey Bataev758e55e2013-09-06 18:03:48 +0000204 /// \brief Returns data sharing attributes from top of the stack for the
205 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000206 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000207 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000208 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000209 /// \brief Checks if the specified variables has data-sharing attributes which
210 /// match specified \a CPred predicate in any directive which matches \a DPred
211 /// predicate.
212 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000213 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000214 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000215 /// \brief Checks if the specified variables has data-sharing attributes which
216 /// match specified \a CPred predicate in any innermost directive which
217 /// matches \a DPred predicate.
218 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000219 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
220 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000221 /// \brief Checks if the specified variables has explicit data-sharing
222 /// attributes which match specified \a CPred predicate at the specified
223 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000224 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000225 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
226 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000227
228 /// \brief Returns true if the directive at level \Level matches in the
229 /// specified \a DPred predicate.
230 bool hasExplicitDirective(
231 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
232 unsigned Level);
233
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000234 /// \brief Finds a directive which matches specified \a DPred predicate.
235 template <class NamedDirectivesPredicate>
236 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000237
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238 /// \brief Returns currently analyzed directive.
239 OpenMPDirectiveKind getCurrentDirective() const {
240 return Stack.back().Directive;
241 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000242 /// \brief Returns parent directive.
243 OpenMPDirectiveKind getParentDirective() const {
244 if (Stack.size() > 2)
245 return Stack[Stack.size() - 2].Directive;
246 return OMPD_unknown;
247 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000248 /// \brief Return the directive associated with the provided scope.
249 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000250
251 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000252 void setDefaultDSANone(SourceLocation Loc) {
253 Stack.back().DefaultAttr = DSA_none;
254 Stack.back().DefaultAttrLoc = Loc;
255 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000257 void setDefaultDSAShared(SourceLocation Loc) {
258 Stack.back().DefaultAttr = DSA_shared;
259 Stack.back().DefaultAttrLoc = Loc;
260 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000261
262 DefaultDataSharingAttributes getDefaultDSA() const {
263 return Stack.back().DefaultAttr;
264 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000265 SourceLocation getDefaultDSALocation() const {
266 return Stack.back().DefaultAttrLoc;
267 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268
Alexey Bataevf29276e2014-06-18 04:14:57 +0000269 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000270 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000271 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000272 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000273 }
274
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000275 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000276 void setOrderedRegion(bool IsOrdered, Expr *Param) {
277 Stack.back().OrderedRegion.setInt(IsOrdered);
278 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000279 }
280 /// \brief Returns true, if parent region is ordered (has associated
281 /// 'ordered' clause), false - otherwise.
282 bool isParentOrderedRegion() const {
283 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000284 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000285 return false;
286 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000287 /// \brief Returns optional parameter for the ordered region.
288 Expr *getParentOrderedRegionParam() const {
289 if (Stack.size() > 2)
290 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
291 return nullptr;
292 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000293 /// \brief Marks current region as nowait (it has a 'nowait' clause).
294 void setNowaitRegion(bool IsNowait = true) {
295 Stack.back().NowaitRegion = IsNowait;
296 }
297 /// \brief Returns true, if parent region is nowait (has associated
298 /// 'nowait' clause), false - otherwise.
299 bool isParentNowaitRegion() const {
300 if (Stack.size() > 2)
301 return Stack[Stack.size() - 2].NowaitRegion;
302 return false;
303 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000304 /// \brief Marks parent region as cancel region.
305 void setParentCancelRegion(bool Cancel = true) {
306 if (Stack.size() > 2)
307 Stack[Stack.size() - 2].CancelRegion =
308 Stack[Stack.size() - 2].CancelRegion || Cancel;
309 }
310 /// \brief Return true if current region has inner cancel construct.
311 bool isCancelRegion() const {
312 return Stack.back().CancelRegion;
313 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000314
Alexey Bataev9c821032015-04-30 04:23:23 +0000315 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000316 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000317 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000318 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000319
Alexey Bataev13314bf2014-10-09 04:18:56 +0000320 /// \brief Marks current target region as one with closely nested teams
321 /// region.
322 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
323 if (Stack.size() > 2)
324 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
325 }
326 /// \brief Returns true, if current region has closely nested teams region.
327 bool hasInnerTeamsRegion() const {
328 return getInnerTeamsRegionLoc().isValid();
329 }
330 /// \brief Returns location of the nested teams region (if any).
331 SourceLocation getInnerTeamsRegionLoc() const {
332 if (Stack.size() > 1)
333 return Stack.back().InnerTeamsRegionLoc;
334 return SourceLocation();
335 }
336
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000337 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000338 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000339 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000340
Samuel Antao5de996e2016-01-22 20:21:36 +0000341 // Do the check specified in MapInfoCheck and return true if any issue is
342 // found.
343 template <class MapInfoCheck>
344 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
345 MapInfoCheck Check) {
346 auto SI = Stack.rbegin();
347 auto SE = Stack.rend();
348
349 if (SI == SE)
350 return false;
351
352 if (CurrentRegionOnly) {
353 SE = std::next(SI);
354 } else {
355 ++SI;
356 }
357
358 for (; SI != SE; ++SI) {
359 auto MI = SI->MappedDecls.find(VD);
360 if (MI != SI->MappedDecls.end()) {
361 for (Expr *E : MI->second) {
362 if (Check(E))
363 return true;
364 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000365 }
366 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000367 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000368 }
369
Samuel Antao5de996e2016-01-22 20:21:36 +0000370 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000371 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000372 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000373 }
374 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000375};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000376bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
377 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000378 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000379 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000380}
Alexey Bataeved09d242014-05-28 05:53:51 +0000381} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000382
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000383static ValueDecl *getCanonicalDecl(ValueDecl *D) {
384 auto *VD = dyn_cast<VarDecl>(D);
385 auto *FD = dyn_cast<FieldDecl>(D);
386 if (VD != nullptr) {
387 VD = VD->getCanonicalDecl();
388 D = VD;
389 } else {
390 assert(FD);
391 FD = FD->getCanonicalDecl();
392 D = FD;
393 }
394 return D;
395}
396
Alexey Bataev758e55e2013-09-06 18:03:48 +0000397DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000398 ValueDecl *D) {
399 D = getCanonicalDecl(D);
400 auto *VD = dyn_cast<VarDecl>(D);
401 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000402 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000403 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000404 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
405 // in a region but not in construct]
406 // File-scope or namespace-scope variables referenced in called routines
407 // in the region are shared unless they appear in a threadprivate
408 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000409 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000410 DVar.CKind = OMPC_shared;
411
412 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
413 // in a region but not in construct]
414 // Variables with static storage duration that are declared in called
415 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000416 if (VD && VD->hasGlobalStorage())
417 DVar.CKind = OMPC_shared;
418
419 // Non-static data members are shared by default.
420 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000421 DVar.CKind = OMPC_shared;
422
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 return DVar;
424 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000425
Alexey Bataev758e55e2013-09-06 18:03:48 +0000426 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
428 // in a Construct, C/C++, predetermined, p.1]
429 // Variables with automatic storage duration that are declared in a scope
430 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000431 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
432 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000433 DVar.CKind = OMPC_private;
434 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000435 }
436
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 // Explicitly specified attributes and local variables with predetermined
438 // attributes.
439 if (Iter->SharingMap.count(D)) {
440 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000441 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000443 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444 return DVar;
445 }
446
447 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
448 // in a Construct, C/C++, implicitly determined, p.1]
449 // In a parallel or task construct, the data-sharing attributes of these
450 // variables are determined by the default clause, if present.
451 switch (Iter->DefaultAttr) {
452 case DSA_shared:
453 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000454 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455 return DVar;
456 case DSA_none:
457 return DVar;
458 case DSA_unspecified:
459 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
460 // in a Construct, implicitly determined, p.2]
461 // In a parallel construct, if no default clause is present, these
462 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000463 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000464 if (isOpenMPParallelDirective(DVar.DKind) ||
465 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000466 DVar.CKind = OMPC_shared;
467 return DVar;
468 }
469
470 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
471 // in a Construct, implicitly determined, p.4]
472 // In a task construct, if no default clause is present, a variable that in
473 // the enclosing context is determined to be shared by all implicit tasks
474 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000475 if (DVar.DKind == OMPD_task) {
476 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000477 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000478 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000479 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
480 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000481 // in a Construct, implicitly determined, p.6]
482 // In a task construct, if no default clause is present, a variable
483 // whose data-sharing attribute is not determined by the rules above is
484 // firstprivate.
485 DVarTemp = getDSA(I, D);
486 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000487 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000489 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000490 return DVar;
491 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000492 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000493 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000494 }
495 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000497 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 return DVar;
499 }
500 }
501 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
502 // in a Construct, implicitly determined, p.3]
503 // For constructs other than task, if no default clause is present, these
504 // variables inherit their data-sharing attributes from the enclosing
505 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000506 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000507}
508
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000509Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000510 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000511 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000512 auto It = Stack.back().AlignedMap.find(D);
513 if (It == Stack.back().AlignedMap.end()) {
514 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
515 Stack.back().AlignedMap[D] = NewDE;
516 return nullptr;
517 } else {
518 assert(It->second && "Unexpected nullptr expr in the aligned map");
519 return It->second;
520 }
521 return nullptr;
522}
523
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000524void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000525 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000526 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000527 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000528}
529
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000530unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000531 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000532 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000533 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
534}
535
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000536unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000537 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000538 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000539 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
540 ? Stack[Stack.size() - 2].LCVMap[D]
541 : 0;
542}
543
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000544ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000545 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
546 if (Stack[Stack.size() - 2].LCVMap.size() < I)
547 return nullptr;
548 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
549 if (Pair.second == I)
550 return Pair.first;
551 }
552 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000553}
554
Alexey Bataev90c228f2016-02-08 09:29:13 +0000555void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
556 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000557 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000558 if (A == OMPC_threadprivate) {
559 Stack[0].SharingMap[D].Attributes = A;
560 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000561 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 } else {
563 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
564 Stack.back().SharingMap[D].Attributes = A;
565 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000566 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
567 if (PrivateCopy)
568 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000569 }
570}
571
Alexey Bataeved09d242014-05-28 05:53:51 +0000572bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000573 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000574 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000575 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000576 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000577 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000578 ++I;
579 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000580 if (I == E)
581 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000582 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000583 Scope *CurScope = getCurScope();
584 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000585 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000586 }
587 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000588 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000589 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000590}
591
Alexey Bataev39f915b82015-05-08 10:41:21 +0000592/// \brief Build a variable declaration for OpenMP loop iteration variable.
593static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000594 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000595 DeclContext *DC = SemaRef.CurContext;
596 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
597 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
598 VarDecl *Decl =
599 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000600 if (Attrs) {
601 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
602 I != E; ++I)
603 Decl->addAttr(*I);
604 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000605 Decl->setImplicit();
606 return Decl;
607}
608
609static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
610 SourceLocation Loc,
611 bool RefersToCapture = false) {
612 D->setReferenced();
613 D->markUsed(S.Context);
614 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
615 SourceLocation(), D, RefersToCapture, Loc, Ty,
616 VK_LValue);
617}
618
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000619DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
620 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621 DSAVarData DVar;
622
623 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
624 // in a Construct, C/C++, predetermined, p.1]
625 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000626 auto *VD = dyn_cast<VarDecl>(D);
627 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
628 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000629 SemaRef.getLangOpts().OpenMPUseTLS &&
630 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000631 (VD && VD->getStorageClass() == SC_Register &&
632 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
633 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000634 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000635 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000636 }
637 if (Stack[0].SharingMap.count(D)) {
638 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
639 DVar.CKind = OMPC_threadprivate;
640 return DVar;
641 }
642
643 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000644 // in a Construct, C/C++, predetermined, p.4]
645 // Static data members are shared.
646 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
647 // in a Construct, C/C++, predetermined, p.7]
648 // Variables with static storage duration that are declared in a scope
649 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000650 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000651 DSAVarData DVarTemp =
652 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
653 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000654 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000655
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000656 DVar.CKind = OMPC_shared;
657 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000658 }
659
660 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000661 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
662 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000663 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
664 // in a Construct, C/C++, predetermined, p.6]
665 // Variables with const qualified type having no mutable member are
666 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000667 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000668 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000669 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
670 if (auto *CTD = CTSD->getSpecializedTemplate())
671 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000672 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000673 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
674 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000675 // Variables with const-qualified type having no mutable member may be
676 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000677 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
678 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000679 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
680 return DVar;
681
Alexey Bataev758e55e2013-09-06 18:03:48 +0000682 DVar.CKind = OMPC_shared;
683 return DVar;
684 }
685
Alexey Bataev758e55e2013-09-06 18:03:48 +0000686 // Explicitly specified attributes and local variables with predetermined
687 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000688 auto StartI = std::next(Stack.rbegin());
689 auto EndI = std::prev(Stack.rend());
690 if (FromParent && StartI != EndI) {
691 StartI = std::next(StartI);
692 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000693 auto I = std::prev(StartI);
694 if (I->SharingMap.count(D)) {
695 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000696 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000697 DVar.CKind = I->SharingMap[D].Attributes;
698 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000699 }
700
701 return DVar;
702}
703
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000704DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
705 bool FromParent) {
706 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000707 auto StartI = Stack.rbegin();
708 auto EndI = std::prev(Stack.rend());
709 if (FromParent && StartI != EndI) {
710 StartI = std::next(StartI);
711 }
712 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713}
714
Alexey Bataevf29276e2014-06-18 04:14:57 +0000715template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000716DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000717 DirectivesPredicate DPred,
718 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000719 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000720 auto StartI = std::next(Stack.rbegin());
721 auto EndI = std::prev(Stack.rend());
722 if (FromParent && StartI != EndI) {
723 StartI = std::next(StartI);
724 }
725 for (auto I = StartI, EE = EndI; I != EE; ++I) {
726 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000727 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000728 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000729 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000730 return DVar;
731 }
732 return DSAVarData();
733}
734
Alexey Bataevf29276e2014-06-18 04:14:57 +0000735template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000736DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000737DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000739 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000740 auto StartI = std::next(Stack.rbegin());
741 auto EndI = std::prev(Stack.rend());
742 if (FromParent && StartI != EndI) {
743 StartI = std::next(StartI);
744 }
745 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000746 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000747 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000748 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000749 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000750 return DVar;
751 return DSAVarData();
752 }
753 return DSAVarData();
754}
755
Alexey Bataevaac108a2015-06-23 04:51:00 +0000756bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000757 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000758 unsigned Level) {
759 if (CPred(ClauseKindMode))
760 return true;
761 if (isClauseParsingMode())
762 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000763 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000764 auto StartI = Stack.rbegin();
765 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000766 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000767 return false;
768 std::advance(StartI, Level);
769 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
770 CPred(StartI->SharingMap[D].Attributes);
771}
772
Samuel Antao4be30e92015-10-02 17:14:03 +0000773bool DSAStackTy::hasExplicitDirective(
774 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
775 unsigned Level) {
776 if (isClauseParsingMode())
777 ++Level;
778 auto StartI = Stack.rbegin();
779 auto EndI = std::prev(Stack.rend());
780 if (std::distance(StartI, EndI) <= (int)Level)
781 return false;
782 std::advance(StartI, Level);
783 return DPred(StartI->Directive);
784}
785
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000786template <class NamedDirectivesPredicate>
787bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
788 auto StartI = std::next(Stack.rbegin());
789 auto EndI = std::prev(Stack.rend());
790 if (FromParent && StartI != EndI) {
791 StartI = std::next(StartI);
792 }
793 for (auto I = StartI, EE = EndI; I != EE; ++I) {
794 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
795 return true;
796 }
797 return false;
798}
799
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000800OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
801 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
802 if (I->CurScope == S)
803 return I->Directive;
804 return OMPD_unknown;
805}
806
Alexey Bataev758e55e2013-09-06 18:03:48 +0000807void Sema::InitDataSharingAttributesStack() {
808 VarDataSharingAttributesStack = new DSAStackTy(*this);
809}
810
811#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
812
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000813bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000814 const CapturedRegionScopeInfo *RSI) {
815 assert(LangOpts.OpenMP && "OpenMP is not allowed");
816
817 auto &Ctx = getASTContext();
818 bool IsByRef = true;
819
820 // Find the directive that is associated with the provided scope.
821 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000822 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000823
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000824 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000825 // This table summarizes how a given variable should be passed to the device
826 // given its type and the clauses where it appears. This table is based on
827 // the description in OpenMP 4.5 [2.10.4, target Construct] and
828 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
829 //
830 // =========================================================================
831 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
832 // | |(tofrom:scalar)| | pvt | | | |
833 // =========================================================================
834 // | scl | | | | - | | bycopy|
835 // | scl | | - | x | - | - | bycopy|
836 // | scl | | x | - | - | - | null |
837 // | scl | x | | | - | | byref |
838 // | scl | x | - | x | - | - | bycopy|
839 // | scl | x | x | - | - | - | null |
840 // | scl | | - | - | - | x | byref |
841 // | scl | x | - | - | - | x | byref |
842 //
843 // | agg | n.a. | | | - | | byref |
844 // | agg | n.a. | - | x | - | - | byref |
845 // | agg | n.a. | x | - | - | - | null |
846 // | agg | n.a. | - | - | - | x | byref |
847 // | agg | n.a. | - | - | - | x[] | byref |
848 //
849 // | ptr | n.a. | | | - | | bycopy|
850 // | ptr | n.a. | - | x | - | - | bycopy|
851 // | ptr | n.a. | x | - | - | - | null |
852 // | ptr | n.a. | - | - | - | x | byref |
853 // | ptr | n.a. | - | - | - | x[] | bycopy|
854 // | ptr | n.a. | - | - | x | | bycopy|
855 // | ptr | n.a. | - | - | x | x | bycopy|
856 // | ptr | n.a. | - | - | x | x[] | bycopy|
857 // =========================================================================
858 // Legend:
859 // scl - scalar
860 // ptr - pointer
861 // agg - aggregate
862 // x - applies
863 // - - invalid in this combination
864 // [] - mapped with an array section
865 // byref - should be mapped by reference
866 // byval - should be mapped by value
867 // null - initialize a local variable to null on the device
868 //
869 // Observations:
870 // - All scalar declarations that show up in a map clause have to be passed
871 // by reference, because they may have been mapped in the enclosing data
872 // environment.
873 // - If the scalar value does not fit the size of uintptr, it has to be
874 // passed by reference, regardless the result in the table above.
875 // - For pointers mapped by value that have either an implicit map or an
876 // array section, the runtime library may pass the NULL value to the
877 // device instead of the value passed to it by the compiler.
878
879 // FIXME: Right now, only implicit maps are implemented. Properly mapping
880 // values requires having the map, private, and firstprivate clauses SEMA
881 // and parsing in place, which we don't yet.
882
883 if (Ty->isReferenceType())
884 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
885 IsByRef = !Ty->isScalarType();
886 }
887
888 // When passing data by value, we need to make sure it fits the uintptr size
889 // and alignment, because the runtime library only deals with uintptr types.
890 // If it does not fit the uintptr size, we need to pass the data by reference
891 // instead.
892 if (!IsByRef &&
893 (Ctx.getTypeSizeInChars(Ty) >
894 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000895 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000896 IsByRef = true;
897
898 return IsByRef;
899}
900
Alexey Bataev90c228f2016-02-08 09:29:13 +0000901VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000902 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000903 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000904
905 // If we are attempting to capture a global variable in a directive with
906 // 'target' we return true so that this global is also mapped to the device.
907 //
908 // FIXME: If the declaration is enclosed in a 'declare target' directive,
909 // then it should not be captured. Therefore, an extra check has to be
910 // inserted here once support for 'declare target' is added.
911 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000912 auto *VD = dyn_cast<VarDecl>(D);
913 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000914 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000915 !DSAStack->isClauseParsingMode())
916 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000917 if (DSAStack->getCurScope() &&
918 DSAStack->hasDirective(
919 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
920 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000921 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000922 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000923 false))
924 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000925 }
926
Alexey Bataev48977c32015-08-04 08:10:48 +0000927 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
928 (!DSAStack->isClauseParsingMode() ||
929 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000930 if (DSAStack->isLoopControlVariable(D) ||
931 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000932 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000933 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000934 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000935 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000936 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000937 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000938 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000939 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000940 if (DVarPrivate.CKind != OMPC_unknown)
941 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000942 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000943 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000944}
945
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000946bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000947 assert(LangOpts.OpenMP && "OpenMP is not allowed");
948 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000949 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000950}
951
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000952bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000953 assert(LangOpts.OpenMP && "OpenMP is not allowed");
954 // Return true if the current level is no longer enclosed in a target region.
955
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000956 auto *VD = dyn_cast<VarDecl>(D);
957 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000958 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
959 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000960}
961
Alexey Bataeved09d242014-05-28 05:53:51 +0000962void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000963
964void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
965 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000966 Scope *CurScope, SourceLocation Loc) {
967 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000968 PushExpressionEvaluationContext(PotentiallyEvaluated);
969}
970
Alexey Bataevaac108a2015-06-23 04:51:00 +0000971void Sema::StartOpenMPClause(OpenMPClauseKind K) {
972 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000973}
974
Alexey Bataevaac108a2015-06-23 04:51:00 +0000975void Sema::EndOpenMPClause() {
976 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000977}
978
Alexey Bataev758e55e2013-09-06 18:03:48 +0000979void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000980 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
981 // A variable of class type (or array thereof) that appears in a lastprivate
982 // clause requires an accessible, unambiguous default constructor for the
983 // class type, unless the list item is also specified in a firstprivate
984 // clause.
985 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000986 for (auto *C : D->clauses()) {
987 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
988 SmallVector<Expr *, 8> PrivateCopies;
989 for (auto *DE : Clause->varlists()) {
990 if (DE->isValueDependent() || DE->isTypeDependent()) {
991 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000992 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000993 }
Alexey Bataev74caaf22016-02-20 04:09:36 +0000994 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +0000995 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
996 QualType Type = VD->getType().getNonReferenceType();
997 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000998 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000999 // Generate helper private variable and initialize it with the
1000 // default value. The address of the original variable is replaced
1001 // by the address of the new private variable in CodeGen. This new
1002 // variable is not added to IdResolver, so the code in the OpenMP
1003 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001004 auto *VDPrivate = buildVarDecl(
1005 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001006 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001007 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1008 if (VDPrivate->isInvalidDecl())
1009 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001010 PrivateCopies.push_back(buildDeclRefExpr(
1011 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001012 } else {
1013 // The variable is also a firstprivate, so initialization sequence
1014 // for private copy is generated already.
1015 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001016 }
1017 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001018 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001019 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001020 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001021 }
1022 }
1023 }
1024
Alexey Bataev758e55e2013-09-06 18:03:48 +00001025 DSAStack->pop();
1026 DiscardCleanupsInEvaluationContext();
1027 PopExpressionEvaluationContext();
1028}
1029
Alexander Musman3276a272015-03-21 10:12:56 +00001030static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1031 Expr *NumIterations, Sema &SemaRef,
1032 Scope *S);
1033
Alexey Bataeva769e072013-03-22 06:34:35 +00001034namespace {
1035
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036class VarDeclFilterCCC : public CorrectionCandidateCallback {
1037private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001038 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001039
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001040public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001041 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001042 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001043 NamedDecl *ND = Candidate.getCorrectionDecl();
1044 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1045 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001046 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1047 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001048 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001050 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001051};
Alexey Bataeved09d242014-05-28 05:53:51 +00001052} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053
1054ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1055 CXXScopeSpec &ScopeSpec,
1056 const DeclarationNameInfo &Id) {
1057 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1058 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1059
1060 if (Lookup.isAmbiguous())
1061 return ExprError();
1062
1063 VarDecl *VD;
1064 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001065 if (TypoCorrection Corrected = CorrectTypo(
1066 Id, LookupOrdinaryName, CurScope, nullptr,
1067 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001068 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001069 PDiag(Lookup.empty()
1070 ? diag::err_undeclared_var_use_suggest
1071 : diag::err_omp_expected_var_arg_suggest)
1072 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001073 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001074 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001075 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1076 : diag::err_omp_expected_var_arg)
1077 << Id.getName();
1078 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001079 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001080 } else {
1081 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001082 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001083 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1084 return ExprError();
1085 }
1086 }
1087 Lookup.suppressDiagnostics();
1088
1089 // OpenMP [2.9.2, Syntax, C/C++]
1090 // Variables must be file-scope, namespace-scope, or static block-scope.
1091 if (!VD->hasGlobalStorage()) {
1092 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001093 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1094 bool IsDecl =
1095 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001096 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001097 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1098 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001099 return ExprError();
1100 }
1101
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001102 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1103 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001104 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1105 // A threadprivate directive for file-scope variables must appear outside
1106 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001107 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1108 !getCurLexicalContext()->isTranslationUnit()) {
1109 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001110 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1111 bool IsDecl =
1112 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1113 Diag(VD->getLocation(),
1114 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1115 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001116 return ExprError();
1117 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001118 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1119 // A threadprivate directive for static class member variables must appear
1120 // in the class definition, in the same scope in which the member
1121 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001122 if (CanonicalVD->isStaticDataMember() &&
1123 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1124 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001125 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1126 bool IsDecl =
1127 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1128 Diag(VD->getLocation(),
1129 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1130 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001131 return ExprError();
1132 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001133 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1134 // A threadprivate directive for namespace-scope variables must appear
1135 // outside any definition or declaration other than the namespace
1136 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001137 if (CanonicalVD->getDeclContext()->isNamespace() &&
1138 (!getCurLexicalContext()->isFileContext() ||
1139 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1140 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001141 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1142 bool IsDecl =
1143 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1144 Diag(VD->getLocation(),
1145 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1146 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001147 return ExprError();
1148 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001149 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1150 // A threadprivate directive for static block-scope variables must appear
1151 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001152 if (CanonicalVD->isStaticLocal() && CurScope &&
1153 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001154 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001155 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1156 bool IsDecl =
1157 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1158 Diag(VD->getLocation(),
1159 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1160 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001161 return ExprError();
1162 }
1163
1164 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1165 // A threadprivate directive must lexically precede all references to any
1166 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001167 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001168 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001169 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001170 return ExprError();
1171 }
1172
1173 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001174 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1175 SourceLocation(), VD,
1176 /*RefersToEnclosingVariableOrCapture=*/false,
1177 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178}
1179
Alexey Bataeved09d242014-05-28 05:53:51 +00001180Sema::DeclGroupPtrTy
1181Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1182 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001183 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001184 CurContext->addDecl(D);
1185 return DeclGroupPtrTy::make(DeclGroupRef(D));
1186 }
David Blaikie0403cb12016-01-15 23:43:25 +00001187 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001188}
1189
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001190namespace {
1191class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1192 Sema &SemaRef;
1193
1194public:
1195 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1196 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1197 if (VD->hasLocalStorage()) {
1198 SemaRef.Diag(E->getLocStart(),
1199 diag::err_omp_local_var_in_threadprivate_init)
1200 << E->getSourceRange();
1201 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1202 << VD << VD->getSourceRange();
1203 return true;
1204 }
1205 }
1206 return false;
1207 }
1208 bool VisitStmt(const Stmt *S) {
1209 for (auto Child : S->children()) {
1210 if (Child && Visit(Child))
1211 return true;
1212 }
1213 return false;
1214 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001215 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001216};
1217} // namespace
1218
Alexey Bataeved09d242014-05-28 05:53:51 +00001219OMPThreadPrivateDecl *
1220Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001221 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001222 for (auto &RefExpr : VarList) {
1223 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001224 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1225 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001226
Alexey Bataev376b4a42016-02-09 09:41:09 +00001227 // Mark variable as used.
1228 VD->setReferenced();
1229 VD->markUsed(Context);
1230
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001231 QualType QType = VD->getType();
1232 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1233 // It will be analyzed later.
1234 Vars.push_back(DE);
1235 continue;
1236 }
1237
Alexey Bataeva769e072013-03-22 06:34:35 +00001238 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1239 // A threadprivate variable must not have an incomplete type.
1240 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001241 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001242 continue;
1243 }
1244
1245 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1246 // A threadprivate variable must not have a reference type.
1247 if (VD->getType()->isReferenceType()) {
1248 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001249 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1250 bool IsDecl =
1251 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1252 Diag(VD->getLocation(),
1253 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1254 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001255 continue;
1256 }
1257
Samuel Antaof8b50122015-07-13 22:54:53 +00001258 // Check if this is a TLS variable. If TLS is not being supported, produce
1259 // the corresponding diagnostic.
1260 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1261 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1262 getLangOpts().OpenMPUseTLS &&
1263 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001264 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1265 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001266 Diag(ILoc, diag::err_omp_var_thread_local)
1267 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001268 bool IsDecl =
1269 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1270 Diag(VD->getLocation(),
1271 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1272 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001273 continue;
1274 }
1275
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001276 // Check if initial value of threadprivate variable reference variable with
1277 // local storage (it is not supported by runtime).
1278 if (auto Init = VD->getAnyInitializer()) {
1279 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001280 if (Checker.Visit(Init))
1281 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001282 }
1283
Alexey Bataeved09d242014-05-28 05:53:51 +00001284 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001285 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001286 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1287 Context, SourceRange(Loc, Loc)));
1288 if (auto *ML = Context.getASTMutationListener())
1289 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001290 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001291 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001292 if (!Vars.empty()) {
1293 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1294 Vars);
1295 D->setAccess(AS_public);
1296 }
1297 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001298}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001299
Alexey Bataev7ff55242014-06-19 09:13:45 +00001300static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001301 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001302 bool IsLoopIterVar = false) {
1303 if (DVar.RefExpr) {
1304 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1305 << getOpenMPClauseName(DVar.CKind);
1306 return;
1307 }
1308 enum {
1309 PDSA_StaticMemberShared,
1310 PDSA_StaticLocalVarShared,
1311 PDSA_LoopIterVarPrivate,
1312 PDSA_LoopIterVarLinear,
1313 PDSA_LoopIterVarLastprivate,
1314 PDSA_ConstVarShared,
1315 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001316 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001317 PDSA_LocalVarPrivate,
1318 PDSA_Implicit
1319 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001320 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001321 auto ReportLoc = D->getLocation();
1322 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001323 if (IsLoopIterVar) {
1324 if (DVar.CKind == OMPC_private)
1325 Reason = PDSA_LoopIterVarPrivate;
1326 else if (DVar.CKind == OMPC_lastprivate)
1327 Reason = PDSA_LoopIterVarLastprivate;
1328 else
1329 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001330 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1331 Reason = PDSA_TaskVarFirstprivate;
1332 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001333 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001334 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001335 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001336 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001337 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001338 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001339 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001340 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001341 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001342 ReportHint = true;
1343 Reason = PDSA_LocalVarPrivate;
1344 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001345 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001346 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001347 << Reason << ReportHint
1348 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1349 } else if (DVar.ImplicitDSALoc.isValid()) {
1350 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1351 << getOpenMPClauseName(DVar.CKind);
1352 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001353}
1354
Alexey Bataev758e55e2013-09-06 18:03:48 +00001355namespace {
1356class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1357 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001358 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001359 bool ErrorFound;
1360 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001361 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001362 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001363
Alexey Bataev758e55e2013-09-06 18:03:48 +00001364public:
1365 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001366 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001367 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001368 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1369 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001370
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001371 auto DVar = Stack->getTopDSA(VD, false);
1372 // Check if the variable has explicit DSA set and stop analysis if it so.
1373 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001375 auto ELoc = E->getExprLoc();
1376 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377 // The default(none) clause requires that each variable that is referenced
1378 // in the construct, and does not have a predetermined data-sharing
1379 // attribute, must have its data-sharing attribute explicitly determined
1380 // by being listed in a data-sharing attribute clause.
1381 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001382 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001383 VarsWithInheritedDSA.count(VD) == 0) {
1384 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001385 return;
1386 }
1387
1388 // OpenMP [2.9.3.6, Restrictions, p.2]
1389 // A list item that appears in a reduction clause of the innermost
1390 // enclosing worksharing or parallel construct may not be accessed in an
1391 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001392 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001393 [](OpenMPDirectiveKind K) -> bool {
1394 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001395 isOpenMPWorksharingDirective(K) ||
1396 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001397 },
1398 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001399 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1400 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001401 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1402 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001403 return;
1404 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001405
1406 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001408 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001409 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001410 }
1411 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001412 void VisitMemberExpr(MemberExpr *E) {
1413 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1414 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1415 auto DVar = Stack->getTopDSA(FD, false);
1416 // Check if the variable has explicit DSA set and stop analysis if it
1417 // so.
1418 if (DVar.RefExpr)
1419 return;
1420
1421 auto ELoc = E->getExprLoc();
1422 auto DKind = Stack->getCurrentDirective();
1423 // OpenMP [2.9.3.6, Restrictions, p.2]
1424 // A list item that appears in a reduction clause of the innermost
1425 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001426 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001427 DVar =
1428 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1429 [](OpenMPDirectiveKind K) -> bool {
1430 return isOpenMPParallelDirective(K) ||
1431 isOpenMPWorksharingDirective(K) ||
1432 isOpenMPTeamsDirective(K);
1433 },
1434 false);
1435 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1436 ErrorFound = true;
1437 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1438 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1439 return;
1440 }
1441
1442 // Define implicit data-sharing attributes for task.
1443 DVar = Stack->getImplicitDSA(FD, false);
1444 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1445 ImplicitFirstprivate.push_back(E);
1446 }
1447 }
1448 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001449 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001450 for (auto *C : S->clauses()) {
1451 // Skip analysis of arguments of implicitly defined firstprivate clause
1452 // for task directives.
1453 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1454 for (auto *CC : C->children()) {
1455 if (CC)
1456 Visit(CC);
1457 }
1458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459 }
1460 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001461 for (auto *C : S->children()) {
1462 if (C && !isa<OMPExecutableDirective>(C))
1463 Visit(C);
1464 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001465 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001466
1467 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001468 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001469 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001470 return VarsWithInheritedDSA;
1471 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001472
Alexey Bataev7ff55242014-06-19 09:13:45 +00001473 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1474 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475};
Alexey Bataeved09d242014-05-28 05:53:51 +00001476} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001477
Alexey Bataevbae9a792014-06-27 10:37:06 +00001478void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001479 switch (DKind) {
1480 case OMPD_parallel: {
1481 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001482 QualType KmpInt32PtrTy =
1483 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001484 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001485 std::make_pair(".global_tid.", KmpInt32PtrTy),
1486 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1487 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001488 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001489 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1490 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001491 break;
1492 }
1493 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001494 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001495 std::make_pair(StringRef(), QualType()) // __context with shared vars
1496 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001497 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1498 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001499 break;
1500 }
1501 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001502 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001503 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001504 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001505 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1506 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001507 break;
1508 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001509 case OMPD_for_simd: {
1510 Sema::CapturedParamNameType Params[] = {
1511 std::make_pair(StringRef(), QualType()) // __context with shared vars
1512 };
1513 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1514 Params);
1515 break;
1516 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001517 case OMPD_sections: {
1518 Sema::CapturedParamNameType Params[] = {
1519 std::make_pair(StringRef(), QualType()) // __context with shared vars
1520 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001521 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1522 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001523 break;
1524 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001525 case OMPD_section: {
1526 Sema::CapturedParamNameType Params[] = {
1527 std::make_pair(StringRef(), QualType()) // __context with shared vars
1528 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001529 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1530 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001531 break;
1532 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001533 case OMPD_single: {
1534 Sema::CapturedParamNameType Params[] = {
1535 std::make_pair(StringRef(), QualType()) // __context with shared vars
1536 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001537 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1538 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001539 break;
1540 }
Alexander Musman80c22892014-07-17 08:54:58 +00001541 case OMPD_master: {
1542 Sema::CapturedParamNameType Params[] = {
1543 std::make_pair(StringRef(), QualType()) // __context with shared vars
1544 };
1545 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1546 Params);
1547 break;
1548 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001549 case OMPD_critical: {
1550 Sema::CapturedParamNameType Params[] = {
1551 std::make_pair(StringRef(), QualType()) // __context with shared vars
1552 };
1553 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1554 Params);
1555 break;
1556 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001557 case OMPD_parallel_for: {
1558 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001559 QualType KmpInt32PtrTy =
1560 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001561 Sema::CapturedParamNameType Params[] = {
1562 std::make_pair(".global_tid.", KmpInt32PtrTy),
1563 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1564 std::make_pair(StringRef(), QualType()) // __context with shared vars
1565 };
1566 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1567 Params);
1568 break;
1569 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001570 case OMPD_parallel_for_simd: {
1571 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001572 QualType KmpInt32PtrTy =
1573 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001574 Sema::CapturedParamNameType Params[] = {
1575 std::make_pair(".global_tid.", KmpInt32PtrTy),
1576 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1577 std::make_pair(StringRef(), QualType()) // __context with shared vars
1578 };
1579 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1580 Params);
1581 break;
1582 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001583 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001584 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001585 QualType KmpInt32PtrTy =
1586 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001587 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001588 std::make_pair(".global_tid.", KmpInt32PtrTy),
1589 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001590 std::make_pair(StringRef(), QualType()) // __context with shared vars
1591 };
1592 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1593 Params);
1594 break;
1595 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001596 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001597 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001598 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1599 FunctionProtoType::ExtProtoInfo EPI;
1600 EPI.Variadic = true;
1601 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001602 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001603 std::make_pair(".global_tid.", KmpInt32Ty),
1604 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001605 std::make_pair(".privates.",
1606 Context.VoidPtrTy.withConst().withRestrict()),
1607 std::make_pair(
1608 ".copy_fn.",
1609 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001610 std::make_pair(StringRef(), QualType()) // __context with shared vars
1611 };
1612 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1613 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001614 // Mark this captured region as inlined, because we don't use outlined
1615 // function directly.
1616 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1617 AlwaysInlineAttr::CreateImplicit(
1618 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001619 break;
1620 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001621 case OMPD_ordered: {
1622 Sema::CapturedParamNameType Params[] = {
1623 std::make_pair(StringRef(), QualType()) // __context with shared vars
1624 };
1625 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1626 Params);
1627 break;
1628 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001629 case OMPD_atomic: {
1630 Sema::CapturedParamNameType Params[] = {
1631 std::make_pair(StringRef(), QualType()) // __context with shared vars
1632 };
1633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1634 Params);
1635 break;
1636 }
Michael Wong65f367f2015-07-21 13:44:28 +00001637 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001638 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001639 case OMPD_target_parallel:
1640 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001641 Sema::CapturedParamNameType Params[] = {
1642 std::make_pair(StringRef(), QualType()) // __context with shared vars
1643 };
1644 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1645 Params);
1646 break;
1647 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001648 case OMPD_teams: {
1649 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001650 QualType KmpInt32PtrTy =
1651 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001652 Sema::CapturedParamNameType Params[] = {
1653 std::make_pair(".global_tid.", KmpInt32PtrTy),
1654 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1655 std::make_pair(StringRef(), QualType()) // __context with shared vars
1656 };
1657 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1658 Params);
1659 break;
1660 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001661 case OMPD_taskgroup: {
1662 Sema::CapturedParamNameType Params[] = {
1663 std::make_pair(StringRef(), QualType()) // __context with shared vars
1664 };
1665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1666 Params);
1667 break;
1668 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001669 case OMPD_taskloop: {
1670 Sema::CapturedParamNameType Params[] = {
1671 std::make_pair(StringRef(), QualType()) // __context with shared vars
1672 };
1673 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1674 Params);
1675 break;
1676 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001677 case OMPD_taskloop_simd: {
1678 Sema::CapturedParamNameType Params[] = {
1679 std::make_pair(StringRef(), QualType()) // __context with shared vars
1680 };
1681 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1682 Params);
1683 break;
1684 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001685 case OMPD_distribute: {
1686 Sema::CapturedParamNameType Params[] = {
1687 std::make_pair(StringRef(), QualType()) // __context with shared vars
1688 };
1689 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1690 Params);
1691 break;
1692 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001693 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001694 case OMPD_taskyield:
1695 case OMPD_barrier:
1696 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001697 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001698 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001699 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001700 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001701 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001702 case OMPD_declare_reduction:
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 llvm_unreachable("OpenMP Directive is not allowed");
1704 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001705 llvm_unreachable("Unknown OpenMP directive");
1706 }
1707}
1708
Alexey Bataev3392d762016-02-16 11:18:12 +00001709static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev61205072016-03-02 04:57:40 +00001710 Expr *CaptureExpr, bool WithInit) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001711 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001712 ASTContext &C = S.getASTContext();
1713 Expr *Init = CaptureExpr->IgnoreImpCasts();
1714 QualType Ty = Init->getType();
1715 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1716 if (S.getLangOpts().CPlusPlus)
1717 Ty = C.getLValueReferenceType(Ty);
1718 else {
1719 Ty = C.getPointerType(Ty);
1720 ExprResult Res =
1721 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1722 if (!Res.isUsable())
1723 return nullptr;
1724 Init = Res.get();
1725 }
Alexey Bataev61205072016-03-02 04:57:40 +00001726 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001727 }
1728 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001729 if (!WithInit)
1730 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001731 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001732 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1733 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001734 return CED;
1735}
1736
Alexey Bataev61205072016-03-02 04:57:40 +00001737static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1738 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001739 OMPCapturedExprDecl *CD;
1740 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1741 CD = cast<OMPCapturedExprDecl>(VD);
1742 else
Alexey Bataev61205072016-03-02 04:57:40 +00001743 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit);
Alexey Bataev3392d762016-02-16 11:18:12 +00001744 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1745 SourceLocation());
1746}
1747
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001748static DeclRefExpr *buildCapture(Sema &S, Expr *CaptureExpr) {
Alexey Bataev61205072016-03-02 04:57:40 +00001749 auto *CD =
1750 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1751 CaptureExpr, /*WithInit=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001752 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1753 SourceLocation());
Alexey Bataev4244be22016-02-11 05:35:55 +00001754}
1755
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001756StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1757 ArrayRef<OMPClause *> Clauses) {
1758 if (!S.isUsable()) {
1759 ActOnCapturedRegionError();
1760 return StmtError();
1761 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001762
1763 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001764 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001765 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001766 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001767 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001768 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001769 Clause->getClauseKind() == OMPC_copyprivate ||
1770 (getLangOpts().OpenMPUseTLS &&
1771 getASTContext().getTargetInfo().isTLSSupported() &&
1772 Clause->getClauseKind() == OMPC_copyin)) {
1773 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001774 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001775 for (auto *VarRef : Clause->children()) {
1776 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001777 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001778 }
1779 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001780 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001781 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001782 // Mark all variables in private list clauses as used in inner region.
1783 // Required for proper codegen of combined directives.
1784 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001785 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001786 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1787 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001788 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1789 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001790 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001791 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1792 if (auto *E = C->getPostUpdateExpr())
1793 MarkDeclarationsReferencedInExpr(E);
1794 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001795 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001796 if (Clause->getClauseKind() == OMPC_schedule)
1797 SC = cast<OMPScheduleClause>(Clause);
1798 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001799 OC = cast<OMPOrderedClause>(Clause);
1800 else if (Clause->getClauseKind() == OMPC_linear)
1801 LCs.push_back(cast<OMPLinearClause>(Clause));
1802 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001803 bool ErrorFound = false;
1804 // OpenMP, 2.7.1 Loop Construct, Restrictions
1805 // The nonmonotonic modifier cannot be specified if an ordered clause is
1806 // specified.
1807 if (SC &&
1808 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1809 SC->getSecondScheduleModifier() ==
1810 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1811 OC) {
1812 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1813 ? SC->getFirstScheduleModifierLoc()
1814 : SC->getSecondScheduleModifierLoc(),
1815 diag::err_omp_schedule_nonmonotonic_ordered)
1816 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1817 ErrorFound = true;
1818 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001819 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1820 for (auto *C : LCs) {
1821 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1822 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1823 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001824 ErrorFound = true;
1825 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001826 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1827 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1828 OC->getNumForLoops()) {
1829 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1830 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1831 ErrorFound = true;
1832 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001833 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001834 ActOnCapturedRegionError();
1835 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001836 }
1837 return ActOnCapturedRegionEnd(S.get());
1838}
1839
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001840static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1841 OpenMPDirectiveKind CurrentRegion,
1842 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001843 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001844 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001845 // Allowed nesting of constructs
1846 // +------------------+-----------------+------------------------------------+
1847 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1848 // +------------------+-----------------+------------------------------------+
1849 // | parallel | parallel | * |
1850 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001851 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001852 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001853 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001854 // | parallel | simd | * |
1855 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001856 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001857 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001858 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001859 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001860 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001861 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001862 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001863 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001864 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001865 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001866 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001867 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001868 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001869 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001870 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001871 // | parallel | target parallel | * |
1872 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001873 // | parallel | target enter | * |
1874 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001875 // | parallel | target exit | * |
1876 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001877 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001878 // | parallel | cancellation | |
1879 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001880 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001881 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001882 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001883 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001884 // +------------------+-----------------+------------------------------------+
1885 // | for | parallel | * |
1886 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001887 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001888 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001889 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001890 // | for | simd | * |
1891 // | for | sections | + |
1892 // | for | section | + |
1893 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001894 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001895 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001896 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001897 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001898 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001899 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001900 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001901 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001902 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001903 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001904 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001905 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001906 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001907 // | for | target parallel | * |
1908 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001909 // | for | target enter | * |
1910 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001911 // | for | target exit | * |
1912 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001913 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001914 // | for | cancellation | |
1915 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001916 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001917 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001918 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001919 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001920 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001921 // | master | parallel | * |
1922 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001923 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001924 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001925 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001926 // | master | simd | * |
1927 // | master | sections | + |
1928 // | master | section | + |
1929 // | master | single | + |
1930 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001931 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001932 // | master |parallel sections| * |
1933 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001934 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001935 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001936 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001937 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001938 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001939 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001940 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001941 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001942 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001943 // | master | target parallel | * |
1944 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001945 // | master | target enter | * |
1946 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001947 // | master | target exit | * |
1948 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001949 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001950 // | master | cancellation | |
1951 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001952 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001953 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001954 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001955 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001956 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001957 // | critical | parallel | * |
1958 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001959 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001960 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001961 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001962 // | critical | simd | * |
1963 // | critical | sections | + |
1964 // | critical | section | + |
1965 // | critical | single | + |
1966 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001967 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001968 // | critical |parallel sections| * |
1969 // | critical | task | * |
1970 // | critical | taskyield | * |
1971 // | critical | barrier | + |
1972 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001973 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001974 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001975 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001976 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001977 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001978 // | critical | target parallel | * |
1979 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001980 // | critical | target enter | * |
1981 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001982 // | critical | target exit | * |
1983 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001984 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001985 // | critical | cancellation | |
1986 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001987 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001988 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001989 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001990 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001991 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001992 // | simd | parallel | |
1993 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001994 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001995 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001996 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001997 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001998 // | simd | sections | |
1999 // | simd | section | |
2000 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002001 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002002 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002003 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002004 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002005 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002006 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002007 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002008 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002009 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002010 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002011 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002012 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002013 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002014 // | simd | target parallel | |
2015 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002016 // | simd | target enter | |
2017 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002018 // | simd | target exit | |
2019 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002020 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002021 // | simd | cancellation | |
2022 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002023 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002024 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002025 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002026 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002027 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002028 // | for simd | parallel | |
2029 // | for simd | for | |
2030 // | for simd | for simd | |
2031 // | for simd | master | |
2032 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002033 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002034 // | for simd | sections | |
2035 // | for simd | section | |
2036 // | for simd | single | |
2037 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002038 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002039 // | for simd |parallel sections| |
2040 // | for simd | task | |
2041 // | for simd | taskyield | |
2042 // | for simd | barrier | |
2043 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002044 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002045 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002046 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002047 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002048 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002049 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002050 // | for simd | target parallel | |
2051 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002052 // | for simd | target enter | |
2053 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002054 // | for simd | target exit | |
2055 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002056 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002057 // | for simd | cancellation | |
2058 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002059 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002060 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002061 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002062 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002063 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002064 // | parallel for simd| parallel | |
2065 // | parallel for simd| for | |
2066 // | parallel for simd| for simd | |
2067 // | parallel for simd| master | |
2068 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002069 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002070 // | parallel for simd| sections | |
2071 // | parallel for simd| section | |
2072 // | parallel for simd| single | |
2073 // | parallel for simd| parallel for | |
2074 // | parallel for simd|parallel for simd| |
2075 // | parallel for simd|parallel sections| |
2076 // | parallel for simd| task | |
2077 // | parallel for simd| taskyield | |
2078 // | parallel for simd| barrier | |
2079 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002080 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002081 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002082 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002083 // | parallel for simd| atomic | |
2084 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002085 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002086 // | parallel for simd| target parallel | |
2087 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002088 // | parallel for simd| target enter | |
2089 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002090 // | parallel for simd| target exit | |
2091 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002092 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002093 // | parallel for simd| cancellation | |
2094 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002095 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002096 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002097 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002098 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002099 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002100 // | sections | parallel | * |
2101 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002102 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002103 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002104 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002105 // | sections | simd | * |
2106 // | sections | sections | + |
2107 // | sections | section | * |
2108 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002109 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002110 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002111 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002112 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002113 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002114 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002115 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002116 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002117 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002118 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002119 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002120 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002121 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002122 // | sections | target parallel | * |
2123 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002124 // | sections | target enter | * |
2125 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002126 // | sections | target exit | * |
2127 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002128 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002129 // | sections | cancellation | |
2130 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002131 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002132 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002133 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002134 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002135 // +------------------+-----------------+------------------------------------+
2136 // | section | parallel | * |
2137 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002138 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002139 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002140 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002141 // | section | simd | * |
2142 // | section | sections | + |
2143 // | section | section | + |
2144 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002145 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002146 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002147 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002148 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002149 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002150 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002151 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002152 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002153 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002154 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002155 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002156 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002157 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002158 // | section | target parallel | * |
2159 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002160 // | section | target enter | * |
2161 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002162 // | section | target exit | * |
2163 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002164 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002165 // | section | cancellation | |
2166 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002167 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002168 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002169 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002170 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002171 // +------------------+-----------------+------------------------------------+
2172 // | single | parallel | * |
2173 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002174 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002175 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002176 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002177 // | single | simd | * |
2178 // | single | sections | + |
2179 // | single | section | + |
2180 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002181 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002182 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002183 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002184 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002185 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002186 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002187 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002188 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002189 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002190 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002191 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002192 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002193 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002194 // | single | target parallel | * |
2195 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002196 // | single | target enter | * |
2197 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002198 // | single | target exit | * |
2199 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002200 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002201 // | single | cancellation | |
2202 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002203 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002204 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002205 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002206 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002207 // +------------------+-----------------+------------------------------------+
2208 // | parallel for | parallel | * |
2209 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002210 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002211 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002212 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002213 // | parallel for | simd | * |
2214 // | parallel for | sections | + |
2215 // | parallel for | section | + |
2216 // | parallel for | single | + |
2217 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002218 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002219 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002220 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002221 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002222 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002223 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002224 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002225 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002226 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002227 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002228 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002229 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002230 // | parallel for | target parallel | * |
2231 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002232 // | parallel for | target enter | * |
2233 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002234 // | parallel for | target exit | * |
2235 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002236 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002237 // | parallel for | cancellation | |
2238 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002239 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002240 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002241 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002242 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002243 // +------------------+-----------------+------------------------------------+
2244 // | parallel sections| parallel | * |
2245 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002246 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002247 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002248 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002249 // | parallel sections| simd | * |
2250 // | parallel sections| sections | + |
2251 // | parallel sections| section | * |
2252 // | parallel sections| single | + |
2253 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002254 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002255 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002256 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002257 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002258 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002259 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002260 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002261 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002262 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002263 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002264 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002265 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002266 // | parallel sections| target parallel | * |
2267 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002268 // | parallel sections| target enter | * |
2269 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002270 // | parallel sections| target exit | * |
2271 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002272 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002273 // | parallel sections| cancellation | |
2274 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002275 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002276 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002277 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002278 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002279 // +------------------+-----------------+------------------------------------+
2280 // | task | parallel | * |
2281 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002282 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002283 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002284 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002285 // | task | simd | * |
2286 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002287 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002288 // | task | single | + |
2289 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002290 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002291 // | task |parallel sections| * |
2292 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002293 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002294 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002295 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002296 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002297 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002298 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002299 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002300 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002301 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002302 // | task | target parallel | * |
2303 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002304 // | task | target enter | * |
2305 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002306 // | task | target exit | * |
2307 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002308 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002309 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002310 // | | point | ! |
2311 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002312 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002313 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002314 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002315 // +------------------+-----------------+------------------------------------+
2316 // | ordered | parallel | * |
2317 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002318 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002319 // | ordered | master | * |
2320 // | ordered | critical | * |
2321 // | ordered | simd | * |
2322 // | ordered | sections | + |
2323 // | ordered | section | + |
2324 // | ordered | single | + |
2325 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002326 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002327 // | ordered |parallel sections| * |
2328 // | ordered | task | * |
2329 // | ordered | taskyield | * |
2330 // | ordered | barrier | + |
2331 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002332 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002333 // | ordered | flush | * |
2334 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002335 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002336 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002337 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002338 // | ordered | target parallel | * |
2339 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002340 // | ordered | target enter | * |
2341 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002342 // | ordered | target exit | * |
2343 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002344 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002345 // | ordered | cancellation | |
2346 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002347 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002348 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002349 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002350 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002351 // +------------------+-----------------+------------------------------------+
2352 // | atomic | parallel | |
2353 // | atomic | for | |
2354 // | atomic | for simd | |
2355 // | atomic | master | |
2356 // | atomic | critical | |
2357 // | atomic | simd | |
2358 // | atomic | sections | |
2359 // | atomic | section | |
2360 // | atomic | single | |
2361 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002362 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002363 // | atomic |parallel sections| |
2364 // | atomic | task | |
2365 // | atomic | taskyield | |
2366 // | atomic | barrier | |
2367 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002368 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002369 // | atomic | flush | |
2370 // | atomic | ordered | |
2371 // | atomic | atomic | |
2372 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002373 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002374 // | atomic | target parallel | |
2375 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002376 // | atomic | target enter | |
2377 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002378 // | atomic | target exit | |
2379 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002380 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002381 // | atomic | cancellation | |
2382 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002383 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002384 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002385 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002386 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002387 // +------------------+-----------------+------------------------------------+
2388 // | target | parallel | * |
2389 // | target | for | * |
2390 // | target | for simd | * |
2391 // | target | master | * |
2392 // | target | critical | * |
2393 // | target | simd | * |
2394 // | target | sections | * |
2395 // | target | section | * |
2396 // | target | single | * |
2397 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002398 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002399 // | target |parallel sections| * |
2400 // | target | task | * |
2401 // | target | taskyield | * |
2402 // | target | barrier | * |
2403 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002404 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002405 // | target | flush | * |
2406 // | target | ordered | * |
2407 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002408 // | target | target | |
2409 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002410 // | target | target parallel | |
2411 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002412 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002413 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002414 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002415 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002416 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002417 // | target | cancellation | |
2418 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002419 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002420 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002421 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002422 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002423 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002424 // | target parallel | parallel | * |
2425 // | target parallel | for | * |
2426 // | target parallel | for simd | * |
2427 // | target parallel | master | * |
2428 // | target parallel | critical | * |
2429 // | target parallel | simd | * |
2430 // | target parallel | sections | * |
2431 // | target parallel | section | * |
2432 // | target parallel | single | * |
2433 // | target parallel | parallel for | * |
2434 // | target parallel |parallel for simd| * |
2435 // | target parallel |parallel sections| * |
2436 // | target parallel | task | * |
2437 // | target parallel | taskyield | * |
2438 // | target parallel | barrier | * |
2439 // | target parallel | taskwait | * |
2440 // | target parallel | taskgroup | * |
2441 // | target parallel | flush | * |
2442 // | target parallel | ordered | * |
2443 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002444 // | target parallel | target | |
2445 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002446 // | target parallel | target parallel | |
2447 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002448 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002449 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002450 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002451 // | | data | |
2452 // | target parallel | teams | |
2453 // | target parallel | cancellation | |
2454 // | | point | ! |
2455 // | target parallel | cancel | ! |
2456 // | target parallel | taskloop | * |
2457 // | target parallel | taskloop simd | * |
2458 // | target parallel | distribute | |
2459 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002460 // | target parallel | parallel | * |
2461 // | for | | |
2462 // | target parallel | for | * |
2463 // | for | | |
2464 // | target parallel | for simd | * |
2465 // | for | | |
2466 // | target parallel | master | * |
2467 // | for | | |
2468 // | target parallel | critical | * |
2469 // | for | | |
2470 // | target parallel | simd | * |
2471 // | for | | |
2472 // | target parallel | sections | * |
2473 // | for | | |
2474 // | target parallel | section | * |
2475 // | for | | |
2476 // | target parallel | single | * |
2477 // | for | | |
2478 // | target parallel | parallel for | * |
2479 // | for | | |
2480 // | target parallel |parallel for simd| * |
2481 // | for | | |
2482 // | target parallel |parallel sections| * |
2483 // | for | | |
2484 // | target parallel | task | * |
2485 // | for | | |
2486 // | target parallel | taskyield | * |
2487 // | for | | |
2488 // | target parallel | barrier | * |
2489 // | for | | |
2490 // | target parallel | taskwait | * |
2491 // | for | | |
2492 // | target parallel | taskgroup | * |
2493 // | for | | |
2494 // | target parallel | flush | * |
2495 // | for | | |
2496 // | target parallel | ordered | * |
2497 // | for | | |
2498 // | target parallel | atomic | * |
2499 // | for | | |
2500 // | target parallel | target | |
2501 // | for | | |
2502 // | target parallel | target parallel | |
2503 // | for | | |
2504 // | target parallel | target parallel | |
2505 // | for | for | |
2506 // | target parallel | target enter | |
2507 // | for | data | |
2508 // | target parallel | target exit | |
2509 // | for | data | |
2510 // | target parallel | teams | |
2511 // | for | | |
2512 // | target parallel | cancellation | |
2513 // | for | point | ! |
2514 // | target parallel | cancel | ! |
2515 // | for | | |
2516 // | target parallel | taskloop | * |
2517 // | for | | |
2518 // | target parallel | taskloop simd | * |
2519 // | for | | |
2520 // | target parallel | distribute | |
2521 // | for | | |
2522 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002523 // | teams | parallel | * |
2524 // | teams | for | + |
2525 // | teams | for simd | + |
2526 // | teams | master | + |
2527 // | teams | critical | + |
2528 // | teams | simd | + |
2529 // | teams | sections | + |
2530 // | teams | section | + |
2531 // | teams | single | + |
2532 // | teams | parallel for | * |
2533 // | teams |parallel for simd| * |
2534 // | teams |parallel sections| * |
2535 // | teams | task | + |
2536 // | teams | taskyield | + |
2537 // | teams | barrier | + |
2538 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002539 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002540 // | teams | flush | + |
2541 // | teams | ordered | + |
2542 // | teams | atomic | + |
2543 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002544 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002545 // | teams | target parallel | + |
2546 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002547 // | teams | target enter | + |
2548 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002549 // | teams | target exit | + |
2550 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002551 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002552 // | teams | cancellation | |
2553 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002554 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002555 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002556 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002557 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002558 // +------------------+-----------------+------------------------------------+
2559 // | taskloop | parallel | * |
2560 // | taskloop | for | + |
2561 // | taskloop | for simd | + |
2562 // | taskloop | master | + |
2563 // | taskloop | critical | * |
2564 // | taskloop | simd | * |
2565 // | taskloop | sections | + |
2566 // | taskloop | section | + |
2567 // | taskloop | single | + |
2568 // | taskloop | parallel for | * |
2569 // | taskloop |parallel for simd| * |
2570 // | taskloop |parallel sections| * |
2571 // | taskloop | task | * |
2572 // | taskloop | taskyield | * |
2573 // | taskloop | barrier | + |
2574 // | taskloop | taskwait | * |
2575 // | taskloop | taskgroup | * |
2576 // | taskloop | flush | * |
2577 // | taskloop | ordered | + |
2578 // | taskloop | atomic | * |
2579 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002580 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002581 // | taskloop | target parallel | * |
2582 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002583 // | taskloop | target enter | * |
2584 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002585 // | taskloop | target exit | * |
2586 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002587 // | taskloop | teams | + |
2588 // | taskloop | cancellation | |
2589 // | | point | |
2590 // | taskloop | cancel | |
2591 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002592 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002593 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002594 // | taskloop simd | parallel | |
2595 // | taskloop simd | for | |
2596 // | taskloop simd | for simd | |
2597 // | taskloop simd | master | |
2598 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002599 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002600 // | taskloop simd | sections | |
2601 // | taskloop simd | section | |
2602 // | taskloop simd | single | |
2603 // | taskloop simd | parallel for | |
2604 // | taskloop simd |parallel for simd| |
2605 // | taskloop simd |parallel sections| |
2606 // | taskloop simd | task | |
2607 // | taskloop simd | taskyield | |
2608 // | taskloop simd | barrier | |
2609 // | taskloop simd | taskwait | |
2610 // | taskloop simd | taskgroup | |
2611 // | taskloop simd | flush | |
2612 // | taskloop simd | ordered | + (with simd clause) |
2613 // | taskloop simd | atomic | |
2614 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002615 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002616 // | taskloop simd | target parallel | |
2617 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002618 // | taskloop simd | target enter | |
2619 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002620 // | taskloop simd | target exit | |
2621 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002622 // | taskloop simd | teams | |
2623 // | taskloop simd | cancellation | |
2624 // | | point | |
2625 // | taskloop simd | cancel | |
2626 // | taskloop simd | taskloop | |
2627 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002628 // | taskloop simd | distribute | |
2629 // +------------------+-----------------+------------------------------------+
2630 // | distribute | parallel | * |
2631 // | distribute | for | * |
2632 // | distribute | for simd | * |
2633 // | distribute | master | * |
2634 // | distribute | critical | * |
2635 // | distribute | simd | * |
2636 // | distribute | sections | * |
2637 // | distribute | section | * |
2638 // | distribute | single | * |
2639 // | distribute | parallel for | * |
2640 // | distribute |parallel for simd| * |
2641 // | distribute |parallel sections| * |
2642 // | distribute | task | * |
2643 // | distribute | taskyield | * |
2644 // | distribute | barrier | * |
2645 // | distribute | taskwait | * |
2646 // | distribute | taskgroup | * |
2647 // | distribute | flush | * |
2648 // | distribute | ordered | + |
2649 // | distribute | atomic | * |
2650 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002651 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002652 // | distribute | target parallel | |
2653 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002654 // | distribute | target enter | |
2655 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002656 // | distribute | target exit | |
2657 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002658 // | distribute | teams | |
2659 // | distribute | cancellation | + |
2660 // | | point | |
2661 // | distribute | cancel | + |
2662 // | distribute | taskloop | * |
2663 // | distribute | taskloop simd | * |
2664 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002665 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002666 if (Stack->getCurScope()) {
2667 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002668 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002669 bool NestingProhibited = false;
2670 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002671 enum {
2672 NoRecommend,
2673 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002674 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002675 ShouldBeInTargetRegion,
2676 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002677 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002678 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2679 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002680 // OpenMP [2.16, Nesting of Regions]
2681 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002682 // OpenMP [2.8.1,simd Construct, Restrictions]
2683 // An ordered construct with the simd clause is the only OpenMP construct
2684 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002685 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2686 return true;
2687 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002688 if (ParentRegion == OMPD_atomic) {
2689 // OpenMP [2.16, Nesting of Regions]
2690 // OpenMP constructs may not be nested inside an atomic region.
2691 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2692 return true;
2693 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002694 if (CurrentRegion == OMPD_section) {
2695 // OpenMP [2.7.2, sections Construct, Restrictions]
2696 // Orphaned section directives are prohibited. That is, the section
2697 // directives must appear within the sections construct and must not be
2698 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002699 if (ParentRegion != OMPD_sections &&
2700 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002701 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2702 << (ParentRegion != OMPD_unknown)
2703 << getOpenMPDirectiveName(ParentRegion);
2704 return true;
2705 }
2706 return false;
2707 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002708 // Allow some constructs to be orphaned (they could be used in functions,
2709 // called from OpenMP regions with the required preconditions).
2710 if (ParentRegion == OMPD_unknown)
2711 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002712 if (CurrentRegion == OMPD_cancellation_point ||
2713 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002714 // OpenMP [2.16, Nesting of Regions]
2715 // A cancellation point construct for which construct-type-clause is
2716 // taskgroup must be nested inside a task construct. A cancellation
2717 // point construct for which construct-type-clause is not taskgroup must
2718 // be closely nested inside an OpenMP construct that matches the type
2719 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002720 // A cancel construct for which construct-type-clause is taskgroup must be
2721 // nested inside a task construct. A cancel construct for which
2722 // construct-type-clause is not taskgroup must be closely nested inside an
2723 // OpenMP construct that matches the type specified in
2724 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002725 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002726 !((CancelRegion == OMPD_parallel &&
2727 (ParentRegion == OMPD_parallel ||
2728 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002729 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002730 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2731 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002732 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2733 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002734 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2735 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002736 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002737 // OpenMP [2.16, Nesting of Regions]
2738 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002739 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002740 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002741 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002742 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002743 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2744 // OpenMP [2.16, Nesting of Regions]
2745 // A critical region may not be nested (closely or otherwise) inside a
2746 // critical region with the same name. Note that this restriction is not
2747 // sufficient to prevent deadlock.
2748 SourceLocation PreviousCriticalLoc;
2749 bool DeadLock =
2750 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2751 OpenMPDirectiveKind K,
2752 const DeclarationNameInfo &DNI,
2753 SourceLocation Loc)
2754 ->bool {
2755 if (K == OMPD_critical &&
2756 DNI.getName() == CurrentName.getName()) {
2757 PreviousCriticalLoc = Loc;
2758 return true;
2759 } else
2760 return false;
2761 },
2762 false /* skip top directive */);
2763 if (DeadLock) {
2764 SemaRef.Diag(StartLoc,
2765 diag::err_omp_prohibited_region_critical_same_name)
2766 << CurrentName.getName();
2767 if (PreviousCriticalLoc.isValid())
2768 SemaRef.Diag(PreviousCriticalLoc,
2769 diag::note_omp_previous_critical_region);
2770 return true;
2771 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002772 } else if (CurrentRegion == OMPD_barrier) {
2773 // OpenMP [2.16, Nesting of Regions]
2774 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002775 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002776 NestingProhibited =
2777 isOpenMPWorksharingDirective(ParentRegion) ||
2778 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002779 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002780 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002781 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002782 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002783 // OpenMP [2.16, Nesting of Regions]
2784 // A worksharing region may not be closely nested inside a worksharing,
2785 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002786 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002787 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002788 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002789 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002790 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002791 Recommend = ShouldBeInParallelRegion;
2792 } else if (CurrentRegion == OMPD_ordered) {
2793 // OpenMP [2.16, Nesting of Regions]
2794 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002795 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002796 // An ordered region must be closely nested inside a loop region (or
2797 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002798 // OpenMP [2.8.1,simd Construct, Restrictions]
2799 // An ordered construct with the simd clause is the only OpenMP construct
2800 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002801 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002802 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002803 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002804 !(isOpenMPSimdDirective(ParentRegion) ||
2805 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002806 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002807 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2808 // OpenMP [2.16, Nesting of Regions]
2809 // If specified, a teams construct must be contained within a target
2810 // construct.
2811 NestingProhibited = ParentRegion != OMPD_target;
2812 Recommend = ShouldBeInTargetRegion;
2813 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2814 }
2815 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2816 // OpenMP [2.16, Nesting of Regions]
2817 // distribute, parallel, parallel sections, parallel workshare, and the
2818 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2819 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002820 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2821 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002822 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002823 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002824 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2825 // OpenMP 4.5 [2.17 Nesting of Regions]
2826 // The region associated with the distribute construct must be strictly
2827 // nested inside a teams region
2828 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2829 Recommend = ShouldBeInTeamsRegion;
2830 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002831 if (!NestingProhibited &&
2832 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2833 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2834 // OpenMP 4.5 [2.17 Nesting of Regions]
2835 // If a target, target update, target data, target enter data, or
2836 // target exit data construct is encountered during execution of a
2837 // target region, the behavior is unspecified.
2838 NestingProhibited = Stack->hasDirective(
2839 [&OffendingRegion](OpenMPDirectiveKind K,
2840 const DeclarationNameInfo &DNI,
2841 SourceLocation Loc) -> bool {
2842 if (isOpenMPTargetExecutionDirective(K)) {
2843 OffendingRegion = K;
2844 return true;
2845 } else
2846 return false;
2847 },
2848 false /* don't skip top directive */);
2849 CloseNesting = false;
2850 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002851 if (NestingProhibited) {
2852 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002853 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2854 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002855 return true;
2856 }
2857 }
2858 return false;
2859}
2860
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002861static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2862 ArrayRef<OMPClause *> Clauses,
2863 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2864 bool ErrorFound = false;
2865 unsigned NamedModifiersNumber = 0;
2866 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2867 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002868 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002869 for (const auto *C : Clauses) {
2870 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2871 // At most one if clause without a directive-name-modifier can appear on
2872 // the directive.
2873 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2874 if (FoundNameModifiers[CurNM]) {
2875 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2876 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2877 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2878 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002879 } else if (CurNM != OMPD_unknown) {
2880 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002881 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002882 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002883 FoundNameModifiers[CurNM] = IC;
2884 if (CurNM == OMPD_unknown)
2885 continue;
2886 // Check if the specified name modifier is allowed for the current
2887 // directive.
2888 // At most one if clause with the particular directive-name-modifier can
2889 // appear on the directive.
2890 bool MatchFound = false;
2891 for (auto NM : AllowedNameModifiers) {
2892 if (CurNM == NM) {
2893 MatchFound = true;
2894 break;
2895 }
2896 }
2897 if (!MatchFound) {
2898 S.Diag(IC->getNameModifierLoc(),
2899 diag::err_omp_wrong_if_directive_name_modifier)
2900 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2901 ErrorFound = true;
2902 }
2903 }
2904 }
2905 // If any if clause on the directive includes a directive-name-modifier then
2906 // all if clauses on the directive must include a directive-name-modifier.
2907 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2908 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2909 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2910 diag::err_omp_no_more_if_clause);
2911 } else {
2912 std::string Values;
2913 std::string Sep(", ");
2914 unsigned AllowedCnt = 0;
2915 unsigned TotalAllowedNum =
2916 AllowedNameModifiers.size() - NamedModifiersNumber;
2917 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2918 ++Cnt) {
2919 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2920 if (!FoundNameModifiers[NM]) {
2921 Values += "'";
2922 Values += getOpenMPDirectiveName(NM);
2923 Values += "'";
2924 if (AllowedCnt + 2 == TotalAllowedNum)
2925 Values += " or ";
2926 else if (AllowedCnt + 1 != TotalAllowedNum)
2927 Values += Sep;
2928 ++AllowedCnt;
2929 }
2930 }
2931 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2932 diag::err_omp_unnamed_if_clause)
2933 << (TotalAllowedNum > 1) << Values;
2934 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002935 for (auto Loc : NameModifierLoc) {
2936 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2937 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002938 ErrorFound = true;
2939 }
2940 return ErrorFound;
2941}
2942
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002943StmtResult Sema::ActOnOpenMPExecutableDirective(
2944 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2945 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2946 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002947 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002948 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2949 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002950 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002951
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002952 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002953 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002954 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002955 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002956 if (AStmt) {
2957 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2958
2959 // Check default data sharing attributes for referenced variables.
2960 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2961 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2962 if (DSAChecker.isErrorFound())
2963 return StmtError();
2964 // Generate list of implicitly defined firstprivate variables.
2965 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002966
2967 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2968 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2969 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2970 SourceLocation(), SourceLocation())) {
2971 ClausesWithImplicit.push_back(Implicit);
2972 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2973 DSAChecker.getImplicitFirstprivate().size();
2974 } else
2975 ErrorFound = true;
2976 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002977 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002978
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002979 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002980 switch (Kind) {
2981 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002982 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2983 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002984 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002985 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002986 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002987 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2988 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002989 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002990 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002991 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2992 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002993 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002994 case OMPD_for_simd:
2995 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2996 EndLoc, VarsWithInheritedDSA);
2997 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002998 case OMPD_sections:
2999 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3000 EndLoc);
3001 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003002 case OMPD_section:
3003 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003004 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003005 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3006 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003007 case OMPD_single:
3008 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3009 EndLoc);
3010 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003011 case OMPD_master:
3012 assert(ClausesWithImplicit.empty() &&
3013 "No clauses are allowed for 'omp master' directive");
3014 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3015 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003016 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003017 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3018 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003019 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003020 case OMPD_parallel_for:
3021 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3022 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003023 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003024 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003025 case OMPD_parallel_for_simd:
3026 Res = ActOnOpenMPParallelForSimdDirective(
3027 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003028 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003029 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003030 case OMPD_parallel_sections:
3031 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3032 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003033 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003034 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003035 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003036 Res =
3037 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003038 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003039 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003040 case OMPD_taskyield:
3041 assert(ClausesWithImplicit.empty() &&
3042 "No clauses are allowed for 'omp taskyield' directive");
3043 assert(AStmt == nullptr &&
3044 "No associated statement allowed for 'omp taskyield' directive");
3045 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3046 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003047 case OMPD_barrier:
3048 assert(ClausesWithImplicit.empty() &&
3049 "No clauses are allowed for 'omp barrier' directive");
3050 assert(AStmt == nullptr &&
3051 "No associated statement allowed for 'omp barrier' directive");
3052 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3053 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003054 case OMPD_taskwait:
3055 assert(ClausesWithImplicit.empty() &&
3056 "No clauses are allowed for 'omp taskwait' directive");
3057 assert(AStmt == nullptr &&
3058 "No associated statement allowed for 'omp taskwait' directive");
3059 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3060 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003061 case OMPD_taskgroup:
3062 assert(ClausesWithImplicit.empty() &&
3063 "No clauses are allowed for 'omp taskgroup' directive");
3064 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3065 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003066 case OMPD_flush:
3067 assert(AStmt == nullptr &&
3068 "No associated statement allowed for 'omp flush' directive");
3069 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3070 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003071 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003072 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3073 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003074 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003075 case OMPD_atomic:
3076 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3077 EndLoc);
3078 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003079 case OMPD_teams:
3080 Res =
3081 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3082 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003083 case OMPD_target:
3084 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3085 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003086 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003087 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003088 case OMPD_target_parallel:
3089 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3090 StartLoc, EndLoc);
3091 AllowedNameModifiers.push_back(OMPD_target);
3092 AllowedNameModifiers.push_back(OMPD_parallel);
3093 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003094 case OMPD_target_parallel_for:
3095 Res = ActOnOpenMPTargetParallelForDirective(
3096 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3097 AllowedNameModifiers.push_back(OMPD_target);
3098 AllowedNameModifiers.push_back(OMPD_parallel);
3099 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003100 case OMPD_cancellation_point:
3101 assert(ClausesWithImplicit.empty() &&
3102 "No clauses are allowed for 'omp cancellation point' directive");
3103 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3104 "cancellation point' directive");
3105 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3106 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003107 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003108 assert(AStmt == nullptr &&
3109 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003110 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3111 CancelRegion);
3112 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003113 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003114 case OMPD_target_data:
3115 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3116 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003117 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003118 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003119 case OMPD_target_enter_data:
3120 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3121 EndLoc);
3122 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3123 break;
Samuel Antao72590762016-01-19 20:04:50 +00003124 case OMPD_target_exit_data:
3125 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3126 EndLoc);
3127 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3128 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003129 case OMPD_taskloop:
3130 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3131 EndLoc, VarsWithInheritedDSA);
3132 AllowedNameModifiers.push_back(OMPD_taskloop);
3133 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003134 case OMPD_taskloop_simd:
3135 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3136 EndLoc, VarsWithInheritedDSA);
3137 AllowedNameModifiers.push_back(OMPD_taskloop);
3138 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003139 case OMPD_distribute:
3140 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3141 EndLoc, VarsWithInheritedDSA);
3142 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003143 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003144 case OMPD_declare_reduction:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003145 llvm_unreachable("OpenMP Directive is not allowed");
3146 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003147 llvm_unreachable("Unknown OpenMP directive");
3148 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003149
Alexey Bataev4acb8592014-07-07 13:01:15 +00003150 for (auto P : VarsWithInheritedDSA) {
3151 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3152 << P.first << P.second->getSourceRange();
3153 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003154 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3155
3156 if (!AllowedNameModifiers.empty())
3157 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3158 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003159
Alexey Bataeved09d242014-05-28 05:53:51 +00003160 if (ErrorFound)
3161 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003162 return Res;
3163}
3164
3165StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3166 Stmt *AStmt,
3167 SourceLocation StartLoc,
3168 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003169 if (!AStmt)
3170 return StmtError();
3171
Alexey Bataev9959db52014-05-06 10:08:46 +00003172 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3173 // 1.2.2 OpenMP Language Terminology
3174 // Structured block - An executable statement with a single entry at the
3175 // top and a single exit at the bottom.
3176 // The point of exit cannot be a branch out of the structured block.
3177 // longjmp() and throw() must not violate the entry/exit criteria.
3178 CS->getCapturedDecl()->setNothrow();
3179
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003180 getCurFunction()->setHasBranchProtectedScope();
3181
Alexey Bataev25e5b442015-09-15 12:52:43 +00003182 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3183 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003184}
3185
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003186namespace {
3187/// \brief Helper class for checking canonical form of the OpenMP loops and
3188/// extracting iteration space of each loop in the loop nest, that will be used
3189/// for IR generation.
3190class OpenMPIterationSpaceChecker {
3191 /// \brief Reference to Sema.
3192 Sema &SemaRef;
3193 /// \brief A location for diagnostics (when there is no some better location).
3194 SourceLocation DefaultLoc;
3195 /// \brief A location for diagnostics (when increment is not compatible).
3196 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003197 /// \brief A source location for referring to loop init later.
3198 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003199 /// \brief A source location for referring to condition later.
3200 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003201 /// \brief A source location for referring to increment later.
3202 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003203 /// \brief Loop variable.
3204 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003205 /// \brief Reference to loop variable.
3206 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003207 /// \brief Lower bound (initializer for the var).
3208 Expr *LB;
3209 /// \brief Upper bound.
3210 Expr *UB;
3211 /// \brief Loop step (increment).
3212 Expr *Step;
3213 /// \brief This flag is true when condition is one of:
3214 /// Var < UB
3215 /// Var <= UB
3216 /// UB > Var
3217 /// UB >= Var
3218 bool TestIsLessOp;
3219 /// \brief This flag is true when condition is strict ( < or > ).
3220 bool TestIsStrictOp;
3221 /// \brief This flag is true when step is subtracted on each iteration.
3222 bool SubtractStep;
3223
3224public:
3225 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3226 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003227 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3228 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003229 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3230 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003231 /// \brief Check init-expr for canonical loop form and save loop counter
3232 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003233 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003234 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3235 /// for less/greater and for strict/non-strict comparison.
3236 bool CheckCond(Expr *S);
3237 /// \brief Check incr-expr for canonical loop form and return true if it
3238 /// does not conform, otherwise save loop step (#Step).
3239 bool CheckInc(Expr *S);
3240 /// \brief Return the loop counter variable.
3241 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003242 /// \brief Return the reference expression to loop counter variable.
3243 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003244 /// \brief Source range of the loop init.
3245 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3246 /// \brief Source range of the loop condition.
3247 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3248 /// \brief Source range of the loop increment.
3249 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3250 /// \brief True if the step should be subtracted.
3251 bool ShouldSubtractStep() const { return SubtractStep; }
3252 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003253 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003254 /// \brief Build the precondition expression for the loops.
3255 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003256 /// \brief Build reference expression to the counter be used for codegen.
3257 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003258 /// \brief Build reference expression to the private counter be used for
3259 /// codegen.
3260 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003261 /// \brief Build initization of the counter be used for codegen.
3262 Expr *BuildCounterInit() const;
3263 /// \brief Build step of the counter be used for codegen.
3264 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003265 /// \brief Return true if any expression is dependent.
3266 bool Dependent() const;
3267
3268private:
3269 /// \brief Check the right-hand side of an assignment in the increment
3270 /// expression.
3271 bool CheckIncRHS(Expr *RHS);
3272 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003273 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003274 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003275 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003276 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003277 /// \brief Helper to set loop increment.
3278 bool SetStep(Expr *NewStep, bool Subtract);
3279};
3280
3281bool OpenMPIterationSpaceChecker::Dependent() const {
3282 if (!Var) {
3283 assert(!LB && !UB && !Step);
3284 return false;
3285 }
3286 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3287 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3288}
3289
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003290template <typename T>
3291static T *getExprAsWritten(T *E) {
3292 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3293 E = ExprTemp->getSubExpr();
3294
3295 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3296 E = MTE->GetTemporaryExpr();
3297
3298 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3299 E = Binder->getSubExpr();
3300
3301 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3302 E = ICE->getSubExprAsWritten();
3303 return E->IgnoreParens();
3304}
3305
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003306bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3307 DeclRefExpr *NewVarRefExpr,
3308 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003309 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003310 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3311 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003312 if (!NewVar || !NewLB)
3313 return true;
3314 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003315 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003316 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3317 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003318 if ((Ctor->isCopyOrMoveConstructor() ||
3319 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3320 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003321 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003322 LB = NewLB;
3323 return false;
3324}
3325
3326bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003327 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003328 // State consistency checking to ensure correct usage.
3329 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3330 !TestIsLessOp && !TestIsStrictOp);
3331 if (!NewUB)
3332 return true;
3333 UB = NewUB;
3334 TestIsLessOp = LessOp;
3335 TestIsStrictOp = StrictOp;
3336 ConditionSrcRange = SR;
3337 ConditionLoc = SL;
3338 return false;
3339}
3340
3341bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3342 // State consistency checking to ensure correct usage.
3343 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3344 if (!NewStep)
3345 return true;
3346 if (!NewStep->isValueDependent()) {
3347 // Check that the step is integer expression.
3348 SourceLocation StepLoc = NewStep->getLocStart();
3349 ExprResult Val =
3350 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3351 if (Val.isInvalid())
3352 return true;
3353 NewStep = Val.get();
3354
3355 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3356 // If test-expr is of form var relational-op b and relational-op is < or
3357 // <= then incr-expr must cause var to increase on each iteration of the
3358 // loop. If test-expr is of form var relational-op b and relational-op is
3359 // > or >= then incr-expr must cause var to decrease on each iteration of
3360 // the loop.
3361 // If test-expr is of form b relational-op var and relational-op is < or
3362 // <= then incr-expr must cause var to decrease on each iteration of the
3363 // loop. If test-expr is of form b relational-op var and relational-op is
3364 // > or >= then incr-expr must cause var to increase on each iteration of
3365 // the loop.
3366 llvm::APSInt Result;
3367 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3368 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3369 bool IsConstNeg =
3370 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003371 bool IsConstPos =
3372 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003373 bool IsConstZero = IsConstant && !Result.getBoolValue();
3374 if (UB && (IsConstZero ||
3375 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003376 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003377 SemaRef.Diag(NewStep->getExprLoc(),
3378 diag::err_omp_loop_incr_not_compatible)
3379 << Var << TestIsLessOp << NewStep->getSourceRange();
3380 SemaRef.Diag(ConditionLoc,
3381 diag::note_omp_loop_cond_requres_compatible_incr)
3382 << TestIsLessOp << ConditionSrcRange;
3383 return true;
3384 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003385 if (TestIsLessOp == Subtract) {
3386 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3387 NewStep).get();
3388 Subtract = !Subtract;
3389 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003390 }
3391
3392 Step = NewStep;
3393 SubtractStep = Subtract;
3394 return false;
3395}
3396
Alexey Bataev9c821032015-04-30 04:23:23 +00003397bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003398 // Check init-expr for canonical loop form and save loop counter
3399 // variable - #Var and its initialization value - #LB.
3400 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3401 // var = lb
3402 // integer-type var = lb
3403 // random-access-iterator-type var = lb
3404 // pointer-type var = lb
3405 //
3406 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003407 if (EmitDiags) {
3408 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3409 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003410 return true;
3411 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003412 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003413 if (Expr *E = dyn_cast<Expr>(S))
3414 S = E->IgnoreParens();
3415 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3416 if (BO->getOpcode() == BO_Assign)
3417 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003418 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003419 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003420 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3421 if (DS->isSingleDecl()) {
3422 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003423 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003424 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003425 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003426 SemaRef.Diag(S->getLocStart(),
3427 diag::ext_omp_loop_not_canonical_init)
3428 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003429 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003430 }
3431 }
3432 }
3433 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3434 if (CE->getOperator() == OO_Equal)
3435 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003436 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3437 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003438
Alexey Bataev9c821032015-04-30 04:23:23 +00003439 if (EmitDiags) {
3440 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3441 << S->getSourceRange();
3442 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003443 return true;
3444}
3445
Alexey Bataev23b69422014-06-18 07:08:49 +00003446/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003447/// variable (which may be the loop variable) if possible.
3448static const VarDecl *GetInitVarDecl(const Expr *E) {
3449 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003450 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003451 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003452 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3453 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003454 if ((Ctor->isCopyOrMoveConstructor() ||
3455 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3456 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003457 E = CE->getArg(0)->IgnoreParenImpCasts();
3458 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3459 if (!DRE)
3460 return nullptr;
3461 return dyn_cast<VarDecl>(DRE->getDecl());
3462}
3463
3464bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3465 // Check test-expr for canonical form, save upper-bound UB, flags for
3466 // less/greater and for strict/non-strict comparison.
3467 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3468 // var relational-op b
3469 // b relational-op var
3470 //
3471 if (!S) {
3472 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3473 return true;
3474 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003475 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003476 SourceLocation CondLoc = S->getLocStart();
3477 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3478 if (BO->isRelationalOp()) {
3479 if (GetInitVarDecl(BO->getLHS()) == Var)
3480 return SetUB(BO->getRHS(),
3481 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3482 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3483 BO->getSourceRange(), BO->getOperatorLoc());
3484 if (GetInitVarDecl(BO->getRHS()) == Var)
3485 return SetUB(BO->getLHS(),
3486 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3487 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3488 BO->getSourceRange(), BO->getOperatorLoc());
3489 }
3490 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3491 if (CE->getNumArgs() == 2) {
3492 auto Op = CE->getOperator();
3493 switch (Op) {
3494 case OO_Greater:
3495 case OO_GreaterEqual:
3496 case OO_Less:
3497 case OO_LessEqual:
3498 if (GetInitVarDecl(CE->getArg(0)) == Var)
3499 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3500 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3501 CE->getOperatorLoc());
3502 if (GetInitVarDecl(CE->getArg(1)) == Var)
3503 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3504 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3505 CE->getOperatorLoc());
3506 break;
3507 default:
3508 break;
3509 }
3510 }
3511 }
3512 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3513 << S->getSourceRange() << Var;
3514 return true;
3515}
3516
3517bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3518 // RHS of canonical loop form increment can be:
3519 // var + incr
3520 // incr + var
3521 // var - incr
3522 //
3523 RHS = RHS->IgnoreParenImpCasts();
3524 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3525 if (BO->isAdditiveOp()) {
3526 bool IsAdd = BO->getOpcode() == BO_Add;
3527 if (GetInitVarDecl(BO->getLHS()) == Var)
3528 return SetStep(BO->getRHS(), !IsAdd);
3529 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3530 return SetStep(BO->getLHS(), false);
3531 }
3532 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3533 bool IsAdd = CE->getOperator() == OO_Plus;
3534 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3535 if (GetInitVarDecl(CE->getArg(0)) == Var)
3536 return SetStep(CE->getArg(1), !IsAdd);
3537 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3538 return SetStep(CE->getArg(0), false);
3539 }
3540 }
3541 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3542 << RHS->getSourceRange() << Var;
3543 return true;
3544}
3545
3546bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3547 // Check incr-expr for canonical loop form and return true if it
3548 // does not conform.
3549 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3550 // ++var
3551 // var++
3552 // --var
3553 // var--
3554 // var += incr
3555 // var -= incr
3556 // var = var + incr
3557 // var = incr + var
3558 // var = var - incr
3559 //
3560 if (!S) {
3561 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3562 return true;
3563 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003564 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003565 S = S->IgnoreParens();
3566 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3567 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3568 return SetStep(
3569 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3570 (UO->isDecrementOp() ? -1 : 1)).get(),
3571 false);
3572 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3573 switch (BO->getOpcode()) {
3574 case BO_AddAssign:
3575 case BO_SubAssign:
3576 if (GetInitVarDecl(BO->getLHS()) == Var)
3577 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3578 break;
3579 case BO_Assign:
3580 if (GetInitVarDecl(BO->getLHS()) == Var)
3581 return CheckIncRHS(BO->getRHS());
3582 break;
3583 default:
3584 break;
3585 }
3586 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3587 switch (CE->getOperator()) {
3588 case OO_PlusPlus:
3589 case OO_MinusMinus:
3590 if (GetInitVarDecl(CE->getArg(0)) == Var)
3591 return SetStep(
3592 SemaRef.ActOnIntegerConstant(
3593 CE->getLocStart(),
3594 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3595 false);
3596 break;
3597 case OO_PlusEqual:
3598 case OO_MinusEqual:
3599 if (GetInitVarDecl(CE->getArg(0)) == Var)
3600 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3601 break;
3602 case OO_Equal:
3603 if (GetInitVarDecl(CE->getArg(0)) == Var)
3604 return CheckIncRHS(CE->getArg(1));
3605 break;
3606 default:
3607 break;
3608 }
3609 }
3610 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3611 << S->getSourceRange() << Var;
3612 return true;
3613}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003614
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003615namespace {
3616// Transform variables declared in GNU statement expressions to new ones to
3617// avoid crash on codegen.
3618class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3619 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3620
3621public:
3622 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3623
3624 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3625 if (auto *VD = cast<VarDecl>(D))
3626 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3627 !isa<ImplicitParamDecl>(D)) {
3628 auto *NewVD = VarDecl::Create(
3629 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3630 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3631 VD->getTypeSourceInfo(), VD->getStorageClass());
3632 NewVD->setTSCSpec(VD->getTSCSpec());
3633 NewVD->setInit(VD->getInit());
3634 NewVD->setInitStyle(VD->getInitStyle());
3635 NewVD->setExceptionVariable(VD->isExceptionVariable());
3636 NewVD->setNRVOVariable(VD->isNRVOVariable());
Alexey Bataev11481f52016-02-17 10:29:05 +00003637 NewVD->setCXXForRangeDecl(VD->isCXXForRangeDecl());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003638 NewVD->setConstexpr(VD->isConstexpr());
3639 NewVD->setInitCapture(VD->isInitCapture());
3640 NewVD->setPreviousDeclInSameBlockScope(
3641 VD->isPreviousDeclInSameBlockScope());
3642 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003643 if (VD->hasAttrs())
3644 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003645 transformedLocalDecl(VD, NewVD);
3646 return NewVD;
3647 }
3648 return BaseTransform::TransformDefinition(Loc, D);
3649 }
3650
3651 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3652 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3653 if (E->getDecl() != NewD) {
3654 NewD->setReferenced();
3655 NewD->markUsed(SemaRef.Context);
3656 return DeclRefExpr::Create(
3657 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3658 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3659 E->getNameInfo(), E->getType(), E->getValueKind());
3660 }
3661 return BaseTransform::TransformDeclRefExpr(E);
3662 }
3663};
3664}
3665
Alexander Musmana5f070a2014-10-01 06:03:56 +00003666/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003667Expr *
3668OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3669 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003670 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003671 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003672 auto VarType = Var->getType().getNonReferenceType();
3673 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003674 SemaRef.getLangOpts().CPlusPlus) {
3675 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003676 auto *UBExpr = TestIsLessOp ? UB : LB;
3677 auto *LBExpr = TestIsLessOp ? LB : UB;
3678 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3679 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3680 if (!Upper || !Lower)
3681 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003682 if (!SemaRef.Context.hasSameType(Upper->getType(), UBExpr->getType())) {
3683 Upper = SemaRef
3684 .PerformImplicitConversion(Upper, UBExpr->getType(),
3685 Sema::AA_Converting,
3686 /*AllowExplicit=*/true)
3687 .get();
3688 }
3689 if (!SemaRef.Context.hasSameType(Lower->getType(), LBExpr->getType())) {
3690 Lower = SemaRef
3691 .PerformImplicitConversion(Lower, LBExpr->getType(),
3692 Sema::AA_Converting,
3693 /*AllowExplicit=*/true)
3694 .get();
3695 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003696 if (!Upper || !Lower)
3697 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003698
3699 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3700
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003701 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003702 // BuildBinOp already emitted error, this one is to point user to upper
3703 // and lower bound, and to tell what is passed to 'operator-'.
3704 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3705 << Upper->getSourceRange() << Lower->getSourceRange();
3706 return nullptr;
3707 }
3708 }
3709
3710 if (!Diff.isUsable())
3711 return nullptr;
3712
3713 // Upper - Lower [- 1]
3714 if (TestIsStrictOp)
3715 Diff = SemaRef.BuildBinOp(
3716 S, DefaultLoc, BO_Sub, Diff.get(),
3717 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3718 if (!Diff.isUsable())
3719 return nullptr;
3720
3721 // Upper - Lower [- 1] + Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003722 auto *StepNoImp = Step->IgnoreImplicit();
3723 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003724 if (NewStep.isInvalid())
3725 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003726 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3727 StepNoImp->getType())) {
3728 NewStep = SemaRef.PerformImplicitConversion(
3729 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3730 /*AllowExplicit=*/true);
3731 if (NewStep.isInvalid())
3732 return nullptr;
3733 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003734 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003735 if (!Diff.isUsable())
3736 return nullptr;
3737
3738 // Parentheses (for dumping/debugging purposes only).
3739 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3740 if (!Diff.isUsable())
3741 return nullptr;
3742
3743 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003744 NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003745 if (NewStep.isInvalid())
3746 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003747 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3748 StepNoImp->getType())) {
3749 NewStep = SemaRef.PerformImplicitConversion(
3750 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3751 /*AllowExplicit=*/true);
3752 if (NewStep.isInvalid())
3753 return nullptr;
3754 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003755 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003756 if (!Diff.isUsable())
3757 return nullptr;
3758
Alexander Musman174b3ca2014-10-06 11:16:29 +00003759 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003760 QualType Type = Diff.get()->getType();
3761 auto &C = SemaRef.Context;
3762 bool UseVarType = VarType->hasIntegerRepresentation() &&
3763 C.getTypeSize(Type) > C.getTypeSize(VarType);
3764 if (!Type->isIntegerType() || UseVarType) {
3765 unsigned NewSize =
3766 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3767 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3768 : Type->hasSignedIntegerRepresentation();
3769 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003770 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3771 Diff = SemaRef.PerformImplicitConversion(
3772 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3773 if (!Diff.isUsable())
3774 return nullptr;
3775 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003776 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003777 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003778 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3779 if (NewSize != C.getTypeSize(Type)) {
3780 if (NewSize < C.getTypeSize(Type)) {
3781 assert(NewSize == 64 && "incorrect loop var size");
3782 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3783 << InitSrcRange << ConditionSrcRange;
3784 }
3785 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003786 NewSize, Type->hasSignedIntegerRepresentation() ||
3787 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003788 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3789 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3790 Sema::AA_Converting, true);
3791 if (!Diff.isUsable())
3792 return nullptr;
3793 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003794 }
3795 }
3796
Alexander Musmana5f070a2014-10-01 06:03:56 +00003797 return Diff.get();
3798}
3799
Alexey Bataev62dbb972015-04-22 11:59:37 +00003800Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3801 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3802 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3803 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003804 TransformToNewDefs Transform(SemaRef);
3805
3806 auto NewLB = Transform.TransformExpr(LB);
3807 auto NewUB = Transform.TransformExpr(UB);
3808 if (NewLB.isInvalid() || NewUB.isInvalid())
3809 return Cond;
Alexey Bataev11481f52016-02-17 10:29:05 +00003810 if (!SemaRef.Context.hasSameType(NewLB.get()->getType(), LB->getType())) {
3811 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3812 Sema::AA_Converting,
3813 /*AllowExplicit=*/true);
3814 }
3815 if (!SemaRef.Context.hasSameType(NewUB.get()->getType(), UB->getType())) {
3816 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3817 Sema::AA_Converting,
3818 /*AllowExplicit=*/true);
3819 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003820 if (NewLB.isInvalid() || NewUB.isInvalid())
3821 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003822 auto CondExpr = SemaRef.BuildBinOp(
3823 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3824 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003825 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003826 if (CondExpr.isUsable()) {
Alexey Bataev11481f52016-02-17 10:29:05 +00003827 if (!SemaRef.Context.hasSameType(CondExpr.get()->getType(),
3828 SemaRef.Context.BoolTy))
3829 CondExpr = SemaRef.PerformImplicitConversion(
3830 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3831 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003832 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003833 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3834 // Otherwise use original loop conditon and evaluate it in runtime.
3835 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3836}
3837
Alexander Musmana5f070a2014-10-01 06:03:56 +00003838/// \brief Build reference expression to the counter be used for codegen.
3839Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003840 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3841 DefaultLoc);
3842}
3843
3844Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3845 if (Var && !Var->isInvalidDecl()) {
3846 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003847 auto *PrivateVar =
3848 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3849 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003850 if (PrivateVar->isInvalidDecl())
3851 return nullptr;
3852 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3853 }
3854 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003855}
3856
3857/// \brief Build initization of the counter be used for codegen.
3858Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3859
3860/// \brief Build step of the counter be used for codegen.
3861Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3862
3863/// \brief Iteration space of a single for loop.
3864struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003865 /// \brief Condition of the loop.
3866 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003867 /// \brief This expression calculates the number of iterations in the loop.
3868 /// It is always possible to calculate it before starting the loop.
3869 Expr *NumIterations;
3870 /// \brief The loop counter variable.
3871 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003872 /// \brief Private loop counter variable.
3873 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003874 /// \brief This is initializer for the initial value of #CounterVar.
3875 Expr *CounterInit;
3876 /// \brief This is step for the #CounterVar used to generate its update:
3877 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3878 Expr *CounterStep;
3879 /// \brief Should step be subtracted?
3880 bool Subtract;
3881 /// \brief Source range of the loop init.
3882 SourceRange InitSrcRange;
3883 /// \brief Source range of the loop condition.
3884 SourceRange CondSrcRange;
3885 /// \brief Source range of the loop increment.
3886 SourceRange IncSrcRange;
3887};
3888
Alexey Bataev23b69422014-06-18 07:08:49 +00003889} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890
Alexey Bataev9c821032015-04-30 04:23:23 +00003891void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3892 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3893 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003894 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3895 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003896 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3897 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003898 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003899 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003900 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003901 }
3902}
3903
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003904/// \brief Called on a for stmt to check and extract its iteration space
3905/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003906static bool CheckOpenMPIterationSpace(
3907 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3908 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003909 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003910 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003911 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003912 // OpenMP [2.6, Canonical Loop Form]
3913 // for (init-expr; test-expr; incr-expr) structured-block
3914 auto For = dyn_cast_or_null<ForStmt>(S);
3915 if (!For) {
3916 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003917 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3918 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3919 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3920 if (NestedLoopCount > 1) {
3921 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3922 SemaRef.Diag(DSA.getConstructLoc(),
3923 diag::note_omp_collapse_ordered_expr)
3924 << 2 << CollapseLoopCountExpr->getSourceRange()
3925 << OrderedLoopCountExpr->getSourceRange();
3926 else if (CollapseLoopCountExpr)
3927 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3928 diag::note_omp_collapse_ordered_expr)
3929 << 0 << CollapseLoopCountExpr->getSourceRange();
3930 else
3931 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3932 diag::note_omp_collapse_ordered_expr)
3933 << 1 << OrderedLoopCountExpr->getSourceRange();
3934 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003935 return true;
3936 }
3937 assert(For->getBody());
3938
3939 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3940
3941 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003942 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003943 if (ISC.CheckInit(Init)) {
3944 return true;
3945 }
3946
3947 bool HasErrors = false;
3948
3949 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003950 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003951
3952 // OpenMP [2.6, Canonical Loop Form]
3953 // Var is one of the following:
3954 // A variable of signed or unsigned integer type.
3955 // For C++, a variable of a random access iterator type.
3956 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003957 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003958 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3959 !VarType->isPointerType() &&
3960 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3961 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3962 << SemaRef.getLangOpts().CPlusPlus;
3963 HasErrors = true;
3964 }
3965
Alexey Bataev4acb8592014-07-07 13:01:15 +00003966 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3967 // Construct
3968 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3969 // parallel for construct is (are) private.
3970 // The loop iteration variable in the associated for-loop of a simd construct
3971 // with just one associated for-loop is linear with a constant-linear-step
3972 // that is the increment of the associated for-loop.
3973 // Exclude loop var from the list of variables with implicitly defined data
3974 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003975 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003976
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3978 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003979 // The loop iteration variable in the associated for-loop of a simd construct
3980 // with just one associated for-loop may be listed in a linear clause with a
3981 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003982 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3983 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003984 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003985 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3986 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3987 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003988 auto PredeterminedCKind =
3989 isOpenMPSimdDirective(DKind)
3990 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3991 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003992 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003993 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003994 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003995 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003996 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003997 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3998 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003999 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00004000 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4001 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00004002 if (DVar.RefExpr == nullptr)
4003 DVar.CKind = PredeterminedCKind;
4004 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004005 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004006 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00004007 // Make the loop iteration variable private (for worksharing constructs),
4008 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00004009 // lastprivate (for simd directives with several collapsed or ordered
4010 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00004011 if (DVar.CKind == OMPC_unknown)
4012 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
4013 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00004014 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004015 }
4016
Alexey Bataev7ff55242014-06-19 09:13:45 +00004017 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00004018
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004019 // Check test-expr.
4020 HasErrors |= ISC.CheckCond(For->getCond());
4021
4022 // Check incr-expr.
4023 HasErrors |= ISC.CheckInc(For->getInc());
4024
Alexander Musmana5f070a2014-10-01 06:03:56 +00004025 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004026 return HasErrors;
4027
Alexander Musmana5f070a2014-10-01 06:03:56 +00004028 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004029 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00004030 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004031 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004032 isOpenMPTaskLoopDirective(DKind) ||
4033 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004034 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00004035 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004036 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4037 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4038 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4039 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4040 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4041 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4042
Alexey Bataev62dbb972015-04-22 11:59:37 +00004043 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4044 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004045 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004046 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004047 ResultIterSpace.CounterInit == nullptr ||
4048 ResultIterSpace.CounterStep == nullptr);
4049
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 return HasErrors;
4051}
4052
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004053/// \brief Build 'VarRef = Start.
4054static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4055 ExprResult VarRef, ExprResult Start) {
4056 TransformToNewDefs Transform(SemaRef);
4057 // Build 'VarRef = Start.
Alexey Bataev11481f52016-02-17 10:29:05 +00004058 auto *StartNoImp = Start.get()->IgnoreImplicit();
4059 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004060 if (NewStart.isInvalid())
4061 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004062 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4063 StartNoImp->getType())) {
4064 NewStart = SemaRef.PerformImplicitConversion(
4065 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4066 /*AllowExplicit=*/true);
4067 if (NewStart.isInvalid())
4068 return ExprError();
4069 }
4070 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4071 VarRef.get()->getType())) {
4072 NewStart = SemaRef.PerformImplicitConversion(
4073 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4074 /*AllowExplicit=*/true);
4075 if (!NewStart.isUsable())
4076 return ExprError();
4077 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004078
4079 auto Init =
4080 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4081 return Init;
4082}
4083
Alexander Musmana5f070a2014-10-01 06:03:56 +00004084/// \brief Build 'VarRef = Start + Iter * Step'.
4085static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4086 SourceLocation Loc, ExprResult VarRef,
4087 ExprResult Start, ExprResult Iter,
4088 ExprResult Step, bool Subtract) {
4089 // Add parentheses (for debugging purposes only).
4090 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4091 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4092 !Step.isUsable())
4093 return ExprError();
4094
Alexey Bataev11481f52016-02-17 10:29:05 +00004095 auto *StepNoImp = Step.get()->IgnoreImplicit();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004096 TransformToNewDefs Transform(SemaRef);
Alexey Bataev11481f52016-02-17 10:29:05 +00004097 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004098 if (NewStep.isInvalid())
4099 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004100 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
4101 StepNoImp->getType())) {
4102 NewStep = SemaRef.PerformImplicitConversion(
4103 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
4104 /*AllowExplicit=*/true);
4105 if (NewStep.isInvalid())
4106 return ExprError();
4107 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004108 ExprResult Update =
4109 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004110 if (!Update.isUsable())
4111 return ExprError();
4112
Alexey Bataevc0214e02016-02-16 12:13:49 +00004113 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4114 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev11481f52016-02-17 10:29:05 +00004115 auto *StartNoImp = Start.get()->IgnoreImplicit();
4116 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004117 if (NewStart.isInvalid())
4118 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004119 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4120 StartNoImp->getType())) {
4121 NewStart = SemaRef.PerformImplicitConversion(
4122 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4123 /*AllowExplicit=*/true);
4124 if (NewStart.isInvalid())
4125 return ExprError();
4126 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004127
Alexey Bataevc0214e02016-02-16 12:13:49 +00004128 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4129 ExprResult SavedUpdate = Update;
4130 ExprResult UpdateVal;
4131 if (VarRef.get()->getType()->isOverloadableType() ||
4132 NewStart.get()->getType()->isOverloadableType() ||
4133 Update.get()->getType()->isOverloadableType()) {
4134 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4135 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4136 Update =
4137 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4138 if (Update.isUsable()) {
4139 UpdateVal =
4140 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4141 VarRef.get(), SavedUpdate.get());
4142 if (UpdateVal.isUsable()) {
4143 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4144 UpdateVal.get());
4145 }
4146 }
4147 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4148 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004149
Alexey Bataevc0214e02016-02-16 12:13:49 +00004150 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4151 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4152 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4153 NewStart.get(), SavedUpdate.get());
4154 if (!Update.isUsable())
4155 return ExprError();
4156
Alexey Bataev11481f52016-02-17 10:29:05 +00004157 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4158 VarRef.get()->getType())) {
4159 Update = SemaRef.PerformImplicitConversion(
4160 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4161 if (!Update.isUsable())
4162 return ExprError();
4163 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004164
4165 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4166 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004167 return Update;
4168}
4169
4170/// \brief Convert integer expression \a E to make it have at least \a Bits
4171/// bits.
4172static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4173 Sema &SemaRef) {
4174 if (E == nullptr)
4175 return ExprError();
4176 auto &C = SemaRef.Context;
4177 QualType OldType = E->getType();
4178 unsigned HasBits = C.getTypeSize(OldType);
4179 if (HasBits >= Bits)
4180 return ExprResult(E);
4181 // OK to convert to signed, because new type has more bits than old.
4182 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4183 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4184 true);
4185}
4186
4187/// \brief Check if the given expression \a E is a constant integer that fits
4188/// into \a Bits bits.
4189static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4190 if (E == nullptr)
4191 return false;
4192 llvm::APSInt Result;
4193 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4194 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4195 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004196}
4197
4198/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004199/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4200/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004201static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004202CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4203 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4204 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004205 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004206 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004207 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004208 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004209 // Found 'collapse' clause - calculate collapse number.
4210 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004211 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004212 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004213 }
4214 if (OrderedLoopCountExpr) {
4215 // Found 'ordered' clause - calculate collapse number.
4216 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004217 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4218 if (Result.getLimitedValue() < NestedLoopCount) {
4219 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4220 diag::err_omp_wrong_ordered_loop_count)
4221 << OrderedLoopCountExpr->getSourceRange();
4222 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4223 diag::note_collapse_loop_count)
4224 << CollapseLoopCountExpr->getSourceRange();
4225 }
4226 NestedLoopCount = Result.getLimitedValue();
4227 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004228 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004229 // This is helper routine for loop directives (e.g., 'for', 'simd',
4230 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004231 SmallVector<LoopIterationSpace, 4> IterSpaces;
4232 IterSpaces.resize(NestedLoopCount);
4233 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004235 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004236 NestedLoopCount, CollapseLoopCountExpr,
4237 OrderedLoopCountExpr, VarsWithImplicitDSA,
4238 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004239 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004240 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004241 // OpenMP [2.8.1, simd construct, Restrictions]
4242 // All loops associated with the construct must be perfectly nested; that
4243 // is, there must be no intervening code nor any OpenMP directive between
4244 // any two loops.
4245 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004246 }
4247
Alexander Musmana5f070a2014-10-01 06:03:56 +00004248 Built.clear(/* size */ NestedLoopCount);
4249
4250 if (SemaRef.CurContext->isDependentContext())
4251 return NestedLoopCount;
4252
4253 // An example of what is generated for the following code:
4254 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004255 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004256 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004257 // for (k = 0; k < NK; ++k)
4258 // for (j = J0; j < NJ; j+=2) {
4259 // <loop body>
4260 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004261 //
4262 // We generate the code below.
4263 // Note: the loop body may be outlined in CodeGen.
4264 // Note: some counters may be C++ classes, operator- is used to find number of
4265 // iterations and operator+= to calculate counter value.
4266 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4267 // or i64 is currently supported).
4268 //
4269 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4270 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4271 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4272 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4273 // // similar updates for vars in clauses (e.g. 'linear')
4274 // <loop body (using local i and j)>
4275 // }
4276 // i = NI; // assign final values of counters
4277 // j = NJ;
4278 //
4279
4280 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4281 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004282 // Precondition tests if there is at least one iteration (all conditions are
4283 // true).
4284 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004285 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004286 ExprResult LastIteration32 = WidenIterationCount(
4287 32 /* Bits */, SemaRef.PerformImplicitConversion(
4288 N0->IgnoreImpCasts(), N0->getType(),
4289 Sema::AA_Converting, /*AllowExplicit=*/true)
4290 .get(),
4291 SemaRef);
4292 ExprResult LastIteration64 = WidenIterationCount(
4293 64 /* Bits */, SemaRef.PerformImplicitConversion(
4294 N0->IgnoreImpCasts(), N0->getType(),
4295 Sema::AA_Converting, /*AllowExplicit=*/true)
4296 .get(),
4297 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004298
4299 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4300 return NestedLoopCount;
4301
4302 auto &C = SemaRef.Context;
4303 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4304
4305 Scope *CurScope = DSA.getCurScope();
4306 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004307 if (PreCond.isUsable()) {
4308 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4309 PreCond.get(), IterSpaces[Cnt].PreCond);
4310 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004311 auto N = IterSpaces[Cnt].NumIterations;
4312 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4313 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004314 LastIteration32 = SemaRef.BuildBinOp(
4315 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4316 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4317 Sema::AA_Converting,
4318 /*AllowExplicit=*/true)
4319 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004320 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004321 LastIteration64 = SemaRef.BuildBinOp(
4322 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4323 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4324 Sema::AA_Converting,
4325 /*AllowExplicit=*/true)
4326 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004327 }
4328
4329 // Choose either the 32-bit or 64-bit version.
4330 ExprResult LastIteration = LastIteration64;
4331 if (LastIteration32.isUsable() &&
4332 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4333 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4334 FitsInto(
4335 32 /* Bits */,
4336 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4337 LastIteration64.get(), SemaRef)))
4338 LastIteration = LastIteration32;
4339
4340 if (!LastIteration.isUsable())
4341 return 0;
4342
4343 // Save the number of iterations.
4344 ExprResult NumIterations = LastIteration;
4345 {
4346 LastIteration = SemaRef.BuildBinOp(
4347 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4348 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4349 if (!LastIteration.isUsable())
4350 return 0;
4351 }
4352
4353 // Calculate the last iteration number beforehand instead of doing this on
4354 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4355 llvm::APSInt Result;
4356 bool IsConstant =
4357 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4358 ExprResult CalcLastIteration;
4359 if (!IsConstant) {
4360 SourceLocation SaveLoc;
4361 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004362 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004363 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004364 ExprResult SaveRef = buildDeclRefExpr(
4365 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004366 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4367 SaveRef.get(), LastIteration.get());
4368 LastIteration = SaveRef;
4369
4370 // Prepare SaveRef + 1.
4371 NumIterations = SemaRef.BuildBinOp(
4372 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4373 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4374 if (!NumIterations.isUsable())
4375 return 0;
4376 }
4377
4378 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4379
Alexander Musmanc6388682014-12-15 07:07:06 +00004380 QualType VType = LastIteration.get()->getType();
4381 // Build variables passed into runtime, nesessary for worksharing directives.
4382 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004383 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4384 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004385 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004386 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4387 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004388 SemaRef.AddInitializerToDecl(
4389 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4390 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4391
4392 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004393 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4394 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004395 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4396 /*DirectInit*/ false,
4397 /*TypeMayContainAuto*/ false);
4398
4399 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4400 // This will be used to implement clause 'lastprivate'.
4401 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004402 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4403 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004404 SemaRef.AddInitializerToDecl(
4405 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4406 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4407
4408 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004409 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4410 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004411 SemaRef.AddInitializerToDecl(
4412 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4413 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4414
4415 // Build expression: UB = min(UB, LastIteration)
4416 // It is nesessary for CodeGen of directives with static scheduling.
4417 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4418 UB.get(), LastIteration.get());
4419 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4420 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4421 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4422 CondOp.get());
4423 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4424 }
4425
4426 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004427 ExprResult IV;
4428 ExprResult Init;
4429 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004430 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4431 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004432 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004433 isOpenMPTaskLoopDirective(DKind) ||
4434 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004435 ? LB.get()
4436 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4437 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4438 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004439 }
4440
Alexander Musmanc6388682014-12-15 07:07:06 +00004441 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004442 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004443 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004444 (isOpenMPWorksharingDirective(DKind) ||
4445 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004446 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4447 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4448 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004449
4450 // Loop increment (IV = IV + 1)
4451 SourceLocation IncLoc;
4452 ExprResult Inc =
4453 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4454 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4455 if (!Inc.isUsable())
4456 return 0;
4457 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004458 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4459 if (!Inc.isUsable())
4460 return 0;
4461
4462 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4463 // Used for directives with static scheduling.
4464 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004465 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4466 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004467 // LB + ST
4468 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4469 if (!NextLB.isUsable())
4470 return 0;
4471 // LB = LB + ST
4472 NextLB =
4473 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4474 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4475 if (!NextLB.isUsable())
4476 return 0;
4477 // UB + ST
4478 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4479 if (!NextUB.isUsable())
4480 return 0;
4481 // UB = UB + ST
4482 NextUB =
4483 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4484 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4485 if (!NextUB.isUsable())
4486 return 0;
4487 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004488
4489 // Build updates and final values of the loop counters.
4490 bool HasErrors = false;
4491 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004492 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004493 Built.Updates.resize(NestedLoopCount);
4494 Built.Finals.resize(NestedLoopCount);
4495 {
4496 ExprResult Div;
4497 // Go from inner nested loop to outer.
4498 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4499 LoopIterationSpace &IS = IterSpaces[Cnt];
4500 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4501 // Build: Iter = (IV / Div) % IS.NumIters
4502 // where Div is product of previous iterations' IS.NumIters.
4503 ExprResult Iter;
4504 if (Div.isUsable()) {
4505 Iter =
4506 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4507 } else {
4508 Iter = IV;
4509 assert((Cnt == (int)NestedLoopCount - 1) &&
4510 "unusable div expected on first iteration only");
4511 }
4512
4513 if (Cnt != 0 && Iter.isUsable())
4514 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4515 IS.NumIterations);
4516 if (!Iter.isUsable()) {
4517 HasErrors = true;
4518 break;
4519 }
4520
Alexey Bataev39f915b82015-05-08 10:41:21 +00004521 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4522 auto *CounterVar = buildDeclRefExpr(
4523 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4524 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4525 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004526 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4527 IS.CounterInit);
4528 if (!Init.isUsable()) {
4529 HasErrors = true;
4530 break;
4531 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004532 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004533 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004534 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4535 if (!Update.isUsable()) {
4536 HasErrors = true;
4537 break;
4538 }
4539
4540 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4541 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004542 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004543 IS.NumIterations, IS.CounterStep, IS.Subtract);
4544 if (!Final.isUsable()) {
4545 HasErrors = true;
4546 break;
4547 }
4548
4549 // Build Div for the next iteration: Div <- Div * IS.NumIters
4550 if (Cnt != 0) {
4551 if (Div.isUnset())
4552 Div = IS.NumIterations;
4553 else
4554 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4555 IS.NumIterations);
4556
4557 // Add parentheses (for debugging purposes only).
4558 if (Div.isUsable())
4559 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4560 if (!Div.isUsable()) {
4561 HasErrors = true;
4562 break;
4563 }
4564 }
4565 if (!Update.isUsable() || !Final.isUsable()) {
4566 HasErrors = true;
4567 break;
4568 }
4569 // Save results
4570 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004571 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004572 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004573 Built.Updates[Cnt] = Update.get();
4574 Built.Finals[Cnt] = Final.get();
4575 }
4576 }
4577
4578 if (HasErrors)
4579 return 0;
4580
4581 // Save results
4582 Built.IterationVarRef = IV.get();
4583 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004584 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004585 Built.CalcLastIteration =
4586 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004587 Built.PreCond = PreCond.get();
4588 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004589 Built.Init = Init.get();
4590 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004591 Built.LB = LB.get();
4592 Built.UB = UB.get();
4593 Built.IL = IL.get();
4594 Built.ST = ST.get();
4595 Built.EUB = EUB.get();
4596 Built.NLB = NextLB.get();
4597 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004598
Alexey Bataevabfc0692014-06-25 06:52:00 +00004599 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004600}
4601
Alexey Bataev10e775f2015-07-30 11:36:16 +00004602static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004603 auto CollapseClauses =
4604 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4605 if (CollapseClauses.begin() != CollapseClauses.end())
4606 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004607 return nullptr;
4608}
4609
Alexey Bataev10e775f2015-07-30 11:36:16 +00004610static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004611 auto OrderedClauses =
4612 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4613 if (OrderedClauses.begin() != OrderedClauses.end())
4614 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004615 return nullptr;
4616}
4617
Alexey Bataev66b15b52015-08-21 11:14:16 +00004618static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4619 const Expr *Safelen) {
4620 llvm::APSInt SimdlenRes, SafelenRes;
4621 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4622 Simdlen->isInstantiationDependent() ||
4623 Simdlen->containsUnexpandedParameterPack())
4624 return false;
4625 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4626 Safelen->isInstantiationDependent() ||
4627 Safelen->containsUnexpandedParameterPack())
4628 return false;
4629 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4630 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4631 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4632 // If both simdlen and safelen clauses are specified, the value of the simdlen
4633 // parameter must be less than or equal to the value of the safelen parameter.
4634 if (SimdlenRes > SafelenRes) {
4635 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4636 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4637 return true;
4638 }
4639 return false;
4640}
4641
Alexey Bataev4acb8592014-07-07 13:01:15 +00004642StmtResult Sema::ActOnOpenMPSimdDirective(
4643 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4644 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004645 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004646 if (!AStmt)
4647 return StmtError();
4648
4649 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004650 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004651 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4652 // define the nested loops number.
4653 unsigned NestedLoopCount = CheckOpenMPLoop(
4654 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4655 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004656 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004657 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004658
Alexander Musmana5f070a2014-10-01 06:03:56 +00004659 assert((CurContext->isDependentContext() || B.builtAll()) &&
4660 "omp simd loop exprs were not built");
4661
Alexander Musman3276a272015-03-21 10:12:56 +00004662 if (!CurContext->isDependentContext()) {
4663 // Finalize the clauses that need pre-built expressions for CodeGen.
4664 for (auto C : Clauses) {
4665 if (auto LC = dyn_cast<OMPLinearClause>(C))
4666 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4667 B.NumIterations, *this, CurScope))
4668 return StmtError();
4669 }
4670 }
4671
Alexey Bataev66b15b52015-08-21 11:14:16 +00004672 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4673 // If both simdlen and safelen clauses are specified, the value of the simdlen
4674 // parameter must be less than or equal to the value of the safelen parameter.
4675 OMPSafelenClause *Safelen = nullptr;
4676 OMPSimdlenClause *Simdlen = nullptr;
4677 for (auto *Clause : Clauses) {
4678 if (Clause->getClauseKind() == OMPC_safelen)
4679 Safelen = cast<OMPSafelenClause>(Clause);
4680 else if (Clause->getClauseKind() == OMPC_simdlen)
4681 Simdlen = cast<OMPSimdlenClause>(Clause);
4682 if (Safelen && Simdlen)
4683 break;
4684 }
4685 if (Simdlen && Safelen &&
4686 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4687 Safelen->getSafelen()))
4688 return StmtError();
4689
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004690 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004691 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4692 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004693}
4694
Alexey Bataev4acb8592014-07-07 13:01:15 +00004695StmtResult Sema::ActOnOpenMPForDirective(
4696 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4697 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004698 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004699 if (!AStmt)
4700 return StmtError();
4701
4702 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004703 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004704 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4705 // define the nested loops number.
4706 unsigned NestedLoopCount = CheckOpenMPLoop(
4707 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4708 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004709 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004710 return StmtError();
4711
Alexander Musmana5f070a2014-10-01 06:03:56 +00004712 assert((CurContext->isDependentContext() || B.builtAll()) &&
4713 "omp for loop exprs were not built");
4714
Alexey Bataev54acd402015-08-04 11:18:19 +00004715 if (!CurContext->isDependentContext()) {
4716 // Finalize the clauses that need pre-built expressions for CodeGen.
4717 for (auto C : Clauses) {
4718 if (auto LC = dyn_cast<OMPLinearClause>(C))
4719 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4720 B.NumIterations, *this, CurScope))
4721 return StmtError();
4722 }
4723 }
4724
Alexey Bataevf29276e2014-06-18 04:14:57 +00004725 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004726 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004727 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004728}
4729
Alexander Musmanf82886e2014-09-18 05:12:34 +00004730StmtResult Sema::ActOnOpenMPForSimdDirective(
4731 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4732 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004733 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004734 if (!AStmt)
4735 return StmtError();
4736
4737 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004738 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004739 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4740 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004741 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004742 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4743 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4744 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004745 if (NestedLoopCount == 0)
4746 return StmtError();
4747
Alexander Musmanc6388682014-12-15 07:07:06 +00004748 assert((CurContext->isDependentContext() || B.builtAll()) &&
4749 "omp for simd loop exprs were not built");
4750
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004751 if (!CurContext->isDependentContext()) {
4752 // Finalize the clauses that need pre-built expressions for CodeGen.
4753 for (auto C : Clauses) {
4754 if (auto LC = dyn_cast<OMPLinearClause>(C))
4755 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4756 B.NumIterations, *this, CurScope))
4757 return StmtError();
4758 }
4759 }
4760
Alexey Bataev66b15b52015-08-21 11:14:16 +00004761 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4762 // If both simdlen and safelen clauses are specified, the value of the simdlen
4763 // parameter must be less than or equal to the value of the safelen parameter.
4764 OMPSafelenClause *Safelen = nullptr;
4765 OMPSimdlenClause *Simdlen = nullptr;
4766 for (auto *Clause : Clauses) {
4767 if (Clause->getClauseKind() == OMPC_safelen)
4768 Safelen = cast<OMPSafelenClause>(Clause);
4769 else if (Clause->getClauseKind() == OMPC_simdlen)
4770 Simdlen = cast<OMPSimdlenClause>(Clause);
4771 if (Safelen && Simdlen)
4772 break;
4773 }
4774 if (Simdlen && Safelen &&
4775 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4776 Safelen->getSafelen()))
4777 return StmtError();
4778
Alexander Musmanf82886e2014-09-18 05:12:34 +00004779 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004780 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4781 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004782}
4783
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004784StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4785 Stmt *AStmt,
4786 SourceLocation StartLoc,
4787 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004788 if (!AStmt)
4789 return StmtError();
4790
4791 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004792 auto BaseStmt = AStmt;
4793 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4794 BaseStmt = CS->getCapturedStmt();
4795 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4796 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004797 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004798 return StmtError();
4799 // All associated statements must be '#pragma omp section' except for
4800 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004801 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004802 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4803 if (SectionStmt)
4804 Diag(SectionStmt->getLocStart(),
4805 diag::err_omp_sections_substmt_not_section);
4806 return StmtError();
4807 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004808 cast<OMPSectionDirective>(SectionStmt)
4809 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004810 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004811 } else {
4812 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4813 return StmtError();
4814 }
4815
4816 getCurFunction()->setHasBranchProtectedScope();
4817
Alexey Bataev25e5b442015-09-15 12:52:43 +00004818 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4819 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004820}
4821
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004822StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4823 SourceLocation StartLoc,
4824 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004825 if (!AStmt)
4826 return StmtError();
4827
4828 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004829
4830 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004831 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004832
Alexey Bataev25e5b442015-09-15 12:52:43 +00004833 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4834 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004835}
4836
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004837StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4838 Stmt *AStmt,
4839 SourceLocation StartLoc,
4840 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004841 if (!AStmt)
4842 return StmtError();
4843
4844 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004845
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004846 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004847
Alexey Bataev3255bf32015-01-19 05:20:46 +00004848 // OpenMP [2.7.3, single Construct, Restrictions]
4849 // The copyprivate clause must not be used with the nowait clause.
4850 OMPClause *Nowait = nullptr;
4851 OMPClause *Copyprivate = nullptr;
4852 for (auto *Clause : Clauses) {
4853 if (Clause->getClauseKind() == OMPC_nowait)
4854 Nowait = Clause;
4855 else if (Clause->getClauseKind() == OMPC_copyprivate)
4856 Copyprivate = Clause;
4857 if (Copyprivate && Nowait) {
4858 Diag(Copyprivate->getLocStart(),
4859 diag::err_omp_single_copyprivate_with_nowait);
4860 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4861 return StmtError();
4862 }
4863 }
4864
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004865 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4866}
4867
Alexander Musman80c22892014-07-17 08:54:58 +00004868StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4869 SourceLocation StartLoc,
4870 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004871 if (!AStmt)
4872 return StmtError();
4873
4874 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004875
4876 getCurFunction()->setHasBranchProtectedScope();
4877
4878 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4879}
4880
Alexey Bataev28c75412015-12-15 08:19:24 +00004881StmtResult Sema::ActOnOpenMPCriticalDirective(
4882 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4883 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004884 if (!AStmt)
4885 return StmtError();
4886
4887 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004888
Alexey Bataev28c75412015-12-15 08:19:24 +00004889 bool ErrorFound = false;
4890 llvm::APSInt Hint;
4891 SourceLocation HintLoc;
4892 bool DependentHint = false;
4893 for (auto *C : Clauses) {
4894 if (C->getClauseKind() == OMPC_hint) {
4895 if (!DirName.getName()) {
4896 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4897 ErrorFound = true;
4898 }
4899 Expr *E = cast<OMPHintClause>(C)->getHint();
4900 if (E->isTypeDependent() || E->isValueDependent() ||
4901 E->isInstantiationDependent())
4902 DependentHint = true;
4903 else {
4904 Hint = E->EvaluateKnownConstInt(Context);
4905 HintLoc = C->getLocStart();
4906 }
4907 }
4908 }
4909 if (ErrorFound)
4910 return StmtError();
4911 auto Pair = DSAStack->getCriticalWithHint(DirName);
4912 if (Pair.first && DirName.getName() && !DependentHint) {
4913 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4914 Diag(StartLoc, diag::err_omp_critical_with_hint);
4915 if (HintLoc.isValid()) {
4916 Diag(HintLoc, diag::note_omp_critical_hint_here)
4917 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4918 } else
4919 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4920 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4921 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4922 << 1
4923 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4924 /*Radix=*/10, /*Signed=*/false);
4925 } else
4926 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4927 }
4928 }
4929
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004930 getCurFunction()->setHasBranchProtectedScope();
4931
Alexey Bataev28c75412015-12-15 08:19:24 +00004932 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4933 Clauses, AStmt);
4934 if (!Pair.first && DirName.getName() && !DependentHint)
4935 DSAStack->addCriticalWithHint(Dir, Hint);
4936 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004937}
4938
Alexey Bataev4acb8592014-07-07 13:01:15 +00004939StmtResult Sema::ActOnOpenMPParallelForDirective(
4940 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4941 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004942 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004943 if (!AStmt)
4944 return StmtError();
4945
Alexey Bataev4acb8592014-07-07 13:01:15 +00004946 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4947 // 1.2.2 OpenMP Language Terminology
4948 // Structured block - An executable statement with a single entry at the
4949 // top and a single exit at the bottom.
4950 // The point of exit cannot be a branch out of the structured block.
4951 // longjmp() and throw() must not violate the entry/exit criteria.
4952 CS->getCapturedDecl()->setNothrow();
4953
Alexander Musmanc6388682014-12-15 07:07:06 +00004954 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004955 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4956 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004957 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004958 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4959 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4960 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004961 if (NestedLoopCount == 0)
4962 return StmtError();
4963
Alexander Musmana5f070a2014-10-01 06:03:56 +00004964 assert((CurContext->isDependentContext() || B.builtAll()) &&
4965 "omp parallel for loop exprs were not built");
4966
Alexey Bataev54acd402015-08-04 11:18:19 +00004967 if (!CurContext->isDependentContext()) {
4968 // Finalize the clauses that need pre-built expressions for CodeGen.
4969 for (auto C : Clauses) {
4970 if (auto LC = dyn_cast<OMPLinearClause>(C))
4971 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4972 B.NumIterations, *this, CurScope))
4973 return StmtError();
4974 }
4975 }
4976
Alexey Bataev4acb8592014-07-07 13:01:15 +00004977 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004978 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004979 NestedLoopCount, Clauses, AStmt, B,
4980 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004981}
4982
Alexander Musmane4e893b2014-09-23 09:33:00 +00004983StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4984 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4985 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004986 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004987 if (!AStmt)
4988 return StmtError();
4989
Alexander Musmane4e893b2014-09-23 09:33:00 +00004990 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4991 // 1.2.2 OpenMP Language Terminology
4992 // Structured block - An executable statement with a single entry at the
4993 // top and a single exit at the bottom.
4994 // The point of exit cannot be a branch out of the structured block.
4995 // longjmp() and throw() must not violate the entry/exit criteria.
4996 CS->getCapturedDecl()->setNothrow();
4997
Alexander Musmanc6388682014-12-15 07:07:06 +00004998 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004999 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5000 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005001 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005002 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5003 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5004 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005005 if (NestedLoopCount == 0)
5006 return StmtError();
5007
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005008 if (!CurContext->isDependentContext()) {
5009 // Finalize the clauses that need pre-built expressions for CodeGen.
5010 for (auto C : Clauses) {
5011 if (auto LC = dyn_cast<OMPLinearClause>(C))
5012 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5013 B.NumIterations, *this, CurScope))
5014 return StmtError();
5015 }
5016 }
5017
Alexey Bataev66b15b52015-08-21 11:14:16 +00005018 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5019 // If both simdlen and safelen clauses are specified, the value of the simdlen
5020 // parameter must be less than or equal to the value of the safelen parameter.
5021 OMPSafelenClause *Safelen = nullptr;
5022 OMPSimdlenClause *Simdlen = nullptr;
5023 for (auto *Clause : Clauses) {
5024 if (Clause->getClauseKind() == OMPC_safelen)
5025 Safelen = cast<OMPSafelenClause>(Clause);
5026 else if (Clause->getClauseKind() == OMPC_simdlen)
5027 Simdlen = cast<OMPSimdlenClause>(Clause);
5028 if (Safelen && Simdlen)
5029 break;
5030 }
5031 if (Simdlen && Safelen &&
5032 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5033 Safelen->getSafelen()))
5034 return StmtError();
5035
Alexander Musmane4e893b2014-09-23 09:33:00 +00005036 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005037 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005038 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005039}
5040
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005041StmtResult
5042Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5043 Stmt *AStmt, SourceLocation StartLoc,
5044 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005045 if (!AStmt)
5046 return StmtError();
5047
5048 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005049 auto BaseStmt = AStmt;
5050 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5051 BaseStmt = CS->getCapturedStmt();
5052 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5053 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005054 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005055 return StmtError();
5056 // All associated statements must be '#pragma omp section' except for
5057 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005058 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005059 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5060 if (SectionStmt)
5061 Diag(SectionStmt->getLocStart(),
5062 diag::err_omp_parallel_sections_substmt_not_section);
5063 return StmtError();
5064 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005065 cast<OMPSectionDirective>(SectionStmt)
5066 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005067 }
5068 } else {
5069 Diag(AStmt->getLocStart(),
5070 diag::err_omp_parallel_sections_not_compound_stmt);
5071 return StmtError();
5072 }
5073
5074 getCurFunction()->setHasBranchProtectedScope();
5075
Alexey Bataev25e5b442015-09-15 12:52:43 +00005076 return OMPParallelSectionsDirective::Create(
5077 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005078}
5079
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005080StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5081 Stmt *AStmt, SourceLocation StartLoc,
5082 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005083 if (!AStmt)
5084 return StmtError();
5085
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005086 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5087 // 1.2.2 OpenMP Language Terminology
5088 // Structured block - An executable statement with a single entry at the
5089 // top and a single exit at the bottom.
5090 // The point of exit cannot be a branch out of the structured block.
5091 // longjmp() and throw() must not violate the entry/exit criteria.
5092 CS->getCapturedDecl()->setNothrow();
5093
5094 getCurFunction()->setHasBranchProtectedScope();
5095
Alexey Bataev25e5b442015-09-15 12:52:43 +00005096 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5097 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005098}
5099
Alexey Bataev68446b72014-07-18 07:47:19 +00005100StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5101 SourceLocation EndLoc) {
5102 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5103}
5104
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005105StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5106 SourceLocation EndLoc) {
5107 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5108}
5109
Alexey Bataev2df347a2014-07-18 10:17:07 +00005110StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5111 SourceLocation EndLoc) {
5112 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5113}
5114
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005115StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5116 SourceLocation StartLoc,
5117 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005118 if (!AStmt)
5119 return StmtError();
5120
5121 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005122
5123 getCurFunction()->setHasBranchProtectedScope();
5124
5125 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5126}
5127
Alexey Bataev6125da92014-07-21 11:26:11 +00005128StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5129 SourceLocation StartLoc,
5130 SourceLocation EndLoc) {
5131 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5132 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5133}
5134
Alexey Bataev346265e2015-09-25 10:37:12 +00005135StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5136 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005137 SourceLocation StartLoc,
5138 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005139 OMPClause *DependFound = nullptr;
5140 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005141 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005142 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005143 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005144 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005145 for (auto *C : Clauses) {
5146 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5147 DependFound = C;
5148 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5149 if (DependSourceClause) {
5150 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5151 << getOpenMPDirectiveName(OMPD_ordered)
5152 << getOpenMPClauseName(OMPC_depend) << 2;
5153 ErrorFound = true;
5154 } else
5155 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005156 if (DependSinkClause) {
5157 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5158 << 0;
5159 ErrorFound = true;
5160 }
5161 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5162 if (DependSourceClause) {
5163 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5164 << 1;
5165 ErrorFound = true;
5166 }
5167 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005168 }
5169 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005170 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005171 else if (C->getClauseKind() == OMPC_simd)
5172 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005173 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005174 if (!ErrorFound && !SC &&
5175 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005176 // OpenMP [2.8.1,simd Construct, Restrictions]
5177 // An ordered construct with the simd clause is the only OpenMP construct
5178 // that can appear in the simd region.
5179 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005180 ErrorFound = true;
5181 } else if (DependFound && (TC || SC)) {
5182 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5183 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5184 ErrorFound = true;
5185 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5186 Diag(DependFound->getLocStart(),
5187 diag::err_omp_ordered_directive_without_param);
5188 ErrorFound = true;
5189 } else if (TC || Clauses.empty()) {
5190 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5191 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5192 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5193 << (TC != nullptr);
5194 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5195 ErrorFound = true;
5196 }
5197 }
5198 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005199 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005200
5201 if (AStmt) {
5202 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5203
5204 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005205 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005206
5207 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005208}
5209
Alexey Bataev1d160b12015-03-13 12:27:31 +00005210namespace {
5211/// \brief Helper class for checking expression in 'omp atomic [update]'
5212/// construct.
5213class OpenMPAtomicUpdateChecker {
5214 /// \brief Error results for atomic update expressions.
5215 enum ExprAnalysisErrorCode {
5216 /// \brief A statement is not an expression statement.
5217 NotAnExpression,
5218 /// \brief Expression is not builtin binary or unary operation.
5219 NotABinaryOrUnaryExpression,
5220 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5221 NotAnUnaryIncDecExpression,
5222 /// \brief An expression is not of scalar type.
5223 NotAScalarType,
5224 /// \brief A binary operation is not an assignment operation.
5225 NotAnAssignmentOp,
5226 /// \brief RHS part of the binary operation is not a binary expression.
5227 NotABinaryExpression,
5228 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5229 /// expression.
5230 NotABinaryOperator,
5231 /// \brief RHS binary operation does not have reference to the updated LHS
5232 /// part.
5233 NotAnUpdateExpression,
5234 /// \brief No errors is found.
5235 NoError
5236 };
5237 /// \brief Reference to Sema.
5238 Sema &SemaRef;
5239 /// \brief A location for note diagnostics (when error is found).
5240 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005241 /// \brief 'x' lvalue part of the source atomic expression.
5242 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005243 /// \brief 'expr' rvalue part of the source atomic expression.
5244 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005245 /// \brief Helper expression of the form
5246 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5247 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5248 Expr *UpdateExpr;
5249 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5250 /// important for non-associative operations.
5251 bool IsXLHSInRHSPart;
5252 BinaryOperatorKind Op;
5253 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005254 /// \brief true if the source expression is a postfix unary operation, false
5255 /// if it is a prefix unary operation.
5256 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005257
5258public:
5259 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005260 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005261 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005262 /// \brief Check specified statement that it is suitable for 'atomic update'
5263 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005264 /// expression. If DiagId and NoteId == 0, then only check is performed
5265 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005266 /// \param DiagId Diagnostic which should be emitted if error is found.
5267 /// \param NoteId Diagnostic note for the main error message.
5268 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005269 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005270 /// \brief Return the 'x' lvalue part of the source atomic expression.
5271 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005272 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5273 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005274 /// \brief Return the update expression used in calculation of the updated
5275 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5276 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5277 Expr *getUpdateExpr() const { return UpdateExpr; }
5278 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5279 /// false otherwise.
5280 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5281
Alexey Bataevb78ca832015-04-01 03:33:17 +00005282 /// \brief true if the source expression is a postfix unary operation, false
5283 /// if it is a prefix unary operation.
5284 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5285
Alexey Bataev1d160b12015-03-13 12:27:31 +00005286private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005287 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5288 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005289};
5290} // namespace
5291
5292bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5293 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5294 ExprAnalysisErrorCode ErrorFound = NoError;
5295 SourceLocation ErrorLoc, NoteLoc;
5296 SourceRange ErrorRange, NoteRange;
5297 // Allowed constructs are:
5298 // x = x binop expr;
5299 // x = expr binop x;
5300 if (AtomicBinOp->getOpcode() == BO_Assign) {
5301 X = AtomicBinOp->getLHS();
5302 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5303 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5304 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5305 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5306 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005307 Op = AtomicInnerBinOp->getOpcode();
5308 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005309 auto *LHS = AtomicInnerBinOp->getLHS();
5310 auto *RHS = AtomicInnerBinOp->getRHS();
5311 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5312 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5313 /*Canonical=*/true);
5314 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5315 /*Canonical=*/true);
5316 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5317 /*Canonical=*/true);
5318 if (XId == LHSId) {
5319 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005320 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005321 } else if (XId == RHSId) {
5322 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005323 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005324 } else {
5325 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5326 ErrorRange = AtomicInnerBinOp->getSourceRange();
5327 NoteLoc = X->getExprLoc();
5328 NoteRange = X->getSourceRange();
5329 ErrorFound = NotAnUpdateExpression;
5330 }
5331 } else {
5332 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5333 ErrorRange = AtomicInnerBinOp->getSourceRange();
5334 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5335 NoteRange = SourceRange(NoteLoc, NoteLoc);
5336 ErrorFound = NotABinaryOperator;
5337 }
5338 } else {
5339 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5340 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5341 ErrorFound = NotABinaryExpression;
5342 }
5343 } else {
5344 ErrorLoc = AtomicBinOp->getExprLoc();
5345 ErrorRange = AtomicBinOp->getSourceRange();
5346 NoteLoc = AtomicBinOp->getOperatorLoc();
5347 NoteRange = SourceRange(NoteLoc, NoteLoc);
5348 ErrorFound = NotAnAssignmentOp;
5349 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005350 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005351 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5352 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5353 return true;
5354 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005355 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005356 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005357}
5358
5359bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5360 unsigned NoteId) {
5361 ExprAnalysisErrorCode ErrorFound = NoError;
5362 SourceLocation ErrorLoc, NoteLoc;
5363 SourceRange ErrorRange, NoteRange;
5364 // Allowed constructs are:
5365 // x++;
5366 // x--;
5367 // ++x;
5368 // --x;
5369 // x binop= expr;
5370 // x = x binop expr;
5371 // x = expr binop x;
5372 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5373 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5374 if (AtomicBody->getType()->isScalarType() ||
5375 AtomicBody->isInstantiationDependent()) {
5376 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5377 AtomicBody->IgnoreParenImpCasts())) {
5378 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005379 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005380 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005381 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005382 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005383 X = AtomicCompAssignOp->getLHS();
5384 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005385 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5386 AtomicBody->IgnoreParenImpCasts())) {
5387 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005388 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5389 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005390 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005391 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5392 // Check for Unary Operation
5393 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005394 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005395 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5396 OpLoc = AtomicUnaryOp->getOperatorLoc();
5397 X = AtomicUnaryOp->getSubExpr();
5398 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5399 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005400 } else {
5401 ErrorFound = NotAnUnaryIncDecExpression;
5402 ErrorLoc = AtomicUnaryOp->getExprLoc();
5403 ErrorRange = AtomicUnaryOp->getSourceRange();
5404 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5405 NoteRange = SourceRange(NoteLoc, NoteLoc);
5406 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005407 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005408 ErrorFound = NotABinaryOrUnaryExpression;
5409 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5410 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5411 }
5412 } else {
5413 ErrorFound = NotAScalarType;
5414 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5415 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5416 }
5417 } else {
5418 ErrorFound = NotAnExpression;
5419 NoteLoc = ErrorLoc = S->getLocStart();
5420 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5421 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005422 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005423 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5424 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5425 return true;
5426 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005427 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005428 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005429 // Build an update expression of form 'OpaqueValueExpr(x) binop
5430 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5431 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5432 auto *OVEX = new (SemaRef.getASTContext())
5433 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5434 auto *OVEExpr = new (SemaRef.getASTContext())
5435 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5436 auto Update =
5437 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5438 IsXLHSInRHSPart ? OVEExpr : OVEX);
5439 if (Update.isInvalid())
5440 return true;
5441 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5442 Sema::AA_Casting);
5443 if (Update.isInvalid())
5444 return true;
5445 UpdateExpr = Update.get();
5446 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005447 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005448}
5449
Alexey Bataev0162e452014-07-22 10:10:35 +00005450StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5451 Stmt *AStmt,
5452 SourceLocation StartLoc,
5453 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005454 if (!AStmt)
5455 return StmtError();
5456
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005457 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005458 // 1.2.2 OpenMP Language Terminology
5459 // Structured block - An executable statement with a single entry at the
5460 // top and a single exit at the bottom.
5461 // The point of exit cannot be a branch out of the structured block.
5462 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005463 OpenMPClauseKind AtomicKind = OMPC_unknown;
5464 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005465 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005466 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005467 C->getClauseKind() == OMPC_update ||
5468 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005469 if (AtomicKind != OMPC_unknown) {
5470 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5471 << SourceRange(C->getLocStart(), C->getLocEnd());
5472 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5473 << getOpenMPClauseName(AtomicKind);
5474 } else {
5475 AtomicKind = C->getClauseKind();
5476 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005477 }
5478 }
5479 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005480
Alexey Bataev459dec02014-07-24 06:46:57 +00005481 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005482 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5483 Body = EWC->getSubExpr();
5484
Alexey Bataev62cec442014-11-18 10:14:22 +00005485 Expr *X = nullptr;
5486 Expr *V = nullptr;
5487 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005488 Expr *UE = nullptr;
5489 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005490 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005491 // OpenMP [2.12.6, atomic Construct]
5492 // In the next expressions:
5493 // * x and v (as applicable) are both l-value expressions with scalar type.
5494 // * During the execution of an atomic region, multiple syntactic
5495 // occurrences of x must designate the same storage location.
5496 // * Neither of v and expr (as applicable) may access the storage location
5497 // designated by x.
5498 // * Neither of x and expr (as applicable) may access the storage location
5499 // designated by v.
5500 // * expr is an expression with scalar type.
5501 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5502 // * binop, binop=, ++, and -- are not overloaded operators.
5503 // * The expression x binop expr must be numerically equivalent to x binop
5504 // (expr). This requirement is satisfied if the operators in expr have
5505 // precedence greater than binop, or by using parentheses around expr or
5506 // subexpressions of expr.
5507 // * The expression expr binop x must be numerically equivalent to (expr)
5508 // binop x. This requirement is satisfied if the operators in expr have
5509 // precedence equal to or greater than binop, or by using parentheses around
5510 // expr or subexpressions of expr.
5511 // * For forms that allow multiple occurrences of x, the number of times
5512 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005513 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005514 enum {
5515 NotAnExpression,
5516 NotAnAssignmentOp,
5517 NotAScalarType,
5518 NotAnLValue,
5519 NoError
5520 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005521 SourceLocation ErrorLoc, NoteLoc;
5522 SourceRange ErrorRange, NoteRange;
5523 // If clause is read:
5524 // v = x;
5525 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5526 auto AtomicBinOp =
5527 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5528 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5529 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5530 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5531 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5532 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5533 if (!X->isLValue() || !V->isLValue()) {
5534 auto NotLValueExpr = X->isLValue() ? V : X;
5535 ErrorFound = NotAnLValue;
5536 ErrorLoc = AtomicBinOp->getExprLoc();
5537 ErrorRange = AtomicBinOp->getSourceRange();
5538 NoteLoc = NotLValueExpr->getExprLoc();
5539 NoteRange = NotLValueExpr->getSourceRange();
5540 }
5541 } else if (!X->isInstantiationDependent() ||
5542 !V->isInstantiationDependent()) {
5543 auto NotScalarExpr =
5544 (X->isInstantiationDependent() || X->getType()->isScalarType())
5545 ? V
5546 : X;
5547 ErrorFound = NotAScalarType;
5548 ErrorLoc = AtomicBinOp->getExprLoc();
5549 ErrorRange = AtomicBinOp->getSourceRange();
5550 NoteLoc = NotScalarExpr->getExprLoc();
5551 NoteRange = NotScalarExpr->getSourceRange();
5552 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005553 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005554 ErrorFound = NotAnAssignmentOp;
5555 ErrorLoc = AtomicBody->getExprLoc();
5556 ErrorRange = AtomicBody->getSourceRange();
5557 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5558 : AtomicBody->getExprLoc();
5559 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5560 : AtomicBody->getSourceRange();
5561 }
5562 } else {
5563 ErrorFound = NotAnExpression;
5564 NoteLoc = ErrorLoc = Body->getLocStart();
5565 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005566 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005567 if (ErrorFound != NoError) {
5568 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5569 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005570 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5571 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005572 return StmtError();
5573 } else if (CurContext->isDependentContext())
5574 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005575 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005576 enum {
5577 NotAnExpression,
5578 NotAnAssignmentOp,
5579 NotAScalarType,
5580 NotAnLValue,
5581 NoError
5582 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005583 SourceLocation ErrorLoc, NoteLoc;
5584 SourceRange ErrorRange, NoteRange;
5585 // If clause is write:
5586 // x = expr;
5587 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5588 auto AtomicBinOp =
5589 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5590 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005591 X = AtomicBinOp->getLHS();
5592 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005593 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5594 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5595 if (!X->isLValue()) {
5596 ErrorFound = NotAnLValue;
5597 ErrorLoc = AtomicBinOp->getExprLoc();
5598 ErrorRange = AtomicBinOp->getSourceRange();
5599 NoteLoc = X->getExprLoc();
5600 NoteRange = X->getSourceRange();
5601 }
5602 } else if (!X->isInstantiationDependent() ||
5603 !E->isInstantiationDependent()) {
5604 auto NotScalarExpr =
5605 (X->isInstantiationDependent() || X->getType()->isScalarType())
5606 ? E
5607 : X;
5608 ErrorFound = NotAScalarType;
5609 ErrorLoc = AtomicBinOp->getExprLoc();
5610 ErrorRange = AtomicBinOp->getSourceRange();
5611 NoteLoc = NotScalarExpr->getExprLoc();
5612 NoteRange = NotScalarExpr->getSourceRange();
5613 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005614 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005615 ErrorFound = NotAnAssignmentOp;
5616 ErrorLoc = AtomicBody->getExprLoc();
5617 ErrorRange = AtomicBody->getSourceRange();
5618 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5619 : AtomicBody->getExprLoc();
5620 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5621 : AtomicBody->getSourceRange();
5622 }
5623 } else {
5624 ErrorFound = NotAnExpression;
5625 NoteLoc = ErrorLoc = Body->getLocStart();
5626 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005627 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005628 if (ErrorFound != NoError) {
5629 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5630 << ErrorRange;
5631 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5632 << NoteRange;
5633 return StmtError();
5634 } else if (CurContext->isDependentContext())
5635 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005636 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005637 // If clause is update:
5638 // x++;
5639 // x--;
5640 // ++x;
5641 // --x;
5642 // x binop= expr;
5643 // x = x binop expr;
5644 // x = expr binop x;
5645 OpenMPAtomicUpdateChecker Checker(*this);
5646 if (Checker.checkStatement(
5647 Body, (AtomicKind == OMPC_update)
5648 ? diag::err_omp_atomic_update_not_expression_statement
5649 : diag::err_omp_atomic_not_expression_statement,
5650 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005651 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005652 if (!CurContext->isDependentContext()) {
5653 E = Checker.getExpr();
5654 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005655 UE = Checker.getUpdateExpr();
5656 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005657 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005658 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005659 enum {
5660 NotAnAssignmentOp,
5661 NotACompoundStatement,
5662 NotTwoSubstatements,
5663 NotASpecificExpression,
5664 NoError
5665 } ErrorFound = NoError;
5666 SourceLocation ErrorLoc, NoteLoc;
5667 SourceRange ErrorRange, NoteRange;
5668 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5669 // If clause is a capture:
5670 // v = x++;
5671 // v = x--;
5672 // v = ++x;
5673 // v = --x;
5674 // v = x binop= expr;
5675 // v = x = x binop expr;
5676 // v = x = expr binop x;
5677 auto *AtomicBinOp =
5678 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5679 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5680 V = AtomicBinOp->getLHS();
5681 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5682 OpenMPAtomicUpdateChecker Checker(*this);
5683 if (Checker.checkStatement(
5684 Body, diag::err_omp_atomic_capture_not_expression_statement,
5685 diag::note_omp_atomic_update))
5686 return StmtError();
5687 E = Checker.getExpr();
5688 X = Checker.getX();
5689 UE = Checker.getUpdateExpr();
5690 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5691 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005692 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005693 ErrorLoc = AtomicBody->getExprLoc();
5694 ErrorRange = AtomicBody->getSourceRange();
5695 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5696 : AtomicBody->getExprLoc();
5697 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5698 : AtomicBody->getSourceRange();
5699 ErrorFound = NotAnAssignmentOp;
5700 }
5701 if (ErrorFound != NoError) {
5702 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5703 << ErrorRange;
5704 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5705 return StmtError();
5706 } else if (CurContext->isDependentContext()) {
5707 UE = V = E = X = nullptr;
5708 }
5709 } else {
5710 // If clause is a capture:
5711 // { v = x; x = expr; }
5712 // { v = x; x++; }
5713 // { v = x; x--; }
5714 // { v = x; ++x; }
5715 // { v = x; --x; }
5716 // { v = x; x binop= expr; }
5717 // { v = x; x = x binop expr; }
5718 // { v = x; x = expr binop x; }
5719 // { x++; v = x; }
5720 // { x--; v = x; }
5721 // { ++x; v = x; }
5722 // { --x; v = x; }
5723 // { x binop= expr; v = x; }
5724 // { x = x binop expr; v = x; }
5725 // { x = expr binop x; v = x; }
5726 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5727 // Check that this is { expr1; expr2; }
5728 if (CS->size() == 2) {
5729 auto *First = CS->body_front();
5730 auto *Second = CS->body_back();
5731 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5732 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5733 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5734 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5735 // Need to find what subexpression is 'v' and what is 'x'.
5736 OpenMPAtomicUpdateChecker Checker(*this);
5737 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5738 BinaryOperator *BinOp = nullptr;
5739 if (IsUpdateExprFound) {
5740 BinOp = dyn_cast<BinaryOperator>(First);
5741 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5742 }
5743 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5744 // { v = x; x++; }
5745 // { v = x; x--; }
5746 // { v = x; ++x; }
5747 // { v = x; --x; }
5748 // { v = x; x binop= expr; }
5749 // { v = x; x = x binop expr; }
5750 // { v = x; x = expr binop x; }
5751 // Check that the first expression has form v = x.
5752 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5753 llvm::FoldingSetNodeID XId, PossibleXId;
5754 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5755 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5756 IsUpdateExprFound = XId == PossibleXId;
5757 if (IsUpdateExprFound) {
5758 V = BinOp->getLHS();
5759 X = Checker.getX();
5760 E = Checker.getExpr();
5761 UE = Checker.getUpdateExpr();
5762 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005763 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005764 }
5765 }
5766 if (!IsUpdateExprFound) {
5767 IsUpdateExprFound = !Checker.checkStatement(First);
5768 BinOp = nullptr;
5769 if (IsUpdateExprFound) {
5770 BinOp = dyn_cast<BinaryOperator>(Second);
5771 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5772 }
5773 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5774 // { x++; v = x; }
5775 // { x--; v = x; }
5776 // { ++x; v = x; }
5777 // { --x; v = x; }
5778 // { x binop= expr; v = x; }
5779 // { x = x binop expr; v = x; }
5780 // { x = expr binop x; v = x; }
5781 // Check that the second expression has form v = x.
5782 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5783 llvm::FoldingSetNodeID XId, PossibleXId;
5784 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5785 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5786 IsUpdateExprFound = XId == PossibleXId;
5787 if (IsUpdateExprFound) {
5788 V = BinOp->getLHS();
5789 X = Checker.getX();
5790 E = Checker.getExpr();
5791 UE = Checker.getUpdateExpr();
5792 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005793 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005794 }
5795 }
5796 }
5797 if (!IsUpdateExprFound) {
5798 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005799 auto *FirstExpr = dyn_cast<Expr>(First);
5800 auto *SecondExpr = dyn_cast<Expr>(Second);
5801 if (!FirstExpr || !SecondExpr ||
5802 !(FirstExpr->isInstantiationDependent() ||
5803 SecondExpr->isInstantiationDependent())) {
5804 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5805 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005806 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005807 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5808 : First->getLocStart();
5809 NoteRange = ErrorRange = FirstBinOp
5810 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005811 : SourceRange(ErrorLoc, ErrorLoc);
5812 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005813 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5814 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5815 ErrorFound = NotAnAssignmentOp;
5816 NoteLoc = ErrorLoc = SecondBinOp
5817 ? SecondBinOp->getOperatorLoc()
5818 : Second->getLocStart();
5819 NoteRange = ErrorRange =
5820 SecondBinOp ? SecondBinOp->getSourceRange()
5821 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005822 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005823 auto *PossibleXRHSInFirst =
5824 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5825 auto *PossibleXLHSInSecond =
5826 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5827 llvm::FoldingSetNodeID X1Id, X2Id;
5828 PossibleXRHSInFirst->Profile(X1Id, Context,
5829 /*Canonical=*/true);
5830 PossibleXLHSInSecond->Profile(X2Id, Context,
5831 /*Canonical=*/true);
5832 IsUpdateExprFound = X1Id == X2Id;
5833 if (IsUpdateExprFound) {
5834 V = FirstBinOp->getLHS();
5835 X = SecondBinOp->getLHS();
5836 E = SecondBinOp->getRHS();
5837 UE = nullptr;
5838 IsXLHSInRHSPart = false;
5839 IsPostfixUpdate = true;
5840 } else {
5841 ErrorFound = NotASpecificExpression;
5842 ErrorLoc = FirstBinOp->getExprLoc();
5843 ErrorRange = FirstBinOp->getSourceRange();
5844 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5845 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5846 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005847 }
5848 }
5849 }
5850 }
5851 } else {
5852 NoteLoc = ErrorLoc = Body->getLocStart();
5853 NoteRange = ErrorRange =
5854 SourceRange(Body->getLocStart(), Body->getLocStart());
5855 ErrorFound = NotTwoSubstatements;
5856 }
5857 } else {
5858 NoteLoc = ErrorLoc = Body->getLocStart();
5859 NoteRange = ErrorRange =
5860 SourceRange(Body->getLocStart(), Body->getLocStart());
5861 ErrorFound = NotACompoundStatement;
5862 }
5863 if (ErrorFound != NoError) {
5864 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5865 << ErrorRange;
5866 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5867 return StmtError();
5868 } else if (CurContext->isDependentContext()) {
5869 UE = V = E = X = nullptr;
5870 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005871 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005872 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005873
5874 getCurFunction()->setHasBranchProtectedScope();
5875
Alexey Bataev62cec442014-11-18 10:14:22 +00005876 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005877 X, V, E, UE, IsXLHSInRHSPart,
5878 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005879}
5880
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005881StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5882 Stmt *AStmt,
5883 SourceLocation StartLoc,
5884 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005885 if (!AStmt)
5886 return StmtError();
5887
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005888 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5889 // 1.2.2 OpenMP Language Terminology
5890 // Structured block - An executable statement with a single entry at the
5891 // top and a single exit at the bottom.
5892 // The point of exit cannot be a branch out of the structured block.
5893 // longjmp() and throw() must not violate the entry/exit criteria.
5894 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005895
Alexey Bataev13314bf2014-10-09 04:18:56 +00005896 // OpenMP [2.16, Nesting of Regions]
5897 // If specified, a teams construct must be contained within a target
5898 // construct. That target construct must contain no statements or directives
5899 // outside of the teams construct.
5900 if (DSAStack->hasInnerTeamsRegion()) {
5901 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5902 bool OMPTeamsFound = true;
5903 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5904 auto I = CS->body_begin();
5905 while (I != CS->body_end()) {
5906 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5907 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5908 OMPTeamsFound = false;
5909 break;
5910 }
5911 ++I;
5912 }
5913 assert(I != CS->body_end() && "Not found statement");
5914 S = *I;
5915 }
5916 if (!OMPTeamsFound) {
5917 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5918 Diag(DSAStack->getInnerTeamsRegionLoc(),
5919 diag::note_omp_nested_teams_construct_here);
5920 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5921 << isa<OMPExecutableDirective>(S);
5922 return StmtError();
5923 }
5924 }
5925
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005926 getCurFunction()->setHasBranchProtectedScope();
5927
5928 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5929}
5930
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005931StmtResult
5932Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5933 Stmt *AStmt, SourceLocation StartLoc,
5934 SourceLocation EndLoc) {
5935 if (!AStmt)
5936 return StmtError();
5937
5938 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5939 // 1.2.2 OpenMP Language Terminology
5940 // Structured block - An executable statement with a single entry at the
5941 // top and a single exit at the bottom.
5942 // The point of exit cannot be a branch out of the structured block.
5943 // longjmp() and throw() must not violate the entry/exit criteria.
5944 CS->getCapturedDecl()->setNothrow();
5945
5946 getCurFunction()->setHasBranchProtectedScope();
5947
5948 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5949 AStmt);
5950}
5951
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005952StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5953 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5954 SourceLocation EndLoc,
5955 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5956 if (!AStmt)
5957 return StmtError();
5958
5959 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5960 // 1.2.2 OpenMP Language Terminology
5961 // Structured block - An executable statement with a single entry at the
5962 // top and a single exit at the bottom.
5963 // The point of exit cannot be a branch out of the structured block.
5964 // longjmp() and throw() must not violate the entry/exit criteria.
5965 CS->getCapturedDecl()->setNothrow();
5966
5967 OMPLoopDirective::HelperExprs B;
5968 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5969 // define the nested loops number.
5970 unsigned NestedLoopCount =
5971 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5972 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5973 VarsWithImplicitDSA, B);
5974 if (NestedLoopCount == 0)
5975 return StmtError();
5976
5977 assert((CurContext->isDependentContext() || B.builtAll()) &&
5978 "omp target parallel for loop exprs were not built");
5979
5980 if (!CurContext->isDependentContext()) {
5981 // Finalize the clauses that need pre-built expressions for CodeGen.
5982 for (auto C : Clauses) {
5983 if (auto LC = dyn_cast<OMPLinearClause>(C))
5984 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5985 B.NumIterations, *this, CurScope))
5986 return StmtError();
5987 }
5988 }
5989
5990 getCurFunction()->setHasBranchProtectedScope();
5991 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5992 NestedLoopCount, Clauses, AStmt,
5993 B, DSAStack->isCancelRegion());
5994}
5995
Samuel Antaodf67fc42016-01-19 19:15:56 +00005996/// \brief Check for existence of a map clause in the list of clauses.
5997static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5998 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5999 I != E; ++I) {
6000 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6001 return true;
6002 }
6003 }
6004
6005 return false;
6006}
6007
Michael Wong65f367f2015-07-21 13:44:28 +00006008StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6009 Stmt *AStmt,
6010 SourceLocation StartLoc,
6011 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006012 if (!AStmt)
6013 return StmtError();
6014
6015 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6016
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006017 // OpenMP [2.10.1, Restrictions, p. 97]
6018 // At least one map clause must appear on the directive.
6019 if (!HasMapClause(Clauses)) {
6020 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6021 getOpenMPDirectiveName(OMPD_target_data);
6022 return StmtError();
6023 }
6024
Michael Wong65f367f2015-07-21 13:44:28 +00006025 getCurFunction()->setHasBranchProtectedScope();
6026
6027 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6028 AStmt);
6029}
6030
Samuel Antaodf67fc42016-01-19 19:15:56 +00006031StmtResult
6032Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6033 SourceLocation StartLoc,
6034 SourceLocation EndLoc) {
6035 // OpenMP [2.10.2, Restrictions, p. 99]
6036 // At least one map clause must appear on the directive.
6037 if (!HasMapClause(Clauses)) {
6038 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6039 << getOpenMPDirectiveName(OMPD_target_enter_data);
6040 return StmtError();
6041 }
6042
6043 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6044 Clauses);
6045}
6046
Samuel Antao72590762016-01-19 20:04:50 +00006047StmtResult
6048Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6049 SourceLocation StartLoc,
6050 SourceLocation EndLoc) {
6051 // OpenMP [2.10.3, Restrictions, p. 102]
6052 // At least one map clause must appear on the directive.
6053 if (!HasMapClause(Clauses)) {
6054 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6055 << getOpenMPDirectiveName(OMPD_target_exit_data);
6056 return StmtError();
6057 }
6058
6059 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6060}
6061
Alexey Bataev13314bf2014-10-09 04:18:56 +00006062StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6063 Stmt *AStmt, SourceLocation StartLoc,
6064 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006065 if (!AStmt)
6066 return StmtError();
6067
Alexey Bataev13314bf2014-10-09 04:18:56 +00006068 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6069 // 1.2.2 OpenMP Language Terminology
6070 // Structured block - An executable statement with a single entry at the
6071 // top and a single exit at the bottom.
6072 // The point of exit cannot be a branch out of the structured block.
6073 // longjmp() and throw() must not violate the entry/exit criteria.
6074 CS->getCapturedDecl()->setNothrow();
6075
6076 getCurFunction()->setHasBranchProtectedScope();
6077
6078 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6079}
6080
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006081StmtResult
6082Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6083 SourceLocation EndLoc,
6084 OpenMPDirectiveKind CancelRegion) {
6085 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6086 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6087 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6088 << getOpenMPDirectiveName(CancelRegion);
6089 return StmtError();
6090 }
6091 if (DSAStack->isParentNowaitRegion()) {
6092 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6093 return StmtError();
6094 }
6095 if (DSAStack->isParentOrderedRegion()) {
6096 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6097 return StmtError();
6098 }
6099 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6100 CancelRegion);
6101}
6102
Alexey Bataev87933c72015-09-18 08:07:34 +00006103StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6104 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006105 SourceLocation EndLoc,
6106 OpenMPDirectiveKind CancelRegion) {
6107 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6108 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6109 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6110 << getOpenMPDirectiveName(CancelRegion);
6111 return StmtError();
6112 }
6113 if (DSAStack->isParentNowaitRegion()) {
6114 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6115 return StmtError();
6116 }
6117 if (DSAStack->isParentOrderedRegion()) {
6118 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6119 return StmtError();
6120 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006121 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006122 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6123 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006124}
6125
Alexey Bataev382967a2015-12-08 12:06:20 +00006126static bool checkGrainsizeNumTasksClauses(Sema &S,
6127 ArrayRef<OMPClause *> Clauses) {
6128 OMPClause *PrevClause = nullptr;
6129 bool ErrorFound = false;
6130 for (auto *C : Clauses) {
6131 if (C->getClauseKind() == OMPC_grainsize ||
6132 C->getClauseKind() == OMPC_num_tasks) {
6133 if (!PrevClause)
6134 PrevClause = C;
6135 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6136 S.Diag(C->getLocStart(),
6137 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6138 << getOpenMPClauseName(C->getClauseKind())
6139 << getOpenMPClauseName(PrevClause->getClauseKind());
6140 S.Diag(PrevClause->getLocStart(),
6141 diag::note_omp_previous_grainsize_num_tasks)
6142 << getOpenMPClauseName(PrevClause->getClauseKind());
6143 ErrorFound = true;
6144 }
6145 }
6146 }
6147 return ErrorFound;
6148}
6149
Alexey Bataev49f6e782015-12-01 04:18:41 +00006150StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6151 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6152 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006153 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006154 if (!AStmt)
6155 return StmtError();
6156
6157 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6158 OMPLoopDirective::HelperExprs B;
6159 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6160 // define the nested loops number.
6161 unsigned NestedLoopCount =
6162 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006163 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006164 VarsWithImplicitDSA, B);
6165 if (NestedLoopCount == 0)
6166 return StmtError();
6167
6168 assert((CurContext->isDependentContext() || B.builtAll()) &&
6169 "omp for loop exprs were not built");
6170
Alexey Bataev382967a2015-12-08 12:06:20 +00006171 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6172 // The grainsize clause and num_tasks clause are mutually exclusive and may
6173 // not appear on the same taskloop directive.
6174 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6175 return StmtError();
6176
Alexey Bataev49f6e782015-12-01 04:18:41 +00006177 getCurFunction()->setHasBranchProtectedScope();
6178 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6179 NestedLoopCount, Clauses, AStmt, B);
6180}
6181
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006182StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6183 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6184 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006185 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006186 if (!AStmt)
6187 return StmtError();
6188
6189 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6190 OMPLoopDirective::HelperExprs B;
6191 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6192 // define the nested loops number.
6193 unsigned NestedLoopCount =
6194 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6195 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6196 VarsWithImplicitDSA, B);
6197 if (NestedLoopCount == 0)
6198 return StmtError();
6199
6200 assert((CurContext->isDependentContext() || B.builtAll()) &&
6201 "omp for loop exprs were not built");
6202
Alexey Bataev382967a2015-12-08 12:06:20 +00006203 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6204 // The grainsize clause and num_tasks clause are mutually exclusive and may
6205 // not appear on the same taskloop directive.
6206 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6207 return StmtError();
6208
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006209 getCurFunction()->setHasBranchProtectedScope();
6210 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6211 NestedLoopCount, Clauses, AStmt, B);
6212}
6213
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006214StmtResult Sema::ActOnOpenMPDistributeDirective(
6215 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6216 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006217 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006218 if (!AStmt)
6219 return StmtError();
6220
6221 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6222 OMPLoopDirective::HelperExprs B;
6223 // In presence of clause 'collapse' with number of loops, it will
6224 // define the nested loops number.
6225 unsigned NestedLoopCount =
6226 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6227 nullptr /*ordered not a clause on distribute*/, AStmt,
6228 *this, *DSAStack, VarsWithImplicitDSA, B);
6229 if (NestedLoopCount == 0)
6230 return StmtError();
6231
6232 assert((CurContext->isDependentContext() || B.builtAll()) &&
6233 "omp for loop exprs were not built");
6234
6235 getCurFunction()->setHasBranchProtectedScope();
6236 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6237 NestedLoopCount, Clauses, AStmt, B);
6238}
6239
Alexey Bataeved09d242014-05-28 05:53:51 +00006240OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006241 SourceLocation StartLoc,
6242 SourceLocation LParenLoc,
6243 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006244 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006245 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006246 case OMPC_final:
6247 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6248 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006249 case OMPC_num_threads:
6250 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6251 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006252 case OMPC_safelen:
6253 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6254 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006255 case OMPC_simdlen:
6256 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6257 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006258 case OMPC_collapse:
6259 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6260 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006261 case OMPC_ordered:
6262 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6263 break;
Michael Wonge710d542015-08-07 16:16:36 +00006264 case OMPC_device:
6265 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6266 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006267 case OMPC_num_teams:
6268 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6269 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006270 case OMPC_thread_limit:
6271 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6272 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006273 case OMPC_priority:
6274 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6275 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006276 case OMPC_grainsize:
6277 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6278 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006279 case OMPC_num_tasks:
6280 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6281 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006282 case OMPC_hint:
6283 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6284 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006285 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006286 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006287 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006288 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006289 case OMPC_private:
6290 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006291 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006292 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006293 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006294 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006295 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006296 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006297 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006298 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006299 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006300 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006301 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006302 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006303 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006304 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006305 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006306 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006307 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006308 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006309 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006310 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006311 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006312 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006313 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006314 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006315 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006316 llvm_unreachable("Clause is not allowed.");
6317 }
6318 return Res;
6319}
6320
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006321OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6322 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006323 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006324 SourceLocation NameModifierLoc,
6325 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006326 SourceLocation EndLoc) {
6327 Expr *ValExpr = Condition;
6328 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6329 !Condition->isInstantiationDependent() &&
6330 !Condition->containsUnexpandedParameterPack()) {
6331 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006332 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006333 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006334 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006335
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006336 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006337 }
6338
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006339 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6340 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006341}
6342
Alexey Bataev3778b602014-07-17 07:32:53 +00006343OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6344 SourceLocation StartLoc,
6345 SourceLocation LParenLoc,
6346 SourceLocation EndLoc) {
6347 Expr *ValExpr = Condition;
6348 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6349 !Condition->isInstantiationDependent() &&
6350 !Condition->containsUnexpandedParameterPack()) {
6351 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6352 Condition->getExprLoc(), Condition);
6353 if (Val.isInvalid())
6354 return nullptr;
6355
6356 ValExpr = Val.get();
6357 }
6358
6359 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6360}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006361ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6362 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006363 if (!Op)
6364 return ExprError();
6365
6366 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6367 public:
6368 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006369 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006370 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6371 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006372 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6373 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006374 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6375 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006376 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6377 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006378 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6379 QualType T,
6380 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006381 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6382 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006383 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6384 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006385 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006386 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006387 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006388 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6389 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006390 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6391 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006392 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6393 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006394 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006395 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006396 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006397 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6398 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006399 llvm_unreachable("conversion functions are permitted");
6400 }
6401 } ConvertDiagnoser;
6402 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6403}
6404
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006405static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006406 OpenMPClauseKind CKind,
6407 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006408 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6409 !ValExpr->isInstantiationDependent()) {
6410 SourceLocation Loc = ValExpr->getExprLoc();
6411 ExprResult Value =
6412 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6413 if (Value.isInvalid())
6414 return false;
6415
6416 ValExpr = Value.get();
6417 // The expression must evaluate to a non-negative integer value.
6418 llvm::APSInt Result;
6419 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006420 Result.isSigned() &&
6421 !((!StrictlyPositive && Result.isNonNegative()) ||
6422 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006423 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006424 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6425 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006426 return false;
6427 }
6428 }
6429 return true;
6430}
6431
Alexey Bataev568a8332014-03-06 06:15:19 +00006432OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6433 SourceLocation StartLoc,
6434 SourceLocation LParenLoc,
6435 SourceLocation EndLoc) {
6436 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006437
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006438 // OpenMP [2.5, Restrictions]
6439 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006440 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6441 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006442 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006443
Alexey Bataeved09d242014-05-28 05:53:51 +00006444 return new (Context)
6445 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006446}
6447
Alexey Bataev62c87d22014-03-21 04:51:18 +00006448ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006449 OpenMPClauseKind CKind,
6450 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006451 if (!E)
6452 return ExprError();
6453 if (E->isValueDependent() || E->isTypeDependent() ||
6454 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006455 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006456 llvm::APSInt Result;
6457 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6458 if (ICE.isInvalid())
6459 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006460 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6461 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006462 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006463 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6464 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006465 return ExprError();
6466 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006467 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6468 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6469 << E->getSourceRange();
6470 return ExprError();
6471 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006472 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6473 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006474 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006475 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006476 return ICE;
6477}
6478
6479OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6480 SourceLocation LParenLoc,
6481 SourceLocation EndLoc) {
6482 // OpenMP [2.8.1, simd construct, Description]
6483 // The parameter of the safelen clause must be a constant
6484 // positive integer expression.
6485 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6486 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006487 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006488 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006489 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006490}
6491
Alexey Bataev66b15b52015-08-21 11:14:16 +00006492OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6493 SourceLocation LParenLoc,
6494 SourceLocation EndLoc) {
6495 // OpenMP [2.8.1, simd construct, Description]
6496 // The parameter of the simdlen clause must be a constant
6497 // positive integer expression.
6498 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6499 if (Simdlen.isInvalid())
6500 return nullptr;
6501 return new (Context)
6502 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6503}
6504
Alexander Musman64d33f12014-06-04 07:53:32 +00006505OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6506 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006507 SourceLocation LParenLoc,
6508 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006509 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006510 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006511 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006512 // The parameter of the collapse clause must be a constant
6513 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006514 ExprResult NumForLoopsResult =
6515 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6516 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006517 return nullptr;
6518 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006519 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006520}
6521
Alexey Bataev10e775f2015-07-30 11:36:16 +00006522OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6523 SourceLocation EndLoc,
6524 SourceLocation LParenLoc,
6525 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006526 // OpenMP [2.7.1, loop construct, Description]
6527 // OpenMP [2.8.1, simd construct, Description]
6528 // OpenMP [2.9.6, distribute construct, Description]
6529 // The parameter of the ordered clause must be a constant
6530 // positive integer expression if any.
6531 if (NumForLoops && LParenLoc.isValid()) {
6532 ExprResult NumForLoopsResult =
6533 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6534 if (NumForLoopsResult.isInvalid())
6535 return nullptr;
6536 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006537 } else
6538 NumForLoops = nullptr;
6539 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006540 return new (Context)
6541 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6542}
6543
Alexey Bataeved09d242014-05-28 05:53:51 +00006544OMPClause *Sema::ActOnOpenMPSimpleClause(
6545 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6546 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006547 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006548 switch (Kind) {
6549 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006550 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006551 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6552 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006553 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006554 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006555 Res = ActOnOpenMPProcBindClause(
6556 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6557 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006558 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006559 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006560 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006561 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006562 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006563 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006564 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006565 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006566 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006567 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006568 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006569 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006570 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006571 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006572 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006573 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006574 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006575 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006576 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006577 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006578 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006579 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006580 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006581 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006582 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006583 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006584 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006585 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006586 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006587 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006588 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006589 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006590 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006591 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006592 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006593 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006594 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006595 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006596 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006597 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006598 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006599 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006600 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006601 llvm_unreachable("Clause is not allowed.");
6602 }
6603 return Res;
6604}
6605
Alexey Bataev6402bca2015-12-28 07:25:51 +00006606static std::string
6607getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6608 ArrayRef<unsigned> Exclude = llvm::None) {
6609 std::string Values;
6610 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6611 unsigned Skipped = Exclude.size();
6612 auto S = Exclude.begin(), E = Exclude.end();
6613 for (unsigned i = First; i < Last; ++i) {
6614 if (std::find(S, E, i) != E) {
6615 --Skipped;
6616 continue;
6617 }
6618 Values += "'";
6619 Values += getOpenMPSimpleClauseTypeName(K, i);
6620 Values += "'";
6621 if (i == Bound - Skipped)
6622 Values += " or ";
6623 else if (i != Bound + 1 - Skipped)
6624 Values += ", ";
6625 }
6626 return Values;
6627}
6628
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006629OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6630 SourceLocation KindKwLoc,
6631 SourceLocation StartLoc,
6632 SourceLocation LParenLoc,
6633 SourceLocation EndLoc) {
6634 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006635 static_assert(OMPC_DEFAULT_unknown > 0,
6636 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006637 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006638 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6639 /*Last=*/OMPC_DEFAULT_unknown)
6640 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006641 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006642 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006643 switch (Kind) {
6644 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006645 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006646 break;
6647 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006648 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006649 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006650 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006651 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006652 break;
6653 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006654 return new (Context)
6655 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006656}
6657
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006658OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6659 SourceLocation KindKwLoc,
6660 SourceLocation StartLoc,
6661 SourceLocation LParenLoc,
6662 SourceLocation EndLoc) {
6663 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006664 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006665 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6666 /*Last=*/OMPC_PROC_BIND_unknown)
6667 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006668 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006669 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006670 return new (Context)
6671 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006672}
6673
Alexey Bataev56dafe82014-06-20 07:16:17 +00006674OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006675 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006676 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006677 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006678 SourceLocation EndLoc) {
6679 OMPClause *Res = nullptr;
6680 switch (Kind) {
6681 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006682 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6683 assert(Argument.size() == NumberOfElements &&
6684 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006685 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006686 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6687 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6688 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6689 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6690 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006691 break;
6692 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006693 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6694 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6695 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6696 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006697 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006698 case OMPC_dist_schedule:
6699 Res = ActOnOpenMPDistScheduleClause(
6700 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6701 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6702 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006703 case OMPC_defaultmap:
6704 enum { Modifier, DefaultmapKind };
6705 Res = ActOnOpenMPDefaultmapClause(
6706 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6707 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6708 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6709 ArgumentLoc[DefaultmapKind], EndLoc);
6710 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006711 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006712 case OMPC_num_threads:
6713 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006714 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006715 case OMPC_collapse:
6716 case OMPC_default:
6717 case OMPC_proc_bind:
6718 case OMPC_private:
6719 case OMPC_firstprivate:
6720 case OMPC_lastprivate:
6721 case OMPC_shared:
6722 case OMPC_reduction:
6723 case OMPC_linear:
6724 case OMPC_aligned:
6725 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006726 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006727 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006728 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006729 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006730 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006731 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006732 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006733 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006734 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006735 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006736 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006737 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006738 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006739 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006740 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006741 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006742 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006743 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006744 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006745 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006746 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006747 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006748 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006749 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006750 case OMPC_unknown:
6751 llvm_unreachable("Clause is not allowed.");
6752 }
6753 return Res;
6754}
6755
Alexey Bataev6402bca2015-12-28 07:25:51 +00006756static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6757 OpenMPScheduleClauseModifier M2,
6758 SourceLocation M1Loc, SourceLocation M2Loc) {
6759 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6760 SmallVector<unsigned, 2> Excluded;
6761 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6762 Excluded.push_back(M2);
6763 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6764 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6765 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6766 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6767 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6768 << getListOfPossibleValues(OMPC_schedule,
6769 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6770 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6771 Excluded)
6772 << getOpenMPClauseName(OMPC_schedule);
6773 return true;
6774 }
6775 return false;
6776}
6777
Alexey Bataev56dafe82014-06-20 07:16:17 +00006778OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006779 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006780 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006781 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6782 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6783 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6784 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6785 return nullptr;
6786 // OpenMP, 2.7.1, Loop Construct, Restrictions
6787 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6788 // but not both.
6789 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6790 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6791 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6792 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6793 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6794 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6795 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6796 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6797 return nullptr;
6798 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006799 if (Kind == OMPC_SCHEDULE_unknown) {
6800 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006801 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6802 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6803 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6804 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6805 Exclude);
6806 } else {
6807 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6808 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006809 }
6810 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6811 << Values << getOpenMPClauseName(OMPC_schedule);
6812 return nullptr;
6813 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006814 // OpenMP, 2.7.1, Loop Construct, Restrictions
6815 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6816 // schedule(guided).
6817 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6818 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6819 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6820 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6821 diag::err_omp_schedule_nonmonotonic_static);
6822 return nullptr;
6823 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006824 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006825 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006826 if (ChunkSize) {
6827 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6828 !ChunkSize->isInstantiationDependent() &&
6829 !ChunkSize->containsUnexpandedParameterPack()) {
6830 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6831 ExprResult Val =
6832 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6833 if (Val.isInvalid())
6834 return nullptr;
6835
6836 ValExpr = Val.get();
6837
6838 // OpenMP [2.7.1, Restrictions]
6839 // chunk_size must be a loop invariant integer expression with a positive
6840 // value.
6841 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006842 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6843 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6844 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006845 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006846 return nullptr;
6847 }
6848 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00006849 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00006850 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
6851 HelperValStmt =
6852 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
6853 /*NumDecls=*/1),
6854 SourceLocation(), SourceLocation());
6855 ValExpr = DefaultLvalueConversion(ValExpr).get();
Alexey Bataev56dafe82014-06-20 07:16:17 +00006856 }
6857 }
6858 }
6859
Alexey Bataev6402bca2015-12-28 07:25:51 +00006860 return new (Context)
6861 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006862 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006863}
6864
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006865OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6866 SourceLocation StartLoc,
6867 SourceLocation EndLoc) {
6868 OMPClause *Res = nullptr;
6869 switch (Kind) {
6870 case OMPC_ordered:
6871 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6872 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006873 case OMPC_nowait:
6874 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6875 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006876 case OMPC_untied:
6877 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6878 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006879 case OMPC_mergeable:
6880 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6881 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006882 case OMPC_read:
6883 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6884 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006885 case OMPC_write:
6886 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6887 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006888 case OMPC_update:
6889 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6890 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006891 case OMPC_capture:
6892 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6893 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006894 case OMPC_seq_cst:
6895 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6896 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006897 case OMPC_threads:
6898 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6899 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006900 case OMPC_simd:
6901 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6902 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006903 case OMPC_nogroup:
6904 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6905 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006906 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006907 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006908 case OMPC_num_threads:
6909 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006910 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006911 case OMPC_collapse:
6912 case OMPC_schedule:
6913 case OMPC_private:
6914 case OMPC_firstprivate:
6915 case OMPC_lastprivate:
6916 case OMPC_shared:
6917 case OMPC_reduction:
6918 case OMPC_linear:
6919 case OMPC_aligned:
6920 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006921 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006922 case OMPC_default:
6923 case OMPC_proc_bind:
6924 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006925 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006926 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006927 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006928 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006929 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006930 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006931 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006932 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006933 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006934 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006935 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006936 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006937 case OMPC_unknown:
6938 llvm_unreachable("Clause is not allowed.");
6939 }
6940 return Res;
6941}
6942
Alexey Bataev236070f2014-06-20 11:19:47 +00006943OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6944 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006945 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006946 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6947}
6948
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006949OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6950 SourceLocation EndLoc) {
6951 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6952}
6953
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006954OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6955 SourceLocation EndLoc) {
6956 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6957}
6958
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006959OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6960 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006961 return new (Context) OMPReadClause(StartLoc, EndLoc);
6962}
6963
Alexey Bataevdea47612014-07-23 07:46:59 +00006964OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6965 SourceLocation EndLoc) {
6966 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6967}
6968
Alexey Bataev67a4f222014-07-23 10:25:33 +00006969OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6970 SourceLocation EndLoc) {
6971 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6972}
6973
Alexey Bataev459dec02014-07-24 06:46:57 +00006974OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6975 SourceLocation EndLoc) {
6976 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6977}
6978
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006979OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6980 SourceLocation EndLoc) {
6981 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6982}
6983
Alexey Bataev346265e2015-09-25 10:37:12 +00006984OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6985 SourceLocation EndLoc) {
6986 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6987}
6988
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006989OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6990 SourceLocation EndLoc) {
6991 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6992}
6993
Alexey Bataevb825de12015-12-07 10:51:44 +00006994OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6995 SourceLocation EndLoc) {
6996 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6997}
6998
Alexey Bataevc5e02582014-06-16 07:08:35 +00006999OMPClause *Sema::ActOnOpenMPVarListClause(
7000 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7001 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7002 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007003 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007004 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7005 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7006 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007007 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007008 switch (Kind) {
7009 case OMPC_private:
7010 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7011 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007012 case OMPC_firstprivate:
7013 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7014 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007015 case OMPC_lastprivate:
7016 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7017 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007018 case OMPC_shared:
7019 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7020 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007021 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007022 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7023 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007024 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007025 case OMPC_linear:
7026 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007027 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007028 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007029 case OMPC_aligned:
7030 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7031 ColonLoc, EndLoc);
7032 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007033 case OMPC_copyin:
7034 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7035 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007036 case OMPC_copyprivate:
7037 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7038 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007039 case OMPC_flush:
7040 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7041 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007042 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007043 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7044 StartLoc, LParenLoc, EndLoc);
7045 break;
7046 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007047 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7048 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7049 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007050 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007051 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007052 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007053 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007054 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007055 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007056 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007057 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007058 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007059 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007060 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007061 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007062 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007063 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007064 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007065 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007066 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007067 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007068 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007069 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007070 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007071 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007072 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007073 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007074 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007075 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007076 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007077 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007078 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007079 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007080 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007081 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007082 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007083 llvm_unreachable("Clause is not allowed.");
7084 }
7085 return Res;
7086}
7087
Alexey Bataev90c228f2016-02-08 09:29:13 +00007088ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007089 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007090 ExprResult Res = BuildDeclRefExpr(
7091 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7092 if (!Res.isUsable())
7093 return ExprError();
7094 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7095 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7096 if (!Res.isUsable())
7097 return ExprError();
7098 }
7099 if (VK != VK_LValue && Res.get()->isGLValue()) {
7100 Res = DefaultLvalueConversion(Res.get());
7101 if (!Res.isUsable())
7102 return ExprError();
7103 }
7104 return Res;
7105}
7106
Alexey Bataev60da77e2016-02-29 05:54:20 +00007107static std::pair<ValueDecl *, bool>
7108getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7109 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007110 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7111 RefExpr->containsUnexpandedParameterPack())
7112 return std::make_pair(nullptr, true);
7113
Alexey Bataevd985eda2016-02-10 11:29:16 +00007114 // OpenMP [3.1, C/C++]
7115 // A list item is a variable name.
7116 // OpenMP [2.9.3.3, Restrictions, p.1]
7117 // A variable that is part of another variable (as an array or
7118 // structure element) cannot appear in a private clause.
7119 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007120 enum {
7121 NoArrayExpr = -1,
7122 ArraySubscript = 0,
7123 OMPArraySection = 1
7124 } IsArrayExpr = NoArrayExpr;
7125 if (AllowArraySection) {
7126 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7127 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7128 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7129 Base = TempASE->getBase()->IgnoreParenImpCasts();
7130 RefExpr = Base;
7131 IsArrayExpr = ArraySubscript;
7132 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7133 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7134 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7135 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7136 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7137 Base = TempASE->getBase()->IgnoreParenImpCasts();
7138 RefExpr = Base;
7139 IsArrayExpr = OMPArraySection;
7140 }
7141 }
7142 ELoc = RefExpr->getExprLoc();
7143 ERange = RefExpr->getSourceRange();
7144 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007145 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7146 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7147 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7148 (S.getCurrentThisType().isNull() || !ME ||
7149 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7150 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007151 if (IsArrayExpr != NoArrayExpr)
7152 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7153 << ERange;
7154 else {
7155 S.Diag(ELoc,
7156 AllowArraySection
7157 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7158 : diag::err_omp_expected_var_name_member_expr)
7159 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7160 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007161 return std::make_pair(nullptr, false);
7162 }
7163 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7164}
7165
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007166OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7167 SourceLocation StartLoc,
7168 SourceLocation LParenLoc,
7169 SourceLocation EndLoc) {
7170 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007171 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007172 for (auto &RefExpr : VarList) {
7173 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007174 SourceLocation ELoc;
7175 SourceRange ERange;
7176 Expr *SimpleRefExpr = RefExpr;
7177 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007178 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007179 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007180 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007181 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007182 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007183 ValueDecl *D = Res.first;
7184 if (!D)
7185 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007186
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007187 QualType Type = D->getType();
7188 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007189
7190 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7191 // A variable that appears in a private clause must not have an incomplete
7192 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007193 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007194 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007195 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007196
Alexey Bataev758e55e2013-09-06 18:03:48 +00007197 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7198 // in a Construct]
7199 // Variables with the predetermined data-sharing attributes may not be
7200 // listed in data-sharing attributes clauses, except for the cases
7201 // listed below. For these exceptions only, listing a predetermined
7202 // variable in a data-sharing attribute clause is allowed and overrides
7203 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007204 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007205 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007206 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7207 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007208 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007209 continue;
7210 }
7211
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007212 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007213 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007214 DSAStack->getCurrentDirective() == OMPD_task) {
7215 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7216 << getOpenMPClauseName(OMPC_private) << Type
7217 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7218 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007219 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007220 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007221 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007222 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007223 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007224 continue;
7225 }
7226
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007227 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7228 // A variable of class type (or array thereof) that appears in a private
7229 // clause requires an accessible, unambiguous default constructor for the
7230 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007231 // Generate helper private variable and initialize it with the default
7232 // value. The address of the original variable is replaced by the address of
7233 // the new private variable in CodeGen. This new variable is not added to
7234 // IdResolver, so the code in the OpenMP region uses original variable for
7235 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007236 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007237 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7238 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007239 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007240 if (VDPrivate->isInvalidDecl())
7241 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007242 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007243 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007244
Alexey Bataev90c228f2016-02-08 09:29:13 +00007245 DeclRefExpr *Ref = nullptr;
7246 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007247 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007248 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7249 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007250 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007251 }
7252
Alexey Bataeved09d242014-05-28 05:53:51 +00007253 if (Vars.empty())
7254 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007255
Alexey Bataev03b340a2014-10-21 03:16:40 +00007256 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7257 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007258}
7259
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007260namespace {
7261class DiagsUninitializedSeveretyRAII {
7262private:
7263 DiagnosticsEngine &Diags;
7264 SourceLocation SavedLoc;
7265 bool IsIgnored;
7266
7267public:
7268 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7269 bool IsIgnored)
7270 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7271 if (!IsIgnored) {
7272 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7273 /*Map*/ diag::Severity::Ignored, Loc);
7274 }
7275 }
7276 ~DiagsUninitializedSeveretyRAII() {
7277 if (!IsIgnored)
7278 Diags.popMappings(SavedLoc);
7279 }
7280};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007281}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007282
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007283OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7284 SourceLocation StartLoc,
7285 SourceLocation LParenLoc,
7286 SourceLocation EndLoc) {
7287 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007288 SmallVector<Expr *, 8> PrivateCopies;
7289 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007290 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007291 bool IsImplicitClause =
7292 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7293 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7294
Alexey Bataeved09d242014-05-28 05:53:51 +00007295 for (auto &RefExpr : VarList) {
7296 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007297 SourceLocation ELoc;
7298 SourceRange ERange;
7299 Expr *SimpleRefExpr = RefExpr;
7300 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007301 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007302 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007303 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007304 PrivateCopies.push_back(nullptr);
7305 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007306 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007307 ValueDecl *D = Res.first;
7308 if (!D)
7309 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007310
Alexey Bataev60da77e2016-02-29 05:54:20 +00007311 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007312 QualType Type = D->getType();
7313 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007314
7315 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7316 // A variable that appears in a private clause must not have an incomplete
7317 // type or a reference type.
7318 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007319 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007320 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007321 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007322
7323 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7324 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007325 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007326 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007327 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007328
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007329 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007330 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007331 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007332 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007333 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007334 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007335 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7336 // A list item that specifies a given variable may not appear in more
7337 // than one clause on the same directive, except that a variable may be
7338 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007339 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007340 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007341 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007342 << getOpenMPClauseName(DVar.CKind)
7343 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007344 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007345 continue;
7346 }
7347
7348 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7349 // in a Construct]
7350 // Variables with the predetermined data-sharing attributes may not be
7351 // listed in data-sharing attributes clauses, except for the cases
7352 // listed below. For these exceptions only, listing a predetermined
7353 // variable in a data-sharing attribute clause is allowed and overrides
7354 // the variable's predetermined data-sharing attributes.
7355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7356 // in a Construct, C/C++, p.2]
7357 // Variables with const-qualified type having no mutable member may be
7358 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007359 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007360 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7361 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007362 << getOpenMPClauseName(DVar.CKind)
7363 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007364 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007365 continue;
7366 }
7367
Alexey Bataevf29276e2014-06-18 04:14:57 +00007368 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007369 // OpenMP [2.9.3.4, Restrictions, p.2]
7370 // A list item that is private within a parallel region must not appear
7371 // in a firstprivate clause on a worksharing construct if any of the
7372 // worksharing regions arising from the worksharing construct ever bind
7373 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007374 if (isOpenMPWorksharingDirective(CurrDir) &&
7375 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007376 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007377 if (DVar.CKind != OMPC_shared &&
7378 (isOpenMPParallelDirective(DVar.DKind) ||
7379 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007380 Diag(ELoc, diag::err_omp_required_access)
7381 << getOpenMPClauseName(OMPC_firstprivate)
7382 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007383 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007384 continue;
7385 }
7386 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007387 // OpenMP [2.9.3.4, Restrictions, p.3]
7388 // A list item that appears in a reduction clause of a parallel construct
7389 // must not appear in a firstprivate clause on a worksharing or task
7390 // construct if any of the worksharing or task regions arising from the
7391 // worksharing or task construct ever bind to any of the parallel regions
7392 // arising from the parallel construct.
7393 // OpenMP [2.9.3.4, Restrictions, p.4]
7394 // A list item that appears in a reduction clause in worksharing
7395 // construct must not appear in a firstprivate clause in a task construct
7396 // encountered during execution of any of the worksharing regions arising
7397 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007398 if (CurrDir == OMPD_task) {
7399 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007400 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007401 [](OpenMPDirectiveKind K) -> bool {
7402 return isOpenMPParallelDirective(K) ||
7403 isOpenMPWorksharingDirective(K);
7404 },
7405 false);
7406 if (DVar.CKind == OMPC_reduction &&
7407 (isOpenMPParallelDirective(DVar.DKind) ||
7408 isOpenMPWorksharingDirective(DVar.DKind))) {
7409 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7410 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007411 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007412 continue;
7413 }
7414 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007415
7416 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7417 // A list item that is private within a teams region must not appear in a
7418 // firstprivate clause on a distribute construct if any of the distribute
7419 // regions arising from the distribute construct ever bind to any of the
7420 // teams regions arising from the teams construct.
7421 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7422 // A list item that appears in a reduction clause of a teams construct
7423 // must not appear in a firstprivate clause on a distribute construct if
7424 // any of the distribute regions arising from the distribute construct
7425 // ever bind to any of the teams regions arising from the teams construct.
7426 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7427 // A list item may appear in a firstprivate or lastprivate clause but not
7428 // both.
7429 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007430 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007431 [](OpenMPDirectiveKind K) -> bool {
7432 return isOpenMPTeamsDirective(K);
7433 },
7434 false);
7435 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7436 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007437 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007438 continue;
7439 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007440 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007441 [](OpenMPDirectiveKind K) -> bool {
7442 return isOpenMPTeamsDirective(K);
7443 },
7444 false);
7445 if (DVar.CKind == OMPC_reduction &&
7446 isOpenMPTeamsDirective(DVar.DKind)) {
7447 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007448 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007449 continue;
7450 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007451 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007452 if (DVar.CKind == OMPC_lastprivate) {
7453 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007454 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007455 continue;
7456 }
7457 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007458 }
7459
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007460 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007461 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007462 DSAStack->getCurrentDirective() == OMPD_task) {
7463 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7464 << getOpenMPClauseName(OMPC_firstprivate) << Type
7465 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7466 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007467 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007468 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007469 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007470 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007471 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007472 continue;
7473 }
7474
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007475 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007476 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7477 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007478 // Generate helper private variable and initialize it with the value of the
7479 // original variable. The address of the original variable is replaced by
7480 // the address of the new private variable in the CodeGen. This new variable
7481 // is not added to IdResolver, so the code in the OpenMP region uses
7482 // original variable for proper diagnostics and variable capturing.
7483 Expr *VDInitRefExpr = nullptr;
7484 // For arrays generate initializer for single element and replace it by the
7485 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007486 if (Type->isArrayType()) {
7487 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007488 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007489 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007490 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007491 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007492 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007493 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007494 InitializedEntity Entity =
7495 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007496 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7497
7498 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7499 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7500 if (Result.isInvalid())
7501 VDPrivate->setInvalidDecl();
7502 else
7503 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007504 // Remove temp variable declaration.
7505 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007506 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007507 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7508 ".firstprivate.temp");
7509 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7510 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007511 AddInitializerToDecl(VDPrivate,
7512 DefaultLvalueConversion(VDInitRefExpr).get(),
7513 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007514 }
7515 if (VDPrivate->isInvalidDecl()) {
7516 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007517 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007518 diag::note_omp_task_predetermined_firstprivate_here);
7519 }
7520 continue;
7521 }
7522 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007523 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007524 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7525 RefExpr->getExprLoc());
7526 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007527 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007528 if (TopDVar.CKind == OMPC_lastprivate)
7529 Ref = TopDVar.PrivateCopy;
7530 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007531 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007532 if (!IsOpenMPCapturedDecl(D))
7533 ExprCaptures.push_back(Ref->getDecl());
7534 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007535 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007536 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7537 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007538 PrivateCopies.push_back(VDPrivateRefExpr);
7539 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007540 }
7541
Alexey Bataeved09d242014-05-28 05:53:51 +00007542 if (Vars.empty())
7543 return nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007544 Stmt *PreInit = nullptr;
7545 if (!ExprCaptures.empty()) {
7546 PreInit = new (Context)
7547 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7548 ExprCaptures.size()),
7549 SourceLocation(), SourceLocation());
7550 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007551
7552 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev417089f2016-02-17 13:19:37 +00007553 Vars, PrivateCopies, Inits, PreInit);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007554}
7555
Alexander Musman1bb328c2014-06-04 13:06:39 +00007556OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7557 SourceLocation StartLoc,
7558 SourceLocation LParenLoc,
7559 SourceLocation EndLoc) {
7560 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007561 SmallVector<Expr *, 8> SrcExprs;
7562 SmallVector<Expr *, 8> DstExprs;
7563 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007564 SmallVector<Decl *, 4> ExprCaptures;
7565 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007566 for (auto &RefExpr : VarList) {
7567 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007568 SourceLocation ELoc;
7569 SourceRange ERange;
7570 Expr *SimpleRefExpr = RefExpr;
7571 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007572 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007573 // It will be analyzed later.
7574 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007575 SrcExprs.push_back(nullptr);
7576 DstExprs.push_back(nullptr);
7577 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007578 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007579 ValueDecl *D = Res.first;
7580 if (!D)
7581 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007582
Alexey Bataev74caaf22016-02-20 04:09:36 +00007583 QualType Type = D->getType();
7584 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007585
7586 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7587 // A variable that appears in a lastprivate clause must not have an
7588 // incomplete type or a reference type.
7589 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007590 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007591 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007592 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007593
7594 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7595 // in a Construct]
7596 // Variables with the predetermined data-sharing attributes may not be
7597 // listed in data-sharing attributes clauses, except for the cases
7598 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007599 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007600 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7601 DVar.CKind != OMPC_firstprivate &&
7602 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7603 Diag(ELoc, diag::err_omp_wrong_dsa)
7604 << getOpenMPClauseName(DVar.CKind)
7605 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007606 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007607 continue;
7608 }
7609
Alexey Bataevf29276e2014-06-18 04:14:57 +00007610 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7611 // OpenMP [2.14.3.5, Restrictions, p.2]
7612 // A list item that is private within a parallel region, or that appears in
7613 // the reduction clause of a parallel construct, must not appear in a
7614 // lastprivate clause on a worksharing construct if any of the corresponding
7615 // worksharing regions ever binds to any of the corresponding parallel
7616 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007617 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007618 if (isOpenMPWorksharingDirective(CurrDir) &&
7619 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007620 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007621 if (DVar.CKind != OMPC_shared) {
7622 Diag(ELoc, diag::err_omp_required_access)
7623 << getOpenMPClauseName(OMPC_lastprivate)
7624 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007625 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007626 continue;
7627 }
7628 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007629
7630 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7631 // A list item may appear in a firstprivate or lastprivate clause but not
7632 // both.
7633 if (CurrDir == OMPD_distribute) {
7634 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7635 if (DVar.CKind == OMPC_firstprivate) {
7636 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7637 ReportOriginalDSA(*this, DSAStack, D, DVar);
7638 continue;
7639 }
7640 }
7641
Alexander Musman1bb328c2014-06-04 13:06:39 +00007642 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007643 // A variable of class type (or array thereof) that appears in a
7644 // lastprivate clause requires an accessible, unambiguous default
7645 // constructor for the class type, unless the list item is also specified
7646 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007647 // A variable of class type (or array thereof) that appears in a
7648 // lastprivate clause requires an accessible, unambiguous copy assignment
7649 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007650 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007651 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007652 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007653 D->hasAttrs() ? &D->getAttrs() : nullptr);
7654 auto *PseudoSrcExpr =
7655 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007656 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007657 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007658 D->hasAttrs() ? &D->getAttrs() : nullptr);
7659 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007660 // For arrays generate assignment operation for single element and replace
7661 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007662 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007663 PseudoDstExpr, PseudoSrcExpr);
7664 if (AssignmentOp.isInvalid())
7665 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007666 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007667 /*DiscardedValue=*/true);
7668 if (AssignmentOp.isInvalid())
7669 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007670
Alexey Bataev74caaf22016-02-20 04:09:36 +00007671 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007672 if (!VD) {
7673 if (TopDVar.CKind == OMPC_firstprivate)
7674 Ref = TopDVar.PrivateCopy;
7675 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007676 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007677 if (!IsOpenMPCapturedDecl(D))
7678 ExprCaptures.push_back(Ref->getDecl());
7679 }
7680 if (TopDVar.CKind == OMPC_firstprivate ||
7681 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007682 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007683 ExprResult RefRes = DefaultLvalueConversion(Ref);
7684 if (!RefRes.isUsable())
7685 continue;
7686 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007687 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7688 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007689 if (!PostUpdateRes.isUsable())
7690 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00007691 ExprPostUpdates.push_back(
7692 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007693 }
7694 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007695 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007696 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7697 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007698 SrcExprs.push_back(PseudoSrcExpr);
7699 DstExprs.push_back(PseudoDstExpr);
7700 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007701 }
7702
7703 if (Vars.empty())
7704 return nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007705 Stmt *PreInit = nullptr;
7706 if (!ExprCaptures.empty()) {
7707 PreInit = new (Context)
7708 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7709 ExprCaptures.size()),
7710 SourceLocation(), SourceLocation());
7711 }
7712 Expr *PostUpdate = nullptr;
7713 if (!ExprPostUpdates.empty()) {
7714 for (auto *E : ExprPostUpdates) {
7715 ExprResult PostUpdateRes =
7716 PostUpdate
7717 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
7718 : E;
7719 PostUpdate = PostUpdateRes.get();
7720 }
7721 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007722
7723 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00007724 Vars, SrcExprs, DstExprs, AssignmentOps,
7725 PreInit, PostUpdate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007726}
7727
Alexey Bataev758e55e2013-09-06 18:03:48 +00007728OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7729 SourceLocation StartLoc,
7730 SourceLocation LParenLoc,
7731 SourceLocation EndLoc) {
7732 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007733 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007734 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007735 SourceLocation ELoc;
7736 SourceRange ERange;
7737 Expr *SimpleRefExpr = RefExpr;
7738 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007739 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007740 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007741 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007742 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007743 ValueDecl *D = Res.first;
7744 if (!D)
7745 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007746
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007747 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007748 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7749 // in a Construct]
7750 // Variables with the predetermined data-sharing attributes may not be
7751 // listed in data-sharing attributes clauses, except for the cases
7752 // listed below. For these exceptions only, listing a predetermined
7753 // variable in a data-sharing attribute clause is allowed and overrides
7754 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007755 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007756 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7757 DVar.RefExpr) {
7758 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7759 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007760 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007761 continue;
7762 }
7763
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007764 DeclRefExpr *Ref = nullptr;
7765 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007766 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007767 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
7768 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007769 }
7770
Alexey Bataeved09d242014-05-28 05:53:51 +00007771 if (Vars.empty())
7772 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007773
7774 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7775}
7776
Alexey Bataevc5e02582014-06-16 07:08:35 +00007777namespace {
7778class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7779 DSAStackTy *Stack;
7780
7781public:
7782 bool VisitDeclRefExpr(DeclRefExpr *E) {
7783 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007784 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007785 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7786 return false;
7787 if (DVar.CKind != OMPC_unknown)
7788 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007789 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007790 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007791 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007792 return true;
7793 return false;
7794 }
7795 return false;
7796 }
7797 bool VisitStmt(Stmt *S) {
7798 for (auto Child : S->children()) {
7799 if (Child && Visit(Child))
7800 return true;
7801 }
7802 return false;
7803 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007804 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007805};
Alexey Bataev23b69422014-06-18 07:08:49 +00007806} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007807
Alexey Bataev60da77e2016-02-29 05:54:20 +00007808namespace {
7809// Transform MemberExpression for specified FieldDecl of current class to
7810// DeclRefExpr to specified OMPCapturedExprDecl.
7811class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
7812 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
7813 ValueDecl *Field;
7814 DeclRefExpr *CapturedExpr;
7815
7816public:
7817 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
7818 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
7819
7820 ExprResult TransformMemberExpr(MemberExpr *E) {
7821 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
7822 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00007823 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00007824 return CapturedExpr;
7825 }
7826 return BaseTransform::TransformMemberExpr(E);
7827 }
7828 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
7829};
7830} // namespace
7831
Alexey Bataevc5e02582014-06-16 07:08:35 +00007832OMPClause *Sema::ActOnOpenMPReductionClause(
7833 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7834 SourceLocation ColonLoc, SourceLocation EndLoc,
7835 CXXScopeSpec &ReductionIdScopeSpec,
7836 const DeclarationNameInfo &ReductionId) {
7837 // TODO: Allow scope specification search when 'declare reduction' is
7838 // supported.
7839 assert(ReductionIdScopeSpec.isEmpty() &&
7840 "No support for scoped reduction identifiers yet.");
7841
7842 auto DN = ReductionId.getName();
7843 auto OOK = DN.getCXXOverloadedOperator();
7844 BinaryOperatorKind BOK = BO_Comma;
7845
7846 // OpenMP [2.14.3.6, reduction clause]
7847 // C
7848 // reduction-identifier is either an identifier or one of the following
7849 // operators: +, -, *, &, |, ^, && and ||
7850 // C++
7851 // reduction-identifier is either an id-expression or one of the following
7852 // operators: +, -, *, &, |, ^, && and ||
7853 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7854 switch (OOK) {
7855 case OO_Plus:
7856 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007857 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007858 break;
7859 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007860 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007861 break;
7862 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007863 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007864 break;
7865 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007866 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007867 break;
7868 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007869 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007870 break;
7871 case OO_AmpAmp:
7872 BOK = BO_LAnd;
7873 break;
7874 case OO_PipePipe:
7875 BOK = BO_LOr;
7876 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007877 case OO_New:
7878 case OO_Delete:
7879 case OO_Array_New:
7880 case OO_Array_Delete:
7881 case OO_Slash:
7882 case OO_Percent:
7883 case OO_Tilde:
7884 case OO_Exclaim:
7885 case OO_Equal:
7886 case OO_Less:
7887 case OO_Greater:
7888 case OO_LessEqual:
7889 case OO_GreaterEqual:
7890 case OO_PlusEqual:
7891 case OO_MinusEqual:
7892 case OO_StarEqual:
7893 case OO_SlashEqual:
7894 case OO_PercentEqual:
7895 case OO_CaretEqual:
7896 case OO_AmpEqual:
7897 case OO_PipeEqual:
7898 case OO_LessLess:
7899 case OO_GreaterGreater:
7900 case OO_LessLessEqual:
7901 case OO_GreaterGreaterEqual:
7902 case OO_EqualEqual:
7903 case OO_ExclaimEqual:
7904 case OO_PlusPlus:
7905 case OO_MinusMinus:
7906 case OO_Comma:
7907 case OO_ArrowStar:
7908 case OO_Arrow:
7909 case OO_Call:
7910 case OO_Subscript:
7911 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007912 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007913 case NUM_OVERLOADED_OPERATORS:
7914 llvm_unreachable("Unexpected reduction identifier");
7915 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007916 if (auto II = DN.getAsIdentifierInfo()) {
7917 if (II->isStr("max"))
7918 BOK = BO_GT;
7919 else if (II->isStr("min"))
7920 BOK = BO_LT;
7921 }
7922 break;
7923 }
7924 SourceRange ReductionIdRange;
7925 if (ReductionIdScopeSpec.isValid()) {
7926 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7927 }
7928 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7929 if (BOK == BO_Comma) {
7930 // Not allowed reduction identifier is found.
7931 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7932 << ReductionIdRange;
7933 return nullptr;
7934 }
7935
7936 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007937 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007938 SmallVector<Expr *, 8> LHSs;
7939 SmallVector<Expr *, 8> RHSs;
7940 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00007941 SmallVector<Decl *, 4> ExprCaptures;
7942 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007943 for (auto RefExpr : VarList) {
7944 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00007945 // OpenMP [2.1, C/C++]
7946 // A list item is a variable or array section, subject to the restrictions
7947 // specified in Section 2.4 on page 42 and in each of the sections
7948 // describing clauses and directives for which a list appears.
7949 // OpenMP [2.14.3.3, Restrictions, p.1]
7950 // A variable that is part of another variable (as an array or
7951 // structure element) cannot appear in a private clause.
Alexey Bataev60da77e2016-02-29 05:54:20 +00007952 SourceLocation ELoc;
7953 SourceRange ERange;
7954 Expr *SimpleRefExpr = RefExpr;
7955 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
7956 /*AllowArraySection=*/true);
7957 if (Res.second) {
7958 // It will be analyzed later.
7959 Vars.push_back(RefExpr);
7960 Privates.push_back(nullptr);
7961 LHSs.push_back(nullptr);
7962 RHSs.push_back(nullptr);
7963 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007964 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00007965 ValueDecl *D = Res.first;
7966 if (!D)
7967 continue;
7968
Alexey Bataeva1764212015-09-30 09:22:36 +00007969 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00007970 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
7971 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
7972 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00007973 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007974 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007975 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7976 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7977 Type = ATy->getElementType();
7978 else
7979 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007980 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007981 } else
7982 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
7983 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00007984
Alexey Bataevc5e02582014-06-16 07:08:35 +00007985 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7986 // A variable that appears in a private clause must not have an incomplete
7987 // type or a reference type.
7988 if (RequireCompleteType(ELoc, Type,
7989 diag::err_omp_reduction_incomplete_type))
7990 continue;
7991 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007992 // A list item that appears in a reduction clause must not be
7993 // const-qualified.
7994 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007995 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007996 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007997 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007998 bool IsDecl = !VD ||
7999 VD->isThisDeclarationADefinition(Context) ==
8000 VarDecl::DeclarationOnly;
8001 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008002 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008003 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008004 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008005 continue;
8006 }
8007 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8008 // If a list-item is a reference type then it must bind to the same object
8009 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008010 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008011 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008012 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008013 DSARefChecker Check(DSAStack);
8014 if (Check.Visit(VDDef->getInit())) {
8015 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8016 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8017 continue;
8018 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008019 }
8020 }
8021 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8022 // The type of a list item that appears in a reduction clause must be valid
8023 // for the reduction-identifier. For a max or min reduction in C, the type
8024 // of the list item must be an allowed arithmetic data type: char, int,
8025 // float, double, or _Bool, possibly modified with long, short, signed, or
8026 // unsigned. For a max or min reduction in C++, the type of the list item
8027 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8028 // double, or bool, possibly modified with long, short, signed, or unsigned.
8029 if ((BOK == BO_GT || BOK == BO_LT) &&
8030 !(Type->isScalarType() ||
8031 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8032 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8033 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008034 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008035 bool IsDecl = !VD ||
8036 VD->isThisDeclarationADefinition(Context) ==
8037 VarDecl::DeclarationOnly;
8038 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008039 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008040 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008041 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008042 continue;
8043 }
8044 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8045 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8046 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008047 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008048 bool IsDecl = !VD ||
8049 VD->isThisDeclarationADefinition(Context) ==
8050 VarDecl::DeclarationOnly;
8051 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008052 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008053 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008054 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008055 continue;
8056 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008057 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8058 // in a Construct]
8059 // Variables with the predetermined data-sharing attributes may not be
8060 // listed in data-sharing attributes clauses, except for the cases
8061 // listed below. For these exceptions only, listing a predetermined
8062 // variable in a data-sharing attribute clause is allowed and overrides
8063 // the variable's predetermined data-sharing attributes.
8064 // OpenMP [2.14.3.6, Restrictions, p.3]
8065 // Any number of reduction clauses can be specified on the directive,
8066 // but a list item can appear only once in the reduction clauses for that
8067 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008068 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008069 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008070 if (DVar.CKind == OMPC_reduction) {
8071 Diag(ELoc, diag::err_omp_once_referenced)
8072 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008073 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008074 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008075 } else if (DVar.CKind != OMPC_unknown) {
8076 Diag(ELoc, diag::err_omp_wrong_dsa)
8077 << getOpenMPClauseName(DVar.CKind)
8078 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008079 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008080 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008081 }
8082
8083 // OpenMP [2.14.3.6, Restrictions, p.1]
8084 // A list item that appears in a reduction clause of a worksharing
8085 // construct must be shared in the parallel regions to which any of the
8086 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008087 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8088 if (isOpenMPWorksharingDirective(CurrDir) &&
8089 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008090 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008091 if (DVar.CKind != OMPC_shared) {
8092 Diag(ELoc, diag::err_omp_required_access)
8093 << getOpenMPClauseName(OMPC_reduction)
8094 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008095 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008096 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008097 }
8098 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008099
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008100 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008101 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008102 D->hasAttrs() ? &D->getAttrs() : nullptr);
8103 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8104 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008105 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008106 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008107 (!ASE &&
8108 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008109 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008110 // Create pseudo array type for private copy. The size for this array will
8111 // be generated during codegen.
8112 // For array subscripts or single variables Private Ty is the same as Type
8113 // (type of the variable or single array element).
8114 PrivateTy = Context.getVariableArrayType(
8115 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8116 Context.getSizeType(), VK_RValue),
8117 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008118 } else if (!ASE && !OASE &&
8119 Context.getAsArrayType(D->getType().getNonReferenceType()))
8120 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008121 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008122 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8123 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008124 // Add initializer for private variable.
8125 Expr *Init = nullptr;
8126 switch (BOK) {
8127 case BO_Add:
8128 case BO_Xor:
8129 case BO_Or:
8130 case BO_LOr:
8131 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008132 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008133 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008134 break;
8135 case BO_Mul:
8136 case BO_LAnd:
8137 if (Type->isScalarType() || Type->isAnyComplexType()) {
8138 // '*' and '&&' reduction ops - initializer is '1'.
8139 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8140 }
8141 break;
8142 case BO_And: {
8143 // '&' reduction op - initializer is '~0'.
8144 QualType OrigType = Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008145 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008146 Type = ComplexTy->getElementType();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008147 if (Type->isRealFloatingType()) {
8148 llvm::APFloat InitValue =
8149 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8150 /*isIEEE=*/true);
8151 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8152 Type, ELoc);
8153 } else if (Type->isScalarType()) {
8154 auto Size = Context.getTypeSize(Type);
8155 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8156 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8157 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8158 }
8159 if (Init && OrigType->isAnyComplexType()) {
8160 // Init = 0xFFFF + 0xFFFFi;
8161 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8162 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8163 }
8164 Type = OrigType;
8165 break;
8166 }
8167 case BO_LT:
8168 case BO_GT: {
8169 // 'min' reduction op - initializer is 'Largest representable number in
8170 // the reduction list item type'.
8171 // 'max' reduction op - initializer is 'Least representable number in
8172 // the reduction list item type'.
8173 if (Type->isIntegerType() || Type->isPointerType()) {
8174 bool IsSigned = Type->hasSignedIntegerRepresentation();
8175 auto Size = Context.getTypeSize(Type);
8176 QualType IntTy =
8177 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8178 llvm::APInt InitValue =
8179 (BOK != BO_LT)
8180 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8181 : llvm::APInt::getMinValue(Size)
8182 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8183 : llvm::APInt::getMaxValue(Size);
8184 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8185 if (Type->isPointerType()) {
8186 // Cast to pointer type.
8187 auto CastExpr = BuildCStyleCastExpr(
8188 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8189 SourceLocation(), Init);
8190 if (CastExpr.isInvalid())
8191 continue;
8192 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008193 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008194 } else if (Type->isRealFloatingType()) {
8195 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8196 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8197 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8198 Type, ELoc);
8199 }
8200 break;
8201 }
8202 case BO_PtrMemD:
8203 case BO_PtrMemI:
8204 case BO_MulAssign:
8205 case BO_Div:
8206 case BO_Rem:
8207 case BO_Sub:
8208 case BO_Shl:
8209 case BO_Shr:
8210 case BO_LE:
8211 case BO_GE:
8212 case BO_EQ:
8213 case BO_NE:
8214 case BO_AndAssign:
8215 case BO_XorAssign:
8216 case BO_OrAssign:
8217 case BO_Assign:
8218 case BO_AddAssign:
8219 case BO_SubAssign:
8220 case BO_DivAssign:
8221 case BO_RemAssign:
8222 case BO_ShlAssign:
8223 case BO_ShrAssign:
8224 case BO_Comma:
8225 llvm_unreachable("Unexpected reduction operation");
8226 }
8227 if (Init) {
8228 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8229 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008230 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008231 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008232 if (!RHSVD->hasInit()) {
8233 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8234 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008235 bool IsDecl =
8236 !VD ||
8237 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8238 Diag(D->getLocation(),
8239 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8240 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008241 continue;
8242 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008243 // Store initializer for single element in private copy. Will be used during
8244 // codegen.
8245 PrivateVD->setInit(RHSVD->getInit());
8246 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008247 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8248 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008249 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008250 ExprResult ReductionOp =
8251 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8252 LHSDRE, RHSDRE);
8253 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008254 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008255 ReductionOp =
8256 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8257 BO_Assign, LHSDRE, ReductionOp.get());
8258 } else {
8259 auto *ConditionalOp = new (Context) ConditionalOperator(
8260 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8261 RHSDRE, Type, VK_LValue, OK_Ordinary);
8262 ReductionOp =
8263 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8264 BO_Assign, LHSDRE, ConditionalOp);
8265 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008266 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008267 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008268 if (ReductionOp.isInvalid())
8269 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008270
Alexey Bataev60da77e2016-02-29 05:54:20 +00008271 DeclRefExpr *Ref = nullptr;
8272 Expr *VarsExpr = RefExpr->IgnoreParens();
8273 if (!VD) {
8274 if (ASE || OASE) {
8275 TransformExprToCaptures RebuildToCapture(*this, D);
8276 VarsExpr =
8277 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8278 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008279 } else {
8280 VarsExpr = Ref =
8281 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8282 if (!IsOpenMPCapturedDecl(D)) {
8283 ExprCaptures.push_back(Ref->getDecl());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008284 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataev61205072016-03-02 04:57:40 +00008285 ExprResult RefRes = DefaultLvalueConversion(Ref);
8286 if (!RefRes.isUsable())
8287 continue;
8288 ExprResult PostUpdateRes =
8289 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8290 SimpleRefExpr, RefRes.get());
8291 if (!PostUpdateRes.isUsable())
8292 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008293 ExprPostUpdates.push_back(
8294 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008295 }
8296 }
8297 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008298 }
8299 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8300 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008301 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008302 LHSs.push_back(LHSDRE);
8303 RHSs.push_back(RHSDRE);
8304 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008305 }
8306
8307 if (Vars.empty())
8308 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008309 Stmt *PreInit = nullptr;
8310 if (!ExprCaptures.empty()) {
8311 PreInit = new (Context)
8312 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
8313 ExprCaptures.size()),
8314 SourceLocation(), SourceLocation());
8315 }
8316 Expr *PostUpdate = nullptr;
8317 if (!ExprPostUpdates.empty()) {
8318 for (auto *E : ExprPostUpdates) {
8319 ExprResult PostUpdateRes =
8320 PostUpdate
8321 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
8322 : E;
8323 PostUpdate = PostUpdateRes.get();
8324 }
8325 }
8326
Alexey Bataevc5e02582014-06-16 07:08:35 +00008327 return OMPReductionClause::Create(
8328 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008329 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev61205072016-03-02 04:57:40 +00008330 LHSs, RHSs, ReductionOps, PreInit, PostUpdate);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008331}
8332
Alexey Bataev182227b2015-08-20 10:54:39 +00008333OMPClause *Sema::ActOnOpenMPLinearClause(
8334 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8335 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8336 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008337 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008338 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008339 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008340 SmallVector<Decl *, 4> ExprCaptures;
8341 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataev182227b2015-08-20 10:54:39 +00008342 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8343 LinKind == OMPC_LINEAR_unknown) {
8344 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8345 LinKind = OMPC_LINEAR_val;
8346 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008347 for (auto &RefExpr : VarList) {
8348 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008349 SourceLocation ELoc;
8350 SourceRange ERange;
8351 Expr *SimpleRefExpr = RefExpr;
8352 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8353 /*AllowArraySection=*/false);
8354 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008355 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008356 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008357 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008358 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008359 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008360 ValueDecl *D = Res.first;
8361 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008362 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008363
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008364 QualType Type = D->getType();
8365 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008366
8367 // OpenMP [2.14.3.7, linear clause]
8368 // A list-item cannot appear in more than one linear clause.
8369 // A list-item that appears in a linear clause cannot appear in any
8370 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008371 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008372 if (DVar.RefExpr) {
8373 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8374 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008375 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008376 continue;
8377 }
8378
8379 // A variable must not have an incomplete type or a reference type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008380 if (RequireCompleteType(ELoc, Type,
8381 diag::err_omp_linear_incomplete_type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008382 continue;
Alexey Bataev1185e192015-08-20 12:15:57 +00008383 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008384 !Type->isReferenceType()) {
Alexey Bataev1185e192015-08-20 12:15:57 +00008385 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008386 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
Alexey Bataev1185e192015-08-20 12:15:57 +00008387 continue;
8388 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008389 Type = Type.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008390
8391 // A list item must not be const-qualified.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008392 if (Type.isConstant(Context)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008393 Diag(ELoc, diag::err_omp_const_variable)
8394 << getOpenMPClauseName(OMPC_linear);
8395 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008396 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008397 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008398 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008399 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008400 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008401 continue;
8402 }
8403
8404 // A list item must be of integral or pointer type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008405 Type = Type.getUnqualifiedType().getCanonicalType();
8406 const auto *Ty = Type.getTypePtrOrNull();
Alexander Musman8dba6642014-04-22 13:09:42 +00008407 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8408 !Ty->isPointerType())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008409 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
Alexander Musman8dba6642014-04-22 13:09:42 +00008410 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008411 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008412 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008413 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008414 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008415 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008416 continue;
8417 }
8418
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008419 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008420 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8421 D->hasAttrs() ? &D->getAttrs() : nullptr);
8422 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008423 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008424 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008425 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008426 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008427 if (!VD) {
8428 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8429 if (!IsOpenMPCapturedDecl(D)) {
8430 ExprCaptures.push_back(Ref->getDecl());
8431 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8432 ExprResult RefRes = DefaultLvalueConversion(Ref);
8433 if (!RefRes.isUsable())
8434 continue;
8435 ExprResult PostUpdateRes =
8436 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8437 SimpleRefExpr, RefRes.get());
8438 if (!PostUpdateRes.isUsable())
8439 continue;
8440 ExprPostUpdates.push_back(
8441 IgnoredValueConversions(PostUpdateRes.get()).get());
8442 }
8443 }
8444 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008445 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008446 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008447 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008448 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008449 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008450 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8451 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8452
8453 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8454 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008455 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008456 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008457 }
8458
8459 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008460 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008461
8462 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008463 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008464 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8465 !Step->isInstantiationDependent() &&
8466 !Step->containsUnexpandedParameterPack()) {
8467 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008468 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008469 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008470 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008471 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008472
Alexander Musman3276a272015-03-21 10:12:56 +00008473 // Build var to save the step value.
8474 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008475 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008476 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008477 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008478 ExprResult CalcStep =
8479 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008480 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008481
Alexander Musman8dba6642014-04-22 13:09:42 +00008482 // Warn about zero linear step (it would be probably better specified as
8483 // making corresponding variables 'const').
8484 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008485 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8486 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008487 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8488 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008489 if (!IsConstant && CalcStep.isUsable()) {
8490 // Calculate the step beforehand instead of doing this on each iteration.
8491 // (This is not used if the number of iterations may be kfold-ed).
8492 CalcStepExpr = CalcStep.get();
8493 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008494 }
8495
Alexey Bataev78849fb2016-03-09 09:49:00 +00008496 Stmt *PreInit = nullptr;
8497 if (!ExprCaptures.empty()) {
8498 PreInit = new (Context)
8499 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
8500 ExprCaptures.size()),
8501 SourceLocation(), SourceLocation());
8502 }
8503 Expr *PostUpdate = nullptr;
8504 if (!ExprPostUpdates.empty()) {
8505 for (auto *E : ExprPostUpdates) {
8506 ExprResult PostUpdateRes =
8507 PostUpdate
8508 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
8509 : E;
8510 PostUpdate = PostUpdateRes.get();
8511 }
8512 }
8513
Alexey Bataev182227b2015-08-20 10:54:39 +00008514 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8515 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev78849fb2016-03-09 09:49:00 +00008516 StepExpr, CalcStepExpr, PreInit, PostUpdate);
Alexander Musman3276a272015-03-21 10:12:56 +00008517}
8518
8519static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8520 Expr *NumIterations, Sema &SemaRef,
8521 Scope *S) {
8522 // Walk the vars and build update/final expressions for the CodeGen.
8523 SmallVector<Expr *, 8> Updates;
8524 SmallVector<Expr *, 8> Finals;
8525 Expr *Step = Clause.getStep();
8526 Expr *CalcStep = Clause.getCalcStep();
8527 // OpenMP [2.14.3.7, linear clause]
8528 // If linear-step is not specified it is assumed to be 1.
8529 if (Step == nullptr)
8530 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8531 else if (CalcStep)
8532 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8533 bool HasErrors = false;
8534 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008535 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008536 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008537 for (auto &RefExpr : Clause.varlists()) {
8538 Expr *InitExpr = *CurInit;
8539
8540 // Build privatized reference to the current linear var.
8541 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008542 Expr *CapturedRef;
8543 if (LinKind == OMPC_LINEAR_uval)
8544 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8545 else
8546 CapturedRef =
8547 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8548 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8549 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008550
8551 // Build update: Var = InitExpr + IV * Step
8552 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008553 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008554 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008555 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8556 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008557
8558 // Build final: Var = InitExpr + NumIterations * Step
8559 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008560 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008561 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008562 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8563 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008564 if (!Update.isUsable() || !Final.isUsable()) {
8565 Updates.push_back(nullptr);
8566 Finals.push_back(nullptr);
8567 HasErrors = true;
8568 } else {
8569 Updates.push_back(Update.get());
8570 Finals.push_back(Final.get());
8571 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008572 ++CurInit;
8573 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008574 }
8575 Clause.setUpdates(Updates);
8576 Clause.setFinals(Finals);
8577 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008578}
8579
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008580OMPClause *Sema::ActOnOpenMPAlignedClause(
8581 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8582 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8583
8584 SmallVector<Expr *, 8> Vars;
8585 for (auto &RefExpr : VarList) {
8586 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8587 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8588 // It will be analyzed later.
8589 Vars.push_back(RefExpr);
8590 continue;
8591 }
8592
8593 SourceLocation ELoc = RefExpr->getExprLoc();
8594 // OpenMP [2.1, C/C++]
8595 // A list item is a variable name.
8596 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8597 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008598 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8599 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008600 continue;
8601 }
8602
8603 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8604
8605 // OpenMP [2.8.1, simd construct, Restrictions]
8606 // The type of list items appearing in the aligned clause must be
8607 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008608 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008609 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008610 const Type *Ty = QType.getTypePtrOrNull();
8611 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8612 !Ty->isPointerType())) {
8613 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8614 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8615 bool IsDecl =
8616 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8617 Diag(VD->getLocation(),
8618 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8619 << VD;
8620 continue;
8621 }
8622
8623 // OpenMP [2.8.1, simd construct, Restrictions]
8624 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008625 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008626 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8627 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8628 << getOpenMPClauseName(OMPC_aligned);
8629 continue;
8630 }
8631
8632 Vars.push_back(DE);
8633 }
8634
8635 // OpenMP [2.8.1, simd construct, Description]
8636 // The parameter of the aligned clause, alignment, must be a constant
8637 // positive integer expression.
8638 // If no optional parameter is specified, implementation-defined default
8639 // alignments for SIMD instructions on the target platforms are assumed.
8640 if (Alignment != nullptr) {
8641 ExprResult AlignResult =
8642 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8643 if (AlignResult.isInvalid())
8644 return nullptr;
8645 Alignment = AlignResult.get();
8646 }
8647 if (Vars.empty())
8648 return nullptr;
8649
8650 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8651 EndLoc, Vars, Alignment);
8652}
8653
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008654OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8655 SourceLocation StartLoc,
8656 SourceLocation LParenLoc,
8657 SourceLocation EndLoc) {
8658 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008659 SmallVector<Expr *, 8> SrcExprs;
8660 SmallVector<Expr *, 8> DstExprs;
8661 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008662 for (auto &RefExpr : VarList) {
8663 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8664 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008665 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008666 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008667 SrcExprs.push_back(nullptr);
8668 DstExprs.push_back(nullptr);
8669 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008670 continue;
8671 }
8672
Alexey Bataeved09d242014-05-28 05:53:51 +00008673 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008674 // OpenMP [2.1, C/C++]
8675 // A list item is a variable name.
8676 // OpenMP [2.14.4.1, Restrictions, p.1]
8677 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008678 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008679 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008680 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8681 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008682 continue;
8683 }
8684
8685 Decl *D = DE->getDecl();
8686 VarDecl *VD = cast<VarDecl>(D);
8687
8688 QualType Type = VD->getType();
8689 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8690 // It will be analyzed later.
8691 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008692 SrcExprs.push_back(nullptr);
8693 DstExprs.push_back(nullptr);
8694 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008695 continue;
8696 }
8697
8698 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8699 // A list item that appears in a copyin clause must be threadprivate.
8700 if (!DSAStack->isThreadPrivate(VD)) {
8701 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008702 << getOpenMPClauseName(OMPC_copyin)
8703 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008704 continue;
8705 }
8706
8707 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8708 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008709 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008710 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008711 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008712 auto *SrcVD =
8713 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8714 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008715 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008716 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8717 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008718 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8719 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008720 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008721 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008722 // For arrays generate assignment operation for single element and replace
8723 // it by the original array element in CodeGen.
8724 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8725 PseudoDstExpr, PseudoSrcExpr);
8726 if (AssignmentOp.isInvalid())
8727 continue;
8728 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8729 /*DiscardedValue=*/true);
8730 if (AssignmentOp.isInvalid())
8731 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008732
8733 DSAStack->addDSA(VD, DE, OMPC_copyin);
8734 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008735 SrcExprs.push_back(PseudoSrcExpr);
8736 DstExprs.push_back(PseudoDstExpr);
8737 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008738 }
8739
Alexey Bataeved09d242014-05-28 05:53:51 +00008740 if (Vars.empty())
8741 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008742
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008743 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8744 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008745}
8746
Alexey Bataevbae9a792014-06-27 10:37:06 +00008747OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8748 SourceLocation StartLoc,
8749 SourceLocation LParenLoc,
8750 SourceLocation EndLoc) {
8751 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008752 SmallVector<Expr *, 8> SrcExprs;
8753 SmallVector<Expr *, 8> DstExprs;
8754 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008755 for (auto &RefExpr : VarList) {
8756 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8757 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8758 // It will be analyzed later.
8759 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008760 SrcExprs.push_back(nullptr);
8761 DstExprs.push_back(nullptr);
8762 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008763 continue;
8764 }
8765
8766 SourceLocation ELoc = RefExpr->getExprLoc();
8767 // OpenMP [2.1, C/C++]
8768 // A list item is a variable name.
8769 // OpenMP [2.14.4.1, Restrictions, p.1]
8770 // A list item that appears in a copyin clause must be threadprivate.
8771 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8772 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008773 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8774 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008775 continue;
8776 }
8777
8778 Decl *D = DE->getDecl();
8779 VarDecl *VD = cast<VarDecl>(D);
8780
8781 QualType Type = VD->getType();
8782 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8783 // It will be analyzed later.
8784 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008785 SrcExprs.push_back(nullptr);
8786 DstExprs.push_back(nullptr);
8787 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008788 continue;
8789 }
8790
8791 // OpenMP [2.14.4.2, Restrictions, p.2]
8792 // A list item that appears in a copyprivate clause may not appear in a
8793 // private or firstprivate clause on the single construct.
8794 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008795 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008796 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8797 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008798 Diag(ELoc, diag::err_omp_wrong_dsa)
8799 << getOpenMPClauseName(DVar.CKind)
8800 << getOpenMPClauseName(OMPC_copyprivate);
8801 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8802 continue;
8803 }
8804
8805 // OpenMP [2.11.4.2, Restrictions, p.1]
8806 // All list items that appear in a copyprivate clause must be either
8807 // threadprivate or private in the enclosing context.
8808 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008809 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008810 if (DVar.CKind == OMPC_shared) {
8811 Diag(ELoc, diag::err_omp_required_access)
8812 << getOpenMPClauseName(OMPC_copyprivate)
8813 << "threadprivate or private in the enclosing context";
8814 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8815 continue;
8816 }
8817 }
8818 }
8819
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008820 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008821 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008822 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008823 << getOpenMPClauseName(OMPC_copyprivate) << Type
8824 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008825 bool IsDecl =
8826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8827 Diag(VD->getLocation(),
8828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8829 << VD;
8830 continue;
8831 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008832
Alexey Bataevbae9a792014-06-27 10:37:06 +00008833 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8834 // A variable of class type (or array thereof) that appears in a
8835 // copyin clause requires an accessible, unambiguous copy assignment
8836 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008837 Type = Context.getBaseElementType(Type.getNonReferenceType())
8838 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008839 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008840 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8841 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008842 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008843 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008844 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008845 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8846 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008847 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008848 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008849 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8850 PseudoDstExpr, PseudoSrcExpr);
8851 if (AssignmentOp.isInvalid())
8852 continue;
8853 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8854 /*DiscardedValue=*/true);
8855 if (AssignmentOp.isInvalid())
8856 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008857
8858 // No need to mark vars as copyprivate, they are already threadprivate or
8859 // implicitly private.
8860 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008861 SrcExprs.push_back(PseudoSrcExpr);
8862 DstExprs.push_back(PseudoDstExpr);
8863 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008864 }
8865
8866 if (Vars.empty())
8867 return nullptr;
8868
Alexey Bataeva63048e2015-03-23 06:18:07 +00008869 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8870 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008871}
8872
Alexey Bataev6125da92014-07-21 11:26:11 +00008873OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8874 SourceLocation StartLoc,
8875 SourceLocation LParenLoc,
8876 SourceLocation EndLoc) {
8877 if (VarList.empty())
8878 return nullptr;
8879
8880 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8881}
Alexey Bataevdea47612014-07-23 07:46:59 +00008882
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008883OMPClause *
8884Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8885 SourceLocation DepLoc, SourceLocation ColonLoc,
8886 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8887 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008888 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008889 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008890 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008891 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008892 return nullptr;
8893 }
8894 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008895 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8896 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008897 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008898 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008899 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8900 /*Last=*/OMPC_DEPEND_unknown, Except)
8901 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008902 return nullptr;
8903 }
8904 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008905 llvm::APSInt DepCounter(/*BitWidth=*/32);
8906 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8907 if (DepKind == OMPC_DEPEND_sink) {
8908 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8909 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8910 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008911 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008912 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008913 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8914 DSAStack->getParentOrderedRegionParam()) {
8915 for (auto &RefExpr : VarList) {
8916 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8917 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8918 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8919 // It will be analyzed later.
8920 Vars.push_back(RefExpr);
8921 continue;
8922 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008923
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008924 SourceLocation ELoc = RefExpr->getExprLoc();
8925 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8926 if (DepKind == OMPC_DEPEND_sink) {
8927 if (DepCounter >= TotalDepCount) {
8928 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8929 continue;
8930 }
8931 ++DepCounter;
8932 // OpenMP [2.13.9, Summary]
8933 // depend(dependence-type : vec), where dependence-type is:
8934 // 'sink' and where vec is the iteration vector, which has the form:
8935 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8936 // where n is the value specified by the ordered clause in the loop
8937 // directive, xi denotes the loop iteration variable of the i-th nested
8938 // loop associated with the loop directive, and di is a constant
8939 // non-negative integer.
8940 SimpleExpr = SimpleExpr->IgnoreImplicit();
8941 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8942 if (!DE) {
8943 OverloadedOperatorKind OOK = OO_None;
8944 SourceLocation OOLoc;
8945 Expr *LHS, *RHS;
8946 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8947 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8948 OOLoc = BO->getOperatorLoc();
8949 LHS = BO->getLHS()->IgnoreParenImpCasts();
8950 RHS = BO->getRHS()->IgnoreParenImpCasts();
8951 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8952 OOK = OCE->getOperator();
8953 OOLoc = OCE->getOperatorLoc();
8954 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8955 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8956 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8957 OOK = MCE->getMethodDecl()
8958 ->getNameInfo()
8959 .getName()
8960 .getCXXOverloadedOperator();
8961 OOLoc = MCE->getCallee()->getExprLoc();
8962 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8963 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8964 } else {
8965 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8966 continue;
8967 }
8968 DE = dyn_cast<DeclRefExpr>(LHS);
8969 if (!DE) {
8970 Diag(LHS->getExprLoc(),
8971 diag::err_omp_depend_sink_expected_loop_iteration)
8972 << DSAStack->getParentLoopControlVariable(
8973 DepCounter.getZExtValue());
8974 continue;
8975 }
8976 if (OOK != OO_Plus && OOK != OO_Minus) {
8977 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8978 continue;
8979 }
8980 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8981 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8982 if (Res.isInvalid())
8983 continue;
8984 }
8985 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8986 if (!CurContext->isDependentContext() &&
8987 DSAStack->getParentOrderedRegionParam() &&
8988 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8989 Diag(DE->getExprLoc(),
8990 diag::err_omp_depend_sink_expected_loop_iteration)
8991 << DSAStack->getParentLoopControlVariable(
8992 DepCounter.getZExtValue());
8993 continue;
8994 }
8995 } else {
8996 // OpenMP [2.11.1.1, Restrictions, p.3]
8997 // A variable that is part of another variable (such as a field of a
8998 // structure) but is not an array element or an array section cannot
8999 // appear in a depend clause.
9000 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9001 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9002 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9003 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9004 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009005 (ASE &&
9006 !ASE->getBase()
9007 ->getType()
9008 .getNonReferenceType()
9009 ->isPointerType() &&
9010 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009011 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9012 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009013 continue;
9014 }
9015 }
9016
9017 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9018 }
9019
9020 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9021 TotalDepCount > VarList.size() &&
9022 DSAStack->getParentOrderedRegionParam()) {
9023 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9024 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9025 }
9026 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9027 Vars.empty())
9028 return nullptr;
9029 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009030
9031 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9032 DepLoc, ColonLoc, Vars);
9033}
Michael Wonge710d542015-08-07 16:16:36 +00009034
9035OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9036 SourceLocation LParenLoc,
9037 SourceLocation EndLoc) {
9038 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009039
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009040 // OpenMP [2.9.1, Restrictions]
9041 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009042 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9043 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009044 return nullptr;
9045
Michael Wonge710d542015-08-07 16:16:36 +00009046 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9047}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009048
9049static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9050 DSAStackTy *Stack, CXXRecordDecl *RD) {
9051 if (!RD || RD->isInvalidDecl())
9052 return true;
9053
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009054 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9055 if (auto *CTD = CTSD->getSpecializedTemplate())
9056 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009057 auto QTy = SemaRef.Context.getRecordType(RD);
9058 if (RD->isDynamicClass()) {
9059 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9060 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9061 return false;
9062 }
9063 auto *DC = RD;
9064 bool IsCorrect = true;
9065 for (auto *I : DC->decls()) {
9066 if (I) {
9067 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9068 if (MD->isStatic()) {
9069 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9070 SemaRef.Diag(MD->getLocation(),
9071 diag::note_omp_static_member_in_target);
9072 IsCorrect = false;
9073 }
9074 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9075 if (VD->isStaticDataMember()) {
9076 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9077 SemaRef.Diag(VD->getLocation(),
9078 diag::note_omp_static_member_in_target);
9079 IsCorrect = false;
9080 }
9081 }
9082 }
9083 }
9084
9085 for (auto &I : RD->bases()) {
9086 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9087 I.getType()->getAsCXXRecordDecl()))
9088 IsCorrect = false;
9089 }
9090 return IsCorrect;
9091}
9092
9093static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9094 DSAStackTy *Stack, QualType QTy) {
9095 NamedDecl *ND;
9096 if (QTy->isIncompleteType(&ND)) {
9097 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9098 return false;
9099 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9100 if (!RD->isInvalidDecl() &&
9101 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9102 return false;
9103 }
9104 return true;
9105}
9106
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009107/// \brief Return true if it can be proven that the provided array expression
9108/// (array section or array subscript) does NOT specify the whole size of the
9109/// array whose base type is \a BaseQTy.
9110static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9111 const Expr *E,
9112 QualType BaseQTy) {
9113 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9114
9115 // If this is an array subscript, it refers to the whole size if the size of
9116 // the dimension is constant and equals 1. Also, an array section assumes the
9117 // format of an array subscript if no colon is used.
9118 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9119 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9120 return ATy->getSize().getSExtValue() != 1;
9121 // Size can't be evaluated statically.
9122 return false;
9123 }
9124
9125 assert(OASE && "Expecting array section if not an array subscript.");
9126 auto *LowerBound = OASE->getLowerBound();
9127 auto *Length = OASE->getLength();
9128
9129 // If there is a lower bound that does not evaluates to zero, we are not
9130 // convering the whole dimension.
9131 if (LowerBound) {
9132 llvm::APSInt ConstLowerBound;
9133 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9134 return false; // Can't get the integer value as a constant.
9135 if (ConstLowerBound.getSExtValue())
9136 return true;
9137 }
9138
9139 // If we don't have a length we covering the whole dimension.
9140 if (!Length)
9141 return false;
9142
9143 // If the base is a pointer, we don't have a way to get the size of the
9144 // pointee.
9145 if (BaseQTy->isPointerType())
9146 return false;
9147
9148 // We can only check if the length is the same as the size of the dimension
9149 // if we have a constant array.
9150 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9151 if (!CATy)
9152 return false;
9153
9154 llvm::APSInt ConstLength;
9155 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9156 return false; // Can't get the integer value as a constant.
9157
9158 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9159}
9160
9161// Return true if it can be proven that the provided array expression (array
9162// section or array subscript) does NOT specify a single element of the array
9163// whose base type is \a BaseQTy.
9164static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9165 const Expr *E,
9166 QualType BaseQTy) {
9167 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9168
9169 // An array subscript always refer to a single element. Also, an array section
9170 // assumes the format of an array subscript if no colon is used.
9171 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9172 return false;
9173
9174 assert(OASE && "Expecting array section if not an array subscript.");
9175 auto *Length = OASE->getLength();
9176
9177 // If we don't have a length we have to check if the array has unitary size
9178 // for this dimension. Also, we should always expect a length if the base type
9179 // is pointer.
9180 if (!Length) {
9181 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9182 return ATy->getSize().getSExtValue() != 1;
9183 // We cannot assume anything.
9184 return false;
9185 }
9186
9187 // Check if the length evaluates to 1.
9188 llvm::APSInt ConstLength;
9189 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9190 return false; // Can't get the integer value as a constant.
9191
9192 return ConstLength.getSExtValue() != 1;
9193}
9194
Samuel Antao5de996e2016-01-22 20:21:36 +00009195// Return the expression of the base of the map clause or null if it cannot
9196// be determined and do all the necessary checks to see if the expression is
9197// valid as a standalone map clause expression.
9198static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9199 SourceLocation ELoc = E->getExprLoc();
9200 SourceRange ERange = E->getSourceRange();
9201
9202 // The base of elements of list in a map clause have to be either:
9203 // - a reference to variable or field.
9204 // - a member expression.
9205 // - an array expression.
9206 //
9207 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9208 // reference to 'r'.
9209 //
9210 // If we have:
9211 //
9212 // struct SS {
9213 // Bla S;
9214 // foo() {
9215 // #pragma omp target map (S.Arr[:12]);
9216 // }
9217 // }
9218 //
9219 // We want to retrieve the member expression 'this->S';
9220
9221 Expr *RelevantExpr = nullptr;
9222
Samuel Antao5de996e2016-01-22 20:21:36 +00009223 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9224 // If a list item is an array section, it must specify contiguous storage.
9225 //
9226 // For this restriction it is sufficient that we make sure only references
9227 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009228 // exist except in the rightmost expression (unless they cover the whole
9229 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009230 //
9231 // r.ArrS[3:5].Arr[6:7]
9232 //
9233 // r.ArrS[3:5].x
9234 //
9235 // but these would be valid:
9236 // r.ArrS[3].Arr[6:7]
9237 //
9238 // r.ArrS[3].x
9239
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009240 bool AllowUnitySizeArraySection = true;
9241 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009242
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009243 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009244 E = E->IgnoreParenImpCasts();
9245
9246 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9247 if (!isa<VarDecl>(CurE->getDecl()))
9248 break;
9249
9250 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009251
9252 // If we got a reference to a declaration, we should not expect any array
9253 // section before that.
9254 AllowUnitySizeArraySection = false;
9255 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009256 continue;
9257 }
9258
9259 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9260 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9261
9262 if (isa<CXXThisExpr>(BaseE))
9263 // We found a base expression: this->Val.
9264 RelevantExpr = CurE;
9265 else
9266 E = BaseE;
9267
9268 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9269 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9270 << CurE->getSourceRange();
9271 break;
9272 }
9273
9274 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9275
9276 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9277 // A bit-field cannot appear in a map clause.
9278 //
9279 if (FD->isBitField()) {
9280 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9281 << CurE->getSourceRange();
9282 break;
9283 }
9284
9285 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9286 // If the type of a list item is a reference to a type T then the type
9287 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009288 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009289
9290 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9291 // A list item cannot be a variable that is a member of a structure with
9292 // a union type.
9293 //
9294 if (auto *RT = CurType->getAs<RecordType>())
9295 if (RT->isUnionType()) {
9296 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9297 << CurE->getSourceRange();
9298 break;
9299 }
9300
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009301 // If we got a member expression, we should not expect any array section
9302 // before that:
9303 //
9304 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9305 // If a list item is an element of a structure, only the rightmost symbol
9306 // of the variable reference can be an array section.
9307 //
9308 AllowUnitySizeArraySection = false;
9309 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009310 continue;
9311 }
9312
9313 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9314 E = CurE->getBase()->IgnoreParenImpCasts();
9315
9316 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9317 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9318 << 0 << CurE->getSourceRange();
9319 break;
9320 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009321
9322 // If we got an array subscript that express the whole dimension we
9323 // can have any array expressions before. If it only expressing part of
9324 // the dimension, we can only have unitary-size array expressions.
9325 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9326 E->getType()))
9327 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009328 continue;
9329 }
9330
9331 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009332 E = CurE->getBase()->IgnoreParenImpCasts();
9333
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009334 auto CurType =
9335 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9336
Samuel Antao5de996e2016-01-22 20:21:36 +00009337 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9338 // If the type of a list item is a reference to a type T then the type
9339 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009340 if (CurType->isReferenceType())
9341 CurType = CurType->getPointeeType();
9342
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009343 bool IsPointer = CurType->isAnyPointerType();
9344
9345 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009346 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9347 << 0 << CurE->getSourceRange();
9348 break;
9349 }
9350
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009351 bool NotWhole =
9352 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9353 bool NotUnity =
9354 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9355
9356 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9357 // Any array section is currently allowed.
9358 //
9359 // If this array section refers to the whole dimension we can still
9360 // accept other array sections before this one, except if the base is a
9361 // pointer. Otherwise, only unitary sections are accepted.
9362 if (NotWhole || IsPointer)
9363 AllowWholeSizeArraySection = false;
9364 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9365 (AllowWholeSizeArraySection && NotWhole)) {
9366 // A unity or whole array section is not allowed and that is not
9367 // compatible with the properties of the current array section.
9368 SemaRef.Diag(
9369 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9370 << CurE->getSourceRange();
9371 break;
9372 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009373 continue;
9374 }
9375
9376 // If nothing else worked, this is not a valid map clause expression.
9377 SemaRef.Diag(ELoc,
9378 diag::err_omp_expected_named_var_member_or_array_expression)
9379 << ERange;
9380 break;
9381 }
9382
9383 return RelevantExpr;
9384}
9385
9386// Return true if expression E associated with value VD has conflicts with other
9387// map information.
9388static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9389 Expr *E, bool CurrentRegionOnly) {
9390 assert(VD && E);
9391
9392 // Types used to organize the components of a valid map clause.
9393 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9394 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9395
9396 // Helper to extract the components in the map clause expression E and store
9397 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9398 // it has already passed the single clause checks.
9399 auto ExtractMapExpressionComponents = [](Expr *TE,
9400 MapExpressionComponents &MEC) {
9401 while (true) {
9402 TE = TE->IgnoreParenImpCasts();
9403
9404 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9405 MEC.push_back(
9406 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9407 break;
9408 }
9409
9410 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9411 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9412
9413 MEC.push_back(MapExpressionComponent(
9414 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9415 if (isa<CXXThisExpr>(BaseE))
9416 break;
9417
9418 TE = BaseE;
9419 continue;
9420 }
9421
9422 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9423 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9424 TE = CurE->getBase()->IgnoreParenImpCasts();
9425 continue;
9426 }
9427
9428 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9429 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9430 TE = CurE->getBase()->IgnoreParenImpCasts();
9431 continue;
9432 }
9433
9434 llvm_unreachable(
9435 "Expecting only valid map clause expressions at this point!");
9436 }
9437 };
9438
9439 SourceLocation ELoc = E->getExprLoc();
9440 SourceRange ERange = E->getSourceRange();
9441
9442 // In order to easily check the conflicts we need to match each component of
9443 // the expression under test with the components of the expressions that are
9444 // already in the stack.
9445
9446 MapExpressionComponents CurComponents;
9447 ExtractMapExpressionComponents(E, CurComponents);
9448
9449 assert(!CurComponents.empty() && "Map clause expression with no components!");
9450 assert(CurComponents.back().second == VD &&
9451 "Map clause expression with unexpected base!");
9452
9453 // Variables to help detecting enclosing problems in data environment nests.
9454 bool IsEnclosedByDataEnvironmentExpr = false;
9455 Expr *EnclosingExpr = nullptr;
9456
9457 bool FoundError =
9458 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9459 MapExpressionComponents StackComponents;
9460 ExtractMapExpressionComponents(RE, StackComponents);
9461 assert(!StackComponents.empty() &&
9462 "Map clause expression with no components!");
9463 assert(StackComponents.back().second == VD &&
9464 "Map clause expression with unexpected base!");
9465
9466 // Expressions must start from the same base. Here we detect at which
9467 // point both expressions diverge from each other and see if we can
9468 // detect if the memory referred to both expressions is contiguous and
9469 // do not overlap.
9470 auto CI = CurComponents.rbegin();
9471 auto CE = CurComponents.rend();
9472 auto SI = StackComponents.rbegin();
9473 auto SE = StackComponents.rend();
9474 for (; CI != CE && SI != SE; ++CI, ++SI) {
9475
9476 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9477 // At most one list item can be an array item derived from a given
9478 // variable in map clauses of the same construct.
9479 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9480 isa<OMPArraySectionExpr>(CI->first)) &&
9481 (isa<ArraySubscriptExpr>(SI->first) ||
9482 isa<OMPArraySectionExpr>(SI->first))) {
9483 SemaRef.Diag(CI->first->getExprLoc(),
9484 diag::err_omp_multiple_array_items_in_map_clause)
9485 << CI->first->getSourceRange();
9486 ;
9487 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9488 << SI->first->getSourceRange();
9489 return true;
9490 }
9491
9492 // Do both expressions have the same kind?
9493 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9494 break;
9495
9496 // Are we dealing with different variables/fields?
9497 if (CI->second != SI->second)
9498 break;
9499 }
9500
9501 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9502 // List items of map clauses in the same construct must not share
9503 // original storage.
9504 //
9505 // If the expressions are exactly the same or one is a subset of the
9506 // other, it means they are sharing storage.
9507 if (CI == CE && SI == SE) {
9508 if (CurrentRegionOnly) {
9509 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9510 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9511 << RE->getSourceRange();
9512 return true;
9513 } else {
9514 // If we find the same expression in the enclosing data environment,
9515 // that is legal.
9516 IsEnclosedByDataEnvironmentExpr = true;
9517 return false;
9518 }
9519 }
9520
9521 QualType DerivedType = std::prev(CI)->first->getType();
9522 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9523
9524 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9525 // If the type of a list item is a reference to a type T then the type
9526 // will be considered to be T for all purposes of this clause.
9527 if (DerivedType->isReferenceType())
9528 DerivedType = DerivedType->getPointeeType();
9529
9530 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9531 // A variable for which the type is pointer and an array section
9532 // derived from that variable must not appear as list items of map
9533 // clauses of the same construct.
9534 //
9535 // Also, cover one of the cases in:
9536 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9537 // If any part of the original storage of a list item has corresponding
9538 // storage in the device data environment, all of the original storage
9539 // must have corresponding storage in the device data environment.
9540 //
9541 if (DerivedType->isAnyPointerType()) {
9542 if (CI == CE || SI == SE) {
9543 SemaRef.Diag(
9544 DerivedLoc,
9545 diag::err_omp_pointer_mapped_along_with_derived_section)
9546 << DerivedLoc;
9547 } else {
9548 assert(CI != CE && SI != SE);
9549 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9550 << DerivedLoc;
9551 }
9552 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9553 << RE->getSourceRange();
9554 return true;
9555 }
9556
9557 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9558 // List items of map clauses in the same construct must not share
9559 // original storage.
9560 //
9561 // An expression is a subset of the other.
9562 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9563 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9564 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9565 << RE->getSourceRange();
9566 return true;
9567 }
9568
9569 // The current expression uses the same base as other expression in the
9570 // data environment but does not contain it completelly.
9571 if (!CurrentRegionOnly && SI != SE)
9572 EnclosingExpr = RE;
9573
9574 // The current expression is a subset of the expression in the data
9575 // environment.
9576 IsEnclosedByDataEnvironmentExpr |=
9577 (!CurrentRegionOnly && CI != CE && SI == SE);
9578
9579 return false;
9580 });
9581
9582 if (CurrentRegionOnly)
9583 return FoundError;
9584
9585 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9586 // If any part of the original storage of a list item has corresponding
9587 // storage in the device data environment, all of the original storage must
9588 // have corresponding storage in the device data environment.
9589 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9590 // If a list item is an element of a structure, and a different element of
9591 // the structure has a corresponding list item in the device data environment
9592 // prior to a task encountering the construct associated with the map clause,
9593 // then the list item must also have a correspnding list item in the device
9594 // data environment prior to the task encountering the construct.
9595 //
9596 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9597 SemaRef.Diag(ELoc,
9598 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9599 << ERange;
9600 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9601 << EnclosingExpr->getSourceRange();
9602 return true;
9603 }
9604
9605 return FoundError;
9606}
9607
Samuel Antao23abd722016-01-19 20:40:49 +00009608OMPClause *
9609Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9610 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9611 SourceLocation MapLoc, SourceLocation ColonLoc,
9612 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9613 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009614 SmallVector<Expr *, 4> Vars;
9615
9616 for (auto &RE : VarList) {
9617 assert(RE && "Null expr in omp map");
9618 if (isa<DependentScopeDeclRefExpr>(RE)) {
9619 // It will be analyzed later.
9620 Vars.push_back(RE);
9621 continue;
9622 }
9623 SourceLocation ELoc = RE->getExprLoc();
9624
Kelvin Li0bff7af2015-11-23 05:32:03 +00009625 auto *VE = RE->IgnoreParenLValueCasts();
9626
9627 if (VE->isValueDependent() || VE->isTypeDependent() ||
9628 VE->isInstantiationDependent() ||
9629 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009630 // We can only analyze this information once the missing information is
9631 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009632 Vars.push_back(RE);
9633 continue;
9634 }
9635
9636 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009637
Samuel Antao5de996e2016-01-22 20:21:36 +00009638 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9639 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9640 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009641 continue;
9642 }
9643
Samuel Antao5de996e2016-01-22 20:21:36 +00009644 // Obtain the array or member expression bases if required.
9645 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9646 if (!BE)
9647 continue;
9648
9649 // If the base is a reference to a variable, we rely on that variable for
9650 // the following checks. If it is a 'this' expression we rely on the field.
9651 ValueDecl *D = nullptr;
9652 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9653 D = DRE->getDecl();
9654 } else {
9655 auto *ME = cast<MemberExpr>(BE);
9656 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9657 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009658 }
9659 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009660
Samuel Antao5de996e2016-01-22 20:21:36 +00009661 auto *VD = dyn_cast<VarDecl>(D);
9662 auto *FD = dyn_cast<FieldDecl>(D);
9663
9664 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009665 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009666
9667 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9668 // threadprivate variables cannot appear in a map clause.
9669 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009670 auto DVar = DSAStack->getTopDSA(VD, false);
9671 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9672 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9673 continue;
9674 }
9675
Samuel Antao5de996e2016-01-22 20:21:36 +00009676 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9677 // A list item cannot appear in both a map clause and a data-sharing
9678 // attribute clause on the same construct.
9679 //
9680 // TODO: Implement this check - it cannot currently be tested because of
9681 // missing implementation of the other data sharing clauses in target
9682 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009683
Samuel Antao5de996e2016-01-22 20:21:36 +00009684 // Check conflicts with other map clause expressions. We check the conflicts
9685 // with the current construct separately from the enclosing data
9686 // environment, because the restrictions are different.
9687 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9688 /*CurrentRegionOnly=*/true))
9689 break;
9690 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9691 /*CurrentRegionOnly=*/false))
9692 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009693
Samuel Antao5de996e2016-01-22 20:21:36 +00009694 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9695 // If the type of a list item is a reference to a type T then the type will
9696 // be considered to be T for all purposes of this clause.
9697 QualType Type = D->getType();
9698 if (Type->isReferenceType())
9699 Type = Type->getPointeeType();
9700
9701 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009702 // A list item must have a mappable type.
9703 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9704 DSAStack, Type))
9705 continue;
9706
Samuel Antaodf67fc42016-01-19 19:15:56 +00009707 // target enter data
9708 // OpenMP [2.10.2, Restrictions, p. 99]
9709 // A map-type must be specified in all map clauses and must be either
9710 // to or alloc.
9711 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9712 if (DKind == OMPD_target_enter_data &&
9713 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9714 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009715 << (IsMapTypeImplicit ? 1 : 0)
9716 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009717 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009718 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009719 }
9720
Samuel Antao72590762016-01-19 20:04:50 +00009721 // target exit_data
9722 // OpenMP [2.10.3, Restrictions, p. 102]
9723 // A map-type must be specified in all map clauses and must be either
9724 // from, release, or delete.
9725 DKind = DSAStack->getCurrentDirective();
9726 if (DKind == OMPD_target_exit_data &&
9727 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9728 MapType == OMPC_MAP_delete)) {
9729 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009730 << (IsMapTypeImplicit ? 1 : 0)
9731 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009732 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009733 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009734 }
9735
Kelvin Li0bff7af2015-11-23 05:32:03 +00009736 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009737 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009738 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009739
Samuel Antao5de996e2016-01-22 20:21:36 +00009740 // We need to produce a map clause even if we don't have variables so that
9741 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009742 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009743 MapTypeModifier, MapType, IsMapTypeImplicit,
9744 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009745}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009746
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00009747QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
9748 TypeResult ParsedType) {
9749 assert(ParsedType.isUsable());
9750
9751 QualType ReductionType = GetTypeFromParser(ParsedType.get());
9752 if (ReductionType.isNull())
9753 return QualType();
9754
9755 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
9756 // A type name in a declare reduction directive cannot be a function type, an
9757 // array type, a reference type, or a type qualified with const, volatile or
9758 // restrict.
9759 if (ReductionType.hasQualifiers()) {
9760 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
9761 return QualType();
9762 }
9763
9764 if (ReductionType->isFunctionType()) {
9765 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
9766 return QualType();
9767 }
9768 if (ReductionType->isReferenceType()) {
9769 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
9770 return QualType();
9771 }
9772 if (ReductionType->isArrayType()) {
9773 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
9774 return QualType();
9775 }
9776 return ReductionType;
9777}
9778
9779Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
9780 Scope *S, DeclContext *DC, DeclarationName Name,
9781 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
9782 AccessSpecifier AS, Decl *PrevDeclInScope) {
9783 SmallVector<Decl *, 8> Decls;
9784 Decls.reserve(ReductionTypes.size());
9785
9786 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
9787 ForRedeclaration);
9788 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
9789 // A reduction-identifier may not be re-declared in the current scope for the
9790 // same type or for a type that is compatible according to the base language
9791 // rules.
9792 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
9793 OMPDeclareReductionDecl *PrevDRD = nullptr;
9794 bool InCompoundScope = true;
9795 if (S != nullptr) {
9796 // Find previous declaration with the same name not referenced in other
9797 // declarations.
9798 FunctionScopeInfo *ParentFn = getEnclosingFunction();
9799 InCompoundScope =
9800 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
9801 LookupName(Lookup, S);
9802 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
9803 /*AllowInlineNamespace=*/false);
9804 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
9805 auto Filter = Lookup.makeFilter();
9806 while (Filter.hasNext()) {
9807 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
9808 if (InCompoundScope) {
9809 auto I = UsedAsPrevious.find(PrevDecl);
9810 if (I == UsedAsPrevious.end())
9811 UsedAsPrevious[PrevDecl] = false;
9812 if (auto *D = PrevDecl->getPrevDeclInScope())
9813 UsedAsPrevious[D] = true;
9814 }
9815 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
9816 PrevDecl->getLocation();
9817 }
9818 Filter.done();
9819 if (InCompoundScope) {
9820 for (auto &PrevData : UsedAsPrevious) {
9821 if (!PrevData.second) {
9822 PrevDRD = PrevData.first;
9823 break;
9824 }
9825 }
9826 }
9827 } else if (PrevDeclInScope != nullptr) {
9828 auto *PrevDRDInScope = PrevDRD =
9829 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
9830 do {
9831 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
9832 PrevDRDInScope->getLocation();
9833 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
9834 } while (PrevDRDInScope != nullptr);
9835 }
9836 for (auto &TyData : ReductionTypes) {
9837 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
9838 bool Invalid = false;
9839 if (I != PreviousRedeclTypes.end()) {
9840 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
9841 << TyData.first;
9842 Diag(I->second, diag::note_previous_definition);
9843 Invalid = true;
9844 }
9845 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
9846 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
9847 Name, TyData.first, PrevDRD);
9848 DC->addDecl(DRD);
9849 DRD->setAccess(AS);
9850 Decls.push_back(DRD);
9851 if (Invalid)
9852 DRD->setInvalidDecl();
9853 else
9854 PrevDRD = DRD;
9855 }
9856
9857 return DeclGroupPtrTy::make(
9858 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
9859}
9860
9861void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
9862 auto *DRD = cast<OMPDeclareReductionDecl>(D);
9863
9864 // Enter new function scope.
9865 PushFunctionScope();
9866 getCurFunction()->setHasBranchProtectedScope();
9867 getCurFunction()->setHasOMPDeclareReductionCombiner();
9868
9869 if (S != nullptr)
9870 PushDeclContext(S, DRD);
9871 else
9872 CurContext = DRD;
9873
9874 PushExpressionEvaluationContext(PotentiallyEvaluated);
9875
9876 QualType ReductionType = DRD->getType();
9877 // Create 'T omp_in;' implicit param.
9878 auto *OmpInParm =
9879 ImplicitParamDecl::Create(Context, DRD, D->getLocation(),
9880 &Context.Idents.get("omp_in"), ReductionType);
9881 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
9882 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
9883 // uses semantics of argument handles by value, but it should be passed by
9884 // reference. C lang does not support references, so pass all parameters as
9885 // pointers.
9886 // Create 'T omp_out;' variable.
9887 auto *OmpOutParm =
9888 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
9889 if (S != nullptr) {
9890 PushOnScopeChains(OmpInParm, S);
9891 PushOnScopeChains(OmpOutParm, S);
9892 } else {
9893 DRD->addDecl(OmpInParm);
9894 DRD->addDecl(OmpOutParm);
9895 }
9896}
9897
9898void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
9899 auto *DRD = cast<OMPDeclareReductionDecl>(D);
9900 DiscardCleanupsInEvaluationContext();
9901 PopExpressionEvaluationContext();
9902
9903 PopDeclContext();
9904 PopFunctionScopeInfo();
9905
9906 if (Combiner != nullptr)
9907 DRD->setCombiner(Combiner);
9908 else
9909 DRD->setInvalidDecl();
9910}
9911
9912void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
9913 auto *DRD = cast<OMPDeclareReductionDecl>(D);
9914
9915 // Enter new function scope.
9916 PushFunctionScope();
9917 getCurFunction()->setHasBranchProtectedScope();
9918
9919 if (S != nullptr)
9920 PushDeclContext(S, DRD);
9921 else
9922 CurContext = DRD;
9923
9924 PushExpressionEvaluationContext(PotentiallyEvaluated);
9925
9926 QualType ReductionType = DRD->getType();
9927 // Create 'T omp_orig;' implicit param.
9928 auto *OmpOrigParm =
9929 ImplicitParamDecl::Create(Context, DRD, D->getLocation(),
9930 &Context.Idents.get("omp_orig"), ReductionType);
9931 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
9932 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
9933 // uses semantics of argument handles by value, but it should be passed by
9934 // reference. C lang does not support references, so pass all parameters as
9935 // pointers.
9936 // Create 'T omp_priv;' variable.
9937 auto *OmpPrivParm =
9938 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
9939 if (S != nullptr) {
9940 PushOnScopeChains(OmpPrivParm, S);
9941 PushOnScopeChains(OmpOrigParm, S);
9942 } else {
9943 DRD->addDecl(OmpPrivParm);
9944 DRD->addDecl(OmpOrigParm);
9945 }
9946}
9947
9948void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
9949 Expr *Initializer) {
9950 auto *DRD = cast<OMPDeclareReductionDecl>(D);
9951 DiscardCleanupsInEvaluationContext();
9952 PopExpressionEvaluationContext();
9953
9954 PopDeclContext();
9955 PopFunctionScopeInfo();
9956
9957 if (Initializer != nullptr)
9958 DRD->setInitializer(Initializer);
9959 else
9960 DRD->setInvalidDecl();
9961}
9962
9963Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
9964 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
9965 for (auto *D : DeclReductions.get()) {
9966 if (IsValid) {
9967 auto *DRD = cast<OMPDeclareReductionDecl>(D);
9968 if (S != nullptr)
9969 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
9970 } else
9971 D->setInvalidDecl();
9972 }
9973 return DeclReductions;
9974}
9975
Kelvin Li099bb8c2015-11-24 20:50:12 +00009976OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9977 SourceLocation StartLoc,
9978 SourceLocation LParenLoc,
9979 SourceLocation EndLoc) {
9980 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009981
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009982 // OpenMP [teams Constrcut, Restrictions]
9983 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009984 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9985 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009986 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009987
9988 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9989}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009990
9991OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9992 SourceLocation StartLoc,
9993 SourceLocation LParenLoc,
9994 SourceLocation EndLoc) {
9995 Expr *ValExpr = ThreadLimit;
9996
9997 // OpenMP [teams Constrcut, Restrictions]
9998 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009999 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10000 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010001 return nullptr;
10002
10003 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10004 EndLoc);
10005}
Alexey Bataeva0569352015-12-01 10:17:31 +000010006
10007OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10008 SourceLocation StartLoc,
10009 SourceLocation LParenLoc,
10010 SourceLocation EndLoc) {
10011 Expr *ValExpr = Priority;
10012
10013 // OpenMP [2.9.1, task Constrcut]
10014 // The priority-value is a non-negative numerical scalar expression.
10015 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10016 /*StrictlyPositive=*/false))
10017 return nullptr;
10018
10019 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10020}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010021
10022OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10023 SourceLocation StartLoc,
10024 SourceLocation LParenLoc,
10025 SourceLocation EndLoc) {
10026 Expr *ValExpr = Grainsize;
10027
10028 // OpenMP [2.9.2, taskloop Constrcut]
10029 // The parameter of the grainsize clause must be a positive integer
10030 // expression.
10031 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10032 /*StrictlyPositive=*/true))
10033 return nullptr;
10034
10035 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10036}
Alexey Bataev382967a2015-12-08 12:06:20 +000010037
10038OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10039 SourceLocation StartLoc,
10040 SourceLocation LParenLoc,
10041 SourceLocation EndLoc) {
10042 Expr *ValExpr = NumTasks;
10043
10044 // OpenMP [2.9.2, taskloop Constrcut]
10045 // The parameter of the num_tasks clause must be a positive integer
10046 // expression.
10047 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10048 /*StrictlyPositive=*/true))
10049 return nullptr;
10050
10051 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10052}
10053
Alexey Bataev28c75412015-12-15 08:19:24 +000010054OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10055 SourceLocation LParenLoc,
10056 SourceLocation EndLoc) {
10057 // OpenMP [2.13.2, critical construct, Description]
10058 // ... where hint-expression is an integer constant expression that evaluates
10059 // to a valid lock hint.
10060 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10061 if (HintExpr.isInvalid())
10062 return nullptr;
10063 return new (Context)
10064 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10065}
10066
Carlo Bertollib4adf552016-01-15 18:50:31 +000010067OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10068 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10069 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10070 SourceLocation EndLoc) {
10071 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10072 std::string Values;
10073 Values += "'";
10074 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10075 Values += "'";
10076 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10077 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10078 return nullptr;
10079 }
10080 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010081 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010082 if (ChunkSize) {
10083 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10084 !ChunkSize->isInstantiationDependent() &&
10085 !ChunkSize->containsUnexpandedParameterPack()) {
10086 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10087 ExprResult Val =
10088 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10089 if (Val.isInvalid())
10090 return nullptr;
10091
10092 ValExpr = Val.get();
10093
10094 // OpenMP [2.7.1, Restrictions]
10095 // chunk_size must be a loop invariant integer expression with a positive
10096 // value.
10097 llvm::APSInt Result;
10098 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10099 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10100 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10101 << "dist_schedule" << ChunkSize->getSourceRange();
10102 return nullptr;
10103 }
10104 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010105 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +000010106 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
10107 HelperValStmt =
10108 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
10109 /*NumDecls=*/1),
10110 SourceLocation(), SourceLocation());
10111 ValExpr = DefaultLvalueConversion(ValExpr).get();
Carlo Bertollib4adf552016-01-15 18:50:31 +000010112 }
10113 }
10114 }
10115
10116 return new (Context)
10117 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010118 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010119}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010120
10121OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10122 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10123 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10124 SourceLocation KindLoc, SourceLocation EndLoc) {
10125 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10126 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10127 Kind != OMPC_DEFAULTMAP_scalar) {
10128 std::string Value;
10129 SourceLocation Loc;
10130 Value += "'";
10131 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10132 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10133 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10134 Loc = MLoc;
10135 } else {
10136 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10137 OMPC_DEFAULTMAP_scalar);
10138 Loc = KindLoc;
10139 }
10140 Value += "'";
10141 Diag(Loc, diag::err_omp_unexpected_clause_value)
10142 << Value << getOpenMPClauseName(OMPC_defaultmap);
10143 return nullptr;
10144 }
10145
10146 return new (Context)
10147 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10148}